From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Hengbin Zhang <uqbarz@gmail.com>,
Andrew Morton <akpm@linux-foundation.org>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org, Lorenzo Stoakes <ljs@kernel.org>,
Zi Yan <ziy@nvidia.com>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
"Liam R. Howlett" <liam@infradead.org>,
Nico Pache <npache@redhat.com>,
Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
Barry Song <baohua@kernel.org>, Lance Yang <lance.yang@linux.dev>,
Usama Arif <usama.arif@linux.dev>
Subject: Re: [RFC PATCH v3] mm/thp: serialize huge-zero folio state transitions
Date: Mon, 27 Jul 2026 18:08:53 +0200 [thread overview]
Message-ID: <b915b058-f5b1-423a-a835-5891e7997080@kernel.org> (raw)
In-Reply-To: <20260727154001.4102341-1-uqbarz@gmail.com>
On 7/27/26 17:40, Hengbin Zhang wrote:
> The nonpersistent huge-zero shrinker clears huge_zero_folio and then
> invalidates huge_zero_pfn. A concurrent fault can publish a replacement
> folio and PFN between those updates, after which the old shrinker
> invalidates the replacement generation's PFN identity.
>
> A later partial mprotect() can then misidentify the live special PMD and
> enter the ordinary anonymous THP split path.
>
> A writer-side spinlock keeps huge_zero_folio and huge_zero_pfn updates
> from different generations together. Lockless getters still use the
> refcount fast path. atomic_set_release() publishes an initialized new
> generation before a successful atomic_inc_not_zero() can admit a getter
> on weakly ordered architectures.
>
> Suggested-by: David Hildenbrand <david@kernel.org>
> Link: https://lore.kernel.org/r/20260724100509.2300200-1-uqbarz@gmail.com
We want a Fixes: tag + Cc: stable.
Can you dig up the proper Fixes commit? Thanks
> Signed-off-by: Hengbin Zhang <uqbarz@gmail.com>
> ---
> mm/huge_memory.c | 22 +++++++++++++++-------
> 1 file changed, 15 insertions(+), 7 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index b5d1e9d4463d..79bec4495540 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -78,6 +78,7 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
> static bool split_underused_thp = true;
>
> static atomic_t huge_zero_refcount;
> +static DEFINE_SPINLOCK(huge_zero_lock);
> struct folio *huge_zero_folio __read_mostly;
> unsigned long huge_zero_pfn __read_mostly = ~0UL;
> unsigned long huge_anon_orders_always __read_mostly;
> @@ -237,17 +238,18 @@ static bool get_huge_zero_folio(void)
> }
> /* Ensure zero folio won't have large_rmappable flag set. */
> folio_clear_large_rmappable(zero_folio);
> - preempt_disable();
> - if (cmpxchg(&huge_zero_folio, NULL, zero_folio)) {
> - preempt_enable();
> + spin_lock(&huge_zero_lock);
> + if (READ_ONCE(huge_zero_folio)) {
Same comment as below: who could possibly modify this value concurrently? I
don't think we need the READ_ONCE.
> + spin_unlock(&huge_zero_lock);
> folio_put(zero_folio);
> goto retry;
> }
> WRITE_ONCE(huge_zero_pfn, folio_pfn(zero_folio));
> + WRITE_ONCE(huge_zero_folio, zero_folio);
> + spin_unlock(&huge_zero_lock);
>
> - /* We take additional reference here. It will be put back by shrinker */
> - atomic_set(&huge_zero_refcount, 2);
> - preempt_enable();
> + /* Publish the identity before admitting lockless getters. */
> + atomic_set_release(&huge_zero_refcount, 2);
Why do we need the _release semantics here when we just did a spin_unlock()?
> count_vm_event(THP_ZERO_PAGE_ALLOC);
> return true;
> }
> @@ -298,9 +300,15 @@ 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);
> + struct folio *zero_folio;
> +
> + spin_lock(&huge_zero_lock);
> + zero_folio = READ_ONCE(huge_zero_folio);
Why do we need a READ_ONCE? Nobody should be legally be allowed to change this
variable concurrently, no?
> BUG_ON(zero_folio == NULL);
> WRITE_ONCE(huge_zero_pfn, ~0UL);
> + WRITE_ONCE(huge_zero_folio, NULL);
> + spin_unlock(&huge_zero_lock);
> +
> folio_put(zero_folio);
> return HPAGE_PMD_NR;
> }
--
Cheers,
David
next prev parent reply other threads:[~2026-07-27 16:08 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-24 10:05 [RFC PATCH] mm/thp: order huge zero folio PFN invalidation before removal Hengbin Zhang
2026-07-24 18:48 ` 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) [this message]
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=b915b058-f5b1-423a-a835-5891e7997080@kernel.org \
--to=david@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=dev.jain@arm.com \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=npache@redhat.com \
--cc=ryan.roberts@arm.com \
--cc=stable@vger.kernel.org \
--cc=uqbarz@gmail.com \
--cc=usama.arif@linux.dev \
--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
all inboxes | Powered by JetHome®