Unverified Commit dcc34901 authored by openeuler-ci-bot's avatar openeuler-ci-bot Committed by Gitee
Browse files

!790 mm: enable ksm per process and cgroup

Merge Pull Request from: @sun_nanyong 
 
patch 1~6: backport mainline patchset(mm: process/cgroup ksm
support)and patches it depends on.
patch 7:Add control file "memory.ksm" to enable ksm per cgroup. 
 
Link:https://gitee.com/openeuler/kernel/pulls/790

 

Reviewed-by: default avatarJialin Zhang <zhangjialin11@huawei.com>
Reviewed-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
Signed-off-by: default avatarJialin Zhang <zhangjialin11@huawei.com>
parents 665edcec 0f6fb357
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -51,3 +51,11 @@ Description: Control merging pages across different NUMA nodes.

		When it is set to 0 only pages from the same node are merged,
		otherwise pages from all nodes can be merged together (default).

What:		/sys/kernel/mm/ksm/general_profit
Date:		April 2023
KernelVersion:  6.4
Contact:	Linux memory management mailing list <linux-mm@kvack.org>
Description:	Measure how effective KSM is.
		general_profit: how effective is KSM. The formula for the
		calculation is in Documentation/admin-guide/mm/ksm.rst.
+1 −0
Original line number Diff line number Diff line
@@ -99,6 +99,7 @@ Brief summary of control files.
 memory.kmem.tcp.failcnt             show the number of tcp buf memory usage
				     hits limits
 memory.kmem.tcp.max_usage_in_bytes  show max tcp buf memory usage recorded
 memory.ksm                          set/show ksm merge any mode
==================================== ==========================================

1. History
+40 −0
Original line number Diff line number Diff line
@@ -159,6 +159,8 @@ stable_node_chains_prune_millisecs

The effectiveness of KSM and MADV_MERGEABLE is shown in ``/sys/kernel/mm/ksm/``:

general_profit
        how effective is KSM. The calculation is explained below.
pages_shared
        how many shared pages are being used
pages_sharing
@@ -184,6 +186,44 @@ The maximum possible ``pages_sharing/pages_shared`` ratio is limited by the
``max_page_sharing`` tunable. To increase the ratio ``max_page_sharing`` must
be increased accordingly.


Monitoring KSM profit
=====================

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.

1) How to determine whether KSM save memory or consume memory in system-wide
   range? Here is a simple approximate calculation for reference::

	general_profit =~ pages_sharing * sizeof(page) - (all_rmap_items) *
			  sizeof(rmap_item);

   where all_rmap_items can be easily obtained by summing ``pages_sharing``,
   ``pages_shared``, ``pages_unshared`` and ``pages_volatile``.

2) The KSM profit inner a single process can be similarly obtained by the
   following approximate calculation::

	process_profit =~ ksm_merging_pages * sizeof(page) -
			  ksm_rmap_items * sizeof(rmap_item).

   where ksm_merging_pages is shown under the directory ``/proc/<pid>/``,
   and ksm_rmap_items is shown in ``/proc/<pid>/ksm_stat``. The process profit
   is also shown in ``/proc/<pid>/ksm_stat`` as ksm_process_profit.

From the perspective of application, a high ratio of ``ksm_rmap_items`` to
``ksm_merging_pages`` means a bad madvise-applied policy, so developers or
administrators have to rethink how to change madvise policy. Giving an example
for reference, a page's size is usually 4K, and the rmap_item's size is
separately 32B on 32-bit CPU architecture and 64B on 64-bit CPU architecture.
so if the ``ksm_rmap_items/ksm_merging_pages`` ratio exceeds 64 on 64-bit CPU
or exceeds 128 on 32-bit CPU, then the app's madvise policy should be dropped,
because the ksm profit is approximately zero or negative.

--
Izik Eidus,
Hugh Dickins, 17 Nov 2009
+7 −0
Original line number Diff line number Diff line
@@ -2573,6 +2573,13 @@ int gmap_mark_unmergeable(void)
	struct vm_area_struct *vma;
	int ret;

	/*
	 * Make sure to disable KSM (if enabled for the whole process or
	 * individual VMAs). Note that nothing currently hinders user space
	 * from re-enabling it.
	 */
	clear_bit(MMF_VM_MERGE_ANY, &mm->flags);

	for (vma = mm->mmap; vma; vma = vma->vm_next) {
		ret = ksm_madvise(vma, vma->vm_start, vma->vm_end,
				  MADV_UNMERGEABLE, &vma->vm_flags);
+40 −0
Original line number Diff line number Diff line
@@ -97,6 +97,7 @@
#include <linux/time_namespace.h>
#include <linux/resctrl.h>
#include <linux/share_pool.h>
#include <linux/ksm.h>
#include <trace/events/oom.h>
#include "internal.h"
#include "fd.h"
@@ -3341,6 +3342,37 @@ static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
}
#endif /* CONFIG_LIVEPATCH */

#ifdef CONFIG_KSM
static int proc_pid_ksm_merging_pages(struct seq_file *m, struct pid_namespace *ns,
				struct pid *pid, struct task_struct *task)
{
	struct mm_struct *mm;

	mm = get_task_mm(task);
	if (mm) {
		seq_printf(m, "%lu\n", mm->ksm_merging_pages);
		mmput(mm);
	}

	return 0;
}
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;

	mm = get_task_mm(task);
	if (mm) {
		seq_printf(m, "ksm_rmap_items %lu\n", mm->ksm_rmap_items);
		seq_printf(m, "ksm_merging_pages %lu\n", mm->ksm_merging_pages);
		seq_printf(m, "ksm_process_profit %ld\n", ksm_process_profit(mm));
		mmput(mm);
	}

	return 0;
}
#endif /* CONFIG_KSM */

#ifdef CONFIG_STACKLEAK_METRICS
static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
				struct pid *pid, struct task_struct *task)
@@ -3482,6 +3514,10 @@ static const struct pid_entry tgid_base_stuff[] = {
#ifdef CONFIG_ASCEND_SHARE_POOL
	ONE("sp_group", 0444, proc_sp_group_state),
#endif
#ifdef CONFIG_KSM
	ONE("ksm_merging_pages",  S_IRUSR, proc_pid_ksm_merging_pages),
	ONE("ksm_stat",  S_IRUSR, proc_pid_ksm_stat),
#endif
};

static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
@@ -3893,6 +3929,10 @@ static const struct pid_entry tid_base_stuff[] = {
#ifdef CONFIG_QOS_SCHED_DYNAMIC_AFFINITY
	REG("preferred_cpuset", 0644, proc_preferred_cpuset_operations),
#endif
#ifdef CONFIG_KSM
	ONE("ksm_merging_pages",  S_IRUSR, proc_pid_ksm_merging_pages),
	ONE("ksm_stat",  S_IRUSR, proc_pid_ksm_stat),
#endif
};

static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Loading