Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: mm-commits@vger.kernel.org,stable@vger.kernel.org,rppt@kernel.org,
	osalvador@suse.de,nao.horiguchi@gmail.com,linmiaohe@huawei.com,
	joao.m.martins@oracle.com,david@kernel.org,songmuchun@bytedance.com,
	akpm@linux-foundation.org
Subject: + mm-memory-failure-fix-races-when-handling-free-hugetlb-folios.patch added to mm-new branch
Date: Sat, 05 Dec 2026 15:54:43 -0800	[thread overview]
Message-ID: <20261205235443.CF69D1F00936@smtp.kernel.org> (raw)
In-Reply-To: <20261205121205.83068-1-songmuchun@bytedance.com>


The patch titled
     Subject: mm/memory-failure: fix races when handling free HugeTLB folios
has been added to the -mm mm-new branch.  Its filename is
     mm-memory-failure-fix-races-when-handling-free-hugetlb-folios.patch

This patch will shortly appear at
     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-memory-failure-fix-races-when-handling-free-hugetlb-folios.patch

This patch will later appear in the mm-new branch at
    git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Note, mm-new is a provisional staging ground for work-in-progress
patches, and acceptance into mm-new is a notification for others take
notice and to finish up reviews.  Please do not hesitate to respond to
review feedback and post updated versions to replace or incrementally
fixup patches in mm-new.

The mm-new branch of mm.git is not included in linux-next

If a few days of testing in mm-new is successful, the patch will me moved
into mm.git's mm-unstable branch, which is included in linux-next

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Muchun Song <songmuchun@bytedance.com>
Subject: mm/memory-failure: fix races when handling free HugeTLB folios
Date: Sat,  5 Dec 2026 20:12:05 +0800

try_memory_failure_hugetlb() does not hold a folio reference when
get_huge_page_for_hwpoison() returns MF_HUGETLB_FREED, but it still
locks the folio and may clear its HugeTLB HWPoison state after dropping
hugetlb_lock.

A concurrent pool shrink can dissolve and free the folio during this
window. Locking it can race with the page allocator's free checks and
cause a bad-page report and a leak, or modify a page that has already
been freed or reused.

CPU 0                           CPU 1
try_memory_failure_hugetlb()
  get_huge_page_for_hwpoison()
    spin_unlock_irq(&hugetlb_lock)
    return MF_HUGETLB_FREED
  folio = page_folio(p)
                                remove_hugetlb_folio()
                                __update_and_free_hugetlb_folio()
                                  folio_clear_hugetlb_hwpoison()
  folio_lock(folio)
                                  free_frozen_pages()
                                    __free_pages_prepare()
                                      free_page_is_bad()
                                        /* PG_locked is set. */
                                        bad_page()

Cleanup can also interpret reused tail-page metadata as raw_hwp_page
entries. Resolving page_folio() again does not ensure that it identifies
the folio originally marked HWPoison.

CPU 0                           CPU 1
try_memory_failure_hugetlb()
  get_huge_page_for_hwpoison()
    spin_unlock_irq(&hugetlb_lock)
    return MF_HUGETLB_FREED
  folio = page_folio(p)
                                remove_hugetlb_folio()
                                __update_and_free_hugetlb_folio()
                                  hugetlb_vmemmap_restore_folio()
                                  __folio_clear_hugetlb()
                                  folio_clear_hugetlb_hwpoison()
                                  free_frozen_pages()
                                /* Tail private is reused. */
  folio_lock(folio)
  if (hwpoison_filter(p))
    folio_clear_hugetlb_hwpoison()
      folio_free_raw_hwp(..., true)
        __folio_free_raw_hwp()
          /* Reads and clears reused private. */
          llist_del_all(raw_hwp_list_head(folio))

Batched vmemmap restoration exposes another race in the same window:
HPG_vmemmap_optimized is cleared before the final TLB flush. Cleanup can
then write HWPoison to a raw tail page through a stale shared mapping,
corrupting metadata shared by other HugeTLB folios.

CPU 0                           CPU 1
try_memory_failure_hugetlb()
  get_huge_page_for_hwpoison()
    hugetlb_update_hwpoison()
    spin_unlock_irq(&hugetlb_lock)
    return MF_HUGETLB_FREED
  folio = page_folio(p)
                                remove_hugetlb_folio()
                                hugetlb_vmemmap_restore_folios()
                                  __hugetlb_vmemmap_restore_folio()
                                    /* Restore without a TLB flush. */
                                    folio_clear_hugetlb_vmemmap_optimized()
  folio_lock(folio)
  if (hwpoison_filter(p))
    folio_clear_hugetlb_hwpoison()
      folio_test_hugetlb_vmemmap_optimized() -> false
      folio_free_raw_hwp(..., true)
        __folio_free_raw_hwp()
          /* May use a stale shared TLB mapping. */
          SetPageHWPoison(raw tail)
                                  flush_tlb_all()

Perform free-folio filtering and HWPoison cleanup in the same
hugetlb_lock critical section that identifies and marks the folio.
This prevents concurrent dissolution and vmemmap restoration during
cleanup.

Only lock in-use folios, whose lifetime is pinned by a reference.

Link: https://patch.msgid.link/20261205121205.83068-1-songmuchun@bytedance.com
Fixes: 8c6c2ecb4466 ("HWPOSION, hugetlb: recover from free hugepage error when !MF_COUNT_INCREASED")
Fixes: 161df60e9e89 ("mm, hwpoison, hugetlb: support saving mechanism of raw error pages")
Fixes: c24f188b2289 ("hugetlb: batch TLB flushes when restoring vmemmap")
Signed-off-by: Muchun Song <songmuchun@bytedance.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Oscar Salvador <osalvador@suse.de>
Cc: Miaohe Lin <linmiaohe@huawei.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: David Hildenbrand <david@kernel.org>
Cc: Naoya Horiguchi <nao.horiguchi@gmail.com>
Cc: Joao Martins <joao.m.martins@oracle.com>
Cc: <stable@vger.kernel.org>

 mm/memory-failure.c |   46 +++++++++++++++++++++++++-----------------
 1 file changed, 28 insertions(+), 18 deletions(-)

--- a/mm/memory-failure.c~mm-memory-failure-fix-races-when-handling-free-hugetlb-folios
+++ a/mm/memory-failure.c
@@ -1958,6 +1958,7 @@ static unsigned long __folio_free_raw_hw
 #define	MF_HUGETLB_FOLIO_PRE_POISONED	3	/* folio already poisoned */
 #define	MF_HUGETLB_PAGE_PRE_POISONED	4	/* exact page already poisoned */
 #define	MF_HUGETLB_RETRY		5	/* hugepage is busy, retry */
+#define	MF_HUGETLB_FILTERED		6	/* rejected by hwpoison filter */
 /*
  * Set hugetlb folio as hwpoisoned, update folio private raw hwpoison list
  * to keep track of the poisoned pages.
@@ -2070,6 +2071,17 @@ static int get_huge_page_for_hwpoison(un
 	}
 
 	/*
+	 * Free folios have no reference pinning them. Filter and clear their
+	 * HWPoison state under hugetlb_lock to prevent concurrent dissolution
+	 * and reuse.
+	 */
+	if (ret == MF_HUGETLB_FREED && hwpoison_filter(page)) {
+		folio_clear_hugetlb_hwpoison(folio);
+		ret = MF_HUGETLB_FILTERED;
+		goto out_unlock;
+	}
+
+	/*
 	 * Clearing hugetlb_migratable for hwpoisoned hugepages to prevent them
 	 * from being migrated by memory hotremove.
 	 */
@@ -2129,8 +2141,22 @@ retry:
 		else
 			action_result(pfn, MF_MSG_HUGE, MF_FAILED);
 		return rv;
+	case MF_HUGETLB_FILTERED:
+		return -EOPNOTSUPP;
+	case MF_HUGETLB_FREED:
+		/*
+		 * PageHWPoison prevents allocation or demotion from the HugeTLB
+		 * pool. The folio cannot be locked without a reference.
+		 */
+		if (__page_handle_poison(p) > 0) {
+			page_ref_inc(p);
+			res = MF_RECOVERED;
+		} else {
+			res = MF_FAILED;
+		}
+		return action_result(pfn, MF_MSG_FREE_HUGE, res);
 	default:
-		WARN_ON((res != MF_HUGETLB_FREED) && (res != MF_HUGETLB_IN_USED));
+		WARN_ON(res != MF_HUGETLB_IN_USED);
 		break;
 	}
 
@@ -2142,26 +2168,10 @@ retry:
 		if (migratable_cleared)
 			folio_set_hugetlb_migratable(folio);
 		folio_unlock(folio);
-		if (res == MF_HUGETLB_IN_USED)
-			folio_put(folio);
+		folio_put(folio);
 		return -EOPNOTSUPP;
 	}
 
-	/*
-	 * Handling free hugepage.  The possible race with hugepage allocation
-	 * or demotion can be prevented by PageHWPoison flag.
-	 */
-	if (res == MF_HUGETLB_FREED) {
-		folio_unlock(folio);
-		if (__page_handle_poison(p) > 0) {
-			page_ref_inc(p);
-			res = MF_RECOVERED;
-		} else {
-			res = MF_FAILED;
-		}
-		return action_result(pfn, MF_MSG_FREE_HUGE, res);
-	}
-
 	page_flags = folio->flags.f;
 
 	if (!hwpoison_user_mappings(folio, p, pfn, flags)) {
_

Patches currently in -mm which might be from songmuchun@bytedance.com are

powerpc-mm-make-vmemmap_populate_compound_pages-static.patch
powerpc-mm-drop-powerpc-vmemmap_can_optimize.patch
mm-sparse-vmemmap-drop-vmemmap_can_optimize.patch
mm-sparse-vmemmap-drop-pgmap-from-vmemmap-population-apis.patch
mm-hugetlb_vmemmap-fix-hwpoison-state-races-during-optimization.patch
mm-hugetlb_vmemmap-fix-pmd-split-tlb-flush-race.patch
mm-hugetlb_vmemmap-fix-deadlock-during-pmd-split.patch
mm-list_lru-fix-potential-infinite-loop-on-xa-node-allocation-failure.patch
mm-mm_init-verify-zones-for-template-initialized-pages.patch
mm-mm_init-factor-out-common-struct-page-initialization.patch
mm-mm_init-simplify-zone-device-page-initialization.patch
mm-mm_init-factor-out-compound-page-initialization.patch
mm-hugetlb-drop-boot-time-hvo-handling-for-gigantic-folios.patch
mm-hugetlb-initialize-all-struct-pages-for-bootmem-hugepages.patch
mm-hugetlb-use-common-compound-page-initialization-helper.patch
mm-mm_init-make-__init_single_page-static.patch
mm-cma-centralize-cma-pageblock-initialization.patch
mm-cma-localize-init_cma_pageblock-to-cmac.patch
mm-mm_init-move-pageblock-migratetype-init-to-memmap-helpers.patch
kmsan-encapsulate-per-page-metadata-access.patch
mm-page_ext-fix-client-data-alignment.patch
mm-page_ext-always-use-vmalloc-for-section-page-extensions.patch
mm-page_ext-use-common-allocation-and-initialization-entry-points.patch
mm-page_ext-allocate-page-extensions-early-for-sparsemem.patch
mm-page_ext-inline-unchecked-page-extension-lookup.patch
kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization.patch
mm-hugetlb_vmemmap-remove-the-vmemmap-savings-boot-message.patch
mm-hugetlb_vmemmap-fold-vmemmap_should_optimize-into-its-callers.patch
mm-hugetlb_vmemmap-drop-the-redundant-hstate-argument.patch
mm-hugetlb_vmemmap-open-code-free_vmemmap_page.patch
mm-hugetlb_vmemmap-drop-obsolete-restore-refcount-warning.patch
mm-vmemmap-optimization-centralize-the-vmemmap-optimization-support-check.patch
mm-memory-failure-fix-races-when-handling-free-hugetlb-folios.patch


      reply	other threads:[~2026-12-05 23:54 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 12:12 [PATCH v3] mm/memory-failure: fix races when handling free HugeTLB folios Muchun Song
2026-12-05 23:54 ` Andrew Morton [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205235443.CF69D1F00936@smtp.kernel.org \
    --to=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=joao.m.martins@oracle.com \
    --cc=linmiaohe@huawei.com \
    --cc=mm-commits@vger.kernel.org \
    --cc=nao.horiguchi@gmail.com \
    --cc=osalvador@suse.de \
    --cc=rppt@kernel.org \
    --cc=songmuchun@bytedance.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox