The patch titled Subject: ksm: count ksm merging pages for each process has been added to the -mm tree. Its filename is ksm-count-ksm-merging-pages-for-each-process.patch This patch should soon appear at https://ozlabs.org/~akpm/mmots/broken-out/ksm-count-ksm-merging-pages-for-each-process.patch and later at https://ozlabs.org/~akpm/mmotm/broken-out/ksm-count-ksm-merging-pages-for-each-process.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next and is updated there every 3-4 working days ------------------------------------------------------ From: xu xin <xu.xin16@xxxxxxxxxx> Subject: ksm: count ksm merging pages for each process 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. Link: https://lkml.kernel.org/r/20220325082318.2352853-1-xu.xin16@xxxxxxxxxx Signed-off-by: xu xin <xu.xin16@xxxxxxxxxx> Reported-by: kernel test robot <lkp@xxxxxxxxx> Reviewed-by: Yang Yang <yang.yang29@xxxxxxxxxx> Reviewed-by: Ran Xiaokai <ran.xiaokai@xxxxxxxxxx> Reported-by: Zeal Robot <zealci@xxxxxxxxxx> Cc: Kees Cook <keescook@xxxxxxxxxxxx> Cc: Alexey Dobriyan <adobriyan@xxxxxxxxx> Cc: Stephen Rothwell <sfr@xxxxxxxxxxxxxxxx> Cc: Ohhoon Kwon <ohoono.kwon@xxxxxxxxxxx> Cc: Matthew Wilcox (Oracle) <willy@xxxxxxxxxxxxx> Cc: Stephen Brennan <stephen.s.brennan@xxxxxxxxxx> Cc: Vlastimil Babka <vbabka@xxxxxxx> Cc: Feng Tang <feng.tang@xxxxxxxxx> Cc: Yang Yang <yang.yang29@xxxxxxxxxx> Cc: Ran Xiaokai <ran.xiaokai@xxxxxxxxxx> Cc: Zeal Robot <zealci@xxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- fs/proc/base.c | 22 ++++++++++++++++++++++ include/linux/mm_types.h | 8 ++++++++ mm/ksm.c | 11 +++++++++++ 3 files changed, 41 insertions(+) --- a/fs/proc/base.c~ksm-count-ksm-merging-pages-for-each-process +++ a/fs/proc/base.c @@ -3155,6 +3155,22 @@ static int proc_pid_patch_state(struct s } #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_ #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_s #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) --- a/include/linux/mm_types.h~ksm-count-ksm-merging-pages-for-each-process +++ a/include/linux/mm_types.h @@ -655,6 +655,14 @@ struct mm_struct { #ifdef CONFIG_IOMMU_SVA 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; /* --- a/mm/ksm.c~ksm-count-ksm-merging-pages-for-each-process +++ a/mm/ksm.c @@ -638,6 +638,10 @@ static void remove_node_from_stable_tree 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(s 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--; @@ -2007,6 +2015,9 @@ static void stable_tree_append(struct rm ksm_pages_sharing++; else ksm_pages_shared++; + + BUG_ON(rmap_item->mm == NULL); + rmap_item->mm->ksm_merging_pages++; } /* _ Patches currently in -mm which might be from xu.xin16@xxxxxxxxxx are ksm-count-ksm-merging-pages-for-each-process.patch