Skip to contentSkip to conversation

mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore

PATCH Superseded Muchun Song 3 participants 3 messages Dec 4

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore

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")
---
 mm/hugetlb_vmemmap.c | 8 +++++++-
 mm/memory-failure.c  | 2 ++
 2 files changed, 9 insertions(+), 1 deletion(-)
mm/hugetlb_vmemmap.c +7 -1
index 336b1405cd6a..66bfd46de289 100644
@@ -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);
mm/memory-failure.c +2 -0
index 227148d116dd..32d13d216543 100644
@@ -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

Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] mm/hugetlb: fs_reclaim deadlock on vmemmap_remap_rwsem via shrinkers - [High] mm/memory-failure…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] mm/hugetlb: fs_reclaim deadlock on vmemmap_remap_rwsem via shrinkers
- [High] mm/memory-failure: folio_clear_hugetlb_hwpoison() sleeps in atomic context
- [High] mm/memory-failure: recursive vmemmap_remap_rwsem deadlock in hwpoison path
- [Medium] mm/hugetlb_vmemmap: vmemmap_remap_rwsem held across batched GFP_KERNEL allocs
- [Medium] mm/hugetlb_vmemmap: unconditional vmemmap_remap_rwsem lock on empty restores
--

commit a61abe88805bf09f0b488bd7991e61609b7ee6c4
Author: Muchun Song <[email protected]>

mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore

This commit hoists the vmemmap_remap_rwsem read lock out of vmemmap_restore_folio() into hugetlb_vmemmap_restore_folio() and hugetlb_vmemmap_restore_folios() so it stays held through the final TLB flush, and takes the write side in folio_clear_hugetlb_hwpoison().

Quoted 4 lines
mm/hugetlb_vmemmap.c +0 -0
index 336b1405cd6a3..66bfd46de2891 100644

[ ... ]

Quoted 5 lines
mm/hugetlb_vmemmap.c +1 -0
@@ -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);

mm/hugetlb_vmemmap.c static int vmemmap_restore_folio(const struct folio *folio, unsigned long flags) · line 406

Quoted 10 lines
mm/hugetlb_vmemmap.c +1 -1
+
 	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);
 }

[Severity: High]
Can holding vmemmap_remap_rwsem across alloc_vmemmap_page_list() deadlock in direct reclaim?

Moving the lock above alloc_vmemmap_page_list(..., GFP_KERNEL) establishes a vmemmap_remap_rwsem -> fs_reclaim dependency, while folio_clear_hugetlb_hwpoison() now takes vmemmap_remap_rwsem for write. Under direct reclaim, a shrinker that unpins user-pinned hugetlb folios can drop the final reference to a poisoned non-HVO folio and synchronously take the write lock:

hugetlb_vmemmap_restore_folio()
down_read(&vmemmap_remap_rwsem)
vmemmap_restore_folio()

    alloc_vmemmap_page_list(..., GFP_KERNEL)
      <direct reclaim>
        i915_gem_shrinker_scan()
          ...
            unpin_user_pages()
              folio_put()
                free_huge_folio()
                  __update_and_free_hugetlb_folio()
                    folio_clear_hugetlb_hwpoison()
                      down_write(&vmemmap_remap_rwsem)  <-- deadlock

This hangs the reclaiming task on vmemmap_remap_rwsem and triggers a lockdep fs_reclaim circular dependency splat even across separate tasks.

Quoted 9 lines
mm/hugetlb_vmemmap.c +1 -0
@@ -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);
 }
 

mm/hugetlb_vmemmap.c long hugetlb_vmemmap_restore_folios(struct list_head *folio_list, · line 469

Quoted 7 lines
mm/hugetlb_vmemmap.c +1 -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);

[Severity: Medium]
Will holding vmemmap_remap_rwsem across the entire batch loop stall writers and cause a reader convoy under memory pressure?

alloc_vmemmap_page_list() allocates up to 4,095 pages per 1GB folio with GFP_KERNEL | __GFP_RETRY_MAYFAIL, so bulk callers such as set_max_huge_pages() can hold the global rwsem across prolonged reclaim and writeback stalls. While held, any queued writer such as memory_failure() -> try_memory_failure_hugetlb() -> get_huge_page_for_hwpoison() blocks and, due to rwsem fairness, stalls all subsequent readers system-wide.

Since vmemmap_restore_folio() skips the head page where HPG_vmemmap_optimized lives, deferring folio_clear_hugetlb_vmemmap_optimized() until after flush_tlb_all() with appropriate memory ordering would close the race without holding vmemmap_remap_rwsem across allocations or locking folio_clear_hugetlb_hwpoison().

[Severity: Medium]
Does this acquire the global vmemmap_remap_rwsem on empty or non-HVO restores?

Hot paths like hugetlb_vm_op_close() and hugetlb_unreserve_pages() reach hugetlb_vmemmap_restore_folios() via return_unused_surplus_pages() -> update_and_free_pages_bulk() without checking list_empty(). In persistent pool setups with no surplus pages, every hugetlb unmap or unreserve now bounces the global vmemmap_remap_rwsem cacheline and sleeps behind any queued writer.

mm/memory-failure.c static unsigned long folio_free_raw_hwp(struct folio *folio, bool move_flag) · line 2030

Quoted 13 lines
mm/memory-failure.c +2 -0
index 227148d116dd9..32d13d216543c 100644
@@ -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))

[Severity: High]
Can folio_clear_hugetlb_hwpoison() now sleep in atomic context?

guard(vmemmap_remap)() calls down_write(&vmemmap_remap_rwsem), but folio_clear_hugetlb_hwpoison() is called from free_huge_folio() via update_and_free_hugetlb_folio(h, free_folio, true) -> __update_and_free_hugetlb_folio(). When a poisoned folio is not vmemmap-optimized, update_and_free_hugetlb_folio() bypasses free_hpage_work and runs inline even from atomic callers such as hugetlb_fault() under vmf.ptl or bio completion in blkdev_bio_end_io_async() -> bio_release_pages() -> folio_put().

This triggers a sleep-in-atomic bug, or a hard lockup if the interrupted CPU already holds vmemmap_remap_rwsem. At __update_and_free_hugetlb_folio(), the folio is already removed from the pool and restored, so the lock is only needed for the try_memory_failure_hugetlb() caller.

[Severity: High]
Is there a recursive write-lock deadlock on vmemmap_remap_rwsem when get_huge_page_for_hwpoison() drops the last reference to a folio?

get_huge_page_for_hwpoison() holds guard(vmemmap_remap)() across its out_unlock path, where it calls folio_put(folio) when count_increased is true (for example, on concurrent madvise(MADV_HWPOISON) calls against an already-poisoned non-HVO surplus folio):

get_huge_page_for_hwpoison()
guard(vmemmap_remap)() /* down_write */
hugetlb_update_hwpoison() /* PRE_POISONED -> out_unlock */
folio_put(folio)
free_huge_folio()
__update_and_free_hugetlb_folio()
folio_clear_hugetlb_hwpoison()
guard(vmemmap_remap)() <-- self-deadlock

This permanently deadlocks the task while holding mf_mutex, stalling all subsequent memory-failure handling and hugetlb vmemmap operations.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
Quoted 5 lines · On Dec 4, 2026, at 21:28, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 5 potential

That's really awkward.

Quoted 7 lines

issues to consider:
- [High] mm/hugetlb: fs_reclaim deadlock on vmemmap_remap_rwsem via shrinkers
- [High] mm/memory-failure: folio_clear_hugetlb_hwpoison() sleeps in atomic context
- [High] mm/memory-failure: recursive vmemmap_remap_rwsem deadlock in hwpoison path

I'll fix those 3 high issues with following diff:

mm/memory-failure.c +2 -3
index 32d13d216543..4f313881e65c 100644
@@ -2023,8 +2023,6 @@ 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))
@@ -2140,7 +2138,8 @@ static int try_memory_failure_hugetlb(unsigned long pfn, int flags)
        folio_lock(folio);
 
        if (hwpoison_filter(p)) {
-               folio_clear_hugetlb_hwpoison(folio);
+               scoped_guard(vmemmap_remap)
+                       folio_clear_hugetlb_hwpoison(folio);
                if (migratable_cleared)
                        folio_set_hugetlb_migratable(folio);
                folio_unlock(folio);
Quoted 88 lines

- [Medium] mm/hugetlb_vmemmap: vmemmap_remap_rwsem held across batched GFP_KERNEL allocs
- [Medium] mm/hugetlb_vmemmap: unconditional vmemmap_remap_rwsem lock on empty restores
--

commit a61abe88805bf09f0b488bd7991e61609b7ee6c4
Author: Muchun Song <[email protected]>

mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore

This commit hoists the vmemmap_remap_rwsem read lock out of vmemmap_restore_folio() into hugetlb_vmemmap_restore_folio() and hugetlb_vmemmap_restore_folios() so it stays held through the final TLB flush, and takes the write side in folio_clear_hugetlb_hwpoison().

Quoted 4 lines
mm/hugetlb_vmemmap.c +0 -0
index 336b1405cd6a3..66bfd46de2891 100644

[ ... ]

Quoted 5 lines
mm/hugetlb_vmemmap.c +1 -0
@@ -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);

mm/hugetlb_vmemmap.c static int vmemmap_restore_folio(const struct folio *folio, unsigned long flags) · line 406

Quoted 10 lines
mm/hugetlb_vmemmap.c +1 -1
+
 	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);
 }

[Severity: High]
Can holding vmemmap_remap_rwsem across alloc_vmemmap_page_list() deadlock in direct reclaim?

Moving the lock above alloc_vmemmap_page_list(..., GFP_KERNEL) establishes a vmemmap_remap_rwsem -> fs_reclaim dependency, while folio_clear_hugetlb_hwpoison() now takes vmemmap_remap_rwsem for write. Under direct reclaim, a shrinker that unpins user-pinned hugetlb folios can drop the final reference to a poisoned non-HVO folio and synchronously take the write lock:

hugetlb_vmemmap_restore_folio()
down_read(&vmemmap_remap_rwsem)
vmemmap_restore_folio()
alloc_vmemmap_page_list(..., GFP_KERNEL)

     <direct reclaim>
       i915_gem_shrinker_scan()
         ...
           unpin_user_pages()
             folio_put()
               free_huge_folio()
                 __update_and_free_hugetlb_folio()
                   folio_clear_hugetlb_hwpoison()
                     down_write(&vmemmap_remap_rwsem)  <-- deadlock

This hangs the reclaiming task on vmemmap_remap_rwsem and triggers a lockdep fs_reclaim circular dependency splat even across separate tasks.

Quoted 9 lines
mm/hugetlb_vmemmap.c +1 -0
@@ -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);
 }
 

mm/hugetlb_vmemmap.c long hugetlb_vmemmap_restore_folios(struct list_head *folio_list, · line 469

Quoted 7 lines
mm/hugetlb_vmemmap.c +1 -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);

[Severity: Medium]
Will holding vmemmap_remap_rwsem across the entire batch loop stall writers and cause a reader convoy under memory pressure?

Quoted 6 lines

alloc_vmemmap_page_list() allocates up to 4,095 pages per 1GB folio with GFP_KERNEL | __GFP_RETRY_MAYFAIL, so bulk callers such as set_max_huge_pages() can hold the global rwsem across prolonged reclaim and writeback stalls. While held, any queued writer such as memory_failure() -> try_memory_failure_hugetlb() -> get_huge_page_for_hwpoison() blocks and, due to rwsem fairness, stalls all subsequent readers system-wide.

memory failure path is not latency-critical and rare. So I don't think this needs to be addressed.

Quoted 7 lines

Since vmemmap_restore_folio() skips the head page where HPG_vmemmap_optimized lives, deferring folio_clear_hugetlb_vmemmap_optimized() until after flush_tlb_all() with appropriate memory ordering would close the race without holding vmemmap_remap_rwsem across allocations or locking folio_clear_hugetlb_hwpoison().

It is not an elegant approach, because it requires traversing the list again.

Quoted 4 lines

[Severity: Medium]
Does this acquire the global vmemmap_remap_rwsem on empty or non-HVO restores?

Will fix it.

Quoted 62 lines

Hot paths like hugetlb_vm_op_close() and hugetlb_unreserve_pages() reach hugetlb_vmemmap_restore_folios() via return_unused_surplus_pages() -> update_and_free_pages_bulk() without checking list_empty(). In persistent pool setups with no surplus pages, every hugetlb unmap or unreserve now bounces the global vmemmap_remap_rwsem cacheline and sleeps behind any queued writer.

mm/memory-failure.c static unsigned long folio_free_raw_hwp(struct folio *folio, bool move_flag) · line 2030

Quoted 13 lines
mm/memory-failure.c +2 -0
index 227148d116dd9..32d13d216543c 100644
@@ -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))

[Severity: High]
Can folio_clear_hugetlb_hwpoison() now sleep in atomic context?

guard(vmemmap_remap)() calls down_write(&vmemmap_remap_rwsem), but folio_clear_hugetlb_hwpoison() is called from free_huge_folio() via update_and_free_hugetlb_folio(h, free_folio, true) -> __update_and_free_hugetlb_folio(). When a poisoned folio is not vmemmap-optimized, update_and_free_hugetlb_folio() bypasses free_hpage_work and runs inline even from atomic callers such as hugetlb_fault() under vmf.ptl or bio completion in blkdev_bio_end_io_async() -> bio_release_pages() -> folio_put().

This triggers a sleep-in-atomic bug, or a hard lockup if the interrupted CPU already holds vmemmap_remap_rwsem. At __update_and_free_hugetlb_folio(), the folio is already removed from the pool and restored, so the lock is only needed for the try_memory_failure_hugetlb() caller.

[Severity: High]
Is there a recursive write-lock deadlock on vmemmap_remap_rwsem when get_huge_page_for_hwpoison() drops the last reference to a folio?

get_huge_page_for_hwpoison() holds guard(vmemmap_remap)() across its out_unlock path, where it calls folio_put(folio) when count_increased is true (for example, on concurrent madvise(MADV_HWPOISON) calls against an already-poisoned non-HVO surplus folio):

get_huge_page_for_hwpoison()
guard(vmemmap_remap)() /* down_write */
hugetlb_update_hwpoison() /* PRE_POISONED -> out_unlock */
folio_put(folio)
free_huge_folio()
__update_and_free_hugetlb_folio()
folio_clear_hugetlb_hwpoison()
guard(vmemmap_remap)() <-- self-deadlock

This permanently deadlocks the task while holding mf_mutex, stalling all subsequent memory-failure handling and hugetlb vmemmap operations.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1