Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm/huge_memory: fix huge zero folio publication race
@ 2026-07-29  4:50 xueyuan.chen
  2026-07-29  6:44 ` David Hildenbrand (Arm)
  0 siblings, 1 reply; 4+ messages in thread
From: xueyuan.chen @ 2026-07-29  4:50 UTC (permalink / raw)
  To: Andrew Morton, David Hildenbrand, Lorenzo Stoakes, linux-mm
  Cc: Zi Yan, Baolin Wang, Liam R. Howlett, Nico Pache, Ryan Roberts,
	Dev Jain, Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau,
	Bob Liu, linux-kernel, Xueyuan Chen

From: Xueyuan Chen <xueyuan.chen@vivo.com>

get_huge_zero_folio() uses huge_zero_refcount as the publication gate for
lockless users. However, it publishes huge_zero_folio with cmpxchg()
before storing huge_zero_pfn and initializes the refcount with an unordered
atomic_set().

On weakly ordered systems, a racing atomic_inc_not_zero() can observe the
nonzero refcount without observing the huge_zero_pfn store.
is_huge_zero_pmd() can then misidentify a huge zero PMD as a regular
anonymous THP. The write-protect fault path may consequently mark the
global huge zero folio anonymous, exclusive and writable.

Use atomic_set_release() to publish the initialized folio and PFN. A
successful atomic_inc_not_zero() is fully ordered, so a reader that obtains
a reference also observes the state published before the refcount became
nonzero.

Fixes: 97ae17497e99 ("thp: implement refcounting for huge zero page")
Cc: stable@vger.kernel.org
Signed-off-by: Xueyuan Chen <xueyuan.chen@vivo.com>
---
 mm/huge_memory.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 032702a4637b..6c74d0375377 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -245,8 +245,11 @@ static bool get_huge_zero_folio(void)
 	}
 	WRITE_ONCE(huge_zero_pfn, folio_pfn(zero_folio));
 
-	/* We take additional reference here. It will be put back by shrinker */
-	atomic_set(&huge_zero_refcount, 2);
+	/*
+	 * Publish the folio and PFN before making them available to lockless
+	 * users. Pairs with a successful atomic_inc_not_zero() above.
+	 */
+	atomic_set_release(&huge_zero_refcount, 2);
 	preempt_enable();
 	count_vm_event(THP_ZERO_PAGE_ALLOC);
 	return true;
-- 
2.47.3




^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-07-30  7:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-29  4:50 [PATCH] mm/huge_memory: fix huge zero folio publication race xueyuan.chen
2026-07-29  6:44 ` David Hildenbrand (Arm)
2026-07-30  1:02   ` Andrew Morton
2026-07-30  7:27     ` Lorenzo Stoakes (ARM)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox