From nobody Thu Oct 2 06:19:39 2025 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [160.30.148.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2403E146588; Sun, 21 Sep 2025 15:09:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=160.30.148.35 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758467345; cv=none; b=F1qENbJ3+kfwRyh1XLqv5qUqjGlw0ozmbjECqiGS9u7Uww9xSkTUurh4IsIzlHFNqMTTQ4Eb5QqOwcrTDePO831+IwaZlh+DaEkw8ysq7hwUlF5xLTPyn886qcidLo6Nhl6qRzrgbmMQbzQazgLWBGi+vwWjgNayDuIi4eFGJ9I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758467345; c=relaxed/simple; bh=KZd/ED+7o7EkoeJyVZYwZmfLg42CgrN/IDRXqp6qUlk=; h=Date:Message-ID:In-Reply-To:References:Mime-Version:From:To:Cc: Subject:Content-Type; b=HRhGoWF7GVr2V1XG3diUbQjdMqEnLmN5ocEODg9ioWoqS+jem9OkKf1yrS7nh83wtmBVWEXj2KaznB5blaL827DGWCgJXxSKT893vMGhtZUlKlHAiijaMzmELm17Kzi2sZoZOX+FLzpGKWSqwwy2a7zu6S08NvzEk7zaTatropA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=160.30.148.35 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4cV8lc0d6Wz8Xs70; Sun, 21 Sep 2025 23:09:00 +0800 (CST) Received: from xaxapp01.zte.com.cn ([10.88.99.176]) by mse-fl2.zte.com.cn with SMTP id 58LF8pmu055494; Sun, 21 Sep 2025 23:08:51 +0800 (+08) (envelope-from xu.xin16@zte.com.cn) Received: from mapi (xaxapp02[null]) by mapi (Zmail) with MAPI id mid32; Sun, 21 Sep 2025 23:08:54 +0800 (CST) Date: Sun, 21 Sep 2025 23:08:54 +0800 (CST) X-Zmail-TransId: 2afa68d0150669b-1c21a X-Mailer: Zmail v1.0 Message-ID: <20250921230854496C6rkj7YZAYi39dNV3t6Fp@zte.com.cn> In-Reply-To: <20250921230726978agBBWNsPLi2hCp9Sxed1Y@zte.com.cn> References: 20250921230726978agBBWNsPLi2hCp9Sxed1Y@zte.com.cn Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 From: To: , Cc: , , , , , , , , , , Subject: =?UTF-8?B?W1BBVENIIGxpbnV4LW5leHQgdjMgMS82XSBtZW1jZzogYWRkIHBlci1tZW1jZyBrc21fcm1hcF9pdGVtcyBzdGF0?= X-MAIL: mse-fl2.zte.com.cn 58LF8pmu055494 X-TLS: YES X-SPF-DOMAIN: zte.com.cn X-ENVELOPE-SENDER: xu.xin16@zte.com.cn X-SPF: None X-SOURCE-IP: 10.5.228.133 unknown Sun, 21 Sep 2025 23:09:00 +0800 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 68D0150C.000/4cV8lc0d6Wz8Xs70 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: xu xin With the enablement of container-level KSM (e.g., via prctl), there is a growing demand for container-level observability of KSM behavior. The value of "ksm_rmap_items" indicates the total allocated ksm rmap_items of this memcg, which could be used to determine how unbeneficial the ksm-policy (like madvise), they are using brings, since the bigger the ratio of ksm_rmap_items over ksm_merging_pages, the more unbeneficial the ksm bring. Add the counter in the existing memory.stat without adding a new interface. We traverse all processes of a memcg and summing the processes' ksm_rmap_items counters instead of adding enum item in memcg_stat_item or node_stat_item and updating the corresponding enum counter when ksmd manipulate pages. Finally, we can look up ksm_rmap_items of per-memcg simply by: cat /sys/fs/cgroup/memory.stat | grep ksm_rmap_items Signed-off-by: xu xin --- include/linux/ksm.h | 1 + mm/ksm.c | 39 +++++++++++++++++++++++++++++++++++++++ mm/memcontrol.c | 5 +++++ 3 files changed, 45 insertions(+) diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 067538fc4d58..ce2a32b73f95 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -100,6 +100,7 @@ void collect_procs_ksm(const struct folio *folio, const= struct page *page, struct list_head *to_kill, int force_early); long ksm_process_profit(struct mm_struct *); bool ksm_process_mergeable(struct mm_struct *mm); +void memcg_stat_ksm_show(struct mem_cgroup *memcg, struct seq_buf *s); #else /* !CONFIG_KSM */ diff --git a/mm/ksm.c b/mm/ksm.c index 2ef29802a49b..be0efa0f8f2b 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include "internal.h" @@ -3308,6 +3309,44 @@ long ksm_process_profit(struct mm_struct *mm) } #endif /* CONFIG_PROC_FS */ +#ifdef CONFIG_MEMCG +struct memcg_ksm_stat { + unsigned long ksm_rmap_items; +}; + +static int evaluate_memcg_ksm_stat(struct task_struct *task, void *arg) +{ + struct mm_struct *mm; + struct memcg_ksm_stat *ksm_stat =3D arg; + + mm =3D get_task_mm(task); + if (mm) { + ksm_stat->ksm_rmap_items +=3D mm->ksm_rmap_items; + mmput(mm); + } + + return 0; +} + +/* Show the ksm statistic count at memory.stat under cgroup mountpoint */ +void memcg_stat_ksm_show(struct mem_cgroup *memcg, struct seq_buf *s) +{ + struct memcg_ksm_stat ksm_stat; + + if (mem_cgroup_is_root(memcg)) { + /* Just use the global counters when root memcg */ + ksm_stat.ksm_rmap_items =3D ksm_rmap_items; + } else { + /* Initialization */ + ksm_stat.ksm_rmap_items =3D 0; + /* Summing all processes'ksm statistic items */ + mem_cgroup_scan_tasks(memcg, evaluate_memcg_ksm_stat, &ksm_stat); + } + /* Print memcg ksm statistic items */ + seq_buf_printf(s, "ksm_rmap_items %lu\n", ksm_stat.ksm_rmap_items); +} +#endif + #ifdef CONFIG_SYSFS /* * This all compiles without CONFIG_SYSFS, but is a waste of space. diff --git a/mm/memcontrol.c b/mm/memcontrol.c index e090f29eb03b..705717f73b89 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -63,6 +63,7 @@ #include #include #include +#include #include "internal.h" #include #include @@ -1493,6 +1494,10 @@ static void memcg_stat_format(struct mem_cgroup *mem= cg, struct seq_buf *s) } } +#ifdef CONFIG_KSM + memcg_stat_ksm_show(memcg, s); +#endif + /* Accumulated memory events */ seq_buf_printf(s, "pgscan %lu\n", memcg_events(memcg, PGSCAN_KSWAPD) + --=20 2.25.1 From nobody Thu Oct 2 06:19:39 2025 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [160.30.148.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D05881C700C; Sun, 21 Sep 2025 15:11:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=160.30.148.35 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758467490; cv=none; b=GUThefzS9LCD0WuHT5AvNkC68sesz93FHhsYGG0Ztv5yZFUycoeWmVgPdD2LVKBmNq/HmHe4ITHv1tpGT8yb5UydoZrBWCVheg+JrEBsDXxKjbKmfWy2GsGWLN/ECJXg/tmkqgjszGArnMLt5mTmvc0CDlFRDmsW9KbG/oUMxRc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758467490; c=relaxed/simple; bh=/PgtUKZo/tTmM/IyFTv///c9ThkDROBOTiHmSM89okM=; h=Date:Message-ID:In-Reply-To:References:Mime-Version:From:To:Cc: Subject:Content-Type; b=hZy+wDc2gycVxASms7+jWbpb0wiAcu/4rKC8jiHvOLzHCrm0+QUvNLMne8zOmeSQDdgTfu38MHsSDt0LzWB8/7NHyPossp9wkTZHCMOcVmqwpITT0W5R1hdFkBoJsLM/G7Gj02Z9hvp6xpMG/KNHEDRLM+FnG0hSyaEyGDrpF5g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=160.30.148.35 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4cV8pP6LXGz8Xs6w; Sun, 21 Sep 2025 23:11:25 +0800 (CST) Received: from xaxapp01.zte.com.cn ([10.88.99.176]) by mse-fl1.zte.com.cn with SMTP id 58LFBFuk071467; Sun, 21 Sep 2025 23:11:15 +0800 (+08) (envelope-from xu.xin16@zte.com.cn) Received: from mapi (xaxapp04[null]) by mapi (Zmail) with MAPI id mid32; Sun, 21 Sep 2025 23:11:18 +0800 (CST) Date: Sun, 21 Sep 2025 23:11:18 +0800 (CST) X-Zmail-TransId: 2afb68d01596a55-055eb X-Mailer: Zmail v1.0 Message-ID: <20250921231118775bX0EITd4B9qu_9qEeGPkr@zte.com.cn> In-Reply-To: <20250921230726978agBBWNsPLi2hCp9Sxed1Y@zte.com.cn> References: 20250921230726978agBBWNsPLi2hCp9Sxed1Y@zte.com.cn Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 From: To: , Cc: , , , , , , , , , Subject: =?UTF-8?B?W1BBVENIIGxpbnV4LW5leHQgdjMgMi82XSBtZW1jZzogc2hvdyBrc21femVyb19wYWdlcyBjb3VudCBpbiBtZW1vcnkuc3RhdA==?= X-MAIL: mse-fl1.zte.com.cn 58LFBFuk071467 X-TLS: YES X-SPF-DOMAIN: zte.com.cn X-ENVELOPE-SENDER: xu.xin16@zte.com.cn X-SPF: None X-SOURCE-IP: 10.5.228.132 unknown Sun, 21 Sep 2025 23:11:25 +0800 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 68D0159D.001/4cV8pP6LXGz8Xs6w Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: xu xin Users can obtain ksm_zero_pages of a cgroup just by: 'cat /sys/fs/cgroup/memory.stat | grep ksm_zero_pages Signed-off-by: xu xin --- mm/ksm.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mm/ksm.c b/mm/ksm.c index be0efa0f8f2b..2fb4198458a4 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3312,6 +3312,7 @@ long ksm_process_profit(struct mm_struct *mm) #ifdef CONFIG_MEMCG struct memcg_ksm_stat { unsigned long ksm_rmap_items; + long ksm_zero_pages; }; static int evaluate_memcg_ksm_stat(struct task_struct *task, void *arg) @@ -3322,6 +3323,7 @@ static int evaluate_memcg_ksm_stat(struct task_struct= *task, void *arg) mm =3D get_task_mm(task); if (mm) { ksm_stat->ksm_rmap_items +=3D mm->ksm_rmap_items; + ksm_stat->ksm_zero_pages +=3D mm_ksm_zero_pages(mm); mmput(mm); } @@ -3336,14 +3338,17 @@ void memcg_stat_ksm_show(struct mem_cgroup *memcg, = struct seq_buf *s) if (mem_cgroup_is_root(memcg)) { /* Just use the global counters when root memcg */ ksm_stat.ksm_rmap_items =3D ksm_rmap_items; + ksm_stat.ksm_zero_pages =3D atomic_long_read(&ksm_zero_pages); } else { /* Initialization */ ksm_stat.ksm_rmap_items =3D 0; + ksm_stat.ksm_zero_pages =3D 0; /* Summing all processes'ksm statistic items */ mem_cgroup_scan_tasks(memcg, evaluate_memcg_ksm_stat, &ksm_stat); } /* Print memcg ksm statistic items */ seq_buf_printf(s, "ksm_rmap_items %lu\n", ksm_stat.ksm_rmap_items); + seq_buf_printf(s, "ksm_zero_pages %lu\n", ksm_stat.ksm_zero_pages); } #endif --=20 2.25.1 From nobody Thu Oct 2 06:19:39 2025 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [160.30.148.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 593191DE3B5; Sun, 21 Sep 2025 15:12:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=160.30.148.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758467556; cv=none; b=i+OiMEvMn6RQuWMKgTxHKkUSKaMtXlDl275xO/P9ioPw9w2sXunSUEADAts4C3GcPn1XV6lHev/TX+cOcOH0CTHuuT7ayrWtJZolrEO2TrGDz0h8efnoSVmu33rbguPq+3OeUQrha2aA6W3erd/bmE/LuNhODgVWsB/xfG/2ma4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758467556; c=relaxed/simple; bh=MVRf6CXjpE+tHya5kNSEjiGyLT6nEq1sf/hPnUwbMJ4=; h=Date:Message-ID:In-Reply-To:References:Mime-Version:From:To:Cc: Subject:Content-Type; b=sl1GQP+cVrXclm0g7qYk4A4qas0L1w9bm/xkKPDF03zZ7tydDnvek5QI1J+Za9t9mv0B5vweSrRS8sfeuXqMcSx9tUfzQCyeTzoPg1Ws27In/TVw/4Mc42HUbrmLrKo1U08/FwtIeMdAjfMbbwBob9uuq+snUAT79joqiGK9hnc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=160.30.148.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4cV8qZ0F03z5BNRd; Sun, 21 Sep 2025 23:12:26 +0800 (CST) Received: from xaxapp04.zte.com.cn ([10.99.98.157]) by mse-fl2.zte.com.cn with SMTP id 58LFCIUF056594; Sun, 21 Sep 2025 23:12:18 +0800 (+08) (envelope-from xu.xin16@zte.com.cn) Received: from mapi (xaxapp04[null]) by mapi (Zmail) with MAPI id mid32; Sun, 21 Sep 2025 23:12:21 +0800 (CST) Date: Sun, 21 Sep 2025 23:12:21 +0800 (CST) X-Zmail-TransId: 2afb68d015d563a-061cf X-Mailer: Zmail v1.0 Message-ID: <20250921231221760ia5tTTBoELVcZzeH5Pl2Z@zte.com.cn> In-Reply-To: <20250921230726978agBBWNsPLi2hCp9Sxed1Y@zte.com.cn> References: 20250921230726978agBBWNsPLi2hCp9Sxed1Y@zte.com.cn Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 From: To: , Cc: , , , , , , , , , Subject: =?UTF-8?B?W1BBVENIIGxpbnV4LW5leHQgdjMgMy82XSBtZW1jZzogc2hvdyBrc21fbWVyZ2luZ19wYWdlcw==?= X-MAIL: mse-fl2.zte.com.cn 58LFCIUF056594 X-TLS: YES X-SPF-DOMAIN: zte.com.cn X-ENVELOPE-SENDER: xu.xin16@zte.com.cn X-SPF: None X-SOURCE-IP: 10.5.228.133 unknown Sun, 21 Sep 2025 23:12:26 +0800 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 68D015DA.000/4cV8qZ0F03z5BNRd Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: xu xin Users can obtain ksm_merging_pages of a cgroup just by: 'cat /sys/fs/cgroup/memory.stat | grep ksm_merging_pages Signed-off-by: xu xin --- mm/ksm.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mm/ksm.c b/mm/ksm.c index 2fb4198458a4..e49f4b86ffb0 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3313,6 +3313,7 @@ long ksm_process_profit(struct mm_struct *mm) struct memcg_ksm_stat { unsigned long ksm_rmap_items; long ksm_zero_pages; + unsigned long ksm_merging_pages; }; static int evaluate_memcg_ksm_stat(struct task_struct *task, void *arg) @@ -3324,6 +3325,7 @@ static int evaluate_memcg_ksm_stat(struct task_struct= *task, void *arg) if (mm) { ksm_stat->ksm_rmap_items +=3D mm->ksm_rmap_items; ksm_stat->ksm_zero_pages +=3D mm_ksm_zero_pages(mm); + ksm_stat->ksm_merging_pages +=3D mm->ksm_merging_pages; mmput(mm); } @@ -3339,16 +3341,20 @@ void memcg_stat_ksm_show(struct mem_cgroup *memcg, = struct seq_buf *s) /* Just use the global counters when root memcg */ ksm_stat.ksm_rmap_items =3D ksm_rmap_items; ksm_stat.ksm_zero_pages =3D atomic_long_read(&ksm_zero_pages); + ksm_stat.ksm_merging_pages =3D ksm_pages_shared + + ksm_pages_sharing; } else { /* Initialization */ ksm_stat.ksm_rmap_items =3D 0; ksm_stat.ksm_zero_pages =3D 0; + ksm_stat.ksm_merging_pages =3D 0; /* Summing all processes'ksm statistic items */ mem_cgroup_scan_tasks(memcg, evaluate_memcg_ksm_stat, &ksm_stat); } /* Print memcg ksm statistic items */ seq_buf_printf(s, "ksm_rmap_items %lu\n", ksm_stat.ksm_rmap_items); seq_buf_printf(s, "ksm_zero_pages %lu\n", ksm_stat.ksm_zero_pages); + seq_buf_printf(s, "ksm_merging_pages %lu\n", ksm_stat.ksm_merging_pages); } #endif --=20 2.25.1 From nobody Thu Oct 2 06:19:39 2025 Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2F0C4282E1; Sun, 21 Sep 2025 15:13:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=183.62.165.209 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758467629; cv=none; b=EeiLWC/NplGGH36XnONTRzvBj4GFR5jTOQfIxM5p3QOajdRAowxu0X0IybdgsEdrBe5N7H9cTwF/5nsXf6K8kLMkL9HKlmpelLWiT2MqSgHuX0rBw1wRqDz0CCGWuO8aD/TfsgxpyhWgM9OlHGmlZvPybCRQYsH+2vXE5zoEuW0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758467629; c=relaxed/simple; bh=IIjIhfF2t8GgLMmXTTNs+U7K8mPPzaD6YpvseQhcB84=; h=Date:Message-ID:In-Reply-To:References:Mime-Version:From:To:Cc: Subject:Content-Type; b=gr9bIa84eWQ35qjhEPpnpM71yF9qQrnAHdKsyGF7a8kAdXKbkExo21zHI1oWDpUET976cM7alPRwkf3F8rrSy6AjdNuUu5tZ9uCORPCwX9rG0wmTJTg6MkSTM2uivWgtnIVefygXqJQgctpS3Tmv2xGmQ8/qxCK6YIiawPnhP6A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=183.62.165.209 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4cV8rt4Lqfz4xPS7; Sun, 21 Sep 2025 23:13:34 +0800 (CST) Received: from xaxapp01.zte.com.cn ([10.88.99.176]) by mse-fl1.zte.com.cn with SMTP id 58LFDV0Z071842; Sun, 21 Sep 2025 23:13:31 +0800 (+08) (envelope-from xu.xin16@zte.com.cn) Received: from mapi (xaxapp01[null]) by mapi (Zmail) with MAPI id mid32; Sun, 21 Sep 2025 23:13:34 +0800 (CST) Date: Sun, 21 Sep 2025 23:13:34 +0800 (CST) X-Zmail-TransId: 2af968d0161e972-2a4e3 X-Mailer: Zmail v1.0 Message-ID: <20250921231334093OILrn169i655S8Pe0KMUC@zte.com.cn> In-Reply-To: <20250921230726978agBBWNsPLi2hCp9Sxed1Y@zte.com.cn> References: 20250921230726978agBBWNsPLi2hCp9Sxed1Y@zte.com.cn Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 From: To: , Cc: , , , , , , , , , Subject: =?UTF-8?B?W1BBVENIIGxpbnV4LW5leHQgdjMgNC82XSBrc206IG1ha2Uga3NtX3Byb2Nlc3NfcHJvZml0IGF2YWlsYWJsZSBvbsKgQ09ORklHX1BST0NGUz1u?= X-MAIL: mse-fl1.zte.com.cn 58LFDV0Z071842 X-TLS: YES X-SPF-DOMAIN: zte.com.cn X-ENVELOPE-SENDER: xu.xin16@zte.com.cn X-SPF: None X-SOURCE-IP: 10.5.228.132 unknown Sun, 21 Sep 2025 23:13:34 +0800 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 68D0161E.001/4cV8rt4Lqfz4xPS7 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: xu xin This remove the restriction CONFIG_PROCFS=3Dy for the heler function ksm_process_profit(), then we can use it for the later patches on CONFIG_PROCFS=3Dn. Reported-by: kernel test robot Closes: https://lore.kernel.org/all/202509142046.QatEaTQV-lkp@intel.com/ Signed-off-by: xu xin --- mm/ksm.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index e49f4b86ffb0..a68d4b37b503 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3282,7 +3282,6 @@ static void wait_while_offlining(void) } #endif /* CONFIG_MEMORY_HOTREMOVE */ -#ifdef CONFIG_PROC_FS /* * The process is mergeable only if any VMA is currently * applicable to KSM. @@ -3307,7 +3306,6 @@ long ksm_process_profit(struct mm_struct *mm) return (long)(mm->ksm_merging_pages + mm_ksm_zero_pages(mm)) * PAGE_SIZE - mm->ksm_rmap_items * sizeof(struct ksm_rmap_item); } -#endif /* CONFIG_PROC_FS */ #ifdef CONFIG_MEMCG struct memcg_ksm_stat { --=20 2.25.1 From nobody Thu Oct 2 06:19:39 2025 Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB1C3282E1; Sun, 21 Sep 2025 15:14:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=183.62.165.209 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758467673; cv=none; b=c4GjT4yq7BVDBBhZsw8DQgN5/m3bt+NY4SMNcNfpJYFlhJ0NR+Fw3f3fUmdIunGTP2iUdg7Qi9yTARoz19ZpNuW8HX4+iP7SGy6NJN7ARVObQu6hF3gcTtKhmqCmsb4IJbHBk4daUybaba2sNLl7bniRpVExY/hhhYijmUiNKIw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758467673; c=relaxed/simple; bh=Hh9jyzgQeiIkRT2nDYmtBJoh5930RBsuhwLrqFEMp04=; h=Date:Message-ID:In-Reply-To:References:Mime-Version:From:To:Cc: Subject:Content-Type; b=Yo67hR3bugHZ5DpW9DxBOilQAhdIKl9uBMJ/tPByvxIJzbWwv94wZQYapSZpNGYVaZHXrprpWkrhL3KE1m4Wy62TsjGJNLPLnM7UIpI1PvzMIjLwnLTE70rsys1/RMz11u3vYAD5xLQVOy3zsik7SNsUusO5ED9ZZhJ7MD/TDB8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=183.62.165.209 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4cV8sq4ggvz4xPS7; Sun, 21 Sep 2025 23:14:23 +0800 (CST) Received: from xaxapp04.zte.com.cn ([10.99.98.157]) by mse-fl2.zte.com.cn with SMTP id 58LFEKLb056996; Sun, 21 Sep 2025 23:14:20 +0800 (+08) (envelope-from xu.xin16@zte.com.cn) Received: from mapi (xaxapp05[null]) by mapi (Zmail) with MAPI id mid32; Sun, 21 Sep 2025 23:14:23 +0800 (CST) Date: Sun, 21 Sep 2025 23:14:23 +0800 (CST) X-Zmail-TransId: 2afc68d0164f141-02cd9 X-Mailer: Zmail v1.0 Message-ID: <202509212314234220Nz2yHDfXTgwoTRJwuZTs@zte.com.cn> In-Reply-To: <20250921230726978agBBWNsPLi2hCp9Sxed1Y@zte.com.cn> References: 20250921230726978agBBWNsPLi2hCp9Sxed1Y@zte.com.cn Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 From: To: , Cc: , , , , , , , , , Subject: =?UTF-8?B?W1BBVENIIGxpbnV4LW5leHQgdjMgNS82XSBtZW1jZzogYWRkIHBlci1tZW1jZyBrc21fcHJvZml0?= X-MAIL: mse-fl2.zte.com.cn 58LFEKLb056996 X-TLS: YES X-SPF-DOMAIN: zte.com.cn X-ENVELOPE-SENDER: xu.xin16@zte.com.cn X-SPF: None X-SOURCE-IP: 10.5.228.133 unknown Sun, 21 Sep 2025 23:14:23 +0800 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 68D0164F.000/4cV8sq4ggvz4xPS7 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: xu xin Users can obtain ksm_profit of a cgroup just by: 'cat /sys/fs/cgroup/memory.stat | grep ksm_profit Signed-off-by: xu xin --- mm/ksm.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index a68d4b37b503..55329398797f 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3307,11 +3307,18 @@ long ksm_process_profit(struct mm_struct *mm) mm->ksm_rmap_items * sizeof(struct ksm_rmap_item); } +static inline long ksm_general_profit(void) +{ + return (ksm_pages_sharing + atomic_long_read(&ksm_zero_pages)) * PAGE_SIZ= E - + ksm_rmap_items * sizeof(struct ksm_rmap_item); +} + #ifdef CONFIG_MEMCG struct memcg_ksm_stat { unsigned long ksm_rmap_items; long ksm_zero_pages; unsigned long ksm_merging_pages; + long ksm_profit; }; static int evaluate_memcg_ksm_stat(struct task_struct *task, void *arg) @@ -3324,6 +3331,7 @@ static int evaluate_memcg_ksm_stat(struct task_struct= *task, void *arg) ksm_stat->ksm_rmap_items +=3D mm->ksm_rmap_items; ksm_stat->ksm_zero_pages +=3D mm_ksm_zero_pages(mm); ksm_stat->ksm_merging_pages +=3D mm->ksm_merging_pages; + ksm_stat->ksm_profit +=3D ksm_process_profit(mm); mmput(mm); } @@ -3341,11 +3349,13 @@ void memcg_stat_ksm_show(struct mem_cgroup *memcg, = struct seq_buf *s) ksm_stat.ksm_zero_pages =3D atomic_long_read(&ksm_zero_pages); ksm_stat.ksm_merging_pages =3D ksm_pages_shared + ksm_pages_sharing; + ksm_stat.ksm_profit =3D ksm_general_profit(); } else { /* Initialization */ ksm_stat.ksm_rmap_items =3D 0; ksm_stat.ksm_zero_pages =3D 0; ksm_stat.ksm_merging_pages =3D 0; + ksm_stat.ksm_profit =3D 0; /* Summing all processes'ksm statistic items */ mem_cgroup_scan_tasks(memcg, evaluate_memcg_ksm_stat, &ksm_stat); } @@ -3353,6 +3363,7 @@ void memcg_stat_ksm_show(struct mem_cgroup *memcg, st= ruct seq_buf *s) seq_buf_printf(s, "ksm_rmap_items %lu\n", ksm_stat.ksm_rmap_items); seq_buf_printf(s, "ksm_zero_pages %lu\n", ksm_stat.ksm_zero_pages); seq_buf_printf(s, "ksm_merging_pages %lu\n", ksm_stat.ksm_merging_pages); + seq_buf_printf(s, "ksm_profit %lu\n", ksm_stat.ksm_profit); } #endif @@ -3647,12 +3658,7 @@ KSM_ATTR_RO(ksm_zero_pages); static ssize_t general_profit_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { - long general_profit; - - general_profit =3D (ksm_pages_sharing + atomic_long_read(&ksm_zero_pages)= ) * PAGE_SIZE - - ksm_rmap_items * sizeof(struct ksm_rmap_item); - - return sysfs_emit(buf, "%ld\n", general_profit); + return sysfs_emit(buf, "%ld\n", ksm_general_profit()); } KSM_ATTR_RO(general_profit); --=20 2.25.1 From nobody Thu Oct 2 06:19:39 2025 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [160.30.148.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2D4BD6FC3; Sun, 21 Sep 2025 15:15:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=160.30.148.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758467728; cv=none; b=VI+5MtRwTDWWtSxt3l1mlyDlMqmGC1zoV5QGJpLH7VOoBrB0ClL9sFGJj0nJJ2Y+mUE8gskb7qkaqT76SLgIUgiTAD/w4oMU150wDMIJoSXsOiSh8cVC2TWs1zf+4wPRSFvuYcnFPvc6+b6nBhyIgvvdWr5xTyb7ZcIHeUyVi14= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758467728; c=relaxed/simple; bh=jdkC5I1ywmOk+XVUobrz8tW9x5D6SlXX/o0ckoJqeYY=; h=Date:Message-ID:In-Reply-To:References:Mime-Version:From:To:Cc: Subject:Content-Type; b=M8V/hAv4g/SemWqskBy5ccfh+6cBRfABOzLZt8utAqPH1pEELBap7ezdNsCi9c7fBzfepXVHI5e3D0+yVqZ5yMHpYpk+YL9Bz0XxngLGg72Lx2NsdRP59AgMDPydsuW83iXLHJHlv52bqKWK1d1gHrxf8kgY/aeb8Wodm0t15W4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=160.30.148.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4cV8v10dxDz5PM35; Sun, 21 Sep 2025 23:15:25 +0800 (CST) Received: from xaxapp02.zte.com.cn ([10.88.97.241]) by mse-fl1.zte.com.cn with SMTP id 58LFFFLK072474; Sun, 21 Sep 2025 23:15:15 +0800 (+08) (envelope-from xu.xin16@zte.com.cn) Received: from mapi (xaxapp02[null]) by mapi (Zmail) with MAPI id mid32; Sun, 21 Sep 2025 23:15:18 +0800 (CST) Date: Sun, 21 Sep 2025 23:15:18 +0800 (CST) X-Zmail-TransId: 2afa68d01686f1c-20a97 X-Mailer: Zmail v1.0 Message-ID: <202509212315188885yTwMVyN-Xn_6tuRc3Zi7@zte.com.cn> In-Reply-To: <20250921230726978agBBWNsPLi2hCp9Sxed1Y@zte.com.cn> References: 20250921230726978agBBWNsPLi2hCp9Sxed1Y@zte.com.cn Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 From: To: , Cc: , , , , , , , , , Subject: =?UTF-8?B?W1BBVENIIGxpbnV4LW5leHQgdjMgNi82XSBEb2N1bWVudGF0aW9uOiBhZGQgS1NNIHN0YXRpc3RpYyBjb3VudGVyc8KgZGVzY3JpcHRpb24=?= X-MAIL: mse-fl1.zte.com.cn 58LFFFLK072474 X-TLS: YES X-SPF-DOMAIN: zte.com.cn X-ENVELOPE-SENDER: xu.xin16@zte.com.cn X-SPF: None X-SOURCE-IP: 10.5.228.132 unknown Sun, 21 Sep 2025 23:15:25 +0800 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 68D0168D.000/4cV8v10dxDz5PM35 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: xu xin This add KSM-related statistic counters description in cgroup-v2.rst, including "ksm_rmap_items", "ksm_zero_pages", "ksm_merging_pages" and "ksm_profit". Signed-off-by: xu xin --- Documentation/admin-guide/cgroup-v2.rst | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-= guide/cgroup-v2.rst index a1e3d431974c..c8c4faa4e3fd 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1776,6 +1776,23 @@ The following nested keys are defined. up if hugetlb usage is accounted for in memory.current (i.e. cgroup is mounted with the memory_hugetlb_accounting option). + ksm_rmap_items + Number of ksm_rmap_item structures in use. The structure + ksm_rmap_item stores the reverse mapping information for virtual + addresses. KSM will generate a ksm_rmap_item for each + ksm-scanned page of the process. + + ksm_zero_pages + Number of empty pages are merged with kernel zero pages by KSM, + which is only useful when /sys/kernel/mm/ksm/use_zero_pages. + + ksm_merging_pages + Number of pages of this process are involved in KSM merging + (not including ksm_zero_pages). + + ksm_profit + Amount of profitable memory that KSM brings (Saved bytes). + memory.numa_stat A read-only nested-keyed file which exists on non-root cgroups. --=20 2.25.1