mm: thp: reduce unnecessary start_stop_khugepaged() calls
Subsystems (4)
v5 cover mm: thp: reduce unnecessary start_stop_khugepaged() calls
Writing to /sys/kernel/mm/transparent_hugepage/enabled causes start_stop_khugepaged() called independent of any change. start_stop_khugepaged() SPAMs the printk ring buffer overflow with the exact same message, even when nothing changes.
For instance, if you have a custom vm.min_free_kbytes, just touching /sys/kernel/mm/transparent_hugepage/enabled causes a printk message. Example:
# sysctl -w vm.min_free_kbytes=112382
# for i in $(seq 100); do echo never > /sys/kernel/mm/transparent_hugepage/enabled ; doneand you have 100 WARN messages like the following, which is pretty dull:
khugepaged: min_free_kbytes is not updated to 112381 because user defined value 112382 is preferred
A similar message shows up when setting thp to "always":
# for i in $(seq 100); do
# echo 1024 > /proc/sys/vm/min_free_kbytes
# echo always > /sys/kernel/mm/transparent_hugepage/enabled
# doneAnd then, we have 100 messages like:
khugepaged: raising min_free_kbytes from 1024 to 67584 to help transparent hugepage allocations
This is more common when you have a configuration management system that writes the THP configuration without an extra read, assuming that nothing will happen if there is no change in the configuration, but it prints these annoying messages.
For instance, at Meta's fleet, ~10K servers were producing 3.5M of these messages per day.
Fix this by making the sysfs _store helpers easier to digest and ratelimiting the message.
This version is heavily based on Lorenzo's suggestion on V1.
---
Changes in v5:
- Used the lockless __test_and_set_bit() primitives (David)
- Rename the functions and variables to match the code (David)
- Link to v4: https://patch.msgid.link/[email protected]
Changes in v4:
- Use the enum instead of int in the new functions (akpm).
- Explicitly initialize the enum values (akpm).
- Link to v3: https://patch.msgid.link/[email protected]
Changes in v3:
- Extra ratelimit patch.
- Create two enums, one for anon and one for global. (Lorenzo)
- Remove the extern from set_recommended_min_free_kbytes (Lorenzo)
- Export set_recommended_min_free_kbytes() definition to mm/internal.h (Lorenzo)
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- V2 is heavily based on Lorenzo and Kiryl feedback on v1.
- Link to v1: https://patch.msgid.link/[email protected]
---
Breno Leitao (4):
mm: khugepaged: export set_recommended_min_free_kbytes()
mm: huge_memory: refactor anon_enabled_store() with change_anon_orders()
mm: huge_memory: refactor enabled_store() with change_enabled()
mm: ratelimit min_free_kbytes adjustment messages
mm/huge_memory.c | 147 +++++++++++++++++++++++++++++++++++++------------------ mm/internal.h | 5 ++ mm/khugepaged.c | 6 +-- mm/page_alloc.c | 4 +- 4 files changed, 110 insertions(+), 52 deletions(-)
---
base-commit: 9dd5012f78d699f7a6051583dc53adeb401e28f0
change-id: 20260303-thp_logs-059d6b80f6d6
Best regards,
Signature · 1 line
Breno Leitao <[email protected]>
PATCH v5 1/4 mm: khugepaged: export set_recommended_min_free_kbytes()
Make set_recommended_min_free_kbytes() callable from outside khugepaged.c by removing the static qualifier and adding a declaration in mm/internal.h.
This allows callers that change THP settings to recalculate watermarks without going through start_stop_khugepaged().
--- mm/internal.h | 5 +++++ mm/khugepaged.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-)
mm/internal.h +5 -0
@@ -623,6 +623,11 @@ int user_proactive_reclaim(char *buf,*/ pmd_t *mm_find_pmd (struct mm_struct *mm,unsigned long address); +/* +* in mm/khugepaged.c +*/ +void set_recommended_min_free_kbytes (void ); +/* * in mm/page_alloc.c */
mm/khugepaged.c +1 -1
@@ -2630,7 +2630,7 @@ static int khugepaged(void *none)return 0 ; } -static void set_recommended_min_free_kbytes (void ) +void set_recommended_min_free_kbytes (void ) {struct zone *zone;int nr_zones =0 ;
Signature · 1 line
2.52.0
Re: [PATCH v5 1/4] mm: khugepaged: export set_recommended_min_free_kbytes()
Quoted 7 lines · Breno Leitao
Make
set_recommended_min_free_kbytes()callable from outsidekhugepaged.cby removing the static qualifier and adding a declaration inmm/internal.h.This allows callers that change THP settings to recalculate watermarks without going through
start_stop_khugepaged().
Quoted 9 lines
Suggested-by Lorenzo Stoakes (Oracle) <[email protected]>Reviewed-by Lorenzo Stoakes (Oracle) <[email protected]>Signed-off-by Breno Leitao <[email protected]>--- mm/internal.h | 5 +++++ mm/khugepaged.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-)
Best Regards,
Yan, Zi
PATCH v5 2/4 mm: huge_memory: refactor anon_enabled_store() with change_anon_orders()
In reply to Breno LeitaoConsolidate the repeated spin_lock/set_bit/clear_bit pattern in anon_enabled_store() into a new change_anon_orders() helper that loops over an orders[] array, setting the bit for the selected mode and clearing the others.
Introduce enum anon_enabled_mode and anon_enabled_mode_strings[] for the per-order anon THP setting.
Use sysfs_match_string() with the anon_enabled_mode_strings[] table to replace the if/else chain of sysfs_streq() calls.
The helper uses test_and_set_bit()/test_and_clear_bit() to track whether the state actually changed, so start_stop_khugepaged() is only called when needed. When the mode is unchanged, set_recommended_min_free_kbytes() is called directly to preserve the watermark recalculation behavior of the original code.
--- mm/huge_memory.c | 84 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 32 deletions(-)
mm/huge_memory.c +52 -32
@@ -316,6 +316,20 @@ static ssize_t enabled_show(struct kobject *kobj,return sysfs_emit (buf,"%s\n" , output); } +enum anon_enabled_mode { +ANON_ENABLED_ALWAYS =0 , +ANON_ENABLED_MADVISE =1 , +ANON_ENABLED_INHERIT =2 , +ANON_ENABLED_NEVER =3 , +}; + +static const char *const anon_enabled_mode_strings[] = { + [ANON_ENABLED_ALWAYS ] ="always" , + [ANON_ENABLED_MADVISE ] ="madvise" , + [ANON_ENABLED_INHERIT ] ="inherit" , + [ANON_ENABLED_NEVER ] ="never" , +}; +static ssize_t enabled_store (struct kobject *kobj,struct kobj_attribute *attr,const char *buf,size_t count)
@@ -515,48 +529,54 @@ static ssize_t anon_enabled_show(struct kobject *kobj,return sysfs_emit (buf,"%s\n" , output); } +static bool set_anon_enabled_mode (int order,enum anon_enabled_mode mode) +{ +static unsigned long *enabled_orders[] = { + &huge_anon_orders_always, + &huge_anon_orders_madvise, + &huge_anon_orders_inherit, + }; +enum anon_enabled_mode m; +bool changed = false; + +spin_lock (&huge_anon_orders_lock); +for (m =0 ; m <ARRAY_SIZE (enabled_orders); m++) { +if (m == mode) + changed |= !__test_and_set_bit (order, enabled_orders[m]); +else + changed |=__test_and_clear_bit (order, enabled_orders[m]); + } +spin_unlock (&huge_anon_orders_lock); + +return changed; +} +static ssize_t anon_enabled_store (struct kobject *kobj,struct kobj_attribute *attr,const char *buf,size_t count) {int order =to_thpsize (kobj)->order; -ssize_t ret = count; +int mode; -if (sysfs_streq (buf,"always" )) { -spin_lock (&huge_anon_orders_lock); -clear_bit (order, &huge_anon_orders_inherit); -clear_bit (order, &huge_anon_orders_madvise); -set_bit (order, &huge_anon_orders_always); -spin_unlock (&huge_anon_orders_lock); - }else if (sysfs_streq (buf,"inherit" )) { -spin_lock (&huge_anon_orders_lock); -clear_bit (order, &huge_anon_orders_always); -clear_bit (order, &huge_anon_orders_madvise); -set_bit (order, &huge_anon_orders_inherit); -spin_unlock (&huge_anon_orders_lock); - }else if (sysfs_streq (buf,"madvise" )) { -spin_lock (&huge_anon_orders_lock); -clear_bit (order, &huge_anon_orders_always); -clear_bit (order, &huge_anon_orders_inherit); -set_bit (order, &huge_anon_orders_madvise); -spin_unlock (&huge_anon_orders_lock); - }else if (sysfs_streq (buf,"never" )) { -spin_lock (&huge_anon_orders_lock); -clear_bit (order, &huge_anon_orders_always); -clear_bit (order, &huge_anon_orders_inherit); -clear_bit (order, &huge_anon_orders_madvise); -spin_unlock (&huge_anon_orders_lock); - }else - ret = -EINVAL ; + mode =sysfs_match_string (anon_enabled_mode_strings, buf); +if (mode <0 ) +return -EINVAL ; -if (ret >0 ) { -int err; +if (set_anon_enabled_mode (order, mode)) { +int err =start_stop_khugepaged (); - err =start_stop_khugepaged ();if (err) - ret = err; +return err; + }else { +/* +* Recalculate watermarks even when the mode didn't +* change, as the previous code always called +* start_stop_khugepaged() which does this internally. +*/ +set_recommended_min_free_kbytes (); } -return ret; + +return count; } static struct kobj_attribute anon_enabled_attr =
Signature · 1 line
2.52.0
Re: [PATCH v5 2/4] mm: huge_memory: refactor anon_enabled_store() with change_anon_orders()
Quoted 17 lines · Breno Leitao
Consolidate the repeated spin_lock/set_bit/clear_bit pattern in
anon_enabled_store()into a newchange_anon_orders()helper that loops over an orders[] array, setting the bit for the selected mode and clearing the others.Introduce enum
anon_enabled_modeand anon_enabled_mode_strings[] for the per-order anon THP setting.Use
sysfs_match_string()with the anon_enabled_mode_strings[] table to replace the if/else chain ofsysfs_streq()calls.The helper uses test_and_set_bit()/test_and_clear_bit() to track whether the state actually changed, so
start_stop_khugepaged()is only called when needed. When the mode is unchanged,set_recommended_min_free_kbytes()is called directly to preserve the watermark recalculation behavior of the original code.
Quoted 6 lines
Signed-off-by Breno Leitao <[email protected]>Reviewed-by Lorenzo Stoakes (Oracle) <[email protected]>--- mm/huge_memory.c | 84 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 32 deletions(-)
Nice cleanup.
Best Regards,
Yan, Zi
Re: [PATCH v5 2/4] mm: huge_memory: refactor anon_enabled_store() with change_anon_orders()
In reply to Breno LeitaoQuoted 35 lines · Breno Leitao
Consolidate the repeated spin_lock/set_bit/clear_bit pattern in
anon_enabled_store()into a newchange_anon_orders()helper that loops over an orders[] array, setting the bit for the selected mode and clearing the others.Introduce enum
anon_enabled_modeand anon_enabled_mode_strings[] for the per-order anon THP setting.Use
sysfs_match_string()with the anon_enabled_mode_strings[] table to replace the if/else chain ofsysfs_streq()calls.The helper uses test_and_set_bit()/test_and_clear_bit() to track whether the state actually changed, so
start_stop_khugepaged()is only called when needed. When the mode is unchanged,set_recommended_min_free_kbytes()is called directly to preserve the watermark recalculation behavior of the original code.Signed-off-by Breno Leitao <[email protected]>Reviewed-by Lorenzo Stoakes (Oracle) <[email protected]>--- mm/huge_memory.c | 84 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 32 deletions(-)mm/huge_memory.c +3
-0@@ -316,6 +316,20 @@ static ssize_t enabled_show(struct kobject *kobj,return sysfs_emit (buf,"%s\n" , output); } +enum anon_enabled_mode { +ANON_ENABLED_ALWAYS =0 , +ANON_ENABLED_MADVISE =1 ,
mm/huge_memory.c static ssize_t enabled_show(struct kobject *kobj, · line 331
Quoted 10 lines
mm/huge_memory.c +10
-0+ANON_ENABLED_INHERIT =2 , + ANON_ENABLED_NEVER = 3, +}; + +static const char *const anon_enabled_mode_strings[] = { + [ANON_ENABLED_ALWAYS ] ="always" , + [ANON_ENABLED_MADVISE ] ="madvise" , + [ANON_ENABLED_INHERIT ] ="inherit" , + [ANON_ENABLED_NEVER ] ="never" , +};
Do we really need anon_enabled_mode here? Would it be ok to just use ...
static const char * const anon_enabled_mode_strings[] = {
"always", "madvise", "inherit", "never",
};Nobody else cares about the values like ANON_ENABLED_ALWAYS except the sysfs interface, right? Maybe we don't need to force people to understand the enum.
Quoted 5 lines
mm/huge_memory.c +1
-0+static ssize_t enabled_store (struct kobject *kobj,struct kobj_attribute *attr,const char *buf,size_t count)
mm/huge_memory.c static ssize_t anon_enabled_show(struct kobject *kobj, · line 538
Quoted 10 lines
mm/huge_memory.c +7
-0@@ -515,48 +529,54 @@ static ssize_t anon_enabled_show(struct kobject *kobj,return sysfs_emit (buf,"%s\n" , output); } +static bool set_anon_enabled_mode (int order,enum anon_enabled_mode mode) +{ +static unsigned long *enabled_orders[] = { + &huge_anon_orders_always, + &huge_anon_orders_madvise, + &huge_anon_orders_inherit, + };
Is there a reason enabled_orders needs to be static? Could it instead be allocated on the stack?
Thanks
Barry
Andrew Morton
maintainerRe: [PATCH v5 2/4] mm: huge_memory: refactor anon_enabled_store() with change_anon_orders()
Quoted 10 lines · On Wed, 11 Mar 2026 04:11:36 +0800 Barry Song <[email protected]> wrote:
mm/huge_memory.c static ssize_t anon_enabled_show(struct kobject *kobj, · line 538
Quoted 7 lines
mm/huge_memory.c +7
-0+static bool set_anon_enabled_mode (int order,enum anon_enabled_mode mode) +{ +static unsigned long *enabled_orders[] = { + &huge_anon_orders_always, + &huge_anon_orders_madvise, + &huge_anon_orders_inherit, + };Is there a reason
enabled_ordersneeds to be static? Could it instead be allocated on the stack?
Could, but then the array would be rebuilt each time the function is called. The idea here is to save a bit of runtime cost.
Re: [PATCH v5 2/4] mm: huge_memory: refactor anon_enabled_store() with change_anon_orders()
Quoted 14 lines · Andrew Morton
Quoted 10 lines · On Wed, 11 Mar 2026 04:11:36 +0800 Barry Song <[email protected]> wrote:
mm/huge_memory.c static ssize_t anon_enabled_show(struct kobject *kobj, · line 538
Quoted 7 lines
mm/huge_memory.c +7
-0+static bool set_anon_enabled_mode (int order,enum anon_enabled_mode mode) +{ +static unsigned long *enabled_orders[] = { + &huge_anon_orders_always, + &huge_anon_orders_madvise, + &huge_anon_orders_inherit, + };Is there a reason
enabled_ordersneeds to be static? Could it instead be allocated on the stack?
Quoted 2 lines
Could, but then the array would be rebuilt each time the function is called. The idea here is to save a bit of runtime cost.
I see. My understanding is that this function is very cold, so we don't really care about its performance. But I'm fine with keeping it static if you prefer.
Thanks
Barry
Re: [PATCH v5 2/4] mm: huge_memory: refactor anon_enabled_store() with change_anon_orders()
In reply to Breno LeitaoQuoted 17 lines · Breno Leitao
Consolidate the repeated spin_lock/set_bit/clear_bit pattern in
anon_enabled_store()into a newchange_anon_orders()helper that loops over an orders[] array, setting the bit for the selected mode and clearing the others.Introduce enum
anon_enabled_modeand anon_enabled_mode_strings[] for the per-order anon THP setting.Use
sysfs_match_string()with the anon_enabled_mode_strings[] table to replace the if/else chain ofsysfs_streq()calls.The helper uses test_and_set_bit()/test_and_clear_bit() to track whether the state actually changed, so
start_stop_khugepaged()is only called when needed. When the mode is unchanged,set_recommended_min_free_kbytes()is called directly to preserve the watermark recalculation behavior of the original code.
Quoted 3 lines
Signed-off-by Breno Leitao <[email protected]>Reviewed-by Lorenzo Stoakes (Oracle) <[email protected]>---
LGTM.
Re: [PATCH v5 2/4] mm: huge_memory: refactor anon_enabled_store() with change_anon_orders()
In reply to Breno LeitaoQuoted 35 lines · Breno Leitao
Consolidate the repeated spin_lock/set_bit/clear_bit pattern in
anon_enabled_store()into a newchange_anon_orders()helper that loops over an orders[] array, setting the bit for the selected mode and clearing the others.Introduce enum
anon_enabled_modeand anon_enabled_mode_strings[] for the per-order anon THP setting.Use
sysfs_match_string()with the anon_enabled_mode_strings[] table to replace the if/else chain ofsysfs_streq()calls.The helper uses test_and_set_bit()/test_and_clear_bit() to track whether the state actually changed, so
start_stop_khugepaged()is only called when needed. When the mode is unchanged,set_recommended_min_free_kbytes()is called directly to preserve the watermark recalculation behavior of the original code.Signed-off-by Breno Leitao <[email protected]>Reviewed-by Lorenzo Stoakes (Oracle) <[email protected]>---
mm/huge_memory.c| 84 +++++++++++++++++++++++++++++++++++---------------------1 file changed, 52 insertions(+), 32 deletions(-)mm/huge_memory.c +4
-0@@ -316,6 +316,20 @@ static ssize_t enabled_show(struct kobject *kobj,return sysfs_emit (buf,"%s\n" , output); } +enum anon_enabled_mode { +ANON_ENABLED_ALWAYS =0 , +ANON_ENABLED_MADVISE =1 , +ANON_ENABLED_INHERIT =2 ,
mm/huge_memory.c static ssize_t enabled_show(struct kobject *kobj, · line 332
Quoted 10 lines
mm/huge_memory.c +10
-0+ ANON_ENABLED_NEVER = 3, +}; + +static const char *const anon_enabled_mode_strings[] = { + [ANON_ENABLED_ALWAYS ] ="always" , + [ANON_ENABLED_MADVISE ] ="madvise" , + [ANON_ENABLED_INHERIT ] ="inherit" , + [ANON_ENABLED_NEVER ] ="never" , +}; +
Just one trivial thing, maybe keep the sequence as the sysfs output?
Currently the output of /sys/kernel/transparent_hugepage/hugepages-xxxkB is
always inherit madvise [never]
But no strong opinion on this.
Quoted 19 lines
mm/huge_memory.c +12
-0static ssize_t enabled_store (struct kobject *kobj,struct kobj_attribute *attr,const char *buf,size_t count)@@ -515,48 +529,54 @@ static ssize_t anon_enabled_show(struct kobject *kobj,return sysfs_emit (buf,"%s\n" , output); } +static bool set_anon_enabled_mode (int order,enum anon_enabled_mode mode) +{ +static unsigned long *enabled_orders[] = { + &huge_anon_orders_always, + &huge_anon_orders_madvise, + &huge_anon_orders_inherit, + }; +enum anon_enabled_mode m; +bool changed = false; + +spin_lock (&huge_anon_orders_lock); +for (m =0 ; m <ARRAY_SIZE (enabled_orders); m++) {
mm/huge_memory.c static ssize_t anon_enabled_show(struct kobject *kobj, · line 553
Quoted 10 lines
mm/huge_memory.c +10
-0+if (m == mode) + changed |= !__test_and_set_bit (order, enabled_orders[m]); +else + changed |=__test_and_clear_bit (order, enabled_orders[m]); + } +spin_unlock (&huge_anon_orders_lock); + +return changed; +} +
Nice cleanup, so
Quoted 65 lines
mm/huge_memory.c +16
-32static ssize_t anon_enabled_store (struct kobject *kobj,struct kobj_attribute *attr,const char *buf,size_t count) {int order =to_thpsize (kobj)->order; -ssize_t ret = count; +int mode; -if (sysfs_streq (buf,"always" )) { -spin_lock (&huge_anon_orders_lock); -clear_bit (order, &huge_anon_orders_inherit); -clear_bit (order, &huge_anon_orders_madvise); -set_bit (order, &huge_anon_orders_always); -spin_unlock (&huge_anon_orders_lock); - }else if (sysfs_streq (buf,"inherit" )) { -spin_lock (&huge_anon_orders_lock); -clear_bit (order, &huge_anon_orders_always); -clear_bit (order, &huge_anon_orders_madvise); -set_bit (order, &huge_anon_orders_inherit); -spin_unlock (&huge_anon_orders_lock); - }else if (sysfs_streq (buf,"madvise" )) { -spin_lock (&huge_anon_orders_lock); -clear_bit (order, &huge_anon_orders_always); -clear_bit (order, &huge_anon_orders_inherit); -set_bit (order, &huge_anon_orders_madvise); -spin_unlock (&huge_anon_orders_lock); - }else if (sysfs_streq (buf,"never" )) { -spin_lock (&huge_anon_orders_lock); -clear_bit (order, &huge_anon_orders_always); -clear_bit (order, &huge_anon_orders_inherit); -clear_bit (order, &huge_anon_orders_madvise); -spin_unlock (&huge_anon_orders_lock); - }else - ret = -EINVAL ; + mode =sysfs_match_string (anon_enabled_mode_strings, buf); +if (mode <0 ) +return -EINVAL ; -if (ret >0 ) { -int err; +if (set_anon_enabled_mode (order, mode)) { +int err =start_stop_khugepaged (); - err =start_stop_khugepaged ();if (err) - ret = err; +return err; + }else { +/* +* Recalculate watermarks even when the mode didn't +* change, as the previous code always called +* start_stop_khugepaged() which does this internally. +*/ +set_recommended_min_free_kbytes (); } -return ret; + +return count; }static struct
kobj_attributeanon_enabled_attr=Signature · 1 line
2.52.0
Signature · 2 lines
Wei Yang Help you, Help me
Re: [PATCH v5 2/4] mm: huge_memory: refactor anon_enabled_store() with change_anon_orders()
Quoted 49 lines · Wei Yang
Quoted 35 lines · Breno Leitao
Consolidate the repeated spin_lock/set_bit/clear_bit pattern in
anon_enabled_store()into a newchange_anon_orders()helper that loops over an orders[] array, setting the bit for the selected mode and clearing the others.Introduce enum
anon_enabled_modeand anon_enabled_mode_strings[] for the per-order anon THP setting.Use
sysfs_match_string()with the anon_enabled_mode_strings[] table to replace the if/else chain ofsysfs_streq()calls.The helper uses test_and_set_bit()/test_and_clear_bit() to track whether the state actually changed, so
start_stop_khugepaged()is only called when needed. When the mode is unchanged,set_recommended_min_free_kbytes()is called directly to preserve the watermark recalculation behavior of the original code.Signed-off-by Breno Leitao <[email protected]>Reviewed-by Lorenzo Stoakes (Oracle) <[email protected]>---
mm/huge_memory.c| 84 +++++++++++++++++++++++++++++++++++---------------------1 file changed, 52 insertions(+), 32 deletions(-)mm/huge_memory.c +4
-0@@ -316,6 +316,20 @@ static ssize_t enabled_show(struct kobject *kobj,return sysfs_emit (buf,"%s\n" , output); } +enum anon_enabled_mode { +ANON_ENABLED_ALWAYS =0 , +ANON_ENABLED_MADVISE =1 , +ANON_ENABLED_INHERIT =2 ,mm/huge_memory.c static ssize_t enabled_show(struct kobject *kobj, · line 332
Quoted 10 lines
mm/huge_memory.c +10
-0+ ANON_ENABLED_NEVER = 3, +}; + +static const char *const anon_enabled_mode_strings[] = { + [ANON_ENABLED_ALWAYS ] ="always" , + [ANON_ENABLED_MADVISE ] ="madvise" , + [ANON_ENABLED_INHERIT ] ="inherit" , + [ANON_ENABLED_NEVER ] ="never" , +}; +Just one trivial thing, maybe keep the sequence as the sysfs output?
Quoted 5 lines
Currently the output of
/sys/kernel/transparent_hugepage/hugepages-xxxkBisalways inherit madvise [never]
But no strong opinion on this.
Yeah, keeping the enum order consistent with the sysfs output looks sensible :)
Apart from that, LGTM.
Re: [PATCH v5 2/4] mm: huge_memory: refactor anon_enabled_store() with change_anon_orders()
Quoted 19 lines · Lance Yang
Quoted 18 lines · Wei Yang
Quoted 4 lines · Breno Leitao
mm/huge_memory.c +4
-0+enum anon_enabled_mode { +ANON_ENABLED_ALWAYS =0 , +ANON_ENABLED_MADVISE =1 , +ANON_ENABLED_INHERIT =2 ,mm/huge_memory.c static ssize_t enabled_show(struct kobject *kobj, · line 332
Quoted 10 lines
mm/huge_memory.c +10
-0+ ANON_ENABLED_NEVER = 3, +}; + +static const char *const anon_enabled_mode_strings[] = { + [ANON_ENABLED_ALWAYS ] ="always" , + [ANON_ENABLED_MADVISE ] ="madvise" , + [ANON_ENABLED_INHERIT ] ="inherit" , + [ANON_ENABLED_NEVER ] ="never" , +}; +Just one trivial thing, maybe keep the sequence as the sysfs output?
Quoted 8 lines
Quoted 5 lines
Currently the output of
/sys/kernel/transparent_hugepage/hugepages-xxxkBisalways inherit madvise [never]
But no strong opinion on this.
Yeah, keeping the enum order consistent with the sysfs output looks sensible :)
Good point, I'll adjust the order to match the sysfs output and send out a new version.
Thanks for the feedback!
--breno
PATCH v5 3/4 mm: huge_memory: refactor enabled_store() with change_enabled()
In reply to Breno LeitaoRefactor enabled_store() to use a new change_enabled() helper. Introduce a separate enum global_enabled_mode and global_enabled_mode_strings[], mirroring the anon_enabled_mode pattern from the previous commit.
A separate enum is necessary because the global THP setting does not support "inherit", only "always", "madvise", and "never". Reusing anon_enabled_mode would leave a NULL gap in the string array, causing sysfs_match_string() to stop early and fail to match entries after the gap.
The helper uses the same loop pattern as set_anon_enabled_mode(), iterating over an array of flag bit positions and using __test_and_set_bit()/__test_and_clear_bit() to track whether the state actually changed.
--- mm/huge_memory.c | 63 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 15 deletions(-)
mm/huge_memory.c +48 -15
@@ -330,30 +330,63 @@ static const char * const anon_enabled_mode_strings[] = { [ANON_ENABLED_NEVER] = "never", }; +enum global_enabled_mode { +GLOBAL_ENABLED_ALWAYS =0 , +GLOBAL_ENABLED_MADVISE =1 , +GLOBAL_ENABLED_NEVER =2 , +}; + +static const char *const global_enabled_mode_strings[] = { + [GLOBAL_ENABLED_ALWAYS ] ="always" , + [GLOBAL_ENABLED_MADVISE ] ="madvise" , + [GLOBAL_ENABLED_NEVER ] ="never" , +}; + +static bool set_global_enabled_mode (enum global_enabled_mode mode) +{ +static const unsigned long thp_flags[] = { +TRANSPARENT_HUGEPAGE_FLAG , +TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG , + }; +enum global_enabled_mode m; +bool changed = false; + +for (m =0 ; m <ARRAY_SIZE (thp_flags); m++) { +if (m == mode) + changed |= !__test_and_set_bit (thp_flags[m], + &transparent_hugepage_flags); +else + changed |=__test_and_clear_bit (thp_flags[m], + &transparent_hugepage_flags); + } + +return changed; +} +static ssize_t enabled_store (struct kobject *kobj,struct kobj_attribute *attr,const char *buf,size_t count) { -ssize_t ret = count; +int mode; -if (sysfs_streq (buf,"always" )) { -clear_bit (TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG , &transparent_hugepage_flags); -set_bit (TRANSPARENT_HUGEPAGE_FLAG , &transparent_hugepage_flags); - }else if (sysfs_streq (buf,"madvise" )) { -clear_bit (TRANSPARENT_HUGEPAGE_FLAG , &transparent_hugepage_flags); -set_bit (TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG , &transparent_hugepage_flags); - }else if (sysfs_streq (buf,"never" )) { -clear_bit (TRANSPARENT_HUGEPAGE_FLAG , &transparent_hugepage_flags); -clear_bit (TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG , &transparent_hugepage_flags); - }else - ret = -EINVAL ; + mode =sysfs_match_string (global_enabled_mode_strings, buf); +if (mode <0 ) +return -EINVAL ; -if (ret >0 ) { +if (set_global_enabled_mode (mode)) {int err =start_stop_khugepaged (); +if (err) - ret = err; +return err; + }else { +/* +* Recalculate watermarks even when the mode didn't +* change, as the previous code always called +* start_stop_khugepaged() which does this internally. +*/ +set_recommended_min_free_kbytes (); } -return ret; +return count; }static struct kobj_attribute enabled_attr =__ATTR_RW (enabled);
Signature · 1 line
2.52.0
Re: [PATCH v5 3/4] mm: huge_memory: refactor enabled_store() with change_enabled()
Quoted 16 lines · Breno Leitao
Refactor
enabled_store()to use a newchange_enabled()helper. Introduce a separate enumglobal_enabled_modeand global_enabled_mode_strings[], mirroring theanon_enabled_modepattern from the previous commit.A separate enum is necessary because the global THP setting does not support "inherit", only "always", "madvise", and "never". Reusing
anon_enabled_modewould leave a NULL gap in the string array, causingsysfs_match_string()to stop early and fail to match entries after the gap.The helper uses the same loop pattern as
set_anon_enabled_mode(), iterating over an array of flag bit positions and using __test_and_set_bit()/__test_and_clear_bit() to track whether the state actually changed.
Quoted 6 lines
Signed-off-by Breno Leitao <[email protected]>Reviewed-by Lorenzo Stoakes (Oracle) <[email protected]>--- mm/huge_memory.c | 63 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 15 deletions(-)
Nice cleanup.
Best Regards,
Yan, Zi
Re: [PATCH v5 3/4] mm: huge_memory: refactor enabled_store() with change_enabled()
In reply to Breno LeitaoQuoted 11 lines · Breno Leitao
Refactor
enabled_store()to use a newchange_enabled()helper. Introduce a separate enumglobal_enabled_modeand global_enabled_mode_strings[], mirroring theanon_enabled_modepattern from the previous commit.A separate enum is necessary because the global THP setting does not support "inherit", only "always", "madvise", and "never". Reusing
anon_enabled_modewould leave a NULL gap in the string array, causingsysfs_match_string()to stop early and fail to match entries after the gap.
Quoted 8 lines
The helper uses the same loop pattern as
set_anon_enabled_mode(), iterating over an array of flag bit positions and using __test_and_set_bit()/__test_and_clear_bit() to track whether the state actually changed.Signed-off-by Breno Leitao <[email protected]>Reviewed-by Lorenzo Stoakes (Oracle) <[email protected]>---
LGTM.
Re: [PATCH v5 3/4] mm: huge_memory: refactor enabled_store() with change_enabled()
In reply to Breno LeitaoQuoted 11 lines · Breno Leitao
Refactor
enabled_store()to use a newchange_enabled()helper. Introduce a separate enumglobal_enabled_modeand global_enabled_mode_strings[], mirroring theanon_enabled_modepattern from the previous commit.A separate enum is necessary because the global THP setting does not support "inherit", only "always", "madvise", and "never". Reusing
anon_enabled_modewould leave a NULL gap in the string array, causingsysfs_match_string()to stop early and fail to match entries after the gap.
Quoted 7 lines
The helper uses the same loop pattern as
set_anon_enabled_mode(), iterating over an array of flag bit positions and using __test_and_set_bit()/__test_and_clear_bit() to track whether the state actually changed.Signed-off-by Breno Leitao <[email protected]>Reviewed-by Lorenzo Stoakes (Oracle) <[email protected]>
Signature · 2 lines
Wei Yang Help you, Help me
PATCH v5 4/4 mm: ratelimit min_free_kbytes adjustment messages
In reply to Breno LeitaoThe "raising min_free_kbytes" pr_info message in set_recommended_min_free_kbytes() and the "min_free_kbytes is not updated to" pr_warn in calculate_min_free_kbytes() can spam the kernel log when called repeatedly.
Switch the pr_info in set_recommended_min_free_kbytes() and the pr_warn in calculate_min_free_kbytes() to their _ratelimited variants to prevent the log spam for this message.
--- mm/khugepaged.c | 4 ++-- mm/page_alloc.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
mm/khugepaged.c +2 -2
@@ -2671,8 +2671,8 @@ void set_recommended_min_free_kbytes(void)if (recommended_min > min_free_kbytes) {if (user_min_free_kbytes >=0 ) -pr_info ("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n" , - min_free_kbytes, recommended_min); +pr_info_ratelimited ("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n" , + min_free_kbytes, recommended_min); min_free_kbytes = recommended_min; }
mm/page_alloc.c +2 -2
@@ -6553,8 +6553,8 @@ void calculate_min_free_kbytes(void)if (new_min_free_kbytes > user_min_free_kbytes) min_free_kbytes =clamp (new_min_free_kbytes,128 ,262144 );else -pr_warn ("min_free_kbytes is not updated to %d because user defined value %d is preferred\n" , - new_min_free_kbytes, user_min_free_kbytes); +pr_warn_ratelimited ("min_free_kbytes is not updated to %d because user defined value %d is preferred\n" , + new_min_free_kbytes, user_min_free_kbytes); }
Signature · 1 line
2.52.0
Re: [PATCH v5 4/4] mm: ratelimit min_free_kbytes adjustment messages
Quoted 9 lines · Breno Leitao
The "raising
min_free_kbytes"pr_infomessage inset_recommended_min_free_kbytes()and the "min_free_kbytesis not updated to"pr_warnincalculate_min_free_kbytes()can spam the kernel log when called repeatedly.Switch the
pr_infoinset_recommended_min_free_kbytes()and thepr_warnincalculate_min_free_kbytes()to their_ratelimitedvariants to prevent the log spam for this message.
Quoted 9 lines
Reviewed-by Lorenzo Stoakes (Oracle) <[email protected]>Reviewed-by Baolin Wang <[email protected]>Signed-off-by Breno Leitao <[email protected]>--- mm/khugepaged.c | 4 ++-- mm/page_alloc.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
Makes sense.
Best Regards,
Yan, Zi