* [PATCH 0/2] mm: huge_memory: clean up defrag sysfs with shared data tables
@ 2026-03-20 16:05 Breno Leitao
2026-03-20 16:05 ` [PATCH 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store() Breno Leitao
2026-03-20 16:05 ` [PATCH 2/2] mm: huge_memory: refactor defrag_show() to use defrag_flags[] Breno Leitao
0 siblings, 2 replies; 12+ messages in thread
From: Breno Leitao @ 2026-03-20 16:05 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Lorenzo Stoakes, Zi Yan,
Baolin Wang, Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain,
Barry Song, Lance Yang
Cc: linux-mm, linux-kernel, Breno Leitao, kernel-team
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[]
Signed-off-by: Breno Leitao <leitao@debian.org>
---
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: 785f0eb2f85decbe7c1ef9ae922931f0194ffc2e
change-id: 20260320-thp_defrag-540fc4f1fa3d
Best regards,
--
Breno Leitao <leitao@debian.org>
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store()
2026-03-20 16:05 [PATCH 0/2] mm: huge_memory: clean up defrag sysfs with shared data tables Breno Leitao
@ 2026-03-20 16:05 ` Breno Leitao
2026-04-02 8:21 ` David Hildenbrand (Arm)
` (2 more replies)
2026-03-20 16:05 ` [PATCH 2/2] mm: huge_memory: refactor defrag_show() to use defrag_flags[] Breno Leitao
1 sibling, 3 replies; 12+ messages in thread
From: Breno Leitao @ 2026-03-20 16:05 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Lorenzo Stoakes, Zi Yan,
Baolin Wang, Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain,
Barry Song, Lance Yang
Cc: linux-mm, linux-kernel, Breno Leitao, kernel-team
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().
Signed-off-by: Breno Leitao <leitao@debian.org>
---
mm/huge_memory.c | 60 ++++++++++++++++++++++++++++++++------------------------
1 file changed, 34 insertions(+), 26 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 3fc02913b63e3..4843e2154038f 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -421,6 +421,29 @@ ssize_t single_hugepage_flag_store(struct kobject *kobj,
return count;
}
+enum defrag_mode {
+ DEFRAG_ALWAYS = 0,
+ DEFRAG_DEFER = 1,
+ DEFRAG_DEFER_MADVISE = 2,
+ DEFRAG_MADVISE = 3,
+ DEFRAG_NEVER = 4,
+};
+
+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)
{
@@ -448,34 +471,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
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH 2/2] mm: huge_memory: refactor defrag_show() to use defrag_flags[]
2026-03-20 16:05 [PATCH 0/2] mm: huge_memory: clean up defrag sysfs with shared data tables Breno Leitao
2026-03-20 16:05 ` [PATCH 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store() Breno Leitao
@ 2026-03-20 16:05 ` Breno Leitao
2026-04-02 8:24 ` David Hildenbrand (Arm)
` (2 more replies)
1 sibling, 3 replies; 12+ messages in thread
From: Breno Leitao @ 2026-03-20 16:05 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Lorenzo Stoakes, Zi Yan,
Baolin Wang, Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain,
Barry Song, Lance Yang
Cc: linux-mm, linux-kernel, Breno Leitao, kernel-team
Replace 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.
Signed-off-by: Breno Leitao <leitao@debian.org>
---
mm/huge_memory.c | 38 ++++++++++++++++++++++----------------
1 file changed, 22 insertions(+), 16 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 4843e2154038f..eaa6623fa49e2 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -447,24 +447,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,
--
2.52.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store()
2026-03-20 16:05 ` [PATCH 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store() Breno Leitao
@ 2026-04-02 8:21 ` David Hildenbrand (Arm)
2026-04-02 13:42 ` Breno Leitao
2026-04-03 5:09 ` Lance Yang
2026-04-03 6:46 ` Barry Song
2 siblings, 1 reply; 12+ messages in thread
From: David Hildenbrand (Arm) @ 2026-04-02 8:21 UTC (permalink / raw)
To: Breno Leitao, Andrew Morton, Lorenzo Stoakes, Zi Yan, Baolin Wang,
Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
Lance Yang
Cc: linux-mm, linux-kernel, kernel-team
On 3/20/26 17:05, Breno Leitao wrote:
> 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().
>
> Signed-off-by: Breno Leitao <leitao@debian.org>
> ---
> mm/huge_memory.c | 60 ++++++++++++++++++++++++++++++++------------------------
> 1 file changed, 34 insertions(+), 26 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 3fc02913b63e3..4843e2154038f 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -421,6 +421,29 @@ ssize_t single_hugepage_flag_store(struct kobject *kobj,
> return count;
> }
>
> +enum defrag_mode {
> + DEFRAG_ALWAYS = 0,
> + DEFRAG_DEFER = 1,
> + DEFRAG_DEFER_MADVISE = 2,
> + DEFRAG_MADVISE = 3,
> + DEFRAG_NEVER = 4,
> +};
These numbers should get assigned as default (C standard) I think, and
can be dropped.
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 2/2] mm: huge_memory: refactor defrag_show() to use defrag_flags[]
2026-03-20 16:05 ` [PATCH 2/2] mm: huge_memory: refactor defrag_show() to use defrag_flags[] Breno Leitao
@ 2026-04-02 8:24 ` David Hildenbrand (Arm)
2026-04-03 5:12 ` Lance Yang
2026-04-03 6:56 ` Barry Song
2 siblings, 0 replies; 12+ messages in thread
From: David Hildenbrand (Arm) @ 2026-04-02 8:24 UTC (permalink / raw)
To: Breno Leitao, Andrew Morton, Lorenzo Stoakes, Zi Yan, Baolin Wang,
Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
Lance Yang
Cc: linux-mm, linux-kernel, kernel-team
On 3/20/26 17:05, Breno Leitao wrote:
> Replace 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.
>
> Signed-off-by: Breno Leitao <leitao@debian.org>
> ---
> mm/huge_memory.c | 38 ++++++++++++++++++++++----------------
> 1 file changed, 22 insertions(+), 16 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 4843e2154038f..eaa6623fa49e2 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -447,24 +447,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;
The old code was certainly way easier to get. But this one here looks
good to me.
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store()
2026-04-02 8:21 ` David Hildenbrand (Arm)
@ 2026-04-02 13:42 ` Breno Leitao
2026-04-02 13:45 ` David Hildenbrand (Arm)
0 siblings, 1 reply; 12+ messages in thread
From: Breno Leitao @ 2026-04-02 13:42 UTC (permalink / raw)
To: David Hildenbrand (Arm)
Cc: Andrew Morton, Lorenzo Stoakes, Zi Yan, Baolin Wang,
Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
Lance Yang, linux-mm, linux-kernel, kernel-team
On Thu, Apr 02, 2026 at 10:21:30AM +0200, David Hildenbrand (Arm) wrote:
> On 3/20/26 17:05, Breno Leitao wrote:
> > +enum defrag_mode {
> > + DEFRAG_ALWAYS = 0,
> > + DEFRAG_DEFER = 1,
> > + DEFRAG_DEFER_MADVISE = 2,
> > + DEFRAG_MADVISE = 3,
> > + DEFRAG_NEVER = 4,
> > +};
>
> These numbers should get assigned as default (C standard) I think, and
> can be dropped.
You're correct that they would be assigned by default. However, I've
explicitly set them to clarify that we iterate over these enum values
using an integer and require these specific values.
This was discussed in the previous patch series:
https://lore.kernel.org/all/20260308140530.9ab6d1445d0936467eab4aef@linux-foundation.org/
Please let me know if it should be removed here, and I will update the
patch,
Thanks for the review,
--breno
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store()
2026-04-02 13:42 ` Breno Leitao
@ 2026-04-02 13:45 ` David Hildenbrand (Arm)
2026-04-02 14:37 ` Breno Leitao
0 siblings, 1 reply; 12+ messages in thread
From: David Hildenbrand (Arm) @ 2026-04-02 13:45 UTC (permalink / raw)
To: Breno Leitao
Cc: Andrew Morton, Lorenzo Stoakes, Zi Yan, Baolin Wang,
Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
Lance Yang, linux-mm, linux-kernel, kernel-team
On 4/2/26 15:42, Breno Leitao wrote:
> On Thu, Apr 02, 2026 at 10:21:30AM +0200, David Hildenbrand (Arm) wrote:
>> On 3/20/26 17:05, Breno Leitao wrote:
>>> +enum defrag_mode {
>>> + DEFRAG_ALWAYS = 0,
>>> + DEFRAG_DEFER = 1,
>>> + DEFRAG_DEFER_MADVISE = 2,
>>> + DEFRAG_MADVISE = 3,
>>> + DEFRAG_NEVER = 4,
>>> +};
>>
>> These numbers should get assigned as default (C standard) I think, and
>> can be dropped.
>
> You're correct that they would be assigned by default. However, I've
> explicitly set them to clarify that we iterate over these enum values
> using an integer and require these specific values.
>
> This was discussed in the previous patch series:
>
> https://lore.kernel.org/all/20260308140530.9ab6d1445d0936467eab4aef@linux-foundation.org/
>
> Please let me know if it should be removed here, and I will update the
> patch,
It's ok to leave it. It's just ... unnecessary :)
What I saw in the past is that we highlight it by only setting the "= 0"
on the first entry.
--
Cheers,
David
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store()
2026-04-02 13:45 ` David Hildenbrand (Arm)
@ 2026-04-02 14:37 ` Breno Leitao
0 siblings, 0 replies; 12+ messages in thread
From: Breno Leitao @ 2026-04-02 14:37 UTC (permalink / raw)
To: David Hildenbrand (Arm)
Cc: Andrew Morton, Lorenzo Stoakes, Zi Yan, Baolin Wang,
Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
Lance Yang, linux-mm, linux-kernel, kernel-team
On Thu, Apr 02, 2026 at 03:45:25PM +0200, David Hildenbrand (Arm) wrote:
> On 4/2/26 15:42, Breno Leitao wrote:
> > On Thu, Apr 02, 2026 at 10:21:30AM +0200, David Hildenbrand (Arm) wrote:
> >> On 3/20/26 17:05, Breno Leitao wrote:
> >>> +enum defrag_mode {
> >>> + DEFRAG_ALWAYS = 0,
> >>> + DEFRAG_DEFER = 1,
> >>> + DEFRAG_DEFER_MADVISE = 2,
> >>> + DEFRAG_MADVISE = 3,
> >>> + DEFRAG_NEVER = 4,
> >>> +};
> >>
> >> These numbers should get assigned as default (C standard) I think, and
> >> can be dropped.
> >
> > You're correct that they would be assigned by default. However, I've
> > explicitly set them to clarify that we iterate over these enum values
> > using an integer and require these specific values.
> >
> > This was discussed in the previous patch series:
> >
> > https://lore.kernel.org/all/20260308140530.9ab6d1445d0936467eab4aef@linux-foundation.org/
> >
> > Please let me know if it should be removed here, and I will update the
> > patch,
>
> It's ok to leave it. It's just ... unnecessary :)
>
> What I saw in the past is that we highlight it by only setting the "= 0"
> on the first entry.
Understood. You're right that I was overly verbose here.
Thanks for the review,
--breno
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store()
2026-03-20 16:05 ` [PATCH 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store() Breno Leitao
2026-04-02 8:21 ` David Hildenbrand (Arm)
@ 2026-04-03 5:09 ` Lance Yang
2026-04-03 6:46 ` Barry Song
2 siblings, 0 replies; 12+ messages in thread
From: Lance Yang @ 2026-04-03 5:09 UTC (permalink / raw)
To: Breno Leitao
Cc: linux-mm, Nico Pache, Liam R. Howlett, Zi Yan, Ryan Roberts,
David Hildenbrand, Barry Song, linux-kernel, kernel-team,
Lorenzo Stoakes, Dev Jain, Baolin Wang, Andrew Morton
On 2026/3/21 00:05, Breno Leitao wrote:
> 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().
>
> Signed-off-by: Breno Leitao <leitao@debian.org>
> ---
Thanks.
Tested-by: Lance Yang <lance.yang@linux.dev>
With David's comments addressed, feel free to add:
Reviewed-by: Lance Yang <lance.yang@linux.dev>
Cheers,
Lance
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 2/2] mm: huge_memory: refactor defrag_show() to use defrag_flags[]
2026-03-20 16:05 ` [PATCH 2/2] mm: huge_memory: refactor defrag_show() to use defrag_flags[] Breno Leitao
2026-04-02 8:24 ` David Hildenbrand (Arm)
@ 2026-04-03 5:12 ` Lance Yang
2026-04-03 6:56 ` Barry Song
2 siblings, 0 replies; 12+ messages in thread
From: Lance Yang @ 2026-04-03 5:12 UTC (permalink / raw)
To: Breno Leitao
Cc: David Hildenbrand, Baolin Wang, Dev Jain, Ryan Roberts, linux-mm,
Andrew Morton, Nico Pache, Barry Song, Zi Yan, linux-kernel,
kernel-team, Lorenzo Stoakes, Liam R. Howlett
On 2026/3/21 00:05, Breno Leitao wrote:
> Replace 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.
>
> Signed-off-by: Breno Leitao <leitao@debian.org>
> ---
LGTM.
Tested-by: Lance Yang <lance.yang@linux.dev>
And also,
Reviewed-by: Lance Yang <lance.yang@linux.dev>
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store()
2026-03-20 16:05 ` [PATCH 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store() Breno Leitao
2026-04-02 8:21 ` David Hildenbrand (Arm)
2026-04-03 5:09 ` Lance Yang
@ 2026-04-03 6:46 ` Barry Song
2 siblings, 0 replies; 12+ messages in thread
From: Barry Song @ 2026-04-03 6:46 UTC (permalink / raw)
To: Breno Leitao
Cc: Andrew Morton, David Hildenbrand, Lorenzo Stoakes, Zi Yan,
Baolin Wang, Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain,
Lance Yang, linux-mm, linux-kernel, kernel-team
On Sat, Mar 21, 2026 at 12:06 AM Breno Leitao <leitao@debian.org> wrote:
>
> 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().
>
> Signed-off-by: Breno Leitao <leitao@debian.org>
> ---
LGTM,
Reviewed-by: Barry Song <baohua@kernel.org>
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 2/2] mm: huge_memory: refactor defrag_show() to use defrag_flags[]
2026-03-20 16:05 ` [PATCH 2/2] mm: huge_memory: refactor defrag_show() to use defrag_flags[] Breno Leitao
2026-04-02 8:24 ` David Hildenbrand (Arm)
2026-04-03 5:12 ` Lance Yang
@ 2026-04-03 6:56 ` Barry Song
2 siblings, 0 replies; 12+ messages in thread
From: Barry Song @ 2026-04-03 6:56 UTC (permalink / raw)
To: Breno Leitao
Cc: Andrew Morton, David Hildenbrand, Lorenzo Stoakes, Zi Yan,
Baolin Wang, Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain,
Lance Yang, linux-mm, linux-kernel, kernel-team
On Sat, Mar 21, 2026 at 12:06 AM Breno Leitao <leitao@debian.org> wrote:
>
> Replace 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.
>
> Signed-off-by: Breno Leitao <leitao@debian.org>
> ---
> mm/huge_memory.c | 38 ++++++++++++++++++++++----------------
> 1 file changed, 22 insertions(+), 16 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 4843e2154038f..eaa6623fa49e2 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -447,24 +447,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';
> +
Interesting. We used to use sysfs_emit(buf, "%s\n", output),
which appends a newline automatically. Now you’re replacing
the final ' ' with '\n'.
LGTM,
Reviewed-by: Barry Song <baohua@kernel.org>
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2026-04-03 6:56 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-20 16:05 [PATCH 0/2] mm: huge_memory: clean up defrag sysfs with shared data tables Breno Leitao
2026-03-20 16:05 ` [PATCH 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store() Breno Leitao
2026-04-02 8:21 ` David Hildenbrand (Arm)
2026-04-02 13:42 ` Breno Leitao
2026-04-02 13:45 ` David Hildenbrand (Arm)
2026-04-02 14:37 ` Breno Leitao
2026-04-03 5:09 ` Lance Yang
2026-04-03 6:46 ` Barry Song
2026-03-20 16:05 ` [PATCH 2/2] mm: huge_memory: refactor defrag_show() to use defrag_flags[] Breno Leitao
2026-04-02 8:24 ` David Hildenbrand (Arm)
2026-04-03 5:12 ` Lance Yang
2026-04-03 6:56 ` Barry Song
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox