Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore
@ 2026-12-04 12:20 Muchun Song
  2026-12-04 13:28 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Muchun Song @ 2026-12-04 12:20 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

folio_clear_hugetlb_hwpoison() skips moving HWPoison state from the head
to raw tail pages while HPG_vmemmap_optimized is set.  Batched restore
clears that flag after updating each folio's page tables, but defers the
TLB flush until the entire batch has been processed.

A free folio can be selected for the batch before memory failure marks it
HWPoison.  If hwpoison_filter() later rejects the event after restore has
cleared HPG_vmemmap_optimized but before the final TLB flush, the cleanup
path can write a raw tail page through a stale shared mapping:

CPU 0                         CPU 1
try_memory_failure_hugetlb()
  get_huge_page_for_hwpoison()
    hugetlb_update_hwpoison()
                              hugetlb_vmemmap_restore_folios()
                                __hugetlb_vmemmap_restore_folio()
                                  /* Restore without a TLB flush. */
                                  folio_clear_hugetlb_vmemmap_optimized()
  if (hwpoison_filter(p))
    folio_clear_hugetlb_hwpoison()
      folio_test_hugetlb_vmemmap_optimized() -> false
      folio_free_raw_hwp(..., true)
        /* May use a stale shared TLB mapping. */
        SetPageHWPoison(raw tail) <-- writes to shared tail page
                                flush_tlb_all()

The raw-tail write can fault on a read-only shared mapping or corrupt
metadata shared by other HugeTLB folios.

Serialize HWPoison cleanup against vmemmap restore with
vmemmap_remap_rwsem.  Hold the read side across both single-folio and
batched restore through the final TLB flush, and take the write side while
checking the optimized state and moving HWPoison to raw tail pages.  The
state can then become clear only after stale shared translations are gone.

Fixes: c24f188b2289 ("hugetlb: batch TLB flushes when restoring vmemmap")
Cc: stable@vger.kernel.org
Signed-off-by: Muchun Song <songmuchun@bytedance.com>
---
 mm/hugetlb_vmemmap.c | 8 +++++++-
 mm/memory-failure.c  | 2 ++
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
index 336b1405cd6a..66bfd46de289 100644
--- a/mm/hugetlb_vmemmap.c
+++ b/mm/hugetlb_vmemmap.c
@@ -382,6 +382,9 @@ static int vmemmap_optimize_folio(const struct folio *folio,
  * tail page back to distinct backing pages allocated for @folio, while
  * non-optimizable entries remain unchanged.
  *
+ * Note: The caller must hold vmemmap_remap_rwsem. If TLB flushing is deferred,
+ * the lock must remain held until the final TLB flush.
+ *
  * Return: %0 on success, negative error code otherwise.
  */
 static int vmemmap_restore_folio(const struct folio *folio, unsigned long flags)
@@ -391,13 +394,14 @@ static int vmemmap_restore_folio(const struct folio *folio, unsigned long flags)
 	int ret, nid = folio_nid(folio);
 	LIST_HEAD(vmemmap_pages);
 
+	rwsem_assert_held(&vmemmap_remap_rwsem);
+
 	start += VMEMMAP_OPTIMIZATION_SIZE;
 	ret = alloc_vmemmap_page_list(&vmemmap_pages, (end - start) / PAGE_SIZE,
 				      nid, GFP_KERNEL);
 	if (ret)
 		return ret;
 
-	guard(rwsem_read)(&vmemmap_remap_rwsem);
 	return vmemmap_restore_range(start, end, &vmemmap_pages, flags);
 }
 
@@ -435,6 +439,7 @@ static int __hugetlb_vmemmap_restore_folio(struct folio *folio, unsigned long fl
  */
 int hugetlb_vmemmap_restore_folio(struct folio *folio)
 {
+	guard(rwsem_read)(&vmemmap_remap_rwsem);
 	return __hugetlb_vmemmap_restore_folio(folio, 0);
 }
 
@@ -458,6 +463,7 @@ long hugetlb_vmemmap_restore_folios(struct list_head *folio_list,
 	long ret = 0;
 	unsigned long flags = VMEMMAP_DEFER_TLB_FLUSH;
 
+	guard(rwsem_read)(&vmemmap_remap_rwsem);
 	list_for_each_entry_safe(folio, t_folio, folio_list, lru) {
 		if (folio_test_hugetlb_vmemmap_optimized(folio)) {
 			ret = __hugetlb_vmemmap_restore_folio(folio, flags);
diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index 227148d116dd..32d13d216543 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -2023,6 +2023,8 @@ static unsigned long folio_free_raw_hwp(struct folio *folio, bool move_flag)
 
 void folio_clear_hugetlb_hwpoison(struct folio *folio)
 {
+	guard(vmemmap_remap)();
+
 	if (folio_test_hugetlb_raw_hwp_unreliable(folio))
 		return;
 	if (folio_test_hugetlb_vmemmap_optimized(folio))

base-commit: 1a7610248a3802e38124ce66b07eef12277be9a0
-- 
2.54.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-04 15:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 12:20 [PATCH] mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore Muchun Song
2026-12-04 13:28 ` sashiko-bot
2026-12-04 15:05   ` Muchun Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox