From patchwork Fri Feb 10 01:19:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 55180 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp687741wrn; Thu, 9 Feb 2023 17:39:49 -0800 (PST) X-Google-Smtp-Source: AK7set+W8xlL9A1Vf5miUSWIDjAXr+fdcJ5w88ueNzZnzFVtufTb0vLnlQ0tyGhaWwipXeudPhRn X-Received: by 2002:a05:6a20:669e:b0:c0:61e9:afe8 with SMTP id o30-20020a056a20669e00b000c061e9afe8mr12006566pzh.52.1675993189428; Thu, 09 Feb 2023 17:39:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675993189; cv=none; d=google.com; s=arc-20160816; b=m0v4/BYH78cD5cq8IJ7ZCv7Gda+mIh7VerV5OXeLndaRzdsQO84EfcKiBDW314Qmf8 58ry5JVr508RYG0EosBu8FZEP5A2tigsd3evAl7oII5NdyPI3MJFXHbMCveZlZzehbZB d5rpAm885SAxmwwUz+iLonC0amcxD8ukNw/3V53GjTuh0LTjCaE34geYyJIvhvi1TEw7 znWTYAQ5/CWVhYmZXTQ0Yx/TQKuiRXPAtUBmlm06uUX7RABtyw9SMLL/VyoqHTW7mFKz xkvNzDdKLVaBZRjGwqspIZOZuNzZytiV4OGods3CsBi5nzaD/VEnkX2Fd7mCo+DRZi9f fmdw== 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=kYr5WVQqOlA4UGoUEmtACqMpk4wQYhe20dijyYWJccU=; b=h61Z3qA4yuDZfD85nSol/sEKPKiM3mnWMjSkVpUzGs1AlzeflfQe831+y1sDNOUlsT 17CyokNCt7bLWWQyoMUUdZxrHpvVlQXYWrREHh3yNqADg8VyF2rC9uwbK2QQ7x3OcdM7 nZ+UbIEkVvMwrJDPBtfCEhiqnu3F51YUUO2jP6dZmE8HOAYVtUCSSQnv0c6ytzTlfqK6 /3Mg4LtAxiQpSqRKviiosTQagEENjpE+8/6aRI6eYPKxRPQhxTk6jzEhPP1eA6hnHIzu pmfa3YTVf6Z8b18zPQId7CSEzM9ooBTKgQyJUOhnUbk9ryAQeyCW9eKwp/t3okEKaESE AyiA== 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 z5-20020a626505000000b0058bcb6283edsi2885706pfb.288.2023.02.09.17.39.32; Thu, 09 Feb 2023 17:39:49 -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 S230190AbjBJBUI (ORCPT + 99 others); Thu, 9 Feb 2023 20:20:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229628AbjBJBUG (ORCPT ); Thu, 9 Feb 2023 20:20:06 -0500 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [63.216.63.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 314CC643F9 for ; Thu, 9 Feb 2023 17:20:05 -0800 (PST) Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4PCbWR6Qvjz8R042; Fri, 10 Feb 2023 09:20:03 +0800 (CST) Received: from szxlzmapp02.zte.com.cn ([10.5.231.79]) by mse-fl2.zte.com.cn with SMTP id 31A1JmBv024052; Fri, 10 Feb 2023 09:19:48 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Fri, 10 Feb 2023 09:19:49 +0800 (CST) Date: Fri, 10 Feb 2023 09:19:49 +0800 (CST) X-Zmail-TransId: 2b0363e59bb508349ce2 X-Mailer: Zmail v1.0 Message-ID: <202302100919492571517@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v6_4/6=5D_ksm=3A_count_zero_pages_for_each_proce?= =?utf-8?q?ss?= X-MAIL: mse-fl2.zte.com.cn 31A1JmBv024052 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.250.137.novalocal with ID 63E59BC3.000 by FangMail milter! X-FangMail-Envelope: 1675992003/4PCbWR6Qvjz8R042/63E59BC3.000/10.5.228.133/[10.5.228.133]/mse-fl2.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63E59BC3.000/4PCbWR6Qvjz8R042 X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_00,RCVD_IN_MSPIKE_H2, 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?1757406234842973807?= X-GMAIL-MSGID: =?utf-8?q?1757406234842973807?= 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 Reviewed-by: Claudio Imbrenda --- 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 1fa668e1fe82..42dbcc3ec90d 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. */