From patchwork Fri Dec 6 01:35:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Ridong X-Patchwork-Id: 13896223 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 7BE2DE77170 for ; Fri, 6 Dec 2024 01:45:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 235776B0153; Thu, 5 Dec 2024 20:45:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C088D6B0151; Thu, 5 Dec 2024 20:45:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A402B6B0154; Thu, 5 Dec 2024 20:45:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 3F8C26B0153 for ; Thu, 5 Dec 2024 20:45:12 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E5D9C1C8942 for ; Fri, 6 Dec 2024 01:45:11 +0000 (UTC) X-FDA: 82862841096.29.8B22334 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by imf28.hostedemail.com (Postfix) with ESMTP id ECD0BC0007 for ; Fri, 6 Dec 2024 01:44:47 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf28.hostedemail.com: domain of chenridong@huaweicloud.com designates 45.249.212.51 as permitted sender) smtp.mailfrom=chenridong@huaweicloud.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733449494; a=rsa-sha256; cv=none; b=Jd1VNsmwe7nroXz0/5UeSVE0sIolWiFQokVyYlIiIhXvM/KjDlOlenVeRIy1PKy4Ynvu2M Do4ulOTrhJq0PsUydxbyJyE3YpbuOVZ1ljqzMudiQSISWA7zJVVXnuxq+2+EYbibgsJOqp cCqck5N/eEd2BmWwP05L5xwq6J8pwp0= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf28.hostedemail.com: domain of chenridong@huaweicloud.com designates 45.249.212.51 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=1733449494; 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=v8l6Qu8B7zFhjEfKrPVkTwlnR8NKfT7bcZX2irjZ4QQ=; b=SqpSl4eQgX35Z1RBfwqf/up1E8FSG1AoS4VTi5JI5yl5m7moEx3ggptZi8RkgHP6kJjtD7 BcTRdtuN9e8IxuA5/KUm7DkSiq5cE9Gv1O19/7lL87odv3nP9dju5wgsBaP6QCcuvXwgHr TUr9Av1F0laouT3wzncqzadI4zbJfUI= Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Y4Db45WKfz4f3jqy for ; Fri, 6 Dec 2024 09:44:48 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id C65D41A0196 for ; Fri, 6 Dec 2024 09:45:02 +0800 (CST) Received: from hulk-vt.huawei.com (unknown [10.67.174.121]) by APP4 (Coremail) with SMTP id gCh0CgD30oYKV1JnkkskDw--.31315S7; Fri, 06 Dec 2024 09:45:02 +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 Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, chenridong@huawei.com, wangweiyang2@huawei.com Subject: [next -v1 5/5] memcg: factor out stat(event)/stat_local(event_local) reading functions Date: Fri, 6 Dec 2024 01:35:12 +0000 Message-Id: <20241206013512.2883617-6-chenridong@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241206013512.2883617-1-chenridong@huaweicloud.com> References: <20241206013512.2883617-1-chenridong@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD30oYKV1JnkkskDw--.31315S7 X-Coremail-Antispam: 1UD129KBjvJXoWxKw4kZFyrKw4rJr18tryxXwb_yoW7XFWUpF sxKayY93y3Jw4F9r1ftFWjv34rA3WxX3yUJryxJ3ySvasxtw1Yg3ZxKF9rZrW5Cr95ZF13 Jayqyr1DJ3y2qa7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmEb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E 14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7 xfMcIj6xIIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Y z7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcI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kZNxwhQstHPN7pPFCUKKd3pLr3/fsvMk2K//wA1sVlxJgp8LNq1tsaCMDfTBeU/XVZExAPwGRlOiTYkX9x0FirRiObn0tMDz1AvovKDbnUzaST9VmMLMSvJfuUPVJ0H6OVtHRaZYkYMNN3i+8ytbkHqXXWoYzf/esIMQBTnMWDVz8FEfyrsjanJExvpAxzAMiqOteH6Ae7Vstll2fXLYUTy/XxiBsalVPXANvqwB1fenTIPqD7C/SWLVEFzP3Xyy2Z3clF70nGy0O4DbWRiymcJ6q5eccpmK2Ee/KWl/rkQdEbUn50v0mtp1qFbG/xh8FAKaNCybfPEpsTC+S6CKi2QukO6u8LtB6hczKryQE6bUOxPf4fPwACUwW/P7iV7VTqcI2w6e2jy+zzvGMU1Jf+KclUI/kkUYNvhnecgtfROtkN2+qwg5wqxvMs3ViVcZ4xHdzD2xmXGXKqMMel+xXIF07VFVs/j5nhxR6rszcyI6gThDFzNZkEQXwxMG53nhLqrm HUWwqCk7 0ukTdmiY4raL+DhlZOE4x0OM2VqnaoIQen2DPuQtgzmc34mQSZ3HqlYoe1MgyDBDKaZaVDh50dflhPNARqfx6JiTUpymGuCf1Tez24m1Bf1RgP2s+absHHuVyC9YhkxKyGBxjkSjaZuB6tu6+HNHCrJRvksTEOD8evOgLEMWc6/NdxSbPppMqWD7a7g== 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 0c9331d7b606..a1b1409a0dce 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)