From patchwork Mon Mar 27 21:15:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13190089 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 371C5C76195 for ; Mon, 27 Mar 2023 21:16:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B6C2900005; Mon, 27 Mar 2023 17:16:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9677B900002; Mon, 27 Mar 2023 17:16:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 74429900005; Mon, 27 Mar 2023 17:16:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5CFC9900002 for ; Mon, 27 Mar 2023 17:16:00 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 258BB4084E for ; Mon, 27 Mar 2023 21:16:00 +0000 (UTC) X-FDA: 80615935680.07.7041D12 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) by imf17.hostedemail.com (Postfix) with ESMTP id 511AE4001C for ; Mon, 27 Mar 2023 21:15:58 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=HylNha+8; spf=pass (imf17.hostedemail.com: domain of 3jQciZAgKCEwxwo4wCo1u22uzs.q20zw18B-00y9oqy.25u@flex--jiaqiyan.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3jQciZAgKCEwxwo4wCo1u22uzs.q20zw18B-00y9oqy.25u@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679951758; a=rsa-sha256; cv=none; b=uFIx97KmGH2jrll4OS3156M84zWfuF4yE5pb6tOUuyDeGGwdgFztXBX27SGI36kDPzVhIZ Qt9qE428JhbFSDdRdMYzzAfaqPiRNa4XK+3kG1+lN8RxBqR9SgRUz6qpKgZNihRMW+urhX P8ohmXCwfcRoN1HN87jiUGR9tYn/PBA= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=HylNha+8; spf=pass (imf17.hostedemail.com: domain of 3jQciZAgKCEwxwo4wCo1u22uzs.q20zw18B-00y9oqy.25u@flex--jiaqiyan.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3jQciZAgKCEwxwo4wCo1u22uzs.q20zw18B-00y9oqy.25u@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=1679951758; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=MqyuxLW2kXP6Ga2JVdfW9yCiE/P5K6gIQXlw5p9VGh0=; b=xAN6Dx6ppDO7EOuGrG5eGb7C29G+mL1a8LeqQOLHtkAFCAyYOCTo1NbRMeSNCCZ3GexYJm POHAFlfjBbjagrsCTAQg1NkzyHyv9Pg+rgIfTau3c6VPzwkdMd7gWiWBfLyQnnlELSo6VX UhaQ7eYncbqxgb9PGJb+4A5I698VQn8= Received: by mail-pf1-f202.google.com with SMTP id d4-20020a056a00198400b00628000145bcso4801591pfl.0 for ; Mon, 27 Mar 2023 14:15:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679951757; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=MqyuxLW2kXP6Ga2JVdfW9yCiE/P5K6gIQXlw5p9VGh0=; b=HylNha+8Y8ZEDY7M/u1Ej07Pt4dh9rMU5YvvQdzClv16N8R9ehRsgsQJvWRVI/6el+ clPKbViN7WvbtPSI7RaREabFMxja94Xxefi6OFok1siC6Vfrxc7kVjSCJzVHSOXtCm/x cn3LPaesXPv5hO2oJCvBFNPYtJO8LgcVHbQ+BdvxZXOMJ5lif7eAHFDWTDdpYPtkia/7 xxaUZNLUC9eruRMP/M3YVh6DFWsV3tcwXF4rmylRnhLk7cCUOMEbJZUpHqS3EKFXBSqY 3vQI1wcb5atcsr+HiFWrKgOHaM8AJHfEoFiH8VEV5Opj8zQTiV7mPR0aD+diOGYr+lDZ weow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679951757; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MqyuxLW2kXP6Ga2JVdfW9yCiE/P5K6gIQXlw5p9VGh0=; b=LQdOxJFYWpTxIRAZU8JDCLjoI5d6EVzWOQHZY3fQ42kbc7QOLPWGwLLz8DL4TAagx2 3vailPHU4Eel4AcP0D3exb9OaQwqUq8CcZWvjQZ0Zy0XJvz5uaM5ap2K8R68con903Ch Hq/A2yjf3C6kCoagivI7bgzu1SKMqdojd5DuIGj2uJcoTzUMDrzuBUn84OR7C+Lr+6pY CdUjKpYTfnl7wdX72A2fYuPisuLD0BCDmnoTslmWFMzxzMugdrQqJgyBmQHsJpHTKL8G W7hxm3wMU8mG4YCwEtPFUuFwhK/IawZeEqw404tjdQqDU1ajQ0ZSeW6BfbHG7m0FecjI jIHQ== X-Gm-Message-State: AAQBX9ee39FnzKUS6j1Ed5MxrA+Kj6ww4+1IXfHQX2iqS4xlfx/jz47H 4Di5rAo/sXJY5kRfXGTimEn2DNQbjCzEgw== X-Google-Smtp-Source: AKy350aqZiKW45DpLdxGdpAIesp01xJcl5NN4Z4odQg2RNyzQPEn1ASMkIUjNO5tLYrJIqgGhlVmG1kKA6iYtA== X-Received: from yjqkernel.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1837]) (user=jiaqiyan job=sendgmr) by 2002:a05:6a00:8c7:b0:625:cda5:c28c with SMTP id s7-20020a056a0008c700b00625cda5c28cmr6422119pfu.6.1679951757244; Mon, 27 Mar 2023 14:15:57 -0700 (PDT) Date: Mon, 27 Mar 2023 14:15:46 -0700 In-Reply-To: <20230327211548.462509-1-jiaqiyan@google.com> Mime-Version: 1.0 References: <20230327211548.462509-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230327211548.462509-2-jiaqiyan@google.com> Subject: [PATCH v11 1/3] mm/khugepaged: recover from poisoned anonymous memory 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-Rspam-User: X-Rspamd-Queue-Id: 511AE4001C X-Rspamd-Server: rspam01 X-Stat-Signature: 75heqodupwn15upksznkyykh7jeo37r4 X-HE-Tag: 1679951758-870464 X-HE-Meta: U2FsdGVkX1/+n5y/9nPHTUOmowmj9aWfcwERdZ3Ex0NUqegysryxfxdlJ8Z/5ZPZP5yvcAjEVy2N/cW1CRKHF2BuqO04wgL4+ZwpqXZu+PtSSPZetIpzKXq1Bg5UzmkX/+k8C3vjMKhCb0o1yQ6GLdeZPhpZSlqLcnUwlf50WorkE5RmWrJcvTuJdpyBLgXuFPUO/RhEiQNTW0RmZx+ntHvoXS25QA/jc66IEOghWQbzUQjAZggOQ7EotBEXKHcC21KOEQ1cesFB6hbQU3EqYLvcmYgNL3/7fC/VUM8omZqLj+dSfdG7B9NsC0zoH40FrA4uO+AY/NLFGfS+haBwGeJHpMYosoOxlrUVYJNEvdK5Qz35lGqcOiJeudizW35pomnc7Ndyumdv8vNIN1JxjetvZ/dRGdJSImNQC4TVzflsB9kLG5DQrJfTyLg7llbxRYv9hxI0I9LLEiV3q5VNRFuSiT9bom8nW5E8oeXdUxOCH+5geT6feLB8MRfK3iVHfBYdRwWn/LsW2O7VYqYJXNQgm9ntAS9sgUhfbiXR2DcYS8qrk3mT6Vb7RT8VjtCNJOSCSzy2elLFm6HmX9zBAza783DVP2X4cy7loA0hT/QNs+eAMw6VkB27Ki9pVoU7SpZtP2T78RDqeepIJWo781C2qzZLWS/m7y7B2wY3rc6WN32nitI7gh8rqpONCs9Giw4IcjheqPN1GSgkCdv9z2Wo7erF5ZQ9H0D+jCm/oxXTFSrWxxrXcQB2JNz7HDAd6bvamJa/rUKcJbdfTZrXAiVEzwv61OLgOtOdDSaNbaDfmyS9fQTaFCa7VaLFHuPE6b1zrpvRrIknqPBgaU3EQR1aJT+gUtA7Qh7z9vCnNu5AIkdDpKa0Hq/e9kT1wiZiEZxvQ47zO5hLpyS0DIvRFedeu/wkZgInGg2Fiqdiq3cN4aY7sjUWbb3GXygrI7sELoHt//7KthyZNIEwslD 7EmalwLw +i9bZTmVl34PXYqEWWTWxWAP4+lrYgAkUTWU7F7v8Zxa8/0AcV51BvyIA0rYZlkfVZJBkYFLAwyrYXOudoZsTzbPbno4W3kpfp3RvdM/wqL38hXQdmlFm8CpuOj93QBGSJjtN5GjvCjfDFFr2QH0JhsX3cGQhXs0rur0F05g+Ttyv9hUMXdgfVLSl2x4juYWQoKZlBNii4zI9uXLIYzPXfi/p/xJi047jVIw/t16q4uWRUpK2RyOXq6OPBXBPh4O8dFU+XxpRPqsHKcqJzR2Ki6u0o28Gff+AvWhv/uh/9095GlPeRvUkgOyeW9vNQG5KoZ6l+wlqrHRe/0SgvIp4FF5nH6wWqhwTyPqCHYM02go1nbB3uAueHugs+6uw1coNrRaTbm9Zkg488UYi4FC09R5oU9tIuXIqUsdGu4l6c/67TQrhmia0D6JVsg8J7ANOaV2xHEK0obUaA9TIkzemEx//UUCEkTF3Xv86XtM7JDVi+gQNU5CDCGcKcZOnqusDMHJwN5nH4FkcZ9ev8ImGgOeVouDPRlNbu6Qb0+j2Z9ExuTmdluBpK9lGjXauAb1exE9110GyToGRbPJDdEXhl0HK3rCWjkZJMa0X5h5x+RbuUU551GIlNyMkbHsyf3a2nyY3Ej/mZ+w8lEw= 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: Make __collapse_huge_page_copy return whether copying anonymous pages succeeded, and make collapse_huge_page handle the return status. Break existing PTE scan loop into two for-loops. The first loop copies source pages into target huge page, and can fail gracefully when running into memory errors in source pages. If copying all pages succeeds, the second loop releases and clears up these normal pages. Otherwise, the second loop rolls back the page table and page states by: - re-establishing the original PTEs-to-PMD connection. - releasing source pages back to their LRU list. Tested manually: 0. Enable khugepaged on system under test. 1. Start a two-thread application. Each thread allocates a chunk of non-huge anonymous memory buffer. 2. Pick 4 random buffer locations (2 in each thread) and inject uncorrectable memory errors at corresponding physical addresses. 3. Signal both threads to make their memory buffer collapsible, i.e. calling madvise(MADV_HUGEPAGE). 4. Wait and check kernel log: khugepaged is able to recover from poisoned pages and skips collapsing them. 5. Signal both threads to inspect their buffer contents and make sure no data corruption. Signed-off-by: Jiaqi Yan Reviewed-by: Yang Shi --- include/trace/events/huge_memory.h | 3 +- mm/khugepaged.c | 114 +++++++++++++++++++++++++---- 2 files changed, 103 insertions(+), 14 deletions(-) diff --git a/include/trace/events/huge_memory.h b/include/trace/events/huge_memory.h index 3e6fb05852f9a..46cce509957ba 100644 --- a/include/trace/events/huge_memory.h +++ b/include/trace/events/huge_memory.h @@ -36,7 +36,8 @@ EM( SCAN_ALLOC_HUGE_PAGE_FAIL, "alloc_huge_page_failed") \ EM( SCAN_CGROUP_CHARGE_FAIL, "ccgroup_charge_failed") \ EM( SCAN_TRUNCATED, "truncated") \ - EMe(SCAN_PAGE_HAS_PRIVATE, "page_has_private") \ + EM( SCAN_PAGE_HAS_PRIVATE, "page_has_private") \ + EMe(SCAN_COPY_MC, "copy_poisoned_page") \ #undef EM #undef EMe diff --git a/mm/khugepaged.c b/mm/khugepaged.c index bee7fd7db380a..bef68286345c8 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -55,6 +55,7 @@ enum scan_result { SCAN_CGROUP_CHARGE_FAIL, SCAN_TRUNCATED, SCAN_PAGE_HAS_PRIVATE, + SCAN_COPY_MC, }; #define CREATE_TRACE_POINTS @@ -681,20 +682,22 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma, return result; } -static void __collapse_huge_page_copy(pte_t *pte, struct page *page, - struct vm_area_struct *vma, - unsigned long address, - spinlock_t *ptl, - struct list_head *compound_pagelist) +static void __collapse_huge_page_copy_succeeded(pte_t *pte, + pmd_t *pmd, + struct vm_area_struct *vma, + unsigned long address, + spinlock_t *ptl, + struct list_head *compound_pagelist) { - struct page *src_page, *tmp; + struct page *src_page; + struct page *tmp; pte_t *_pte; - for (_pte = pte; _pte < pte + HPAGE_PMD_NR; - _pte++, page++, address += PAGE_SIZE) { - pte_t pteval = *_pte; + pte_t pteval; + for (_pte = pte; _pte < pte + HPAGE_PMD_NR; + _pte++, address += PAGE_SIZE) { + pteval = *_pte; if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { - clear_user_highpage(page, address); add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1); if (is_zero_pfn(pte_pfn(pteval))) { /* @@ -706,7 +709,6 @@ static void __collapse_huge_page_copy(pte_t *pte, struct page *page, } } else { src_page = pte_page(pteval); - copy_user_highpage(page, src_page, address, vma); if (!PageCompound(src_page)) release_pte_page(src_page); /* @@ -733,6 +735,88 @@ static void __collapse_huge_page_copy(pte_t *pte, struct page *page, } } +static void __collapse_huge_page_copy_failed(pte_t *pte, + pmd_t *pmd, + pmd_t orig_pmd, + struct vm_area_struct *vma, + unsigned long address, + struct list_head *compound_pagelist) +{ + spinlock_t *pmd_ptl; + + /* + * Re-establish the PMD to point to the original page table + * entry. Restoring PMD needs to be done prior to releasing + * pages. Since pages are still isolated and locked here, + * acquiring anon_vma_lock_write is unnecessary. + */ + pmd_ptl = pmd_lock(vma->vm_mm, pmd); + pmd_populate(vma->vm_mm, pmd, pmd_pgtable(orig_pmd)); + spin_unlock(pmd_ptl); + /* + * Release both raw and compound pages isolated + * in __collapse_huge_page_isolate. + */ + release_pte_pages(pte, pte + HPAGE_PMD_NR, compound_pagelist); +} + +/* + * __collapse_huge_page_copy - attempts to copy memory contents from raw + * pages to a hugepage. Cleans up the raw pages if copying succeeds; + * otherwise restores the original page table and releases isolated raw pages. + * Returns SCAN_SUCCEED if copying succeeds, otherwise returns SCAN_COPY_MC. + * + * @pte: starting of the PTEs to copy from + * @page: the new hugepage to copy contents to + * @pmd: pointer to the new hugepage's PMD + * @orig_pmd: the original raw pages' PMD + * @vma: the original raw pages' virtual memory area + * @address: starting address to copy + * @pte_ptl: lock on raw pages' PTEs + * @compound_pagelist: list that stores compound pages + */ +static int __collapse_huge_page_copy(pte_t *pte, + struct page *page, + pmd_t *pmd, + pmd_t orig_pmd, + struct vm_area_struct *vma, + unsigned long address, + spinlock_t *pte_ptl, + struct list_head *compound_pagelist) +{ + struct page *src_page; + pte_t *_pte; + pte_t pteval; + unsigned long _address; + int result = SCAN_SUCCEED; + + /* + * Copying pages' contents is subject to memory poison at any iteration. + */ + for (_pte = pte, _address = address; _pte < pte + HPAGE_PMD_NR; + _pte++, page++, _address += PAGE_SIZE) { + pteval = *_pte; + if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { + clear_user_highpage(page, _address); + continue; + } + src_page = pte_page(pteval); + if (copy_mc_user_highpage(page, src_page, _address, vma) > 0) { + result = SCAN_COPY_MC; + break; + } + } + + if (likely(result == SCAN_SUCCEED)) + __collapse_huge_page_copy_succeeded(pte, pmd, vma, address, + pte_ptl, compound_pagelist); + else + __collapse_huge_page_copy_failed(pte, pmd, orig_pmd, vma, + address, compound_pagelist); + + return result; +} + static void khugepaged_alloc_sleep(void) { DEFINE_WAIT(wait); @@ -1106,9 +1190,13 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, */ anon_vma_unlock_write(vma->anon_vma); - __collapse_huge_page_copy(pte, hpage, vma, address, pte_ptl, - &compound_pagelist); + result = __collapse_huge_page_copy(pte, hpage, pmd, _pmd, + vma, address, pte_ptl, + &compound_pagelist); pte_unmap(pte); + if (unlikely(result != SCAN_SUCCEED)) + goto out_up_write; + /* * spin_lock() below is not the equivalent of smp_wmb(), but * the smp_wmb() inside __SetPageUptodate() can be reused to From patchwork Mon Mar 27 21:15:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13190090 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 80A12C761A6 for ; Mon, 27 Mar 2023 21:16:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21728900006; Mon, 27 Mar 2023 17:16:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 154F3900002; Mon, 27 Mar 2023 17:16:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F3401900006; Mon, 27 Mar 2023 17:16:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E59EF900002 for ; Mon, 27 Mar 2023 17:16:01 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id B5DCB80878 for ; Mon, 27 Mar 2023 21:16:01 +0000 (UTC) X-FDA: 80615935722.14.AC2DCBC Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf11.hostedemail.com (Postfix) with ESMTP id D92BE4000C for ; Mon, 27 Mar 2023 21:15:59 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Y4FmrDl2; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3jwciZAgKCE4zyq6yEq3w44w1u.s421y3AD-220Bqs0.47w@flex--jiaqiyan.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3jwciZAgKCE4zyq6yEq3w44w1u.s421y3AD-220Bqs0.47w@flex--jiaqiyan.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679951759; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=h2MviYegIfxUChJ7qdoz4n2fEXetu9024Go8q9W59mM=; b=G7dEMkCnswoqArUXTNiIMLMbhqkL/LaFfEIKWJbeovDdPaOVSa8Na2OYc068mPaguGFVhw dG+Kszgtz5OycBuOT0SZ32K3Jrm/3uho5soUmkklGJk9kFFMRMhox6vdDtvbFCV4uAw/MI QJKqfFlHmFJC+WmKJVy56L3jVyDmJK4= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Y4FmrDl2; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3jwciZAgKCE4zyq6yEq3w44w1u.s421y3AD-220Bqs0.47w@flex--jiaqiyan.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3jwciZAgKCE4zyq6yEq3w44w1u.s421y3AD-220Bqs0.47w@flex--jiaqiyan.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679951759; a=rsa-sha256; cv=none; b=PLX/Ny1zxxydSq48leXg3hm8RwRb/SCoiuj2MZteWIo9ov/FVNGWJX8FMrrl94ap82RZd+ 1eDDB/wgTF1aJE0vL+Q2rQ0vu6xSuE73Yh3Itk3DZSuK7Ldb7ZrtEUCtCPvn7MkkdWLwQT 6zv9SUY9hoZR9M0WKT3rvf3b0IPZGK4= Received: by mail-yb1-f202.google.com with SMTP id 204-20020a250fd5000000b00b6d6655dc35so9919294ybp.6 for ; Mon, 27 Mar 2023 14:15:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679951759; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=h2MviYegIfxUChJ7qdoz4n2fEXetu9024Go8q9W59mM=; b=Y4FmrDl2OSVHfcKYRSiKQbvPj/0fdyQhs5fGoGQvI21fcmILo0hfEuJgLw7sJNZYvS v11Iq6Z+Pe2pmbxWNiXdGiawVWGdQsRVKBEDGW7NGVaPq9bodOmlxATHFaK3jlsMhkdi xVVA919sE/B+SEMTsNZtP4mhKxCGhmemmIEJlcyVhNOQhE7fbdXtZs0KkFrFCMxN0K/X YJT7ZYnCL/iD3MwKdm45/7/reY/bl7arSPP33NCC9tAzbal+KWSbd+/arXC/rVG34jOp Ox26m7WQPSJrPXhDsPVFeHpY8Rmj8aYPmCrQjA4z4QYSCVFuxNoT9qVXfFhu9zdiu6Jo Gmdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679951759; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=h2MviYegIfxUChJ7qdoz4n2fEXetu9024Go8q9W59mM=; b=R7So6s99KhktDckcgdWl4kl9SHwDHLaWlVeb5K9+GovwO20JGWJA6zLKRjlr0smnvF JcTL7POQuk4rJyXTYKS9XR4UDMkFbJ8XHnnojG/+K/Oq8nB7CUaXErMR+Up7zNwRFk36 E/LbUw2ZM6sJM9btdm2rlKJmIVmd14raOAIamdecrccf9Zc6vkyppKptd64pjpyvJh24 8CYS2CYQn5O6r0CkSFILjJaxBCdLbnlp9bYuFRKblu4D4xH73NnaNzRbITN5C03+W1Ns N1DULENozDttX/YecIQcvObDMCSnsDdsoShIqL1nzlJEVfFFYa/SlANYoN4RACxvbNq9 sO1g== X-Gm-Message-State: AAQBX9dPKrx34OP1SoKCaMYBAA2htLLNSqrzjdJ/TKkT1LRZ9IJd1405 Yy7Qg+aWHHQgdxKRmhon3kWXrwEUy9osSg== X-Google-Smtp-Source: AKy350adRLEahVa6I3F3rcYSbpMLjqEIosLNJllOLsKEYp4EZwPvVJrIcsmkKsdv2qMbXMpva65HopDiWKAT0Q== X-Received: from yjqkernel.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1837]) (user=jiaqiyan job=sendgmr) by 2002:a05:6902:70d:b0:b4c:9333:2a1 with SMTP id k13-20020a056902070d00b00b4c933302a1mr8311262ybt.10.1679951759017; Mon, 27 Mar 2023 14:15:59 -0700 (PDT) Date: Mon, 27 Mar 2023 14:15:47 -0700 In-Reply-To: <20230327211548.462509-1-jiaqiyan@google.com> Mime-Version: 1.0 References: <20230327211548.462509-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230327211548.462509-3-jiaqiyan@google.com> Subject: [PATCH v11 2/3] mm/hwpoison: introduce copy_mc_highpage 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-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: D92BE4000C X-Stat-Signature: bcfnc1nfya3mbpy39j1m1ek4uauajynd X-HE-Tag: 1679951759-836298 X-HE-Meta: U2FsdGVkX197cddPsj+VSB85bJmGmyy6/ZtxMxmLgSkgSswYyRqk3M5vvw1DiSpWG8vjmCSkoO89eemTnfTD20tq2BNzsqq4TYSBl8GfDrLRIt03+itHxYrO2ZWNbLFCRBeQBIwbxLY9m5Sv5IXGPIQqETRgu7iveyDXz5rlo5dDCTZhf3vDrwrLfhSTSj4Dd7C1DdNRxF3RhCzUDSFPHVmgzokNPyBU8+7CSYjLHO+SGPhSRde8SjgLyF/s13wVYyQe+u6wkgRje933RwyCz9aMrun3zWAMGqba4JWjyFRPAKboKRDffkQU7t8SwK6eYc8mRddWVBOxQ7oqAsCb4Cx9EWGE85CLxejDQJ05CoEoBSVEC531ZFAb7YJRZHpPlzUB+iFimtdlRM1fzLtJsjJ3/3AMn4HyRt5vBfbqSfMq0HB8EVb6wKFY2KcccV/Av7dknjyCPis+iahEOY/Io0yM377A7HRATXlYxuB86FCKsXxb2ufB+irOFKo0CsA+9Z2hUoxb1vSuiqG74dQgPowSY92stwZFuMQ3lQhHRDj5OIiCceoRJ5DD8UK8iqTYV2HDEKVZ6tEio5Dw/y5TIAcZbrQB434lKwCcaVPoZJq++MCRJEJoY+xSx88pXmTLjyOpxzdbf6YCFRVUUdK7f5duo2jpMQNu2Oc60A4jvx1ymxcEiE/xiNMCoets3JAee6fthQp0LAOJwyRI9iiolabbfbFTzbUPO9yCYnugUMHMVKk6atPlI0vW/LmqN6zpDo/kYlYuV0JDSEc1rzt01plx4FYPUojU3thhrtW9TyNzoVwNOKK9cq32DrpwMcwtmCJQaQ5NMzMQScfYfYSi81Cj/WSdfCGavslLJmrH0GF9RxZM3ucNXSv6XTxhX9kHZcmiB9VzUvBSKCbFVMBMnlMISAVvYvQSLLZTq0+T/C/DxPauPJUHZv6J6B88dCAD+Nzp/3FdfGo0BCp5eI0 Xv/ic9wn a5+gBJ9K3ulJgmq6V3X7eGcjD3913HKs1ElY12VOQrC4D89XAFH6hFNa0lgsJmYVXSe+u2KfFo5KRG9ieVM0E6uB/iebfNkhsmy9+MslnzTVn/hq3k54uRLbpOtwZD9F6JV7tFzminZCnEqVyWhJ93E/Xdvt4YDcw4iux3+W5i1zZSnpOtcfEDlGH1LV1M3z8CO+LTiYGFymJyavktIOxoGQ5lxP3xNEYMVna4t6G4mxjbD78/W8Z6Qehd1KERUHf0ElXchurqLru/mOXr8IWdcDN92Ek46wQYq/NxOjxSgK4kx0DaKNeeT4g2xTq87HepFlUsSYapMm7OUGh5W/MvQbhOvtuxUFjoWjkif+ENdAjPjP5HCSjr2osUvva4TEDFeGeB4UCTCOueO8g69A2reIZSx79X/meYl7mzIrlLk7RGJ+NJ9VxSnOnxarLj/5bZ2ipziA2nG/lGJc4RAsUereB3SWA1IIevuzQC4aoei3/DaFENbDJYIBSt1/AFasr4QSWzM6a2qp7yQVVa5RLTK4PoNi5PG0Gtrv4vT6TbnM28IuYaILWHYB4ugAeZTSFRYgFNkkg/1vRtfCFfq8pSqS9KcwHuEglIJQwfZGbj4cUf5MP/plLkb7FE8zO0VElcjwVo7Y7kNvB+rYwDDt8UQW+S5oyEdo6cyo16rALN/Y5f73gMGzIm6ALKQ== 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: Similar to how copy_mc_user_highpage is implemented for copy_user_highpage on #MC supported architecture, introduce the #MC handled version of copy_highpage. This helper has immediate usage when khugepaged wants to copy file-backed memory pages and tolerate #MC. Signed-off-by: Jiaqi Yan Reviewed-by: Yang Shi --- include/linux/highmem.h | 54 +++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/include/linux/highmem.h b/include/linux/highmem.h index 9c7cdaa3de8cd..4de1dbcd3ef64 100644 --- a/include/linux/highmem.h +++ b/include/linux/highmem.h @@ -315,7 +315,29 @@ static inline void copy_user_highpage(struct page *to, struct page *from, #endif +#ifndef __HAVE_ARCH_COPY_HIGHPAGE + +static inline void copy_highpage(struct page *to, struct page *from) +{ + char *vfrom, *vto; + + vfrom = kmap_local_page(from); + vto = kmap_local_page(to); + copy_page(vto, vfrom); + kmsan_copy_page_meta(to, from); + kunmap_local(vto); + kunmap_local(vfrom); +} + +#endif + #ifdef copy_mc_to_kernel +/* + * If architecture supports machine check exception handling, define the + * #MC versions of copy_user_highpage and copy_highpage. They copy a memory + * page with #MC in source page (@from) handled, and return the number + * of bytes not copied if there was a #MC, otherwise 0 for success. + */ static inline int copy_mc_user_highpage(struct page *to, struct page *from, unsigned long vaddr, struct vm_area_struct *vma) { @@ -332,29 +354,35 @@ static inline int copy_mc_user_highpage(struct page *to, struct page *from, return ret; } -#else -static inline int copy_mc_user_highpage(struct page *to, struct page *from, - unsigned long vaddr, struct vm_area_struct *vma) -{ - copy_user_highpage(to, from, vaddr, vma); - return 0; -} -#endif -#ifndef __HAVE_ARCH_COPY_HIGHPAGE - -static inline void copy_highpage(struct page *to, struct page *from) +static inline int copy_mc_highpage(struct page *to, struct page *from) { + unsigned long ret; char *vfrom, *vto; vfrom = kmap_local_page(from); vto = kmap_local_page(to); - copy_page(vto, vfrom); - kmsan_copy_page_meta(to, from); + ret = copy_mc_to_kernel(vto, vfrom, PAGE_SIZE); + if (!ret) + kmsan_copy_page_meta(to, from); kunmap_local(vto); kunmap_local(vfrom); + + return ret; +} +#else +static inline int copy_mc_user_highpage(struct page *to, struct page *from, + unsigned long vaddr, struct vm_area_struct *vma) +{ + copy_user_highpage(to, from, vaddr, vma); + return 0; } +static inline int copy_mc_highpage(struct page *to, struct page *from) +{ + copy_highpage(to, from); + return 0; +} #endif static inline void memcpy_page(struct page *dst_page, size_t dst_off, From patchwork Mon Mar 27 21:15:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13190091 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 EFC19C76195 for ; Mon, 27 Mar 2023 21:16:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 941BE900002; Mon, 27 Mar 2023 17:16:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F278280001; Mon, 27 Mar 2023 17:16:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B97B900007; Mon, 27 Mar 2023 17:16:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 6D97A900002 for ; Mon, 27 Mar 2023 17:16:04 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 3AF5BC0748 for ; Mon, 27 Mar 2023 21:16:04 +0000 (UTC) X-FDA: 80615935848.16.5968EB0 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) by imf28.hostedemail.com (Postfix) with ESMTP id B0327C001C for ; Mon, 27 Mar 2023 21:16:01 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=LS8xMIcj; spf=pass (imf28.hostedemail.com: domain of 3kAciZAgKCE80zr7zFr4x55x2v.t532z4BE-331Crt1.58x@flex--jiaqiyan.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3kAciZAgKCE80zr7zFr4x55x2v.t532z4BE-331Crt1.58x@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=1679951761; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=V6PL2SzOs50LrRx6GRjsy4y4hYbcVBBC5q6RTP2zkTI=; b=KYDFjP2i0Ah5qrlNlPc4MsoXTyWzhfB0Yk9L4aM8inNTVEGsshHrvSckLcR4arDWsH+xnj dd16vilGfxc1sNP8fpZkuV1oNI/bvnI1cKTr73YcFcQxkp3RmvO/Xfl2Jx7Djr1MmtMO4b gRPBZg1gMK7tSZSdv62udifqgNDrmwI= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=LS8xMIcj; spf=pass (imf28.hostedemail.com: domain of 3kAciZAgKCE80zr7zFr4x55x2v.t532z4BE-331Crt1.58x@flex--jiaqiyan.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3kAciZAgKCE80zr7zFr4x55x2v.t532z4BE-331Crt1.58x@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679951761; a=rsa-sha256; cv=none; b=KtNMF/cM+f2wWzjC2ua5M2zWCafOPzZcuMgvr17/LKmC1Fe74nCsSZPz5oQ9E3HuhAsMpr avKM8fu9C3dQ/PFLH7Yk5QpJOaoi52WNb1o9rSQMrkvOR4JN518v9TmlZndpkot7EPgZeL Q7Bbaly5sFLjp+4o/I9eFZZmhjEqp10= Received: by mail-pf1-f202.google.com with SMTP id i192-20020a6287c9000000b0062a43acb7faso4741616pfe.8 for ; Mon, 27 Mar 2023 14:16:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679951760; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=V6PL2SzOs50LrRx6GRjsy4y4hYbcVBBC5q6RTP2zkTI=; b=LS8xMIcjUcFBY99EVt7I4ZUphmOMxLQTbr52OHALPnngZ8KD+c5LRsGbKsLHCbD1+9 RWRq2rcH5esFMkfKHNEuoJBOxr1wcix2nr5pct+HQLG4KIXeev+k7hITbhecdVU231AE 0XBvMjMkTtACjFcwtrEfxwBZlEWPO2UYywa044d9gFZyZXtp/NDNlkomo7a17mxJizu9 e8lhu/NtGhQs2hTntY7IyQTQ6kXwPtpInBXjvH3TyzLnA5CqShvdVJDL/ypIAD3HE3SK oGk9fptyG9dFE43cesIc0sfgj3kEf44HC4LmhvYWabWgt0q5tor63YowHfXp3olpZM6J AeMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679951760; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=V6PL2SzOs50LrRx6GRjsy4y4hYbcVBBC5q6RTP2zkTI=; b=pM02olC7BCIP9n5i6BaOazGRH/Rx5m3vkIVHnyiAE6So9TkGw20j9SMywMAlZvXjc7 khCD54cytBYmpfN5DasMVH8IRklPaIt1YpwFqkPAf1+6aqigTkseprovehd14P2P6f06 OnNYwRjxh7Uo6MBmGFFjuz41KGr89leMn56YPPui71OLfPr3Fwx2FRbWt1OUxE99AOgL P0+CIj/YMEZd3+ZzP5FeKCCrbbAx6+0HqNzPlKRjeNq49B4nqTIALNIYnLt+PhnXu4u+ tBnznv3BnAtG0GExC2CD8BmxRV04qnYSmbbkfw7jTea7rELttpwEdJy98psGwCsx9wzS uR/w== X-Gm-Message-State: AAQBX9dyaJ7NpefEuIOVcA2TLygPvStZZyd0q2bd+9QEe3JN4BAMJDag CBG+kQT/+hNiXyD+bPJWScT9i0q2i4cTHQ== X-Google-Smtp-Source: AKy350bNAFJZoNSTPE5PD77ywPGNkFUJU/McoLWGIEcYCKgHOS8UkJZ4HEt5n7C075Z++xNGIiRR87evJCOGKg== X-Received: from yjqkernel.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1837]) (user=jiaqiyan job=sendgmr) by 2002:a17:90a:bd87:b0:23b:457b:a2a4 with SMTP id z7-20020a17090abd8700b0023b457ba2a4mr3955815pjr.2.1679951760720; Mon, 27 Mar 2023 14:16:00 -0700 (PDT) Date: Mon, 27 Mar 2023 14:15:48 -0700 In-Reply-To: <20230327211548.462509-1-jiaqiyan@google.com> Mime-Version: 1.0 References: <20230327211548.462509-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230327211548.462509-4-jiaqiyan@google.com> Subject: [PATCH v11 3/3] mm/khugepaged: recover from poisoned file-backed memory 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-Queue-Id: B0327C001C X-Stat-Signature: qpfeefi8fswx4s18pmns8kpztts1b58k X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1679951761-342389 X-HE-Meta: U2FsdGVkX1+n/brpsvMnkGHYt8oNiGpKSOq7My5XTJyl3hxkUIfnkJM3YmqBH//6EC8n4BhBwlqvuPTqCdvc/MUSOzxnlbd9nVonOOubrYO/NLf/n/V6htYnwhmZdKoZk7jDxr8r2MwL5Azyu70eyMg+iraQiHdhaJ+ScZWnAOgiDH+99h+8l3rO2vQJLF6rTmnyzs0BdfxE+Xm7vsIvHxeLUXvZhGWmqOpUiPg5TLX54+TSNCzU75Z2adcw15E/kZVXUgfGH4tOUaiC5+w3r+cW0WQYRBn2xV4uv0zgwkYtRZHs1NfFrNM+VgyXfdgocj3ags48zZs8KV3/402EZLoMFkirR7av3cfGpAILaQiy1cP84KxrjojVYJbVxgnKLlfwsTrQhPP2nJcvJIhfcL/KEx81JQC6mA1zaUJEQcP0L59fZIwe8r4KlImGYjNlTFzsQyLyrtvuGRiY9hAMfoEsV7ciCUWmrOPKF/spzSeBC5gEP7uIEuu1AIysLV1heKCOascMPyV/hXvBuiRgSJDteLVguanMzdF2+dBuc+LR0uXM97ccUo0FSiOn6LojdVbkRmJX12UIRFhfTkIrIqLtNhUC40X2/DQ7HucCzl1WE299DVqcd3cQgidY/7LqAMOvMbDG36sJJqiIWbhnrGwJIRT05XMODy521KE2sKQ2RaSeE4pAl42RVJ1nskprAVVd9joPC4qXwH/45nwzvkeANDu3K82Udq8brEzfqtqp18jOOE8Nf2Ao0ifQRNgB2JP5TveNZdO9QapH10eGrnVGM304s5bfxRNZL5SE6DYVkiHq8Kowg39t9k3ZWR4SPX+b2BLEeuj1tJ9j3TCt7KaqlhvLRyrZ3ABfIyY4PGJY6frgA1B4YMtSghZnaJarQJ6I+vUeKe5/WgOxhSP06ZCuFdDrqsxwXPZ6X87JFdJVImsEDzPD2nj2KEzUJibnn4s5Ex8i5/dRrm8HlZG ynXCy84c 7kkQrb9OTBm7BUJOKey+RGHAuHwFXRBA//UYkXBsclMKk9rRDodk4Qi8M7nAE43XBLDLWdhMukM1nLR0i+VhXCFM9ujlbOJM5Tqd/t4gzJt9+bulwrrFIdk8Xaa7d5i5EO/7lEl0mCCpjVDBp9qTPWv2ZTPz9MWQxZv0hk3q9WbY+IEXlptLh5Gg16eVMA8RRcdP6dbacvcpN+x0zXXZvAZqfFAC//PuZjeHNn+V9MJRuDu0Js/G2/6O1+a3uNQJpl1pKdhIfMp9f/IQf4dlSwRnxxD7s8X20pe3tQ4ypxd8pMUPAy42DOL1gycEycy6+XLPOY/HM/9tsqoAd83vNdFLKo/T2CFO50c+OIgZp7PqaEttJLxXlzh8nfa/wUdYV9dN4PlMeLknNFPsnAARIgLW6hUYYgbFL2ljdChsWoti0PAXRjFsAVMrEr+H4TOcFI++5+muhUkYqofZO9EcY64XpAprYfhgurdf8KefqkwwJa/y6W5yL8vs6F9gZ2lXFpbeVEDw8W1zd/gfgYSgtgWd0iD2+TslZqhbQ7HoqXCdK/BL2Hexheyxd0YJD7887/8/JpkMtHm7RWPbJbt/eA1vqFo4iIuBfXGvtnjfycGRuiR3d1SuTPl9EDtUJWIPOLzNoT24VUZfPAjE= 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: Make collapse_file roll back when copying pages failed. More concretely: - extract copying operations into a separate loop - postpone the updates for nr_none until both scanning and copying succeeded - postpone joining small xarray entries until both scanning and copying succeeded - postpone the update operations to NR_XXX_THPS until both scanning and copying succeeded - for non-SHMEM file, roll back filemap_nr_thps_inc if scan succeeded but copying failed Tested manually: 0. Enable khugepaged on system under test. Mount tmpfs at /mnt/ramdisk. 1. Start a two-thread application. Each thread allocates a chunk of non-huge memory buffer from /mnt/ramdisk. 2. Pick 4 random buffer address (2 in each thread) and inject uncorrectable memory errors at physical addresses. 3. Signal both threads to make their memory buffer collapsible, i.e. calling madvise(MADV_HUGEPAGE). 4. Wait and then check kernel log: khugepaged is able to recover from poisoned pages by skipping them. 5. Signal both threads to inspect their buffer contents and make sure no data corruption. Signed-off-by: Jiaqi Yan Reviewed-by: Yang Shi --- mm/khugepaged.c | 86 +++++++++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 32 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index bef68286345c8..38c1655ce0a9e 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1874,6 +1874,9 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, { struct address_space *mapping = file->f_mapping; struct page *hpage; + struct page *page; + struct page *tmp; + struct folio *folio; pgoff_t index = 0, end = start + HPAGE_PMD_NR; LIST_HEAD(pagelist); XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER); @@ -1918,8 +1921,7 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, xas_set(&xas, start); for (index = start; index < end; index++) { - struct page *page = xas_next(&xas); - struct folio *folio; + page = xas_next(&xas); VM_BUG_ON(index != xas.xa_index); if (is_shmem) { @@ -2099,12 +2101,8 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, put_page(page); goto xa_unlocked; } - nr = thp_nr_pages(hpage); - if (is_shmem) - __mod_lruvec_page_state(hpage, NR_SHMEM_THPS, nr); - else { - __mod_lruvec_page_state(hpage, NR_FILE_THPS, nr); + if (!is_shmem) { filemap_nr_thps_inc(mapping); /* * Paired with smp_mb() in do_dentry_open() to ensure @@ -2115,21 +2113,9 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, smp_mb(); if (inode_is_open_for_write(mapping->host)) { result = SCAN_FAIL; - __mod_lruvec_page_state(hpage, NR_FILE_THPS, -nr); filemap_nr_thps_dec(mapping); - goto xa_locked; } } - - if (nr_none) { - __mod_lruvec_page_state(hpage, NR_FILE_PAGES, nr_none); - /* nr_none is always 0 for non-shmem. */ - __mod_lruvec_page_state(hpage, NR_SHMEM, nr_none); - } - - /* Join all the small entries into a single multi-index entry */ - xas_set_order(&xas, start, HPAGE_PMD_ORDER); - xas_store(&xas, hpage); xa_locked: xas_unlock_irq(&xas); xa_unlocked: @@ -2142,21 +2128,36 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, try_to_unmap_flush(); if (result == SCAN_SUCCEED) { - struct page *page, *tmp; - struct folio *folio; - /* * Replacing old pages with new one has succeeded, now we - * need to copy the content and free the old pages. + * attempt to copy the contents. */ index = start; - list_for_each_entry_safe(page, tmp, &pagelist, lru) { + list_for_each_entry(page, &pagelist, lru) { while (index < page->index) { clear_highpage(hpage + (index % HPAGE_PMD_NR)); index++; } - copy_highpage(hpage + (page->index % HPAGE_PMD_NR), - page); + if (copy_mc_highpage(hpage + (page->index % HPAGE_PMD_NR), + page) > 0) { + result = SCAN_COPY_MC; + break; + } + index++; + } + while (result == SCAN_SUCCEED && index < end) { + clear_highpage(hpage + (index % HPAGE_PMD_NR)); + index++; + } + } + + nr = thp_nr_pages(hpage); + if (result == SCAN_SUCCEED) { + /* + * Copying old pages to huge one has succeeded, now we + * need to free the old pages. + */ + list_for_each_entry_safe(page, tmp, &pagelist, lru) { list_del(&page->lru); page->mapping = NULL; page_ref_unfreeze(page, 1); @@ -2164,12 +2165,23 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, ClearPageUnevictable(page); unlock_page(page); put_page(page); - index++; } - while (index < end) { - clear_highpage(hpage + (index % HPAGE_PMD_NR)); - index++; + + xas_lock_irq(&xas); + if (is_shmem) + __mod_lruvec_page_state(hpage, NR_SHMEM_THPS, nr); + else + __mod_lruvec_page_state(hpage, NR_FILE_THPS, nr); + + if (nr_none) { + __mod_lruvec_page_state(hpage, NR_FILE_PAGES, nr_none); + /* nr_none is always 0 for non-shmem. */ + __mod_lruvec_page_state(hpage, NR_SHMEM, nr_none); } + /* Join all the small entries into a single multi-index entry. */ + xas_set_order(&xas, start, HPAGE_PMD_ORDER); + xas_store(&xas, hpage); + xas_unlock_irq(&xas); folio = page_folio(hpage); folio_mark_uptodate(folio); @@ -2187,8 +2199,6 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, unlock_page(hpage); hpage = NULL; } else { - struct page *page; - /* Something went wrong: roll back page cache changes */ xas_lock_irq(&xas); if (nr_none) { @@ -2222,6 +2232,18 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, xas_lock_irq(&xas); } VM_BUG_ON(nr_none); + /* + * Undo the updates of filemap_nr_thps_inc for non-SHMEM + * file only. This undo is not needed unless failure is + * due to SCAN_COPY_MC. + * + * Paired with smp_mb() in do_dentry_open() to ensure the + * update to nr_thps is visible. + */ + smp_mb(); + if (!is_shmem && result == SCAN_COPY_MC) + filemap_nr_thps_dec(mapping); + xas_unlock_irq(&xas); hpage->mapping = NULL;