Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.102 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796523986; b=ASq+9K2+3VzSZXQeXtbHTsskoShnjurZv7H6JQy1eRCYXXJxmmYNiFUd1YGVxCeuMXJa F7UPnnX+JV9lYIyCOBXfJDcBdCpwSyf4eWQYtnqsY4NRbnZ/NVzWNFJEwu90mLI/mfM2Q bpnpZz7F2WiWw2Pd0gJTQglyTJ9vckDY+dMv774GyJbbUXyU8gMd4r2ymAnOvpa+4TvCG cPy4WJcBKQ6KT1NQBv1panXD0dEL8qELF3p/nRDzKBkTS3qCMv3OcXbxNtsqBO1rzy6XE fmHx5j+K6dHqaJe7AyMITLPXApnZ0zN5VBsA0ee3rs5QcqsoHpO7oxsuhKDLb8hxKTA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796523986; 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=iMz1Oa4ntTfX61+F479jiTR7vC4nkyCGGn60qRfta3M=; b=1o8OFArfQtYHb0rKZ0S0EtVDPt0p6liYJUhMdumHcDwrRMg2tQsyDQOSZ3asGsTSB3XL fu33+CLNpGVKHoPwAyKklGWWG4vbOXP8UHpBAFBc4kNXx0TrwlXOTRnvS6YqHLwSXX6Hu HCwbRN2CD/0SFfLUmH/RX4ZPW/eq6/MuSHO6GXPR06GhE+s12frTRL9rwYwewMdhxs3si iZlKK2z6bh3cosO9fyVQUFAaxTT3CC4AtIY/d8cjReFkfMADmTClan9KUL/e/c/7vWSUO QoILIvaUwIRh8oUoiDQfV9u2l05Oqmp+wHDaVLySB5u0V6/GYl8MhJcd3l7Wb6CElkg== 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="fbO/g7tx"; arc=none smtp.remote-ip=95.215.58.102 Received: from mta1.migadu.com (out-102.mta1.migadu.com [95.215.58.102]) (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 C79DB606A31F for ; Sun, 6 Dec 2026 02:26:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C79DB606A31F 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=fbO/g7tx X-Envelope-To: stable@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=AmLhGLm4DaWVnnu4lImx/4akIwLkrwpNNReGlINdlxs=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796523982; v=1; x=1797128782; b=fbO/g7txxXBk+w+6/o/NQdj19k4+lA1WFXpW6KScR5NOz8Yvt/3SRzCMcAeI3IgJEqE+zFSo Oa+KMxrgBPLcuRWDL0RGoeKVi/cC57qc0NL2Jeg2aO/rUp/fZJYl/a7HqcTblzdUynPBWDBdZPY orBOG00+quwJ5T4bzxy30UaY= X-Envelope-To: stable@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id faa71b1fc85788c5; Sun, 06 Dec 2026 02:26:22 +0000 X-Mizu-Trace-ID: faa71b1fc85788c5 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset=us-ascii X-Mailing-List: stable@vger.kernel.org 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 v3] mm/memory-failure: fix races when handling free HugeTLB folios From: Muchun Song In-Reply-To: <20261205173355.b23201f43437ce8a182a7d27@linux-foundation.org> Date: Sun, 6 Dec 2026 10:26:05 +0800 Cc: Muchun Song , Muchun Song , Oscar Salvador , Miaohe Lin , Mike Rapoport , David Hildenbrand , Naoya Horiguchi , Joao Martins , linux-mm@kvack.org, stable@vger.kernel.org Content-Transfer-Encoding: quoted-printable Message-Id: References: <20261205121205.83068-1-songmuchun@bytedance.com> <20261205173355.b23201f43437ce8a182a7d27@linux-foundation.org> To: Andrew Morton X-Mailer: Apple Mail (2.3901.200.71.1.16) > On Dec 6, 2026, at 09:33, Andrew Morton = wrote: >=20 > On Sat, 5 Dec 2026 20:12:05 +0800 Muchun Song = wrote: >=20 >> try_memory_failure_hugetlb() does not hold a folio reference when >> get_huge_page_for_hwpoison() returns MF_HUGETLB_FREED, but it still >> locks the folio and may clear its HugeTLB HWPoison state after = dropping >> hugetlb_lock. >>=20 >> A concurrent pool shrink can dissolve and free the folio during this >> window. Locking it can race with the page allocator's free checks and >> cause a bad-page report and a leak, or modify a page that has already >> been freed or reused. >>=20 >> ... >>=20 >> Perform free-folio filtering and HWPoison cleanup in the same >> hugetlb_lock critical section that identifies and marks the folio. >> This prevents concurrent dissolution and vmemmap restoration during >> cleanup. >>=20 >> Only lock in-use folios, whose lifetime is pinned by a reference. >=20 > I did A/B testing of this. >=20 > : A test paused memory-failure handling after it found a free HugeTLB > : folio. Pool shrink then freed that folio to the buddy allocator. = On > : A, memory-failure handling resumed and locked the freed folio, = despite > : its zero reference count. On B, the same ordering occurred, but the > : patched code returned without locking the freed folio. B's = injection > : returned EBUSY, so the result confirms the lock safety fix, not > : successful page recovery. Neither run showed reuse of the freed = page. >=20 > Tested-by: Andrew Morton >=20 Thanks for your testing. >=20 >=20 > I hit another issue while testing. Please check the proposed fix: >=20 >=20 > From: Andrew Morton > Subject: mm/memory-failure: don't leave a filtered HugeTLB page = poisoned > Date: Sat, 05 Dec 2026 14:50:00 -0800 >=20 > When hwpoison_filter() rejects an error in a HugeTLB page, > memory_failure() returns -EOPNOTSUPP but leaves the page marked as > poisoned. >=20 > The filter rejects a page when a tester has used the hwpoison-inject > module's corrupt-filter-* debugfs files to limit a memory failure test = to > one filesystem, one memcg or pages with certain flags, and the page is = not > one of those. Nothing else registers a filter, so only such tests are > affected. Found by reading the code. >=20 > Depending on where the error was, the folio stays poisoned, or a tail = page > still has PG_hwpoison set when the folio is later freed to the page > allocator. free_pages_prepare() only refuses a PG_hwpoison page at = order > 0, so that page goes onto the free lists. When it is later allocated, = a > kernel with page checking enabled (CONFIG_DEBUG_VM, init_on_alloc, > init_on_free) quietly discards the allocated block and retries, so the > memory is lost. Other kernels hand the page out with PG_hwpoison set. >=20 > A/B testing result: the same hwpoison filter rejected injections into > four distinct HugeTLB folios on A and B kernels. On A, injections = into > the head of both a free and a mapped folio left KPF_HWPOISON set. On > B, the head flag was clear after those injections. Tail injections = did > not leave the head flag set on A, so the observed A failure concerns > the head cases. >=20 > Fixes: 161df60e9e89 ("mm, hwpoison, hugetlb: support saving mechanism = of raw error pages") > Signed-off-by: Andrew Morton LGTM. But some nits bellow. Acked-by: Muchun Song (ByteDance) Thanks. > Cc: Miaohe Lin > Cc: Naoya Horiguchi > Cc: Muchun Song > Cc: Oscar Salvador > Cc: David Hildenbrand >=20 > mm/memory-failure.c | 36 ++++++++++++++++++++++++++++++++++-- > 1 file changed, 34 insertions(+), 2 deletions(-) >=20 > --- = a/mm/memory-failure.c~mm-memory-failure-dont-leave-a-filtered-hugetlb-page= -poisoned > +++ a/mm/memory-failure.c > @@ -2003,6 +2003,38 @@ static int hugetlb_update_hwpoison(struc > return ret; > } >=20 > +/** > + * hugetlb_undo_hwpoison - undo hugetlb_update_hwpoison() for a = filtered error > + * @folio: the hugetlb folio which hugetlb_update_hwpoison() marked > + * > + * hwpoison_filter() rejected the error for which = hugetlb_update_hwpoison() > + * returned 0. @folio was not poisoned before that call, so all of = the > + * HWPoison state which it has now came from that call. Remove it. > + * > + * folio_clear_hugetlb_hwpoison() must not be used for this. It is = for a > + * folio which is being freed to the page allocator: it moves = PG_hwpoison to > + * the raw error pages, and it does nothing if the folio's vmemmap is I think these comments were generated by AI, so they're not very = concise. I feel we should rewrite them. This information is suitable for commit message, but it seems a bit too = clunky to put directly in the code. > + * optimized. Here @folio stays a hugetlb folio. Only its first = four struct The number "four" here is hardcoded, which isn't ideal. We probably = don't need to mention HVO details, since this function doesn't actually manipulate any = related data structures. > + * pages are written and those are never in the shared vmemmap tail = page. > + * > + * The rejected error was not added to num_poisoned_pages, so it is = not > + * subtracted here. > + * > + * Context: The caller holds mf_mutex, and either a reference on = @folio or > + * hugetlb_lock. We usually use lock-assert. We only use comments when the code can't = express it. > + */ > +static void hugetlb_undo_hwpoison(struct folio *folio) > +{ > + struct raw_hwp_page *p, *next; > + struct llist_node *head; > + > + head =3D llist_del_all(raw_hwp_list_head(folio)); > + llist_for_each_entry_safe(p, next, head, node) > + kfree(p); > + folio_clear_hugetlb_raw_hwp_unreliable(folio); > + folio_clear_hwpoison(folio); > +} > + > static unsigned long folio_free_raw_hwp(struct folio *folio, bool = move_flag) > { > /* > @@ -2076,7 +2108,7 @@ static int get_huge_page_for_hwpoison(un > * and reuse. > */ > if (ret =3D=3D MF_HUGETLB_FREED && hwpoison_filter(page)) { > - folio_clear_hugetlb_hwpoison(folio); > + hugetlb_undo_hwpoison(folio); > ret =3D MF_HUGETLB_FILTERED; > goto out_unlock; > } > @@ -2164,7 +2196,7 @@ retry: > folio_lock(folio); >=20 > if (hwpoison_filter(p)) { > - folio_clear_hugetlb_hwpoison(folio); > + hugetlb_undo_hwpoison(folio); > if (migratable_cleared) > folio_set_hugetlb_migratable(folio); > folio_unlock(folio); > _