All of lore.kernel.org
 help / color / mirror / Atom feed
From: Muchun Song <muchun.song@linux.dev>
To: sashiko-reviews@lists.linux.dev
Cc: Muchun Song <muchun.song@linux.dev>,
	Muchun Song <songmuchun@bytedance.com>,
	Oscar Salvador <osalvador@suse.de>,
	Andrew Morton <akpm@linux-foundation.org>,
	Miaohe Lin <linmiaohe@huawei.com>,
	Mike Rapoport <rppt@kernel.org>,
	David Hildenbrand <david@kernel.org>,
	Naoya Horiguchi <nao.horiguchi@gmail.com>,
	Joao Martins <joao.m.martins@oracle.com>,
	linux-mm@kvack.org
Subject: Re: [PATCH v2] mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore
Date: Sat, 5 Dec 2026 17:04:09 +0800	[thread overview]
Message-ID: <2A2A1CDF-952F-4129-8F99-83534E172D35@linux.dev> (raw)
In-Reply-To: <sashiko-outbox-250917@kernel.org>



> 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



      reply	other threads:[~2026-12-05  9:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2A2A1CDF-952F-4129-8F99-83534E172D35@linux.dev \
    --to=muchun.song@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=joao.m.martins@oracle.com \
    --cc=linmiaohe@huawei.com \
    --cc=linux-mm@kvack.org \
    --cc=nao.horiguchi@gmail.com \
    --cc=osalvador@suse.de \
    --cc=rppt@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=songmuchun@bytedance.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.