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.
The value of "ksm_rmap_items" indicates the total allocated ksm
rmap_items of this memcg, which could be used to determine how
unbeneficial the ksm-policy (like madvise), they are using brings,
since the bigger the ratio of ksm_rmap_items over ksm_merging_pages,
the more unbeneficial the ksm bring.
Add the counter in the existing memory.stat without adding a new interface.
We traverse all processes of a memcg and summing the processes'
ksm_rmap_items counters instead of adding enum item in memcg_stat_item
or node_stat_item and updating the corresponding enum counter when
ksmd manipulate pages.
Finally, we can look up ksm_rmap_items of per-memcg simply by:
cat /sys/fs/cgroup/memory.stat | grep ksm_rmap_items
Signed-off-by: xu xin <xu.xin16@zte.com.cn>
---
include/linux/ksm.h | 1 +
mm/ksm.c | 38 ++++++++++++++++++++++++++++++++++++++
mm/memcontrol.c | 5 +++++
3 files changed, 44 insertions(+)
diff --git a/include/linux/ksm.h b/include/linux/ksm.h
index 22e67ca7cba3..a41ed503f152 100644
--- a/include/linux/ksm.h
+++ b/include/linux/ksm.h
@@ -94,6 +94,7 @@ void collect_procs_ksm(const struct folio *folio, const struct page *page,
struct list_head *to_kill, int force_early);
long ksm_process_profit(struct mm_struct *);
bool ksm_process_mergeable(struct mm_struct *mm);
+void memcg_stat_ksm_show(struct mem_cgroup *memcg, struct seq_buf *s);
#else /* !CONFIG_KSM */
diff --git a/mm/ksm.c b/mm/ksm.c
index 2ef29802a49b..b533f0edaf96 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -3308,6 +3308,44 @@ long ksm_process_profit(struct mm_struct *mm)
}
#endif /* CONFIG_PROC_FS */
+#ifdef CONFIG_MEMCG
+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;
+}
+
+/* Show the ksm statistic count at memory.stat under cgroup mountpoint */
+void memcg_stat_ksm_show(struct mem_cgroup *memcg, struct seq_buf *s)
+{
+ struct memcg_ksm_stat ksm_stat;
+
+ if (mem_cgroup_is_root(memcg)) {
+ /* Just use the global counters when root memcg */
+ ksm_stat.ksm_rmap_items = ksm_rmap_items;
+ } else {
+ /* Initialization */
+ ksm_stat.ksm_rmap_items = 0;
+ /* Summing all processes'ksm statistic items */
+ mem_cgroup_scan_tasks(memcg, evaluate_memcg_ksm_stat, &ksm_stat);
+ }
+ /* Print memcg ksm statistic items */
+ seq_buf_printf(s, "ksm_rmap_items %lu\n", ksm_stat.ksm_rmap_items);
+}
+#endif
+
#ifdef CONFIG_SYSFS
/*
* This all compiles without CONFIG_SYSFS, but is a waste of space.
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 257d2c76b730..9595b132c6c3 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -63,6 +63,7 @@
#include <linux/seq_buf.h>
#include <linux/sched/isolation.h>
#include <linux/kmemleak.h>
+#include <linux/ksm.h>
#include "internal.h"
#include <net/sock.h>
#include <net/ip.h>
@@ -1492,6 +1493,10 @@ static void memcg_stat_format(struct mem_cgroup *memcg, struct seq_buf *s)
}
}
+#ifdef CONFIG_KSM
+ memcg_stat_ksm_show(memcg, s);
+#endif
+
/* Accumulated memory events */
seq_buf_printf(s, "pgscan %lu\n",
memcg_events(memcg, PGSCAN_KSWAPD) +
--
2.25.1
Hi, kernel test robot noticed the following build errors: [auto build test ERROR on akpm-mm/mm-everything] [also build test ERROR on tj-cgroup/for-next linus/master v6.17-rc5 next-20250912] [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-xin16-zte-com-cn/memcg-add-per-memcg-ksm_rmap_items-stat/20250914-181132 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20250914180351288rcLuZnPAMUej48nuTc7KV%40zte.com.cn patch subject: [PATCH v2 1/5] memcg: add per-memcg ksm_rmap_items stat config: parisc-randconfig-001-20250914 (https://download.01.org/0day-ci/archive/20250914/202509142147.WQI0impC-lkp@intel.com/config) compiler: hppa-linux-gcc (GCC) 8.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250914/202509142147.WQI0impC-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/202509142147.WQI0impC-lkp@intel.com/ All errors (new ones prefixed by >>): mm/ksm.c: In function 'memcg_stat_ksm_show': >> mm/ksm.c:3345:2: error: implicit declaration of function 'seq_buf_printf'; did you mean 'seq_bprintf'? [-Werror=implicit-function-declaration] seq_buf_printf(s, "ksm_rmap_items %lu\n", ksm_stat.ksm_rmap_items); ^~~~~~~~~~~~~~ seq_bprintf cc1: some warnings being treated as errors vim +3345 mm/ksm.c 3329 3330 /* Show the ksm statistic count at memory.stat under cgroup mountpoint */ 3331 void memcg_stat_ksm_show(struct mem_cgroup *memcg, struct seq_buf *s) 3332 { 3333 struct memcg_ksm_stat ksm_stat; 3334 3335 if (mem_cgroup_is_root(memcg)) { 3336 /* Just use the global counters when root memcg */ 3337 ksm_stat.ksm_rmap_items = ksm_rmap_items; 3338 } else { 3339 /* Initialization */ 3340 ksm_stat.ksm_rmap_items = 0; 3341 /* Summing all processes'ksm statistic items */ 3342 mem_cgroup_scan_tasks(memcg, evaluate_memcg_ksm_stat, &ksm_stat); 3343 } 3344 /* Print memcg ksm statistic items */ > 3345 seq_buf_printf(s, "ksm_rmap_items %lu\n", ksm_stat.ksm_rmap_items); 3346 } 3347 #endif 3348 -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
© 2016 - 2025 Red Hat, Inc.