linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH linux-next 0/6] support ksm_stat showing at cgroup
@ 2025-04-03  5:25 xu.xin16
  2025-04-03  5:27 ` [PATCH linux-next 1/6] memcontrol: rename mem_cgroup_scan_tasks() xu.xin16
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: xu.xin16 @ 2025-04-03  5:25 UTC (permalink / raw)
  To: akpm, mhocko
  Cc: hannes, roman.gushchin, muchun.song, shakeel.butt, cgroups,
	linux-mm, linux-kernel, xu.xin16, yang.yang29, chen.haonan2,
	wang.yaxin

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.

---
[1] commit d7597f59d1d3 ("mm: add new api to enable ksm per process").

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.15.2


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

* [PATCH linux-next 1/6] memcontrol: rename mem_cgroup_scan_tasks()  
  2025-04-03  5:25 [PATCH linux-next 0/6] support ksm_stat showing at cgroup xu.xin16
@ 2025-04-03  5:27 ` xu.xin16
  2025-04-03  5:30 ` [PATCH linux-next 2/6] memcontrol: introduce the new mem_cgroup_scan_tasks() xu.xin16
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: xu.xin16 @ 2025-04-03  5:27 UTC (permalink / raw)
  To: xu.xin16
  Cc: akpm, mhocko, hannes, roman.gushchin, muchun.song, shakeel.butt,
	cgroups, linux-mm, linux-kernel, yang.yang29, chen.haonan2,
	wang.yaxin

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

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 53364526d877..232cea80e71f 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 421740f1bcdc..9af30fbfe819 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -1151,7 +1151,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
@@ -1163,7 +1163,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.15.2


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

* [PATCH linux-next 2/6] memcontrol: introduce the new   mem_cgroup_scan_tasks()
  2025-04-03  5:25 [PATCH linux-next 0/6] support ksm_stat showing at cgroup xu.xin16
  2025-04-03  5:27 ` [PATCH linux-next 1/6] memcontrol: rename mem_cgroup_scan_tasks() xu.xin16
@ 2025-04-03  5:30 ` xu.xin16
  2025-04-03  5:31 ` [PATCH linux-next 3/6] memcontrol-v1: introduce ksm_stat at cgroup level xu.xin16
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: xu.xin16 @ 2025-04-03  5:30 UTC (permalink / raw)
  To: akpm
  Cc: mhocko, hannes, roman.gushchin, muchun.song, shakeel.butt,
	cgroups, linux-mm, linux-kernel, yang.yang29, chen.haonan2,
	wang.yaxin, xu.xin16

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

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 232cea80e71f..3af34e124ce6 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 9af30fbfe819..aff5a095b9e4 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -1150,6 +1150,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.15.


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

* [PATCH linux-next 3/6] memcontrol-v1: introduce ksm_stat at cgroup   level
  2025-04-03  5:25 [PATCH linux-next 0/6] support ksm_stat showing at cgroup xu.xin16
  2025-04-03  5:27 ` [PATCH linux-next 1/6] memcontrol: rename mem_cgroup_scan_tasks() xu.xin16
  2025-04-03  5:30 ` [PATCH linux-next 2/6] memcontrol: introduce the new mem_cgroup_scan_tasks() xu.xin16
@ 2025-04-03  5:31 ` xu.xin16
  2025-04-03  5:37 ` [PATCH linux-next 4/6] memcontrol-v1: add ksm_zero_pages in cgroup/memory.ksm_stat xu.xin16
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: xu.xin16 @ 2025-04-03  5:31 UTC (permalink / raw)
  To: akpm, mhocko
  Cc: hannes, roman.gushchin, muchun.song, shakeel.butt, cgroups,
	linux-mm, linux-kernel, yang.yang29, chen.haonan2, wang.yaxin,
	xu.xin16

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

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 8660908850dc..95da877d5516 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.15.2


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

* [PATCH linux-next 4/6] memcontrol-v1: add ksm_zero_pages in   cgroup/memory.ksm_stat
  2025-04-03  5:25 [PATCH linux-next 0/6] support ksm_stat showing at cgroup xu.xin16
                   ` (2 preceding siblings ...)
  2025-04-03  5:31 ` [PATCH linux-next 3/6] memcontrol-v1: introduce ksm_stat at cgroup level xu.xin16
@ 2025-04-03  5:37 ` xu.xin16
  2025-04-03  5:38 ` [PATCH linux-next 5/6] memcontrol-v1: add ksm_merging_pages " xu.xin16
  2025-04-03  5:40 ` [PATCH linux-next 6/6] memcontrol-v1: add ksm_profit " xu.xin16
  5 siblings, 0 replies; 7+ messages in thread
From: xu.xin16 @ 2025-04-03  5:37 UTC (permalink / raw)
  To: akpm, mhocko
  Cc: hannes, roman.gushchin, muchun.song, shakeel.butt, cgroups,
	linux-mm, linux-kernel, yang.yang29, chen.haonan2, wang.yaxin,
	xu.xin16

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

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 95da877d5516..a6eea1deebff 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.15.2


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

* [PATCH linux-next 5/6] memcontrol-v1: add ksm_merging_pages in cgroup/memory.ksm_stat
  2025-04-03  5:25 [PATCH linux-next 0/6] support ksm_stat showing at cgroup xu.xin16
                   ` (3 preceding siblings ...)
  2025-04-03  5:37 ` [PATCH linux-next 4/6] memcontrol-v1: add ksm_zero_pages in cgroup/memory.ksm_stat xu.xin16
@ 2025-04-03  5:38 ` xu.xin16
  2025-04-03  5:40 ` [PATCH linux-next 6/6] memcontrol-v1: add ksm_profit " xu.xin16
  5 siblings, 0 replies; 7+ messages in thread
From: xu.xin16 @ 2025-04-03  5:38 UTC (permalink / raw)
  To: akpm, mhocko
  Cc: hannes, roman.gushchin, muchun.song, shakeel.butt, cgroups,
	linux-mm, linux-kernel, yang.yang29, chen.haonan2, wang.yaxin

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

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 a6eea1deebff..b52f728af963 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.15.2


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

* [PATCH linux-next 6/6] memcontrol-v1: add ksm_profit in cgroup/memory.ksm_stat
  2025-04-03  5:25 [PATCH linux-next 0/6] support ksm_stat showing at cgroup xu.xin16
                   ` (4 preceding siblings ...)
  2025-04-03  5:38 ` [PATCH linux-next 5/6] memcontrol-v1: add ksm_merging_pages " xu.xin16
@ 2025-04-03  5:40 ` xu.xin16
  5 siblings, 0 replies; 7+ messages in thread
From: xu.xin16 @ 2025-04-03  5:40 UTC (permalink / raw)
  To: akpm, mhocko
  Cc: hannes, roman.gushchin, muchun.song, shakeel.butt, cgroups,
	linux-mm, linux-kernel, yang.yang29, chen.haonan2, wang.yaxin,
	xu.xin16

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

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 b52f728af963..3e7a653e3df1 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.15.2


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

end of thread, other threads:[~2025-04-03  5:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-03  5:25 [PATCH linux-next 0/6] support ksm_stat showing at cgroup xu.xin16
2025-04-03  5:27 ` [PATCH linux-next 1/6] memcontrol: rename mem_cgroup_scan_tasks() xu.xin16
2025-04-03  5:30 ` [PATCH linux-next 2/6] memcontrol: introduce the new mem_cgroup_scan_tasks() xu.xin16
2025-04-03  5:31 ` [PATCH linux-next 3/6] memcontrol-v1: introduce ksm_stat at cgroup level xu.xin16
2025-04-03  5:37 ` [PATCH linux-next 4/6] memcontrol-v1: add ksm_zero_pages in cgroup/memory.ksm_stat xu.xin16
2025-04-03  5:38 ` [PATCH linux-next 5/6] memcontrol-v1: add ksm_merging_pages " xu.xin16
2025-04-03  5:40 ` [PATCH linux-next 6/6] memcontrol-v1: add ksm_profit " xu.xin16

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