mm/huge_memory: order the MMF_HUGE_ZERO_FOLIO test before the huge_zero_folio load
Subsystems (3)
PATCH v3 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 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.
---
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/[email protected]/
include/linux/mm.h | 5 +++++ mm/huge_memory.c | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-)
include/linux/mm.h +5 -0
@@ -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));
mm/huge_memory.c +5 -1
@@ -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 ())
Signature · 1 line
2.56.1.839.g22a8290db9-goog
Quoted 23 lines · Zhenwen Liang
With
CONFIG_PERSISTENT_HUGE_ZERO_FOLIO=n,mm_get_huge_zero_folio()testsMMF_HUGE_ZERO_FOLIOwith a plaintest_bit()and then loadshuge_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. Addmm_flags_test_acquire()and use it for this test; it pairs with the full barrier ofmm_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 <[email protected]>Signed-off-by Zhenwen Liang <[email protected]>---
v3: Use an acquire bit test instead ofsmp_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/[email protected]/
Quoted 4 lines
include/linux/mm.h | 5 +++++ mm/huge_memory.c | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-)
LGTM. Thanks.
Best Regards,
Yan, Zi
Quoted 10 lines · Zhenwen Liang
With
CONFIG_PERSISTENT_HUGE_ZERO_FOLIO=n,mm_get_huge_zero_folio()testsMMF_HUGE_ZERO_FOLIOwith a plaintest_bit()and then loadshuge_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. Addmm_flags_test_acquire()and use it for this test; it pairs with the full barrier ofmm_flags_test_and_set().Compile-tested only.
Quoted 8 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]>Signed-off-by Zhenwen Liang <[email protected]>---
Signature · 3 lines
Cheers, David
Quoted 9 lines · Zhenwen Liang
With
CONFIG_PERSISTENT_HUGE_ZERO_FOLIO=n,mm_get_huge_zero_folio()testsMMF_HUGE_ZERO_FOLIOwith a plaintest_bit()and then loadshuge_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. Addmm_flags_test_acquire()and use it for this test; it pairs with the full barrier ofmm_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
Signature · 3 lines
Cheers, David
Quoted 10 lines · Zhenwen Liang
With
CONFIG_PERSISTENT_HUGE_ZERO_FOLIO=n,mm_get_huge_zero_folio()testsMMF_HUGE_ZERO_FOLIOwith a plaintest_bit()and then loadshuge_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. Addmm_flags_test_acquire()and use it for this test; it pairs with the full barrier ofmm_flags_test_and_set().Compile-tested only.
Quoted 7 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]>Signed-off-by Zhenwen Liang <[email protected]>
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:
Quoted 15 lines
---
v3: Use an acquire bit test instead ofsmp_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/[email protected]/include/linux/mm.h | 5 +++++ mm/huge_memory.c | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-)include/linux/mm.h +0
-0
include/linux/mm.h static inline bool mm_flags_test(int flag, const struct mm_struct *mm) · line 936
Quoted 8 lines
include/linux/mm.h +5
-0@@ -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.
Quoted 8 lines
include/linux/mm.h +0
-0static 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));mm/huge_memory.c +0
-0
mm/huge_memory.c static void __init huge_zero_shrinker_exit(void) · line 400
Quoted 7 lines
mm/huge_memory.c +3
-1@@ -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...
Quoted 8 lines
mm/huge_memory.c +2
-1+*/ +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
Zi Yan
reviewerQuoted 36 lines · Lorenzo Stoakes (ARM)
Quoted 10 lines · Zhenwen Liang
With
CONFIG_PERSISTENT_HUGE_ZERO_FOLIO=n,mm_get_huge_zero_folio()testsMMF_HUGE_ZERO_FOLIOwith a plaintest_bit()and then loadshuge_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. Addmm_flags_test_acquire()and use it for this test; it pairs with the full barrier ofmm_flags_test_and_set().Compile-tested only.
Quoted 7 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]>Signed-off-by Zhenwen Liang <[email protected]>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) <[email protected]>Quoted 10 lines
---
v3: Use an acquire bit test instead ofsmp_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/[email protected]/include/linux/mm.h | 5 +++++ mm/huge_memory.c | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-)
<snip>
Quoted 9 lines
Quoted 9 lines
static inline bool
mm_flags_test_and_set(int flag, struct mm_struct *mm){include/linux/mm.h +0
-0return test_and_set_bit (flag,ACCESS_PRIVATE (&mm->flags, __mm_flags));mm/huge_memory.c +0
-0@@ -395,7 +395,11 @@ static void __init huge_zero_shrinker_exit(void)
Quoted 9 lines
mm/huge_memory.c static void __init huge_zero_shrinker_exit(void) · line 400
Quoted 6 lines
mm/huge_memory.c +3
-1struct 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. :)
Quoted 5 lines
Quoted 5 lines
mm/huge_memory.c +2
-0+*/ +if (mm_flags_test_acquire (MMF_HUGE_ZERO_FOLIO , mm))return READ_ONCE (huge_zero_folio);
Quoted 7 lines
Quoted 4 lines
mm/huge_memory.c +0
-1if (!get_huge_zero_folio ()) --2.56.1.839.g22a8290db9-goog
--
Cheers, Lorenzo
Best Regards,
Yan, Zi
Lorenzo Stoakes (ARM)
reviewerQuoted 19 lines · Zi Yan
Quoted 9 lines
Quoted 9 lines
static inline bool
mm_flags_test_and_set(int flag, struct mm_struct *mm){include/linux/mm.h +0
-0return test_and_set_bit (flag,ACCESS_PRIVATE (&mm->flags, __mm_flags));mm/huge_memory.c +0
-0@@ -395,7 +395,11 @@ static void __init huge_zero_shrinker_exit(void)Quoted 9 lines
mm/huge_memory.c static void __init huge_zero_shrinker_exit(void) · line 400
Quoted 6 lines
mm/huge_memory.c +3
-1struct 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...
Quoted 6 lines
I suggested it because when I looked at v2, it is not straightforward to me why pairing
smp_rmb()withmm_flags_test_and_set()would work until I readget_huge_zero_folio(). Then I wonder why not atomically readhuge_zero_refcountto 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. :)
Ack, yeah. I did wonder about saying something in general about _why_, maybe I stared at this long enough to get the context and felt it was 'obvious'.
But what you just describe is literally why real life experience trumps assumptions like that :)
So fair enough as-is!
--
Cheers, Lorenzo
Hi Lorenzo,
Sorry for wasting your time on v2. I sent v3 too soon after David's reply, before you had a chance to comment, and I did not point to it from the v2 thread. That was my mistake.
Thank you so much for reviewing it anyway. In future I will wait for the discussion to settle before sending the next one.
Zhenwen
Lorenzo Stoakes (ARM)
reviewerQuoted 8 lines · Zhenwen Liang
Hi Lorenzo,
Sorry for wasting your time on v2. I sent v3 too soon after David's reply, before you had a chance to comment, and I did not point to it from the v2 thread. That was my mistake.
Thank you so much for reviewing it anyway. In future I will wait for the discussion to settle before sending the next one.
Don't worry, you waited a day at least and I am possibly suffering from grumpy maintainer syndrome (TM) a bit today :)
Thanks for the fix!
--
Cheers, Lorenzo
David Hildenbrand (Arm)
maintainerQuoted 15 lines · Lorenzo Stoakes (ARM)
Quoted 14 lines · Zhenwen Liang
With
CONFIG_PERSISTENT_HUGE_ZERO_FOLIO=n,mm_get_huge_zero_folio()testsMMF_HUGE_ZERO_FOLIOwith a plaintest_bit()and then loadshuge_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. Addmm_flags_test_acquire()and use it for this test; it pairs with the full barrier ofmm_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.
Quoted 6 lines
Quoted 3 lines
Fixes 6fcb52a56ff6 ("thp: reduce usage of huge zero page's atomic counter")Cc Lorenzo Stoakes <[email protected]>Signed-off-by Zhenwen Liang <[email protected]>Little annoyed to have wasted my time replying to v2 only to see there's a v3 already, and also already taken, but anyway.
As my length comment in there spells out: this can be yanked every time. I applied it before spotting your comment on v2.
So let' please have a v4 if stuff here needs more changes, and I'll apply that one instead.
Signature · 3 lines
Cheers, David
Lorenzo Stoakes (ARM)
reviewerQuoted 20 lines · David Hildenbrand (Arm)
Quoted 19 lines · Lorenzo Stoakes (ARM)
Quoted 17 lines · Zhenwen Liang
With
CONFIG_PERSISTENT_HUGE_ZERO_FOLIO=n,mm_get_huge_zero_folio()testsMMF_HUGE_ZERO_FOLIOwith a plaintest_bit()and then loadshuge_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. Addmm_flags_test_acquire()and use it for this test; it pairs with the full barrier ofmm_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 <[email protected]>Signed-off-by Zhenwen Liang <[email protected]>
Quoted 8 lines
Quoted 2 lines
Little annoyed to have wasted my time replying to v2 only to see there's a v3 already, and also already taken, but anyway.
As my length comment in there spells out: this can be yanked every time. I applied it before spotting your comment on v2.
So let' please have a v4 if stuff here needs more changes, and I'll apply that one instead.
Sorry I'm extra-grumpy today... :)
I thought you would be reticent to for stable commit hash reasons? I guess you can keep the commit hash but still change things like add tags though? Or no?
--
Cheers, Lorenzo
Lorenzo Stoakes (ARM)
reviewerQuoted 21 lines · Lorenzo Stoakes (ARM)
Quoted 20 lines · David Hildenbrand (Arm)
Quoted 19 lines · Lorenzo Stoakes (ARM)
Quoted 17 lines · Zhenwen Liang
With
CONFIG_PERSISTENT_HUGE_ZERO_FOLIO=n,mm_get_huge_zero_folio()testsMMF_HUGE_ZERO_FOLIOwith a plaintest_bit()and then loadshuge_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. Addmm_flags_test_acquire()and use it for this test; it pairs with the full barrier ofmm_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 <[email protected]>Signed-off-by Zhenwen Liang <[email protected]>
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
Little annoyed to have wasted my time replying to v2 only to see there's a v3 already, and also already taken, but anyway.
As my length comment in there spells out: this can be yanked every time. I applied it before spotting your comment on v2.
So let' please have a v4 if stuff here needs more changes, and I'll apply that one instead.
And v3 is fine just add my tag thanks
Quoted 8 lines
Sorry I'm extra-grumpy today... :)
I thought you would be reticent to for stable commit hash reasons? I guess you can keep the commit hash but still change things like add tags though? Or no?
--
Cheers, Lorenzo
--
Cheers, Lorenzo
David Hildenbrand (Arm)
maintainerQuoted 9 lines · Lorenzo Stoakes (ARM)
Quoted 8 lines · David Hildenbrand (Arm)
Quoted 3 lines · Lorenzo Stoakes (ARM)
Little annoyed to have wasted my time replying to v2 only to see there's a v3 already, and also already taken, but anyway.
As my length comment in there spells out: this can be yanked every time. I applied it before spotting your comment on v2.
Quoted 6 lines
Quoted 2 lines
So let' please have a v4 if stuff here needs more changes, and I'll apply that one instead.
Sorry I'm extra-grumpy today... :)
I thought you would be reticent to for stable commit hash reasons?
Primarily before the merge window. I decided that rebasing and picking up trailers is more important :)
Quoted 2 lines
I guess you
can keep the commit hash but still change things like add tags though? Or no?
Will change the commit. But that's not a problem.
We really only need stable commit IDs for shared topic branches or just before the merge window.
A learning curve ... :)
Signature · 3 lines
Cheers, David
Quoted 10 lines · Zhenwen Liang
With
CONFIG_PERSISTENT_HUGE_ZERO_FOLIO=n,mm_get_huge_zero_folio()testsMMF_HUGE_ZERO_FOLIOwith a plaintest_bit()and then loadshuge_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. Addmm_flags_test_acquire()and use it for this test; it pairs with the full barrier ofmm_flags_test_and_set().Compile-tested only.
Quoted 8 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]>Signed-off-by Zhenwen Liang <[email protected]>---
LGTM.