From patchwork Fri Dec 30 01:16:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 37542 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2669191wrt; Thu, 29 Dec 2022 17:19:17 -0800 (PST) X-Google-Smtp-Source: AMrXdXvDHe8/mSRTLY1l5MUlhN5/l1LkUg1XOkM22jTxyrgphUv4oL8ZSB0DqjecuigmJfVEzh2i X-Received: by 2002:a17:907:d68b:b0:7c1:691a:6d2c with SMTP id wf11-20020a170907d68b00b007c1691a6d2cmr36003529ejc.7.1672363157315; Thu, 29 Dec 2022 17:19:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672363157; cv=none; d=google.com; s=arc-20160816; b=g0nQGQ+h55ffWlNidlTjUsPT4qVRkeaujbR3SfLsqwQpHQd88b81UeHSjIjIRfJRO6 P7ulkXVqy6FpjvRsuWAvtcQG4Mws3s/HUyGqO+KEtd0xNaSlfBnCxl6c7fhAa6xy3xQw m6rLoUYUqhd4tZpqvEh2BXEgbl3yY3X9Fk4mEsx2ne3S03tlLQ/J37PwbgDRaZVCrrga KcNY1LeXuxLCPL3E0oazy4AZQTGibSXMznAiCXtw0cuk0pFTkPU/26cXjgxDGQO3l3pl /0ZK1Eq41C55HD+g2h1MJj7ZH/DMgCr+PUusex2AK0JVYqwaGHE1uM7QLonPrbTTfzHx J9sg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:subject:cc:to:from:mime-version:message-id:date; bh=lcOvOXzlofwtDdJWJO/YaawEgINJw6332MG6HHQ7ykY=; b=ss1SR+/9qWKp5mLdzLQa66fZ+QF3Jr4gEaRmCfb1Y43Gzxwj+HaJEfPYSl1a+2w+Am BQWo/THbWv2U83GOUXDgs9gNmTndk0/Y+LTSVEhXyn2E5payRyh1EiLfkq4mPlIoi0vT fxwwG7NT5qFihTvhT0pyH7ybpAdIjZr7NO9J6lEYfO8eKPcKoO0A0xLozD1g065HoA9Q xURCQDIdWVekma2KGqYTv7rjs7Ggy2mmgv/rrfOb2Rfmt6nuuehBVu5df/dEvGm6xrxa hNuZAP+D3DspO4ZW2kljSnXGIirHdLxPqIb7sGoCFy0GEn+AYDFuMyWJ2UrenKaZhmfb 2D1w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ji11-20020a170907980b00b0084514612c28si16798031ejc.612.2022.12.29.17.18.53; Thu, 29 Dec 2022 17:19:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234216AbiL3BQh (ORCPT + 99 others); Thu, 29 Dec 2022 20:16:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234169AbiL3BQg (ORCPT ); Thu, 29 Dec 2022 20:16:36 -0500 Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD716FB for ; Thu, 29 Dec 2022 17:16:34 -0800 (PST) Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4NjnQn2W1mz501Qh; Fri, 30 Dec 2022 09:16:33 +0800 (CST) Received: from szxlzmapp06.zte.com.cn ([10.5.230.252]) by mse-fl1.zte.com.cn with SMTP id 2BU1GSAF084154; Fri, 30 Dec 2022 09:16:28 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Fri, 30 Dec 2022 09:16:29 +0800 (CST) Date: Fri, 30 Dec 2022 09:16:29 +0800 (CST) X-Zmail-TransId: 2b0363ae3bed7a8bf824 X-Mailer: Zmail v1.0 Message-ID: <202212300916292181912@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v5_4/6=5D_ksm=3A_count_zero_pages_for_each_proce?= =?utf-8?q?ss?= X-MAIL: mse-fl1.zte.com.cn 2BU1GSAF084154 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.251.13.novalocal with ID 63AE3BF1.000 by FangMail milter! X-FangMail-Envelope: 1672362993/4NjnQn2W1mz501Qh/63AE3BF1.000/10.5.228.132/[10.5.228.132]/mse-fl1.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63AE3BF1.000/4NjnQn2W1mz501Qh X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_00,SORTED_RECIPS, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753599869979645050?= X-GMAIL-MSGID: =?utf-8?q?1753599869979645050?= From: xu xin As the number of ksm zero pages is not included in ksm_merging_pages per process when enabling use_zero_pages, it's unclear of how many actual pages are merged by KSM. To let users accurately estimate their memory demands when unsharing KSM zero-pages, it's necessary to show KSM zero- pages per process. since unsharing zero pages placed by KSM accurately is achieved, then tracking empty pages merging and unmerging is not a difficult thing any longer. Since we already have /proc//ksm_stat, just add the information of zero_pages_sharing in it. Cc: Claudio Imbrenda Cc: David Hildenbrand Cc: Xuexin Jiang Cc: Xiaokai Ran Cc: Yang Yang Signed-off-by: xu xin --- fs/proc/base.c | 1 + include/linux/mm_types.h | 7 ++++++- mm/ksm.c | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/fs/proc/base.c b/fs/proc/base.c index 9e479d7d202b..ac9ebe972be0 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -3207,6 +3207,7 @@ 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, "zero_pages_sharing %lu\n", mm->ksm_zero_pages_sharing); mmput(mm); } diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 4e1031626403..5c734ebc1890 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -776,7 +776,7 @@ struct mm_struct { #ifdef CONFIG_KSM /* * Represent how many pages of this process are involved in KSM - * merging. + * merging (not including ksm_zero_pages_sharing). */ unsigned long ksm_merging_pages; /* @@ -784,6 +784,11 @@ struct mm_struct { * including merged and not merged. */ unsigned long ksm_rmap_items; + /* + * Represent how many empty pages are merged with kernel zero + * pages when enabling KSM use_zero_pages. + */ + unsigned long ksm_zero_pages_sharing; #endif #ifdef CONFIG_LRU_GEN struct { diff --git a/mm/ksm.c b/mm/ksm.c index 72c0722be280..083f5d125373 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -794,6 +794,7 @@ static inline void clean_rmap_item_zero_flag(struct ksm_rmap_item *rmap_item) { if (rmap_item->address & ZERO_PAGE_FLAG) { ksm_zero_pages_sharing--; + rmap_item->mm->ksm_zero_pages_sharing--; rmap_item->address &= PAGE_MASK; } } @@ -2117,6 +2118,7 @@ static int try_to_merge_with_kernel_zero_page(struct ksm_rmap_item *rmap_item, if (!err) { rmap_item->address |= ZERO_PAGE_FLAG; ksm_zero_pages_sharing++; + rmap_item->mm->ksm_zero_pages_sharing++; } } else { /* If the vma is out of date, we do not need to continue. */