The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Hengbin Zhang <uqbarz@gmail.com>
To: linux-mm@kvack.org, akpm@linux-foundation.org, david@kernel.org,
	ljs@kernel.org
Cc: ziy@nvidia.com, linux-kernel@vger.kernel.org,
	Hengbin Zhang <uqbarz@gmail.com>
Subject: [RFC PATCH] mm/thp: order huge zero folio PFN invalidation before removal
Date: Fri, 24 Jul 2026 10:05:09 +0000	[thread overview]
Message-ID: <20260724100509.2300200-1-uqbarz@gmail.com> (raw)

The nonpersistent huge-zero shrinker removes huge_zero_folio with
xchg() and then invalidates huge_zero_pfn. A concurrent fault can
publish a new folio and its PFN between these operations, after which
the old shrinker invalidates the new generation's PFN identity.

A later partial mprotect() can misclassify the live special PMD and
enter the ordinary anonymous THP split path.

Invalidate huge_zero_pfn before making huge_zero_folio NULL. A getter
which observes a zero refcount while the old pointer is still present
cannot publish a new folio: its cmpxchg() fails and it retries. A
getter which succeeds does so after the invalidation and publishes the
new PFN afterwards.

Fixes: 3b77e8c8cde5 ("mm/thp: make is_huge_zero_pmd() safe and quicker")

Signed-off-by: Hengbin Zhang <uqbarz@gmail.com>
---
 mm/huge_memory.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index b5d1e9d4463d..fcc492368160 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -298,9 +298,11 @@ static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink,
 						 struct shrink_control *sc)
 {
 	if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
-		struct folio *zero_folio = xchg(&huge_zero_folio, NULL);
-		BUG_ON(zero_folio == NULL);
+		struct folio *zero_folio;
+
 		WRITE_ONCE(huge_zero_pfn, ~0UL);
+		zero_folio = xchg(&huge_zero_folio, NULL);
+		BUG_ON(zero_folio == NULL);
 		folio_put(zero_folio);
 		return HPAGE_PMD_NR;
 	}
-- 
2.34.1


             reply	other threads:[~2026-07-24 10:06 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-24 10:05 Hengbin Zhang [this message]
2026-07-24 18:48 ` [RFC PATCH] mm/thp: order huge zero folio PFN invalidation before removal David Hildenbrand (Arm)
2026-07-27 13:18   ` [RFC PATCH v2] mm/thp: serialize huge-zero folio state transitions Hengbin Zhang
2026-07-27 13:40     ` David Hildenbrand (Arm)
2026-07-27 15:40       ` [RFC PATCH v3] " Hengbin Zhang
2026-07-27 15:42         ` Lorenzo Stoakes (ARM)
2026-07-27 16:08         ` David Hildenbrand (Arm)
2026-07-27 18:09           ` Lorenzo Stoakes (ARM)
2026-07-27 18:10             ` Lorenzo Stoakes (ARM)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260724100509.2300200-1-uqbarz@gmail.com \
    --to=uqbarz@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox