From patchwork Tue Dec 8 09:51:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11958189 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 23FB6C433FE for ; Tue, 8 Dec 2020 09:53:48 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1670523A84 for ; Tue, 8 Dec 2020 09:53:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1670523A84 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 41CD76B005C; Tue, 8 Dec 2020 04:53:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3CD7E6B005D; Tue, 8 Dec 2020 04:53:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2BBD76B0068; Tue, 8 Dec 2020 04:53:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0006.hostedemail.com [216.40.44.6]) by kanga.kvack.org (Postfix) with ESMTP id 1639D6B005C for ; Tue, 8 Dec 2020 04:53:44 -0500 (EST) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id CFF948249980 for ; Tue, 8 Dec 2020 09:53:43 +0000 (UTC) X-FDA: 77569653126.15.house80_3414162273e6 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin15.hostedemail.com (Postfix) with ESMTP id AB5151814B0C1 for ; Tue, 8 Dec 2020 09:53:43 +0000 (UTC) X-HE-Tag: house80_3414162273e6 X-Filterd-Recvd-Size: 5789 Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by imf29.hostedemail.com (Postfix) with ESMTP for ; Tue, 8 Dec 2020 09:53:42 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id x15so6725314pll.2 for ; Tue, 08 Dec 2020 01:53:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=6CIOYlRgtphmf+WmK1R+aOhJzgaX/le8P/Q7sWamhAQ=; b=F38cAAV8Hsq7gNN4wz6Mlgpz78c9+U/9pMam2t514mSZvpMBMINEdbTKhTTGebaqxz tbZmtyC62U+wSA7GToz5yGiCDhXoeFbnGTc9w8nEDjfsHAwmMTaA7h7WcgL6lUEGajpr 8vcZ40JKiLE+/RbRitqJSgAKVPmT6b9UHSOOE0gUoLfarLPo90g2jElNiJ+XvZtL2UAh u/RHXEcOqJZ2nxpxsJ155rLUV8wlysExb2Fc3FOc+1h32VXw8fpDS1TXmDRvJ8wmmXRS oy8LggNdBHaJAHu9rYDImijFL6FstkicZjOiijpa7dcSO4KIhPTlF2PHnmnzsKXN52DN mWpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=6CIOYlRgtphmf+WmK1R+aOhJzgaX/le8P/Q7sWamhAQ=; b=G64QMmeTBInT7IAUxiwDtzTUbNsrjdrceqiBzDMEfB0yuQ9lx8jmK2AYIRRjYTsYHD kpFy79FHGuKqqaLnkA0+ZtsUNz+dmQ3BRe1wQoWx8HeDCPqZeJoHC/Vo18UVasGIgp1J N2KbSMPRz9Up5bDfxyXv6LjgcUnCgpcxoNZDQiO7G9Ejw8b/ANoX0bjmwcK0VG6RnT4i o9BbiRnAe73lwCZm8rvVe3qoEvyuOnWsvBMn9ST9Zy8tJe5Z/DpvivzpyaJ81SS/S3J2 Jix3pfD2MbPgCA182bfTQa4+EHo+5AEn1IKjPAm05C1r/dgGuUJEGj8WQzNgfxF00/mH 11zQ== X-Gm-Message-State: AOAM532uSHhwWIQuAImd6rU58+2GAfUzjaYLCRqIllfHTRcxBwouuEMJ ZNcXLdA5+KHGE3jwhCAqWCTrIg== X-Google-Smtp-Source: ABdhPJx6nCLdNO2wjDxRZ5BSzSgHPcLiRtQ+HqNdJSQt3D7SF5MhWPaaIZRVjVcBUrh4jPLZNuAlXw== X-Received: by 2002:a17:902:a404:b029:d6:ebe0:6e57 with SMTP id p4-20020a170902a404b02900d6ebe06e57mr20588719plq.12.1607421222034; Tue, 08 Dec 2020 01:53:42 -0800 (PST) Received: from localhost.localdomain ([103.136.221.73]) by smtp.gmail.com with ESMTPSA id w9sm2496281pjq.0.2020.12.08.01.53.36 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Dec 2020 01:53:41 -0800 (PST) From: Muchun Song To: hannes@cmpxchg.org, mhocko@kernel.org, vdavydov.dev@gmail.com, akpm@linux-foundation.org, shakeelb@google.com, guro@fb.com, sfr@canb.auug.org.au, chris@chrisdown.name, laoar.shao@gmail.com, richard.weiyang@gmail.com Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Muchun Song Subject: [PATCH v2] mm: memcontrol: optimize per-lruvec stats counter memory usage Date: Tue, 8 Dec 2020 17:51:32 +0800 Message-Id: <20201208095132.79383-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) MIME-Version: 1.0 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: The vmstat threshold is 32 (MEMCG_CHARGE_BATCH), so the type of s32 of lruvec_stat_cpu is enough. And introduce struct per_cpu_lruvec_stat to optimize memory usage. The size of struct lruvec_stat is 304 bytes on 64 bits system. As it is a per-cpu structure. So with this patch, we can save 304 / 2 * ncpu bytes per-memcg per-node where ncpu is the number of the possible CPU. If there are c memory cgroup (include dying cgroup) and n NUMA node in the system. Finally, we can save (152 * ncpu * c * n) bytes. Signed-off-by: Muchun Song Reviewed-by: Shakeel Butt --- Changes in v1 -> v2: - Update the commit log to point out how many bytes that we can save. include/linux/memcontrol.h | 6 +++++- mm/memcontrol.c | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 3febf64d1b80..290d6ec8535a 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -92,6 +92,10 @@ struct lruvec_stat { long count[NR_VM_NODE_STAT_ITEMS]; }; +struct per_cpu_lruvec_stat { + s32 count[NR_VM_NODE_STAT_ITEMS]; +}; + /* * Bitmap of shrinker::id corresponding to memcg-aware shrinkers, * which have elements charged to this memcg. @@ -111,7 +115,7 @@ struct mem_cgroup_per_node { struct lruvec_stat __percpu *lruvec_stat_local; /* Subtree VM stats (batched updates) */ - struct lruvec_stat __percpu *lruvec_stat_cpu; + struct per_cpu_lruvec_stat __percpu *lruvec_stat_cpu; atomic_long_t lruvec_stat[NR_VM_NODE_STAT_ITEMS]; unsigned long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS]; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index eec44918d373..da6dc6ca388d 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5198,7 +5198,7 @@ static int alloc_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node) return 1; } - pn->lruvec_stat_cpu = alloc_percpu_gfp(struct lruvec_stat, + pn->lruvec_stat_cpu = alloc_percpu_gfp(struct per_cpu_lruvec_stat, GFP_KERNEL_ACCOUNT); if (!pn->lruvec_stat_cpu) { free_percpu(pn->lruvec_stat_local); @@ -7089,6 +7089,14 @@ static int __init mem_cgroup_init(void) { int cpu, node; + /* + * Currently s32 type (can refer to struct per_cpu_lruvec_stat) is + * used for per-memcg-per-cpu caching of per-node statistics. In order + * to work fine, we should make sure that the overfill threshold can't + * exceed S32_MAX / PAGE_SIZE. + */ + BUILD_BUG_ON(MEMCG_CHARGE_BATCH > S32_MAX / PAGE_SIZE); + cpuhp_setup_state_nocalls(CPUHP_MM_MEMCQ_DEAD, "mm/memctrl:dead", NULL, memcg_hotplug_cpu_dead);