mm: huge_memory: clean up defrag sysfs with shared data tables
Subsystems (2)
v2 cover mm: huge_memory: clean up defrag sysfs with shared data tables
Refactor defrag_store() and defrag_show() to use shared data tables instead of duplicated if/else chains.
Patch 1 introduces an enum defrag_mode, a defrag_mode_strings[] table, and a defrag_flags[] mapping array, then rewrites defrag_store() to use sysfs_match_string() with a loop over defrag_flags[].
Patch 2 refactors defrag_show() to use the same arrays, replacing its hardcoded if/else chain of test_bit() calls and string literals.
This follows the same pattern applied to anon_enabled_store() in commit 522dfb4ba71f ("mm: huge_memory: refactor anon_enabled_store() with change_anon_orders()").
Breno Leitao (2):
mm: huge_memory: use sysfs_match_string() in defrag_store()
mm: huge_memory: refactor defrag_show() to use defrag_flags[]
---
Changes in v2:
- Do not be too verbose on enum defrag_mode (David Hildenbrand)
- Link to v1: https://patch.msgid.link/[email protected]
---
Breno Leitao (2):
mm: huge_memory: use sysfs_match_string() in defrag_store()
mm: huge_memory: refactor defrag_show() to use defrag_flags[]
mm/huge_memory.c | 98 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 56 insertions(+), 42 deletions(-)
---
base-commit: f3e6330d7fe42b204af05a2dbc68b379e0ad179e
change-id: 20260320-thp_defrag-540fc4f1fa3d
Best regards,
Signature · 1 line
Breno Leitao <[email protected]>
PATCH v2 1/2 mm: huge_memory: use sysfs_match_string() in defrag_store()
Replace the if/else chain of sysfs_streq() calls in defrag_store() with sysfs_match_string() and a defrag_mode_strings[] table.
Introduce enum defrag_mode and defrag_flags[] array mapping each mode to its corresponding transparent_hugepage_flag. The store function now loops over defrag_flags[], setting the bit for the selected mode and clearing the others. When mode is DEFRAG_NEVER (index 4), no index in the 4-element defrag_flags[] matches, so all flags are cleared.
Note that the enum ordering (always, defer, defer+madvise, madvise, never) differs from the original if/else chain order in defrag_store() (always, defer+madvise, defer, madvise, never). This is intentional to match the display order used by defrag_show().
This is a follow-up cleanup to commit 522dfb4ba71f ("mm: huge_memory:
refactor anon_enabled_store() with change_anon_orders()") which applied the same sysfs_match_string() pattern to anon_enabled_store().
--- mm/huge_memory.c | 60 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 26 deletions(-)
mm/huge_memory.c +34 -26
@@ -429,6 +429,29 @@ ssize_t single_hugepage_flag_store(struct kobject *kobj,return count; } +enum defrag_mode { +DEFRAG_ALWAYS =0 , +DEFRAG_DEFER , +DEFRAG_DEFER_MADVISE , +DEFRAG_MADVISE , +DEFRAG_NEVER , +}; + +static const char *const defrag_mode_strings[] = { + [DEFRAG_ALWAYS ] ="always" , + [DEFRAG_DEFER ] ="defer" , + [DEFRAG_DEFER_MADVISE ] ="defer+madvise" , + [DEFRAG_MADVISE ] ="madvise" , + [DEFRAG_NEVER ] ="never" , +}; + +static const enum transparent_hugepage_flag defrag_flags[] = { + [DEFRAG_ALWAYS ] =TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG , + [DEFRAG_DEFER ] =TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG , + [DEFRAG_DEFER_MADVISE ] =TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG , + [DEFRAG_MADVISE ] =TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG , +}; +static ssize_t defrag_show (struct kobject *kobj,struct kobj_attribute *attr,char *buf) {
@@ -456,34 +479,19 @@ static ssize_t defrag_store(struct kobject *kobj,struct kobj_attribute *attr, const char *buf, size_t count) { - if (sysfs_streq(buf, "always")) { - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); - } else if (sysfs_streq(buf, "defer+madvise")) { - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); - } else if (sysfs_streq(buf, "defer")) { - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); - } else if (sysfs_streq(buf, "madvise")) { - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); - } else if (sysfs_streq(buf, "never")) { - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); - } else + int mode, m; + + mode =sysfs_match_string (defrag_mode_strings, buf); +if (mode <0 )return -EINVAL ; +for (m =0 ; m <ARRAY_SIZE (defrag_flags); m++) { +if (m == mode) +set_bit (defrag_flags[m], &transparent_hugepage_flags); +else +clear_bit (defrag_flags[m], &transparent_hugepage_flags); + } +return count; }static struct kobj_attribute defrag_attr =__ATTR_RW (defrag);
Signature · 1 line
2.52.0
Re: [PATCH v2 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store()
Quoted 18 lines · Breno Leitao
Replace the if/else chain of
sysfs_streq()calls indefrag_store()withsysfs_match_string()and a defrag_mode_strings[] table.Introduce enum
defrag_modeand defrag_flags[] array mapping each mode to its correspondingtransparent_hugepage_flag. The store function now loops over defrag_flags[], setting the bit for the selected mode and clearing the others. When mode isDEFRAG_NEVER(index 4), no index in the 4-element defrag_flags[] matches, so all flags are cleared.Note that the enum ordering (always, defer, defer+madvise, madvise, never) differs from the original if/else chain order in
defrag_store()(always, defer+madvise, defer, madvise, never). This is intentional to match the display order used bydefrag_show().This is a follow-up cleanup to commit
522dfb4ba71f("mm:huge_memory:
refactoranon_enabled_store()withchange_anon_orders()") which applied the samesysfs_match_string()pattern toanon_enabled_store().
Quoted 5 lines
Tested-by Lance Yang <[email protected]>Reviewed-by Lance Yang <[email protected]>Reviewed-by Barry Song <[email protected]>Signed-off-by Breno Leitao <[email protected]>
Thanks for doing this, big improvement! :)
LGTM, so:
Quoted 88 lines
--- mm/huge_memory.c | 60 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 26 deletions(-)mm/huge_memory.c +34
-27@@ -429,6 +429,29 @@ ssize_t single_hugepage_flag_store(struct kobject *kobj,return count; } +enum defrag_mode { +DEFRAG_ALWAYS =0 , +DEFRAG_DEFER , +DEFRAG_DEFER_MADVISE , +DEFRAG_MADVISE , +DEFRAG_NEVER , +}; + +static const char *const defrag_mode_strings[] = { + [DEFRAG_ALWAYS ] ="always" , + [DEFRAG_DEFER ] ="defer" , + [DEFRAG_DEFER_MADVISE ] ="defer+madvise" , + [DEFRAG_MADVISE ] ="madvise" , + [DEFRAG_NEVER ] ="never" , +}; + +static const enum transparent_hugepage_flag defrag_flags[] = { + [DEFRAG_ALWAYS ] =TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG , + [DEFRAG_DEFER ] =TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG , + [DEFRAG_DEFER_MADVISE ] =TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG , + [DEFRAG_MADVISE ] =TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG , +}; +static ssize_t defrag_show (struct kobject *kobj,struct kobj_attribute *attr,char *buf) {@@ -456,34 +479,19 @@ static ssize_t defrag_store(struct kobject *kobj,struct kobj_attribute *attr, const char *buf, size_t count) { - if (sysfs_streq(buf, "always")) { - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); - } else if (sysfs_streq(buf, "defer+madvise")) { - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); - } else if (sysfs_streq(buf, "defer")) { - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); - } else if (sysfs_streq(buf, "madvise")) { - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); - } else if (sysfs_streq(buf, "never")) { - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); - } else + int mode, m; + + mode =sysfs_match_string (defrag_mode_strings, buf); +if (mode <0 )return -EINVAL ; +for (m =0 ; m <ARRAY_SIZE (defrag_flags); m++) { +if (m == mode) +set_bit (defrag_flags[m], &transparent_hugepage_flags); +else +clear_bit (defrag_flags[m], &transparent_hugepage_flags); + } +return count; }static struct kobj_attribute defrag_attr =__ATTR_RW (defrag); --2.52.0
PATCH v2 2/2 mm: huge_memory: refactor defrag_show() to use defrag_flags[]
In reply to Breno LeitaoReplace the hardcoded if/else chain of test_bit() calls and string literals in defrag_show() with a loop over defrag_flags[] and defrag_mode_strings[] arrays introduced in the previous commit.
This makes defrag_show() consistent with defrag_store() and eliminates the duplicated mode name strings.
--- mm/huge_memory.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-)
mm/huge_memory.c +22 -16
@@ -455,24 +455,30 @@ static const enum transparent_hugepage_flag defrag_flags[] = {static ssize_t defrag_show (struct kobject *kobj,struct kobj_attribute *attr,char *buf) { -const char *output; +int active =DEFRAG_NEVER ; +int len =0 ; +int i; -if (test_bit (TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG , - &transparent_hugepage_flags)) - output ="[always] defer defer+madvise madvise never" ; -else if (test_bit (TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG , - &transparent_hugepage_flags)) - output ="always [defer] defer+madvise madvise never" ; -else if (test_bit (TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG , - &transparent_hugepage_flags)) - output ="always defer [defer+madvise] madvise never" ; -else if (test_bit (TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG , - &transparent_hugepage_flags)) - output ="always defer defer+madvise [madvise] never" ; -else - output ="always defer defer+madvise madvise [never]" ; +for (i =0 ; i <ARRAY_SIZE (defrag_flags); i++) { +if (test_bit (defrag_flags[i], &transparent_hugepage_flags)) { + active = i; +break ; + } + } -return sysfs_emit (buf,"%s\n" , output); +for (i =0 ; i <ARRAY_SIZE (defrag_mode_strings); i++) { +if (i == active) + len +=sysfs_emit_at (buf, len,"[%s] " , + defrag_mode_strings[i]); +else + len +=sysfs_emit_at (buf, len,"%s " , + defrag_mode_strings[i]); + } + +/* Replace trailing space with newline */ + buf[len -1 ] ='\n' ; + +return len; } static ssize_t defrag_store(struct kobject *kobj,
Signature · 1 line
2.52.0
Re: [PATCH v2 2/2] mm: huge_memory: refactor defrag_show() to use defrag_flags[]
Quoted 4 lines · Breno Leitao
Replace the hardcoded if/else chain of
test_bit()calls and string literals indefrag_show()with a loop over defrag_flags[] and defrag_mode_strings[] arrays introduced in the previous commit.
Quoted 8 lines
This makes
defrag_show()consistent withdefrag_store()and eliminates the duplicated mode name strings.Tested-by Lance Yang <[email protected]>Reviewed-by Lance Yang <[email protected]>Reviewed-by Barry Song <[email protected]>Signed-off-by Breno Leitao <[email protected]>
I almost made a comment, but then realised it was fine :P so LGTM and:
Quoted 35 lines
--- mm/huge_memory.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-)mm/huge_memory.c +7
-15@@ -455,24 +455,30 @@ static const enum transparent_hugepage_flag defrag_flags[] = {static ssize_t defrag_show (struct kobject *kobj,struct kobj_attribute *attr,char *buf) { -const char *output; +int active =DEFRAG_NEVER ; +int len =0 ; +int i; -if (test_bit (TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG , - &transparent_hugepage_flags)) - output ="[always] defer defer+madvise madvise never" ; -else if (test_bit (TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG , - &transparent_hugepage_flags)) - output ="always [defer] defer+madvise madvise never" ; -else if (test_bit (TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG , - &transparent_hugepage_flags)) - output ="always defer [defer+madvise] madvise never" ; -else if (test_bit (TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG , - &transparent_hugepage_flags)) - output ="always defer defer+madvise [madvise] never" ; -else - output ="always defer defer+madvise madvise [never]" ; +for (i =0 ; i <ARRAY_SIZE (defrag_flags); i++) { +if (test_bit (defrag_flags[i], &transparent_hugepage_flags)) { + active = i; +break ;
mm/huge_memory.c static const enum transparent_hugepage_flag defrag_flags[] = { · line 474
Quoted 10 lines
mm/huge_memory.c +8
-1+ } + } -return sysfs_emit (buf,"%s\n" , output); +for (i =0 ; i <ARRAY_SIZE (defrag_mode_strings); i++) { +if (i == active) + len +=sysfs_emit_at (buf, len,"[%s] " , + defrag_mode_strings[i]); +else + len += sysfs_emit_at(buf, len, "%s ",
I mean, we'll end up with a single trailing space, but I think we can probably... oh wait hang on...
mm/huge_memory.c static const enum transparent_hugepage_flag defrag_flags[] = { · line 479
Quoted 5 lines
mm/huge_memory.c +5
-0+ defrag_mode_strings[i]); + } + +/* Replace trailing space with newline */ + buf[len -1 ] ='\n' ;
...OK :)
Quoted 9 lines
mm/huge_memory.c +2
-1+ +return len; } static ssize_t defrag_store(struct kobject *kobj, --2.52.0
Cheers, Lorenzo
Quoted 29 lines · Breno Leitao
Refactor
defrag_store()anddefrag_show()to use shared data tables instead of duplicated if/else chains.Patch 1 introduces an enum
defrag_mode, a defrag_mode_strings[] table, and a defrag_flags[] mapping array, then rewritesdefrag_store()to usesysfs_match_string()with a loop over defrag_flags[].Patch 2 refactors
defrag_show()to use the same arrays, replacing its hardcoded if/else chain oftest_bit()calls and string literals.This follows the same pattern applied to
anon_enabled_store()in commit522dfb4ba71f("mm:huge_memory: refactoranon_enabled_store()withchange_anon_orders()").Breno Leitao (2):
mm:huge_memory: usesysfs_match_string()indefrag_store()
mm:huge_memory: refactordefrag_show()to use defrag_flags[]Signed-off-by Breno Leitao <[email protected]>---
Changes in v2:
- Do not be too verbose on enumdefrag_mode(David Hildenbrand)
- Link to v1: https://patch.msgid.link/[email protected]---
Breno Leitao (2):
mm:huge_memory: usesysfs_match_string()indefrag_store()
mm:huge_memory: refactordefrag_show()to use defrag_flags[]
Quoted 5 lines
mm/huge_memory.c | 98 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 56 insertions(+), 42 deletions(-)---
base-commit:f3e6330d7fe42b204af05a2dbc68b379e0ad179e
change-id: 20260320-thp_defrag-540fc4f1fa3d
For the whole series,
Thanks.
Best Regards,
Yan, Zi