Re: [PATCH v9 2/5] ksm: count all zero pages placed by KSM

From: David Hildenbrand
Date: Wed May 24 2023 - 03:24:09 EST

On 24.05.23 07:57, Yang Yang wrote:
From: xu xin <xu.xin16@xxxxxxxxxx>

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 ksm_zero_pages under /sys/kernel/mm/ksm/ to show the number
of all zero pages placed by KSM. Meanwhile, we update the Documentation.

Signed-off-by: xu xin <xu.xin16@xxxxxxxxxx>
Suggested-by: David Hildenbrand <david@xxxxxxxxxx>
Cc: Claudio Imbrenda <imbrenda@xxxxxxxxxxxxx>
Cc: Xuexin Jiang <jiang.xuexin@xxxxxxxxxx>
Reviewed-by: Xiaokai Ran <ran.xiaokai@xxxxxxxxxx>
Reviewed-by: Yang Yang <yang.yang29@xxxxxxxxxx>
Documentation/admin-guide/mm/ksm.rst | 7 +++++++
include/linux/ksm.h | 12 ++++++++++++
mm/khugepaged.c | 2 ++
mm/ksm.c | 12 ++++++++++++
mm/memory.c | 5 ++++-
5 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst
index 7626392fe82c..6cc919dbfd55 100644
--- a/Documentation/admin-guide/mm/ksm.rst
+++ b/Documentation/admin-guide/mm/ksm.rst
@@ -173,6 +173,13 @@ stable_node_chains
the number of KSM pages that hit the ``max_page_sharing`` limit
number of duplicated KSM pages
+ how many zero pages that are still mapped into processes were mapped by
+ KSM when deduplicating.
+When ``use_zero_pages`` is/was enabled, the sum of ``pages_sharing`` +
+``ksm_zero_pages`` represents the actual number of pages saved by KSM.
+if ``use_zero_pages`` has never been enabled, ``ksm_zero_pages`` is 0.
A high ratio of ``pages_sharing`` to ``pages_shared`` indicates good
sharing, but a high ratio of ``pages_unshared`` to ``pages_sharing``
diff --git a/include/linux/ksm.h b/include/linux/ksm.h
index 4fd5f4a50bac..f2d98c53cfec 100644
--- a/include/linux/ksm.h
+++ b/include/linux/ksm.h
@@ -33,6 +33,14 @@ void __ksm_exit(struct mm_struct *mm);
#define is_ksm_zero_pte(pte) (is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte))
+extern unsigned long ksm_zero_pages;
+static inline void ksm_notify_unmap_zero_page(pte_t pte)
+ if (is_ksm_zero_pte(pte))
+ ksm_zero_pages--;
static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
int ret;
@@ -103,6 +111,10 @@ static inline void ksm_exit(struct mm_struct *mm)
#define is_ksm_zero_pte(pte) 0
+static inline void ksm_notify_unmap_zero_page(pte_t pte)

Having proposed that name ... I realize that we call this function whenever there might be a zeropage mapped (when we have !page after vm_normal_page()) -- but it could also not be the zeropage.

Not really able to come up with a better name :)

ksm_notify_maybe_unmap_zero_page ?

ksm_maybe_unmap_zero_page ?

Maybe someone else reading along has a better idea. In any case, the logic itself LGTM

Reviewed-by: David Hildenbrand <david@xxxxxxxxxx>


David / dhildenb