Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.183 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796461469; b=PIE2YlXAlQt4ypsfGFuzZfGsbCfSj04bpJeDEcG1ilbKFgpzy7EY9Mo4jDYgLCX8SCL5 uDkmAYy2hiTWhu4+DJaRghY1C8GvXgAwqwtmbzXwGftVbpDTcjbEQY8msD8XWIuLteGql Fns1a+VTihTa6RbA0khYMFtHjBo6YGdhAmxh6L0WZv89HLF1q9DFfPOEeLVSRIyJLhgfY rkTGv5UzkFQwJ155/lUQf5a4UhpxRfbwQcLEfO0+mOldJLWxMai/ZadouKjn/rS1XBU2A AeLxaGhNwMk/K1ho6IbCstPBN/VQNXrJixQXsZ0+3V60YALxyPDuguxZ8W+4WkDmInw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796461469; 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=mPu4b6AxSSJ22ryXY0fQq++68mASZpbsA4iy7ZWUGzA=; b=F4+f+0ioLSelCYVaHSBEqiakQgbJ+HHNXoLLpwcaEVAj7xWwnXFf5GYMtSxHyDUlCi1Z W5+H4Vh7u9S5updKCIML472X/6gArn9ioWMJT1l311la2HjQ7vWGMOTMpaTQbelvvViHn +dBBPT0mqmVYSTEwxIgUP13l0q5qZBhwcx9+w5Ipr1ZAFx+O73/ma3uhoLHXwqe69d7XE Tr4O5eGyfMV16IooGTKDipWE/yHvy+9OQxKCSu0EUOumZTeIsODP1Wz7zZan1USnt8RDD jlKkw9KWjlWMMVz2cz+aqFBeDKJ4/0iRSNNY9eGU1yvPD2+THZjpdkpHn5X68W/HXyA== 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=LwzNPwPB; arc=none smtp.remote-ip=91.218.175.183 Received: from mta0.migadu.com (out-183.mta0.migadu.com [91.218.175.183]) (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 A745A606A322 for ; Sat, 5 Dec 2026 09:04:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A745A606A322 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=LwzNPwPB X-Envelope-To: sashiko-reviews@lists.linux.dev DKIM-Signature: a=rsa-sha256; bh=ptrTWwugKV8edcvjkMy7AR9Z/rN9a6emZ36ADLO0MVs=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796461465; v=1; x=1797066265; b=LwzNPwPBtfpH8zj/j1yOQOV57TQSM1DW4HDUljWeQ67iLGwsqqIp9L0y8kjJe+9pSqaUeNCQ Lt/2vLVapl5JQbNBU4t5RWs0lS3j27p8cnbj9Uq9trTL2X0p2tmA/l0BNvBKDYqk3CyGjycS60V kfRRDOtDQF2fg69yRD1Tk20k= X-Envelope-To: sashiko-reviews@lists.linux.dev Received: by smtp.migadu.com with ESMTPS id b92a49507e0e2cb9; Sat, 05 Dec 2026 09:04:25 +0000 X-Mizu-Trace-ID: b92a49507e0e2cb9 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 v2] mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore From: Muchun Song In-Reply-To: Date: Sat, 5 Dec 2026 17:04:09 +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: <2A2A1CDF-952F-4129-8F99-83534E172D35@linux.dev> References: <20261205013246.18791-1-songmuchun@bytedance.com> To: sashiko-reviews@lists.linux.dev X-Mailer: Apple Mail (2.3901.200.71.1.16) > On Dec 5, 2026, at 09:56, sashiko-bot@kernel.org wrote: >=20 > 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 > -- >=20 > commit 8481ae594b618f95f12ceb1cd57476e303d9ea76 > Author: Muchun Song >=20 > mm/hugetlb_vmemmap: fix HWPoison race during vmemmap restore >=20 > 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(). >=20 >> 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: >=20 > [Severity: Low] > Does this sentence reverse the race ordering relative to the code and = the > diagram below it? >=20 > 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(). >=20 > 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. >=20 >> 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 >=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 >> @@ -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,10 @@ long hugetlb_vmemmap_restore_folios(struct = list_head *folio_list, >> long ret =3D 0; >> unsigned long flags =3D VMEMMAP_DEFER_TLB_FLUSH; >>=20 >> + 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 =3D __hugetlb_vmemmap_restore_folio(folio, = flags); >=20 > [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. >=20 > 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. >=20 > 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. >=20 > Also, single-folio hugetlb_vmemmap_restore_folio() passes flags =3D=3D = 0 and > flushes the TLB synchronously before = folio_clear_hugetlb_vmemmap_optimized(), > so it does not need the rwsem across alloc_vmemmap_page_list(). >=20 > 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. >=20 > --=20 > Sashiko AI review =C2=B7 = https://sashiko.dev/#/patchset/20261205013246.18791-1-songmuchun@bytedance= .com?part=3D1