Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load
@ 2026-11-30 12:53 Zhenwen Liang
  2026-11-30 15:11 ` Zi Yan
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Zhenwen Liang @ 2026-11-30 12:53 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, Vlastimil Babka, Mike Rapoport, Michal Hocko

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 mm_flags_test_acquire() and
use it for this test; it pairs with the full barrier of
mm_flags_test_and_set().

Compile-tested only.

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>
---
v3: Use an acquire bit test instead of smp_rmb() (Baolin Wang, David
    Hildenbrand); document the pairing barrier (David Hildenbrand, Zi Yan).
    Dropped Zi Yan's Reviewed-by because the code changed.
v2: https://lore.kernel.org/r/20261129125223.1182465-1-zhenwenliang@google.com/

 include/linux/mm.h | 5 +++++
 mm/huge_memory.c   | 6 +++++-
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 040cff64381b..e26de2ec7b0d 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -929,6 +929,11 @@ static inline bool mm_flags_test(int flag, const struct mm_struct *mm)
 	return test_bit(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags));
 }
 
+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));
+}
+
 static inline bool mm_flags_test_and_set(int flag, struct mm_struct *mm)
 {
 	return test_and_set_bit(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags));
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index d065b26a94df..139471a861ed 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -395,7 +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))
+	/*
+	 * Paired with the full barrier of mm_flags_test_and_set().
+	 * The flag implies huge_zero_folio is visible.
+	 */
+	if (mm_flags_test_acquire(MMF_HUGE_ZERO_FOLIO, mm))
 		return READ_ONCE(huge_zero_folio);
 
 	if (!get_huge_zero_folio())
-- 
2.56.1.839.g22a8290db9-goog



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

* Re: [PATCH v3] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load
  2026-11-30 12:53 [PATCH v3] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load Zhenwen Liang
@ 2026-11-30 15:11 ` Zi Yan
  2026-11-30 15:33 ` David Hildenbrand (Arm)
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Zi Yan @ 2026-11-30 15:11 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,
	Vlastimil Babka, Mike Rapoport, Michal Hocko

On 30 Nov 2026, at 7:53, 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 mm_flags_test_acquire() and
> use it for this test; it pairs with the full barrier of
> mm_flags_test_and_set().
>
> Compile-tested only.
>
> 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>
> ---
> v3: Use an acquire bit test instead of smp_rmb() (Baolin Wang, David
>     Hildenbrand); document the pairing barrier (David Hildenbrand, Zi Yan).
>     Dropped Zi Yan's Reviewed-by because the code changed.
> v2: https://lore.kernel.org/r/20261129125223.1182465-1-zhenwenliang@google.com/
>
>  include/linux/mm.h | 5 +++++
>  mm/huge_memory.c   | 6 +++++-
>  2 files changed, 10 insertions(+), 1 deletion(-)
>
LGTM. Thanks.

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

Best Regards,
Yan, Zi


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

* Re: [PATCH v3] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load
  2026-11-30 12:53 [PATCH v3] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load Zhenwen Liang
  2026-11-30 15:11 ` Zi Yan
@ 2026-11-30 15:33 ` David Hildenbrand (Arm)
  2026-11-30 15:34 ` David Hildenbrand
  2026-11-30 16:06 ` Lorenzo Stoakes (ARM)
  3 siblings, 0 replies; 6+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-30 15:33 UTC (permalink / raw)
  To: Zhenwen Liang, 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, Vlastimil Babka, Mike Rapoport, Michal Hocko

On 11/30/26 13:53, 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 mm_flags_test_acquire() and
> use it for this test; it pairs with the full barrier of
> mm_flags_test_and_set().
> 
> Compile-tested only.
> 
> 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>
> ---


Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>

-- 
Cheers,

David


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

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

On Mon, 30 Nov 2026 12:53:01 +0000, 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 mm_flags_test_acquire() and
> use it for this test; it pairs with the full barrier of
> mm_flags_test_and_set().
> 
> [...]

Applied, thanks!

tree: mm/core.git
branch: thp-7.5.misc

Applied patches will appear soon in mm-next, followed by linux-next.

Please report any outstanding bugs that were missed during review, so we
can either drop the patch series or decide how to fix it up.

It is encouraged to provide Acked-bys, Reviewed-bys, and Tested-bys even
though the patches have already been applied. If possible, patch trailers
will be updated.

Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. Further, this is not a guarantee that the
patches will go upstream. If in doubt, please check the listed branch.

[1/1] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load
      https://git.kernel.org/mm/core/c/327ba3cb

-- 
Cheers,

David



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

* Re: [PATCH v3] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load
  2026-11-30 12:53 [PATCH v3] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load Zhenwen Liang
                   ` (2 preceding siblings ...)
  2026-11-30 15:34 ` David Hildenbrand
@ 2026-11-30 16:06 ` Lorenzo Stoakes (ARM)
  2026-11-30 16:22   ` Zi Yan
  3 siblings, 1 reply; 6+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-30 16:06 UTC (permalink / raw)
  To: Zhenwen Liang
  Cc: David Hildenbrand, Zi Yan, Andrew Morton, Baolin Wang,
	Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
	Lance Yang, Usama Arif, Kiryl Shutsemau, linux-mm,
	Vlastimil Babka, Mike Rapoport, Michal Hocko

On Mon, Nov 30, 2026 at 12:53:01PM +0000, 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 mm_flags_test_acquire() and
> use it for this test; it pairs with the full barrier of
> mm_flags_test_and_set().
>
> Compile-tested only.
>
> 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>

Little annoyed to have wasted my time replying to v2 only to see there's a v3
already, and also already taken, but anyway.

FWIW:

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

> ---
> v3: Use an acquire bit test instead of smp_rmb() (Baolin Wang, David
>     Hildenbrand); document the pairing barrier (David Hildenbrand, Zi Yan).
>     Dropped Zi Yan's Reviewed-by because the code changed.
> v2: https://lore.kernel.org/r/20261129125223.1182465-1-zhenwenliang@google.com/
>
>  include/linux/mm.h | 5 +++++
>  mm/huge_memory.c   | 6 +++++-
>  2 files changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 040cff64381b..e26de2ec7b0d 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -929,6 +929,11 @@ static inline bool mm_flags_test(int flag, const struct mm_struct *mm)
>  	return test_bit(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags));
>  }
>
> +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));
> +}
> +

The headers make life difficult with 'stuff you own', at least ostensibly.

mm.h is in MM - CORE, but a ton of VMA flag and mm flag and etc. stuff is there.

I am only aware of this API effectively changing because I happened to be cc-d
:)

I wonder if we shouldn't split headers out a bit for this kind of thing,
assuming it doesn't somehow make the header bloat thing even worse.

>  static inline bool mm_flags_test_and_set(int flag, struct mm_struct *mm)
>  {
>  	return test_and_set_bit(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags));
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index d065b26a94df..139471a861ed 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -395,7 +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))
> +	/*
> +	 * Paired with the full barrier of mm_flags_test_and_set().
> +	 * The flag implies huge_zero_folio is visible.

This line seems redundant. But not the end of the world given it's taken
already...

> +	 */
> +	if (mm_flags_test_acquire(MMF_HUGE_ZERO_FOLIO, mm))
>  		return READ_ONCE(huge_zero_folio);
>
>  	if (!get_huge_zero_folio())
> --
> 2.56.1.839.g22a8290db9-goog
>

--
Cheers, Lorenzo


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

* Re: [PATCH v3] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load
  2026-11-30 16:06 ` Lorenzo Stoakes (ARM)
@ 2026-11-30 16:22   ` Zi Yan
  0 siblings, 0 replies; 6+ messages in thread
From: Zi Yan @ 2026-11-30 16:22 UTC (permalink / raw)
  To: Lorenzo Stoakes (ARM)
  Cc: Zhenwen Liang, 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,
	Vlastimil Babka, Mike Rapoport, Michal Hocko

On 30 Nov 2026, at 11:06, Lorenzo Stoakes (ARM) wrote:

> On Mon, Nov 30, 2026 at 12:53:01PM +0000, 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 mm_flags_test_acquire() and
>> use it for this test; it pairs with the full barrier of
>> mm_flags_test_and_set().
>>
>> Compile-tested only.
>>
>> 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>
>
> Little annoyed to have wasted my time replying to v2 only to see there's a v3
> already, and also already taken, but anyway.
>
> FWIW:
>
> Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
>
>> ---
>> v3: Use an acquire bit test instead of smp_rmb() (Baolin Wang, David
>>     Hildenbrand); document the pairing barrier (David Hildenbrand, Zi Yan).
>>     Dropped Zi Yan's Reviewed-by because the code changed.
>> v2: https://lore.kernel.org/r/20261129125223.1182465-1-zhenwenliang@google.com/
>>
>>  include/linux/mm.h | 5 +++++
>>  mm/huge_memory.c   | 6 +++++-
>>  2 files changed, 10 insertions(+), 1 deletion(-)
>>

<snip>

>>  static inline bool mm_flags_test_and_set(int flag, struct mm_struct *mm)
>>  {
>>  	return test_and_set_bit(flag, ACCESS_PRIVATE(&mm->flags, __mm_flags));
>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
>> index d065b26a94df..139471a861ed 100644
>> --- a/mm/huge_memory.c
>> +++ b/mm/huge_memory.c
>> @@ -395,7 +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))
>> +	/*
>> +	 * Paired with the full barrier of mm_flags_test_and_set().
>> +	 * The flag implies huge_zero_folio is visible.
>
> This line seems redundant. But not the end of the world given it's taken
> already...

I suggested it because when I looked at v2, it is not straightforward
to me why pairing smp_rmb() with mm_flags_test_and_set() would work
until I read get_huge_zero_folio(). Then I wonder why not atomically
read huge_zero_refcount to achieve the same effect, but that is
a costly global read. I just want to save another round of reasoning
when I see the code next time. :)

>
>> +	 */
>> +	if (mm_flags_test_acquire(MMF_HUGE_ZERO_FOLIO, mm))
>>  		return READ_ONCE(huge_zero_folio);
>>
>>  	if (!get_huge_zero_folio())
>> --
>> 2.56.1.839.g22a8290db9-goog
>>
>
> --
> Cheers, Lorenzo


Best Regards,
Yan, Zi


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

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

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 12:53 [PATCH v3] mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load Zhenwen Liang
2026-11-30 15:11 ` Zi Yan
2026-11-30 15:33 ` David Hildenbrand (Arm)
2026-11-30 15:34 ` David Hildenbrand
2026-11-30 16:06 ` Lorenzo Stoakes (ARM)
2026-11-30 16:22   ` Zi Yan

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