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=1796356498; b=aSazptziICpSdZQV3QpGsfjLB6FSAtmC7RpXEMl5igG8HIDqqT7lweJ0xqzarNNJgz5L MLn5zm+P9N9Z3if6/2mNgTkcrEKQKqcV9bPxjBdGbQyQip8ddJtm3OkrasK/+7y3yY0lK 2WnvALgM2T6JNu8JZH0XBAjlPIzkxa6f3hHbdUavX8MAq40Au30tNgAeZ8Bjg++Uraj/y HiRAJ2RyOGg1hoAw72hdbQ0gh/5E43j2Cfz5XwE3yusoKv6vLcXD+39z55H1pIzZAucgt Jshl2Pi/OFdPawTdXiIWTbpqx8/ujOZC23NjfvNCe9fdI0druOFsP30EUg0oYmqQO2g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796356498; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=zoU+GEbNXxe+lS03fDnoX+r/goTtDe/72XP+9v4DExU=; b=hVu7LuymxBYcofSvP6C2Z5wYI8k+Pi82vpsPXAjUgkCaXuRcoMux3fNEuyibPLuCCCQf CsI3H11THAVT+mgTkCynpdQT13Z5mYqJsixgk9P34nZhfSxqs8bW0dlcWigeAlX8ecw3d TBtEP/nH8POz2mChzsag36lW+i0PfnE8JcATbCbvcnNngV3FEW9JuUQCHc5rArk0GBsSS shbIetRmuwrFZzON6WGjufpKEk8DgeBQMlXL6mSWb3Jv/D1+znGQKq9nMbepyzydal4gb Iez2L7rL6ii36GCzl9qv1lL506/MYCqGvXNuVHQL+FVubGECkZFMqEA47QzqAXdzLKg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=UNip+HhG; 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 6BABA606A322 for ; Fri, 4 Dec 2026 03:54:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=UNip+HhG Received: by smtp.kernel.org (Postfix) with ESMTPSA id 234D41F000C2; Fri, 4 Dec 2026 03:54:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796356498; bh=zoU+GEbNXxe+lS03fDnoX+r/goTtDe/72XP+9v4DExU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=UNip+HhGlgkIAik9tFfbZjdfe6zd9mHq5vzbkU3zIC7VS5HbzmsNlXZnj3U97+dsT QiLb0RqJGoj9kF0gu05/wCvp1bvJi2itjQwpRfdjIRrArZcb+dHCLaa/WGSgkeexRX uTVUIVSWO7u3MW+waX20WTBsAUoDCIvdkckmlF5CIgFQvoL6sYupeeB9aGpGcXwsYV PkLPTofgP0AK0zFsFrZt2eqRrEHyWxjN+CVuC+LAJhdzeEUX9OZwBSe7mbI2nzvEhy Gx1Elo51URO377gHHx/vcY2j7GgUz4abIYYEV2bVS+JeQNI7a6J7AdwRhvFmIRQq5j E39X5YNt7MfJg== From: Sasha Levin To: stable@vger.kernel.org Cc: Zi Yan , Kiryl Shutsemau , David Hildenbrand , Ye Liu , Baolin Wang , Ye Liu , Sasha Levin Subject: [PATCH 6.12.y] mm/huge_memory: prevent losing PG_waiters in __split_folio_to_order() Date: Thu, 3 Dec 2026 22:54:55 -0500 Message-ID: <20261204035432.2521698-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120215-fetal-threaten-7c70@gregkh> References: <2026120215-fetal-threaten-7c70@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-Transfer-Encoding: 8bit From: Zi Yan [ Upstream commit 7183af7f01c839ecef87840a90c0d8c5843f37d7 ] __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) [ sashal: Replaced prep_compound_page() with __prep_compound_page() in __split_huge_page_tail() (no __split_folio_to_order() here), using plain unsigned long flags and migration_entry_wait() in comments. ] Signed-off-by: Sasha Levin --- mm/huge_memory.c | 32 ++++++++++++++++++++++++++++---- mm/internal.h | 1 + mm/page_alloc.c | 17 +++++++++++++++-- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 6d5a615107a80..5648ea00d94fa 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3226,6 +3226,7 @@ static void __split_huge_page_tail(struct folio *folio, int tail, * Don't pass it around before clear_compound_head(). */ struct folio *new_folio = (struct folio *)page_tail; + unsigned long new_flags; VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail); @@ -3242,8 +3243,15 @@ static void __split_huge_page_tail(struct folio *folio, int tail, * unreferenced sub-pages of an anonymous THP: we can simply drop * PG_anon_exclusive (-> PG_mappedtodisk) for these here. */ - page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; - page_tail->flags |= (head->flags & + new_flags = page_tail->flags & ~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 migration_entry_wait(). + * The flag is not transferred to new_flags as it is + * folio specific, so it is not lost. + */ + new_flags |= data_race(head->flags) & ((1L << PG_referenced) | (1L << PG_swapbacked) | (1L << PG_swapcache) | @@ -3260,7 +3268,19 @@ static void __split_huge_page_tail(struct folio *folio, int tail, (1L << PG_arch_3) | #endif (1L << PG_dirty) | - 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 page_tail as a folio of its + * own, page_tail->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 + * migration_entry_wait(). + */ + if (new_order) + new_flags |= 1L << PG_head; + WRITE_ONCE(page_tail->flags, new_flags); /* ->mapping in first and second tail page is replaced by other uses */ VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING, @@ -3290,7 +3310,11 @@ static void __split_huge_page_tail(struct folio *folio, int tail, */ clear_compound_head(page_tail); if (new_order) { - prep_compound_page(page_tail, new_order); + /* + * PG_head is set above, so only set up the remaining + * metadata on head page and tail pages. + */ + __prep_compound_page(page_tail, new_order); folio_set_large_rmappable(new_folio); } diff --git a/mm/internal.h b/mm/internal.h index 0cb49d2a51025..11f108f6d024e 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -729,6 +729,7 @@ static inline void prep_compound_tail(struct page *head, int tail_idx) } extern void prep_compound_page(struct page *page, unsigned int order); +void __prep_compound_page(struct page *page, unsigned int order); void post_alloc_hook(struct page *page, unsigned int order, gfp_t gfp_flags); extern bool free_pages_prepare(struct page *page, unsigned int order); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index f3383313d6d80..4cc138ca61dd8 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -590,18 +590,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); 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); -- 2.53.0