Skip to contentSkip to conversation

mm/memory-failure: fix races when handling free HugeTLB folios

PATCH In review Muchun Song 4 participants 7 messages Dec 5 – Dec 6

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 mm/memory-failure: fix races when handling free HugeTLB folios

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.

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")

---
v3:
- Handle free-folio filtering and HWPoison cleanup under hugetlb_lock, and avoid folio_lock() on unreferenced folios
- Drop the vmemmap_remap_rwsem changes from v2, as hugetlb_lock now keeps remove_hugetlb_folio() out of the cleanup window

v2: https://lore.kernel.org/[email protected]/
- Take vmemmap_remap_rwsem for write in try_memory_failure_hugetlb() instead of folio_clear_hugetlb_hwpoison() to avoid sleeping in atomic context and recursive locking on the free path (reported by sashiko-bot)
- Skip taking vmemmap_remap_rwsem in hugetlb_vmemmap_restore_folios() when the folio list is empty (suggested by sashiko-bot)

v1: https://lore.kernel.org/[email protected]/

---
 mm/memory-failure.c | 46 +++++++++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 18 deletions(-)
mm/memory-failure.c +28 -18
index 227148d116dd..bc38993bd0d6 100644
@@ -1958,6 +1958,7 @@ static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag)
 #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.
@@ -2069,6 +2070,17 @@ static int get_huge_page_for_hwpoison(unsigned long pfn, int flags,
 		goto out_unlock;
 	}
 
+	/*
+	 * 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 @@ static int try_memory_failure_hugetlb(unsigned long pfn, int flags)
 		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 @@ static int try_memory_failure_hugetlb(unsigned long pfn, int flags)
 		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)) {

base-commit: d0d65ff2e433e741c23715e8359c4a248d432179

Signature · 1 line
2.54.0
maintainer tree notice
#1 lore raw

+ mm-memory-failure-fix-races-when-handling-free-hugetlb-folios.patch added to mm-new branch

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…

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 <[email protected]>
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.

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")
Cc Oscar Salvador <[email protected]>
Cc Miaohe Lin <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Joao Martins <[email protected]>
 mm/memory-failure.c |   46 +++++++++++++++++++++++++-----------------
 1 file changed, 28 insertions(+), 18 deletions(-)
mm/memory-failure.c renamed from mm/memory-failure.c~mm-memory-failure-fix-races-when-handling-free-hugetlb-folios +28 -18
@@ -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 [email protected] 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

maintainer Tested-by on v3 1/1
#2 lore raw
In reply to Muchun Song
Quoted 10 lines · On Sat, 5 Dec 2026 20:12:05 +0800 Muchun Song <[email protected]> wrote:

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.

Quoted 8 lines

...

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.

I did A/B testing of this.

: A test paused memory-failure handling after it found a free HugeTLB : folio. Pool shrink then freed that folio to the buddy allocator. On : A, memory-failure handling resumed and locked the freed folio, despite : its zero reference count. On B, the same ordering occurred, but the : patched code returned without locking the freed folio. B's injection : returned EBUSY, so the result confirms the lock safety fix, not : successful page recovery. Neither run showed reuse of the freed page.

I hit another issue while testing. Please check the proposed fix:

From: Andrew Morton <[email protected]>
Subject: mm/memory-failure: don't leave a filtered HugeTLB page poisoned
Date: Sat, 05 Dec 2026 14:50:00 -0800

When hwpoison_filter() rejects an error in a HugeTLB page, memory_failure() returns -EOPNOTSUPP but leaves the page marked as poisoned.

The filter rejects a page when a tester has used the hwpoison-inject module's corrupt-filter-* debugfs files to limit a memory failure test to one filesystem, one memcg or pages with certain flags, and the page is not one of those. Nothing else registers a filter, so only such tests are affected. Found by reading the code.

Depending on where the error was, the folio stays poisoned, or a tail page still has PG_hwpoison set when the folio is later freed to the page allocator. free_pages_prepare() only refuses a PG_hwpoison page at order 0, so that page goes onto the free lists. When it is later allocated, a kernel with page checking enabled (CONFIG_DEBUG_VM, init_on_alloc, init_on_free) quietly discards the allocated block and retries, so the memory is lost. Other kernels hand the page out with PG_hwpoison set.

A/B testing result: the same hwpoison filter rejected injections into four distinct HugeTLB folios on A and B kernels. On A, injections into the head of both a free and a mapped folio left KPF_HWPOISON set. On B, the head flag was clear after those injections. Tail injections did not leave the head flag set on A, so the observed A failure concerns the head cases.

Fixes 161df60e9e89 ("mm, hwpoison, hugetlb: support saving mechanism of raw error pages")
Cc Miaohe Lin <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc David Hildenbrand <[email protected]>
 mm/memory-failure.c |   36 ++++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)
mm/memory-failure.c renamed from mm/memory-failure.c~mm-memory-failure-dont-leave-a-filtered-hugetlb-page-poisoned +34 -2
@@ -2003,6 +2003,38 @@ static int hugetlb_update_hwpoison(struc
 	return ret;
 }
 
+/**
+ * hugetlb_undo_hwpoison - undo hugetlb_update_hwpoison() for a filtered error
+ * @folio: the hugetlb folio which hugetlb_update_hwpoison() marked
+ *
+ * hwpoison_filter() rejected the error for which hugetlb_update_hwpoison()
+ * returned 0.  @folio was not poisoned before that call, so all of the
+ * HWPoison state which it has now came from that call.  Remove it.
+ *
+ * folio_clear_hugetlb_hwpoison() must not be used for this.  It is for a
+ * folio which is being freed to the page allocator: it moves PG_hwpoison to
+ * the raw error pages, and it does nothing if the folio's vmemmap is
+ * optimized.  Here @folio stays a hugetlb folio.  Only its first four struct
+ * pages are written and those are never in the shared vmemmap tail page.
+ *
+ * The rejected error was not added to num_poisoned_pages, so it is not
+ * subtracted here.
+ *
+ * Context: The caller holds mf_mutex, and either a reference on @folio or
+ * hugetlb_lock.
+ */
+static void hugetlb_undo_hwpoison(struct folio *folio)
+{
+	struct raw_hwp_page *p, *next;
+	struct llist_node *head;
+
+	head = llist_del_all(raw_hwp_list_head(folio));
+	llist_for_each_entry_safe(p, next, head, node)
+		kfree(p);
+	folio_clear_hugetlb_raw_hwp_unreliable(folio);
+	folio_clear_hwpoison(folio);
+}
+
 static unsigned long folio_free_raw_hwp(struct folio *folio, bool move_flag)
 {
 	/*
@@ -2076,7 +2108,7 @@ static int get_huge_page_for_hwpoison(un
 	 * and reuse.
 	 */
 	if (ret == MF_HUGETLB_FREED && hwpoison_filter(page)) {
-		folio_clear_hugetlb_hwpoison(folio);
+		hugetlb_undo_hwpoison(folio);
 		ret = MF_HUGETLB_FILTERED;
 		goto out_unlock;
 	}
@@ -2164,7 +2196,7 @@ retry:
 	folio_lock(folio);
 
 	if (hwpoison_filter(p)) {
-		folio_clear_hugetlb_hwpoison(folio);
+		hugetlb_undo_hwpoison(folio);
 		if (migratable_cleared)
 			folio_set_hugetlb_migratable(folio);
 		folio_unlock(folio);

_

Acked-by
#3 lore raw
Quoted 35 lines · Andrew Morton
Quoted 10 lines · On Sat, 5 Dec 2026 20:12:05 +0800 Muchun Song <[email protected]> wrote:

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.

Quoted 8 lines

...

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.

I did A/B testing of this.

: A test paused memory-failure handling after it found a free HugeTLB : folio. Pool shrink then freed that folio to the buddy allocator. On : A, memory-failure handling resumed and locked the freed folio, despite : its zero reference count. On B, the same ordering occurred, but the : patched code returned without locking the freed folio. B's injection : returned EBUSY, so the result confirms the lock safety fix, not : successful page recovery. Neither run showed reuse of the freed page.

Quoted 2 lines

Thanks for your testing.

Quoted 34 lines

I hit another issue while testing. Please check the proposed fix:

From: Andrew Morton <[email protected]>
Subject: mm/memory-failure: don't leave a filtered HugeTLB page poisoned
Date: Sat, 05 Dec 2026 14:50:00 -0800

When hwpoison_filter() rejects an error in a HugeTLB page, memory_failure() returns -EOPNOTSUPP but leaves the page marked as poisoned.

The filter rejects a page when a tester has used the hwpoison-inject module's corrupt-filter-* debugfs files to limit a memory failure test to one filesystem, one memcg or pages with certain flags, and the page is not one of those. Nothing else registers a filter, so only such tests are affected. Found by reading the code.

Depending on where the error was, the folio stays poisoned, or a tail page still has PG_hwpoison set when the folio is later freed to the page allocator. free_pages_prepare() only refuses a PG_hwpoison page at order 0, so that page goes onto the free lists. When it is later allocated, a kernel with page checking enabled (CONFIG_DEBUG_VM, init_on_alloc, init_on_free) quietly discards the allocated block and retries, so the memory is lost. Other kernels hand the page out with PG_hwpoison set.

A/B testing result: the same hwpoison filter rejected injections into four distinct HugeTLB folios on A and B kernels. On A, injections into the head of both a free and a mapped folio left KPF_HWPOISON set. On B, the head flag was clear after those injections. Tail injections did not leave the head flag set on A, so the observed A failure concerns the head cases.

Quoted 2 lines
Fixes 161df60e9e89 ("mm, hwpoison, hugetlb: support saving mechanism of raw error pages")

LGTM. But some nits bellow.

Thanks.

Quoted 16 lines
Cc Miaohe Lin <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Muchun Song <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc David Hildenbrand <[email protected]>

mm/memory-failure.c | 36 ++++++++++++++++++++++++++++++++++--

1 file changed, 34 insertions(+), 2 deletions(-)
mm/memory-failure.c renamed from mm/memory-failure.c~mm-memory-failure-dont-leave-a-filtered-hugetlb-page-poisoned +1 -0
@@ -2003,6 +2003,38 @@ static int hugetlb_update_hwpoison(struc
 	return ret;
 }
 
+/**

mm/memory-failure.c static int hugetlb_update_hwpoison(struc · line 2016

Quoted 10 lines
mm/memory-failure.c renamed from mm/memory-failure.c~mm-memory-failure-dont-leave-a-filtered-hugetlb-page-poisoned +10 -0
+ * hugetlb_undo_hwpoison - undo hugetlb_update_hwpoison() for a filtered error
+ * @folio: the hugetlb folio which hugetlb_update_hwpoison() marked
+ *
+ * hwpoison_filter() rejected the error for which hugetlb_update_hwpoison()
+ * returned 0.  @folio was not poisoned before that call, so all of the
+ * HWPoison state which it has now came from that call.  Remove it.
+ *
+ * folio_clear_hugetlb_hwpoison() must not be used for this.  It is for a
+ * folio which is being freed to the page allocator: it moves PG_hwpoison to
+ * the raw error pages, and it does nothing if the folio's vmemmap is

I think these comments were generated by AI, so they're not very concise. I feel we should rewrite them.

This information is suitable for commit message, but it seems a bit too clunky to put directly in the code.

mm/memory-failure.c static int hugetlb_update_hwpoison(struc · line 2017

Quoted 1 line
mm/memory-failure.c renamed from mm/memory-failure.c~mm-memory-failure-dont-leave-a-filtered-hugetlb-page-poisoned +1 -0
+ * optimized.  Here @folio stays a hugetlb folio.  Only its first four struct

The number "four" here is hardcoded, which isn't ideal. We probably don't need to mention HVO details, since this function doesn't actually manipulate any related data structures.

mm/memory-failure.c static int hugetlb_update_hwpoison(struc · line 2024

Quoted 7 lines
mm/memory-failure.c renamed from mm/memory-failure.c~mm-memory-failure-dont-leave-a-filtered-hugetlb-page-poisoned +7 -0
+ * pages are written and those are never in the shared vmemmap tail page.
+ *
+ * The rejected error was not added to num_poisoned_pages, so it is not
+ * subtracted here.
+ *
+ * Context: The caller holds mf_mutex, and either a reference on @folio or
+ * hugetlb_lock.

We usually use lock-assert. We only use comments when the code can't express it.

Quoted 35 lines
mm/memory-failure.c renamed from mm/memory-failure.c~mm-memory-failure-dont-leave-a-filtered-hugetlb-page-poisoned +15 -2
+ */
+static void hugetlb_undo_hwpoison(struct folio *folio)
+{
+ 	struct raw_hwp_page *p, *next;
+ 	struct llist_node *head;
+
+ 	head = llist_del_all(raw_hwp_list_head(folio));
+ 	llist_for_each_entry_safe(p, next, head, node)
+ 		kfree(p);
+ 	folio_clear_hugetlb_raw_hwp_unreliable(folio);
+ 	folio_clear_hwpoison(folio);
+}
+
 static unsigned long folio_free_raw_hwp(struct folio *folio, bool move_flag)
 {
 	/*
@@ -2076,7 +2108,7 @@ static int get_huge_page_for_hwpoison(un
 	 * and reuse.
 	 */
 	if (ret == MF_HUGETLB_FREED && hwpoison_filter(page)) {
- 		folio_clear_hugetlb_hwpoison(folio);
+ 		hugetlb_undo_hwpoison(folio);
 		ret = MF_HUGETLB_FILTERED;
 		goto out_unlock;
 	}
@@ -2164,7 +2196,7 @@ retry:
 	folio_lock(folio);
 
 	if (hwpoison_filter(p)) {
- 		folio_clear_hugetlb_hwpoison(folio);
+		hugetlb_undo_hwpoison(folio);
 		if (migratable_cleared)
 			folio_set_hugetlb_migratable(folio);
 		folio_unlock(folio);

_

maintainer Acked-by Muchun Song (ByteDance)
#4 lore raw
Quoted 5 lines · On Sun, 6 Dec 2026 10:26:05 +0800 Muchun Song <[email protected]> wrote:

LGTM. But some nits bellow.

Thanks.

Quoted 6 lines

I think these comments were generated by AI, so they're not very concise. I feel we should rewrite them.

This information is suitable for commit message, but it seems a bit too clunky to put directly in the code.

ok.

Quoted 4 lines
Quoted 2 lines

+ * Context: The caller holds mf_mutex, and either a reference on @folio or
+ * hugetlb_lock.

We usually use lock-assert. We only use comments when the code can't express it.

yup.

From: Andrew Morton <[email protected]>
Subject: mm/memory-failure: don't leave a filtered HugeTLB page poisoned
Date: Sat, 05 Dec 2026 14:50:00 -0800

When hwpoison_filter() rejects an error in a HugeTLB page, memory_failure() returns -EOPNOTSUPP but leaves the page marked as poisoned.

The filter rejects a page when a tester has used the hwpoison-inject module's corrupt-filter-* debugfs files to limit a memory failure test to one filesystem, one memcg or pages with certain flags, and the page is not one of those. Nothing else registers a filter, so only such tests are affected. Found by reading the code.

Depending on where the error was, the folio stays poisoned, or a tail page still has PG_hwpoison set when the folio is later freed to the page allocator. free_pages_prepare() only refuses a PG_hwpoison page at order 0, so that page goes onto the free lists. When it is later allocated, a kernel with page checking enabled (CONFIG_DEBUG_VM, init_on_alloc, init_on_free) quietly discards the allocated block and retries, so the memory is lost. Other kernels hand the page out with PG_hwpoison set.

The filter paths undo hugetlb_update_hwpoison() with folio_clear_hugetlb_hwpoison(). That function is for a folio which is being freed to the page allocator: it moves PG_hwpoison to the raw error pages, and it does nothing if the folio's vmemmap is optimized or HPG_raw_hwp_unreliable is set. Add hugetlb_undo_hwpoison(), which removes what hugetlb_update_hwpoison() added, and use it at both places.

A/B testing result: the same hwpoison filter rejected injections into four distinct HugeTLB folios on adjacent A and B kernels. On A, injections into the head of both a free and a mapped folio left KPF_HWPOISON set. On B, the head flag was clear after those injections. Tail injections did not leave the head flag set on A, so the observed A failure concerns the head cases.

Fixes 161df60e9e89 ("mm, hwpoison, hugetlb: support saving mechanism of raw error pages")
Cc Miaohe Lin <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc David Hildenbrand <[email protected]>
 mm/memory-failure.c |   23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
mm/memory-failure.c renamed from mm/memory-failure.c~mm-memory-failure-dont-leave-a-filtered-hugetlb-page-poisoned +21 -2
@@ -2003,6 +2003,25 @@ static int hugetlb_update_hwpoison(struc
 	return ret;
 }
 
+/*
+ * hwpoison_filter() rejected the error which hugetlb_update_hwpoison() just
+ * recorded.  The folio was not poisoned before that, so clear all of its
+ * HWPoison state.  The error was not counted in num_poisoned_pages.
+ */
+static void hugetlb_undo_hwpoison(struct folio *folio)
+{
+	struct raw_hwp_page *p, *next;
+	struct llist_node *head;
+
+	lockdep_assert_held(&mf_mutex);
+
+	head = llist_del_all(raw_hwp_list_head(folio));
+	llist_for_each_entry_safe(p, next, head, node)
+		kfree(p);
+	folio_clear_hugetlb_raw_hwp_unreliable(folio);
+	folio_clear_hwpoison(folio);
+}
+
 static unsigned long folio_free_raw_hwp(struct folio *folio, bool move_flag)
 {
 	/*
@@ -2076,7 +2095,7 @@ static int get_huge_page_for_hwpoison(un
 	 * and reuse.
 	 */
 	if (ret == MF_HUGETLB_FREED && hwpoison_filter(page)) {
-		folio_clear_hugetlb_hwpoison(folio);
+		hugetlb_undo_hwpoison(folio);
 		ret = MF_HUGETLB_FILTERED;
 		goto out_unlock;
 	}
@@ -2164,7 +2183,7 @@ retry:
 	folio_lock(folio);
 
 	if (hwpoison_filter(p)) {
-		folio_clear_hugetlb_hwpoison(folio);
+		hugetlb_undo_hwpoison(folio);
 		if (migratable_cleared)
 			folio_set_hugetlb_migratable(folio);
 		folio_unlock(folio);

_

#5 lore raw
Quoted 66 lines · Andrew Morton
Quoted 5 lines · On Sun, 6 Dec 2026 10:26:05 +0800 Muchun Song <[email protected]> wrote:

LGTM. But some nits bellow.

Thanks.

Quoted 6 lines

I think these comments were generated by AI, so they're not very concise. I feel we should rewrite them.

This information is suitable for commit message, but it seems a bit too clunky to put directly in the code.

ok.

Quoted 4 lines
Quoted 2 lines

+ * Context: The caller holds mf_mutex, and either a reference on @folio or
+ * hugetlb_lock.

We usually use lock-assert. We only use comments when the code can't express it.

yup.

From: Andrew Morton <[email protected]>
Subject: mm/memory-failure: don't leave a filtered HugeTLB page poisoned
Date: Sat, 05 Dec 2026 14:50:00 -0800

When hwpoison_filter() rejects an error in a HugeTLB page, memory_failure() returns -EOPNOTSUPP but leaves the page marked as poisoned.

The filter rejects a page when a tester has used the hwpoison-inject module's corrupt-filter-* debugfs files to limit a memory failure test to one filesystem, one memcg or pages with certain flags, and the page is not one of those. Nothing else registers a filter, so only such tests are affected. Found by reading the code.

Depending on where the error was, the folio stays poisoned, or a tail page still has PG_hwpoison set when the folio is later freed to the page allocator. free_pages_prepare() only refuses a PG_hwpoison page at order 0, so that page goes onto the free lists. When it is later allocated, a kernel with page checking enabled (CONFIG_DEBUG_VM, init_on_alloc, init_on_free) quietly discards the allocated block and retries, so the memory is lost. Other kernels hand the page out with PG_hwpoison set.

The filter paths undo hugetlb_update_hwpoison() with folio_clear_hugetlb_hwpoison(). That function is for a folio which is being freed to the page allocator: it moves PG_hwpoison to the raw error pages, and it does nothing if the folio's vmemmap is optimized or HPG_raw_hwp_unreliable is set. Add hugetlb_undo_hwpoison(), which removes what hugetlb_update_hwpoison() added, and use it at both places.

A/B testing result: the same hwpoison filter rejected injections into four distinct HugeTLB folios on adjacent A and B kernels. On A, injections into the head of both a free and a mapped folio left KPF_HWPOISON set. On B, the head flag was clear after those injections. Tail injections did not leave the head flag set on A, so the observed A failure concerns the head cases.

Quoted 7 lines
Fixes 161df60e9e89 ("mm, hwpoison, hugetlb: support saving mechanism of raw error pages")
Cc Miaohe Lin <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc David Hildenbrand <[email protected]>

LGTM. Thanks.

Reviewed-by on v3 1/1
#6 lore raw
In reply to Muchun Song
Quoted 83 lines · Muchun Song

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.

Quoted 7 lines

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

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")

LGTM:

Signature · 2 lines
cheers,
jose a. p-a