From patchwork Wed Mar 29 15:11:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13192690 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 86D75C761AF for ; Wed, 29 Mar 2023 15:11:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DAD6E900004; Wed, 29 Mar 2023 11:11:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D5BFB900002; Wed, 29 Mar 2023 11:11:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C23B9900004; Wed, 29 Mar 2023 11:11:30 -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 B407C900002 for ; Wed, 29 Mar 2023 11:11:30 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 08E1B1A0609 for ; Wed, 29 Mar 2023 15:11:30 +0000 (UTC) X-FDA: 80622274740.20.A8D6036 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf19.hostedemail.com (Postfix) with ESMTP id 138F21A0018 for ; Wed, 29 Mar 2023 15:11:27 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=rIXXwFUI; spf=pass (imf19.hostedemail.com: domain of 3H1UkZAgKCIIpogwo4gtmuumrk.iusrot03-ssq1giq.uxm@flex--jiaqiyan.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3H1UkZAgKCIIpogwo4gtmuumrk.iusrot03-ssq1giq.uxm@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=1680102688; 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=Ky+wdshhLqyJAiHPRVddmE8oftGUteWHVoj9BXSbSfw=; b=NNOxY515kkaHOQzjoO1yPFdCQ7z5YLi/lbyi788LvVyj/EYAQ5YB/wg/dPHaopqdYma407 NykxIuDdwGwF9arztUSCPNFCZPyTNG6jHB7KZ0JustXIYR80ZoKcbkBEhHCtxuE4wc9Rnt MFLGhPnSuS1jkwic1VCTPqELwG9ALAI= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=rIXXwFUI; spf=pass (imf19.hostedemail.com: domain of 3H1UkZAgKCIIpogwo4gtmuumrk.iusrot03-ssq1giq.uxm@flex--jiaqiyan.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3H1UkZAgKCIIpogwo4gtmuumrk.iusrot03-ssq1giq.uxm@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680102688; a=rsa-sha256; cv=none; b=jHb9HZzkh56tF41fR/ujGw68PqiuEny9Qn4wXnCiaUsxx+ZRauDkusLge6ht0YiLB6k+5d GzDdWZDojs5Mzlr/Q9xEHVxNgZ7N7I3knGZkMbGEZz3UrjzFaFGKXOnfEMui5bOmpAPxtU 41BR/kOF8U6jgkC0w5Oso0GXo99vd9s= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-54574d6204aso157714857b3.15 for ; Wed, 29 Mar 2023 08:11:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680102687; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Ky+wdshhLqyJAiHPRVddmE8oftGUteWHVoj9BXSbSfw=; b=rIXXwFUI6eJ032lyoW1kidSd6RT2JO4l/1la76d/LckVnqUWT5EDLNULN+X10ncDKw 5HNo0KPppjwizpDk9ORg6UlyWk57QJakxLvs/XfESDj1/qlaNJjX0ATNLMiDfXjvCbb8 uPIkzmraLepCFt+E+gxMoc2e2wE1f+jrRRsmzsk0VCHH3FNBd339x52kaPY5fG75cEoX MLtAObOXZWdShFAnRQdyszu3LKOGsHoZq3XnfHXSxGI50cTEYQVJTZVrSJ9bRelfgnHF ZKEaL+NelDkSO3FUwy68Lm6uOvZqiPzexhHyS/Ci8D69u2f/ci0Tk2iSzxF4FXuCcM2k BC2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680102687; 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=Ky+wdshhLqyJAiHPRVddmE8oftGUteWHVoj9BXSbSfw=; b=Z/4FiEQtWVzoBNY0rCeYC8p9iPFgdIHLom4PTcJoHmq6pGxxCwWvd1rXf3UP59tx5D h9GUgcOS5wSg/SIcE/705oaQgRojlu76jAKv3mInSVoqPJ3Z7mUgfBnMHPxwtodOSXGJ 7vnUruAbdP1OPy66NFrQiCV3zFaqW8U8qT/HzQXElz3pl8rnMAT9Y2OfbWwlfc54RSnl NoZ90L58O34ra/1b6TxIETBwsTweEDwCyIIKR20CZiJj7UVXVdjjog0OP7gsCHML5eYL KGDQoC+uFF23nriUutfTn9LZtX/LyOPmNqS2ub0xHN5MIpi/Ng/Iii1Hc9fitqvLzAmQ xSYw== X-Gm-Message-State: AAQBX9cbUBWLW2oIujySRNCi4iDVRbZwMaJqzV9pxniftqNaksXE4A9j UGuQvI57m984WAokPjbupKq8n5mYAfu02A== X-Google-Smtp-Source: AKy350ayqDIcmr4ppXW9akRiYY4BDgZ2qFWuwxIcTx0bDUdihcLgb4G7WCcSvrK7dsqRKj5ySfX22m0JkY4hEg== X-Received: from yjqkernel.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1837]) (user=jiaqiyan job=sendgmr) by 2002:a05:6902:1507:b0:b78:8bd8:6e88 with SMTP id q7-20020a056902150700b00b788bd86e88mr11787681ybu.6.1680102687204; Wed, 29 Mar 2023 08:11:27 -0700 (PDT) Date: Wed, 29 Mar 2023 08:11:19 -0700 In-Reply-To: <20230329151121.949896-1-jiaqiyan@google.com> Mime-Version: 1.0 References: <20230329151121.949896-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230329151121.949896-2-jiaqiyan@google.com> Subject: [PATCH v12 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-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 138F21A0018 X-Rspam-User: X-Stat-Signature: th53opfk7hqs9io7mfgi6iczirkcfh7i X-HE-Tag: 1680102687-164661 X-HE-Meta: U2FsdGVkX18T8mb4KPZqaWFKiNhsmnqLj4qNnVYKX6KDboLp7dxTIrVs2raCFvAX7/yGl4kKnD8ckLrsxyoZ3N17+q5fuyQ+NOk6gGQqcEXskHjK3af3xKwwApAQBynpl9HcymDMDDOEzdEn/OywTNN2qiHd7PEObHW6BaYwIx1BKQvzfE19IL1NpUbUdyAkgOrevC7pvBoEMruYN7r+rYabCnuUt31ID85zFWGXmV+Cl9vFBuSwa7cLHRlWcy+SFv5/0yMk+lSzQ9iAnNy+ml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c2neerbiiQW0sVwoc1sRg6ezLEqbS04kyxmoBtaWIa1WxCn82QadhQp/j9M0r9d/lkDyc3kjNo0nr+h57rIQIQ/UK+ZXQgKTveGSswl+SG/Senh5SerjPjpsQni76daEQCi9B1uShjMcNx5aR1SViJfFkeioP3mB3N3vy4q8W5STIAQCrFa3BuN76tmcZyFJiowwtP5j1SSu5GeDs8DnmA2HrEAAgMkA5JzbDqELcuf6A19eackgGjKQFM0bk+d3ereI98If9OWj0hWZsjs= 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 --- include/trace/events/huge_memory.h | 3 +- mm/khugepaged.c | 112 +++++++++++++++++++++++++---- 2 files changed, 101 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..269df3f052e8e 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,21 @@ 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, + 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 +708,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 +734,87 @@ 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, + 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 + * @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 *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, vma, address, ptl, + compound_pagelist); + else + __collapse_huge_page_copy_failed(pte, pmd, orig_pmd, vma, + compound_pagelist); + + return result; +} + static void khugepaged_alloc_sleep(void) { DEFINE_WAIT(wait); @@ -1106,9 +1188,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 Wed Mar 29 15:11:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13192689 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 6CC5EC77B61 for ; Wed, 29 Mar 2023 15:11:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 01901900005; Wed, 29 Mar 2023 11:11:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EBD6F900002; Wed, 29 Mar 2023 11:11:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D0FC2900005; Wed, 29 Mar 2023 11:11:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BE015900002 for ; Wed, 29 Mar 2023 11:11:31 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 88C131C6264 for ; Wed, 29 Mar 2023 15:11:31 +0000 (UTC) X-FDA: 80622274782.30.01572D6 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf27.hostedemail.com (Postfix) with ESMTP id BE74C40007 for ; Wed, 29 Mar 2023 15:11:29 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=H4q+TwD9; spf=pass (imf27.hostedemail.com: domain of 3IFUkZAgKCIMqphxp5hunvvnsl.jvtspu14-ttr2hjr.vyn@flex--jiaqiyan.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3IFUkZAgKCIMqphxp5hunvvnsl.jvtspu14-ttr2hjr.vyn@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=1680102689; 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=4YhYo6B188EjYYv/dO1DqyMOiNYtHUIng4cDGkzB8Lk=; b=Z1DUx5s7C33TZtfAODGSF/Ny3qGMAMtl1/YFlCXuy/AKH9qJy7zGYseqL0NL1WfQPRWR33 I33VP0BXkBWZVpkSB91WoWtFk2LGhqDKb0P9H3jZw8O757dYkEWBf5yyOjTPpYUfKRtLQB Ihj6zWNI/PPNXpTMLbZIYAtyumdvAxc= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=H4q+TwD9; spf=pass (imf27.hostedemail.com: domain of 3IFUkZAgKCIMqphxp5hunvvnsl.jvtspu14-ttr2hjr.vyn@flex--jiaqiyan.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3IFUkZAgKCIMqphxp5hunvvnsl.jvtspu14-ttr2hjr.vyn@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680102689; a=rsa-sha256; cv=none; b=vrkcE+6V8BpsU2e6i8jMPI85o+2LwrcHKZIh8X2Q6nHySFP9pqYAHmUhuNYeBHQdAE/KWd LmXoPofCokyvGHjX/zhiXEqFjOXqV+MInAWgjuObP2Q7IJmMekd1MUc87bvgz6RbuDYDsq wh/T0dDx+3HZ56XfxKrCFQ1Q4vsBrLI= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-54629ed836aso18475627b3.10 for ; Wed, 29 Mar 2023 08:11:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680102689; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4YhYo6B188EjYYv/dO1DqyMOiNYtHUIng4cDGkzB8Lk=; b=H4q+TwD9UVpgB5nFgnHUeSTy5tD0C0o/7KXWk/aX4kOJfKGqztfLfegB7y6oFPt10l zT/qiuo3l6IfZBy8o+vMHOurFOfvLa/VLAO1yXBXfYtwP9GISZDxPZelRRJmUeAN49hr jFwM+F7C3+hbhDBEDUzazCLLQo+CFAyVebNDt0d9nPWNt+WKgN1ALhbksLuhohjo71DR ZfXwoAjwArUaEQTX43NeNulPfo5XZKGT6NnlBe/xx6dh0haZg/iJN0NOXxt1pWgeutBN DRQnuFLe3uV1Tglvuy8jYPJ1yNcuOOYcaklRBzt1wwC5oVRJb7V7A/TWgq+WfoOnckbJ 1p4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680102689; 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=4YhYo6B188EjYYv/dO1DqyMOiNYtHUIng4cDGkzB8Lk=; b=vFjltVoybRXY6ZRmDR6txULBS3n7GZK1+hMSWaH5N0QYqYowesdOERnt2F+fmVrCNk ukA5tnXeFqSy0BWm8c3hwaJPjD1W62tPHOjCVA/7bzcmE8RsF2O5gNR/b9fm0nj0u0QK 0wCvLmdU1ceN1XGHSaA7vvgdz399R570LXNdLGcVbFsZp0cYYKZ8RIB62COOojjbp+IJ acIbNICznKAOopOmWIb2LKbtjmiitiphKtN9tC6IeaC82sJqrbPNroHTiPSM7zMqVEb1 RKn+mfFNm5HD0cv8p/qWB3wwWtRCaT3B2HWWasb1IJq//ESwNwHKv3UH272MtOwqXjMz KpcQ== X-Gm-Message-State: AAQBX9f5h3YWTMQAvjRLv0ME/mwf+ce5uucJ6L+Vy3/nE79l5M5j005D SNbMbLKc+O1FEQ0nisAOJ5NS7PjGpp0YQw== X-Google-Smtp-Source: AKy350a6vnNmt0apkVinDUk9B3nDoGqNiJBFJu9rC2M/NhUgqqaqBkIa19qq+cveyhDdOd5rJA+y2DJGs3jtKg== X-Received: from yjqkernel.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1837]) (user=jiaqiyan job=sendgmr) by 2002:a81:d007:0:b0:546:81f:a89e with SMTP id v7-20020a81d007000000b00546081fa89emr3865593ywi.9.1680102688868; Wed, 29 Mar 2023 08:11:28 -0700 (PDT) Date: Wed, 29 Mar 2023 08:11:20 -0700 In-Reply-To: <20230329151121.949896-1-jiaqiyan@google.com> Mime-Version: 1.0 References: <20230329151121.949896-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230329151121.949896-3-jiaqiyan@google.com> Subject: [PATCH v12 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-Rsp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enGwh a2/ZZgLo4rIvD1iDz7IDl783Q4GjGbs6sHENfeeLF/fGvD93DhUfkhD4PbtB4GrEJsF2cX1gHvnShSy6IfdRrhhTaDgpRx1XlS0cTidZRFBZKgs0rteL9sZy0oPY5wNGiNY0CkJpodUX9badwnaKYNyFSX+kSDOf1h7Y0H5qSleOfd02SUUDVhS77KXuHT5cqJ68HX+AWSr8HHhEuZ0/gAIRUlQQpKjU/hYTEVIdO3Yav10YqK5p7JnLtlC4fnKe8CMIsJP3iQS2FK4CTpMROVJPeEZjGrYv8tFbkFPy0aahTMZv7WnEzUS2wWYfv0DQ2DBemOIYUqTuNORgJyXpmVDTrArV4HxeRkif2NBfWQyuOlkcp7NNfdvR0EchyGrBjj2SIVEu8C7muhHofemOVKp71siuTFrJqnzY/pmOjfWeSzaqW8gD02m7jiBFVY530pyFGgSombSpW9PDzUFzkWoox5Sz2CBVQhSi0svLTilLXzqMZy8DHUXp3t+CTWWWrw7izy4nzHBqE9QD5fkezat7QfKWu+gYhM06kMhNtEYHlsFvWoHKOwO/EsN5D7h7oPgoKtCrOLoVwChU2r8LzQ80QX+E4ECZ1AuHo65n4dk2muefDkqZiX8iCGwO2kulZNFb7ND+avhfskjnh0H1ol8X+9ftk1A8Cqpr2V2fXRqN8kxfaeq87n4/vyoeeexdmXcuG57F5U0DkHq+QFFYl/VBPAw== 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 Wed Mar 29 15:11:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13192691 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 50C0DC74A5B for ; Wed, 29 Mar 2023 15:11:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D156B280001; Wed, 29 Mar 2023 11:11:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BAF20900002; Wed, 29 Mar 2023 11:11:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A28A1280001; Wed, 29 Mar 2023 11:11:33 -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 893CC900002 for ; Wed, 29 Mar 2023 11:11:33 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 454021C625D for ; Wed, 29 Mar 2023 15:11:33 +0000 (UTC) X-FDA: 80622274866.06.4EE26BD Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf05.hostedemail.com (Postfix) with ESMTP id 70908100022 for ; Wed, 29 Mar 2023 15:11:31 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Dyuhe+7n; spf=pass (imf05.hostedemail.com: domain of 3IlUkZAgKCIUsrjzr7jwpxxpun.lxvurw36-vvt4jlt.x0p@flex--jiaqiyan.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3IlUkZAgKCIUsrjzr7jwpxxpun.lxvurw36-vvt4jlt.x0p@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680102691; a=rsa-sha256; cv=none; b=vN0bAbCcx59bU9+CGvJ3WEA3k566rYwIjvvyz0Pw3HsMjMsdU3J18Lhb3fdpa/VDmQAInV idlHIphGhS7U0A44slpOZfJTQaASQo2PkkNOOw4kLIMhBHbClMUhiiyyHGgV8kAMFziOVv okon47YOX5UlNwTpqtsVOtiwZZKBE0A= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Dyuhe+7n; spf=pass (imf05.hostedemail.com: domain of 3IlUkZAgKCIUsrjzr7jwpxxpun.lxvurw36-vvt4jlt.x0p@flex--jiaqiyan.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3IlUkZAgKCIUsrjzr7jwpxxpun.lxvurw36-vvt4jlt.x0p@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=1680102691; 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=H+gtwlatEJ8DMC6CZIRqkE0iHjFuMvKnIG4rRqiGZVc=; b=5l9ADrYyH6RHZ7MbOqlHr7NUd4YlJ13rd3ANxD/VlTjfcQLNcgMtY18JuFEFBao6o+gSiM 6FBog6QX4qQQlQe5GL7cYDtebk3wpnuIEv65kU+tH+8pFhQ0q+9HvJ0K1k4dWof+20Q5yY ShAfc+xaWq75cpTm3yWtr2kGdpEJuPY= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-544781e30easo156276637b3.1 for ; Wed, 29 Mar 2023 08:11:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680102690; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=H+gtwlatEJ8DMC6CZIRqkE0iHjFuMvKnIG4rRqiGZVc=; b=Dyuhe+7niyozcVMA6ZfeP+KTNWuJt/aEaRxzveJjycaCb/b8jvs7bWcPudJafxB1Mo IDKgyHReGg9abAxZDoBd/Z138l2cPUj47LztZYLJIQxQa+E8x7dP+RTNnu527VNc3qNc w4eQgtfUBaSCDoQuVzZx3V1XWaz00b3A55R9wWqG8f/AELYiXhLMuLeZ3EZO9RiyIpgE 0xHyFoP5Y8OqMXWg0NmU82Gw89K7xU4vfsfePrf36AmvsREzflCijqNonrJPyLxmCo6e 17fbuJ0AmuwPVQyznf/nGVnTfxTqD5kSDW/mFrmhqYx1rw5Vc0HLxCK472smthgVsurj q6Yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680102690; 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=H+gtwlatEJ8DMC6CZIRqkE0iHjFuMvKnIG4rRqiGZVc=; b=y9UmOgIhVh4pUklZWERrcf375cndcAywNVgzrNNf9cZTlFZM2jtoqv3zbJ2UYNuNdf q1iQOQCA3oYcIOm9Cp6z+xc+bptRxVcVrCBnLPnLmWhi6fVLztE0dsiCmyqRA2NTwadh cvox1Z2W0GiNRnHFQ0nKKHO1t08P24bj8tsUXonbXkbLwrIbI7oTMFqQ1wArIBqd364Y cF6FVIMFAln7d/0cuQHmsG5E2dZ7Zz7PEBwHZhXkKPnmyYb6u8D8cU4YzGBJ0ihs+KCi KIEwGs/SQHjcBWpF89RPfMJVe3GsKfE4gkNsqt6QjEX0I76bXjqp4PRwUUoaNnt748oe bhtw== X-Gm-Message-State: AAQBX9cB4HAcRxSQe1+sALn3V6+4HKfCrfnxFX9JcKn/aVJg4H0kDBrU NgVnRr/60SaMHzUjYMlTXDg8Sa/XaOKy4w== X-Google-Smtp-Source: AKy350ZcxfJuL1wchjjonaTebnqM7QP4cLP2vpRpMGJEs4S2Yn8kQqp+kN6skd7LgcYnLy/3eoWfCNOd+FMbsQ== X-Received: from yjqkernel.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1837]) (user=jiaqiyan job=sendgmr) by 2002:a05:6902:1586:b0:b68:7b14:186b with SMTP id k6-20020a056902158600b00b687b14186bmr9666036ybu.1.1680102690657; Wed, 29 Mar 2023 08:11:30 -0700 (PDT) Date: Wed, 29 Mar 2023 08:11:21 -0700 In-Reply-To: <20230329151121.949896-1-jiaqiyan@google.com> Mime-Version: 1.0 References: <20230329151121.949896-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230329151121.949896-4-jiaqiyan@google.com> Subject: [PATCH v12 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-Rspam-User: X-Rspamd-Queue-Id: 70908100022 X-Rspamd-Server: rspam01 X-Stat-Signature: 34fs55sqb3dtg5up83jqqqhtz9jjarjc X-HE-Tag: 1680102691-121458 X-HE-Meta: U2FsdGVkX1/XqpIR5wttdG76ISw1drYbutUHUUVGLo6Y2N54cMeoj0JSyNSze+4WrZGIizLbYD/xXFNYVuF5FfJAyRmOVKOqKMh79kRlsWUVhep9XG4oaKfd91BAXwGa7hLGtS8nVaGUe8suEE3RDuA9TB8dNx7dKxQOaY/rbI1lAFLgLqKb7HdVOq1X6QP+iAfqyRnuhFX6lPTXkDYI3Wth1B+GvNnHhKH6JVa6p6N8HQLMuqhi4g/D4zDrIl4h565LFs6PBEIoD+NaGPmY6HyXs52Ws4zWtWW0BZQY/EbbmSIhOZ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7pW5ge0TCuHuzH3lhpChGL1PL29aVKg2HOYYA8FweCxv0pb1yxEwkrfDsjy7p3pUZmvm799ud4F5v8nNB8/Hm7R399PTC4VrfkGP+m1psgtWtD61cfE4zQF7HGzajaALG5rHOL9TEFmaopEoUoLcDJXKbr4Ftdu0qFYqu9c+zOZmyQl0ppzjO53IoXZAihvsN6A4hKWwURNepGSTMmraUd9dqnejW9QOENfeGyxeY4ffrpwt56Y= 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 --- mm/khugepaged.c | 88 +++++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 32 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 269df3f052e8e..0b04276f764db 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1872,6 +1872,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); @@ -1916,8 +1919,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) { @@ -2097,12 +2099,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 @@ -2113,21 +2111,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: @@ -2140,21 +2126,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); @@ -2162,12 +2163,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); @@ -2185,8 +2197,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) { @@ -2220,6 +2230,20 @@ 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. + */ + if (!is_shmem && result == SCAN_COPY_MC) { + filemap_nr_thps_dec(mapping); + /* + * Paired with smp_mb() in do_dentry_open() to + * ensure the update to nr_thps is visible. + */ + smp_mb(); + } + xas_unlock_irq(&xas); hpage->mapping = NULL;