Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796526773; b=pdGRLZfY7WNFAY4hCOL8zuSmsgx8BMJrhH7Vnr5HOsUxgLGbvb/t2Gdk9ZS0W71eTqDH CeAE54DLcrTBWxtBByLJrsSbB2HW29gimYIU5HOOASiacBqSgb+VE0z9pA+hyyvDAJrEG hsi42a0sWH/K/gHugWxnjDjiIBRzse622aVz0xjXAG+L3BOj8y/o1sDfg5nYf20jw5+bm fdTI9UoqTy8lxfvVkkSgTgucldbysIPS72u24w90baobd4rAf3CN8nBlHs5TD2WiU+CtG uoqUqA20+EAGcdruZXkxi4Jsv7Rmgg2K0+21MS3aag7xnmMVHFI/6EkJT6BxIdHvzwA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796526773; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-Id: In-Reply-To:References:X-Mailer:Mime-Version:Content-Type: Content-Transfer-Encoding; bh=K+ijP5UvnJ2URJxdknv+uMbAqHTSeLTexWYkAeBR3hM=; b=1DYjcOjjzFc3FVlegQiK5+FGuFXe9eyOckQVsJ3x/2mFiR6NtvXtoRN+2h1dnGRpEh67 9iRtPu0m4Zeus598SHUlbbypOgBWrLVvfsswcHvDRNKXCXT0yxGBpKE5M28pfALS111ee MFYKnZkIQQcEymEIwJKcGFvPYJVSo2YZEWxlxvQjpx60nuuYvJgzkpPi6dnNF1MTIf0w8 6vOZm+J1rdniV0HRZukoKbTpjdIywRnAFFbDEiN3pizdl1ZgEK24K/BOXM2I42V6+jt2u LNMg+RVQRzgW5tqxgzyU+SB6EFrR8AUyg3AaWAaeFaTKVLjx2UK98Ymz2+8v3AHl1gg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=rsl5O1IR; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79172606A31F for ; Sun, 6 Dec 2026 03:12:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=rsl5O1IR Received: by smtp.kernel.org (Postfix) with ESMTPSA id E88A01F0093B; Sun, 6 Dec 2026 03:12:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796526773; bh=K+ijP5UvnJ2URJxdknv+uMbAqHTSeLTexWYkAeBR3hM=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=rsl5O1IRTx4nRl5lHSO8Ch+GGB5BG1DBfvwmvAjwE9oXZYUltwNHeknmcr/hEFS2q CSD85ISoLTDpshMr+GE0c0QW8eSqCpKIgrBCF2zxMTcft5IVoxnE8ZYgRVGAmHdAfG 8mUuDp0pLUz5FxfIdc0ybaXeLC8XO1vGUdC/qDlo= Date: Sat, 5 Dec 2026 19:12:52 -0800 From: Andrew Morton To: Muchun Song Cc: Muchun Song , Oscar Salvador , Miaohe Lin , Mike Rapoport , David Hildenbrand , Naoya Horiguchi , Joao Martins , linux-mm@kvack.org, stable@vger.kernel.org Subject: Re: [PATCH v3] mm/memory-failure: fix races when handling free HugeTLB folios Message-Id: <20261205191252.7d9d0b3b2d15b00cef8aefe0@linux-foundation.org> In-Reply-To: References: <20261205121205.83068-1-songmuchun@bytedance.com> <20261205173355.b23201f43437ce8a182a7d27@linux-foundation.org> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) 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 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sun, 6 Dec 2026 10:26:05 +0800 Muchun Song wrote: > > > LGTM. But some nits bellow. > > Acked-by: Muchun Song (ByteDance) Thanks. > > 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. ok. > > + * 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. yup. From: Andrew Morton Subject: mm/memory-failure: don't leave a filtered HugeTLB page poisoned Date: Sat, 05 Dec 2026 14:50:00 -0800 When hwpoison_filter() rejects an error in a HugeTLB page, memory_failure() returns -EOPNOTSUPP but leaves the page marked as poisoned. 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. 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. The filter paths undo hugetlb_update_hwpoison() with folio_clear_hugetlb_hwpoison(). That function 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 optimized or HPG_raw_hwp_unreliable is set. Add hugetlb_undo_hwpoison(), which removes what hugetlb_update_hwpoison() added, and use it at both places. A/B testing result: the same hwpoison filter rejected injections into four distinct HugeTLB folios on adjacent 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. Fixes: 161df60e9e89 ("mm, hwpoison, hugetlb: support saving mechanism of raw error pages") Signed-off-by: Andrew Morton Acked-by: Muchun Song (ByteDance) Cc: Miaohe Lin Cc: Naoya Horiguchi Cc: Oscar Salvador Cc: David Hildenbrand mm/memory-failure.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) --- a/mm/memory-failure.c~mm-memory-failure-dont-leave-a-filtered-hugetlb-page-poisoned +++ a/mm/memory-failure.c @@ -2003,6 +2003,25 @@ static int hugetlb_update_hwpoison(struc return ret; } +/* + * hwpoison_filter() rejected the error which hugetlb_update_hwpoison() just + * recorded. The folio was not poisoned before that, so clear all of its + * HWPoison state. The error was not counted in num_poisoned_pages. + */ +static void hugetlb_undo_hwpoison(struct folio *folio) +{ + struct raw_hwp_page *p, *next; + struct llist_node *head; + + lockdep_assert_held(&mf_mutex); + + head = 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 +2095,7 @@ static int get_huge_page_for_hwpoison(un * and reuse. */ if (ret == MF_HUGETLB_FREED && hwpoison_filter(page)) { - folio_clear_hugetlb_hwpoison(folio); + hugetlb_undo_hwpoison(folio); ret = MF_HUGETLB_FILTERED; goto out_unlock; } @@ -2164,7 +2183,7 @@ retry: folio_lock(folio); if (hwpoison_filter(p)) { - folio_clear_hugetlb_hwpoison(folio); + hugetlb_undo_hwpoison(folio); if (migratable_cleared) folio_set_hugetlb_migratable(folio); folio_unlock(folio); _