* [PATCH v5] ksm: add ksm involvement information for each process
@ 2025-01-10 9:40 xu.xin16
2025-01-11 0:45 ` Andrew Morton
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: xu.xin16 @ 2025-01-10 9:40 UTC (permalink / raw)
To: akpm
Cc: david, linux-kernel, wang.yaxin, linux-mm, linux-fsdevel, yang.yang29
From: xu xin <xu.xin16@zte.com.cn>
In /proc/<pid>/ksm_stat, Add two extra ksm involvement items including
KSM_mergeable and KSM_merge_any. It helps administrators to
better know the system's KSM behavior at process level.
ksm_merge_any: yes/no
whether the process'mm is added by prctl() into the candidate list
of KSM or not, and fully enabled at process level.
ksm_mergeable: yes/no
whether any VMAs of the process'mm are currently applicable to KSM.
Purpose
=======
These two items are just to improve the observability of KSM at process
level, so that users can know if a certain process has enable KSM.
For example, if without these two items, when we look at
/proc/<pid>/ksm_stat and there's no merging pages found, We are not sure
whether it is because KSM was not enabled or because KSM did not
successfully merge any pages.
Althrough "mg" in /proc/<pid>/smaps indicate VM_MERGEABLE, it's opaque
and not very obvious for non professionals.
Signed-off-by: xu xin <xu.xin16@zte.com.cn>
Cc: Wang Yaxin <wang.yaxin@zte.com.cn>
---
Changelog v4 -> v5:
1. Update the documentation.
2. Correct a comment sentence and add purpose statment in commit message.
---
Documentation/filesystems/proc.rst | 66 ++++++++++++++++++++++++++++++++++++++
fs/proc/base.c | 11 +++++++
include/linux/ksm.h | 1 +
mm/ksm.c | 19 +++++++++++
4 files changed, 97 insertions(+)
diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst
index 6a882c57a7e7..916f83203de0 100644
--- a/Documentation/filesystems/proc.rst
+++ b/Documentation/filesystems/proc.rst
@@ -48,6 +48,7 @@ fixes/update part 1.1 Stefani Seibold <stefani@seibold.net> June 9 2009
3.11 /proc/<pid>/patch_state - Livepatch patch operation state
3.12 /proc/<pid>/arch_status - Task architecture specific information
3.13 /proc/<pid>/fd - List of symlinks to open files
+ 3.14 /proc/<pid/ksm_stat - Information about the process' ksm status.
4 Configuring procfs
4.1 Mount options
@@ -2232,6 +2233,71 @@ The number of open files for the process is stored in 'size' member
of stat() output for /proc/<pid>/fd for fast access.
-------------------------------------------------------
+3.14 /proc/<pid/ksm_stat - Information about the process' ksm status
+--------------------------------------------------------------------
+When CONFIG_KSM is enabled, each process has this file which displays
+the information of ksm merging status.
+
+Example
+~~~~~~~
+
+::
+
+ / # cat /proc/self/ksm_stat
+ ksm_rmap_items 0
+ ksm_zero_pages 0
+ ksm_merging_pages 0
+ ksm_process_profit 0
+ ksm_merge_any: no
+ ksm_mergeable: no
+
+Description
+~~~~~~~~~~~
+
+ksm_rmap_items
+^^^^^^^^^^^^^^
+
+The number of ksm_rmap_item structure in use. The structure of
+ksm_rmap_item is to store the reverse mapping information for virtual
+addresses. KSM will generate a ksm_rmap_item for each ksm-scanned page
+of the process.
+
+ksm_zero_pages
+^^^^^^^^^^^^^^
+
+When /sys/kernel/mm/ksm/use_zero_pages is enabled, it represent how many
+empty pages are merged with kernel zero pages by KSM.
+
+ksm_merging_pages
+^^^^^^^^^^^^^^^^^
+
+It represents how many pages of this process are involved in KSM merging
+(not including ksm_zero_pages). It is the same with what
+/proc/<pid>/ksm_merging_pages shows.
+
+ksm_process_profit
+^^^^^^^^^^^^^^^^^^
+
+The profit that KSM brings (Saved bytes). KSM can save memory by merging
+identical pages, but also can consume additional memory, because it needs
+to generate a number of rmap_items to save each scanned page's brief rmap
+information. Some of these pages may be merged, but some may not be abled
+to be merged after being checked several times, which are unprofitable
+memory consumed.
+
+ksm_merge_any
+^^^^^^^^^^^^^
+
+It specifies whether the process'mm is added by prctl() into the candidate list
+of KSM or not, and KSM scanning is fully enabled at process level.
+
+ksm_mergeable
+^^^^^^^^^^^^^
+
+It specifies whether any VMAs of the process'mm are currently applicable to KSM.
+
+More information about KSM can be found at Documentation/admin-guide/mm/ksm.rst.
+
Chapter 4: Configuring procfs
=============================
diff --git a/fs/proc/base.c b/fs/proc/base.c
index 0edf14a9840e..a50b222a5917 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -3269,6 +3269,7 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns,
struct pid *pid, struct task_struct *task)
{
struct mm_struct *mm;
+ int ret = 0;
mm = get_task_mm(task);
if (mm) {
@@ -3276,6 +3277,16 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns,
seq_printf(m, "ksm_zero_pages %ld\n", mm_ksm_zero_pages(mm));
seq_printf(m, "ksm_merging_pages %lu\n", mm->ksm_merging_pages);
seq_printf(m, "ksm_process_profit %ld\n", ksm_process_profit(mm));
+ seq_printf(m, "ksm_merge_any: %s\n",
+ test_bit(MMF_VM_MERGE_ANY, &mm->flags) ? "yes" : "no");
+ ret = mmap_read_lock_killable(mm);
+ if (ret) {
+ mmput(mm);
+ return ret;
+ }
+ seq_printf(m, "ksm_mergeable: %s\n",
+ ksm_process_mergeable(mm) ? "yes" : "no");
+ mmap_read_unlock(mm);
mmput(mm);
}
diff --git a/include/linux/ksm.h b/include/linux/ksm.h
index 6a53ac4885bb..d73095b5cd96 100644
--- a/include/linux/ksm.h
+++ b/include/linux/ksm.h
@@ -93,6 +93,7 @@ void folio_migrate_ksm(struct folio *newfolio, struct folio *folio);
void collect_procs_ksm(const struct folio *folio, const struct page *page,
struct list_head *to_kill, int force_early);
long ksm_process_profit(struct mm_struct *);
+bool ksm_process_mergeable(struct mm_struct *mm);
#else /* !CONFIG_KSM */
diff --git a/mm/ksm.c b/mm/ksm.c
index 7ac59cde626c..be2eb1778225 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -3263,6 +3263,25 @@ static void wait_while_offlining(void)
#endif /* CONFIG_MEMORY_HOTREMOVE */
#ifdef CONFIG_PROC_FS
+/*
+ * The process is mergeable only if any VMA is currently
+ * applicable to KSM.
+ *
+ * The mmap lock must be held in read mode.
+ */
+bool ksm_process_mergeable(struct mm_struct *mm)
+{
+ struct vm_area_struct *vma;
+
+ mmap_assert_locked(mm);
+ VMA_ITERATOR(vmi, mm, 0);
+ for_each_vma(vmi, vma)
+ if (vma->vm_flags & VM_MERGEABLE)
+ return true;
+
+ return false;
+}
+
long ksm_process_profit(struct mm_struct *mm)
{
return (long)(mm->ksm_merging_pages + mm_ksm_zero_pages(mm)) * PAGE_SIZE -
--
2.15.2
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH v5] ksm: add ksm involvement information for each process
2025-01-10 9:40 [PATCH v5] ksm: add ksm involvement information for each process xu.xin16
@ 2025-01-11 0:45 ` Andrew Morton
2025-01-13 9:18 ` David Hildenbrand
2025-01-14 15:46 ` Mario Casquero
2 siblings, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2025-01-11 0:45 UTC (permalink / raw)
To: xu.xin16
Cc: david, linux-kernel, wang.yaxin, linux-mm, linux-fsdevel, yang.yang29
On Fri, 10 Jan 2025 17:40:34 +0800 (CST) <xu.xin16@zte.com.cn> wrote:
> From: xu xin <xu.xin16@zte.com.cn>
>
> In /proc/<pid>/ksm_stat, Add two extra ksm involvement items including
> KSM_mergeable and KSM_merge_any. It helps administrators to
> better know the system's KSM behavior at process level.
>
> ksm_merge_any: yes/no
> whether the process'mm is added by prctl() into the candidate list
> of KSM or not, and fully enabled at process level.
>
> ksm_mergeable: yes/no
> whether any VMAs of the process'mm are currently applicable to KSM.
>
> Purpose
> =======
> These two items are just to improve the observability of KSM at process
> level, so that users can know if a certain process has enable KSM.
>
> For example, if without these two items, when we look at
> /proc/<pid>/ksm_stat and there's no merging pages found, We are not sure
> whether it is because KSM was not enabled or because KSM did not
> successfully merge any pages.
>
> Althrough "mg" in /proc/<pid>/smaps indicate VM_MERGEABLE, it's opaque
> and not very obvious for non professionals.
Thanks, seems useful enough to me.
> + 3.14 /proc/<pid/ksm_stat - Information about the process' ksm status.
hm, I added this as a separate thing:
From: Andrew Morton <akpm@linux-foundation.org>
Subject: Documentation/filesystems/proc.rst: fix possessive form of "process"
Date: Fri Jan 10 04:38:41 PM PST 2025
The possessive form of "process" is "process's". Fix up various
misdirected attempts at this. Also reflow some paragraphs.
Cc: David Hildenbrand <david@redhat.com>
Cc: Wang Yaxin <wang.yaxin@zte.com.cn>
Cc: xu xin <xu.xin16@zte.com.cn>
Cc: Yang Yang <yang.yang29@zte.com.cn>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---
Documentation/filesystems/proc.rst | 36 +++++++++++++++------------
1 file changed, 20 insertions(+), 16 deletions(-)
--- a/Documentation/filesystems/proc.rst~documentation-filesystems-procrst-fix-possessive-form-of-process
+++ a/Documentation/filesystems/proc.rst
@@ -48,7 +48,7 @@ fixes/update part 1.1 Stefani Seibold <
3.11 /proc/<pid>/patch_state - Livepatch patch operation state
3.12 /proc/<pid>/arch_status - Task architecture specific information
3.13 /proc/<pid>/fd - List of symlinks to open files
- 3.14 /proc/<pid/ksm_stat - Information about the process' ksm status.
+ 3.14 /proc/<pid/ksm_stat - Information about the process's ksm status.
4 Configuring procfs
4.1 Mount options
@@ -485,14 +485,15 @@ Memory Area, or VMA) there is a series o
THPeligible: 0
VmFlags: rd ex mr mw me dw
-The first of these lines shows the same information as is displayed for the
-mapping in /proc/PID/maps. Following lines show the size of the mapping
-(size); the size of each page allocated when backing a VMA (KernelPageSize),
-which is usually the same as the size in the page table entries; the page size
-used by the MMU when backing a VMA (in most cases, the same as KernelPageSize);
-the amount of the mapping that is currently resident in RAM (RSS); the
-process' proportional share of this mapping (PSS); and the number of clean and
-dirty shared and private pages in the mapping.
+The first of these lines shows the same information as is displayed for
+the mapping in /proc/PID/maps. Following lines show the size of the
+mapping (size); the size of each page allocated when backing a VMA
+(KernelPageSize), which is usually the same as the size in the page table
+entries; the page size used by the MMU when backing a VMA (in most cases,
+the same as KernelPageSize); the amount of the mapping that is currently
+resident in RAM (RSS); the process's proportional share of this mapping
+(PSS); and the number of clean and dirty shared and private pages in the
+mapping.
The "proportional set size" (PSS) of a process is the count of pages it has
in memory, where each page is divided by the number of processes sharing it.
@@ -2233,8 +2234,8 @@ The number of open files for the process
of stat() output for /proc/<pid>/fd for fast access.
-------------------------------------------------------
-3.14 /proc/<pid/ksm_stat - Information about the process' ksm status
---------------------------------------------------------------------
+3.14 /proc/<pid/ksm_stat - Information about the process's ksm status
+---------------------------------------------------------------------
When CONFIG_KSM is enabled, each process has this file which displays
the information of ksm merging status.
@@ -2288,15 +2289,18 @@ memory consumed.
ksm_merge_any
^^^^^^^^^^^^^
-It specifies whether the process'mm is added by prctl() into the candidate list
-of KSM or not, and KSM scanning is fully enabled at process level.
+It specifies whether the process's mm is added by prctl() into the
+candidate list of KSM or not, and KSM scanning is fully enabled at process
+level.
ksm_mergeable
^^^^^^^^^^^^^
-It specifies whether any VMAs of the process'mm are currently applicable to KSM.
+It specifies whether any VMAs of the process's mm are currently applicable
+to KSM.
-More information about KSM can be found at Documentation/admin-guide/mm/ksm.rst.
+More information about KSM can be found at
+Documentation/admin-guide/mm/ksm.rst.
Chapter 4: Configuring procfs
@@ -2327,7 +2331,7 @@ arguments are now protected against loca
hidepid=invisible or hidepid=2 means hidepid=1 plus all /proc/<pid>/ will be
fully invisible to other users. It doesn't mean that it hides a fact whether a
process with a specific pid value exists (it can be learned by other means, e.g.
-by "kill -0 $PID"), but it hides process' uid and gid, which may be learned by
+by "kill -0 $PID"), but it hides process's uid and gid, which may be learned by
stat()'ing /proc/<pid>/ otherwise. It greatly complicates an intruder's task of
gathering information about running processes, whether some daemon runs with
elevated privileges, whether other user runs some sensitive program, whether
_
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH v5] ksm: add ksm involvement information for each process
2025-01-10 9:40 [PATCH v5] ksm: add ksm involvement information for each process xu.xin16
2025-01-11 0:45 ` Andrew Morton
@ 2025-01-13 9:18 ` David Hildenbrand
2025-01-14 15:46 ` Mario Casquero
2 siblings, 0 replies; 4+ messages in thread
From: David Hildenbrand @ 2025-01-13 9:18 UTC (permalink / raw)
To: xu.xin16, akpm
Cc: linux-kernel, wang.yaxin, linux-mm, linux-fsdevel, yang.yang29
On 10.01.25 10:40, xu.xin16@zte.com.cn wrote:
> From: xu xin <xu.xin16@zte.com.cn>
>
> In /proc/<pid>/ksm_stat, Add two extra ksm involvement items including
> KSM_mergeable and KSM_merge_any. It helps administrators to
> better know the system's KSM behavior at process level.
>
> ksm_merge_any: yes/no
> whether the process'mm is added by prctl() into the candidate list
> of KSM or not, and fully enabled at process level.
>
> ksm_mergeable: yes/no
> whether any VMAs of the process'mm are currently applicable to KSM.
>
> Purpose
> =======
> These two items are just to improve the observability of KSM at process
> level, so that users can know if a certain process has enable KSM.
s/enable/enabled/
>
> For example, if without these two items, when we look at
> /proc/<pid>/ksm_stat and there's no merging pages found, We are not sure
> whether it is because KSM was not enabled or because KSM did not
> successfully merge any pages.
>
> Althrough "mg" in /proc/<pid>/smaps indicate VM_MERGEABLE, it's opaque
s/Althrough/Although/
> and not very obvious for non professionals.
>
> Signed-off-by: xu xin <xu.xin16@zte.com.cn>
> Cc: Wang Yaxin <wang.yaxin@zte.com.cn>
> ---
> Changelog v4 -> v5:
> 1. Update the documentation.
> 2. Correct a comment sentence and add purpose statment in commit message.
> ---
> Documentation/filesystems/proc.rst | 66 ++++++++++++++++++++++++++++++++++++++
> fs/proc/base.c | 11 +++++++
> include/linux/ksm.h | 1 +
> mm/ksm.c | 19 +++++++++++
> 4 files changed, 97 insertions(+)
>
> diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst
> index 6a882c57a7e7..916f83203de0 100644
> --- a/Documentation/filesystems/proc.rst
> +++ b/Documentation/filesystems/proc.rst
> @@ -48,6 +48,7 @@ fixes/update part 1.1 Stefani Seibold <stefani@seibold.net> June 9 2009
> 3.11 /proc/<pid>/patch_state - Livepatch patch operation state
> 3.12 /proc/<pid>/arch_status - Task architecture specific information
> 3.13 /proc/<pid>/fd - List of symlinks to open files
> + 3.14 /proc/<pid/ksm_stat - Information about the process' ksm status.
>
> 4 Configuring procfs
> 4.1 Mount options
> @@ -2232,6 +2233,71 @@ The number of open files for the process is stored in 'size' member
> of stat() output for /proc/<pid>/fd for fast access.
> -------------------------------------------------------
>
> +3.14 /proc/<pid/ksm_stat - Information about the process' ksm status
> +--------------------------------------------------------------------
> +When CONFIG_KSM is enabled, each process has this file which displays
> +the information of ksm merging status.
> +
> +Example
> +~~~~~~~
> +
> +::
> +
> + / # cat /proc/self/ksm_stat
> + ksm_rmap_items 0
> + ksm_zero_pages 0
> + ksm_merging_pages 0
> + ksm_process_profit 0
> + ksm_merge_any: no
> + ksm_mergeable: no
> +
> +Description
> +~~~~~~~~~~~
> +
> +ksm_rmap_items
> +^^^^^^^^^^^^^^
> +
> +The number of ksm_rmap_item structure in use. The structure of
structures
> +ksm_rmap_item is to store the reverse mapping information for virtual
is to store -> stores?
> +addresses. KSM will generate a ksm_rmap_item for each ksm-scanned page
> +of the process.
Is it really each
> +
> +ksm_zero_pages
> +^^^^^^^^^^^^^^
> +
> +When /sys/kernel/mm/ksm/use_zero_pages is enabled, it represent how many
> +empty pages are merged with kernel zero pages by KSM.
> +
> +ksm_merging_pages
> +^^^^^^^^^^^^^^^^^
> +
> +It represents how many pages of this process are involved in KSM merging
> +(not including ksm_zero_pages). It is the same with what
> +/proc/<pid>/ksm_merging_pages shows.
> +> +ksm_process_profit
> +^^^^^^^^^^^^^^^^^^
> +
> +The profit that KSM brings (Saved bytes). KSM can save memory by merging
> +identical pages, but also can consume additional memory, because it needs
> +to generate a number of rmap_items to save each scanned page's brief rmap
> +information. Some of these pages may be merged, but some may not be abled
> +to be merged after being checked several times, which are unprofitable
> +memory consumed.
> +
> +ksm_merge_any
> +^^^^^^^^^^^^^
> +
> +It specifies whether the process'mm is added by prctl() into the candidate list
Shows whether ... ?
> +of KSM or not, and KSM scanning is fully enabled at process level.
> +
> +ksm_mergeable
> +^^^^^^^^^^^^^
> +
> +It specifies whether any VMAs of the process'mm are currently applicable to KSM.
Shows whether ... ?
"any VMA"
Apart from that LGTM
Acked-by: David Hildenbrand <david@redhat.com>
--
Cheers,
David / dhildenb
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v5] ksm: add ksm involvement information for each process
2025-01-10 9:40 [PATCH v5] ksm: add ksm involvement information for each process xu.xin16
2025-01-11 0:45 ` Andrew Morton
2025-01-13 9:18 ` David Hildenbrand
@ 2025-01-14 15:46 ` Mario Casquero
2 siblings, 0 replies; 4+ messages in thread
From: Mario Casquero @ 2025-01-14 15:46 UTC (permalink / raw)
To: xu.xin16
Cc: akpm, david, linux-kernel, wang.yaxin, linux-mm, linux-fsdevel,
yang.yang29
This patch has been successfully tested. After starting the KSM and
obtaining the PID of a process, e.g. qemu-kvm PID, now the new items
can be checked.
echo 1 > /sys/kernel/mm/ksm/run
pid=$(pgrep -f "qemu-kvm")
cat /proc/$pid/ksm_stat
ksm_rmap_items 70692
ksm_zero_pages 0
ksm_merging_pages 4211
ksm_process_profit 12723968
ksm_merge_any: no
ksm_mergeable: yes
Tested-by: Mario Casquero <mcasquer@redhat.com>
On Fri, Jan 10, 2025 at 10:52 AM <xu.xin16@zte.com.cn> wrote:
>
> From: xu xin <xu.xin16@zte.com.cn>
>
> In /proc/<pid>/ksm_stat, Add two extra ksm involvement items including
> KSM_mergeable and KSM_merge_any. It helps administrators to
> better know the system's KSM behavior at process level.
>
> ksm_merge_any: yes/no
> whether the process'mm is added by prctl() into the candidate list
> of KSM or not, and fully enabled at process level.
>
> ksm_mergeable: yes/no
> whether any VMAs of the process'mm are currently applicable to KSM.
>
> Purpose
> =======
> These two items are just to improve the observability of KSM at process
> level, so that users can know if a certain process has enable KSM.
>
> For example, if without these two items, when we look at
> /proc/<pid>/ksm_stat and there's no merging pages found, We are not sure
> whether it is because KSM was not enabled or because KSM did not
> successfully merge any pages.
>
> Althrough "mg" in /proc/<pid>/smaps indicate VM_MERGEABLE, it's opaque
> and not very obvious for non professionals.
>
> Signed-off-by: xu xin <xu.xin16@zte.com.cn>
> Cc: Wang Yaxin <wang.yaxin@zte.com.cn>
> ---
> Changelog v4 -> v5:
> 1. Update the documentation.
> 2. Correct a comment sentence and add purpose statment in commit message.
> ---
> Documentation/filesystems/proc.rst | 66 ++++++++++++++++++++++++++++++++++++++
> fs/proc/base.c | 11 +++++++
> include/linux/ksm.h | 1 +
> mm/ksm.c | 19 +++++++++++
> 4 files changed, 97 insertions(+)
>
> diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst
> index 6a882c57a7e7..916f83203de0 100644
> --- a/Documentation/filesystems/proc.rst
> +++ b/Documentation/filesystems/proc.rst
> @@ -48,6 +48,7 @@ fixes/update part 1.1 Stefani Seibold <stefani@seibold.net> June 9 2009
> 3.11 /proc/<pid>/patch_state - Livepatch patch operation state
> 3.12 /proc/<pid>/arch_status - Task architecture specific information
> 3.13 /proc/<pid>/fd - List of symlinks to open files
> + 3.14 /proc/<pid/ksm_stat - Information about the process' ksm status.
>
> 4 Configuring procfs
> 4.1 Mount options
> @@ -2232,6 +2233,71 @@ The number of open files for the process is stored in 'size' member
> of stat() output for /proc/<pid>/fd for fast access.
> -------------------------------------------------------
>
> +3.14 /proc/<pid/ksm_stat - Information about the process' ksm status
> +--------------------------------------------------------------------
> +When CONFIG_KSM is enabled, each process has this file which displays
> +the information of ksm merging status.
> +
> +Example
> +~~~~~~~
> +
> +::
> +
> + / # cat /proc/self/ksm_stat
> + ksm_rmap_items 0
> + ksm_zero_pages 0
> + ksm_merging_pages 0
> + ksm_process_profit 0
> + ksm_merge_any: no
> + ksm_mergeable: no
> +
> +Description
> +~~~~~~~~~~~
> +
> +ksm_rmap_items
> +^^^^^^^^^^^^^^
> +
> +The number of ksm_rmap_item structure in use. The structure of
> +ksm_rmap_item is to store the reverse mapping information for virtual
> +addresses. KSM will generate a ksm_rmap_item for each ksm-scanned page
> +of the process.
> +
> +ksm_zero_pages
> +^^^^^^^^^^^^^^
> +
> +When /sys/kernel/mm/ksm/use_zero_pages is enabled, it represent how many
> +empty pages are merged with kernel zero pages by KSM.
> +
> +ksm_merging_pages
> +^^^^^^^^^^^^^^^^^
> +
> +It represents how many pages of this process are involved in KSM merging
> +(not including ksm_zero_pages). It is the same with what
> +/proc/<pid>/ksm_merging_pages shows.
> +
> +ksm_process_profit
> +^^^^^^^^^^^^^^^^^^
> +
> +The profit that KSM brings (Saved bytes). KSM can save memory by merging
> +identical pages, but also can consume additional memory, because it needs
> +to generate a number of rmap_items to save each scanned page's brief rmap
> +information. Some of these pages may be merged, but some may not be abled
> +to be merged after being checked several times, which are unprofitable
> +memory consumed.
> +
> +ksm_merge_any
> +^^^^^^^^^^^^^
> +
> +It specifies whether the process'mm is added by prctl() into the candidate list
> +of KSM or not, and KSM scanning is fully enabled at process level.
> +
> +ksm_mergeable
> +^^^^^^^^^^^^^
> +
> +It specifies whether any VMAs of the process'mm are currently applicable to KSM.
> +
> +More information about KSM can be found at Documentation/admin-guide/mm/ksm.rst.
> +
>
> Chapter 4: Configuring procfs
> =============================
> diff --git a/fs/proc/base.c b/fs/proc/base.c
> index 0edf14a9840e..a50b222a5917 100644
> --- a/fs/proc/base.c
> +++ b/fs/proc/base.c
> @@ -3269,6 +3269,7 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns,
> struct pid *pid, struct task_struct *task)
> {
> struct mm_struct *mm;
> + int ret = 0;
>
> mm = get_task_mm(task);
> if (mm) {
> @@ -3276,6 +3277,16 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns,
> seq_printf(m, "ksm_zero_pages %ld\n", mm_ksm_zero_pages(mm));
> seq_printf(m, "ksm_merging_pages %lu\n", mm->ksm_merging_pages);
> seq_printf(m, "ksm_process_profit %ld\n", ksm_process_profit(mm));
> + seq_printf(m, "ksm_merge_any: %s\n",
> + test_bit(MMF_VM_MERGE_ANY, &mm->flags) ? "yes" : "no");
> + ret = mmap_read_lock_killable(mm);
> + if (ret) {
> + mmput(mm);
> + return ret;
> + }
> + seq_printf(m, "ksm_mergeable: %s\n",
> + ksm_process_mergeable(mm) ? "yes" : "no");
> + mmap_read_unlock(mm);
> mmput(mm);
> }
>
> diff --git a/include/linux/ksm.h b/include/linux/ksm.h
> index 6a53ac4885bb..d73095b5cd96 100644
> --- a/include/linux/ksm.h
> +++ b/include/linux/ksm.h
> @@ -93,6 +93,7 @@ void folio_migrate_ksm(struct folio *newfolio, struct folio *folio);
> void collect_procs_ksm(const struct folio *folio, const struct page *page,
> struct list_head *to_kill, int force_early);
> long ksm_process_profit(struct mm_struct *);
> +bool ksm_process_mergeable(struct mm_struct *mm);
>
> #else /* !CONFIG_KSM */
>
> diff --git a/mm/ksm.c b/mm/ksm.c
> index 7ac59cde626c..be2eb1778225 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -3263,6 +3263,25 @@ static void wait_while_offlining(void)
> #endif /* CONFIG_MEMORY_HOTREMOVE */
>
> #ifdef CONFIG_PROC_FS
> +/*
> + * The process is mergeable only if any VMA is currently
> + * applicable to KSM.
> + *
> + * The mmap lock must be held in read mode.
> + */
> +bool ksm_process_mergeable(struct mm_struct *mm)
> +{
> + struct vm_area_struct *vma;
> +
> + mmap_assert_locked(mm);
> + VMA_ITERATOR(vmi, mm, 0);
> + for_each_vma(vmi, vma)
> + if (vma->vm_flags & VM_MERGEABLE)
> + return true;
> +
> + return false;
> +}
> +
> long ksm_process_profit(struct mm_struct *mm)
> {
> return (long)(mm->ksm_merging_pages + mm_ksm_zero_pages(mm)) * PAGE_SIZE -
> --
> 2.15.2
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-01-14 15:46 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-01-10 9:40 [PATCH v5] ksm: add ksm involvement information for each process xu.xin16
2025-01-11 0:45 ` Andrew Morton
2025-01-13 9:18 ` David Hildenbrand
2025-01-14 15:46 ` Mario Casquero
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox