Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2726287lqt; Mon, 22 Apr 2024 22:19:34 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXWpc/lgZ0V9AMpKHf92MpjNG1Ov313i7JtVkQ2chHtoDop10U3e70S/XkuPWjtgZDaFNpRe3kfh9+CzfauYgsrvANWAqlAr9JTFhqA9g== X-Google-Smtp-Source: AGHT+IEPpY7k0MSRqBGz/9zaKEU9RdYAQg0048amR+Wm1Z8oJcO9IgfeKncp1gefG9nnaetRuH24 X-Received: by 2002:a17:906:394e:b0:a51:d19c:aba8 with SMTP id g14-20020a170906394e00b00a51d19caba8mr7110278eje.20.1713849574214; Mon, 22 Apr 2024 22:19:34 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713849574; cv=pass; d=google.com; s=arc-20160816; b=O8uHh3qNozEuKIvcI/cYZgkPjGN/BlqXlTWI9oYWntBX+utK/wE+WoKl/7VgFdjsM5 Jqfdpjbxq5HfqDF00a7NADLpGHFAY96GBuQk9M4YUZbT0mW+2unA7dzoa99WxbLrI/mZ xR2Xt5kMDVJsRNfpRvx7QTm9+1eyn+Nevnvbw4GcB4XGWFfy9r7P7PxHAGpP3aIPYT4H Hi3jEm9yJS4Xo7MoyUbWCivNds2piMbNz9v291ruUN/4nwXaBSYJnhOoKJ8mybMILALK isfpsS5t7Hblc/Jskw+cyBA9e+7bfgg6mCfyIhAt5NQBuHV0Dd5Do31SieK1EEnGNiYs 6pMQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=mPVb/7bJYg0jCXHlUHlEkoC8VRMDznvlUA6F/qpq0LU=; fh=RT6qUd/cFk7mA1daRZyPKZ73SkJUv705B/3NiEK9oSU=; b=qFu+lPi8yMZ9oqpxyFVKdCAYrWxUUpbnCXxiMiAeLlxbwBVpV8VBiH6Gw67Zri6pe3 Yyl2CIvVsLQ1Yt/j29vX2pIliVSf6sgXC6FOpmTfhSF11oIqOZyKg8sFp3rHuxQyRvqY pWMvyOfttr9HMLs+Amyi/LKi5DfpcvTqwHNZAHo1Ap/eiwT+Z78MYn+oVKHabhlvHrDJ b18gFWYjuoQbDSv87ht4M13DQFOy6w1HL/Z9wdfF4q6aFRpaMNqLbxxBZqbjd3HUYCIR 7nbv6iJPmnNTfsswDaLRjCC45YnY+oVNLcXVfwAHEIfrgkLizxxfV5Zjup6w5ihIliKg qOHA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=rs11xhPn; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-154522-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154522-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id di22-20020a170906731600b00a55b22c915fsi2750341ejc.277.2024.04.22.22.19.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 22:19:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-154522-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=rs11xhPn; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-154522-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154522-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id ED8421F2220B for ; Tue, 23 Apr 2024 05:19:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0BC4920DC3; Tue, 23 Apr 2024 05:19:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="rs11xhPn" Received: from out-187.mta1.migadu.com (out-187.mta1.migadu.com [95.215.58.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 48F6C225A2 for ; Tue, 23 Apr 2024 05:19:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.187 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713849551; cv=none; b=UQr8UnyRasN37wMk52LTyE/2iU39tuqQxGRjh+wxsv78a233qIJtq5RYaNRwTvxvGryjqwwMquViTyCzpEIuTx9Lv4Wv/JQuuEjnhmafW9gbIqrBbPAElguyeFFmSTaGWQsLvsUxOV+H4VXIjiNMNGiOhNkLHnLeSDEQ+h3XfTA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713849551; c=relaxed/simple; bh=Wmvq48LEHKtnsh9tlioUOV+HIKXT4mpP0GExGJRNVys=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lNqgdC3PyNDBdKlbg+29NMwF32Jco7MKLtsSPVHsD0N+zN4YVIWxubSrp14yvMMfrB/JbO1fQDFpUxBgURlB2DXDy+SCDaqG3sew4Fg6RuaeHHEblvuUGLNqaZ/wohR2kPWu9lc4TItc3/m6CW/AgVX7Vr2oTWcMR75l+1Iqehk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=rs11xhPn; arc=none smtp.client-ip=95.215.58.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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; -- 2.43.0