From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-4.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from gnuweeb.org by gnuweeb.org with LMTP id 6LUhOz1xfWKN/B4ALGQddQ (envelope-from ) for ; Thu, 12 May 2022 20:42:37 +0000 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by gnuweeb.org (Postfix) with ESMTPS id CD0C67F62C for ; Thu, 12 May 2022 20:42:37 +0000 (UTC) Authentication-Results: gnuweeb.org; dkim=pass (1024-bit key; unprotected) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=QknJ9F0Q; dkim-atps=neutral Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 032A161E21; Thu, 12 May 2022 20:42:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 57AA6C385B8; Thu, 12 May 2022 20:42:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1652388126; bh=1WEaTGzamAXHrPiiRBToJDUr5d4JVKtJT6mWqTEBOuY=; h=Date:To:From:Subject:From; b=QknJ9F0QxN2KCObDzCXyCkFCE3Ygjt7lK9/OcsjY5hT2Kp+QLGDZgPmiGHjIu7mBx Ku4tfyDCJ96KszmpZfLzHg6V9jQwPumHA+XUJLsnRta9agZ8mr9xhbZldrM9JD1S7F pTrBS3UECJc8Qqvv+QP5fVhNw2hmCT/rbBe4/gwI= Date: Thu, 12 May 2022 13:42:05 -0700 To: mm-commits@vger.kernel.org,zhang.yunkai@zte.com.cn,yang.yang29@zte.com.cn,willy@infradead.org,wang.yong12@zte.com.cn,ran.xiaokai@zte.com.cn,ammarfaizi2@gnuweeb.org,xu.xin16@zte.com.cn,akpm@linux-foundation.org,linux-kernel@vger.kernel.org,linux-mm@kvack.org From: Andrew Morton Subject: + mm-ksm-introduce-ksm_force-for-each-process.patch added to mm-unstable branch Message-Id: <20220512204206.57AA6C385B8@smtp.kernel.org> List-Id: The patch titled Subject: mm/ksm: introduce ksm_force for each process has been added to the -mm mm-unstable branch. Its filename is mm-ksm-introduce-ksm_force-for-each-process.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-ksm-introduce-ksm_force-for-each-process.patch This patch will later appear in the mm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via the mm-everything branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there every 2-3 working days ------------------------------------------------------ From: cgel.zte@gmail.com Subject: mm/ksm: introduce ksm_force for each process To use KSM, we have to explicitly call madvise() in application code, which means installed apps on OS needs to be uninstall and source code needs to be modified. It is inconvenient. In order to change this situation, We add a new proc file ksm_force under /proc// to support turning on/off KSM scanning of a process's mm dynamically. If ksm_force is set to 1, force all anonymous and 'qualified' VMAs of this mm to be involved in KSM scanning without explicitly calling madvise to mark VMA as MADV_MERGEABLE. But It is effective only when the klob of /sys/kernel/mm/ksm/run is set as 1. If ksm_force is set to 0, cancel the feature of ksm_force of this process and unmerge those merged pages belonging to VMAs which is not madvised as MADV_MERGEABLE of this process, but leave MADV_MERGEABLE areas merged. Link: https://lkml.kernel.org/r/20220512070347.1628163-1-xu.xin16@zte.com.cn Signed-off-by: xu xin Reviewed-by: Yang Yang Reviewed-by: Ran Xiaokai Reviewed-by: wangyong Reviewed-by: Yunkai Zhang Suggested-by: Matthew Wilcox Suggested-by: Ammar Faizi Signed-off-by: Andrew Morton --- Documentation/admin-guide/mm/ksm.rst | 19 ++++ Documentation/filesystems/proc.rst | 17 ++++ fs/proc/base.c | 97 +++++++++++++++++++++++++ include/linux/mm_types.h | 9 ++ mm/ksm.c | 32 +++++++- 5 files changed, 171 insertions(+), 3 deletions(-) --- a/Documentation/admin-guide/mm/ksm.rst~mm-ksm-introduce-ksm_force-for-each-process +++ a/Documentation/admin-guide/mm/ksm.rst @@ -32,7 +32,7 @@ are swapped back in: ksmd must rediscove Controlling KSM with madvise ============================ -KSM only operates on those areas of address space which an application +KSM can operates on those areas of address space which an application has advised to be likely candidates for merging, by using the madvise(2) system call:: @@ -70,6 +70,23 @@ Applications should be considerate in th restricting its use to areas likely to benefit. KSM's scans may use a lot of processing power: some installations will disable KSM for that reason. +Controlling KSM with procfs +=========================== + +KSM can also operate on anonymous areas of address space of those processes's +knob ``/proc//ksm_force`` is on, even if app codes doesn't call madvise() +explicitly to advise specific areas as MADV_MERGEABLE. + +You can set ksm_force to 1 to force all anonymous and qualified VMAs of +this process to be involved in KSM scanning. + e.g. ``echo 1 > /proc//ksm_force`` + +You can also set ksm_force to 0 to cancel that force feature of this process +and unmerge those merged pages which belongs to those VMAs not marked as +MADV_MERGEABLE of this process. But that still leave those pages belonging to +VMAs marked as MADV_MERGEABLE merged (fallback to the default state). + e.g. ``echo 0 > /proc//ksm_force`` + .. _ksm_sysfs: KSM daemon sysfs interface --- a/Documentation/filesystems/proc.rst~mm-ksm-introduce-ksm_force-for-each-process +++ a/Documentation/filesystems/proc.rst @@ -47,6 +47,7 @@ fixes/update part 1.1 Stefani Seibold < 3.10 /proc//timerslack_ns - Task timerslack value 3.11 /proc//patch_state - Livepatch patch operation state 3.12 /proc//arch_status - Task architecture specific information + 3.13 /proc//ksm_force - Setting of mandatory involvement in KSM 4 Configuring procfs 4.1 Mount options @@ -2218,6 +2219,22 @@ AVX512_elapsed_ms the task is unlikely an AVX512 user, but depends on the workload and the scheduling scenario, it also could be a false negative mentioned above. +3.13 /proc//ksm_force - Setting of mandatory involvement in KSM +----------------------------------------------------------------------- +When CONFIG_KSM is enabled, this file can be used to specify if this +process's anonymous memory can be involved in KSM scanning without app codes +explicitly calling madvise to mark memory address as MADV_MERGEABLE. + +If writing 1 to this file, the kernel will force all anonymous and qualified +memory to be involved in KSM scanning without explicitly calling madvise to +mark memory address as MADV_MERGEABLE. But that is effective only when the +klob of '/sys/kernel/mm/ksm/run' is set as 1. + +If writing 0 to this file, the mandatory KSM feature of this process's will +be cancelled and unmerge those merged pages which belongs to those areas not +marked as MADV_MERGEABLE of this process, but leave those pages belonging to +areas marked as MADV_MERGEABLE merged (fallback to the default state). + Chapter 4: Configuring procfs ============================= --- a/fs/proc/base.c~mm-ksm-introduce-ksm_force-for-each-process +++ a/fs/proc/base.c @@ -96,6 +96,7 @@ #include #include #include +#include #include #include "internal.h" #include "fd.h" @@ -3171,6 +3172,100 @@ static int proc_pid_ksm_merging_pages(st return 0; } + +static ssize_t ksm_force_read(struct file *file, char __user *buf, size_t count, + loff_t *ppos) +{ + struct task_struct *task; + struct mm_struct *mm; + char buffer[PROC_NUMBUF]; + ssize_t len; + int ret; + + task = get_proc_task(file_inode(file)); + if (!task) + return -ESRCH; + + mm = get_task_mm(task); + ret = 0; + if (mm) { + len = snprintf(buffer, sizeof(buffer), "%d\n", mm->ksm_force); + ret = simple_read_from_buffer(buf, count, ppos, buffer, len); + mmput(mm); + } + + return ret; +} + +static ssize_t ksm_force_write(struct file *file, const char __user *buf, + size_t count, loff_t *ppos) +{ + struct task_struct *task; + struct mm_struct *mm; + char buffer[PROC_NUMBUF]; + int force; + int err = 0; + int str_len; + + if (count > sizeof(buffer) - 1) { + count = sizeof(buffer) - 1; + } + + str_len = strncpy_from_user(buffer, buf, count); + if (str_len < 0) + return -EFAULT; + buffer[str_len] = '\0'; + + err = kstrtoint(strstrip(buffer), 0, &force); + if (err) + return err; + + if (force != 0 && force != 1) + return -EINVAL; + + task = get_proc_task(file_inode(file)); + if (!task) + return -ESRCH; + + mm = get_task_mm(task); + if (!mm) + goto out_put_task; + + if (mm->ksm_force != force) { + if (mmap_write_lock_killable(mm)) { + err = -EINTR; + goto out_mmput; + } + + if (force == 0) + mm->ksm_force = force; + else { + /* + * Force anonymous pages of this mm to be involved in KSM merging + * without explicitly calling madvise. + */ + if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) + err = __ksm_enter(mm); + if (!err) + mm->ksm_force = force; + } + + mmap_write_unlock(mm); + } + +out_mmput: + mmput(mm); +out_put_task: + put_task_struct(task); + + return err < 0 ? err : count; +} + +static const struct file_operations proc_pid_ksm_force_operations = { + .read = ksm_force_read, + .write = ksm_force_write, + .llseek = generic_file_llseek, +}; #endif /* CONFIG_KSM */ #ifdef CONFIG_STACKLEAK_METRICS @@ -3306,6 +3401,7 @@ static const struct pid_entry tgid_base_ #endif #ifdef CONFIG_KSM ONE("ksm_merging_pages", S_IRUSR, proc_pid_ksm_merging_pages), + REG("ksm_force", S_IRUSR|S_IWUSR, proc_pid_ksm_force_operations), #endif }; @@ -3642,6 +3738,7 @@ static const struct pid_entry tid_base_s #endif #ifdef CONFIG_KSM ONE("ksm_merging_pages", S_IRUSR, proc_pid_ksm_merging_pages), + REG("ksm_force", S_IRUSR|S_IWUSR, proc_pid_ksm_force_operations), #endif }; --- a/include/linux/mm_types.h~mm-ksm-introduce-ksm_force-for-each-process +++ a/include/linux/mm_types.h @@ -649,6 +649,15 @@ struct mm_struct { * merging. */ unsigned long ksm_merging_pages; + /* + * If true, force anonymous pages of this mm to be involved in KSM + * merging without explicitly calling madvise. It is effctive only + * when the klob of '/sys/kernel/mm/ksm/run' is set as 1. If false, + * cancel the feature of ksm_force of this process and unmerge + * those merged pages which is not madvised as MERGEABLE of this + * process, but leave MERGEABLE areas merged. + */ + bool ksm_force; #endif } __randomize_layout; --- a/mm/ksm.c~mm-ksm-introduce-ksm_force-for-each-process +++ a/mm/ksm.c @@ -334,6 +334,34 @@ static void __init ksm_slab_free(void) mm_slot_cache = NULL; } +/* Check if vma is qualified for ksmd scanning */ +static bool ksm_vma_check(struct vm_area_struct *vma) +{ + unsigned long vm_flags = vma->vm_flags; + + if (!(vma->vm_flags & VM_MERGEABLE) && !(vma->vm_mm->ksm_force)) + return false; + + if (vm_flags & (VM_SHARED | VM_MAYSHARE | + VM_PFNMAP | VM_IO | VM_DONTEXPAND | + VM_HUGETLB | VM_MIXEDMAP)) + return false; /* just ignore this vma*/ + + if (vma_is_dax(vma)) + return false; + +#ifdef VM_SAO + if (vm_flags & VM_SAO) + return false; +#endif +#ifdef VM_SPARC_ADI + if (vm_flags & VM_SPARC_ADI) + return false; +#endif + + return true; +} + static __always_inline bool is_stable_node_chain(struct stable_node *chain) { return chain->rmap_hlist_len == STABLE_NODE_CHAIN; @@ -523,7 +551,7 @@ static struct vm_area_struct *find_merge if (ksm_test_exit(mm)) return NULL; vma = vma_lookup(mm, addr); - if (!vma || !(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma) + if (!vma || !ksm_vma_check(vma) || !vma->anon_vma) return NULL; return vma; } @@ -2300,7 +2328,7 @@ next_mm: goto no_vmas; for_each_vma(vmi, vma) { - if (!(vma->vm_flags & VM_MERGEABLE)) + if (!ksm_vma_check(vma)) continue; if (ksm_scan.address < vma->vm_start) ksm_scan.address = vma->vm_start; _ Patches currently in -mm which might be from xu.xin16@zte.com.cn are mm-ksm-introduce-ksm_force-for-each-process.patch