diff mbox series

[4/5] ksm: count zero pages for each process

Message ID 20221008070642.308693-1-xu.xin16@zte.com.cn (mailing list archive)
State New
Headers show
Series ksm: support tracking KSM-placed zero-pages | expand

Commit Message

xu xin Oct. 8, 2022, 7:06 a.m. UTC
From: xu xin <xu.xin16@zte.com.cn>

As the number of ksm zero pages is not included in ksm_merging_pages per
process when enabling use_zero_pages, it's unclear of how many actual
pages are merged by KSM. To let users accurately estimate their memory
demands when unsharing KSM zero-pages, it's necessary to show KSM zero-
pages per process.

since unsharing zero pages placed by KSM accurately is achieved, then
tracking empty pages merging and unmerging is not a difficult thing any
longer.

Since we already have /proc/<pid>/ksm_stat, just add the information of
zero_pages_sharing in it.

Cc: Claudio Imbrenda <imbrenda@linux.ibm.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Xuexin Jiang <jiang.xuexin@zte.com.cn>
Cc: Xiaokai Ran <ran.xiaokai@zte.com.cn>
Cc: Yang Yang <yang.yang29@zte.com.cn>
Signed-off-by: xu xin <xu.xin16@zte.com.cn>
---
 fs/proc/base.c           | 1 +
 include/linux/mm_types.h | 7 ++++++-
 mm/ksm.c                 | 3 +++
 3 files changed, 10 insertions(+), 1 deletion(-)

Comments

kernel test robot Oct. 8, 2022, 1:55 p.m. UTC | #1
Hi,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on akpm-mm/mm-everything]
[also build test WARNING on next-20221007]
[cannot apply to linus/master v6.0]
[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-sc-gmail-com/ksm-support-tracking-KSM-placed-zero-pages/20221008-150936
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
config: i386-randconfig-a002-20221003
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/73e535667af6553c5f6317da4d8034e79557417b
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review xu-xin-sc-gmail-com/ksm-support-tracking-KSM-placed-zero-pages/20221008-150936
        git checkout 73e535667af6553c5f6317da4d8034e79557417b
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

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

All warnings (new ones prefixed by >>):

>> mm/ksm.c:550:4: warning: misleading indentation; statement is not part of the previous 'if' [-Wmisleading-indentation]
                           rmap_item->mm->ksm_zero_pages_sharing--;
                           ^
   mm/ksm.c:548:3: note: previous statement is here
                   if (!unshare_zero_pages(rmap_item))
                   ^
   1 warning generated.


vim +/if +550 mm/ksm.c

   544	
   545	static inline void free_rmap_item(struct ksm_rmap_item *rmap_item)
   546	{
   547		if (rmap_item->address & ZERO_PAGE_FLAG) {
   548			if (!unshare_zero_pages(rmap_item))
   549				ksm_zero_pages_sharing--;
 > 550				rmap_item->mm->ksm_zero_pages_sharing--;
   551		}
   552		ksm_rmap_items--;
   553		rmap_item->mm->ksm_rmap_items--;
   554		rmap_item->mm = NULL;   /* debug safety */
   555		kmem_cache_free(rmap_item_cache, rmap_item);
   556	}
   557
diff mbox series

Patch

diff --git a/fs/proc/base.c b/fs/proc/base.c
index 9e479d7d202b..ac9ebe972be0 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -3207,6 +3207,7 @@  static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns,
 	mm = get_task_mm(task);
 	if (mm) {
 		seq_printf(m, "ksm_rmap_items %lu\n", mm->ksm_rmap_items);
+		seq_printf(m, "zero_pages_sharing %lu\n", mm->ksm_zero_pages_sharing);
 		mmput(mm);
 	}
 
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 500e536796ca..78a4ee264645 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -691,7 +691,7 @@  struct mm_struct {
 #ifdef CONFIG_KSM
 		/*
 		 * Represent how many pages of this process are involved in KSM
-		 * merging.
+		 * merging (not including ksm_zero_pages_sharing).
 		 */
 		unsigned long ksm_merging_pages;
 		/*
@@ -699,6 +699,11 @@  struct mm_struct {
 		 * including merged and not merged.
 		 */
 		unsigned long ksm_rmap_items;
+		/*
+		 * Represent how many empty pages are merged with kernel zero
+		 * pages when enabling KSM use_zero_pages.
+		 */
+		unsigned long ksm_zero_pages_sharing;
 #endif
 #ifdef CONFIG_LRU_GEN
 		struct {
diff --git a/mm/ksm.c b/mm/ksm.c
index 9b7c28abfc89..a889b1925f51 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -547,6 +547,7 @@  static inline void free_rmap_item(struct ksm_rmap_item *rmap_item)
 	if (rmap_item->address & ZERO_PAGE_FLAG) {
 		if (!unshare_zero_pages(rmap_item))
 			ksm_zero_pages_sharing--;
+			rmap_item->mm->ksm_zero_pages_sharing--;
 	}
 	ksm_rmap_items--;
 	rmap_item->mm->ksm_rmap_items--;
@@ -2082,6 +2083,7 @@  static int try_to_merge_with_kernel_zero_page(struct mm_struct *mm,
 		if (!err) {
 			rmap_item->address |= ZERO_PAGE_FLAG;
 			ksm_zero_pages_sharing++;
+			rmap_item->mm->ksm_zero_pages_sharing++;
 		}
 	}
 
@@ -2185,6 +2187,7 @@  static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
 			 */
 			rmap_item->address &= PAGE_MASK;
 			ksm_zero_pages_sharing--;
+			rmap_item->mm->ksm_zero_pages_sharing--;
 		}
 	}