From mboxrd@z Thu Jan 1 00:00:00 1970 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=1796514884; b=nlYQAmutYPVcLjYd4YzR0trPmZdZSY3scSJkjHwsC2YuS5FbS/reTM/Xsxs7vlcM58vg X2li9VnmmaBRl5S4o+ALuqgSUn2fmG5h03vmcyLL88Bg8y9BqD0uvSmYQe5YvCd9S0r6c B0+y6vgPo7Cjn+lwfmlKoEPZ4MnYzYCo7Nlodw06TWdmW6cucdrdVsPvTeirOm6XmESWC ZqgnS2IQDjcPnioHCOjt5Yvm0wD0DslQszJrbbmz5kBiOTUtHyiDTLPbNwtyxfigIiH2I UqaX9qOq8YW/XIsr+1hxCp3IQWwVhn038tBRwh9cC/5Czs/VGAFvy4y2rkXZPLu1sLA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796514884; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=IzwjtqSt3D0dyUgDif3RHT9ACsghNyiBISQiHdJV7qk=; b=jo7lXm9xP4KTquvFUyyOo49+yRh3fTiU+TcG3kn+MNq9WZT/J+E9Jw6YGsVtKo6g0U4h sOnKZp8rspMgLtuO6yTNv/qtLXfX8xuYAT1VYXO5Oxvyf/FBMbZduCbr+DjbY8HY8AcEe GLO3frtfFsu+FbR71+F8imBn7rbLJlIeFVxwU7aT1w/xcLF0TEqDIiL2G6/g2XZONFkHE dKcRZDzG8g+32Y3fqjAOoP2IgQkMZqsELqz34p9vrnHvTO8KC6AEjn7GtrDRi3x9YGS60 WUTrwCp56VRpeeaO6gMl/H6bEgahtuBfr2WavM4K3H0saI8YzCJbVpHNvDs1AT8BZlw== 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=PLKdtgZt; 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 24840606A31F; Sat, 5 Dec 2026 23:54:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=PLKdtgZt Received: by smtp.kernel.org (Postfix) with ESMTPSA id CF69D1F00936; Sat, 5 Dec 2026 23:54:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796514883; bh=IzwjtqSt3D0dyUgDif3RHT9ACsghNyiBISQiHdJV7qk=; h=Date:To:From:In-Reply-To:References:Subject; b=PLKdtgZtBrA2LJ9xyWX/V9GEXpswZ12izkbo6P/pf+OF6EvhGFFzIibBskXGkfDvz VEM0xxbPlFH7HguS9pjPdKj8ZipZOWkoB8F3cYAxklCEEaUUQ/eNGa2JU5vAFnvkNu x1WEAruq8Kux0vieXyv57uD4puQ3D103ufFFDbYk= Date: Sat, 05 Dec 2026 15:54:43 -0800 To: mm-commits@vger.kernel.org,stable@vger.kernel.org,rppt@kernel.org, osalvador@suse.de,nao.horiguchi@gmail.com,linmiaohe@huawei.com, joao.m.martins@oracle.com,david@kernel.org,songmuchun@bytedance.com, akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261205121205.83068-1-songmuchun@bytedance.com> References: <20261205121205.83068-1-songmuchun@bytedance.com> Subject: + mm-memory-failure-fix-races-when-handling-free-hugetlb-folios.patch added to mm-new branch Message-Id: <20261205235443.CF69D1F00936@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list The patch titled Subject: mm/memory-failure: fix races when handling free HugeTLB folios has been added to the -mm mm-new branch. Its filename is mm-memory-failure-fix-races-when-handling-free-hugetlb-folios.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-memory-failure-fix-races-when-handling-free-hugetlb-folios.patch This patch will later appear in the mm-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. The mm-new branch of mm.git is not included in linux-next If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Muchun Song Subject: mm/memory-failure: fix races when handling free HugeTLB folios Date: Sat, 5 Dec 2026 20:12:05 +0800 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. CPU 0 CPU 1 try_memory_failure_hugetlb() get_huge_page_for_hwpoison() spin_unlock_irq(&hugetlb_lock) return MF_HUGETLB_FREED folio = page_folio(p) remove_hugetlb_folio() __update_and_free_hugetlb_folio() folio_clear_hugetlb_hwpoison() folio_lock(folio) free_frozen_pages() __free_pages_prepare() free_page_is_bad() /* PG_locked is set. */ bad_page() Cleanup can also interpret reused tail-page metadata as raw_hwp_page entries. Resolving page_folio() again does not ensure that it identifies the folio originally marked HWPoison. CPU 0 CPU 1 try_memory_failure_hugetlb() get_huge_page_for_hwpoison() spin_unlock_irq(&hugetlb_lock) return MF_HUGETLB_FREED folio = page_folio(p) remove_hugetlb_folio() __update_and_free_hugetlb_folio() hugetlb_vmemmap_restore_folio() __folio_clear_hugetlb() folio_clear_hugetlb_hwpoison() free_frozen_pages() /* Tail private is reused. */ folio_lock(folio) if (hwpoison_filter(p)) folio_clear_hugetlb_hwpoison() folio_free_raw_hwp(..., true) __folio_free_raw_hwp() /* Reads and clears reused private. */ llist_del_all(raw_hwp_list_head(folio)) Batched vmemmap restoration exposes another race in the same window: HPG_vmemmap_optimized is cleared before the final TLB flush. Cleanup can then write HWPoison to a raw tail page through a stale shared mapping, corrupting metadata shared by other HugeTLB folios. CPU 0 CPU 1 try_memory_failure_hugetlb() get_huge_page_for_hwpoison() hugetlb_update_hwpoison() spin_unlock_irq(&hugetlb_lock) return MF_HUGETLB_FREED folio = page_folio(p) remove_hugetlb_folio() hugetlb_vmemmap_restore_folios() __hugetlb_vmemmap_restore_folio() /* Restore without a TLB flush. */ folio_clear_hugetlb_vmemmap_optimized() folio_lock(folio) if (hwpoison_filter(p)) folio_clear_hugetlb_hwpoison() folio_test_hugetlb_vmemmap_optimized() -> false folio_free_raw_hwp(..., true) __folio_free_raw_hwp() /* May use a stale shared TLB mapping. */ SetPageHWPoison(raw tail) flush_tlb_all() 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. Link: https://patch.msgid.link/20261205121205.83068-1-songmuchun@bytedance.com Fixes: 8c6c2ecb4466 ("HWPOSION, hugetlb: recover from free hugepage error when !MF_COUNT_INCREASED") Fixes: 161df60e9e89 ("mm, hwpoison, hugetlb: support saving mechanism of raw error pages") Fixes: c24f188b2289 ("hugetlb: batch TLB flushes when restoring vmemmap") Signed-off-by: Muchun Song Signed-off-by: Andrew Morton Cc: Oscar Salvador Cc: Miaohe Lin Cc: Mike Rapoport Cc: David Hildenbrand Cc: Naoya Horiguchi Cc: Joao Martins Cc: mm/memory-failure.c | 46 +++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 18 deletions(-) --- a/mm/memory-failure.c~mm-memory-failure-fix-races-when-handling-free-hugetlb-folios +++ a/mm/memory-failure.c @@ -1958,6 +1958,7 @@ static unsigned long __folio_free_raw_hw #define MF_HUGETLB_FOLIO_PRE_POISONED 3 /* folio already poisoned */ #define MF_HUGETLB_PAGE_PRE_POISONED 4 /* exact page already poisoned */ #define MF_HUGETLB_RETRY 5 /* hugepage is busy, retry */ +#define MF_HUGETLB_FILTERED 6 /* rejected by hwpoison filter */ /* * Set hugetlb folio as hwpoisoned, update folio private raw hwpoison list * to keep track of the poisoned pages. @@ -2070,6 +2071,17 @@ static int get_huge_page_for_hwpoison(un } /* + * Free folios have no reference pinning them. Filter and clear their + * HWPoison state under hugetlb_lock to prevent concurrent dissolution + * and reuse. + */ + if (ret == MF_HUGETLB_FREED && hwpoison_filter(page)) { + folio_clear_hugetlb_hwpoison(folio); + ret = MF_HUGETLB_FILTERED; + goto out_unlock; + } + + /* * Clearing hugetlb_migratable for hwpoisoned hugepages to prevent them * from being migrated by memory hotremove. */ @@ -2129,8 +2141,22 @@ retry: else action_result(pfn, MF_MSG_HUGE, MF_FAILED); return rv; + case MF_HUGETLB_FILTERED: + return -EOPNOTSUPP; + case MF_HUGETLB_FREED: + /* + * PageHWPoison prevents allocation or demotion from the HugeTLB + * pool. The folio cannot be locked without a reference. + */ + if (__page_handle_poison(p) > 0) { + page_ref_inc(p); + res = MF_RECOVERED; + } else { + res = MF_FAILED; + } + return action_result(pfn, MF_MSG_FREE_HUGE, res); default: - WARN_ON((res != MF_HUGETLB_FREED) && (res != MF_HUGETLB_IN_USED)); + WARN_ON(res != MF_HUGETLB_IN_USED); break; } @@ -2142,26 +2168,10 @@ retry: if (migratable_cleared) folio_set_hugetlb_migratable(folio); folio_unlock(folio); - if (res == MF_HUGETLB_IN_USED) - folio_put(folio); + folio_put(folio); return -EOPNOTSUPP; } - /* - * Handling free hugepage. The possible race with hugepage allocation - * or demotion can be prevented by PageHWPoison flag. - */ - if (res == MF_HUGETLB_FREED) { - folio_unlock(folio); - if (__page_handle_poison(p) > 0) { - page_ref_inc(p); - res = MF_RECOVERED; - } else { - res = MF_FAILED; - } - return action_result(pfn, MF_MSG_FREE_HUGE, res); - } - page_flags = folio->flags.f; if (!hwpoison_user_mappings(folio, p, pfn, flags)) { _ Patches currently in -mm which might be from songmuchun@bytedance.com are powerpc-mm-make-vmemmap_populate_compound_pages-static.patch powerpc-mm-drop-powerpc-vmemmap_can_optimize.patch mm-sparse-vmemmap-drop-vmemmap_can_optimize.patch mm-sparse-vmemmap-drop-pgmap-from-vmemmap-population-apis.patch mm-hugetlb_vmemmap-fix-hwpoison-state-races-during-optimization.patch mm-hugetlb_vmemmap-fix-pmd-split-tlb-flush-race.patch mm-hugetlb_vmemmap-fix-deadlock-during-pmd-split.patch mm-list_lru-fix-potential-infinite-loop-on-xa-node-allocation-failure.patch mm-mm_init-verify-zones-for-template-initialized-pages.patch mm-mm_init-factor-out-common-struct-page-initialization.patch mm-mm_init-simplify-zone-device-page-initialization.patch mm-mm_init-factor-out-compound-page-initialization.patch mm-hugetlb-drop-boot-time-hvo-handling-for-gigantic-folios.patch mm-hugetlb-initialize-all-struct-pages-for-bootmem-hugepages.patch mm-hugetlb-use-common-compound-page-initialization-helper.patch mm-mm_init-make-__init_single_page-static.patch mm-cma-centralize-cma-pageblock-initialization.patch mm-cma-localize-init_cma_pageblock-to-cmac.patch mm-mm_init-move-pageblock-migratetype-init-to-memmap-helpers.patch kmsan-encapsulate-per-page-metadata-access.patch mm-page_ext-fix-client-data-alignment.patch mm-page_ext-always-use-vmalloc-for-section-page-extensions.patch mm-page_ext-use-common-allocation-and-initialization-entry-points.patch mm-page_ext-allocate-page-extensions-early-for-sparsemem.patch mm-page_ext-inline-unchecked-page-extension-lookup.patch kmsan-fix-metadata-loss-during-hugetlb-struct-page-reinitialization.patch mm-hugetlb_vmemmap-remove-the-vmemmap-savings-boot-message.patch mm-hugetlb_vmemmap-fold-vmemmap_should_optimize-into-its-callers.patch mm-hugetlb_vmemmap-drop-the-redundant-hstate-argument.patch mm-hugetlb_vmemmap-open-code-free_vmemmap_page.patch mm-hugetlb_vmemmap-drop-obsolete-restore-refcount-warning.patch mm-vmemmap-optimization-centralize-the-vmemmap-optimization-support-check.patch mm-memory-failure-fix-races-when-handling-free-hugetlb-folios.patch