All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5 3/6] ksm: count all zero pages placed by KSM
@ 2022-12-30  1:15 yang.yang29
  2023-01-18 14:30 ` David Hildenbrand
  0 siblings, 1 reply; 4+ messages in thread
From: yang.yang29 @ 2022-12-30  1:15 UTC (permalink / raw)
  To: akpm
  Cc: david, imbrenda, jiang.xuexin, linux-kernel, linux-mm,
	ran.xiaokai, xu.xin.sc, xu.xin16, yang.yang29

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

As pages_sharing and pages_shared don't include the number of zero pages
merged by KSM, we cannot know how many pages are zero pages placed by KSM
when enabling use_zero_pages, which leads to KSM not being transparent with
all actual merged pages by KSM. In the early days of use_zero_pages,
zero-pages was unable to get unshared by the ways like MADV_UNMERGEABLE so
it's hard to count how many times one of those zeropages was then unmerged.

But now, unsharing KSM-placed zero page accurately has been achieved, so we
can easily count both how many times a page full of zeroes was merged with
zero-page and how many times one of those pages was then unmerged. and so,
it helps to estimate memory demands when each and every shared page could
get unshared.

So we add zero_pages_sharing under /sys/kernel/mm/ksm/ to show the number
of all zero pages placed by KSM.

Signed-off-by: xu xin <xu.xin16@zte.com.cn>
Cc: Claudio Imbrenda <imbrenda@linux.ibm.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Xuexin Jiang <jiang.xuexin@zte.com.cn>
Reviewed-by: Xiaokai Ran <ran.xiaokai@zte.com.cn>
Reviewed-by: Yang Yang <yang.yang29@zte.com.cn>

 v4->v5:
 fix warning  mm/ksm.c:3238:9: warning: no previous prototype for
 'zero_pages_sharing_show' [-Wmissing-prototypes].
---
 mm/ksm.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/mm/ksm.c b/mm/ksm.c
index 652c088f9786..72c0722be280 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -276,6 +276,9 @@ static unsigned int zero_checksum __read_mostly;
 /* Whether to merge empty (zeroed) pages with actual zero pages */
 static bool ksm_use_zero_pages __read_mostly;

+/* The number of zero pages placed by KSM use_zero_pages */
+static unsigned long ksm_zero_pages_sharing;
+
 #ifdef CONFIG_NUMA
 /* Zeroed when merging across nodes is not allowed */
 static unsigned int ksm_merge_across_nodes = 1;
@@ -789,8 +792,10 @@ static struct page *get_ksm_page(struct ksm_stable_node *stable_node,
  */
 static inline void clean_rmap_item_zero_flag(struct ksm_rmap_item *rmap_item)
 {
-	if (rmap_item->address & ZERO_PAGE_FLAG)
+	if (rmap_item->address & ZERO_PAGE_FLAG) {
+		ksm_zero_pages_sharing--;
 		rmap_item->address &= PAGE_MASK;
+	}
 }

 /* Only called when rmap_item is going to be freed */
@@ -2109,8 +2114,10 @@ static int try_to_merge_with_kernel_zero_page(struct ksm_rmap_item *rmap_item,
 		if (vma) {
 			err = try_to_merge_one_page(vma, page,
 						ZERO_PAGE(rmap_item->address));
-			if (!err)
+			if (!err) {
 				rmap_item->address |= ZERO_PAGE_FLAG;
+				ksm_zero_pages_sharing++;
+			}
 		} else {
 			/* If the vma is out of date, we do not need to continue. */
 			err = 0;
@@ -3228,6 +3235,13 @@ static ssize_t pages_volatile_show(struct kobject *kobj,
 }
 KSM_ATTR_RO(pages_volatile);

+static ssize_t zero_pages_sharing_show(struct kobject *kobj,
+				struct kobj_attribute *attr, char *buf)
+{
+	return sysfs_emit(buf, "%ld\n", ksm_zero_pages_sharing);
+}
+KSM_ATTR_RO(zero_pages_sharing);
+
 static ssize_t stable_node_dups_show(struct kobject *kobj,
 				     struct kobj_attribute *attr, char *buf)
 {
@@ -3283,6 +3297,7 @@ static struct attribute *ksm_attrs[] = {
 	&pages_sharing_attr.attr,
 	&pages_unshared_attr.attr,
 	&pages_volatile_attr.attr,
+	&zero_pages_sharing_attr.attr,
 	&full_scans_attr.attr,
 #ifdef CONFIG_NUMA
 	&merge_across_nodes_attr.attr,
-- 
2.15.2

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

* Re: [PATCH v5 3/6] ksm: count all zero pages placed by KSM
  2022-12-30  1:15 [PATCH v5 3/6] ksm: count all zero pages placed by KSM yang.yang29
@ 2023-01-18 14:30 ` David Hildenbrand
  2023-02-09 12:01   ` yang.yang29
  0 siblings, 1 reply; 4+ messages in thread
From: David Hildenbrand @ 2023-01-18 14:30 UTC (permalink / raw)
  To: yang.yang29, akpm
  Cc: imbrenda, jiang.xuexin, linux-kernel, linux-mm, ran.xiaokai,
	xu.xin.sc, xu.xin16

On 30.12.22 02:15, yang.yang29@zte.com.cn wrote:
> From: xu xin <xu.xin16@zte.com.cn>
> 
> As pages_sharing and pages_shared don't include the number of zero pages
> merged by KSM, we cannot know how many pages are zero pages placed by KSM
> when enabling use_zero_pages, which leads to KSM not being transparent with
> all actual merged pages by KSM. In the early days of use_zero_pages,
> zero-pages was unable to get unshared by the ways like MADV_UNMERGEABLE so
> it's hard to count how many times one of those zeropages was then unmerged.
> 
> But now, unsharing KSM-placed zero page accurately has been achieved, so we
> can easily count both how many times a page full of zeroes was merged with
> zero-page and how many times one of those pages was then unmerged. and so,
> it helps to estimate memory demands when each and every shared page could
> get unshared.
> 
> So we add zero_pages_sharing under /sys/kernel/mm/ksm/ to show the number
> of all zero pages placed by KSM.
> 
> Signed-off-by: xu xin <xu.xin16@zte.com.cn>
> Cc: Claudio Imbrenda <imbrenda@linux.ibm.com>
> Cc: David Hildenbrand <david@redhat.com>
> Cc: Xuexin Jiang <jiang.xuexin@zte.com.cn>
> Reviewed-by: Xiaokai Ran <ran.xiaokai@zte.com.cn>
> Reviewed-by: Yang Yang <yang.yang29@zte.com.cn>
> 
>   v4->v5:
>   fix warning  mm/ksm.c:3238:9: warning: no previous prototype for
>   'zero_pages_sharing_show' [-Wmissing-prototypes].
> ---
>   mm/ksm.c | 19 +++++++++++++++++--
>   1 file changed, 17 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/ksm.c b/mm/ksm.c
> index 652c088f9786..72c0722be280 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -276,6 +276,9 @@ static unsigned int zero_checksum __read_mostly;
>   /* Whether to merge empty (zeroed) pages with actual zero pages */
>   static bool ksm_use_zero_pages __read_mostly;
> 
> +/* The number of zero pages placed by KSM use_zero_pages */
> +static unsigned long ksm_zero_pages_sharing;

Does this count how many zero pages are currently placed or how many 
rmap items with ZERO_PAGE_FLAG are in the system?

IOW, if someone triggers MADV_DONTNEED on such a zeropage, the counter 
will not get updated, correct?


-- 
Thanks,

David / dhildenb


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

* Re: [PATCH v5 3/6] ksm: count all zero pages placed by KSM
  2023-01-18 14:30 ` David Hildenbrand
@ 2023-02-09 12:01   ` yang.yang29
  2023-02-13 12:55     ` David Hildenbrand
  0 siblings, 1 reply; 4+ messages in thread
From: yang.yang29 @ 2023-02-09 12:01 UTC (permalink / raw)
  To: david
  Cc: akpm, imbrenda, jiang.xuexin, linux-kernel, linux-mm,
	ran.xiaokai, xu.xin.sc, xu.xin16

> > diff --git a/mm/ksm.c b/mm/ksm.c
> > index 652c088f9786..72c0722be280 100644
> > --- a/mm/ksm.c
> > +++ b/mm/ksm.c
> > @@ -276,6 +276,9 @@ static unsigned int zero_checksum __read_mostly;
> >   /* Whether to merge empty (zeroed) pages with actual zero pages */
> >   static bool ksm_use_zero_pages __read_mostly;
> > 
> > +/* The number of zero pages placed by KSM use_zero_pages */
> > +static unsigned long ksm_zero_pages_sharing;
> 
> Does this count how many zero pages are currently placed or how many 
> rmap items with ZERO_PAGE_FLAG are in the system?

Yes, it counts how many ksm zero pages are currently placed. and we use
rmap items with ZERO_PAGE_FLAG to record these zero page, similar to 
ksm_pages_sharing and ksm_pages_shared which are recorded by the rmap_items
with STABLE_FLAG.

> 
> IOW, if someone triggers MADV_DONTNEED on such a zeropage, the counter 
> will not get updated, correct?

Well, the counter can get updated as someone triggers MADV_DONTNEED on such
a zeropage. You might write a simple code to test it.

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

* Re: [PATCH v5 3/6] ksm: count all zero pages placed by KSM
  2023-02-09 12:01   ` yang.yang29
@ 2023-02-13 12:55     ` David Hildenbrand
  0 siblings, 0 replies; 4+ messages in thread
From: David Hildenbrand @ 2023-02-13 12:55 UTC (permalink / raw)
  To: yang.yang29
  Cc: akpm, imbrenda, jiang.xuexin, linux-kernel, linux-mm,
	ran.xiaokai, xu.xin.sc, xu.xin16

On 09.02.23 13:01, yang.yang29@zte.com.cn wrote:
>>> diff --git a/mm/ksm.c b/mm/ksm.c
>>> index 652c088f9786..72c0722be280 100644
>>> --- a/mm/ksm.c
>>> +++ b/mm/ksm.c
>>> @@ -276,6 +276,9 @@ static unsigned int zero_checksum __read_mostly;
>>>    /* Whether to merge empty (zeroed) pages with actual zero pages */
>>>    static bool ksm_use_zero_pages __read_mostly;
>>>
>>> +/* The number of zero pages placed by KSM use_zero_pages */
>>> +static unsigned long ksm_zero_pages_sharing;
>>
>> Does this count how many zero pages are currently placed or how many
>> rmap items with ZERO_PAGE_FLAG are in the system?
> 
> Yes, it counts how many ksm zero pages are currently placed. and we use
> rmap items with ZERO_PAGE_FLAG to record these zero page, similar to
> ksm_pages_sharing and ksm_pages_shared which are recorded by the rmap_items
> with STABLE_FLAG.
> 
>>
>> IOW, if someone triggers MADV_DONTNEED on such a zeropage, the counter
>> will not get updated, correct?
> 
> Well, the counter can get updated as someone triggers MADV_DONTNEED on such
> a zeropage. You might write a simple code to test it.

Interesting, I'll have a look how that will be triggered.

-- 
Thanks,

David / dhildenb


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

end of thread, other threads:[~2023-02-13 12:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-30  1:15 [PATCH v5 3/6] ksm: count all zero pages placed by KSM yang.yang29
2023-01-18 14:30 ` David Hildenbrand
2023-02-09 12:01   ` yang.yang29
2023-02-13 12:55     ` David Hildenbrand

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.