linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH linux-next] ksm: Count ksm-merging pages for each process
@ 2022-03-14  1:53 cgel.zte
  2022-03-14 17:34 ` kernel test robot
  0 siblings, 1 reply; 4+ messages in thread
From: cgel.zte @ 2022-03-14  1:53 UTC (permalink / raw)
  To: mingo, peterz, juri.lelli; +Cc: akpm, bristot, linux-kernel, linux-mm, xu xin

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

As current KSM only counts the number of KSM merging pages(e.g.
ksm_pages_sharing and ksm_pages_shared) of the whole system, we cannot
see the more fine-grained KSM merging, for the upper application
optimization, the merging area cannot be set easily according to the
KSM page merging probability of each process. Therefore, it is necessary
to add extra statistical means so that the upper level users can know
the detailed KSM merging information of each process.

We add a new proc file named as ksm_merging_pages under /proc/<pid>/
to indicate the involved ksm merging pages of this process.

Thanks-to: Yang Yang <yang.yang29@zte.com.cn>
Signed-off-by: xu xin <xu.xin16@zte.com.cn>
---
 fs/proc/base.c        | 15 +++++++++++++++
 include/linux/sched.h |  5 +++++
 mm/ksm.c              | 33 +++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+)

diff --git a/fs/proc/base.c b/fs/proc/base.c
index 76bf1aa3cfe8..b044535df0d2 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -3155,6 +3155,15 @@ static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
 }
 #endif /* CONFIG_LIVEPATCH */
 
+#if defined(CONFIG_KSM) && defined(CONFIG_MEMCG)
+static int proc_pid_ksm_merging_pages(struct seq_file *m, struct pid_namespace *ns,
+				struct pid *pid, struct task_struct *task)
+{
+	seq_printf(m, "%lu\n", task->ksm_merging_pages);
+	return 0;
+}
+#endif /* CONFIG_KSM and CONFIG_MEMCG */
+
 #ifdef CONFIG_STACKLEAK_METRICS
 static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
 				struct pid *pid, struct task_struct *task)
@@ -3286,6 +3295,9 @@ static const struct pid_entry tgid_base_stuff[] = {
 #ifdef CONFIG_SECCOMP_CACHE_DEBUG
 	ONE("seccomp_cache", S_IRUSR, proc_pid_seccomp_cache),
 #endif
+#if defined(CONFIG_KSM) && defined(CONFIG_MEMCG)
+	ONE("ksm_merging_pages",  S_IRUSR, proc_pid_ksm_merging_pages),
+#endif
 };
 
 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
@@ -3619,6 +3631,9 @@ static const struct pid_entry tid_base_stuff[] = {
 #ifdef CONFIG_SECCOMP_CACHE_DEBUG
 	ONE("seccomp_cache", S_IRUSR, proc_pid_seccomp_cache),
 #endif
+#if defined(CONFIG_KSM) && defined(CONFIG_MEMCG)
+	ONE("ksm_merging_pages",  S_IRUSR, proc_pid_ksm_merging_pages),
+#endif
 };
 
 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index ad710b3f664e..d092cf1a100d 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1346,6 +1346,11 @@ struct task_struct {
 	unsigned int			kasan_depth;
 #endif
 
+#if defined(CONFIG_KSM) && defined(CONFIG_MEMCG)
+	unsigned long ksm_merging_pages;
+#endif
+
+
 #ifdef CONFIG_KCSAN
 	struct kcsan_ctx		kcsan_ctx;
 #ifdef CONFIG_TRACE_IRQFLAGS
diff --git a/mm/ksm.c b/mm/ksm.c
index 063a48eeb5ee..c79e7ff278bd 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -629,6 +629,9 @@ static inline void free_stable_node_chain(struct stable_node *chain,
 static void remove_node_from_stable_tree(struct stable_node *stable_node)
 {
 	struct rmap_item *rmap_item;
+#ifdef CONFIG_MEMCG
+	struct task_struct *owner;
+#endif
 
 	/* check it's not STABLE_NODE_CHAIN or negative */
 	BUG_ON(stable_node->rmap_hlist_len < 0);
@@ -638,6 +641,14 @@ static void remove_node_from_stable_tree(struct stable_node *stable_node)
 			ksm_pages_sharing--;
 		else
 			ksm_pages_shared--;
+
+#ifdef CONFIG_MEMCG /*Condition of mm_struct with owner*/
+		BUG_ON(rmap_item->mm == NULL);
+		owner = rmap_item->mm->owner;
+		/* In case that the process of mm may be killed or exit */
+		if (owner)
+			owner->ksm_merging_pages--;
+#endif
 		VM_BUG_ON(stable_node->rmap_hlist_len <= 0);
 		stable_node->rmap_hlist_len--;
 		put_anon_vma(rmap_item->anon_vma);
@@ -771,6 +782,9 @@ static void remove_rmap_item_from_tree(struct rmap_item *rmap_item)
 	if (rmap_item->address & STABLE_FLAG) {
 		struct stable_node *stable_node;
 		struct page *page;
+#ifdef CONFIG_MEMCG
+		struct task_struct *owner;
+#endif
 
 		stable_node = rmap_item->head;
 		page = get_ksm_page(stable_node, GET_KSM_PAGE_LOCK);
@@ -785,6 +799,14 @@ static void remove_rmap_item_from_tree(struct rmap_item *rmap_item)
 			ksm_pages_sharing--;
 		else
 			ksm_pages_shared--;
+
+#ifdef CONFIG_MEMCG /*Condition of mm_struct with owner*/
+		BUG_ON(rmap_item->mm == NULL);
+		owner = rmap_item->mm->owner;
+		/* In case that the process of mm may be killed or exit */
+		if (owner)
+			owner->ksm_merging_pages--;
+#endif
 		VM_BUG_ON(stable_node->rmap_hlist_len <= 0);
 		stable_node->rmap_hlist_len--;
 
@@ -1981,6 +2003,9 @@ static void stable_tree_append(struct rmap_item *rmap_item,
 			       struct stable_node *stable_node,
 			       bool max_page_sharing_bypass)
 {
+#ifdef CONFIG_MEMCG
+	struct task_struct *owner;
+#endif
 	/*
 	 * rmap won't find this mapping if we don't insert the
 	 * rmap_item in the right stable_node
@@ -2007,6 +2032,14 @@ static void stable_tree_append(struct rmap_item *rmap_item,
 		ksm_pages_sharing++;
 	else
 		ksm_pages_shared++;
+
+#ifdef CONFIG_MEMCG /*Condition of mm_struct with owner*/
+	BUG_ON(rmap_item->mm == NULL);
+	owner = rmap_item->mm->owner;
+	/* In case that the process of mm may be killed or exit */
+	if (owner)
+		owner->ksm_merging_pages++;
+#endif
 }
 
 /*
-- 
2.25.1



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

* Re: [PATCH linux-next] ksm: Count ksm-merging pages for each process
  2022-03-14  1:53 [PATCH linux-next] ksm: Count ksm-merging pages for each process cgel.zte
@ 2022-03-14 17:34 ` kernel test robot
  2022-03-15 11:48   ` [PATCH v2] ksm: Count ksm merging " cgel.zte
  0 siblings, 1 reply; 4+ messages in thread
From: kernel test robot @ 2022-03-14 17:34 UTC (permalink / raw)
  To: cgel.zte, mingo, peterz, juri.lelli
  Cc: kbuild-all, akpm, bristot, linux-kernel, linux-mm, xu xin

Hi,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on next-20220310]

url:    https://github.com/0day-ci/linux/commits/cgel-zte-gmail-com/ksm-Count-ksm-merging-pages-for-each-process/20220314-095633
base:    71941773e143369a73c9c4a3b62fbb60736a1182
config: s390-randconfig-s031-20220313 (https://download.01.org/0day-ci/archive/20220315/202203150131.oDjproo7-lkp@intel.com/config)
compiler: s390-linux-gcc (GCC) 11.2.0
reproduce:
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # apt-get install sparse
        # sparse version: v0.6.4-dirty
        # https://github.com/0day-ci/linux/commit/64577121c010929e9f2604053f4b96402d3089a8
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review cgel-zte-gmail-com/ksm-Count-ksm-merging-pages-for-each-process/20220314-095633
        git checkout 64577121c010929e9f2604053f4b96402d3089a8
        # save the config file to linux build tree
        mkdir build_dir
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=s390 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>


sparse warnings: (new ones prefixed by >>)
>> mm/ksm.c:647:23: sparse: sparse: incorrect type in assignment (different address spaces) @@     expected struct task_struct *owner @@     got struct task_struct [noderef] __rcu *owner @@
   mm/ksm.c:647:23: sparse:     expected struct task_struct *owner
   mm/ksm.c:647:23: sparse:     got struct task_struct [noderef] __rcu *owner
   mm/ksm.c:805:23: sparse: sparse: incorrect type in assignment (different address spaces) @@     expected struct task_struct *owner @@     got struct task_struct [noderef] __rcu *owner @@
   mm/ksm.c:805:23: sparse:     expected struct task_struct *owner
   mm/ksm.c:805:23: sparse:     got struct task_struct [noderef] __rcu *owner
   mm/ksm.c:2038:15: sparse: sparse: incorrect type in assignment (different address spaces) @@     expected struct task_struct *owner @@     got struct task_struct [noderef] __rcu *owner @@
   mm/ksm.c:2038:15: sparse:     expected struct task_struct *owner
   mm/ksm.c:2038:15: sparse:     got struct task_struct [noderef] __rcu *owner
   mm/ksm.c: note: in included file:
   include/linux/rmap.h:248:28: sparse: sparse: context imbalance in 'write_protect_page' - unexpected unlock

vim +647 mm/ksm.c

   635	
   636		/* check it's not STABLE_NODE_CHAIN or negative */
   637		BUG_ON(stable_node->rmap_hlist_len < 0);
   638	
   639		hlist_for_each_entry(rmap_item, &stable_node->hlist, hlist) {
   640			if (rmap_item->hlist.next)
   641				ksm_pages_sharing--;
   642			else
   643				ksm_pages_shared--;
   644	
   645	#ifdef CONFIG_MEMCG /*Condition of mm_struct with owner*/
   646			BUG_ON(rmap_item->mm == NULL);
 > 647			owner = rmap_item->mm->owner;
   648			/* In case that the process of mm may be killed or exit */
   649			if (owner)
   650				owner->ksm_merging_pages--;
   651	#endif
   652			VM_BUG_ON(stable_node->rmap_hlist_len <= 0);
   653			stable_node->rmap_hlist_len--;
   654			put_anon_vma(rmap_item->anon_vma);
   655			rmap_item->address &= PAGE_MASK;
   656			cond_resched();
   657		}
   658	
   659		/*
   660		 * We need the second aligned pointer of the migrate_nodes
   661		 * list_head to stay clear from the rb_parent_color union
   662		 * (aligned and different than any node) and also different
   663		 * from &migrate_nodes. This will verify that future list.h changes
   664		 * don't break STABLE_NODE_DUP_HEAD. Only recent gcc can handle it.
   665		 */
   666		BUILD_BUG_ON(STABLE_NODE_DUP_HEAD <= &migrate_nodes);
   667		BUILD_BUG_ON(STABLE_NODE_DUP_HEAD >= &migrate_nodes + 1);
   668	
   669		if (stable_node->head == &migrate_nodes)
   670			list_del(&stable_node->list);
   671		else
   672			stable_node_dup_del(stable_node);
   673		free_stable_node(stable_node);
   674	}
   675	

---
0-DAY CI Kernel Test Service
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org


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

* [PATCH v2] ksm: Count ksm merging pages for each process
  2022-03-14 17:34 ` kernel test robot
@ 2022-03-15 11:48   ` cgel.zte
  2022-03-22  8:46     ` cgel.zte
  0 siblings, 1 reply; 4+ messages in thread
From: cgel.zte @ 2022-03-15 11:48 UTC (permalink / raw)
  To: lkp
  Cc: akpm, bristot, cgel.zte, juri.lelli, kbuild-all, linux-kernel,
	linux-mm, mingo, peterz, xu.xin16, Yang Yang, Ran Xiaokai,
	Zeal Robot

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

As current KSM only counts the number of KSM merging pages(e.g.
ksm_pages_sharing and ksm_pages_shared) of the whole system, we cannot
see the more fine-grained KSM merging, for the upper application
optimization, the merging area cannot be set easily according to the
KSM page merging probability of each process. Therefore, it is necessary
to add extra statistical means so that the upper level users can know
the detailed KSM merging information of each process.

We add a new proc file named as ksm_merging_pages under /proc/<pid>/
to indicate the involved ksm merging pages of this process.

Reported-by: kernel test robot <lkp@intel.com>
Reviewed-by: Yang Yang <yang.yang29@zte.com.cn>
Reviewed-by: Ran Xiaokai <ran.xiaokai@zte.com.cn>
Reported-by: Zeal Robot <zealci@zte.com.cn>
Signed-off-by: xu xin <xu.xin16@zte.com.cn>
---
 fs/proc/base.c           | 22 ++++++++++++++++++++++
 include/linux/mm_types.h |  8 ++++++++
 mm/ksm.c                 | 11 +++++++++++
 3 files changed, 41 insertions(+)

diff --git a/fs/proc/base.c b/fs/proc/base.c
index d654ce7..5c8feef 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -3155,6 +3155,22 @@ static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
 }
 #endif /* CONFIG_LIVEPATCH */
 
+#ifdef CONFIG_KSM
+static int proc_pid_ksm_merging_pages(struct seq_file *m, struct pid_namespace *ns,
+				struct pid *pid, struct task_struct *task)
+{
+	struct mm_struct *mm;
+
+	mm = get_task_mm(task);
+	if (mm) {
+		seq_printf(m, "%lu\n", mm->ksm_merging_pages);
+		mmput(mm);
+	}
+
+	return 0;
+}
+#endif /* CONFIG_KSM */
+
 #ifdef CONFIG_STACKLEAK_METRICS
 static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
 				struct pid *pid, struct task_struct *task)
@@ -3286,6 +3302,9 @@ static const struct pid_entry tgid_base_stuff[] = {
 #ifdef CONFIG_SECCOMP_CACHE_DEBUG
 	ONE("seccomp_cache", S_IRUSR, proc_pid_seccomp_cache),
 #endif
+#ifdef CONFIG_KSM
+	ONE("ksm_merging_pages",  S_IRUSR, proc_pid_ksm_merging_pages),
+#endif
 };
 
 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
@@ -3619,6 +3638,9 @@ static const struct pid_entry tid_base_stuff[] = {
 #ifdef CONFIG_SECCOMP_CACHE_DEBUG
 	ONE("seccomp_cache", S_IRUSR, proc_pid_seccomp_cache),
 #endif
+#ifdef CONFIG_KSM
+	ONE("ksm_merging_pages",  S_IRUSR, proc_pid_ksm_merging_pages),
+#endif
 };
 
 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 9db36dc..4d98bd5 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -633,6 +633,14 @@ struct mm_struct {
 #ifdef CONFIG_IOMMU_SUPPORT
 		u32 pasid;
 #endif
+
+#ifdef CONFIG_KSM
+		/*
+		 * Represet how many pages of this process are
+		 * involved in KSM merging.
+		 */
+		unsigned long ksm_merging_pages;
+#endif
 	} __randomize_layout;
 
 	/*
diff --git a/mm/ksm.c b/mm/ksm.c
index c20bd4d..5889733 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -638,6 +638,10 @@ static void remove_node_from_stable_tree(struct stable_node *stable_node)
 			ksm_pages_sharing--;
 		else
 			ksm_pages_shared--;
+
+		BUG_ON(rmap_item->mm == NULL);
+		rmap_item->mm->ksm_merging_pages--;
+
 		VM_BUG_ON(stable_node->rmap_hlist_len <= 0);
 		stable_node->rmap_hlist_len--;
 		put_anon_vma(rmap_item->anon_vma);
@@ -785,6 +789,10 @@ static void remove_rmap_item_from_tree(struct rmap_item *rmap_item)
 			ksm_pages_sharing--;
 		else
 			ksm_pages_shared--;
+
+		BUG_ON(rmap_item->mm == NULL);
+		rmap_item->mm->ksm_merging_pages--;
+
 		VM_BUG_ON(stable_node->rmap_hlist_len <= 0);
 		stable_node->rmap_hlist_len--;
 
@@ -2020,6 +2028,9 @@ static void stable_tree_append(struct rmap_item *rmap_item,
 		ksm_pages_sharing++;
 	else
 		ksm_pages_shared++;
+
+	BUG_ON(rmap_item->mm == NULL);
+	rmap_item->mm->ksm_merging_pages++;
 }
 
 /*
-- 
2.15.2



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

* Re: [PATCH v2] ksm: Count ksm merging pages for each process
  2022-03-15 11:48   ` [PATCH v2] ksm: Count ksm merging " cgel.zte
@ 2022-03-22  8:46     ` cgel.zte
  0 siblings, 0 replies; 4+ messages in thread
From: cgel.zte @ 2022-03-22  8:46 UTC (permalink / raw)
  To: akpm, lkp
  Cc: juri.lelli, kbuild-all, linux-kernel, linux-mm, mingo, peterz, keescook

Hi, everyone.

What do you think of this patch? I look forward to your views.

patch: https://lore.kernel.org/lkml/20220315114849.2119443-1-xu.xin16@zte.com.cn/#t

Thanks,
xu xin


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

end of thread, other threads:[~2022-03-22  8:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-14  1:53 [PATCH linux-next] ksm: Count ksm-merging pages for each process cgel.zte
2022-03-14 17:34 ` kernel test robot
2022-03-15 11:48   ` [PATCH v2] ksm: Count ksm merging " cgel.zte
2022-03-22  8:46     ` cgel.zte

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