From patchwork Tue Apr 30 06:06:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 13648288 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 B88B8C4345F for ; Tue, 30 Apr 2024 06:06:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4AAF96B0096; Tue, 30 Apr 2024 02:06:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4331F6B0098; Tue, 30 Apr 2024 02:06:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 211986B0099; Tue, 30 Apr 2024 02:06:31 -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 E79E46B0096 for ; Tue, 30 Apr 2024 02:06:30 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7D39BA0F29 for ; Tue, 30 Apr 2024 06:06:30 +0000 (UTC) X-FDA: 82065163740.16.C6EB41B Received: from out-183.mta0.migadu.com (out-183.mta0.migadu.com [91.218.175.183]) by imf29.hostedemail.com (Postfix) with ESMTP id CBEE0120003 for ; Tue, 30 Apr 2024 06:06:28 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=je6PYpsu; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf29.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.183 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714457189; 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=TJurZ26Ei0Ypt+z9OF2t2IjIht8hsBt7wgbBwSaQpf8=; b=fmJwxCnJywGj9IAHWn48gpxkxDdHQdBFXIcZNiZSitx8yswlmJ9lfxSjN1SXYc9HdXifO+ QRogV2lKrhA9ADmcEJCxBRcZAeWk599g6y7YtoaxsPOftXC4NNbye5ALJm6+Els8D3kKGk F+WTGITLa1Ji0RT4YTXrcYqq3dzPq8E= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714457189; a=rsa-sha256; cv=none; b=lpDIVdKshDpo4qh1EVv83adoLor2YuJUnmdsJaVqS5Hbw2v2aDYpZox1cHbBSbShVW+T6c iU6QKu4o3y9TufunHy4Jh/J3I52bxWCXLaOL+BI8YsAdmDLiffWLDmDI1n35wT69erUbdx +SoYP6ISMalNhPi9dw5YnP9L+oK+b1I= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=je6PYpsu; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf29.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.183 as permitted sender) smtp.mailfrom=shakeel.butt@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=1714457187; 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=TJurZ26Ei0Ypt+z9OF2t2IjIht8hsBt7wgbBwSaQpf8=; b=je6PYpsuWMMT4ClDbZOM+FDe6Wa7t5KPl7NGfnZGypdcykVsLQ7bzMC/DUlhoH4PbjhXOw kSljsOgicb7sCAwL0LnTAbu6ZwNNVnO66ZdtMa3H8yCDKLM4rtyAKijVBa7NOETK6dJIAT WERHD4+A0LUkC53y/Rl7J/LAm+rUWzM= From: Shakeel Butt To: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Yosry Ahmed , "T . J . Mercier" Cc: kernel-team@meta.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/8] memcg: dynamically allocate lruvec_stats Date: Mon, 29 Apr 2024 23:06:06 -0700 Message-ID: <20240430060612.2171650-3-shakeel.butt@linux.dev> In-Reply-To: <20240430060612.2171650-1-shakeel.butt@linux.dev> References: <20240430060612.2171650-1-shakeel.butt@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: CBEE0120003 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 8wxujr919xcgmdie894bi1m51oirkjah X-HE-Tag: 1714457188-228854 X-HE-Meta: U2FsdGVkX18J0fYf1Px7/T3l5WXlKnshwGtITIktsEb1R0waX/rSjWigC7oxLw2k81XDIq6msvz3stSiwbPtIxzB8dFM1WjVP3Xd2Oc3rDMjzih4gY1C4Z56ooEsHJ/k/Z/nV+XqgyKaY9PYfpMD0LuAA4Pg3zyQRMBVOmmCOzZ6+iyO02zYDM5sQ+kjBK684Wa34tGlTyVo5Ctn29mAR94aKZQbctWWV82e/HDbBYACsv/qLecaO09d/TpGrH/I7PbiodESgExaiyj3cM8BorhmsxMjLKyP55lwIDBjBBhs4KR1BhwbnwOugGr8YnRahQDZApmhe2C2mA8P3gtmGPQSIRQaaSBVnKxObfNOyWZsYCBCU2l4TFv0ywl1RiMI3n6A4nrLxEmWbke8wFicCUv/q1j8YgjMAenS1XXNfEKEgJM+rC1cSF1TI1UpNNcbuiZAPrtjjpPksYQxAMQrvMDqY8se0Pl8vb0CAjJToaVFSQL56hJlQ6ANYwvc8mNZ6ENP70fdBc9+gCVBqUerG8u4QesnW+C/bARdxmtcbjZhzRRfXM9/vKlLcNRxJ1ofbuAILWFj3ZdmL00iYAsSgvHkRbFeg7W+ZsFLsWmmUyGQucs52wEbKNE9BlXcBJGO1WL3pgt8VbwwCcF7xBXY1Z8WnSisUTIsxYdNZeTg6jysyl0J4NeQWJgxm3Xe15zRmUYkFr4TOKSMubUNvcONy0BOFDHhoh7n+CwirlOEykPjGtWE+vZyDSd1JsgQCtGbGmeNrcZT/6bDt/9wckp3/dkDGJdaILFTfrriD+Hi+zU7cWud15r7a+EK0Bo8TPjgK2wutDgUOrAmKKkQYx1nXbn6sAbNSmzg1zz//CLIziGXqyyBAJGvwfCuGTsApph9BLtyvnXKFvmvr7km1vwtImVo2B8EfkQ/2xJaTSykz3X/zDW9E7VYL8IUgA9OKf9QirFra3a0VyywIcK3sUX I/Ie2RpQ eVWkcPbF2+3EhyBrrch4+wGxdZe47QldkCJK5HrG0SttVga9lAkJP4bGwhDzca3LshhH6fdQQSry9Pg2qM9s+8Tc4htBclgGfBfgEr+A/sBFXH4ZSLlCllQQOtAkQI+p262pG1lvCwvPIZyGwpD/fNpqqWA== 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: To decouple the dependency of lruvec_stats on NR_VM_NODE_STAT_ITEMS, we need to dynamically allocate lruvec_stats in the mem_cgroup_per_node structure. Also move the definition of lruvec_stats_percpu and lruvec_stats and related functions to the memcontrol.c to facilitate later patches. No functional changes in the patch. Signed-off-by: Shakeel Butt Reviewed-by: Yosry Ahmed Reviewed-by: T.J. Mercier --- Changes since v2: - N/A include/linux/memcontrol.h | 62 +++------------------------ mm/memcontrol.c | 87 ++++++++++++++++++++++++++++++++------ 2 files changed, 81 insertions(+), 68 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 9aba0d0462ca..ab8a6e884375 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -83,6 +83,8 @@ enum mem_cgroup_events_target { struct memcg_vmstats_percpu; struct memcg_vmstats; +struct lruvec_stats_percpu; +struct lruvec_stats; struct mem_cgroup_reclaim_iter { struct mem_cgroup *position; @@ -90,25 +92,6 @@ struct mem_cgroup_reclaim_iter { unsigned int generation; }; -struct lruvec_stats_percpu { - /* Local (CPU and cgroup) state */ - long state[NR_VM_NODE_STAT_ITEMS]; - - /* Delta calculation for lockless upward propagation */ - long state_prev[NR_VM_NODE_STAT_ITEMS]; -}; - -struct lruvec_stats { - /* Aggregated (CPU and subtree) state */ - long state[NR_VM_NODE_STAT_ITEMS]; - - /* Non-hierarchical (CPU aggregated) state */ - long state_local[NR_VM_NODE_STAT_ITEMS]; - - /* Pending child counts during tree propagation */ - long state_pending[NR_VM_NODE_STAT_ITEMS]; -}; - /* * per-node information in memory controller. */ @@ -116,7 +99,7 @@ struct mem_cgroup_per_node { struct lruvec lruvec; struct lruvec_stats_percpu __percpu *lruvec_stats_percpu; - struct lruvec_stats lruvec_stats; + struct lruvec_stats *lruvec_stats; unsigned long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS]; @@ -1037,42 +1020,9 @@ static inline void mod_memcg_page_state(struct page *page, } unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx); - -static inline unsigned long lruvec_page_state(struct lruvec *lruvec, - enum node_stat_item idx) -{ - struct mem_cgroup_per_node *pn; - long x; - - if (mem_cgroup_disabled()) - return node_page_state(lruvec_pgdat(lruvec), idx); - - pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); - x = READ_ONCE(pn->lruvec_stats.state[idx]); -#ifdef CONFIG_SMP - if (x < 0) - x = 0; -#endif - return x; -} - -static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec, - enum node_stat_item idx) -{ - struct mem_cgroup_per_node *pn; - long x = 0; - - if (mem_cgroup_disabled()) - return node_page_state(lruvec_pgdat(lruvec), idx); - - pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); - x = READ_ONCE(pn->lruvec_stats.state_local[idx]); -#ifdef CONFIG_SMP - if (x < 0) - x = 0; -#endif - return x; -} +unsigned long lruvec_page_state(struct lruvec *lruvec, enum node_stat_item idx); +unsigned long lruvec_page_state_local(struct lruvec *lruvec, + enum node_stat_item idx); void mem_cgroup_flush_stats(struct mem_cgroup *memcg); void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg); diff --git a/mm/memcontrol.c b/mm/memcontrol.c index c146187cda9c..7126459ec56a 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -576,6 +576,60 @@ mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz) return mz; } +struct lruvec_stats_percpu { + /* Local (CPU and cgroup) state */ + long state[NR_VM_NODE_STAT_ITEMS]; + + /* Delta calculation for lockless upward propagation */ + long state_prev[NR_VM_NODE_STAT_ITEMS]; +}; + +struct lruvec_stats { + /* Aggregated (CPU and subtree) state */ + long state[NR_VM_NODE_STAT_ITEMS]; + + /* Non-hierarchical (CPU aggregated) state */ + long state_local[NR_VM_NODE_STAT_ITEMS]; + + /* Pending child counts during tree propagation */ + long state_pending[NR_VM_NODE_STAT_ITEMS]; +}; + +unsigned long lruvec_page_state(struct lruvec *lruvec, enum node_stat_item idx) +{ + struct mem_cgroup_per_node *pn; + long x; + + if (mem_cgroup_disabled()) + return node_page_state(lruvec_pgdat(lruvec), idx); + + pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); + x = READ_ONCE(pn->lruvec_stats->state[idx]); +#ifdef CONFIG_SMP + if (x < 0) + x = 0; +#endif + return x; +} + +unsigned long lruvec_page_state_local(struct lruvec *lruvec, + enum node_stat_item idx) +{ + struct mem_cgroup_per_node *pn; + long x = 0; + + if (mem_cgroup_disabled()) + return node_page_state(lruvec_pgdat(lruvec), idx); + + pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); + x = READ_ONCE(pn->lruvec_stats->state_local[idx]); +#ifdef CONFIG_SMP + if (x < 0) + x = 0; +#endif + return x; +} + /* Subset of vm_event_item to report for memcg event stats */ static const unsigned int memcg_vm_event_stat[] = { PGPGIN, @@ -5491,18 +5545,25 @@ static int alloc_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node) if (!pn) return 1; + pn->lruvec_stats = kzalloc_node(sizeof(struct lruvec_stats), GFP_KERNEL, + node); + if (!pn->lruvec_stats) + goto fail; + pn->lruvec_stats_percpu = alloc_percpu_gfp(struct lruvec_stats_percpu, GFP_KERNEL_ACCOUNT); - if (!pn->lruvec_stats_percpu) { - kfree(pn); - return 1; - } + if (!pn->lruvec_stats_percpu) + goto fail; lruvec_init(&pn->lruvec); pn->memcg = memcg; memcg->nodeinfo[node] = pn; return 0; +fail: + kfree(pn->lruvec_stats); + kfree(pn); + return 1; } static void free_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node) @@ -5513,6 +5574,7 @@ static void free_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node) return; free_percpu(pn->lruvec_stats_percpu); + kfree(pn->lruvec_stats); kfree(pn); } @@ -5865,18 +5927,19 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) for_each_node_state(nid, N_MEMORY) { struct mem_cgroup_per_node *pn = memcg->nodeinfo[nid]; - struct mem_cgroup_per_node *ppn = NULL; + struct lruvec_stats *lstats = pn->lruvec_stats; + struct lruvec_stats *plstats = NULL; struct lruvec_stats_percpu *lstatc; if (parent) - ppn = parent->nodeinfo[nid]; + plstats = parent->nodeinfo[nid]->lruvec_stats; lstatc = per_cpu_ptr(pn->lruvec_stats_percpu, cpu); for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) { - delta = pn->lruvec_stats.state_pending[i]; + delta = lstats->state_pending[i]; if (delta) - pn->lruvec_stats.state_pending[i] = 0; + lstats->state_pending[i] = 0; delta_cpu = 0; v = READ_ONCE(lstatc->state[i]); @@ -5887,12 +5950,12 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) } if (delta_cpu) - pn->lruvec_stats.state_local[i] += delta_cpu; + lstats->state_local[i] += delta_cpu; if (delta) { - pn->lruvec_stats.state[i] += delta; - if (ppn) - ppn->lruvec_stats.state_pending[i] += delta; + lstats->state[i] += delta; + if (plstats) + plstats->state_pending[i] += delta; } } }