Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.1 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796396768; b=LNJV0BCEYHrT3ZycFTnEn8ogX8cFtyQ3LWsD+QVeGcZWE00Q6hKO1+jcaReA9RmYKgXw un4wEdx8d0g+jWzPXbPA/4d9PKqA0pmFB6H4+YhSgH4zN3ZqBa6WQYMXpzeH21di26N8L eQu3i2pCKaqj85L2cAeTIhBl9P/KMKws5BnapAUvxt1KoX66D+arz55D0MhqWMTVmQZ+9 WYLj305EzAOCJ8FRBvb9OELq2xe3OM7+ZpxpBDvc0uPNFPIuh83VDYL5ZWi/hr1i9KpoV RbxPJweb94zQNR1LWJfoYRxGi3p5Bl4HFlrwVsJDI621fYelXTXMBX7gLShGDJv15pg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796396768; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Content-Type:Mime-Version:Subject:From: In-Reply-To:Date:Cc:Content-Transfer-Encoding:Message-Id:References: To:X-Mailer; bh=nEC8EjmgKUZJQ7p4UP8xGq0tCbLdZynDBqTJBSPxK+k=; b=20RWEFqTnE/8G8mH1A7QYCYPSWh9nTzyV/gXeKYNekF7WEZ+mMHa+oInv7CVE5cU+D5E dqlTH5LPVMr2qapdLHH62OdtVzAQumGrgjJ9oCdZ0E8krM5zIR+bV9zLFCjzJTky637+V 8D7tXu7SYkID7C1EU2VcULg3i5pXTBI1BaZwunD18wOZCuaJshoet5M2gGazhF5H0PuW0 lN80Vlqtc4s7bM2/AaZGmbHcpAdwhE5xYfRgXisu/OJlh6n1F3OSzWT3ebZh8C+r5bP+u gcqg7ziPMkq+P8B/jvCSwFSzrMVM91Sp4I5OyfTMKCRnq+Q/zYr0x0LnB+aOWzDVNLg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=h2LU8OEV; arc=none smtp.remote-ip=91.218.175.1 Received: from mta0.migadu.com (out-1.mta0.migadu.com [91.218.175.1]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A46A606A322 for ; Fri, 4 Dec 2026 15:06:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2A46A606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=h2LU8OEV X-Envelope-To: sashiko-reviews@lists.linux.dev DKIM-Signature: a=rsa-sha256; bh=OchZQP4T4YX9jK7xbfQcAFdOanRxNGWTxOoY9oHnJOw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796396766; v=1; x=1797001566; b=h2LU8OEVBjyiaeQCx8j8DLcewMYdHeia5CQPxJfv/ZXzo1U0TiabObMqf95AB5r6IV5Et7q3 CoPHCQlO5SvNqP8yR4H87i6KGRM6AMIJSaZMfTIq+02DsXqdi0GInHLbFHpOdilf0eE1Vn6OOG+ +Py6tCxLffJQ12mh6P50dfUQ= X-Envelope-To: sashiko-reviews@lists.linux.dev Received: by smtp.migadu.com with ESMTPS id 85d70a2a86b2c7d7; Fri, 04 Dec 2026 15:06:06 +0000 X-Mizu-Trace-ID: 85d70a2a86b2c7d7 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset=utf-8 X-Mailing-List: sashiko-reviews@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3901.200.71.1.16\)) Subject: Re: [PATCH] mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore From: Muchun Song In-Reply-To: Date: Fri, 4 Dec 2026 23:05:48 +0800 Cc: Muchun Song , Muchun Song , Oscar Salvador , Andrew Morton , Miaohe Lin , Mike Rapoport , David Hildenbrand , Naoya Horiguchi , Joao Martins , linux-mm@kvack.org Content-Transfer-Encoding: quoted-printable Message-Id: <7F77C8BE-E5AE-494E-AE36-D3905C8C283C@linux.dev> References: <20261204122056.85384-1-songmuchun@bytedance.com> To: sashiko-reviews@lists.linux.dev X-Mailer: Apple Mail (2.3901.200.71.1.16) > On Dec 4, 2026, at 21:28, sashiko-bot@kernel.org wrote: >=20 > Thank you for your contribution! Sashiko AI review found 5 potential That's really awkward. > 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: diff --git a/mm/memory-failure.c b/mm/memory-failure.c index 32d13d216543..4f313881e65c 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -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); > - [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 > -- >=20 > commit a61abe88805bf09f0b488bd7991e61609b7ee6c4 > Author: Muchun Song >=20 > mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore >=20 > 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(). >=20 >> 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 >=20 > [ ... ] >=20 >> @@ -391,13 +394,14 @@ static int vmemmap_restore_folio(const struct = folio *folio, unsigned long flags) >> int ret, nid =3D folio_nid(folio); >> LIST_HEAD(vmemmap_pages); >>=20 >> + rwsem_assert_held(&vmemmap_remap_rwsem); >> + >> start +=3D VMEMMAP_OPTIMIZATION_SIZE; >> ret =3D alloc_vmemmap_page_list(&vmemmap_pages, (end - start) / = PAGE_SIZE, >> nid, GFP_KERNEL); >> if (ret) >> return ret; >>=20 >> - guard(rwsem_read)(&vmemmap_remap_rwsem); >> return vmemmap_restore_range(start, end, &vmemmap_pages, flags); >> } >=20 > [Severity: High] > Can holding vmemmap_remap_rwsem across alloc_vmemmap_page_list() = deadlock in > direct reclaim? >=20 > 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: >=20 > hugetlb_vmemmap_restore_folio() > down_read(&vmemmap_remap_rwsem) > vmemmap_restore_folio() > alloc_vmemmap_page_list(..., GFP_KERNEL) > > 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 >=20 > This hangs the reclaiming task on vmemmap_remap_rwsem and triggers a = lockdep > fs_reclaim circular dependency splat even across separate tasks. >=20 >> @@ -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); >> } >>=20 >> @@ -458,6 +463,7 @@ long hugetlb_vmemmap_restore_folios(struct = list_head *folio_list, >> long ret =3D 0; >> unsigned long flags =3D VMEMMAP_DEFER_TLB_FLUSH; >>=20 >> + 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 =3D __hugetlb_vmemmap_restore_folio(folio, = flags); >=20 > [Severity: Medium] > Will holding vmemmap_remap_rwsem across the entire batch loop stall = writers > and cause a reader convoy under memory pressure? >=20 > 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. >=20 > 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. >=20 > [Severity: Medium] > Does this acquire the global vmemmap_remap_rwsem on empty or non-HVO > restores? Will fix it. >=20 > 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. >=20 >> 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) >>=20 >> 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)) >=20 > [Severity: High] > Can folio_clear_hugetlb_hwpoison() now sleep in atomic context? >=20 > 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(). >=20 > 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. >=20 > [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? >=20 > 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): >=20 > 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 >=20 > This permanently deadlocks the task while holding mf_mutex, stalling = all > subsequent memory-failure handling and hugetlb vmemmap operations. >=20 > --=20 > Sashiko AI review =C2=B7 = https://sashiko.dev/#/patchset/20261204122056.85384-1-songmuchun@bytedance= .com?part=3D1