From patchwork Sun Oct 9 02:23:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: xu xin X-Patchwork-Id: 13001881 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 AC618C433FE for ; Sun, 9 Oct 2022 02:23:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 436B98E0001; Sat, 8 Oct 2022 22:23:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BD9A6B0074; Sat, 8 Oct 2022 22:23:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 236F28E0001; Sat, 8 Oct 2022 22:23:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 0EED46B0073 for ; Sat, 8 Oct 2022 22:23:46 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id D214481363 for ; Sun, 9 Oct 2022 02:23:45 +0000 (UTC) X-FDA: 79999815210.27.64D7C50 Received: from mail-pj1-f68.google.com (mail-pj1-f68.google.com [209.85.216.68]) by imf22.hostedemail.com (Postfix) with ESMTP id 5D4DDC002B for ; Sun, 9 Oct 2022 02:23:45 +0000 (UTC) Received: by mail-pj1-f68.google.com with SMTP id t12-20020a17090a3b4c00b0020b04251529so7711340pjf.5 for ; Sat, 08 Oct 2022 19:23:45 -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=RoDnBFDHVA7LU5T0eht7qIcxVf5BHA7MxEN+oTzlcX0=; b=ofCcVhUEDdNXNttt6XZggV43bk1949OnzTBSrXtuZcJcps0cu2XwVVz6ksK9cV74m+ Ta2GaV5wrtZWb5wpawvNbHLTiT95mPnnX/yBm3ErBO+AVXBdWFhQDeoxvUSH59k1PU3u IGtk3nC1csD38ozpWW24RtpPRl35romwjkmD+tZpABFsdK2ACdW/6wMqagcJxl9rWkW6 vhCCFF2S0ucKwCd7HOE8EscU085BQf4QpHm9C+PYDiV9ITerSaIZtzJSswOnAjU8vBjy kYGBVBbJSu3aznrKX5wbXkUvRNWGUMmKqWKdwYD405pTUoqT7J8uMHj84NJ1JcX1M2D2 fw3g== 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=RoDnBFDHVA7LU5T0eht7qIcxVf5BHA7MxEN+oTzlcX0=; b=qrMHwUbG+53VjKxolmJ8goTwIG68/LsjpvvzwJLEnVBhidstG81Mh1z9cVjpIAQKGz GPAlASGrlO+rFoHl/pZIJATciJGY0ULzVB6CN7Mjh90mYj/7EbKkhMz71s03gg0o/d5/ rYKCQnvr3CtDlMRaa0MUfx1HmU83VU1Gsl4jMR3QLgJx9vzyHOMVysE/BttkTTuQsLy+ /9cTxWPvGlzMq4HobMhA7qIN9cRxNa/M3oQLNz0wibjJv5Pa7SlB4PWy+kkXzWTmD41z gsgBdAC16FhgorfGyWNwEtSyJxGodT78CqaRB3Y0r4aFpbeb1Tt9dhJJrHkvz5i3TXJj pQUA== X-Gm-Message-State: ACrzQf0QSm9sVEgnlxTF9CP2o7per6Kh2OqQAirkkiAiGi2mv7AWoa9N mcIZo+ei0yPd9P0yipY4+aE= X-Google-Smtp-Source: AMsMyM7SvcN37Jq7m3iuPnNHgqj3NFAaHIJhjx0KT7J46cKR6I1fV87kmBd2iajvV+IMG3jvXDdU1w== X-Received: by 2002:a17:90a:1b43:b0:203:70c0:9f60 with SMTP id q61-20020a17090a1b4300b0020370c09f60mr23779368pjq.184.1665282224445; Sat, 08 Oct 2022 19:23:44 -0700 (PDT) Received: from localhost.localdomain ([193.203.214.57]) by smtp.gmail.com with ESMTPSA id u64-20020a17090a51c600b00200a85fa777sm6826951pjh.1.2022.10.08.19.23.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Oct 2022 19:23:43 -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 v2 4/5] ksm: count zero pages for each process Date: Sun, 9 Oct 2022 02:23:38 +0000 Message-Id: <20221009022338.315428-1-xu.xin16@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221009021816.315205-1-xu.xin16@zte.com.cn> References: <20221009021816.315205-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=1665282225; 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=RoDnBFDHVA7LU5T0eht7qIcxVf5BHA7MxEN+oTzlcX0=; b=5Dl3ROgQdM4C0uY6jxhKWQcJpipLnc7fApkWBND+NueW+mpXo8T9mNk8BHKEAgpmrTBo3c F94veL/hE57K6EDgfMhoBz5EwYrRPREn+3mb7X6sD4hrhdtxcWrx4N4ockuGPEt92BQKs9 57vdaRZxsF4Ye+KXHbyOece7KZkDooM= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=ofCcVhUE; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of xu.xin.sc@gmail.com designates 209.85.216.68 as permitted sender) smtp.mailfrom=xu.xin.sc@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1665282225; a=rsa-sha256; cv=none; b=eTJnBzhWwDIWqBo0ncWDUW9qT916CmR+rxsKPwB9eUYNgrQKk5UFDC/m6DzAZ8FXpAxBI/ nXT5aVcKnoMNCyX9b7tJ2Oa6WN0fs0elvEPeNdpqEZqLNa9xVQSY2f+XRiJNBLgAiXguTO RH6D93/s3lz1hNZq5oGRxvQDFbxccnU= Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=ofCcVhUE; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of xu.xin.sc@gmail.com designates 209.85.216.68 as permitted sender) smtp.mailfrom=xu.xin.sc@gmail.com X-Stat-Signature: 3hrt7e388ra587ogmitsxkrmitm3abwn X-Rspamd-Queue-Id: 5D4DDC002B X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1665282225-678006 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 80672325f179..c1cc3cd370aa 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -545,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) { - 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--; @@ -2082,6 +2084,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 +2188,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--; } }