From patchwork Mon Mar 27 21:15:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13190088 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 978A9C6FD1D for ; Mon, 27 Mar 2023 21:15:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2631C900004; Mon, 27 Mar 2023 17:15:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 213D0900002; Mon, 27 Mar 2023 17:15:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0DC1E900004; Mon, 27 Mar 2023 17:15:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id F3421900002 for ; Mon, 27 Mar 2023 17:15:58 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C91B31C63E2 for ; Mon, 27 Mar 2023 21:15:58 +0000 (UTC) X-FDA: 80615935596.16.2F5C3B8 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) by imf08.hostedemail.com (Postfix) with ESMTP id 9F8DE16000D for ; Mon, 27 Mar 2023 21:15:56 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=oJeQtJ7x; spf=pass (imf08.hostedemail.com: domain of 3iwciZAgKCEovum2uAmzs00sxq.o0yxuz69-yyw7mow.03s@flex--jiaqiyan.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3iwciZAgKCEovum2uAmzs00sxq.o0yxuz69-yyw7mow.03s@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679951756; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=pKTT46injlqiRhAQowVmX4iRpwR5ATW5i0iS4y+F4Lo=; b=JGsA2GXTHMXE8/BeivtkAhF67h4pVY+rB+j8wh9fCYyXCzOZRqUmx2WBGuw1yvtgb2xyer KfRfZCqOQrydn5NJDk4hocDcyjQRUSWJnMfyqGAP9LFiHTBJ83i2axGpXFPgNpE6/wfrjQ 4P6/QYD47r5Sq7VgHaZQzwgZ/MJ9Ozo= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=oJeQtJ7x; spf=pass (imf08.hostedemail.com: domain of 3iwciZAgKCEovum2uAmzs00sxq.o0yxuz69-yyw7mow.03s@flex--jiaqiyan.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3iwciZAgKCEovum2uAmzs00sxq.o0yxuz69-yyw7mow.03s@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679951756; a=rsa-sha256; cv=none; b=3wygCw3k5KESwH9OAa0a75ysfqfm7PQKamEl04MfUaTKFLAR5crA/ddvn4u++5i16JNALA WdRdLM5LZmyY780pH6/aiIt56Ai8usArk01tnhf2JtcGb3gwOwdZ7W/UuIQQv301C8qpua QIB03093xxYwrOBF/Ewlu7F0DIVZRLE= Received: by mail-pf1-f202.google.com with SMTP id t67-20020a628146000000b0062d6d838243so2358190pfd.21 for ; Mon, 27 Mar 2023 14:15:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679951755; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:from:to:cc:subject:date:message-id:reply-to; bh=pKTT46injlqiRhAQowVmX4iRpwR5ATW5i0iS4y+F4Lo=; b=oJeQtJ7xE6tdC3yiMPCXRfm/PRRiTP4XUyjKHKkKlTM1y4+MEIeb9L1z5uqFbGYXxG gQIRyfAiq/2ha1EsztM/diOZuI+1UGvLM20xudBkuwxqbg6ZWlSgcHVtJCsHLuAF8DTo p2StCxHSbHnhX6be7MWo0QN2/4wSF4dE9TWR/LDxyh9+aO5naUQ+iMZF+zpsXmmAGHyy 09RxSzQZEHq3ik9kqtoNBTt7kAWoywLw1E/WKywqDZOkY+xkYKaAWPuY/mDgy7M0IWx5 Q8rGWV+Hiuyz2XL2u0J0GjKUO+B7uaSkIDQIbaXM3Uh/20vBHWV4WxEFZwo6le/htqQD pzOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679951755; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=pKTT46injlqiRhAQowVmX4iRpwR5ATW5i0iS4y+F4Lo=; b=ZqH/K3eVLmJp73BjX4epc5YLki4hg2AN58Ee31Xiz3CztpKzBEltZcIo0XtuyMtBxP UwNjRq88N99CpiOPCpKck8h9u120SDubNEIdv6REDHWQj3aGXJmIfW/lx1nIATcPaFA/ vgYo4snCM33E208jD+RqUpEWugw1gPo6sTYWPSb90C2aRjoiBUcBcriTehXJTH8UQFTx cMoLQS2g3hnFArgMezpqzJxR+4H3HeJmiHNu+TPiMVSIAX+r88PtccdUxwNnIhqKYYKs Mv44Njr0wp7328qoSVaK5vuFpz/b+hgYdi83jfqQMQUCce+0zhip/UaTHux0dA6hDawK 22sQ== X-Gm-Message-State: AAQBX9f1NP2oOM8GBfhZ7GHbjjEqt4zi1ufFofU+mZ3I20AbLZjj+Cqh YJkT1vv/yHQQBhQ8MKxXDnNOzLf8uCqETg== X-Google-Smtp-Source: AKy350bqX2R7AAO7pB8UakuQqNSq/jrlgvcNAqTv4phqXpNa5aaZXqxHq2UDIYtE4D440QP4kkWWF1RLebxbsw== X-Received: from yjqkernel.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1837]) (user=jiaqiyan job=sendgmr) by 2002:a05:6a00:10c2:b0:626:1eb8:31d7 with SMTP id d2-20020a056a0010c200b006261eb831d7mr6710075pfu.1.1679951755524; Mon, 27 Mar 2023 14:15:55 -0700 (PDT) Date: Mon, 27 Mar 2023 14:15:45 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230327211548.462509-1-jiaqiyan@google.com> Subject: [PATCH v11 0/3] Memory poison recovery in khugepaged collapsing From: Jiaqi Yan To: kirill.shutemov@linux.intel.com, kirill@shutemov.name, shy828301@gmail.com, tongtiangen@huawei.com, tony.luck@intel.com Cc: naoya.horiguchi@nec.com, linmiaohe@huawei.com, jiaqiyan@google.com, linux-mm@kvack.org, akpm@linux-foundation.org, osalvador@suse.de, wangkefeng.wang@huawei.com, stevensd@chromium.org, hughd@google.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 9F8DE16000D X-Stat-Signature: dk88aedphcmd1bt7byy8i15e8zgjxx1f X-Rspam-User: X-HE-Tag: 1679951756-297541 X-HE-Meta: U2FsdGVkX19DYxsxtgpWH7ugQ99cVDtyfdOG2FkZaUmZG+5hml/Ls/h4L8LXXZp3yibVH+Cpr8e5F4Hsw8Gin2MHWcVXgUXwLoSt2rc5GugddXyZx7bB+yCl706IWjjtsqHndcFPfg8503SqOo8xqCnyZrt4frw4LcgG4upNoBoYgtvqydNuneDQHNT68a7t4NY8oquYArBa+mT143XReTqBsfCrl5u3Q7CF0q8NFgWiZE++2ejt1+TVVuXzmsPTQ6l4NX8Z76eTt2xDwlcJrQCfcZ4Guu/gRFMgytf4gvQCJ04cly5LawKD+NCuj/IYTdaiW4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hiApin4u8OGJBhkbPmqJxHnax7Dr8tHwkQE/sVC0r80Pm64yBm8DXWbE0HIL2dG4wWrftN3O5pxV1QuTux6baj7muz0ySjui2hPQCrveB0t33665FLPEMJlU9vpncVp3v6Jw5YtxCUtVRwGER6+4WJiA11CWAEVXiq84kFbj8sMwX00pUgRxnyKQikiqgUdxkfTuuPbtN81WFq74Mtckq7xXpRdbbxTesKJBQZ4T9sk+SItvcYNqxw7YUgnGiKJYsaaS2SyHSwH/c78baTQK9e238VO2cIb6mJEtTTnEtiof8Rp18ruyrh1gNOcVp91WlZL9QTfF6p2zhuEKYn8rbmr6ewpoiXC6OO7EAwad8ZAh+PftwW9tuiNdm8qqQkdukk2zCiIPlLupekiXRDqV+y9kg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Problem ======= Memory DIMMs are subject to multi-bit flips, i.e. memory errors. As memory size and density increase, the chances of and number of memory errors increase. The increasing size and density of server RAM in the data center and cloud have shown increased uncorrectable memory errors. There are already mechanisms in the kernel to recover from uncorrectable memory errors. This series of patches provides the recovery mechanism for the particular kernel agent khugepaged when it collapses memory pages. Impact ====== The main reason we chose to make khugepaged collapsing tolerant of memory failures was its high possibility of accessing poisoned memory while performing functionally optional compaction actions. Standard applications typically don't have strict requirements on the size of its pages. So they are given 4K pages by the kernel. The kernel is able to improve application performance by either 1) giving applications 2M pages to begin with, or 2) collapsing 4K pages into 2M pages when possible. This collapsing operation is done by khugepaged, a kernel agent that is constantly scanning memory. When collapsing 4K pages into a 2M page, it must copy the data from the 4K pages into a physically contiguous 2M page. Therefore, as long as there exists one poisoned cache line in collapsible 4K pages, khugepaged will eventually access it. The current impact to users is a machine check exception triggered kernel panic. However, khugepaged’s compaction operations are not functionally required kernel actions. Therefore making khugepaged tolerant to poisoned memory will greatly improve user experience. This patch series is for cases where khugepaged is the first guy that detects the memory errors on the poisoned pages. IOW, the pages are not known to have memory errors when khugepaged collapsing gets to them. In our observation, this happens frequently when the huge page ratio of the system is relatively low, which is fairly common in virtual machines running on cloud. Solution ======== As stated before, it is less desirable to crash the system only because khugepaged accesses poisoned pages while it is collapsing 4K pages. The high level idea of this patch series is to skip the group of pages (usually 512 4K-size pages) once khugepaged finds one of them is poisoned, as these pages have become ineligible to be collapsed. We are also careful to unwind operations khuagepaged has performed before it detects memory failures. For example, before copying and collapsing a group of anonymous pages into a huge page, the source pages will be isolated and their page table is unlinked from their PMD. These operations need to be undone in order to ensure these pages are not changed/lost from the perspective of other threads (both user and kernel space). As for file backed memory pages, there already exists a rollback case. This patch just extends it so that khugepaged also correctly rolls back when it fails to copy poisoned 4K pages. Changelog ========= v11 changes - Incorporate feedbacks from Shi Yang and Hugh Dickins - Replace releasing pages for-loop with release_pte_pages in __collapse_huge_page_copy_failed. - Rename pte_ptl to ptl in __collapse_huge_page_copy_succeeded. - Fix a bug in __collapse_huge_page_copy_succeeded: ptep_clear should be used instead of pte_clear. - Drop _address in __collapse_huge_page_copy_succeeded. - Add smp_mb() before updating filemap_nr_thps_dec. - Move `nr = thp_nr_pages()` closer to its references. - Remove an unnecessary goto statement. - This revision is rebased to mm-unstable at commit b4e1277ee31db ("xtensa: reword ARCH_FORCE_MAX_ORDER prompt and help text") v10 changes - Incorporate feedbacks from Kirill A. Shutemov - Refactor the 2nd loop (after the loop for copying memory) into 2 helper functions, one for actions to take when copying succeeded, one for when copying failed due to #MC. - Use copy_mc_user_highpage for anonymous memory. - Introduce copy_mc_highpage and use it for file-backed memory. - Rename the original PMD from `rollback` to `orig_pmd`. - Some minor changes in comments, e.g. `normal page` to `raw page`. - This revision is rebased to mm-unstable at commit df3ae4347aff9 ("dma-buf: system_heap: avoid reclaim for order 4") v9 changes - Incorporate feedback from Andrew Morton - Move copy_mc_highpage into khugepage.c as a static out-of-line function copy_mc_page. v8 changes - Incorporate feedbacks from Tony Luck - Rename copy_highpage_mc to copy_mc_highpage. - Update copy_mc_highpage with kmsan changes. - Code style changes: 1) copy_mc_highpage returns int as "copy" is an action and is consistent with copy_mc_user_highpage. 2) __collapse_huge_page_copy returns scan_result(int) and is consistent with __collapse_huge_page_isolate/swapin. 3) variables are declared in separate lines in collapse_file. v7 changes - Fix a bug "KASAN: stack-out-of-bounds Read in collapse_file". After copying all pages into the huge page, clear_highpage should use index instead of page->index. v6 changes - Address comments from Kirill Shutemov - Rewrite __collapse_huge_page_copy to make rollback operations more clear to its reader. - Add detailed test steps in each commit message. v5 changes - Rebase patches to mm-unstable at commit ffb39098bf87 ("Merge tag 'linux-kselftest-kunit-6.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest"). - Resolves conflicts with: commit 2f55f070e5b8 ("mm/khugepaged: minor cleanup for collapse_file") commit 1baec203b77c ("mm/khugepaged: try to free transhuge swapcache when possible") v4 changes - Incorporate feedbacks from Yang Shi - Remove tracepoint for __collapse_huge_page_copy, just keep SCAN_COPY_MC and let trace_mm_collapse_huge_page it - Remove unnecessary comments v3 changes - Incorporate feedbacks from Yang Shi - Add tracepoint for __collapse_huge_page_copy - Restore PMD in collapse_huge_page - Correct comment about mmap_read_lock v2 changes - Incorporate feedbacks from Yang Shi - Only keep copy_highpage_mc - Adding new scan_result SCAN_COPY_MC - Defer NR_FILE_THPS update until copying succeeded Jiaqi Yan (3): mm/khugepaged: recover from poisoned anonymous memory mm/hwpoison: introduce copy_mc_highpage mm/khugepaged: recover from poisoned file-backed memory include/linux/highmem.h | 54 ++++++-- include/trace/events/huge_memory.h | 3 +- mm/khugepaged.c | 200 ++++++++++++++++++++++------- 3 files changed, 198 insertions(+), 59 deletions(-)