From patchwork Tue Apr 23 05:18:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 13639356 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 11F13C4345F for ; Tue, 23 Apr 2024 05:19:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9CB3E6B00B9; Tue, 23 Apr 2024 01:19:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 97A496B00BB; Tue, 23 Apr 2024 01:19:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 81A8E6B00BC; Tue, 23 Apr 2024 01:19:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 62DB46B00B9 for ; Tue, 23 Apr 2024 01:19:11 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 25FBA1C0A6D for ; Tue, 23 Apr 2024 05:19:11 +0000 (UTC) X-FDA: 82039642902.27.225D04D Received: from out-173.mta1.migadu.com (out-173.mta1.migadu.com [95.215.58.173]) by imf22.hostedemail.com (Postfix) with ESMTP id 71B48C000A for ; Tue, 23 Apr 2024 05:19:09 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=rs11xhPn; spf=pass (imf22.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.173 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713849549; a=rsa-sha256; cv=none; b=A+Xv+810JtnYbSHXyofFWKnYWJtSsQeWZOxoP3V8sf1f4/AIT5H0ifAX8aPanLORhN2lFF YuPauBh/K6ZcrZksivujlS8Sjmf5kX6XY7AjMFgcUof3/7Iow3zsEffjtJgN9F2R+QHp86 7FDSrTnayddEe/pG22aS/cAZIOvl7f4= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=rs11xhPn; spf=pass (imf22.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.173 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713849549; 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=mPVb/7bJYg0jCXHlUHlEkoC8VRMDznvlUA6F/qpq0LU=; b=L3voLyuvizt9uJ/pJ+XViyLN9ORrCFeLi+CzZgqXzdsaZtHM6gE4+/y18jE3wzgH7f9x/v 5XHZpZNoLP5WAPDapmsIjRw49NqL33xZ6odfpbhahEzRR1aS9WH6uFf9TmgXil0YnFJahD /9ipPWuXsqrU7QmVNL3tG4TTK/boq2s= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1713849547; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mPVb/7bJYg0jCXHlUHlEkoC8VRMDznvlUA6F/qpq0LU=; b=rs11xhPnd+9ujJmxyiwAvIbaHbCvJYdPVIddaFlw5SnCPmRJf2G7uYXy12IKeEAF5st3Lj b59FvyJ5wKuhH/XVQ6PnB0vVo8qJTGEnv4SY4rfkSAgFn0xbURo2o9NiV0/PjYCL0/JEeB Uv0DlU5Nl1YornvPTybWThJ/C1flxus= From: Shakeel Butt To: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/4] memcg: reduce memory for the lruvec and memcg stats Date: Mon, 22 Apr 2024 22:18:24 -0700 Message-ID: <20240423051826.791934-3-shakeel.butt@linux.dev> In-Reply-To: <20240423051826.791934-1-shakeel.butt@linux.dev> References: <20240423051826.791934-1-shakeel.butt@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 71B48C000A X-Stat-Signature: ts958rwtn5wa9wa6j4hfepwyhj4kzs7n X-Rspam-User: X-HE-Tag: 1713849549-591814 X-HE-Meta: U2FsdGVkX19llFHoJIbnRidd4IRvrZtqoPvbzxqBUT6Cq5jGHepsEfGGKud53Z1T5Je9XouTF0HVx7MVSJv70p9KqSpWP+5M8/N0F8XjJAgzkdKPZUPzyD3Iodb6C1gG+F1V+8JiKfoRPHdTdyIiJRT5LHJcczQRRrHyrMOV/TmdneUMe/d6iH0xQXxK0QH2Ak/UkVzEH7ZQiR1nLYE6mBU1DfD3EPZAX/Lyjrs4Yf8vJk/t3F/E5IooducQqVGEtG5IidfEjadUSOJI8MS5CJq2sLY1qTo/7nnq2n9dnhqju8tYH37WAeKQyDE2J4TDzcHnijJ2h1heXRvzYdDzRMiK+hfqSE81RxzDlXrCYP1qT/Ck2LuV1b80eiPyYDva7VOwr1yyhg/M+NVK3MN+u7FD6Bg2axa4SY1HFcj+7iuRQEvaKRBj67ttYLHdFEj4a/eJFyvR4qQYpX0NeX8c02ZGZ4UYvIOZHGgm8v2LF7kvAGJ2iIIecJBIH1x+akDDr3qYZ5xXhxeAhe8ixBRZS+PO9zb9C1KD+HE2ose2L4k7HvY3e69CW6c7wDtGluOcB8xsszvH5n6ySB5et8TaP5HwGy3GTVXV53jjlG9VZySoTHicpPgO8dBnAa/jO2vL5uhGLSKkasfwMccG5NDt7zTbZwp1Bf1ceMhdbVminUyjxIpksrmcnHONu3x85ZWr0N4QsFk8BjVbodbvcBtqRdXQCbQykgkufkVv0EvNVh682Yo4I5HWDQI/tBJPphx/i4uyfJCL11qemd8Q4PMHzzT4dergtuRrvZpQbPv8s0BxRcIGlbtnqqANBdEFdcYuxEm3glUxo4H5STxKqOotuCOTK/T8uiJhuQrftQ8sxcT0Kap/FM/LiO8CY8FPnf8Pv0pUYAIrWIUu5lqCgVgVUHNroen8vZgBdQk71ZiYeHBXQCwzr28NXVKdn1STSxnHHlwsuR2Vkb+9mtBFpOU gJqWgwjg Mro1AEhPq+P1pBoB0T8JgZcBtDO7vMNWqzKB3/LCo8/xfRGZdsjNwVWgZ2V99zrrqkr9LW23je6xjuT0= 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: List-Subscribe: List-Unsubscribe: At the moment, the amount of memory allocated for stats related structs in the mem_cgroup corresponds to the size of enum node_stat_item. However not all fields in enum node_stat_item has corresponding memcg stats. The fields of enum node_stat_item is sorted in such a way that all the fields with corresponding memcg stats are at the start of the enum node_stat_item. So, let's just make an explicit boundary within enum node_stat_item and use that boundary to allocate memory for stats related structs of memcgs. For a given x86_64 config, the size of stats with and without patch is: structs size in bytes w/o with struct lruvec_stats 1128 648 struct lruvec_stats_percpu 752 432 struct memcg_vmstats 1832 1352 struct memcg_vmstats_percpu 1280 960 The memory savings is further compounded by the fact that these structs are allocated for each cpu and for node. To be precise, for each memcg, the memory saved would be: Memory saved = ((21 * 3 * NR_NODES) + (21 * 2 * NR_NODS * NR_CPUS) + (21 * 3) + (21 * 2 * NR_CPUS)) * sizeof(long) Where 21 is the number of fields eliminated. Signed-off-by: Shakeel Butt --- include/linux/memcontrol.h | 12 ++++++------ include/linux/mmzone.h | 8 ++++++-- mm/memcontrol.c | 5 ++++- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 9aba0d0462ca..d68db7a0e829 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -32,7 +32,7 @@ struct kmem_cache; /* Cgroup-specific page state, on top of universal node page state */ enum memcg_stat_item { - MEMCG_SWAP = NR_VM_NODE_STAT_ITEMS, + MEMCG_SWAP = NR_VM_NODE_MEMCG_STAT_ITEMS, MEMCG_SOCK, MEMCG_PERCPU_B, MEMCG_VMALLOC, @@ -92,21 +92,21 @@ struct mem_cgroup_reclaim_iter { struct lruvec_stats_percpu { /* Local (CPU and cgroup) state */ - long state[NR_VM_NODE_STAT_ITEMS]; + long state[NR_VM_NODE_MEMCG_STAT_ITEMS]; /* Delta calculation for lockless upward propagation */ - long state_prev[NR_VM_NODE_STAT_ITEMS]; + long state_prev[NR_VM_NODE_MEMCG_STAT_ITEMS]; }; struct lruvec_stats { /* Aggregated (CPU and subtree) state */ - long state[NR_VM_NODE_STAT_ITEMS]; + long state[NR_VM_NODE_MEMCG_STAT_ITEMS]; /* Non-hierarchical (CPU aggregated) state */ - long state_local[NR_VM_NODE_STAT_ITEMS]; + long state_local[NR_VM_NODE_MEMCG_STAT_ITEMS]; /* Pending child counts during tree propagation */ - long state_pending[NR_VM_NODE_STAT_ITEMS]; + long state_pending[NR_VM_NODE_MEMCG_STAT_ITEMS]; }; /* diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 989ca97402c6..59592f3c7d9b 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -192,8 +192,12 @@ enum node_stat_item { NR_SHMEM_THPS, NR_FILE_THPS, NR_ANON_THPS, - /* No memcg stats for the following fields. */ - NR_SHMEM_PMDMAPPED, + /* + * No memcg stats for the following fields. Please add stats which have + * memcg counterpart above NR_VM_NODE_MEMCG_STAT_ITEMS. + */ + NR_VM_NODE_MEMCG_STAT_ITEMS, + NR_SHMEM_PMDMAPPED = NR_VM_NODE_MEMCG_STAT_ITEMS, NR_FILE_PMDMAPPED, NR_WRITEBACK_TEMP, /* Writeback using temporary buffers */ NR_VMSCAN_WRITE, diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 833d09c1d523..bb1bbf417a46 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1648,6 +1648,9 @@ static void memcg_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) { int i; + /* Reduce by 1 for MEMCG_SWAP as that is not exposed in v2. */ + BUILD_BUG_ON(ARRAY_SIZE(memory_stats) != MEMCG_NR_STAT - 1); + /* * Provide statistics on the state of the memory subsystem as * well as cumulative event counters that show past behavior. @@ -5869,7 +5872,7 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) lstatc = per_cpu_ptr(pn->lruvec_stats_percpu, cpu); - for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) { + for (i = 0; i < NR_VM_NODE_MEMCG_STAT_ITEMS; i++) { delta = pn->lruvec_stats.state_pending[i]; if (delta) pn->lruvec_stats.state_pending[i] = 0;