From patchwork Tue Apr 18 05:13:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13215032 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 E5EE7C77B76 for ; Tue, 18 Apr 2023 05:20:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6EBA06B0074; Tue, 18 Apr 2023 01:20:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6751B6B0075; Tue, 18 Apr 2023 01:20:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C8218E0002; Tue, 18 Apr 2023 01:20:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 34F9A6B0074 for ; Tue, 18 Apr 2023 01:20:02 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 0080EA013B for ; Tue, 18 Apr 2023 05:20:01 +0000 (UTC) X-FDA: 80693360244.21.5626592 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf19.hostedemail.com (Postfix) with ESMTP id 4A73F1A001B for ; Tue, 18 Apr 2023 05:19:59 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf19.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=1681795199; 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=xET7LJAcy3xdht7hDsxpHJlEmTDNKMadejYfAdl19XU=; b=Ja/WT5f7LoLbrckp4rbLNXoYOZr/tydY3gb5mZG1mXJ7ZlDajjGR61qgqbsY97gZXFWXoM 2XaVPPVESSzX7Ug+uhFLcL9hREPAJ7Vh9XlEyqsyctUmra6Tp1+UURE+wnw7DSdsMzf4gn JUJAsFslYMgLhrTC32TGK8Jtg/K5DYE= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf19.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=1681795199; a=rsa-sha256; cv=none; b=fOmuh6kGfYXoXXDqZk8rSaTywAHugeCs4AwOC28w1pqmwQ/H/BjJVfqu6pzh++MFDkSbhv XjXi0XbnehlgxBJxqCLl4Owi2RlwuGpRPOwwYNYqroAOzKenpUGMSkC2XwJKdKez8rQjYu 8Xow1d0i3Z6H2k/kxsYsxpXR/FqyjBE= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id D99393AA01F4; Mon, 17 Apr 2023 22:14:10 -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, willy@infradead.org, Bagas Sanjaya Subject: [PATCH v9 2/3] mm: add new KSM process and sysfs knobs Date: Mon, 17 Apr 2023 22:13:41 -0700 Message-Id: <20230418051342.1919757-3-shr@devkernel.io> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230418051342.1919757-1-shr@devkernel.io> References: <20230418051342.1919757-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 4A73F1A001B X-Rspam-User: X-Stat-Signature: g7pis83cjqz3yrkjggxpc8fqozfgaonf X-HE-Tag: 1681795199-330006 X-HE-Meta: U2FsdGVkX18IGURhitPWJoFxqrF4wS7Amvhy6vDrxNlzFY03hYubcbv+Fzuw9Cfmsw08YbdDuggDb8HzAgWEhDVutYJ8loMhZbUB2Ko371GF4BtDegI57SMiUelNSKxM4Tl8HdzQ78XTT24180AJFWN5Mp0USCghwI+5MgslWGjUovjU04IT0YG0kOHREj+v0TGUoacnk1lHx30HOvfKrsJtSUnnzX6sbhQGwjDGgaDMfSC6McClgeDPKfgiyAzyAtTgTCDqkM0ILT1d8Ma+s2dLvWJxTCry4sWGkLPrGDPhC6NWku21n88pF2+fW6RvBJnFd93fOiQ82lGYqAb7uBRbDA8mFezQTTRrWxNnsFySV0C8qvFwynxqqnzx+tKTI9mQcXCYG3RXq/Ku7EhGo0TQVQQghrG3vD0fQzTa/Sv/2Qgq825yqxJIYLd5W+5oFwqxi+/jYphOckvvKb4U6RnA0j1pvrdeJBXbGxDl0Dp671gBgQc/roLDxNJoWQyOZpd4D161ql8VndGhqoqYQV0IiBrsbxvO+wfLM4QWLo/HEmnlONhlYLPGi3wYuARVeX1Y2jJeTpqTyAuqBRy7njUllIjfVUB7mG7yx2KDyihJAACNDcg4iizY5HWhyjcGwW078/d10R91/yG0BCvyf9lm6x/o3v0PaFtkCWhYDxX7N5RTZCd5jRhoed2UVAccOnrsTjv6stZwkoYxKhAU8ikoHdw4yCW4qWHA5qSd2ldOYg6K3L//ZG0kV+gHvN2vAU5ePl8uHqiDb1dYVPhQ09oVUB5voueIQf1qzP9Pij/W/b3+jgOHdu9BB79Xjv+/AviiojISxbpABaggfqrcOwVUMyLaomLr8lPIjA+xXutxCGHviMk9Y+yGdjI8AK063jAY8DozTw64LD/aWhr7C1zkDzLvFhiDP0BKzqxPPpv9gXwa0wcrwI6RdU33+ZJqSDOEeQSS10DCgaA1O/X ptnUR0EF oBqzn4U8A6Rk36xqZ4oTx2OT09+u6WgT/3dA9t2MA42iOKLcT/eGC201ycJ4429LvqzsEObnUPO24yhhxfIcthMKKAtecWTDVXEbpYWy1xBv8sUsLYGren25IoHCmI3YWFrfowG7WJsZLB0GouUqqOJ4n0OTPXwfM4FjzICYvvU7PYeyP25tFgb/Z8JfywqPUbax0RlSmd7cLr5TSP4y0G1WOdyWf2/MZVMBU1qCRk09X8x3RpxMi+sGXOaXpQW8MIm+EEsoMYzLmR70hTU+jElggJWIVNzMA0SV5kRO0FJ4fzXNIuuTwHDqNbwiYsuHxwfxY+iVdIHqnfU/eVHJvfIVWOyylaXjocSQS5bgl5M6Y1gnor/UiwIheFnxr3P6yzxYYmk5m5E1HwmmFnBzxm0iADDl98tpFBU8hJtkSJ/1YyWA= 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 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) mm: expose ksm process profit metric in ksm_stat This exposes the ksm process profit metric in /proc//ksm_stat. 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. 5) document new procfs ksm knobs Signed-off-by: Stefan Roesch Reviewed-by: Bagas Sanjaya Acked-by: David Hildenbrand Cc: David Hildenbrand Cc: Johannes Weiner Cc: Michal Hocko Cc: Rik van Riel Signed-off-by: Andrew Morton --- Documentation/ABI/testing/sysfs-kernel-mm-ksm | 8 +++++++ Documentation/admin-guide/mm/ksm.rst | 5 ++++- fs/proc/base.c | 3 +++ include/linux/ksm.h | 4 ++++ mm/ksm.c | 21 +++++++++++++++++++ 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Documentation/ABI/testing/sysfs-kernel-mm-ksm b/Documentation/ABI/testing/sysfs-kernel-mm-ksm index d244674a9480..6041a025b65a 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: April 2023 +KernelVersion: 6.4 +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 eed51a910c94..551083a396fb 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 @@ -207,7 +209,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 diff --git a/fs/proc/base.c b/fs/proc/base.c index 5e0e0ccd47aa..96a6a08c8235 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" @@ -3207,6 +3208,8 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns, 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); } diff --git a/include/linux/ksm.h b/include/linux/ksm.h index fa996373653b..590934bdddcf 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -64,6 +64,10 @@ 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 *); +#endif /* CONFIG_PROC_FS */ + #else /* !CONFIG_KSM */ static inline void ksm_add_vma(struct vm_area_struct *vma) diff --git a/mm/ksm.c b/mm/ksm.c index f3f76f2ebe89..a959e8925413 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2962,6 +2962,14 @@ static void wait_while_offlining(void) } #endif /* CONFIG_MEMORY_HOTREMOVE */ +#ifdef CONFIG_PROC_FS +long ksm_process_profit(struct mm_struct *mm) +{ + return mm->ksm_merging_pages * PAGE_SIZE - + mm->ksm_rmap_items * sizeof(struct ksm_rmap_item); +} +#endif /* CONFIG_PROC_FS */ + #ifdef CONFIG_SYSFS /* * This all compiles without CONFIG_SYSFS, but is a waste of space. @@ -3226,6 +3234,18 @@ static ssize_t pages_volatile_show(struct kobject *kobj, } KSM_ATTR_RO(pages_volatile); +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) { @@ -3290,6 +3310,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, };