From patchwork Mon Mar 14 01:53:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: CGEL X-Patchwork-Id: 12779489 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 781E6C433FE for ; Mon, 14 Mar 2022 01:54:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE8A68D0003; Sun, 13 Mar 2022 21:54:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D981F8D0001; Sun, 13 Mar 2022 21:54:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C37FF8D0003; Sun, 13 Mar 2022 21:54:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0245.hostedemail.com [216.40.44.245]) by kanga.kvack.org (Postfix) with ESMTP id B3B778D0001 for ; Sun, 13 Mar 2022 21:54:24 -0400 (EDT) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 6772C181C9BBC for ; Mon, 14 Mar 2022 01:54:24 +0000 (UTC) X-FDA: 79241322048.23.6FDC1E7 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf07.hostedemail.com (Postfix) with ESMTP id 0778A40028 for ; Mon, 14 Mar 2022 01:54:23 +0000 (UTC) Received: by mail-pf1-f172.google.com with SMTP id u17so10784515pfk.11 for ; Sun, 13 Mar 2022 18:54:23 -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:mime-version :content-transfer-encoding; bh=mLSI5DTfdZAfsDHGQkcA3WiBGQTVoj9JCzoyZ8csosI=; b=JmFQPTjrGuH5DpX0TBKEVlRxuBimlcc/k5AYIug3795U3wH129i14a3/ms1r1+S06S 5MmnYoY/n2TaidSABqqJSD+1rfrn69jdjqmtOFy090ANFBzUoPLHkPEksgRKutMaNlrM J2Ox5AMddcK+oxowisewghksd/xZLYfdfyCsHy/N15EuKAr59dETUObCh/gmvkHXy35C i1DFJEEGOYtWLdbWKT4tqXfT8/UhH4Sms4IopkNwDHDUZXNoiIIpyI55tV4XakC0Osld ajrFU8+SKA0WI+IxGbxKfCEg4NbITAhGvFf8jsQniF9b/arCnaUp6DvsnPORojvhpvio zNMg== 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:mime-version :content-transfer-encoding; bh=mLSI5DTfdZAfsDHGQkcA3WiBGQTVoj9JCzoyZ8csosI=; b=pF+rlRpQsXPqc8Em/x3XcRBnPZRwhI1kU3J22qGsO4p/y7CbrlmsqmBYM/7fQfd7ZJ fp6U3HHnWZfZ0/1XFQeQ/ctgyISV42HPP1Fr/snnuZsREYt8Le8xtNrLp9Qi91TPxDnk SWQxBRkHJQ+WOwQ7IZy48xOQ/oODrv8xy0FWrtWtSSC8O5vuG+Vw56X4U4kDCGR8DFVg ybb3hdhGroqoOTfLEpE2mantyAEfS/OVsN8vOBe+vgMqB3lWc5JHeZE6i4Y8Qsonw/NV 1MU7iq41xGaJ/XM3GUw4FRbzm55eHAwFUAuLczcuHf3JwB1pKHqVmsuuC70mQ75pPeZv 9DIA== X-Gm-Message-State: AOAM531ZZskpqq+4P8Q9umX/njgQO3YZRljyXRVVwf2mDsy3K9QPG11j 84ti7eFXbMZqCxISFtu4Lys= X-Google-Smtp-Source: ABdhPJzK99lg8c9LrlBXPBwUg5Wrbm0/JB+NmJ01xxEaFuUy7K7HKS9uwrXVDw3yXYjv/425931+RA== X-Received: by 2002:a63:7d5d:0:b0:34c:17:6174 with SMTP id m29-20020a637d5d000000b0034c00176174mr18517043pgn.133.1647222863015; Sun, 13 Mar 2022 18:54:23 -0700 (PDT) Received: from localhost.localdomain ([193.203.214.57]) by smtp.gmail.com with ESMTPSA id s25-20020a056a00179900b004f737ce5c1asm17278447pfg.73.2022.03.13.18.54.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Mar 2022 18:54:22 -0700 (PDT) From: cgel.zte@gmail.com X-Google-Original-From: xu.xin16@zte.com.cn To: mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com Cc: akpm@linux-foundation.org, bristot@redhat.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, xu xin Subject: [PATCH linux-next] ksm: Count ksm-merging pages for each process Date: Mon, 14 Mar 2022 01:53:55 +0000 Message-Id: <20220314015355.2111696-1-xu.xin16@zte.com.cn> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 0778A40028 X-Stat-Signature: 5c8oa5ojyawt1bzhoudrzijyz6mmmj1m Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=JmFQPTjr; spf=pass (imf07.hostedemail.com: domain of cgel.zte@gmail.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=cgel.zte@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1647222863-998418 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. Thanks-to: Yang Yang Signed-off-by: xu xin Reported-by: kernel test robot --- fs/proc/base.c | 15 +++++++++++++++ include/linux/sched.h | 5 +++++ mm/ksm.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/fs/proc/base.c b/fs/proc/base.c index 76bf1aa3cfe8..b044535df0d2 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -3155,6 +3155,15 @@ static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns, } #endif /* CONFIG_LIVEPATCH */ +#if defined(CONFIG_KSM) && defined(CONFIG_MEMCG) +static int proc_pid_ksm_merging_pages(struct seq_file *m, struct pid_namespace *ns, + struct pid *pid, struct task_struct *task) +{ + seq_printf(m, "%lu\n", task->ksm_merging_pages); + return 0; +} +#endif /* CONFIG_KSM and CONFIG_MEMCG */ + #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 +3295,9 @@ static const struct pid_entry tgid_base_stuff[] = { #ifdef CONFIG_SECCOMP_CACHE_DEBUG ONE("seccomp_cache", S_IRUSR, proc_pid_seccomp_cache), #endif +#if defined(CONFIG_KSM) && defined(CONFIG_MEMCG) + 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 +3631,9 @@ static const struct pid_entry tid_base_stuff[] = { #ifdef CONFIG_SECCOMP_CACHE_DEBUG ONE("seccomp_cache", S_IRUSR, proc_pid_seccomp_cache), #endif +#if defined(CONFIG_KSM) && defined(CONFIG_MEMCG) + 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/sched.h b/include/linux/sched.h index ad710b3f664e..d092cf1a100d 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1346,6 +1346,11 @@ struct task_struct { unsigned int kasan_depth; #endif +#if defined(CONFIG_KSM) && defined(CONFIG_MEMCG) + unsigned long ksm_merging_pages; +#endif + + #ifdef CONFIG_KCSAN struct kcsan_ctx kcsan_ctx; #ifdef CONFIG_TRACE_IRQFLAGS diff --git a/mm/ksm.c b/mm/ksm.c index 063a48eeb5ee..c79e7ff278bd 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -629,6 +629,9 @@ static inline void free_stable_node_chain(struct stable_node *chain, static void remove_node_from_stable_tree(struct stable_node *stable_node) { struct rmap_item *rmap_item; +#ifdef CONFIG_MEMCG + struct task_struct *owner; +#endif /* check it's not STABLE_NODE_CHAIN or negative */ BUG_ON(stable_node->rmap_hlist_len < 0); @@ -638,6 +641,14 @@ static void remove_node_from_stable_tree(struct stable_node *stable_node) ksm_pages_sharing--; else ksm_pages_shared--; + +#ifdef CONFIG_MEMCG /*Condition of mm_struct with owner*/ + BUG_ON(rmap_item->mm == NULL); + owner = rmap_item->mm->owner; + /* In case that the process of mm may be killed or exit */ + if (owner) + owner->ksm_merging_pages--; +#endif VM_BUG_ON(stable_node->rmap_hlist_len <= 0); stable_node->rmap_hlist_len--; put_anon_vma(rmap_item->anon_vma); @@ -771,6 +782,9 @@ static void remove_rmap_item_from_tree(struct rmap_item *rmap_item) if (rmap_item->address & STABLE_FLAG) { struct stable_node *stable_node; struct page *page; +#ifdef CONFIG_MEMCG + struct task_struct *owner; +#endif stable_node = rmap_item->head; page = get_ksm_page(stable_node, GET_KSM_PAGE_LOCK); @@ -785,6 +799,14 @@ static void remove_rmap_item_from_tree(struct rmap_item *rmap_item) ksm_pages_sharing--; else ksm_pages_shared--; + +#ifdef CONFIG_MEMCG /*Condition of mm_struct with owner*/ + BUG_ON(rmap_item->mm == NULL); + owner = rmap_item->mm->owner; + /* In case that the process of mm may be killed or exit */ + if (owner) + owner->ksm_merging_pages--; +#endif VM_BUG_ON(stable_node->rmap_hlist_len <= 0); stable_node->rmap_hlist_len--; @@ -1981,6 +2003,9 @@ static void stable_tree_append(struct rmap_item *rmap_item, struct stable_node *stable_node, bool max_page_sharing_bypass) { +#ifdef CONFIG_MEMCG + struct task_struct *owner; +#endif /* * rmap won't find this mapping if we don't insert the * rmap_item in the right stable_node @@ -2007,6 +2032,14 @@ static void stable_tree_append(struct rmap_item *rmap_item, ksm_pages_sharing++; else ksm_pages_shared++; + +#ifdef CONFIG_MEMCG /*Condition of mm_struct with owner*/ + BUG_ON(rmap_item->mm == NULL); + owner = rmap_item->mm->owner; + /* In case that the process of mm may be killed or exit */ + if (owner) + owner->ksm_merging_pages++; +#endif } /*