* [PATCH v2] mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore
@ 2026-12-05 1:32 Muchun Song
2026-12-05 1:56 ` sashiko-bot
0 siblings, 1 reply; 3+ messages in thread
From: Muchun Song @ 2026-12-05 1:32 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>
---
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/20261204122056.85384-1-songmuchun@bytedance.com/
---
mm/hugetlb_vmemmap.c | 11 ++++++++++-
mm/memory-failure.c | 3 ++-
2 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
index 336b1405cd6a..18b9ad149347 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,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);
diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index 227148d116dd..4f313881e65c 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -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
--
2.54.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v2] mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore
2026-12-05 1:32 [PATCH v2] mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore Muchun Song
@ 2026-12-05 1:56 ` sashiko-bot
2026-12-05 9:04 ` Muchun Song
0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-12-05 1:56 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Miaohe Lin,
Mike Rapoport
Cc: Muchun Song, David Hildenbrand, Naoya Horiguchi, Joao Martins,
linux-mm
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 <muchun.song@linux.dev>
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().
> 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.
> diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
> index 336b1405cd6a3..18b9ad149347b 100644
> --- a/mm/hugetlb_vmemmap.c
> +++ b/mm/hugetlb_vmemmap.c
[ ... ]
> @@ -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);
[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.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205013246.18791-1-songmuchun@bytedance.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v2] mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore
2026-12-05 1:56 ` sashiko-bot
@ 2026-12-05 9:04 ` Muchun Song
0 siblings, 0 replies; 3+ messages in thread
From: Muchun Song @ 2026-12-05 9:04 UTC (permalink / raw)
To: sashiko-reviews
Cc: Muchun Song, Muchun Song, Oscar Salvador, Andrew Morton,
Miaohe Lin, Mike Rapoport, David Hildenbrand, Naoya Horiguchi,
Joao Martins, linux-mm
> On Dec 5, 2026, at 09:56, sashiko-bot@kernel.org 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 <muchun.song@linux.dev>
>
> 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().
>
>> 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.
>
>> diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
>> index 336b1405cd6a3..18b9ad149347b 100644
>> --- a/mm/hugetlb_vmemmap.c
>> +++ b/mm/hugetlb_vmemmap.c
>
> [ ... ]
>
>> @@ -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);
>
> [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.
>
> 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.
>
> --
> Sashiko AI review · https://sashiko.dev/#/patchset/20261205013246.18791-1-songmuchun@bytedance.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-05 9:04 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 1:32 [PATCH v2] mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore Muchun Song
2026-12-05 1:56 ` sashiko-bot
2026-12-05 9:04 ` Muchun Song
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox