Skip to content

Commit 3ab76c7

Browse files
xu xinakpm00
authored andcommitted
ksm: add ksm involvement information for each process
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 enabled 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. Although "mg" in /proc/<pid>/smaps indicate VM_MERGEABLE, it's opaque and not very obvious for non professionals. [[email protected]: wording tweaks, per David and akpm] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: xu xin <[email protected]> Acked-by: David Hildenbrand <[email protected]> Tested-by: Mario Casquero <[email protected]> Cc: Wang Yaxin <[email protected]> Cc: Yang Yang <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 8f65ac0 commit 3ab76c7

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

Documentation/filesystems/proc.rst

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ fixes/update part 1.1 Stefani Seibold <[email protected]> June 9 2009
4848
3.11 /proc/<pid>/patch_state - Livepatch patch operation state
4949
3.12 /proc/<pid>/arch_status - Task architecture specific information
5050
3.13 /proc/<pid>/fd - List of symlinks to open files
51+
3.14 /proc/<pid/ksm_stat - Information about the process' ksm status.
5152
5253
4 Configuring procfs
5354
4.1 Mount options
@@ -2232,6 +2233,73 @@ The number of open files for the process is stored in 'size' member
22322233
of stat() output for /proc/<pid>/fd for fast access.
22332234
-------------------------------------------------------
22342235

2236+
3.14 /proc/<pid/ksm_stat - Information about the process' ksm status
2237+
--------------------------------------------------------------------
2238+
When CONFIG_KSM is enabled, each process has this file which displays
2239+
the information of ksm merging status.
2240+
2241+
Example
2242+
~~~~~~~
2243+
2244+
::
2245+
2246+
/ # cat /proc/self/ksm_stat
2247+
ksm_rmap_items 0
2248+
ksm_zero_pages 0
2249+
ksm_merging_pages 0
2250+
ksm_process_profit 0
2251+
ksm_merge_any: no
2252+
ksm_mergeable: no
2253+
2254+
Description
2255+
~~~~~~~~~~~
2256+
2257+
ksm_rmap_items
2258+
^^^^^^^^^^^^^^
2259+
2260+
The number of ksm_rmap_item structures in use. The structure
2261+
ksm_rmap_item stores the reverse mapping information for virtual
2262+
addresses. KSM will generate a ksm_rmap_item for each ksm-scanned page of
2263+
the process.
2264+
2265+
ksm_zero_pages
2266+
^^^^^^^^^^^^^^
2267+
2268+
When /sys/kernel/mm/ksm/use_zero_pages is enabled, it represent how many
2269+
empty pages are merged with kernel zero pages by KSM.
2270+
2271+
ksm_merging_pages
2272+
^^^^^^^^^^^^^^^^^
2273+
2274+
It represents how many pages of this process are involved in KSM merging
2275+
(not including ksm_zero_pages). It is the same with what
2276+
/proc/<pid>/ksm_merging_pages shows.
2277+
2278+
ksm_process_profit
2279+
^^^^^^^^^^^^^^^^^^
2280+
2281+
The profit that KSM brings (Saved bytes). KSM can save memory by merging
2282+
identical pages, but also can consume additional memory, because it needs
2283+
to generate a number of rmap_items to save each scanned page's brief rmap
2284+
information. Some of these pages may be merged, but some may not be abled
2285+
to be merged after being checked several times, which are unprofitable
2286+
memory consumed.
2287+
2288+
ksm_merge_any
2289+
^^^^^^^^^^^^^
2290+
2291+
It specifies whether the process'mm is added by prctl() into the candidate list
2292+
of KSM or not, and if KSM scanning is fully enabled at process level.
2293+
2294+
ksm_mergeable
2295+
^^^^^^^^^^^^^
2296+
2297+
It specifies whether any VMAs of the process'mm are currently applicable
2298+
to KSM.
2299+
2300+
More information about KSM can be found in
2301+
Documentation/admin-guide/mm/ksm.rst.
2302+
22352303

22362304
Chapter 4: Configuring procfs
22372305
=============================

fs/proc/base.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,13 +3269,24 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns,
32693269
struct pid *pid, struct task_struct *task)
32703270
{
32713271
struct mm_struct *mm;
3272+
int ret = 0;
32723273

32733274
mm = get_task_mm(task);
32743275
if (mm) {
32753276
seq_printf(m, "ksm_rmap_items %lu\n", mm->ksm_rmap_items);
32763277
seq_printf(m, "ksm_zero_pages %ld\n", mm_ksm_zero_pages(mm));
32773278
seq_printf(m, "ksm_merging_pages %lu\n", mm->ksm_merging_pages);
32783279
seq_printf(m, "ksm_process_profit %ld\n", ksm_process_profit(mm));
3280+
seq_printf(m, "ksm_merge_any: %s\n",
3281+
test_bit(MMF_VM_MERGE_ANY, &mm->flags) ? "yes" : "no");
3282+
ret = mmap_read_lock_killable(mm);
3283+
if (ret) {
3284+
mmput(mm);
3285+
return ret;
3286+
}
3287+
seq_printf(m, "ksm_mergeable: %s\n",
3288+
ksm_process_mergeable(mm) ? "yes" : "no");
3289+
mmap_read_unlock(mm);
32793290
mmput(mm);
32803291
}
32813292

include/linux/ksm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ void folio_migrate_ksm(struct folio *newfolio, struct folio *folio);
9393
void collect_procs_ksm(const struct folio *folio, const struct page *page,
9494
struct list_head *to_kill, int force_early);
9595
long ksm_process_profit(struct mm_struct *);
96+
bool ksm_process_mergeable(struct mm_struct *mm);
9697

9798
#else /* !CONFIG_KSM */
9899

mm/ksm.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3262,6 +3262,25 @@ static void wait_while_offlining(void)
32623262
#endif /* CONFIG_MEMORY_HOTREMOVE */
32633263

32643264
#ifdef CONFIG_PROC_FS
3265+
/*
3266+
* The process is mergeable only if any VMA is currently
3267+
* applicable to KSM.
3268+
*
3269+
* The mmap lock must be held in read mode.
3270+
*/
3271+
bool ksm_process_mergeable(struct mm_struct *mm)
3272+
{
3273+
struct vm_area_struct *vma;
3274+
3275+
mmap_assert_locked(mm);
3276+
VMA_ITERATOR(vmi, mm, 0);
3277+
for_each_vma(vmi, vma)
3278+
if (vma->vm_flags & VM_MERGEABLE)
3279+
return true;
3280+
3281+
return false;
3282+
}
3283+
32653284
long ksm_process_profit(struct mm_struct *mm)
32663285
{
32673286
return (long)(mm->ksm_merging_pages + mm_ksm_zero_pages(mm)) * PAGE_SIZE -

0 commit comments

Comments
 (0)