From patchwork Tue Mar 15 11:48:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: CGEL X-Patchwork-Id: 12781360 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 68432C433F5 for ; Tue, 15 Mar 2022 11:48:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7D6B68D0002; Tue, 15 Mar 2022 07:48:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 786208D0001; Tue, 15 Mar 2022 07:48:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 626BC8D0002; Tue, 15 Mar 2022 07:48:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 501048D0001 for ; Tue, 15 Mar 2022 07:48:57 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 11A3A181C49A5 for ; Tue, 15 Mar 2022 11:48:57 +0000 (UTC) X-FDA: 79246449114.24.DC5C983 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by imf24.hostedemail.com (Postfix) with ESMTP id 8E372180004 for ; Tue, 15 Mar 2022 11:48:56 +0000 (UTC) Received: by mail-pj1-f43.google.com with SMTP id kx6-20020a17090b228600b001bf859159bfso2083135pjb.1 for ; Tue, 15 Mar 2022 04:48:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZqgQrWW9h8EA9ThxRZG5uSYD0/VFUVnqeaBjtQ/fiog=; b=H3lf9PYnNQ/mMrla7WKPf/Jgd4iBVY0u19a2rKEzVP8bMFQr5kNDf40lnGoos9UiQK Wg7PJ0OSZFaDIE95IEY0fp9rkqH7C7Wv1nNtXomC4/oGNZCUNlkky/r6bWZ+LVveZyJC wewieciGs/G3B+iG9LTU0TU+WPnwdihhYxVauD+3d5U2M7KeO6s5viUvgjGz5Qrsz5n7 sLpHrxCnxBe4nWu2cExSIrzuKK9NFFT5haz3BB5+BkGC3UlajZJVTtpiXt3DHYAkSYZc llTUQcDeSwbC3+QYIj53FQ9LUybCkdJA5l9WoQBkLa1N0dXahz9hs2pMMGd7G8a7Lmgw cdBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZqgQrWW9h8EA9ThxRZG5uSYD0/VFUVnqeaBjtQ/fiog=; b=kANd1J4Og6oMRZJ1Rl83XNnEkdkx9tO0fhDWnAZKfKhrxmU0nnne4pHA27JexXeLrv 1R+1sGDIhjs11UFwG7xXUli1v5g6a8XS0Ba+2p6jQyqPJogV2YoICb+J1B9P2qJDWesO 0oboyexRP+ykwWR1/PN3N/yi5dJYbZVpZtbb+BzuB9JLdHQTLXfbZWJcwfgMWiGs+uRc Q7pm1FggjKeYgQQlyBKXkFTf4Ae9a4hp8X8B5MkCoKdaM9TYROmHd/ZtO2IQMV9XC1vD ySu7Pda7eZVd7ediyGnoJLLbN+fyYj1MD4BPXzd31HvEQXp0h+MTKRnO72NoUiuWgdvY Pl7Q== X-Gm-Message-State: AOAM5327MvT7d4jgcdxl0g6OMivuNkIv+XQ5cGxiFSl2qD6BO1cbtyQ4 9Ak94OGVmaAeMfPRLfm/6rw= X-Google-Smtp-Source: ABdhPJzfwMdkLjsT8RF8fJRStDWH5MpVCMN8duoELf//ldmmvYGpibhYFDpUazLmTITItGx02cfi2w== X-Received: by 2002:a17:90a:4f0b:b0:1be:e31f:bd38 with SMTP id p11-20020a17090a4f0b00b001bee31fbd38mr4216688pjh.76.1647344935490; Tue, 15 Mar 2022 04:48:55 -0700 (PDT) Received: from localhost.localdomain ([193.203.214.57]) by smtp.gmail.com with ESMTPSA id q4-20020a056a00150400b004f78d4821a0sm16414921pfu.204.2022.03.15.04.48.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Mar 2022 04:48:55 -0700 (PDT) From: cgel.zte@gmail.com X-Google-Original-From: xu.xin16@zte.com.cn To: lkp@intel.com Cc: akpm@linux-foundation.org, bristot@redhat.com, cgel.zte@gmail.com, juri.lelli@redhat.com, kbuild-all@lists.01.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, mingo@redhat.com, peterz@infradead.org, xu.xin16@zte.com.cn, Yang Yang , Ran Xiaokai , Zeal Robot Subject: [PATCH v2] ksm: Count ksm merging pages for each process Date: Tue, 15 Mar 2022 11:48:49 +0000 Message-Id: <20220315114849.2119443-1-xu.xin16@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <202203150131.oDjproo7-lkp@intel.com> References: <202203150131.oDjproo7-lkp@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 8E372180004 X-Rspam-User: Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=H3lf9PYn; spf=pass (imf24.hostedemail.com: domain of cgel.zte@gmail.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=cgel.zte@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Stat-Signature: fh9ceyroisnac3oorrdz8a7ffqscwfbr X-Rspamd-Server: rspam07 X-HE-Tag: 1647344936-744749 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: From: xu xin As current KSM only counts the number of KSM merging pages(e.g. ksm_pages_sharing and ksm_pages_shared) of the whole system, we cannot see the more fine-grained KSM merging, for the upper application optimization, the merging area cannot be set easily according to the KSM page merging probability of each process. Therefore, it is necessary to add extra statistical means so that the upper level users can know the detailed KSM merging information of each process. We add a new proc file named as ksm_merging_pages under /proc// to indicate the involved ksm merging pages of this process. Reported-by: kernel test robot Reviewed-by: Yang Yang Reviewed-by: Ran Xiaokai Reported-by: Zeal Robot Signed-off-by: xu xin --- fs/proc/base.c | 22 ++++++++++++++++++++++ include/linux/mm_types.h | 8 ++++++++ mm/ksm.c | 11 +++++++++++ 3 files changed, 41 insertions(+) diff --git a/fs/proc/base.c b/fs/proc/base.c index d654ce7..5c8feef 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -3155,6 +3155,22 @@ 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; +} +#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) @@ -3286,6 +3302,9 @@ static const struct pid_entry tgid_base_stuff[] = { #ifdef CONFIG_SECCOMP_CACHE_DEBUG ONE("seccomp_cache", S_IRUSR, proc_pid_seccomp_cache), #endif +#ifdef CONFIG_KSM + ONE("ksm_merging_pages", S_IRUSR, proc_pid_ksm_merging_pages), +#endif }; static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx) @@ -3619,6 +3638,9 @@ static const struct pid_entry tid_base_stuff[] = { #ifdef CONFIG_SECCOMP_CACHE_DEBUG ONE("seccomp_cache", S_IRUSR, proc_pid_seccomp_cache), #endif +#ifdef CONFIG_KSM + ONE("ksm_merging_pages", S_IRUSR, proc_pid_ksm_merging_pages), +#endif }; static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 9db36dc..4d98bd5 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -633,6 +633,14 @@ struct mm_struct { #ifdef CONFIG_IOMMU_SUPPORT u32 pasid; #endif + +#ifdef CONFIG_KSM + /* + * Represet how many pages of this process are + * involved in KSM merging. + */ + unsigned long ksm_merging_pages; +#endif } __randomize_layout; /* diff --git a/mm/ksm.c b/mm/ksm.c index c20bd4d..5889733 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -638,6 +638,10 @@ static void remove_node_from_stable_tree(struct stable_node *stable_node) ksm_pages_sharing--; else ksm_pages_shared--; + + BUG_ON(rmap_item->mm == NULL); + rmap_item->mm->ksm_merging_pages--; + VM_BUG_ON(stable_node->rmap_hlist_len <= 0); stable_node->rmap_hlist_len--; put_anon_vma(rmap_item->anon_vma); @@ -785,6 +789,10 @@ static void remove_rmap_item_from_tree(struct rmap_item *rmap_item) ksm_pages_sharing--; else ksm_pages_shared--; + + BUG_ON(rmap_item->mm == NULL); + rmap_item->mm->ksm_merging_pages--; + VM_BUG_ON(stable_node->rmap_hlist_len <= 0); stable_node->rmap_hlist_len--; @@ -2020,6 +2028,9 @@ static void stable_tree_append(struct rmap_item *rmap_item, ksm_pages_sharing++; else ksm_pages_shared++; + + BUG_ON(rmap_item->mm == NULL); + rmap_item->mm->ksm_merging_pages++; } /*