Skip to contentSkip to conversation

ksm: count zero pages for each process

PATCH Superseded [email protected] 2 participants 2 messages Dec 30, 2022 – Jan 18, 2023

Subsystems (4)

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 4/6 ksm: count zero pages for each process

From: xu xin <[email protected]>

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 <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Xuexin Jiang <[email protected]>
Cc Xiaokai Ran <[email protected]>
Cc Yang Yang <[email protected]>
---
 fs/proc/base.c           | 1 +
 include/linux/mm_types.h | 7 ++++++-
 mm/ksm.c                 | 2 ++
 3 files changed, 9 insertions(+), 1 deletion(-)
fs/proc/base.c +1 -0
index 9e479d7d202b..ac9ebe972be0 100644
@@ -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);
 	}
 
include/linux/mm_types.h +6 -1
index 4e1031626403..5c734ebc1890 100644
@@ -776,7 +776,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;
 		/*
@@ -784,6 +784,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 {
mm/ksm.c +2 -0
index 72c0722be280..083f5d125373 100644
@@ -794,6 +794,7 @@ static inline void clean_rmap_item_zero_flag(struct ksm_rmap_item *rmap_item)
 {
 	if (rmap_item->address & ZERO_PAGE_FLAG) {
 		ksm_zero_pages_sharing--;
+		rmap_item->mm->ksm_zero_pages_sharing--;
 		rmap_item->address &= PAGE_MASK;
 	}
 }
@@ -2117,6 +2118,7 @@ static int try_to_merge_with_kernel_zero_page(struct ksm_rmap_item *rmap_item,
 			if (!err) {
 				rmap_item->address |= ZERO_PAGE_FLAG;
 				ksm_zero_pages_sharing++;
+				rmap_item->mm->ksm_zero_pages_sharing++;
 			}
 		} else {
 			/* If the vma is out of date, we do not need to continue. */
Signature · 1 line
2.15.2
#1 lore raw
Quoted 53 lines · On 30.12.22 02:16, [email protected] wrote:

From: xu xin <[email protected]>

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 <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Xuexin Jiang <[email protected]>
Cc Xiaokai Ran <[email protected]>
Cc Yang Yang <[email protected]>
---
  fs/proc/base.c           | 1 +
  include/linux/mm_types.h | 7 ++++++-
  mm/ksm.c                 | 2 ++
  3 files changed, 9 insertions(+), 1 deletion(-)
fs/proc/base.c +1 -0
index 9e479d7d202b..ac9ebe972be0 100644
@@ -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);
  	}
 
include/linux/mm_types.h +1 -1
index 4e1031626403..5c734ebc1890 100644
@@ -776,7 +776,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;
  		/*

include/linux/mm_types.h struct mm_struct { · line 791

Quoted 8 lines
include/linux/mm_types.h +5 -0
@@ -784,6 +784,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;

Same comment as for previous patch: this counts rmap itmaps, no? ksm_zero_rmap_items might be more appropriate.

Signature · 3 lines
Thanks,

David / dhildenb