linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RESEND 0/6] support ksm_stat showing at cgroup level
@ 2025-04-22 11:14 xu.xin16
  2025-04-22 11:19 ` [PATCH RESEND 1/6] memcontrol: rename mem_cgroup_scan_tasks() xu xin
                   ` (6 more replies)
  0 siblings, 7 replies; 12+ messages in thread
From: xu.xin16 @ 2025-04-22 11:14 UTC (permalink / raw)
  To: akpm; +Cc: david, linux-kernel, wang.yaxin, linux-mm, linux-fsdevel,
	yang.yang29

From: xu xin <xu.xin16@zte.com.cn>

With the enablement of container-level KSM (e.g., via prctl [1]), there is
a growing demand for container-level observability of KSM behavior. However,
current cgroup implementations lack support for exposing KSM-related
metrics.

This patch introduces a new interface named ksm_stat
at the cgroup hierarchy level, enabling users to monitor KSM merging
statistics specifically for containers where this feature has been
activated, eliminating the need to manually inspect KSM information for
each individual process within the cgroup.

Users can obtain the KSM information of a cgroup just by:

# cat /sys/fs/cgroup/memory.ksm_stat
ksm_rmap_items 76800
ksm_zero_pages 0
ksm_merging_pages 76800
ksm_process_profit 309657600

Current implementation supports cgroup v1 temporarily; cgroup v2
compatibility is planned for future versions.


xu xin (6):
  memcontrol: rename mem_cgroup_scan_tasks()
  memcontrol: introduce the new mem_cgroup_scan_tasks()
  memcontrol-v1: introduce ksm_stat at cgroup level
  memcontrol-v1: add ksm_zero_pages in cgroup/memory.ksm_stat
  memcontrol-v1: add ksm_merging_pages in cgroup/memory.ksm_stat
  memcontrol-v1: add ksm_profit in cgroup/memory.ksm_stat

 include/linux/memcontrol.h |  7 +++++
 mm/memcontrol-v1.c         | 55 ++++++++++++++++++++++++++++++++++++++
 mm/memcontrol.c            | 28 +++++++++++++++++--
 mm/oom_kill.c              |  6 ++---
 4 files changed, 91 insertions(+), 5 deletions(-)

-- 
2.39.3

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

* [PATCH RESEND 1/6] memcontrol: rename mem_cgroup_scan_tasks()
  2025-04-22 11:14 [PATCH RESEND 0/6] support ksm_stat showing at cgroup level xu.xin16
@ 2025-04-22 11:19 ` xu xin
  2025-04-22 23:29   ` Andrew Morton
  2025-04-22 11:20 ` [PATCH RESEND 2/6] memcontrol: introduce the new mem_cgroup_scan_tasks() xu xin
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 12+ messages in thread
From: xu xin @ 2025-04-22 11:19 UTC (permalink / raw)
  To: xu.xin16
  Cc: akpm, david, linux-fsdevel, linux-kernel, linux-mm, wang.yaxin,
	yang.yang29

Current Issue:
==============
The function mem_cgroup_scan_tasks in memcontrol.c has a naming ambiguity.
While its name suggests it only iterates through processes belonging to
the current memcgroup, it actually scans all descendant cgroups under the
subtree rooted at this memcgroup. This discrepancy can cause confusion
for developers relying on the semantic meaning of the function name.

Resolution:
=========
Renaming: We have renamed the original function to
**mem_cgroup_tree_scan_tasks** to explicitly reflect its subtree-traversal
behavior.

A subsequent patch will introduce a new mem_cgroup_scan_tasks function that
strictly iterates processes only within the current memcgroup, aligning its
behavior with its name.

Signed-off-by: xu xin <xu.xin16@zte.com.cn>
---
 include/linux/memcontrol.h | 4 ++--
 mm/memcontrol.c            | 4 ++--
 mm/oom_kill.c              | 6 +++---
 3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 5264d148bdd9..1c1ce25fae4c 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -795,7 +795,7 @@ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
 				   struct mem_cgroup *,
 				   struct mem_cgroup_reclaim_cookie *);
 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
-void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
+void mem_cgroup_tree_scan_tasks(struct mem_cgroup *memcg,
 			   int (*)(struct task_struct *, void *), void *arg);
 
 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
@@ -1289,7 +1289,7 @@ static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
 {
 }
 
-static inline void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
+static inline void mem_cgroup_tree_scan_tasks(struct mem_cgroup *memcg,
 		int (*fn)(struct task_struct *, void *), void *arg)
 {
 }
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 6bc6dade60d8..3baf0a4e0674 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -1164,7 +1164,7 @@ static void invalidate_reclaim_iterators(struct mem_cgroup *dead_memcg)
 }
 
 /**
- * mem_cgroup_scan_tasks - iterate over tasks of a memory cgroup hierarchy
+ * mem_cgroup_tree_scan_tasks - iterate over tasks of a memory cgroup hierarchy
  * @memcg: hierarchy root
  * @fn: function to call for each task
  * @arg: argument passed to @fn
@@ -1176,7 +1176,7 @@ static void invalidate_reclaim_iterators(struct mem_cgroup *dead_memcg)
  *
  * This function must not be called for the root memory cgroup.
  */
-void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
+void mem_cgroup_tree_scan_tasks(struct mem_cgroup *memcg,
 			   int (*fn)(struct task_struct *, void *), void *arg)
 {
 	struct mem_cgroup *iter;
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 25923cfec9c6..af3b8407fb08 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -367,7 +367,7 @@ static void select_bad_process(struct oom_control *oc)
 	oc->chosen_points = LONG_MIN;
 
 	if (is_memcg_oom(oc))
-		mem_cgroup_scan_tasks(oc->memcg, oom_evaluate_task, oc);
+		mem_cgroup_tree_scan_tasks(oc->memcg, oom_evaluate_task, oc);
 	else {
 		struct task_struct *p;
 
@@ -428,7 +428,7 @@ static void dump_tasks(struct oom_control *oc)
 	pr_info("[  pid  ]   uid  tgid total_vm      rss rss_anon rss_file rss_shmem pgtables_bytes swapents oom_score_adj name\n");
 
 	if (is_memcg_oom(oc))
-		mem_cgroup_scan_tasks(oc->memcg, dump_task, oc);
+		mem_cgroup_tree_scan_tasks(oc->memcg, dump_task, oc);
 	else {
 		struct task_struct *p;
 		int i = 0;
@@ -1056,7 +1056,7 @@ static void oom_kill_process(struct oom_control *oc, const char *message)
 	if (oom_group) {
 		memcg_memory_event(oom_group, MEMCG_OOM_GROUP_KILL);
 		mem_cgroup_print_oom_group(oom_group);
-		mem_cgroup_scan_tasks(oom_group, oom_kill_memcg_member,
+		mem_cgroup_tree_scan_tasks(oom_group, oom_kill_memcg_member,
 				      (void *)message);
 		mem_cgroup_put(oom_group);
 	}
-- 
2.39.3



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

* [PATCH RESEND 2/6] memcontrol: introduce the new mem_cgroup_scan_tasks()
  2025-04-22 11:14 [PATCH RESEND 0/6] support ksm_stat showing at cgroup level xu.xin16
  2025-04-22 11:19 ` [PATCH RESEND 1/6] memcontrol: rename mem_cgroup_scan_tasks() xu xin
@ 2025-04-22 11:20 ` xu xin
  2025-04-22 11:21 ` [PATCH RESEND 3/6] memcontrol-v1: introduce ksm_stat at cgroup level xu xin
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 12+ messages in thread
From: xu xin @ 2025-04-22 11:20 UTC (permalink / raw)
  To: xu.xin16
  Cc: akpm, david, linux-fsdevel, linux-kernel, linux-mm, wang.yaxin,
	yang.yang29

Introduce a new mem_cgroup_scan_tasks function that strictly iterates
processes only within the current memcgroup, aligning its behavior with
its name.

Signed-off-by: xu xin <xu.xin16@zte.com.cn>
---
 include/linux/memcontrol.h |  7 +++++++
 mm/memcontrol.c            | 24 ++++++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 1c1ce25fae4c..f9d663a7ccde 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -795,6 +795,8 @@ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
 				   struct mem_cgroup *,
 				   struct mem_cgroup_reclaim_cookie *);
 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
+void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
+			   int (*)(struct task_struct *, void *), void *arg);
 void mem_cgroup_tree_scan_tasks(struct mem_cgroup *memcg,
 			   int (*)(struct task_struct *, void *), void *arg);
 
@@ -1289,6 +1291,11 @@ static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
 {
 }
 
+static inline void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
+		int (*fn)(struct task_struct *, void *), void *arg)
+{
+}
+
 static inline void mem_cgroup_tree_scan_tasks(struct mem_cgroup *memcg,
 		int (*fn)(struct task_struct *, void *), void *arg)
 {
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 3baf0a4e0674..629e2ce2d830 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -1163,6 +1163,30 @@ static void invalidate_reclaim_iterators(struct mem_cgroup *dead_memcg)
 						dead_memcg);
 }
 
+/* *
+ * mem_cgroup_scan_tasks - iterate over tasks of only this memory cgroup.
+ * @memcg: the specified memory cgroup.
+ * @fn: function to call for each task
+ * @arg: argument passed to @fn
+ *
+ * Unlike mem_cgroup_tree_scan_tasks(), this function only iterate over
+ * these tasks attached to @memcg, not including any of its descendants
+ * memcg. And this could be called for the root memory cgroup.
+ */
+void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
+			  int (*fn)(struct task_struct *, void *), void *arg)
+{
+	int ret = 0;
+	struct css_task_iter it;
+	struct task_struct *task;
+
+	css_task_iter_start(&memcg->css, CSS_TASK_ITER_PROCS, &it);
+	while (!ret && (task = css_task_iter_next(&it)))
+		ret = fn(task, arg);
+
+	css_task_iter_end(&it);
+}
+
 /**
  * mem_cgroup_tree_scan_tasks - iterate over tasks of a memory cgroup hierarchy
  * @memcg: hierarchy root
-- 
2.39.3



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

* [PATCH RESEND 3/6] memcontrol-v1: introduce ksm_stat at cgroup level
  2025-04-22 11:14 [PATCH RESEND 0/6] support ksm_stat showing at cgroup level xu.xin16
  2025-04-22 11:19 ` [PATCH RESEND 1/6] memcontrol: rename mem_cgroup_scan_tasks() xu xin
  2025-04-22 11:20 ` [PATCH RESEND 2/6] memcontrol: introduce the new mem_cgroup_scan_tasks() xu xin
@ 2025-04-22 11:21 ` xu xin
  2025-04-22 11:21 ` [PATCH RESEND 4/6] memcontrol-v1: add ksm_zero_pages in cgroup/memory.ksm_stat xu xin
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 12+ messages in thread
From: xu xin @ 2025-04-22 11:21 UTC (permalink / raw)
  To: xu.xin16
  Cc: akpm, david, linux-fsdevel, linux-kernel, linux-mm, wang.yaxin,
	yang.yang29, Haonan Chen

With the enablement of container-level KSM (e.g., via prctl), there is a
growing demand for container-level observability of KSM behavior. However,
current cgroup implementations lack support for exposing KSM-related
metrics.

This patch introduces a new interface named ksm_stat
at the cgroup hierarchy level, enabling users to monitor KSM merging
statistics specifically for containers where this feature has been
activated, eliminating the need to manually inspect KSM information for
each individual process within the cgroup.

Users can obtain the KSM information of a cgroup just by:

        `cat /sys/fs/cgroup/memory.ksm_stat`

Current implementation supports cgroup v1 temporarily; cgroup v2
compatibility is planned for future versions.

Co-developed-by: Haonan Chen <chen.haonan2@zte.com.cn>
Signed-off-by: Haonan Chen <chen.haonan2@zte.com.cn>
Signed-off-by: xu xin <xu.xin16@zte.com.cn>
---
 mm/memcontrol-v1.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index 4a9cf27a70af..fa57a5deb28c 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -1821,6 +1821,40 @@ static int memcg_numa_stat_show(struct seq_file *m, void *v)
 }
 #endif /* CONFIG_NUMA */
 
+#ifdef CONFIG_KSM
+struct memcg_ksm_stat {
+	unsigned long ksm_rmap_items;
+};
+
+static int evaluate_memcg_ksm_stat(struct task_struct *task, void *arg)
+{
+	struct mm_struct *mm;
+	struct memcg_ksm_stat *ksm_stat = arg;
+
+	mm = get_task_mm(task);
+	if (mm) {
+		ksm_stat->ksm_rmap_items += mm->ksm_rmap_items;
+		mmput(mm);
+	}
+
+	return 0;
+}
+
+static int memcg_ksm_stat_show(struct seq_file *m, void *v)
+{
+	struct memcg_ksm_stat ksm_stat;
+	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
+
+	/* Initialization */
+	ksm_stat.ksm_rmap_items = 0;
+	/* summing all processes'ksm statistic items of this cgroup hierarchy */
+	mem_cgroup_scan_tasks(memcg, evaluate_memcg_ksm_stat, &ksm_stat);
+	seq_printf(m, "ksm_rmap_items %lu\n", ksm_stat.ksm_rmap_items);
+
+	return 0;
+}
+#endif
+
 static const unsigned int memcg1_stats[] = {
 	NR_FILE_PAGES,
 	NR_ANON_MAPPED,
@@ -2079,6 +2113,12 @@ struct cftype mem_cgroup_legacy_files[] = {
 		.name = "numa_stat",
 		.seq_show = memcg_numa_stat_show,
 	},
+#endif
+#ifdef CONFIG_KSM
+	{
+		.name = "ksm_stat",
+		.seq_show = memcg_ksm_stat_show,
+	},
 #endif
 	{
 		.name = "kmem.limit_in_bytes",
-- 
2.39.3



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

* [PATCH RESEND 4/6] memcontrol-v1: add ksm_zero_pages in cgroup/memory.ksm_stat
  2025-04-22 11:14 [PATCH RESEND 0/6] support ksm_stat showing at cgroup level xu.xin16
                   ` (2 preceding siblings ...)
  2025-04-22 11:21 ` [PATCH RESEND 3/6] memcontrol-v1: introduce ksm_stat at cgroup level xu xin
@ 2025-04-22 11:21 ` xu xin
  2025-04-22 11:22 ` [PATCH RESEND 5/6] memcontrol-v1: add ksm_merging_pages " xu xin
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 12+ messages in thread
From: xu xin @ 2025-04-22 11:21 UTC (permalink / raw)
  To: xu.xin16
  Cc: akpm, david, linux-fsdevel, linux-kernel, linux-mm, wang.yaxin,
	yang.yang29

Users can obtain ksm_zero_pages of a cgroup just by:

/ # cat /sys/fs/cgroup/memory.ksm_stat
ksm_rmap_items 76800
ksm_zero_pages 0

Current implementation supports cgroup v1 temporarily; cgroup v2
compatibility is planned for future versions.

Signed-off-by: xu xin <xu.xin16@zte.com.cn>
---
 mm/memcontrol-v1.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index fa57a5deb28c..9680749f4eef 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -11,6 +11,7 @@
 #include <linux/sort.h>
 #include <linux/file.h>
 #include <linux/seq_buf.h>
+#include <linux/ksm.h>
 
 #include "internal.h"
 #include "swap.h"
@@ -1824,6 +1825,7 @@ static int memcg_numa_stat_show(struct seq_file *m, void *v)
 #ifdef CONFIG_KSM
 struct memcg_ksm_stat {
 	unsigned long ksm_rmap_items;
+	long ksm_zero_pages;
 };
 
 static int evaluate_memcg_ksm_stat(struct task_struct *task, void *arg)
@@ -1834,6 +1836,7 @@ static int evaluate_memcg_ksm_stat(struct task_struct *task, void *arg)
 	mm = get_task_mm(task);
 	if (mm) {
 		ksm_stat->ksm_rmap_items += mm->ksm_rmap_items;
+		ksm_stat->ksm_zero_pages += mm_ksm_zero_pages(mm);
 		mmput(mm);
 	}
 
@@ -1847,9 +1850,13 @@ static int memcg_ksm_stat_show(struct seq_file *m, void *v)
 
 	/* Initialization */
 	ksm_stat.ksm_rmap_items = 0;
+	ksm_stat.ksm_zero_pages = 0;
+
 	/* summing all processes'ksm statistic items of this cgroup hierarchy */
 	mem_cgroup_scan_tasks(memcg, evaluate_memcg_ksm_stat, &ksm_stat);
+
 	seq_printf(m, "ksm_rmap_items %lu\n", ksm_stat.ksm_rmap_items);
+	seq_printf(m, "ksm_zero_pages %ld\n", ksm_stat.ksm_zero_pages);
 
 	return 0;
 }
-- 
2.39.3



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

* [PATCH RESEND 5/6] memcontrol-v1: add ksm_merging_pages in cgroup/memory.ksm_stat
  2025-04-22 11:14 [PATCH RESEND 0/6] support ksm_stat showing at cgroup level xu.xin16
                   ` (3 preceding siblings ...)
  2025-04-22 11:21 ` [PATCH RESEND 4/6] memcontrol-v1: add ksm_zero_pages in cgroup/memory.ksm_stat xu xin
@ 2025-04-22 11:22 ` xu xin
  2025-04-22 11:22 ` [PATCH RESEND 6/6] memcontrol-v1: add ksm_profit " xu xin
  2025-04-23 13:08 ` [PATCH RESEND 0/6] support ksm_stat showing at cgroup level David Hildenbrand
  6 siblings, 0 replies; 12+ messages in thread
From: xu xin @ 2025-04-22 11:22 UTC (permalink / raw)
  To: xu.xin16
  Cc: akpm, david, linux-fsdevel, linux-kernel, linux-mm, wang.yaxin,
	yang.yang29

Users can obtain ksm_merging_pages of a cgroup just by:

/ # cat /sys/fs/cgroup/memory.ksm_stat
ksm_rmap_items 76800
ksm_zero_pages 0
ksm_merging_pages 1092

Current implementation supports cgroup v1 temporarily; cgroup v2
compatibility is planned for future versions.

Signed-off-by: xu xin <xu.xin16@zte.com.cn>
---
 mm/memcontrol-v1.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index 9680749f4eef..7ee38d633d85 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -1826,6 +1826,7 @@ static int memcg_numa_stat_show(struct seq_file *m, void *v)
 struct memcg_ksm_stat {
 	unsigned long ksm_rmap_items;
 	long ksm_zero_pages;
+	unsigned long ksm_merging_pages;
 };
 
 static int evaluate_memcg_ksm_stat(struct task_struct *task, void *arg)
@@ -1837,6 +1838,7 @@ static int evaluate_memcg_ksm_stat(struct task_struct *task, void *arg)
 	if (mm) {
 		ksm_stat->ksm_rmap_items += mm->ksm_rmap_items;
 		ksm_stat->ksm_zero_pages += mm_ksm_zero_pages(mm);
+		ksm_stat->ksm_merging_pages += mm->ksm_merging_pages;
 		mmput(mm);
 	}
 
@@ -1851,12 +1853,14 @@ static int memcg_ksm_stat_show(struct seq_file *m, void *v)
 	/* Initialization */
 	ksm_stat.ksm_rmap_items = 0;
 	ksm_stat.ksm_zero_pages = 0;
+	ksm_stat.ksm_merging_pages = 0;
 
 	/* summing all processes'ksm statistic items of this cgroup hierarchy */
 	mem_cgroup_scan_tasks(memcg, evaluate_memcg_ksm_stat, &ksm_stat);
 
 	seq_printf(m, "ksm_rmap_items %lu\n", ksm_stat.ksm_rmap_items);
 	seq_printf(m, "ksm_zero_pages %ld\n", ksm_stat.ksm_zero_pages);
+	seq_printf(m, "ksm_merging_pages %ld\n", ksm_stat.ksm_merging_pages);
 
 	return 0;
 }
-- 
2.39.3



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

* [PATCH RESEND 6/6] memcontrol-v1: add ksm_profit in cgroup/memory.ksm_stat
  2025-04-22 11:14 [PATCH RESEND 0/6] support ksm_stat showing at cgroup level xu.xin16
                   ` (4 preceding siblings ...)
  2025-04-22 11:22 ` [PATCH RESEND 5/6] memcontrol-v1: add ksm_merging_pages " xu xin
@ 2025-04-22 11:22 ` xu xin
  2025-04-23  8:21   ` kernel test robot
  2025-04-23 13:08 ` [PATCH RESEND 0/6] support ksm_stat showing at cgroup level David Hildenbrand
  6 siblings, 1 reply; 12+ messages in thread
From: xu xin @ 2025-04-22 11:22 UTC (permalink / raw)
  To: xu.xin16
  Cc: akpm, david, linux-fsdevel, linux-kernel, linux-mm, wang.yaxin,
	yang.yang29

Users can obtain ksm_profit of a cgroup just by:

/ # cat /sys/fs/cgroup/memory.ksm_stat
ksm_rmap_items 76800
ksm_zero_pages 0
ksm_merging_pages 76800
ksm_profit 309657600

Current implementation supports cgroup v1 temporarily; cgroup v2
compatibility is planned for future versions.

Signed-off-by: xu xin <xu.xin16@zte.com.cn>
---
 mm/memcontrol-v1.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index 7ee38d633d85..2cf2823c5514 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -1827,6 +1827,7 @@ struct memcg_ksm_stat {
 	unsigned long ksm_rmap_items;
 	long ksm_zero_pages;
 	unsigned long ksm_merging_pages;
+	long ksm_profit;
 };
 
 static int evaluate_memcg_ksm_stat(struct task_struct *task, void *arg)
@@ -1839,6 +1840,7 @@ static int evaluate_memcg_ksm_stat(struct task_struct *task, void *arg)
 		ksm_stat->ksm_rmap_items += mm->ksm_rmap_items;
 		ksm_stat->ksm_zero_pages += mm_ksm_zero_pages(mm);
 		ksm_stat->ksm_merging_pages += mm->ksm_merging_pages;
+		ksm_stat->ksm_profit += ksm_process_profit(mm);
 		mmput(mm);
 	}
 
@@ -1854,6 +1856,7 @@ static int memcg_ksm_stat_show(struct seq_file *m, void *v)
 	ksm_stat.ksm_rmap_items = 0;
 	ksm_stat.ksm_zero_pages = 0;
 	ksm_stat.ksm_merging_pages = 0;
+	ksm_stat.ksm_profit = 0;
 
 	/* summing all processes'ksm statistic items of this cgroup hierarchy */
 	mem_cgroup_scan_tasks(memcg, evaluate_memcg_ksm_stat, &ksm_stat);
@@ -1861,6 +1864,7 @@ static int memcg_ksm_stat_show(struct seq_file *m, void *v)
 	seq_printf(m, "ksm_rmap_items %lu\n", ksm_stat.ksm_rmap_items);
 	seq_printf(m, "ksm_zero_pages %ld\n", ksm_stat.ksm_zero_pages);
 	seq_printf(m, "ksm_merging_pages %ld\n", ksm_stat.ksm_merging_pages);
+	seq_printf(m, "ksm_profit %ld\n", ksm_stat.ksm_profit);
 
 	return 0;
 }
-- 
2.39.3



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

* Re: [PATCH RESEND 1/6] memcontrol: rename mem_cgroup_scan_tasks()
  2025-04-22 11:19 ` [PATCH RESEND 1/6] memcontrol: rename mem_cgroup_scan_tasks() xu xin
@ 2025-04-22 23:29   ` Andrew Morton
  2025-04-23 12:22     ` Matthew Wilcox
  0 siblings, 1 reply; 12+ messages in thread
From: Andrew Morton @ 2025-04-22 23:29 UTC (permalink / raw)
  To: xu xin
  Cc: xu.xin16, david, linux-fsdevel, linux-kernel, linux-mm,
	wang.yaxin, yang.yang29

On Tue, 22 Apr 2025 11:19:19 +0000 xu xin <xu.xin.sc@gmail.com> wrote:

> From: xu xin <xu.xin.sc@gmail.com>
> To: xu.xin16@zte.com.cn
> Cc: akpm@linux-foundation.org, david@redhat.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, wang.yaxin@zte.com.cn, yang.yang29@zte.com.cn
> Subject: [PATCH RESEND 1/6] memcontrol: rename mem_cgroup_scan_tasks()
> Date: Tue, 22 Apr 2025 11:19:19 +0000
> 
> ...
>
> Signed-off-by: xu xin <xu.xin16@zte.com.cn>

It's clear what was meant, but please include the explicit

	From: xu xin <xu.xin16@zte.com.cn>

within each changelog, rather than only in the [0/N].

Patchset looks nice to me, thanks.  I'll await reviewer feedback before
proceeding.


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

* Re: [PATCH RESEND 6/6] memcontrol-v1: add ksm_profit in cgroup/memory.ksm_stat
  2025-04-22 11:22 ` [PATCH RESEND 6/6] memcontrol-v1: add ksm_profit " xu xin
@ 2025-04-23  8:21   ` kernel test robot
  0 siblings, 0 replies; 12+ messages in thread
From: kernel test robot @ 2025-04-23  8:21 UTC (permalink / raw)
  To: xu xin, xu.xin16
  Cc: llvm, oe-kbuild-all, akpm, david, linux-fsdevel, linux-kernel,
	linux-mm, wang.yaxin, yang.yang29

Hi xu,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on linus/master v6.15-rc3 next-20250422]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/xu-xin/memcontrol-rename-mem_cgroup_scan_tasks/20250422-231623
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250422112251.3231599-1-xu.xin16%40zte.com.cn
patch subject: [PATCH RESEND 6/6] memcontrol-v1: add ksm_profit in cgroup/memory.ksm_stat
config: i386-buildonly-randconfig-006-20250423 (https://download.01.org/0day-ci/archive/20250423/202504231523.owjrO2Yy-lkp@intel.com/config)
compiler: clang version 20.1.2 (https://github.com/llvm/llvm-project 58df0ef89dd64126512e4ee27b4ac3fd8ddf6247)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250423/202504231523.owjrO2Yy-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202504231523.owjrO2Yy-lkp@intel.com/

All errors (new ones prefixed by >>):

>> ld.lld: error: undefined symbol: ksm_process_profit
   >>> referenced by memcontrol-v1.c:1843 (mm/memcontrol-v1.c:1843)
   >>>               mm/memcontrol-v1.o:(evaluate_memcg_ksm_stat) in archive vmlinux.a

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

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

* Re: [PATCH RESEND 1/6] memcontrol: rename mem_cgroup_scan_tasks()
  2025-04-22 23:29   ` Andrew Morton
@ 2025-04-23 12:22     ` Matthew Wilcox
  2025-04-25 14:30       ` Michal Hocko
  0 siblings, 1 reply; 12+ messages in thread
From: Matthew Wilcox @ 2025-04-23 12:22 UTC (permalink / raw)
  To: Andrew Morton
  Cc: xu xin, xu.xin16, david, linux-fsdevel, linux-kernel, linux-mm,
	wang.yaxin, yang.yang29

On Tue, Apr 22, 2025 at 04:29:52PM -0700, Andrew Morton wrote:
> Patchset looks nice to me, thanks.  I'll await reviewer feedback before
> proceeding.

I thought we had a policy against adding new features to memcg-v1?
Certainly adding the feature to memcg-v2 would be a requirement before
it could be added to v1.

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

* Re: [PATCH RESEND 0/6] support ksm_stat showing at cgroup level
  2025-04-22 11:14 [PATCH RESEND 0/6] support ksm_stat showing at cgroup level xu.xin16
                   ` (5 preceding siblings ...)
  2025-04-22 11:22 ` [PATCH RESEND 6/6] memcontrol-v1: add ksm_profit " xu xin
@ 2025-04-23 13:08 ` David Hildenbrand
  6 siblings, 0 replies; 12+ messages in thread
From: David Hildenbrand @ 2025-04-23 13:08 UTC (permalink / raw)
  To: xu.xin16, akpm
  Cc: linux-kernel, wang.yaxin, linux-mm, linux-fsdevel, yang.yang29

On 22.04.25 13:14, xu.xin16@zte.com.cn wrote:
> From: xu xin <xu.xin16@zte.com.cn>
> 
> With the enablement of container-level KSM (e.g., via prctl [1]), there is
> a growing demand for container-level observability of KSM behavior. However,
> current cgroup implementations lack support for exposing KSM-related
> metrics.
> 
> This patch introduces a new interface named ksm_stat
> at the cgroup hierarchy level, enabling users to monitor KSM merging
> statistics specifically for containers where this feature has been
> activated, eliminating the need to manually inspect KSM information for
> each individual process within the cgroup.
> 
> Users can obtain the KSM information of a cgroup just by:
> 
> # cat /sys/fs/cgroup/memory.ksm_stat
> ksm_rmap_items 76800
> ksm_zero_pages 0
> ksm_merging_pages 76800
> ksm_process_profit 309657600
> 
> Current implementation supports cgroup v1 temporarily; cgroup v2
> compatibility is planned for future versions.

As raised by Willy, we focus on v2.

Independent of that, I strongly assume that 
Documentation/admin-guide/cgroup-v1/memory.rst needs care :)

-- 
Cheers,

David / dhildenb


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

* Re: [PATCH RESEND 1/6] memcontrol: rename mem_cgroup_scan_tasks()
  2025-04-23 12:22     ` Matthew Wilcox
@ 2025-04-25 14:30       ` Michal Hocko
  0 siblings, 0 replies; 12+ messages in thread
From: Michal Hocko @ 2025-04-25 14:30 UTC (permalink / raw)
  To: xu xin
  Cc: Andrew Morton, Matthew Wilcox, xu.xin16, david, linux-fsdevel,
	linux-kernel, linux-mm, wang.yaxin, yang.yang29

On Wed 23-04-25 13:22:07, Matthew Wilcox wrote:
> On Tue, Apr 22, 2025 at 04:29:52PM -0700, Andrew Morton wrote:
> > Patchset looks nice to me, thanks.  I'll await reviewer feedback before
> > proceeding.
> 
> I thought we had a policy against adding new features to memcg-v1?
> Certainly adding the feature to memcg-v2 would be a requirement before
> it could be added to v1.

Completely agreed! Is there any reason why v2 was not the first choice?
-- 
Michal Hocko
SUSE Labs

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

end of thread, other threads:[~2025-04-25 14:30 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-22 11:14 [PATCH RESEND 0/6] support ksm_stat showing at cgroup level xu.xin16
2025-04-22 11:19 ` [PATCH RESEND 1/6] memcontrol: rename mem_cgroup_scan_tasks() xu xin
2025-04-22 23:29   ` Andrew Morton
2025-04-23 12:22     ` Matthew Wilcox
2025-04-25 14:30       ` Michal Hocko
2025-04-22 11:20 ` [PATCH RESEND 2/6] memcontrol: introduce the new mem_cgroup_scan_tasks() xu xin
2025-04-22 11:21 ` [PATCH RESEND 3/6] memcontrol-v1: introduce ksm_stat at cgroup level xu xin
2025-04-22 11:21 ` [PATCH RESEND 4/6] memcontrol-v1: add ksm_zero_pages in cgroup/memory.ksm_stat xu xin
2025-04-22 11:22 ` [PATCH RESEND 5/6] memcontrol-v1: add ksm_merging_pages " xu xin
2025-04-22 11:22 ` [PATCH RESEND 6/6] memcontrol-v1: add ksm_profit " xu xin
2025-04-23  8:21   ` kernel test robot
2025-04-23 13:08 ` [PATCH RESEND 0/6] support ksm_stat showing at cgroup level David Hildenbrand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).