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=1796520836; b=u5h/+xlz7xmPTbNIiVAHepK6xutew6l3BkHmdGOZ8A1x8EUqekNwyOT/+3X1tQ14Swty 86B54FJFh/prxWcaNAmE7oFitqbI+xEXyryMEZk7UdwIh+38UuVp79Zv7k7yF53+XjitF 9DVBDDPweSw1GZkLKV1USE2Pagmatv9FF0lXdagwT7AYEQb099s8svo+1ItN6IS8OZXbc 33KA/O46d0fwaIsO5xHrQjc31x09MiPQcwK4uOzHb5OyKtBcryxvg4Y8dWJkQez64KkfR gNKxZ2a7ytJunfqApCFH1e00BcRyT6LsOaSuKb3i7ZCVO9IhqxqOF+6Zq+EK33rrbow== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796520836; 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=VLkhTdflZc+3lMhNc9IqFr1JEnH4YTV4dkiMjL1LE4o=; b=D2EHpeSN2D5kPV0JIM9TYvtiEpRK2alKsJrYwpMxPbWHBgydJbtjhn2LzzX7xRjZ94Zz S3KTMbz+J9r3bldK0fp1z/h4PJec9vY+RlOwezwL+8Ni5pL0K+O7OMS+3qU+gcyWsoDKK M+77gyZI7zuTKKVXCsVzYTm4wzSrA4JdVrpBIODCa22L2fQVzHNkCskcqXKB1+9CPPv5H ZE1rgk85veJ0rMyihSkmuqc6tOBh6OymqyEx44PP/ItGaHujdF5M+80yEWMb25s7Pt6op vcnE146o5J+69RBjIRKqGka/7X0mFmgEjHzavxsICXpJ/61rpuVkVtiQR644s9cG4wQ== 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=zQUi4Su9; 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 9E1A0606A321 for ; Sun, 6 Dec 2026 01:33:56 +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=zQUi4Su9 Received: by smtp.kernel.org (Postfix) with ESMTPSA id D38401F0092B; Sun, 6 Dec 2026 01:33:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796520836; bh=VLkhTdflZc+3lMhNc9IqFr1JEnH4YTV4dkiMjL1LE4o=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=zQUi4Su9OOCvxXy24M9DDb9i7ZtstyUZT6YqNmQHrc3D0Moyth2fSCn5lQMpZjH4k ZoukMpWU0mAxK+VmE/BslYdwcwoLF42bj5rmOeXS8T4nvvLNt53E/z/oJ42yTDS1Tz 2Nhc2FimYqiPDyj3GoOVhJTDDZHnwvi+gobdvYdU= Date: Sat, 5 Dec 2026 17:33:55 -0800 From: Andrew Morton To: Muchun Song Cc: Oscar Salvador , Miaohe Lin , Mike Rapoport , Muchun Song , 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: <20261205173355.b23201f43437ce8a182a7d27@linux-foundation.org> In-Reply-To: <20261205121205.83068-1-songmuchun@bytedance.com> References: <20261205121205.83068-1-songmuchun@bytedance.com> 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 Sat, 5 Dec 2026 20:12:05 +0800 Muchun Song wrote: > 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. > > 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. > > ... > > 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. > > Only lock in-use folios, whose lifetime is pinned by a reference. I did A/B testing of this. : 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. Tested-by: Andrew Morton I hit another issue while testing. Please check the proposed fix: 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. 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. Fixes: 161df60e9e89 ("mm, hwpoison, hugetlb: support saving mechanism of raw error pages") Signed-off-by: Andrew Morton Cc: Miaohe Lin Cc: Naoya Horiguchi Cc: Muchun Song Cc: Oscar Salvador Cc: David Hildenbrand mm/memory-failure.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 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,38 @@ static int hugetlb_update_hwpoison(struc return ret; } +/** + * 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 + * optimized. Here @folio stays a hugetlb folio. Only its first four struct + * 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. + */ +static void hugetlb_undo_hwpoison(struct folio *folio) +{ + struct raw_hwp_page *p, *next; + struct llist_node *head; + + 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 +2108,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 +2196,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); _