From patchwork Tue Oct 11 02:22:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: xu xin X-Patchwork-Id: 13003562 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 DB27DC433F5 for ; Tue, 11 Oct 2022 02:22:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 78F7C8E0001; Mon, 10 Oct 2022 22:22:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 716446B0074; Mon, 10 Oct 2022 22:22:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 56A628E0001; Mon, 10 Oct 2022 22:22:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 3AC9E6B0073 for ; Mon, 10 Oct 2022 22:22:54 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 01C9D160814 for ; Tue, 11 Oct 2022 02:22:53 +0000 (UTC) X-FDA: 80007070668.20.01CDEC4 Received: from mail-pj1-f68.google.com (mail-pj1-f68.google.com [209.85.216.68]) by imf27.hostedemail.com (Postfix) with ESMTP id 9A6E14001A for ; Tue, 11 Oct 2022 02:22:53 +0000 (UTC) Received: by mail-pj1-f68.google.com with SMTP id p3-20020a17090a284300b0020a85fa3ffcso14655154pjf.2 for ; Mon, 10 Oct 2022 19:22:53 -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=0i90jsiDje3OippwaWc7d+5Gj/jeYy2WnHfwavKx1Zc=; b=MbQSbPtXvqYlFl1N+H0dMvFlCM2YQF26s9pH/nam6VIgQ2wFgvKeUspQIecinHRbUs Hj9fALXGL82tlCQZCVhYowBU8NZsqc7tDHR7jptur05LWdJ9IRwsjK4tPmvC9LlYRxNr K87nnfFq8DMfBcx9NkiACVW1/uAcUwQ6zoOmr5pPYeNkV9XHunRzCPT+Tktu/drkcLt6 XD6UV/aXHyM686jv87C2eBwtIvvZHSepGPXufc4227d2Em3NzYVP+7cJX59xpguD/Hpn ZgUtsxjWQ8qqRzFCHaF6RYD3lqNp64V+Kg38o3fnJ1bYiKBjdfaagRf8YzMw9q09zHVm 6pPA== 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=0i90jsiDje3OippwaWc7d+5Gj/jeYy2WnHfwavKx1Zc=; b=1t/jeqiHZlDxJasKe9iTRazl7llmnPgwWsgeKAvkMrhd0uAMJsNxKN8VdYVjFFwNH9 56wTAo2CdvL2APIWkKlGxsd6jGCJHT6GJs4OjPyn//foggCWEwqy8QffsGPLI9vHFF+A UVRWsGmMfMLrqadkouxfeRtDBBcVa4ZyNuCnIJiOE4tz4M96uYtJ7L1qHjvWKPHoCQoN mDQfRFxM51H89rtpN+7R26dBV75GHNRLHkiJmqW3i4mp6EgBGlDtJSZLTT670Dbxii3f HXeQjx2gKR6VXI3lfkP/uFer2UvCYSxma8t+e+MJpi0KQAjJeQ8aQfHaa8uaSnqyjKEn U8mg== X-Gm-Message-State: ACrzQf0YYtZTMTRpTVT0VgFGzX+BLINHYyc0qVryQVs0yFYxgbEbslJx adJeeXGne+rW8spW5iDK0Ug= X-Google-Smtp-Source: AMsMyM4z5grjzv3Vvk1EW21MJh3qJtZLK69EMl0iS6JtR43bWHIz9+nVBnfZX/qCphFVvPIBr6OMSw== X-Received: by 2002:a17:902:dad1:b0:183:243c:d0d0 with SMTP id q17-20020a170902dad100b00183243cd0d0mr4632808plx.157.1665454972670; Mon, 10 Oct 2022 19:22:52 -0700 (PDT) Received: from localhost.localdomain ([193.203.214.57]) by smtp.gmail.com with ESMTPSA id e2-20020aa798c2000000b005631a40a00bsm4423241pfm.139.2022.10.10.19.22.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Oct 2022 19:22:52 -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 v3 4/5] ksm: count zero pages for each process Date: Tue, 11 Oct 2022 02:22:46 +0000 Message-Id: <20221011022246.322377-1-xu.xin16@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221011022006.322158-1-xu.xin16@zte.com.cn> References: <20221011022006.322158-1-xu.xin16@zte.com.cn> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1665454973; a=rsa-sha256; cv=none; b=sDDyh7Fv+xcORWwvHg89KwwvAy/9K8T7Sdqb1uiJE7DgFRwg2Vhso8gEDZgrJwB9f5YFns GArKQOsXtKuhx/VybTC9RZT8Vv5cadFm7CUhYhWx+6tML7V/UCdCjax8pXy3EZkpWYWOOD LGSgbrho/0NPsAyaIIgo9hs0RH+H3M8= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=MbQSbPtX; spf=pass (imf27.hostedemail.com: domain of xu.xin.sc@gmail.com designates 209.85.216.68 as permitted sender) smtp.mailfrom=xu.xin.sc@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1665454973; 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=0i90jsiDje3OippwaWc7d+5Gj/jeYy2WnHfwavKx1Zc=; b=ruXFcrc5tCKTPjIJf+UT9aZ4HSYmZ0BTwXLDqmcwWox0S9m4HQUTihsIMt75f1vsVF14IK mVARtnGqC+jR9QlEBEw67KP1h6vUicq+Lt+X9dAw3AFFsyyyEperIQisttJr6X82h8totD QfuEOCX0ofNqrin1bZ75CVKJb9zQLSA= Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=MbQSbPtX; spf=pass (imf27.hostedemail.com: domain of xu.xin.sc@gmail.com designates 209.85.216.68 as permitted sender) smtp.mailfrom=xu.xin.sc@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Stat-Signature: apq47pcg5buyy4musgpkm11iyyzazy5g X-Rspamd-Queue-Id: 9A6E14001A X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1665454973-594104 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 | 6 +++++- 3 files changed, 12 insertions(+), 2 deletions(-) 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 2970a7062db6..c049a95afc26 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -546,8 +546,10 @@ static inline int unshare_zero_pages(struct ksm_rmap_item *rmap_item) 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)) + 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--; @@ -2083,6 +2085,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++; } } @@ -2186,6 +2189,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--; } }