From patchwork Tue Oct 11 02:22:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: xu xin X-Patchwork-Id: 13003561 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 936A8C433F5 for ; Tue, 11 Oct 2022 02:22:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2D39C8E0001; Mon, 10 Oct 2022 22:22:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 25BB96B0074; Mon, 10 Oct 2022 22:22:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0BB3B8E0001; Mon, 10 Oct 2022 22:22:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id E60BC6B0073 for ; Mon, 10 Oct 2022 22:22:37 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B823A402CA for ; Tue, 11 Oct 2022 02:22:37 +0000 (UTC) X-FDA: 80007069954.04.B35171E Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) by imf22.hostedemail.com (Postfix) with ESMTP id 669B6C0028 for ; Tue, 11 Oct 2022 02:22:37 +0000 (UTC) Received: by mail-pf1-f193.google.com with SMTP id g28so12255691pfk.8 for ; Mon, 10 Oct 2022 19:22:37 -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=jBGBO38UtGSWwXVp7Ms/n5NhA5Pd+Mi6T8z7EXrrTqE=; b=NQwP6y9bldduXueh4GTe05alGLYH2cXxs6iR6vqImjtYM+ijwRDNkiUFqRfINXVKnc lPNsjMlrdYCD/aQbcS+3J5F6ov+nKAIyIz8fHQnOMnJMVayFdHpk+dUzr0/ShM/aQ4zQ PIYCeXOqyA6j/JdcmzrfsfvNdk95wsVOFdb0rARM1VmPokMffWAPbzgNsgUTBRSW3iQI TGQJmqYdZd5YOSMSmSsSCgLgXogoXIY0JmT/e/olVLdSk5Suu0uIkOGokMlh+UnvnvLC WjzAbxhTg04rIwWiOQcOLB4kfxGpAL7tYhAK4fjuIiOODSi2rgBDazceiLPQFN5OD7rP VPSA== 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=jBGBO38UtGSWwXVp7Ms/n5NhA5Pd+Mi6T8z7EXrrTqE=; b=x9hHF2twDdn0IeZOxZzNZJLUNhST/5rhrMIv+D2uj7j4+jwOq0MIN4PZ3vQ82EdNNC HKes9FI97sm9x5iLqYdWgTnVNk1O+kTDdFHpic9781UQvaR68ARI63lmyO1M6heRH4H/ eIiFnN4jDV+RcUEhlvdYCepvYcYGdYvyOBAmhotG7mf4GtPIOyHC0N6B5pOWuvZKzYii rFHww9vfHRqGixlN6lLjFDlZel62Mkkm11+Owgcqjb50WzwHppMh4c6JRnBTm3Z0e/Dn 62V2YEUOMmUfpz44IpSYMfLgjPtmGn83pp7JBEG6nXHLpJCWDbtzAMwga5B3nEE8szpJ HXbw== X-Gm-Message-State: ACrzQf38DsisRaB0b5Jzv+X6U3r5QgdmiBxi5HzgGj8+sPuE8JyB0z+x 3c39O4toZHwb5nMzn+71lbg= X-Google-Smtp-Source: AMsMyM4HJO1bRVw+FEpT/jpF8AW/W1KC5CylFOLFTiM7r3HksmwbDm+KaSDXGiVpS8apzQDl8EckZQ== X-Received: by 2002:a05:6a00:2291:b0:563:9d0d:62ae with SMTP id f17-20020a056a00229100b005639d0d62aemr3557524pfe.17.1665454956397; Mon, 10 Oct 2022 19:22:36 -0700 (PDT) Received: from localhost.localdomain ([193.203.214.57]) by smtp.gmail.com with ESMTPSA id m15-20020a17090a158f00b0020d6fc00072sm69660pja.9.2022.10.10.19.22.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Oct 2022 19:22:35 -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 3/5] ksm: count all zero pages placed by KSM Date: Tue, 11 Oct 2022 02:22:30 +0000 Message-Id: <20221011022230.322323-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=1665454957; a=rsa-sha256; cv=none; b=mVXsqQbSwuzyfmO4bokBQGJS07Ax3EOpsQObrdm+AkLsiQSmAKlgC13KthofaX1zdS7ZCa B3u7oO8YWv/UzncTiDeKfAI6+uIQx+zNzS2aYiUKcUculSegGx1OaapV9qiQD4Dml278wO yATGmE2lmHmE3XZaDxg0XJT4dzSwdZU= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=NQwP6y9b; spf=pass (imf22.hostedemail.com: domain of xu.xin.sc@gmail.com designates 209.85.210.193 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=1665454957; 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=jBGBO38UtGSWwXVp7Ms/n5NhA5Pd+Mi6T8z7EXrrTqE=; b=BtpQ48Xg9+dRBwO4ZnvVRZETGf0ArrfRf+MJCS3i0jQRw9kw9ev3XyNFHvVKbUxVcJfsFL PSJIkkiFbyfAxgZoGPSwdeZdPkRsoXHkONLk7cQ/+TT/WfreP2wo4rz25B8HdxFEvzdGjp +ytWjdKuGj2blS3eqGeFRxlrAmT9S+Q= Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=NQwP6y9b; spf=pass (imf22.hostedemail.com: domain of xu.xin.sc@gmail.com designates 209.85.210.193 as permitted sender) smtp.mailfrom=xu.xin.sc@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Stat-Signature: wshrhost58iddddyhsbemyjnbw86uzjd X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 669B6C0028 X-Rspam-User: X-HE-Tag: 1665454957-388232 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 pages_sharing and pages_shared don't include the number of zero pages merged by KSM, we cannot know how many pages are zero pages placed by KSM when enabling use_zero_pages, which leads to KSM not being transparent with all actual merged pages by KSM. In the early days of use_zero_pages, zero-pages was unable to get unshared by the ways like MADV_UNMERGEABLE so it's hard to count how many times one of those zeropages was then unmerged. But now, unsharing KSM-placed zero page accurately has been achieved, so we can easily count both how many times a page full of zeroes was merged with zero-page and how many times one of those pages was then unmerged. and so, it helps to estimate memory demands when each and every shared page could get unshared. So we add zero_pages_sharing under /sys/kernel/mm/ksm/ to show the number of all zero pages placed by KSM. Cc: Claudio Imbrenda Cc: David Hildenbrand Cc: Xuexin Jiang Cc: Xiaokai Ran Cc: Yang Yang Signed-off-by: xu xin --- mm/ksm.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index e351d7b6d15e..2970a7062db6 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -275,6 +275,9 @@ static unsigned int zero_checksum __read_mostly; /* Whether to merge empty (zeroed) pages with actual zero pages */ static bool ksm_use_zero_pages __read_mostly; +/* The number of zero pages placed by KSM use_zero_pages */ +static unsigned long ksm_zero_pages_sharing; + #ifdef CONFIG_NUMA /* Zeroed when merging across nodes is not allowed */ static unsigned int ksm_merge_across_nodes = 1; @@ -542,8 +545,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) - unshare_zero_pages(rmap_item); + if (rmap_item->address & ZERO_PAGE_FLAG) { + if (!unshare_zero_pages(rmap_item)) + ksm_zero_pages_sharing--; + } ksm_rmap_items--; rmap_item->mm->ksm_rmap_items--; rmap_item->mm = NULL; /* debug safety */ @@ -2075,8 +2080,10 @@ static int try_to_merge_with_kernel_zero_page(struct mm_struct *mm, * In case of failure, the page was not really empty, so we * need to continue. Otherwise we're done. */ - if (!err) + if (!err) { rmap_item->address |= ZERO_PAGE_FLAG; + ksm_zero_pages_sharing++; + } } return err; @@ -2178,6 +2185,7 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite * to reset the flag and update the corresponding count. */ rmap_item->address &= PAGE_MASK; + ksm_zero_pages_sharing--; } } @@ -3190,6 +3198,13 @@ static ssize_t pages_volatile_show(struct kobject *kobj, } KSM_ATTR_RO(pages_volatile); +static ssize_t zero_pages_sharing_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%ld\n", ksm_zero_pages_sharing); +} +KSM_ATTR_RO(zero_pages_sharing); + static ssize_t stable_node_dups_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -3250,6 +3265,7 @@ static struct attribute *ksm_attrs[] = { &merge_across_nodes_attr.attr, #endif &max_page_sharing_attr.attr, + &zero_pages_sharing_attr.attr, &stable_node_chains_attr.attr, &stable_node_dups_attr.attr, &stable_node_chains_prune_millisecs_attr.attr,