From patchwork Thu Apr 6 16:53:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13203738 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9FD88C77B6F for ; Thu, 6 Apr 2023 16:56:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F1AB46B0074; Thu, 6 Apr 2023 12:56:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EA2C66B0075; Thu, 6 Apr 2023 12:56:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D443B6B0078; Thu, 6 Apr 2023 12:56:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BE9F06B0074 for ; Thu, 6 Apr 2023 12:56:41 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 9797D1C721D for ; Thu, 6 Apr 2023 16:56:41 +0000 (UTC) X-FDA: 80651570202.12.E07E877 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf27.hostedemail.com (Postfix) with ESMTP id F3F5840008 for ; Thu, 6 Apr 2023 16:56:38 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf27.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680800199; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=02OllJMvVBF/krrkhBg4cfJi3+wdPE8UUtNkVEWhAfU=; b=SbaS+/KQNxdQe/fioNdOJQmRg4gftiBwHk+bojyTALXeajnK/5tB9V+BI4+5GyENW2pJ9H U/lfLGUb3yZRDfzrr8oQ6KRO8468SkTggJP+JTd70/AFXcS6N2MqpbjKUkpSWpbZ+Dhb+D CBCQPKv82ycyUjHqy4wrnsT86sjqbD0= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf27.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680800199; a=rsa-sha256; cv=none; b=uXHoeUjb76Jd0j+Sj1WY52snO6ysWGFx9tUliGkwV7eweNVRKCkkqpc88wsSzZDWslDvgK qOHGPObCE0ztX5EriQAHs+6NIek4BDP6Jv2yBlUwO+9v3AWAgPM3hnNklvuk/Gl7RDEktG p7FOXsKDKkIKYD82pAVqNJUxCCoCJrw= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 1EF01AC5FA67; Thu, 6 Apr 2023 09:53:44 -0700 (PDT) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, linux-mm@kvack.org, riel@surriel.com, mhocko@suse.com, david@redhat.com, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, akpm@linux-foundation.org, hannes@cmpxchg.org, Bagas Sanjaya Subject: [PATCH v5 2/3] mm: add new KSM process and sysfs knobs Date: Thu, 6 Apr 2023 09:53:38 -0700 Message-Id: <20230406165339.1017597-3-shr@devkernel.io> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230406165339.1017597-1-shr@devkernel.io> References: <20230406165339.1017597-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: F3F5840008 X-Rspam-User: X-Stat-Signature: fz8akxwcz74asurq69afk7mr9q5s979p X-HE-Tag: 1680800198-564752 X-HE-Meta: U2FsdGVkX1+4gFSuRyloO5vdElRqlVrTFHQQJe4ttafks17iX0id55NZa9JeOMxLMOWKa2NyXxgEl1bUMwoNshVqr4n/xIN7dvGBQboV+vg0pmCps7jVklRffhvHalQ5Hz5vWti/0lWSKShiEzKqQPF+tXIz7Ij/J8y3HzD0fqYg1/u71nrZ1JDjmJRTu5yfS2kQ2tvLWTJgc1Ga6Q2Kelt94x/dJoZDV8KU1bByz3qTgEbXcn2zo25vi4EppgX32iTzMYq31ZBYXUSoewBYr0qmcsQa4Xd2igxMwK7MxcTqyj1cHMjr2ZMQooJ/bAKDydxdku3gLicktpFLF8eNyaovMF9AXv3ytywDdd9uea/O6IkGzE3jihzMX9kTHIYIU03tYFfIAAoFqL5Z7vlMgCNID4jYgY90xNhaZrWe4bhOaoCLJ14mKsFc5dEVdrl4Fm8215Oigk1pPB/REwrC76fX4MY96teXUL4diF+27C1Psis0C0OxIbL70M+5VU9Vz/R3nTwprbEar09tqCZGXAWZb4NZuflvUAxBvvvbZzzXj28XWOB9x6hXr/C5veeL4Z9EGekbJpsXtyK3yMfpSvvZXXQae8dOiXC7SQfj7qneFlkgdzIKH9BMdNh6xfJj58n79cDLSNrcUKHpd2E0W9yJSt0Xbd02F2HmA0oUAOYfILA+YpNKciMMxom8LaWVLlK/s4gEYLOIwfcYnF5eSc/DhdkBvppMSYAH5ZfLQpUmxQ1/t73vW7GXxReW0JfteMw7Cchi/j68Pa64hwMaI2rxMVwOIMQnv82hS8sanmajkNy+oSeoPq5gq2XSAHjyzP1vGgGIY5NgNMOtU71EbxIgQxxUHjPfT5ilEKA9ZpTlFEUUO/EnIuynwhJVFdOeRHH5Jsng18eQXCBu8qrEpnku5yCHw3vWfFE5vhab9BuaSh53tq0ajfMo34CM7XhLu5XtpYB3V5tMuOkhzue sgjOit+G 6M0CEsu+kbWyvgKEyKYp+sN3U27ogBm4gmJH38M0cfJUSjR16LSf0pg65euovyiIRIZEYUQz9G0UiOCkOGUpIZaCOhlJBrmWzP3dyiLWtXcqP5lCDue9xvLQvyPYRzQxgtGo4s0BH8iFYknAtE5KA9TM4Gy5NUbrpGdyiLN00f8xX1Umr6zVZRYfG1/7FqsoaSZjoOPAM+cpUEB4zQYrDWOCk1EAooVOmFS9uoRmBCkEuYGcKczkeyBQwelJNdWOej8Q+PIz7nLTzYA8cy7iCxAAeeE9iyRkVNe0dhlRi4Nsq3iF6tPDPn9pP+3xq4BncmI1D+Suh8qHY2KwKi2GA1tFPkMYT2j/svTIyJ1bmfgIZGlAFdCvRSnj3XOyzUeNLsPfhbJWd8EKZFwzDCX67qpKIz8uWBgicH7tJ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This adds the general_profit KSM sysfs knob and the process profit metric and process merge type knobs to ksm_stat. 1) expose general_profit metric The documentation mentions a general profit metric, however this metric is not calculated. In addition the formula depends on the size of internal structures, which makes it more difficult for an administrator to make the calculation. Adding the metric for a better user experience. 2) document general_profit sysfs knob 3) calculate ksm process profit metric The ksm documentation mentions the process profit metric and how to calculate it. This adds the calculation of the metric. 4) add ksm_merge_type() function This adds the ksm_merge_type function. The function returns the merge type for the process. For madvise it returns "madvise", for prctl it returns "process" and otherwise it returns "none". 5) mm: expose ksm process profit metric and merge type in ksm_stat This exposes the ksm process profit metric in /proc//ksm_stat. The name of the value is ksm_merge_type. The documentation mentions the formula for the ksm process profit metric, however it does not calculate it. In addition the formula depends on the size of internal structures. So it makes sense to expose it. 6) document new procfs ksm knobs Signed-off-by: Stefan Roesch Reviewed-by: Bagas Sanjaya Cc: David Hildenbrand Cc: Johannes Weiner Cc: Michal Hocko Cc: Rik van Riel Signed-off-by: Andrew Morton Acked-by: David Hildenbrand --- Documentation/ABI/testing/sysfs-kernel-mm-ksm | 8 +++++ Documentation/admin-guide/mm/ksm.rst | 8 ++++- fs/proc/base.c | 5 +++ include/linux/ksm.h | 5 +++ mm/ksm.c | 32 +++++++++++++++++++ 5 files changed, 57 insertions(+), 1 deletion(-) diff --git a/Documentation/ABI/testing/sysfs-kernel-mm-ksm b/Documentation/ABI/testing/sysfs-kernel-mm-ksm index d244674a9480..7768e90f7a8f 100644 --- a/Documentation/ABI/testing/sysfs-kernel-mm-ksm +++ b/Documentation/ABI/testing/sysfs-kernel-mm-ksm @@ -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: January 2023 +KernelVersion: 6.1 +Contact: Linux memory management mailing list +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. diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst index 270560fef3b2..d2929964cd0f 100644 --- a/Documentation/admin-guide/mm/ksm.rst +++ b/Documentation/admin-guide/mm/ksm.rst @@ -157,6 +157,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 @@ -214,7 +216,8 @@ several times, which are unprofitable memory consumed. ksm_rmap_items * sizeof(rmap_item). where ksm_merging_pages is shown under the directory ``/proc//``, - and ksm_rmap_items is shown in ``/proc//ksm_stat``. + and ksm_rmap_items is shown in ``/proc//ksm_stat``. The process profit + is also shown in ``/proc//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 @@ -225,6 +228,9 @@ 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. +The ksm_merge_type in ``/proc//ksm_stat`` shows the merge type of the +process. Valid values are ``none``, ``madvise`` and ``process``. + Monitoring KSM events ===================== diff --git a/fs/proc/base.c b/fs/proc/base.c index 07463ad4a70a..c74450318e05 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -96,6 +96,7 @@ #include #include #include +#include #include #include "internal.h" #include "fd.h" @@ -3199,6 +3200,7 @@ static int proc_pid_ksm_merging_pages(struct seq_file *m, struct pid_namespace * return 0; } + static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns, struct pid *pid, struct task_struct *task) { @@ -3208,6 +3210,9 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns, if (mm) { seq_printf(m, "ksm_rmap_items %lu\n", mm->ksm_rmap_items); seq_printf(m, "zero_pages_sharing %lu\n", mm->ksm_zero_pages_sharing); + seq_printf(m, "ksm_merging_pages %lu\n", mm->ksm_merging_pages); + seq_printf(m, "ksm_merge_type %s\n", ksm_merge_type(mm)); + seq_printf(m, "ksm_process_profit %ld\n", ksm_process_profit(mm)); mmput(mm); } diff --git a/include/linux/ksm.h b/include/linux/ksm.h index c65455bf124c..4c32f9bca723 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -60,6 +60,11 @@ struct page *ksm_might_need_to_copy(struct page *page, void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc); void folio_migrate_ksm(struct folio *newfolio, struct folio *folio); +#ifdef CONFIG_PROC_FS +long ksm_process_profit(struct mm_struct *); +const char *ksm_merge_type(struct mm_struct *mm); +#endif /* CONFIG_PROC_FS */ + #else /* !CONFIG_KSM */ static inline int ksm_add_mm(struct mm_struct *mm) diff --git a/mm/ksm.c b/mm/ksm.c index ab95ae0f9def..76b10ff840ac 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3042,6 +3042,25 @@ static void wait_while_offlining(void) } #endif /* CONFIG_MEMORY_HOTREMOVE */ +#ifdef CONFIG_PROC_FS +long ksm_process_profit(struct mm_struct *mm) +{ + return (long)mm->ksm_merging_pages * PAGE_SIZE - + mm->ksm_rmap_items * sizeof(struct ksm_rmap_item); +} + +/* Return merge type name as string. */ +const char *ksm_merge_type(struct mm_struct *mm) +{ + if (test_bit(MMF_VM_MERGE_ANY, &mm->flags)) + return "process"; + else if (test_bit(MMF_VM_MERGEABLE, &mm->flags)) + return "madvise"; + else + return "none"; +} +#endif /* CONFIG_PROC_FS */ + #ifdef CONFIG_SYSFS /* * This all compiles without CONFIG_SYSFS, but is a waste of space. @@ -3313,6 +3332,18 @@ static ssize_t zero_pages_sharing_show(struct kobject *kobj, } KSM_ATTR_RO(zero_pages_sharing); +static ssize_t general_profit_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + long general_profit; + + general_profit = ksm_pages_sharing * PAGE_SIZE - + ksm_rmap_items * sizeof(struct ksm_rmap_item); + + return sysfs_emit(buf, "%ld\n", general_profit); +} +KSM_ATTR_RO(general_profit); + static ssize_t stable_node_dups_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -3378,6 +3409,7 @@ static struct attribute *ksm_attrs[] = { &stable_node_dups_attr.attr, &stable_node_chains_prune_millisecs_attr.attr, &use_zero_pages_attr.attr, + &general_profit_attr.attr, NULL, };