linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] mm: huge_memory: clean up defrag sysfs with shared data tables
@ 2026-04-08 15:46 Breno Leitao
  2026-04-08 15:47 ` [PATCH v2 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store() Breno Leitao
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Breno Leitao @ 2026-04-08 15:46 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>
---
Changes in v2:
- Do not be too verbose on enum defrag_mode (David Hildenbrand)
- Link to v1: https://patch.msgid.link/20260320-thp_defrag-v1-0-6ab15d0d26eb@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: f3e6330d7fe42b204af05a2dbc68b379e0ad179e
change-id: 20260320-thp_defrag-540fc4f1fa3d

Best regards,
--  
Breno Leitao <leitao@debian.org>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store()
  2026-04-08 15:46 [PATCH v2 0/2] mm: huge_memory: clean up defrag sysfs with shared data tables Breno Leitao
@ 2026-04-08 15:47 ` Breno Leitao
  2026-04-08 15:47 ` [PATCH v2 2/2] mm: huge_memory: refactor defrag_show() to use defrag_flags[] Breno Leitao
  2026-04-08 16:04 ` [PATCH v2 0/2] mm: huge_memory: clean up defrag sysfs with shared data tables Zi Yan
  2 siblings, 0 replies; 4+ messages in thread
From: Breno Leitao @ 2026-04-08 15:47 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().

Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Tested-by: Lance Yang <lance.yang@linux.dev>
Reviewed-by: Lance Yang <lance.yang@linux.dev>
Reviewed-by: Barry Song <baohua@kernel.org>
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 970e077019b75..76f2d32428c7f 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -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



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] mm: huge_memory: refactor defrag_show() to use defrag_flags[]
  2026-04-08 15:46 [PATCH v2 0/2] mm: huge_memory: clean up defrag sysfs with shared data tables Breno Leitao
  2026-04-08 15:47 ` [PATCH v2 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store() Breno Leitao
@ 2026-04-08 15:47 ` Breno Leitao
  2026-04-08 16:04 ` [PATCH v2 0/2] mm: huge_memory: clean up defrag sysfs with shared data tables Zi Yan
  2 siblings, 0 replies; 4+ messages in thread
From: Breno Leitao @ 2026-04-08 15:47 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.

Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Tested-by: Lance Yang <lance.yang@linux.dev>
Reviewed-by: Lance Yang <lance.yang@linux.dev>
Reviewed-by: Barry Song <baohua@kernel.org>
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 76f2d32428c7f..6993c6d8709dc 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -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,

-- 
2.52.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 0/2] mm: huge_memory: clean up defrag sysfs with shared data tables
  2026-04-08 15:46 [PATCH v2 0/2] mm: huge_memory: clean up defrag sysfs with shared data tables Breno Leitao
  2026-04-08 15:47 ` [PATCH v2 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store() Breno Leitao
  2026-04-08 15:47 ` [PATCH v2 2/2] mm: huge_memory: refactor defrag_show() to use defrag_flags[] Breno Leitao
@ 2026-04-08 16:04 ` Zi Yan
  2 siblings, 0 replies; 4+ messages in thread
From: Zi Yan @ 2026-04-08 16:04 UTC (permalink / raw)
  To: Breno Leitao
  Cc: Andrew Morton, David Hildenbrand, Lorenzo Stoakes, Baolin Wang,
	Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
	Lance Yang, linux-mm, linux-kernel, kernel-team

On 8 Apr 2026, at 11:46, Breno Leitao wrote:

> 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>
> ---
> Changes in v2:
> - Do not be too verbose on enum defrag_mode (David Hildenbrand)
> - Link to v1: https://patch.msgid.link/20260320-thp_defrag-v1-0-6ab15d0d26eb@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: f3e6330d7fe42b204af05a2dbc68b379e0ad179e
> change-id: 20260320-thp_defrag-540fc4f1fa3d

For the whole series,

Tested-by: Zi Yan <ziy@nvidia.com>
Acked-by: Zi Yan <ziy@nvidia.com>

Thanks.

Best Regards,
Yan, Zi


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-04-08 16:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-04-08 15:46 [PATCH v2 0/2] mm: huge_memory: clean up defrag sysfs with shared data tables Breno Leitao
2026-04-08 15:47 ` [PATCH v2 1/2] mm: huge_memory: use sysfs_match_string() in defrag_store() Breno Leitao
2026-04-08 15:47 ` [PATCH v2 2/2] mm: huge_memory: refactor defrag_show() to use defrag_flags[] Breno Leitao
2026-04-08 16:04 ` [PATCH v2 0/2] mm: huge_memory: clean up defrag sysfs with shared data tables Zi Yan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox