From patchwork Tue Jan 14 12:25:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Ridong X-Patchwork-Id: 13938735 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 ED271E77188 for ; Tue, 14 Jan 2025 12:36:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7C31A6B0088; Tue, 14 Jan 2025 07:36:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7726D6B0089; Tue, 14 Jan 2025 07:36:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5ED466B008C; Tue, 14 Jan 2025 07:36:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 425AE6B0088 for ; Tue, 14 Jan 2025 07:36:57 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 64172AF323 for ; Tue, 14 Jan 2025 12:36:08 +0000 (UTC) X-FDA: 83006004816.05.62A7628 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) by imf24.hostedemail.com (Postfix) with ESMTP id B46FF180016 for ; Tue, 14 Jan 2025 12:36:04 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf24.hostedemail.com: domain of chenridong@huaweicloud.com designates 45.249.212.56 as permitted sender) smtp.mailfrom=chenridong@huaweicloud.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736858166; 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; bh=7ydYpndpRJmJFmZQj+DQyRJThoROW0LRsQ/1tw8rMWQ=; b=Mve14cPZsFtQWmyzoaqm3k9w+PSSVO9Ht5z8XWiSp41Q5T44CKY+OdOAexl/Gz1+MsawJp cs/8SZ5xNPKOf8bV7s4Jn6xRrKn9o9W1CaWExRQ8cusqKD1BYTtYVrDtH1vOyyOPniV91i Zd2NBgf2HIbLMoGZHPKiLmHltswape8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736858166; a=rsa-sha256; cv=none; b=DP+aEmUscUivr6R77X2+ktS6yGFAu/4iGELd0m72pO/gx5Ef6fHvMvudZuKAAzjB5OAq3T +97w5yi2e6iu3SOiPjjnPJwzTS+s06JhYmKtIKubiTH+6/4kaFLFRJOqIzgiDbwwdyrkv1 vXmGmOKyXgN92iiuOhx7p4HqpHo0EQQ= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf24.hostedemail.com: domain of chenridong@huaweicloud.com designates 45.249.212.56 as permitted sender) smtp.mailfrom=chenridong@huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4YXTB314mLz4f3kG6 for ; Tue, 14 Jan 2025 20:35:39 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.252]) by mail.maildlp.com (Postfix) with ESMTP id 82B401A0F38 for ; Tue, 14 Jan 2025 20:35:59 +0800 (CST) Received: from hulk-vt.huawei.com (unknown [10.67.174.121]) by APP3 (Coremail) with SMTP id _Ch0CgB3F8IhWoZnN4KvAw--.1325S6; Tue, 14 Jan 2025 20:35:59 +0800 (CST) From: Chen Ridong To: akpm@linux-foundation.org, mhocko@kernel.org, hannes@cmpxchg.org, yosryahmed@google.com, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, davidf@vimeo.com, vbabka@suse.cz, mkoutny@suse.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, chenridong@huawei.com, wangweiyang2@huawei.com Subject: [PATCH -v2 next 4/4] memcg: factor out stat(event)/stat_local(event_local) reading functions Date: Tue, 14 Jan 2025 12:25:19 +0000 Message-Id: <20250114122519.1404275-5-chenridong@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250114122519.1404275-1-chenridong@huaweicloud.com> References: <20250114122519.1404275-1-chenridong@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: _Ch0CgB3F8IhWoZnN4KvAw--.1325S6 X-Coremail-Antispam: 1UD129KBjvJXoWxKw4kZFyrKw4rJr18tryxXwb_yoW7XFWUpF sxtayY9rW3Jw4F9r1ftFWjv34rA3WxX3yUJryxJ3ySvasxtw1Yg3ZxKF9rZrW5Cr95ZF13 Jayqyr1DJ3y2qa7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPqb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E 14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7 xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Y z7v_Jr0_Gr1lF7xvr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mv2Z0b37jDolpl9/VsggwowZsj0Sx7z/262RQRHGCcm7dgt80/UnONoectz7cFAZUbex0H6+A1BSt7U4czGxddek7ry2QE381/8LyqotilsNary/QjUA3IlPWq2XMI1cX3zChUyS+zzF+81YMPphUdxIfZyRG4geLzENwh5cj3tMRz6pojPWA/gmwVgs+nYr7wFZ2VDalk6dweuEa7Lt46rQy3Sf25BtjXeKViPfU+caaKp+fcKr0ROusnUvReHxYErp6HTzGip/yMmGM8PtunDdQ/ddkffNER9kHYtoG+mMPeu6oP6zV7gycqTLAxrPin6+20yUwg5b6i4cJ5oAzWH6XWdqawBr26+LLNWMf5cs7aWaFLGgGFr/6R4Jk5IPeOH4XSU+N/ezoMEDe0D6IETRCql67omPNwEkfcC9aKQS+1al59KTeEmVsrEvvalpsDIvt6fO7KLI9JIFb3qVqnOqAu1Rh1lbp7VOl0IN01eu83og+Qx Xa1k10B5 pB1b8cShaq76tF4NrVaaY2E6EByTnaH5NXGF3Yh0Kwjuy3azroX3huYr428YDAwIGOLG+B83YRmP6VcbF8m0Er6jzfweeAmafe3JIoKh5CMxBOMdTTveazQYx0VkSLYOClNnrXJ26MTrrT9/+FrdfNhPB9AQWsHjh+//K8q7jNPPSYfCBL8tlxkB1vA== 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: From: Chen Ridong The only difference between 'lruvec_page_state' and 'lruvec_page_state_local' is that they read 'state' and 'state_local', respectively. Factor out an inner functions to make the code more concise. Do the same for reading 'memcg_page_stat' and 'memcg_events'. Signed-off-by: Chen Ridong --- mm/memcontrol.c | 72 +++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 42 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index b10e0a8f3375..14541610cad0 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -375,7 +375,8 @@ struct lruvec_stats { long state_pending[NR_MEMCG_NODE_STAT_ITEMS]; }; -unsigned long lruvec_page_state(struct lruvec *lruvec, enum node_stat_item idx) +static unsigned long __lruvec_page_state(struct lruvec *lruvec, + enum node_stat_item idx, bool local) { struct mem_cgroup_per_node *pn; long x; @@ -389,7 +390,8 @@ unsigned long lruvec_page_state(struct lruvec *lruvec, enum node_stat_item idx) return 0; pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); - x = READ_ONCE(pn->lruvec_stats->state[i]); + x = local ? READ_ONCE(pn->lruvec_stats->state_local[i]) : + READ_ONCE(pn->lruvec_stats->state[i]); #ifdef CONFIG_SMP if (x < 0) x = 0; @@ -397,27 +399,16 @@ unsigned long lruvec_page_state(struct lruvec *lruvec, enum node_stat_item idx) return x; } + +unsigned long lruvec_page_state(struct lruvec *lruvec, enum node_stat_item idx) +{ + return __lruvec_page_state(lruvec, idx, false); +} + unsigned long lruvec_page_state_local(struct lruvec *lruvec, enum node_stat_item idx) { - struct mem_cgroup_per_node *pn; - long x; - int i; - - if (mem_cgroup_disabled()) - return node_page_state(lruvec_pgdat(lruvec), idx); - - i = memcg_stats_index(idx); - if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx)) - return 0; - - pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); - x = READ_ONCE(pn->lruvec_stats->state_local[i]); -#ifdef CONFIG_SMP - if (x < 0) - x = 0; -#endif - return x; + return __lruvec_page_state(lruvec, idx, true); } /* Subset of vm_event_item to report for memcg event stats */ @@ -651,7 +642,7 @@ static void flush_memcg_stats_dwork(struct work_struct *w) queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME); } -unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) +static unsigned long __memcg_page_state(struct mem_cgroup *memcg, int idx, bool local) { long x; int i = memcg_stats_index(idx); @@ -659,7 +650,9 @@ unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx)) return 0; - x = READ_ONCE(memcg->vmstats->state[i]); + x = local ? READ_ONCE(memcg->vmstats->state_local[i]) : + READ_ONCE(memcg->vmstats->state[i]); + #ifdef CONFIG_SMP if (x < 0) x = 0; @@ -667,6 +660,11 @@ unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) return x; } +unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) +{ + return __memcg_page_state(memcg, idx, false); +} + static int memcg_page_state_unit(int item); /* @@ -709,18 +707,7 @@ void __mod_memcg_state(struct mem_cgroup *memcg, enum memcg_stat_item idx, /* idx can be of type enum memcg_stat_item or node_stat_item. */ unsigned long memcg_page_state_local(struct mem_cgroup *memcg, int idx) { - long x; - int i = memcg_stats_index(idx); - - if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx)) - return 0; - - x = READ_ONCE(memcg->vmstats->state_local[i]); -#ifdef CONFIG_SMP - if (x < 0) - x = 0; -#endif - return x; + return __memcg_page_state(memcg, idx, true); } static void __mod_memcg_lruvec_state(struct lruvec *lruvec, @@ -859,24 +846,25 @@ void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx, memcg_stats_unlock(); } -unsigned long memcg_events(struct mem_cgroup *memcg, int event) +static unsigned long __memcg_events(struct mem_cgroup *memcg, int event, bool local) { int i = memcg_events_index(event); if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, event)) return 0; - return READ_ONCE(memcg->vmstats->events[i]); + return local ? READ_ONCE(memcg->vmstats->events_local[i]) : + READ_ONCE(memcg->vmstats->events[i]); } -unsigned long memcg_events_local(struct mem_cgroup *memcg, int event) +unsigned long memcg_events(struct mem_cgroup *memcg, int event) { - int i = memcg_events_index(event); - - if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, event)) - return 0; + return __memcg_events(memcg, event, false); +} - return READ_ONCE(memcg->vmstats->events_local[i]); +unsigned long memcg_events_local(struct mem_cgroup *memcg, int event) +{ + return __memcg_events(memcg, event, true); } struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)