Skip to contentSkip to conversation

mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore

PATCH In review Muchun Song 3 participants 3 messages Dec 5

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

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

---
v2:
- 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/hugetlb_vmemmap.c | 11 ++++++++++-
 mm/memory-failure.c  |  3 ++-
 2 files changed, 12 insertions(+), 2 deletions(-)
mm/hugetlb_vmemmap.c +10 -1
index 336b1405cd6a..18b9ad149347 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,10 @@ long hugetlb_vmemmap_restore_folios(struct list_head *folio_list,
 	long ret = 0;
 	unsigned long flags = VMEMMAP_DEFER_TLB_FLUSH;
 
+	if (list_empty(folio_list))
+		return 0;
+
+	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 -1
index 227148d116dd..4f313881e65c 100644
@@ -2138,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);

base-commit: 1a7610248a3802e38124ce66b07eef12277be9a0

Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] mm/hugetlb_vmemmap: vmemmap_remap_rwsem held across restore allocs - [Low] mm/hugetlb_vmemmap…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] mm/hugetlb_vmemmap: vmemmap_remap_rwsem held across restore allocs
- [Low] mm/hugetlb_vmemmap: commit message reverses race ordering vs. code and diagram
--

commit 8481ae594b618f95f12ceb1cd57476e303d9ea76
Author: Muchun Song <[email protected]>

mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore

This commit moves vmemmap_remap_rwsem read locking from vmemmap_restore_folio() into hugetlb_vmemmap_restore_folio() and hugetlb_vmemmap_restore_folios() so it stays held through the final TLB flush, and takes vmemmap_remap_rwsem for write around folio_clear_hugetlb_hwpoison() in try_memory_failure_hugetlb().

Quoted 4 lines

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:

[Severity: Low]
Does this sentence reverse the race ordering relative to the code and the diagram below it?

Once remove_pool_hugetlb_folio() selects a free folio for a restore batch, remove_hugetlb_folio() clears HPG_freed under hugetlb_lock. A subsequent get_huge_page_for_hwpoison() then sees both HPG_freed and HPG_migratable clear, returns MF_HUGETLB_RETRY, and never reaches hwpoison_filter().

To hit the race, memory failure must run first while HPG_freed is still set and mark the folio HWPoison, before the folio is selected for the restore batch.

Quoted 4 lines
mm/hugetlb_vmemmap.c +0 -0
index 336b1405cd6a3..18b9ad149347b 100644

[ ... ]

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

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

Quoted 10 lines
mm/hugetlb_vmemmap.c +4 -0
@@ -458,6 +463,10 @@ long hugetlb_vmemmap_restore_folios(struct list_head *folio_list,
 	long ret = 0;
 	unsigned long flags = VMEMMAP_DEFER_TLB_FLUSH;
 
+	if (list_empty(folio_list))
+		return 0;
+
+	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]
Can holding vmemmap_remap_rwsem across alloc_vmemmap_page_list() stall concurrent writers and block freed HVO hugepages from returning to the buddy allocator under memory pressure?

Previously vmemmap_restore_folio() allocated vmemmap pages before taking the rwsem. Hoisting the lock into hugetlb_vmemmap_restore_folios() holds the global rwsem across an entire batch of GFP_KERNEL | __GFP_RETRY_MAYFAIL allocations (up to 4,095 pages per 1GB folio), which can stall in reclaim.

If hugetlb_vmemmap_optimize_folios() (e.g. from set_max_huge_pages()) queues on down_write(&vmemmap_remap_rwsem) during that stall, rwsem fairness blocks subsequent readers including free_hpage_workfn(), preventing freed HVO hugepages from returning to the buddy allocator to relieve that pressure.

Also, single-folio hugetlb_vmemmap_restore_folio() passes flags == 0 and flushes the TLB synchronously before folio_clear_hugetlb_vmemmap_optimized(), so it does not need the rwsem across alloc_vmemmap_page_list().

Deferring folio_clear_hugetlb_vmemmap_optimized() until after flush_tlb_all() in hugetlb_vmemmap_restore_folios(), or pre-allocating vmemmap pages outside vmemmap_remap_rwsem, would avoid holding the rwsem across reclaiming allocations.

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

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] mm/hugetlb_vmemmap: vmemmap_remap_rwsem held across restore allocs
- [Low] mm/hugetlb_vmemmap: commit message reverses race ordering vs. code and diagram
--

commit 8481ae594b618f95f12ceb1cd57476e303d9ea76
Author: Muchun Song <[email protected]>

mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore

This commit moves vmemmap_remap_rwsem read locking from vmemmap_restore_folio() into hugetlb_vmemmap_restore_folio() and hugetlb_vmemmap_restore_folios() so it stays held through the final TLB flush, and takes vmemmap_remap_rwsem for write around folio_clear_hugetlb_hwpoison() in try_memory_failure_hugetlb().

Quoted 4 lines

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:

[Severity: Low]
Does this sentence reverse the race ordering relative to the code and the diagram below it?

Once remove_pool_hugetlb_folio() selects a free folio for a restore batch, remove_hugetlb_folio() clears HPG_freed under hugetlb_lock. A subsequent get_huge_page_for_hwpoison() then sees both HPG_freed and HPG_migratable clear, returns MF_HUGETLB_RETRY, and never reaches hwpoison_filter().

To hit the race, memory failure must run first while HPG_freed is still set and mark the folio HWPoison, before the folio is selected for the restore batch.

Quoted 4 lines
mm/hugetlb_vmemmap.c +0 -0
index 336b1405cd6a3..18b9ad149347b 100644

[ ... ]

Quoted 28 lines
mm/hugetlb_vmemmap.c +3 -1
@@ -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,10 @@ long hugetlb_vmemmap_restore_folios(struct list_head *folio_list,
 	long ret = 0;
 	unsigned long flags = VMEMMAP_DEFER_TLB_FLUSH;
 
Quoted 12 lines

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

Quoted 7 lines
mm/hugetlb_vmemmap.c +4 -0
+	if (list_empty(folio_list))
+		return 0;
+
+	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]
Can holding vmemmap_remap_rwsem across alloc_vmemmap_page_list() stall concurrent writers and block freed HVO hugepages from returning to the buddy allocator under memory pressure?

Although I don't think there are any major problems with the current approach, it seems that the approach holding vmemmap_remap_rwsem is also not very ideal. Therefore, I will propose a completely new approach to solve this problem. So everyone can ignore this version for now and wait for the V3 version.

Thanks.

Quoted 22 lines

Previously vmemmap_restore_folio() allocated vmemmap pages before taking the rwsem. Hoisting the lock into hugetlb_vmemmap_restore_folios() holds the global rwsem across an entire batch of GFP_KERNEL | __GFP_RETRY_MAYFAIL allocations (up to 4,095 pages per 1GB folio), which can stall in reclaim.

If hugetlb_vmemmap_optimize_folios() (e.g. from set_max_huge_pages()) queues on down_write(&vmemmap_remap_rwsem) during that stall, rwsem fairness blocks subsequent readers including free_hpage_workfn(), preventing freed HVO hugepages from returning to the buddy allocator to relieve that pressure.

Also, single-folio hugetlb_vmemmap_restore_folio() passes flags == 0 and flushes the TLB synchronously before folio_clear_hugetlb_vmemmap_optimized(), so it does not need the rwsem across alloc_vmemmap_page_list().

Deferring folio_clear_hugetlb_vmemmap_optimized() until after flush_tlb_all() in hugetlb_vmemmap_restore_folios(), or pre-allocating vmemmap pages outside vmemmap_remap_rwsem, would avoid holding the rwsem across reclaiming allocations.

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