Skip to content

Commit

Permalink
ksm: count zero pages for each process
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
xu xin authored and intel-lab-lkp committed Oct 8, 2022
1 parent e7bc1de commit 73e5356
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions fs/proc/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
7 changes: 6 additions & 1 deletion include/linux/mm_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -691,14 +691,19 @@ 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;
/*
* Represent how many pages are checked for ksm merging
* 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 {
Expand Down
3 changes: 3 additions & 0 deletions mm/ksm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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--;
Expand Down Expand Up @@ -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++;
}
}

Expand Down Expand Up @@ -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--;
}
}

Expand Down

0 comments on commit 73e5356

Please sign in to comment.