Skip to contentSkip to conversation

mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load

PATCH Superseded Zhenwen Liang 5 participants 7 messages Nov 29 – Nov 30

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load

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 <[email protected]>

---
v2: Shorten the changelog (David Hildenbrand). No code change.
v1: https://lore.kernel.org/r/[email protected]/

 mm/huge_memory.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/huge_memory.c +4 -1
index 75e8641bf9ff..c31fc1c7d01e 100644
@@ -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;
Signature · 1 line
2.56.1.839.g22a8290db9-goog
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 26 lines · Zhenwen Liang

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 <[email protected]>

---
v2: Shorten the changelog (David Hildenbrand). No code change.
v1: https://lore.kernel.org/r/[email protected]/

 mm/huge_memory.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/huge_memory.c +0 -0
index 75e8641bf9ff..c31fc1c7d01e 100644

mm/huge_memory.c static void __init huge_zero_shrinker_exit(void) · line 399

Quoted 6 lines
mm/huge_memory.c +2 -1
@@ -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.

Quoted 8 lines
mm/huge_memory.c +2 -0
+		smp_rmb();
 		return READ_ONCE(huge_zero_folio);
+	}
 
 	if (!get_huge_zero_folio())
 		return NULL;
Signature · 1 line
2.56.1.839.g22a8290db9-goog

Otherwise, LGTM. Thanks.

Best Regards,
Yan, Zi

reviewer
#2 lore raw
In reply to Zhenwen Liang
Quoted 6 lines · Zhenwen Liang

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 ...

Quoted 20 lines

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 <[email protected]>

---
v2: Shorten the changelog (David Hildenbrand). No code change.
v1: https://lore.kernel.org/r/[email protected]/

  mm/huge_memory.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
mm/huge_memory.c +0 -0
index 75e8641bf9ff..c31fc1c7d01e 100644

mm/huge_memory.c static void __init huge_zero_shrinker_exit(void) · line 402

Quoted 9 lines
mm/huge_memory.c +4 -1
@@ -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));
}
#3 lore raw
Quoted 45 lines · Baolin Wang
Quoted 6 lines · Zhenwen Liang

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 ...

Quoted 20 lines

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 <[email protected]>

---
v2: Shorten the changelog (David Hildenbrand). No code change.
v1: https://lore.kernel.org/r/[email protected]/

  mm/huge_memory.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
mm/huge_memory.c +0 -0
index 75e8641bf9ff..c31fc1c7d01e 100644

mm/huge_memory.c static void __init huge_zero_shrinker_exit(void) · line 402

Quoted 9 lines
mm/huge_memory.c +4 -1
@@ -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:

Quoted 5 lines

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.

Signature · 3 lines
Cheers,

David
#4 lore raw
Quoted 11 lines · David Hildenbrand (Arm)
Quoted 10 lines · Baolin Wang
Quoted 6 lines · Zhenwen Liang

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().

Quoted 8 lines
Quoted 8 lines

Yes, this is possible. But ...

Quoted 6 lines

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.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Cc Lorenzo Stoakes <[email protected]>

---
v2: Shorten the changelog (David Hildenbrand). No code change.
v1: https://lore.kernel.org/r/[email protected]/

  mm/huge_memory.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
mm/huge_memory.c +0 -0
index 75e8641bf9ff..c31fc1c7d01e 100644
@@ -395,8 +395,11 @@ static void __init huge_zero_shrinker_exit(void)
 
Quoted 8 lines
Quoted 8 lines

mm/huge_memory.c static void __init huge_zero_shrinker_exit(void) · line 402

Quoted 8 lines
mm/huge_memory.c +4 -1
  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.

Quoted 11 lines
Quoted 9 lines

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:

To that!

--
Cheers, Lorenzo

#5 lore raw
Quoted 66 lines · Lorenzo Stoakes (ARM)
Quoted 6 lines · David Hildenbrand (Arm)
Quoted 5 lines · Baolin Wang

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.

Quoted 11 lines
Quoted 9 lines

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) {

(quoted diff) +2 -1
+	/* 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);
}
Quoted 3 lines

That version LGTM so feel free to add:

Can you take a look at v3, if your tag applies there or whether you want some changes?

Thanks!

Signature · 3 lines
Cheers,

David
#6 lore raw
Quoted 2 lines · David Hildenbrand (Arm)

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