From patchwork Sat Oct 8 07:06:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: xu xin X-Patchwork-Id: 13001708 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 391DEC433F5 for ; Sat, 8 Oct 2022 07:06:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C38216B0074; Sat, 8 Oct 2022 03:06:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BE7C26B0075; Sat, 8 Oct 2022 03:06:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A61B76B0078; Sat, 8 Oct 2022 03:06:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 90BE66B0074 for ; Sat, 8 Oct 2022 03:06:51 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 68B6A16015D for ; Sat, 8 Oct 2022 07:06:51 +0000 (UTC) X-FDA: 79996899822.04.D4B2AF4 Received: from mail-pf1-f194.google.com (mail-pf1-f194.google.com [209.85.210.194]) by imf03.hostedemail.com (Postfix) with ESMTP id 0AD642000E for ; Sat, 8 Oct 2022 07:06:50 +0000 (UTC) Received: by mail-pf1-f194.google.com with SMTP id i3so6672729pfk.9 for ; Sat, 08 Oct 2022 00:06:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=U+7cGrSCaB8toCY9LzJTV6eREW3WTAoZDgmk6SweSp0=; b=NI+szN5GnNITjU+LpGlHOTnVh2BNC7vF7YjihBuKzgQvD13g7J70KAt8n01JOrPCGE pfjhUSYusC/G0Rj7vJSaSLmiou6u2wtpZFAj1ygAIjlpggrW7H/0ADcIQM+2DCw11/Tl eM5igZHSw+2qV2dqucWmz5HaEv1wTvni+/GCpHOgRN6oxpsoNJqMmEQOfR65/8AFnqPM 77uv6tGOZYwqebSlCFSjeOWOlK449c1myqh4id/BZLKFXjA3Kd64Cf69h70BkmHFoPL6 F4mERXoSD7EgxjZAVhhAkfR9ipQXWDPZiRaWAN0WpfzMb7XTy+xLNCIR4Xhf6WuBnyfz Jvpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=U+7cGrSCaB8toCY9LzJTV6eREW3WTAoZDgmk6SweSp0=; b=LIUiE2g/Hfqco9d88RoT52K4UMhFmQ4jt/UMv5wLlJHiV+sTdUtFBGHtY4MIfi3TkC iJjefTMN8PnqTSjv/AoXCCjJINqsf297D0QGmJzVLl4iY+btN046KGwWToQtN3rusBlU ZbRly+xF3zORC/zRqhinBtr9RvVwuXiS/pe24f4TGZwWicnvzrnZITGHnA5naKblEiep 9LBdWEdefMv3kNoi+nrxZkhxxrM5aTB3/HC413waZppv9ziuYJFUHQM1+sEh3/S71039 M5uiDKBARMsyHEGocK+S8v0P4g9MumJIVkWfuVimxVGGJAI42N/yXxmkSJtFtJYGH7QM OCWA== X-Gm-Message-State: ACrzQf0Ku9z4KJEnAe6/8Z+U5ewFNxD5z3+18vlaNqbyyyHWdetfMQsC NANRslMK3iymKaa7IGQxAPA= X-Google-Smtp-Source: AMsMyM5y3td7v+CEksjuK/XyY+otk4kTI3C1PTEjt8Qr6W7VoZaJT/xWMQBDRZnZZMRIN+xcHpeLqw== X-Received: by 2002:a63:4283:0:b0:457:dced:8ba3 with SMTP id p125-20020a634283000000b00457dced8ba3mr7736754pga.220.1665212810118; Sat, 08 Oct 2022 00:06:50 -0700 (PDT) Received: from localhost.localdomain ([193.203.214.57]) by smtp.gmail.com with ESMTPSA id pc14-20020a17090b3b8e00b0020a28156e11sm4281098pjb.26.2022.10.08.00.06.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Oct 2022 00:06:49 -0700 (PDT) From: xu.xin.sc@gmail.com X-Google-Original-From: xu.xin16@zte.com.cn To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, xu xin , Claudio Imbrenda , David Hildenbrand , Xuexin Jiang , Xiaokai Ran , Yang Yang Subject: [PATCH 4/5] ksm: count zero pages for each process Date: Sat, 8 Oct 2022 07:06:42 +0000 Message-Id: <20221008070642.308693-1-xu.xin16@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221008070156.308465-1-xu.xin16@zte.com.cn> References: <20221008070156.308465-1-xu.xin16@zte.com.cn> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1665212811; a=rsa-sha256; cv=none; b=E2k6LRGRXPQJg4oQCNXAIVVJmt24bHNbH8s8ukBmt8RUSyzmSij1ejXkmWlwHetGMRy6Dg FuZhZUICs5vLjNPp8PZVGTMIVw2G2wFO2T80qlpzMuZr5VhEJKgX/N7Vpaw0d0B2GhPZkZ aUHvkXAycLI8jKW8xeqb6IVMwYro5uk= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=NI+szN5G; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of xu.xin.sc@gmail.com designates 209.85.210.194 as permitted sender) smtp.mailfrom=xu.xin.sc@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1665212811; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=U+7cGrSCaB8toCY9LzJTV6eREW3WTAoZDgmk6SweSp0=; b=slIibdk/XmhTtREFarLHPPbUMLIIcGSbsmjNBx068JII+4MvUylaiYIkROewlNK2Hx2lal IdIdq5ux+vYGtf9wsmQMOG+wtugh2N3Ehoc5xpcL7Zx0OZxr62AzCTbxNImpIhLgL+2SwV RghMvQo3ek53xFhrivPeo+KadKs9Pmg= X-Rspam-User: X-Stat-Signature: rpf34znh4x4xjjsbc86xmesnedi8awug X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 0AD642000E Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=NI+szN5G; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of xu.xin.sc@gmail.com designates 209.85.210.194 as permitted sender) smtp.mailfrom=xu.xin.sc@gmail.com X-HE-Tag: 1665212810-737743 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 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 | 3 +++ 3 files changed, 10 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 500e536796ca..78a4ee264645 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -691,7 +691,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; /* @@ -699,6 +699,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 9b7c28abfc89..a889b1925f51 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -547,6 +547,7 @@ static inline void free_rmap_item(struct ksm_rmap_item *rmap_item) if (rmap_item->address & ZERO_PAGE_FLAG) { if (!unshare_zero_pages(rmap_item)) ksm_zero_pages_sharing--; + rmap_item->mm->ksm_zero_pages_sharing--; } ksm_rmap_items--; rmap_item->mm->ksm_rmap_items--; @@ -2082,6 +2083,7 @@ static int try_to_merge_with_kernel_zero_page(struct mm_struct *mm, if (!err) { rmap_item->address |= ZERO_PAGE_FLAG; ksm_zero_pages_sharing++; + rmap_item->mm->ksm_zero_pages_sharing++; } } @@ -2185,6 +2187,7 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite */ rmap_item->address &= PAGE_MASK; ksm_zero_pages_sharing--; + rmap_item->mm->ksm_zero_pages_sharing--; } }