All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "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>
Cc: "Muchun Song" <muchun.song@linux.dev>,
	"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] mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore
Date: Fri, 04 Dec 2026 13:28:56 +0000	[thread overview]
Message-ID: <sashiko-outbox-249866@kernel.org> (raw)
In-Reply-To: <20261204122056.85384-1-songmuchun@bytedance.com>

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 <muchun.song@linux.dev>

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().

> diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
> index 336b1405cd6a3..66bfd46de2891 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);
>  }

[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.

> @@ -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);

[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.

> diff --git a/mm/memory-failure.c b/mm/memory-failure.c
> index 227148d116dd9..32d13d216543c 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))

[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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204122056.85384-1-songmuchun@bytedance.com?part=1


  reply	other threads:[~2026-12-04 13:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 12:20 [PATCH] mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore Muchun Song
2026-12-04 13:28 ` sashiko-bot [this message]
2026-12-04 15:05   ` Muchun Song

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=sashiko-outbox-249866@kernel.org \
    --to=sashiko-bot@kernel.org \
    --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=muchun.song@linux.dev \
    --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.