Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load
@ 2026-11-29 12:52 Zhenwen Liang
  2026-11-29 13:35 ` Zi Yan
  2026-11-30  8:11 ` Baolin Wang
  0 siblings, 2 replies; 7+ messages in thread
From: Zhenwen Liang @ 2026-11-29 12:52 UTC (permalink / raw)
  To: David Hildenbrand, Zi Yan, Andrew Morton
  Cc: Baolin Wang, Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain,
	Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau, linux-mm,
	Lorenzo Stoakes

With CONFIG_PERSISTENT_HUGE_ZERO_FOLIO=n, mm_get_huge_zero_folio() tests
MMF_HUGE_ZERO_FOLIO with a plain test_bit() and then loads
huge_zero_folio, ordered only by a control dependency. A reader can see
the flag set by another thread of the same mm and still load NULL; the
fault then falls back to a normal page. Add smp_rmb(), paired with the
full barrier of mm_flags_test_and_set().

The problem was found, and this patch prepared, with the help of a
formal analysis tool we are developing that uses AI assistance; I have
reviewed the analysis and the patch.

Fixes: 6fcb52a56ff6 ("thp: reduce usage of huge zero page's atomic counter")
Cc: Lorenzo Stoakes <ljs@kernel.org>
Signed-off-by: Zhenwen Liang <zhenwenliang@google.com>
---
v2: Shorten the changelog (David Hildenbrand). No code change.
v1: https://lore.kernel.org/r/20261129024728.282267-1-zhenwenliang@google.com/

 mm/huge_memory.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 75e8641bf9ff..c31fc1c7d01e 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -395,8 +395,11 @@ static void __init huge_zero_shrinker_exit(void)
 
 struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
 {
-	if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm))
+	if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm)) {
+		/* Paired with the full barrier of mm_flags_test_and_set(). */
+		smp_rmb();
 		return READ_ONCE(huge_zero_folio);
+	}
 
 	if (!get_huge_zero_folio())
 		return NULL;
-- 
2.56.1.839.g22a8290db9-goog



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

* Re: [PATCH v2] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load
  2026-11-29 12:52 [PATCH v2] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load Zhenwen Liang
@ 2026-11-29 13:35 ` Zi Yan
  2026-11-30  8:11 ` Baolin Wang
  1 sibling, 0 replies; 7+ messages in thread
From: Zi Yan @ 2026-11-29 13:35 UTC (permalink / raw)
  To: Zhenwen Liang
  Cc: David Hildenbrand, Andrew Morton, Baolin Wang, Liam R. Howlett,
	Nico Pache, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
	Usama Arif, Kiryl Shutsemau, linux-mm, Lorenzo Stoakes

On 29 Nov 2026, at 7:52, Zhenwen Liang wrote:

> With CONFIG_PERSISTENT_HUGE_ZERO_FOLIO=n, mm_get_huge_zero_folio() tests
> MMF_HUGE_ZERO_FOLIO with a plain test_bit() and then loads
> huge_zero_folio, ordered only by a control dependency. A reader can see
> the flag set by another thread of the same mm and still load NULL; the
> fault then falls back to a normal page. Add smp_rmb(), paired with the
> full barrier of mm_flags_test_and_set().
>
> The problem was found, and this patch prepared, with the help of a
> formal analysis tool we are developing that uses AI assistance; I have
> reviewed the analysis and the patch.
>
> Fixes: 6fcb52a56ff6 ("thp: reduce usage of huge zero page's atomic counter")
> Cc: Lorenzo Stoakes <ljs@kernel.org>
> Signed-off-by: Zhenwen Liang <zhenwenliang@google.com>
> ---
> v2: Shorten the changelog (David Hildenbrand). No code change.
> v1: https://lore.kernel.org/r/20261129024728.282267-1-zhenwenliang@google.com/
>
>  mm/huge_memory.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 75e8641bf9ff..c31fc1c7d01e 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -395,8 +395,11 @@ static void __init huge_zero_shrinker_exit(void)
>
>  struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
>  {
> -	if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm))
> +	if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm)) {
> +		/* Paired with the full barrier of mm_flags_test_and_set(). */

Maybe add below to the comment for clarification.

The flag implies huge_zero_folio is visible.

> +		smp_rmb();
>  		return READ_ONCE(huge_zero_folio);
> +	}
>
>  	if (!get_huge_zero_folio())
>  		return NULL;
> -- 
> 2.56.1.839.g22a8290db9-goog

Otherwise, LGTM. Thanks.

Reviewed-by: Zi Yan <ziy@nvidia.com>

Best Regards,
Yan, Zi


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

* Re: [PATCH v2] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load
  2026-11-29 12:52 [PATCH v2] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load Zhenwen Liang
  2026-11-29 13:35 ` Zi Yan
@ 2026-11-30  8:11 ` Baolin Wang
  2026-11-30 12:23   ` David Hildenbrand (Arm)
  1 sibling, 1 reply; 7+ messages in thread
From: Baolin Wang @ 2026-11-30  8:11 UTC (permalink / raw)
  To: Zhenwen Liang, David Hildenbrand, Zi Yan, Andrew Morton
  Cc: Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
	Lance Yang, Usama Arif, Kiryl Shutsemau, linux-mm,
	Lorenzo Stoakes



On 11/29/26 8:52 PM, Zhenwen Liang wrote:
> With CONFIG_PERSISTENT_HUGE_ZERO_FOLIO=n, mm_get_huge_zero_folio() tests
> MMF_HUGE_ZERO_FOLIO with a plain test_bit() and then loads
> huge_zero_folio, ordered only by a control dependency. A reader can see
> the flag set by another thread of the same mm and still load NULL; the
> fault then falls back to a normal page. Add smp_rmb(), paired with the
> full barrier of mm_flags_test_and_set().

Yes, this is possible. But ...

> 
> The problem was found, and this patch prepared, with the help of a
> formal analysis tool we are developing that uses AI assistance; I have
> reviewed the analysis and the patch.
> 
> Fixes: 6fcb52a56ff6 ("thp: reduce usage of huge zero page's atomic counter")
> Cc: Lorenzo Stoakes <ljs@kernel.org>
> Signed-off-by: Zhenwen Liang <zhenwenliang@google.com>
> ---
> v2: Shorten the changelog (David Hildenbrand). No code change.
> v1: https://lore.kernel.org/r/20261129024728.282267-1-zhenwenliang@google.com/
> 
>   mm/huge_memory.c | 5 ++++-
>   1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 75e8641bf9ff..c31fc1c7d01e 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -395,8 +395,11 @@ static void __init huge_zero_shrinker_exit(void)
>   
>   struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
>   {
> -	if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm))
> +	if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm)) {
> +		/* Paired with the full barrier of mm_flags_test_and_set(). */
> +		smp_rmb();
>   		return READ_ONCE(huge_zero_folio);
> +	}

Not a fan of adding a separate rmb() barrier. Why not use the acquire 
bit test? Something like:

static inline bool mm_flags_test_acquire(int flag, const struct 
mm_struct *mm)
{
	return test_bit_acquire(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags));
}


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

* Re: [PATCH v2] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load
  2026-11-30  8:11 ` Baolin Wang
@ 2026-11-30 12:23   ` David Hildenbrand (Arm)
  2026-11-30 14:40     ` Lorenzo Stoakes (ARM)
  0 siblings, 1 reply; 7+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-30 12:23 UTC (permalink / raw)
  To: Baolin Wang, Zhenwen Liang, Zi Yan, Andrew Morton
  Cc: Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
	Lance Yang, Usama Arif, Kiryl Shutsemau, linux-mm,
	Lorenzo Stoakes

On 11/30/26 09:11, Baolin Wang wrote:
> 
> 
> On 11/29/26 8:52 PM, Zhenwen Liang wrote:
>> With CONFIG_PERSISTENT_HUGE_ZERO_FOLIO=n, mm_get_huge_zero_folio() tests
>> MMF_HUGE_ZERO_FOLIO with a plain test_bit() and then loads
>> huge_zero_folio, ordered only by a control dependency. A reader can see
>> the flag set by another thread of the same mm and still load NULL; the
>> fault then falls back to a normal page. Add smp_rmb(), paired with the
>> full barrier of mm_flags_test_and_set().
> 
> Yes, this is possible. But ...
> 
>>
>> The problem was found, and this patch prepared, with the help of a
>> formal analysis tool we are developing that uses AI assistance; I have
>> reviewed the analysis and the patch.
>>
>> Fixes: 6fcb52a56ff6 ("thp: reduce usage of huge zero page's atomic counter")
>> Cc: Lorenzo Stoakes <ljs@kernel.org>
>> Signed-off-by: Zhenwen Liang <zhenwenliang@google.com>
>> ---
>> v2: Shorten the changelog (David Hildenbrand). No code change.
>> v1: https://lore.kernel.org/r/20261129024728.282267-1-zhenwenliang@google.com/
>>
>>   mm/huge_memory.c | 5 ++++-
>>   1 file changed, 4 insertions(+), 1 deletion(-)
>>
>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
>> index 75e8641bf9ff..c31fc1c7d01e 100644
>> --- a/mm/huge_memory.c
>> +++ b/mm/huge_memory.c
>> @@ -395,8 +395,11 @@ static void __init huge_zero_shrinker_exit(void)
>>   
>>   struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
>>   {
>> -	if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm))
>> +	if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm)) {
>> +		/* Paired with the full barrier of mm_flags_test_and_set(). */
>> +		smp_rmb();
>>   		return READ_ONCE(huge_zero_folio);
>> +	}
> 
> Not a fan of adding a separate rmb() barrier. Why not use the acquire 
> bit test? Something like:
> 
> static inline bool mm_flags_test_acquire(int flag, const struct 
> mm_struct *mm)
> {
> 	return test_bit_acquire(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags));
> }

That looks better indeed. Micht still want to document the corresponding barrier.

-- 
Cheers,

David


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

* Re: [PATCH v2] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load
  2026-11-30 12:23   ` David Hildenbrand (Arm)
@ 2026-11-30 14:40     ` Lorenzo Stoakes (ARM)
  2026-11-30 15:36       ` David Hildenbrand (Arm)
  0 siblings, 1 reply; 7+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-30 14:40 UTC (permalink / raw)
  To: David Hildenbrand (Arm)
  Cc: Baolin Wang, Zhenwen Liang, Zi Yan, Andrew Morton,
	Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
	Lance Yang, Usama Arif, Kiryl Shutsemau, linux-mm

On Mon, Nov 30, 2026 at 01:23:44PM +0100, David Hildenbrand (Arm) wrote:
> On 11/30/26 09:11, Baolin Wang wrote:
> >
> >
> > On 11/29/26 8:52 PM, Zhenwen Liang wrote:
> >> With CONFIG_PERSISTENT_HUGE_ZERO_FOLIO=n, mm_get_huge_zero_folio() tests
> >> MMF_HUGE_ZERO_FOLIO with a plain test_bit() and then loads
> >> huge_zero_folio, ordered only by a control dependency. A reader can see
> >> the flag set by another thread of the same mm and still load NULL; the
> >> fault then falls back to a normal page. Add smp_rmb(), paired with the
> >> full barrier of mm_flags_test_and_set().
> >
> > Yes, this is possible. But ...
> >
> >>
> >> The problem was found, and this patch prepared, with the help of a
> >> formal analysis tool we are developing that uses AI assistance; I have
> >> reviewed the analysis and the patch.
> >>
> >> Fixes: 6fcb52a56ff6 ("thp: reduce usage of huge zero page's atomic counter")

I am not sure it really matter that much. The window is tiny, and the outcome is
something has smaller folios, it's not a big deal.

I definitely wouldn't cc: stable anyway.

> >> Cc: Lorenzo Stoakes <ljs@kernel.org>
> >> Signed-off-by: Zhenwen Liang <zhenwenliang@google.com>
> >> ---
> >> v2: Shorten the changelog (David Hildenbrand). No code change.
> >> v1: https://lore.kernel.org/r/20261129024728.282267-1-zhenwenliang@google.com/
> >>
> >>   mm/huge_memory.c | 5 ++++-
> >>   1 file changed, 4 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> >> index 75e8641bf9ff..c31fc1c7d01e 100644
> >> --- a/mm/huge_memory.c
> >> +++ b/mm/huge_memory.c
> >> @@ -395,8 +395,11 @@ static void __init huge_zero_shrinker_exit(void)
> >>
> >>   struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
> >>   {
> >> -	if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm))
> >> +	if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm)) {
> >> +		/* Paired with the full barrier of mm_flags_test_and_set(). */
> >> +		smp_rmb();
> >>   		return READ_ONCE(huge_zero_folio);
> >> +	}

This whole thing is another byproduct of the mess that is the non-preallocated
huge zero folio. How I hate it... Utterly cursed :)

There was a significant bug with how all that was implemented that flummoxed
many mm developers (because memory barriers always do that).

I still feel like we should still default to that :) need to un-RFC my patch on
that.

Anyway I agree it's more cromulent to use an acquire test here. Thanks Baolin.



> >
> > Not a fan of adding a separate rmb() barrier. Why not use the acquire
> > bit test? Something like:
> >
> > static inline bool mm_flags_test_acquire(int flag, const struct
> > mm_struct *mm)
> > {
> > 	return test_bit_acquire(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags));
> > }
>
> That looks better indeed. Micht still want to document the corresponding barrier.

More than might please :) Any use of the arcane arts (memory barriers) must
specify what it is paired with, otherwise it becomes something that people are
afraid to touch.

But I think you don't want that in the mm_flags_test_acquire() function you want
it at the caller.

So something like:

< ... add mm_flags_test_acquire() to mm.h ... >

struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
{
+	/* Paired with full barrier of mm_flags_test_and_set(). */
-	if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm))
+	if (mm_flags_test_acquire(MMF_HUGE_ZERO_FOLIO, mm))
		return READ_ONCE(huge_zero_folio);

	if (!get_huge_zero_folio())
		return NULL;

	if (mm_flags_test_and_set(MMF_HUGE_ZERO_FOLIO, mm))
		put_huge_zero_folio();

	return READ_ONCE(huge_zero_folio);
}

That version LGTM so feel free to add:

Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>

To that!

--
Cheers, Lorenzo


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

* Re: [PATCH v2] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load
  2026-11-30 14:40     ` Lorenzo Stoakes (ARM)
@ 2026-11-30 15:36       ` David Hildenbrand (Arm)
  2026-11-30 16:02         ` Lorenzo Stoakes (ARM)
  0 siblings, 1 reply; 7+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-30 15:36 UTC (permalink / raw)
  To: Lorenzo Stoakes (ARM)
  Cc: Baolin Wang, Zhenwen Liang, Zi Yan, Andrew Morton,
	Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
	Lance Yang, Usama Arif, Kiryl Shutsemau, linux-mm

On 11/30/26 15:40, Lorenzo Stoakes (ARM) wrote:
> On Mon, Nov 30, 2026 at 01:23:44PM +0100, David Hildenbrand (Arm) wrote:
>> On 11/30/26 09:11, Baolin Wang wrote:
>>>
>>>
>>>
>>> Yes, this is possible. But ...
>>>
> 
> I am not sure it really matter that much. The window is tiny, and the outcome is
> something has smaller folios, it's not a big deal.
> 
> I definitely wouldn't cc: stable anyway.
> 
> 
> This whole thing is another byproduct of the mess that is the non-preallocated
> huge zero folio. How I hate it... Utterly cursed :)
> 
> There was a significant bug with how all that was implemented that flummoxed
> many mm developers (because memory barriers always do that).
> 
> I still feel like we should still default to that :) need to un-RFC my patch on
> that.
> 
> Anyway I agree it's more cromulent to use an acquire test here. Thanks Baolin.
> 
> 
> 
>>>
>>> Not a fan of adding a separate rmb() barrier. Why not use the acquire
>>> bit test? Something like:
>>>
>>> static inline bool mm_flags_test_acquire(int flag, const struct
>>> mm_struct *mm)
>>> {
>>> 	return test_bit_acquire(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags));
>>> }
>>
>> That looks better indeed. Micht still want to document the corresponding barrier.
> 
> More than might please :) Any use of the arcane arts (memory barriers) must
> specify what it is paired with, otherwise it becomes something that people are
> afraid to touch.
> 
> But I think you don't want that in the mm_flags_test_acquire() function you want
> it at the caller.
> 
> So something like:
> 
> < ... add mm_flags_test_acquire() to mm.h ... >
> 
> struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
> {
> +	/* Paired with full barrier of mm_flags_test_and_set(). */
> -	if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm))
> +	if (mm_flags_test_acquire(MMF_HUGE_ZERO_FOLIO, mm))
> 		return READ_ONCE(huge_zero_folio);
> 
> 	if (!get_huge_zero_folio())
> 		return NULL;
> 
> 	if (mm_flags_test_and_set(MMF_HUGE_ZERO_FOLIO, mm))
> 		put_huge_zero_folio();
> 
> 	return READ_ONCE(huge_zero_folio);
> }
> 
> That version LGTM so feel free to add:
> 
> Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Can you take a look at v3, if your tag applies there or whether you want some
changes?

Thanks!

-- 
Cheers,

David


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

* Re: [PATCH v2] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load
  2026-11-30 15:36       ` David Hildenbrand (Arm)
@ 2026-11-30 16:02         ` Lorenzo Stoakes (ARM)
  0 siblings, 0 replies; 7+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-30 16:02 UTC (permalink / raw)
  To: David Hildenbrand (Arm)
  Cc: Baolin Wang, Zhenwen Liang, Zi Yan, Andrew Morton,
	Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
	Lance Yang, Usama Arif, Kiryl Shutsemau, linux-mm

On Mon, Nov 30, 2026 at 04:36:35PM +0100, David Hildenbrand (Arm) wrote:
> Can you take a look at v3, if your tag applies there or whether you want some
> changes?

Sigh, I guess that thing about not immediately respinning is not a thing after
all..

I'll take a look.

--
Cheers, Lorenzo


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

end of thread, other threads:[~2026-11-30 16:02 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 12:52 [PATCH v2] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load Zhenwen Liang
2026-11-29 13:35 ` Zi Yan
2026-11-30  8:11 ` Baolin Wang
2026-11-30 12:23   ` David Hildenbrand (Arm)
2026-11-30 14:40     ` Lorenzo Stoakes (ARM)
2026-11-30 15:36       ` David Hildenbrand (Arm)
2026-11-30 16:02         ` 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