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=1796221221; b=216vygJICiOD6CiBN9Fc2dyCilBBC9+a7pD4ZMdQevEbPHd6Day8JEGOkZRQ7Ody4SgW +CUl/EltMC2uU9JXucepJVr8eLWbc0H79dhfneOCHjnZQX5SbTUeIvjFFEjmOdFuKF7S2 seRRm3F1U0MY0lWjoDOvP1pveVQ6aBWh/TO+sdX6+rlWM4gb3Z0CGuwpqMtGctJXievOG BTxLtm13+jwy4QGNJZFk16Vn6VujIbNX9/zv8xTpY8DlmONhYoB7y46KDjo8IIB4cDuKu zQsNSVTheH9b2LeOow0rdmy0zWx1qZzB+0Ymj3+87p7WRU2DNAU11/in++362mjA7LA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796221221; h=Received:DKIM-Signature:Subject:To:Cc:From:Date:Message-ID: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=zfxKBA5lPwqUSXetJwiJuabj2eWm+wCtwiHg5MRLjNQ=; b=KCmEWZmHWUugrlnQvS9lIZc7Pn8T0nabxE5XqeE7UaYipoelOS3EEH3TsTS7P0MXueqA ETd5fJ9wY4fCo8r2hBgWIBhWzeB1YDGX8bUOjOfn7SJVNy04jd2gvXuKP3C4LrnXhYLzK BurO1vTlP8Wa5ekmFX2lkBSQitZcdKiTn4d4wbh4PiU2S+jInPJxAPBUV3NyfHtDBYzVC uneHADAZbt1P6OaLQcnKPdOmqPiI6q4Zra9HUlDIfkDWOJ0WE8lldm0EhQKPH4hQ6/qTO ZXANZ459x4h2M5Lfm1hDDfP9D5jmB0U5uoCqVsswQPPmkxTBWcNFX60f9M7du8ss3HA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=KnnyQ8v2; 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 DE4B3606A31A for ; Wed, 2 Dec 2026 14:20:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=KnnyQ8v2 Received: by smtp.kernel.org (Postfix) with ESMTPSA id D483C1F0089A; Wed, 2 Dec 2026 14:20:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796221221; bh=zfxKBA5lPwqUSXetJwiJuabj2eWm+wCtwiHg5MRLjNQ=; h=Subject:To:Cc:From:Date; b=KnnyQ8v2uWyis7Abo5OcoOVFOpakkXeDskek+kzLnm8cfWWy87caglpF7SmRVdImp CzZ+W2uARxr9RhybOuzc0bVYfBAdkgLPmZDjJxm83PZUa4PNCXg88f9xcrxYz4BUW6 mCghmxgv2skbwWgjanPzCesXNLDDkOhbPUTQc1Jg= Subject: FAILED: patch "[PATCH] mm/huge_memory: prevent losing PG_waiters in" failed to apply to 6.18-stable tree To: ziy@nvidia.com,baolin.wang@linux.alibaba.com,david@kernel.org,kas@kernel.org,liuye@kylinos.cn,ye.liu@linux.dev Cc: From: Date: Wed, 02 Dec 2026 14:16:14 +0100 Message-ID: <2026120214-jolt-buffalo-cdb4@gregkh> 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=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit The patch below does not apply to the 6.18-stable tree. If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to . To reproduce the conflict and resubmit, you may use the following commands: git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.18.y git checkout FETCH_HEAD git cherry-pick -x 7183af7f01c839ecef87840a90c0d8c5843f37d7 # git commit -s git send-email --to '' --in-reply-to '2026120214-jolt-buffalo-cdb4@gregkh' --subject-prefix 'PATCH 6.18.y' 'HEAD^..' Possible dependencies: thanks, greg k-h ------------------ original commit in Linus's tree ------------------ >From 7183af7f01c839ecef87840a90c0d8c5843f37d7 Mon Sep 17 00:00:00 2001 From: Zi Yan Date: Tue, 10 Nov 2026 22:19:25 -0500 Subject: [PATCH] mm/huge_memory: prevent losing PG_waiters in __split_folio_to_order() __split_folio_to_order() calls prep_compound_page() on each new head page to create a compound page after clear_compound_head(). prep_compound_page() uses non atomic __SetPageHead(). It is mostly OK, since the folio is locked and frozen and no one else is supposed to modify the folio's flags. However, code calling migration_entry_wait() can reach the folio by PFN, then access its flags. It is because softleaf_entry_wait_on_locked() used by migration_entry_wait() sets PG_waiters concurrently without taking a reference and the frozen refcount during split cannot block it. Meanwhile, __SetPageHead() does a plain read and write, so PG_waiters atomically set by softleaf_entry_wait_on_locked() will be lost if the set happens after __SetPageHead()'s read and before its write. One example code interleaving: CPU0: __split_folio_to_order(new_order>0) CPU1: do_swap_page() 1. clear_compound_head(new_head) new_head->compound_info = 0 (page_folio(new_head) == new_head now) 2. migration_entry_wait() softleaf_to_folio(entry) page_folio() -> new_head (not the old head any more) 3. prep_compound_page(new_head, new_order) __SetPageHead(new_head) tmp = new_head->flags.f <-- load; PG_waiters == 0 in tmp 4. folio_set_waiters(new_head) set_bit(PG_waiters, &flags.f) enqueue on waitqueue, sleep 5. new_head->flags.f = tmp | (1UL << PG_head) <-- store; overwrites step 4, PG_waiters == 0 again Later, folio_unlock(new_folio) sees no PG_waiters, skips folio_wake_bit(), and CPU1 can remain blocked indefinitely. Fix it by setting PG_head along with other flags in new_folio->flags before smp_wmb() and clear_compound_head(). The remaining compound page metadata is set after clear_compound_head(). In that way, after clear_compound_head() makes new_folio visible, nothing overwrites the PG_waiters from softleaf_entry_wait_on_locked() by modifying new_folio->flags non-atomically. While at it, use data_race() to suppress the KCSAN issue reported by Ye Liu's patch (the second Closes) on reading folio->flags in __split_folio_to_order(). Suggested-by: Kiryl Shutsemau Reported-by: David Hildenbrand Closes: https://lore.kernel.org/all/DMHE8BEEER1H.2BIY52NXATPIA@nvidia.com/ Reported-by: Ye Liu Closes: https://lore.kernel.org/all/20261029-data_race_huge_mem-v1-1-112e331291c1@linux.dev/ Fixes: c010d47f107f6 ("mm: thp: split huge page to any lower order pages") Cc: stable@vger.kernel.org Signed-off-by: Zi Yan Reviewed-by: David Hildenbrand (Arm) Reviewed-by: Kiryl Shutsemau (Meta) Reviewed-by: Baolin Wang Reviewed-by: Ye Liu Link: https://patch.msgid.link/20261110-fix-split_folio_to_order-race-v4-1-cc5042f33408@nvidia.com Signed-off-by: David Hildenbrand (Arm) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index eb913acb03b7..2e7f4e4c13d2 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3775,6 +3775,7 @@ static void __split_folio_to_order(struct folio *folio, int old_order, * Don't pass it around before clear_compound_head(). */ struct folio *new_folio = (struct folio *)new_head; + memdesc_flags_t new_flags; VM_BUG_ON_PAGE(atomic_read(&new_folio->_mapcount) != -1, new_head); @@ -3791,8 +3792,15 @@ static void __split_folio_to_order(struct folio *folio, int old_order, * unreferenced sub-pages of an anonymous THP: we can simply drop * PG_anon_exclusive (-> PG_mappedtodisk) for these here. */ - new_folio->flags.f &= ~PAGE_FLAGS_CHECK_AT_PREP; - new_folio->flags.f |= (folio->flags.f & + new_flags.f = new_folio->flags.f & ~PAGE_FLAGS_CHECK_AT_PREP; + /* + * data_race() is used because folio->flags is only read here, + * but a reference-less writer can still write to it, e.g., + * PG_waiters update from softleaf_entry_wait_on_locked(). + * The flag is not transferred to new_flags as it is + * folio specific, so it is not lost. + */ + new_flags.f |= data_race(folio->flags.f) & ((1L << PG_referenced) | (1L << PG_swapbacked) | (1L << PG_swapcache) | @@ -3810,7 +3818,19 @@ static void __split_folio_to_order(struct folio *folio, int old_order, #endif (1L << PG_dirty) | (1L << PG_dropbehind) | - LRU_GEN_MASK | LRU_REFS_MASK)); + LRU_GEN_MASK | LRU_REFS_MASK); + + /* + * Set PG_head before smp_wmb() and clear_compound_head(). + * After clear_compound_head() marks new_head as a folio of its + * own, new_head->flags must not be modified by plain RMWs. + * A plain RMW can cause a loss of atomic PG_* updates from + * a reference-less writer, e.g., setting PG_waiters via + * softleaf_entry_wait_on_locked(). + */ + if (new_order) + new_flags.f |= 1L << PG_head; + WRITE_ONCE(new_folio->flags.f, new_flags.f); new_folio->mapping = folio->mapping; new_folio->index = folio->index + i; @@ -3840,7 +3860,11 @@ static void __split_folio_to_order(struct folio *folio, int old_order, */ clear_compound_head(new_head); if (new_order) { - prep_compound_page(new_head, new_order); + /* + * PG_head is set above, so only set up the remaining + * metadata on head page and tail pages. + */ + __prep_compound_page(new_head, new_order); folio_set_large_rmappable(new_folio); } diff --git a/mm/internal.h b/mm/internal.h index e3f92883bf23..a0e0c4f91020 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -834,6 +834,8 @@ static inline void prep_compound_tail(struct page *tail, VM_WARN_ON_ONCE(tail->private); } +void __prep_compound_page(struct page *page, unsigned int order); + #if defined CONFIG_COMPACTION || defined CONFIG_CMA /* diff --git a/mm/page_alloc.c b/mm/page_alloc.c index eb4f02617f53..fcc6ec394678 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -682,18 +682,31 @@ static inline bool pcp_allowed_order(unsigned int order) * This usage means that zero-order pages may not be compound. */ -void prep_compound_page(struct page *page, unsigned int order) +/* + * Set up the metadata on head page and tail pages. + * Caller must set PG_head before this. + */ +void __prep_compound_page(struct page *page, unsigned int order) { int i; int nr_pages = 1 << order; - __SetPageHead(page); for (i = 1; i < nr_pages; i++) prep_compound_tail(page + i, page, order); prep_compound_head(page, order); } +/* + * PG_head is set non-atomically. It should be used when no one else can modify + * @page's flags. + */ +void prep_compound_page(struct page *page, unsigned int order) +{ + __SetPageHead(page); + __prep_compound_page(page, order); +} + static inline void set_buddy_order(struct page *page, unsigned int order) { set_page_private(page, order);