Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796472751; b=kSgrIhPrmhdM55MYYWeCWsxLrndIuzIqNg2/VCaMfxkHW1gb6Ts8j0IbYB0Xiez0EUui ncCFdFXLrq+gcumATVm+ZiV7jbizG75n4QrGOKhscpdZifOInDm9U54GM6hprlD5wSjet QueznOk1czrAihKG5l1qN6XM+n4Z1lnMQGeJOGHbWAs9uuUvaQSZD7qUxfEB4xQHAA4vO bt8NI+I4hNp1lkIkH01SjW7vFieYIOlKkPejbSsPbN7oyI7J/ys2IJGxXTE84nRlB/T5d G5SQAvckfqQkeBXvWpPajYhdFUzGXy8dlqYHXSaw2ITdObwr5XBNfPrfsCBAHDPr3Jw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796472751; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=qIJV7qJbvCqJwb1Xx3W+NpN6oK2g7D7lekRl2ko6KuM=; b=triFOGw6O8L7bGUY/VJQWT2OusHIEOrlcO5ZXc30AVrZush05woaZulovd4Q++sEBLAm myb5tbM5U1oAe0stPBZN11geIEY1vGIAKmpXX4bvxJZFMNsUq8jhm85mULEIlgtv6L7rR 5swHyfjSEbEY6d6dMs8oX7JggH9oNqp1ZFV71FZGWEN2NHpXSPFtB9HhMvJddmWqGIVho dMGo79O8QyTa03RqY+Nnt4KV5SZ1wqnuea6zoFmNL4MgWNK7Vxyntc+p9juTkdeQBAdll aw4MNa84DX3nWiVc+XFV0vXnnG+Zoj68f+tV5COJHGuKonCRYWmwhhvT9j3bdlj0lig== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=google header.b=V8rSZx7g; arc=none smtp.remote-ip=209.85.216.43 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) (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 D4809606A339 for ; Sat, 5 Dec 2026 12:12:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D4809606A339 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=google header.b=V8rSZx7g Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-3b54096b2d0so1264803a91.1 for ; Sat, 05 Dec 2026 04:12:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1796472750; x=1797077550; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=qIJV7qJbvCqJwb1Xx3W+NpN6oK2g7D7lekRl2ko6KuM=; b=V8rSZx7gMYbI12CEeNZk19KxH7t+uBRrXNfDt7+ey/LO7N7f6Uaqge+INOCCstLkDB oa9AHk8/vCpxnrLCT++GhenG1cjSDhOrNze6VgqIwXYrTrJqO9RXixsyaXL8L0vjM/W9 mO8MYdBDoudaE8pRGuv55Q9H0Dsmmmjtn87L1nsRs9W5bkEGVTyrq1KLBJpLpN3tPi9m THKrR2O3qn+YjJSNaGn86vfAcb/lnXF2czsVWux8qzFqMWWbCN8TVn5L2tuguBZ6bbKw X6pOtXE6vdxihdcSKKKJURLs8xsWdTHCeehHaxEqppmpRhFbt0ZVsGztrEiomOSX4DmZ hikQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796472750; x=1797077550; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=qIJV7qJbvCqJwb1Xx3W+NpN6oK2g7D7lekRl2ko6KuM=; b=T0Ryzqd32ns6t1AqINW/sMoJhfIc6pTy8pqC/IOPSMoBpfGcKAgyxfHhbWvZweOti9 wJhZ6b8TJ52xnhxkf4GHwqtZVg1p4G8EfBBXi4PRwM8e99N+NI8B2tvb9cucQMPXN2kt raxb8nZEjTfSroqHoi+/5GKaUysM3WNq8tfil3+y4nkUsoq4QemoC8bLMUk3hN5PumMb gFhlPHO+1uWQnAtMmLHfuL1S8XueHN/xdelB9gVov7nc9XmIr/Xt6Pe1smZ+XAnNgXNg Lq3fRngO9FwRLPodcztwQ3oK8B8bjvXB0LibEWIHyEIlH2dwTSsZkkygEAXniMQyuwIl YvVg== X-Forwarded-Encrypted: i=1; AL+hErHwVmB30uqwz10ARq+F/EO08VLVqbhZ1+GXIV45Qrzg0fJpV8aB1lcwacm9ptkDIVQN33YnrEQ=@vger.kernel.org X-Gm-Message-State: AB4q+uFwZdSDH9AA8MGYLJtBqeQ47jA8Rad9rnkJ35ojMdpsbK4a1U+K jP6x39es1iYqcbDkNkUOEbATXUI14U2pTGTNZpv48zNnp3PJcZjLhr0w536xpVR+72E= X-Gm-Gg: AZFdTZeDVIpSknjUFP+7dSbgVZU0MfyMZEizQMXFNbxT59PpEE42MG9nw+XnjWZDxY9 7rXAoUc2Fbi8dzuEJKdXjtYPHB3K+uGDHtesTsrYSJ2UKtsb3GH4osGNNy4mexS9bs8tz/o8+W8 5eCc1Mxeh2+33C96WAzqbc0aOMNFY8hwGh8JZWsW5FM7qK1tZ/n7pm7oPCM5ZBJLN0BRDSaiokx FC35o9igxdBz5c+4aT3USB8WTs0S/UyNpy6crvXedOCjCckw/UZ4aeIohKpFkDP/IKSqKI13MBm vdxVrCNYoYkg8IjYwu0fSaL9M1GT8RgLvEgx0ubavD2m1tHj4OaOpLLrE8VlwMY4axFLDTa7Q82 syBeFXc+HpuuUKkPco1j6K5r486Z4atRN2LJtqZRQUT0IRiJG9eZI+Qf0uYGawO8b8oqxN98jc9 RpZPkGQ/FMb9j0s12UcKN5LvBEU9655kAsjKr25PUExX4AYJdUWkEF5qoEoHCc5auUNJoIqqlTG L4rFAUmGRSued3zePQRSDXWpXXt0dexZPdExjMC690c/j6X6G/YfzHZgwaTQBN2p6BlA8bARqwt /ido00fm+HiqUUAuVrUPz4M2cbXxj9GSs+6+P+QVfeuH7C/OeCrcJ5EdgErmmvgEGx2hd3KSrav 1 X-Received: by 2002:a17:90b:2cd0:b0:3b3:cfc5:60b3 with SMTP id 98e67ed59e1d1-3b5e06bff17mr4879697a91.63.1796472749852; Sat, 05 Dec 2026 04:12:29 -0800 (PST) Received: from G6L4RL2QG9 ([2409:8a00:1832:6900:bdaf:c63b:725e:ac60]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-ce123461206sm2856847a12.2.2026.12.05.04.12.25 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sat, 05 Dec 2026 04:12:29 -0800 (PST) From: Muchun Song To: Oscar Salvador , Andrew Morton , Miaohe Lin , Mike Rapoport Cc: Muchun Song , David Hildenbrand , Naoya Horiguchi , Joao Martins , linux-mm@kvack.org, stable@vger.kernel.org, Muchun Song Subject: [PATCH v3] mm/memory-failure: fix races when handling free HugeTLB folios Date: Sat, 5 Dec 2026 20:12:05 +0800 Message-ID: <20261205121205.83068-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.54.0 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-Transfer-Encoding: 8bit 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. 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") Cc: stable@vger.kernel.org Signed-off-by: Muchun Song --- v3: - Handle free-folio filtering and HWPoison cleanup under hugetlb_lock, and avoid folio_lock() on unreferenced folios - Drop the vmemmap_remap_rwsem changes from v2, as hugetlb_lock now keeps remove_hugetlb_folio() out of the cleanup window v2: https://lore.kernel.org/20261205013246.18791-1-songmuchun@bytedance.com/ - Take vmemmap_remap_rwsem for write in try_memory_failure_hugetlb() instead of folio_clear_hugetlb_hwpoison() to avoid sleeping in atomic context and recursive locking on the free path (reported by sashiko-bot) - Skip taking vmemmap_remap_rwsem in hugetlb_vmemmap_restore_folios() when the folio list is empty (suggested by sashiko-bot) v1: https://lore.kernel.org/20261204122056.85384-1-songmuchun@bytedance.com/ --- mm/memory-failure.c | 46 +++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index 227148d116dd..bc38993bd0d6 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -1958,6 +1958,7 @@ static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag) #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. @@ -2069,6 +2070,17 @@ static int get_huge_page_for_hwpoison(unsigned long pfn, int flags, goto out_unlock; } + /* + * 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 @@ static int try_memory_failure_hugetlb(unsigned long pfn, int flags) 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 @@ static int try_memory_failure_hugetlb(unsigned long pfn, int flags) 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)) { base-commit: d0d65ff2e433e741c23715e8359c4a248d432179 -- 2.54.0