From patchwork Sat Oct 8 07:06:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: xu xin X-Patchwork-Id: 13001707 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 F001CC433F5 for ; Sat, 8 Oct 2022 07:06:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 311286B0073; Sat, 8 Oct 2022 03:06:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2BFC06B0074; Sat, 8 Oct 2022 03:06:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 161306B0075; Sat, 8 Oct 2022 03:06:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 02B806B0073 for ; Sat, 8 Oct 2022 03:06:26 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A74061C5C26 for ; Sat, 8 Oct 2022 07:06:25 +0000 (UTC) X-FDA: 79996898730.28.5464944 Received: from mail-pj1-f65.google.com (mail-pj1-f65.google.com [209.85.216.65]) by imf22.hostedemail.com (Postfix) with ESMTP id 469ECC0023 for ; Sat, 8 Oct 2022 07:06:25 +0000 (UTC) Received: by mail-pj1-f65.google.com with SMTP id x1-20020a17090ab00100b001fda21bbc90so9027916pjq.3 for ; Sat, 08 Oct 2022 00:06:24 -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=rrPOD+KhqOFJAEX56XxLC5jHOX5mIwwsCLX+qV5KxT4=; b=LKDDwupQjCXTZCVBlTOqwhB0ZHmLddxOJkaQI9iqpkAm4GBejemWj47pXzHJZBE5Lc 0kZ/v/vwQymfmd/+TkJJVFJtMeMkKl/ULS2YAYwgePcUhsmNUrl9ZgpX1cbeJSnaJwU7 Dw3yNw+wUw+i15xdQ1Pl1Ic5rS8WSgUeA01eGMhN+WNe97QRrGTnY6QLVNe2RgXLVjO9 el8zjjlkjzMHc8ytX9dgKZnz3BWj5/r03iIFgEhnR0kKILmiYW1Y6RkZuKhvl6BHxHG9 34p4HIHximpsSckGEyvvIOaAJ+xecU+iCKiGqP3oQc5TZqpjPxi7Fu2b74fhPq8vydcc 65GA== 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=rrPOD+KhqOFJAEX56XxLC5jHOX5mIwwsCLX+qV5KxT4=; b=1iyruBIHrH8LG+Kyb7avQoMX4ohUhk/TZPbJTgzJzwfxkVCSgFaFEkaZMMvQcq/I3S k76tYhhUANPaKwvvWA4z0M/D9/DT1i3JfwL3IeLtofM69C1zr3F8Dd7IZQrOlIHWBo/Q uGPaMLxg2tB2l2MqARp4cZn7kgHUq/gb4Q3zdUl34kOm970Agi1nioPXVl8T+PTBTrmT YImehL6esUJddwrJViZXOfX9uP3FdBIOECrIs4JsYGRec1RhvNEPf4/7QE9Ctz7X/sqc 1viqHkdbSp8RcX0yZV5SwnkMyTzu4VLkI9cyqxnpXbDXBwt/1H0An8X6Z6JFfBgnEDcy xe1A== X-Gm-Message-State: ACrzQf1aeNUillb1ESWOvHpJt36zQVKI8uWgt99k7I+qrBDx8NR0QfVf b+8xEvbSEeI5V0iMLb/DE0d24z0ulMs= X-Google-Smtp-Source: AMsMyM7NGewJHu/y94jUlrNlEopVSi5jWxeOZAwuJpFctyaF2a3hIMtPMHYAHu62m2I9qFp6SMx9Mg== X-Received: by 2002:a17:90b:2651:b0:20a:daaf:75f0 with SMTP id pa17-20020a17090b265100b0020adaaf75f0mr9004581pjb.142.1665212784299; Sat, 08 Oct 2022 00:06:24 -0700 (PDT) Received: from localhost.localdomain ([193.203.214.57]) by smtp.gmail.com with ESMTPSA id o1-20020a17090a678100b002004380686bsm5557157pjj.46.2022.10.08.00.06.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Oct 2022 00:06:22 -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 3/5] ksm: count all zero pages placed by KSM Date: Sat, 8 Oct 2022 07:06:13 +0000 Message-Id: <20221008070613.308637-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-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1665212785; 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=rrPOD+KhqOFJAEX56XxLC5jHOX5mIwwsCLX+qV5KxT4=; b=iMhpVIsRqcz67dnyx2DYANQPwJbyuDObRk3tSPO1ByPrTH0ixmIfN9UTEXg/0bj/DHK0OP /Jjd0WH4wrIpI8gFw5DK+ihUZXIoRRsmdqdwCCFBl0nrZVEzMLOesXhrH54iK2eg4FYo3V Q+hz7sK9dIED/fyPWIICHQgMO5AfYGU= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=LKDDwupQ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of xu.xin.sc@gmail.com designates 209.85.216.65 as permitted sender) smtp.mailfrom=xu.xin.sc@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1665212785; a=rsa-sha256; cv=none; b=OSU72R2GCD3OlhxTLMdyNZZIweR4PzUlaFMFrPCzl/B2AKYuQw5JJLPzPLuDZO6JzFOg1C 9DRRamd0RBNY5Y8assAkmS1o623Tf9AcajciuEi3CkMks/jntuXa+uUWUhQMnr6hmdFJSW ahlYqKzt7ZjpzWTCQ2aqEUQ+1a4VbEc= Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=LKDDwupQ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of xu.xin.sc@gmail.com designates 209.85.216.65 as permitted sender) smtp.mailfrom=xu.xin.sc@gmail.com X-Stat-Signature: yfcecgba1qbry5iczmke9thapbsqs8sp X-Rspamd-Queue-Id: 469ECC0023 X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1665212785-453140 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 75978f7eeed1..9b7c28abfc89 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; @@ -541,8 +544,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 */ @@ -2074,8 +2079,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; @@ -2177,6 +2184,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--; } } @@ -3189,6 +3197,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) { @@ -3249,6 +3264,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,