* [PATCH v3] mm/memory-failure: fix races when handling free HugeTLB folios
@ 2026-12-05 12:12 Muchun Song
2026-12-05 23:54 ` + mm-memory-failure-fix-races-when-handling-free-hugetlb-folios.patch added to mm-new branch Andrew Morton
0 siblings, 1 reply; 2+ messages in thread
From: Muchun Song @ 2026-12-05 12:12 UTC (permalink / raw)
To: Oscar Salvador, Andrew Morton, Miaohe Lin, Mike Rapoport
Cc: Muchun Song, David Hildenbrand, Naoya Horiguchi, Joao Martins,
linux-mm, stable, 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.
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: stable@vger.kernel.org
Signed-off-by: Muchun Song <songmuchun@bytedance.com>
---
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/20261205013246.18791-1-songmuchun@bytedance.com/
- 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/20261204122056.85384-1-songmuchun@bytedance.com/
---
mm/memory-failure.c | 46 +++++++++++++++++++++++++++------------------
1 file changed, 28 insertions(+), 18 deletions(-)
diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index 227148d116dd..bc38993bd0d6 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -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
--
2.54.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* + mm-memory-failure-fix-races-when-handling-free-hugetlb-folios.patch added to mm-new branch
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
0 siblings, 0 replies; 2+ messages in thread
From: Andrew Morton @ 2026-12-05 23:54 UTC (permalink / raw)
To: mm-commits, stable, rppt, osalvador, nao.horiguchi, linmiaohe,
joao.m.martins, david, songmuchun, akpm
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-05 23:54 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 12:12 [PATCH v3] mm/memory-failure: fix races when handling free HugeTLB folios Muchun Song
2026-12-05 23:54 ` + mm-memory-failure-fix-races-when-handling-free-hugetlb-folios.patch added to mm-new branch Andrew Morton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox