From patchwork Fri Jan 17 01:46:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Ridong X-Patchwork-Id: 13942787 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 467A4C02187 for ; Fri, 17 Jan 2025 01:57:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2120E6B0089; Thu, 16 Jan 2025 20:57:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DC94D6B0098; Thu, 16 Jan 2025 20:57:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ABB986B008A; Thu, 16 Jan 2025 20:57:44 -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 306086B0092 for ; Thu, 16 Jan 2025 20:57:44 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E8370161530 for ; Fri, 17 Jan 2025 01:57:43 +0000 (UTC) X-FDA: 83015282406.11.061A2E4 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by imf23.hostedemail.com (Postfix) with ESMTP id 07401140010 for ; Fri, 17 Jan 2025 01:57:39 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf23.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=1737079062; a=rsa-sha256; cv=none; b=Rm32LWl/nrwXqJA2+fXAvF85CVrDRjl0WAsaDK4+1uRn1Ta1W3lMdXk5UsIKcwrDFO8cF6 XOmqgXEYKI7luXyU26Iza+y5HuWvnFTnC0Z5pOR5ar0hoNeugfjMLngXQA7uMjRlTsophZ 91BMXSY6RMuP89Qi341fAaQsEuj9QzU= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf23.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=1737079062; 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=uc7fuHJxjtRSUKrDsMWSn+mR1ozjU1KS6GIgp7TxyME=; b=c2o5nD6fn8Bpxtsgi0I3yIFiq8rmKs5WfJI4hVSP4UPH69r/h9cirm6VE4jVWyv6I2V0Mx R9cguFGSq210C37pj8VoBrSW0soeGCCtU1+ZdM/pXcsvZhLRPCjMwiMQkBM2+xxdM4i1mY 4kPK+8NkqgKB6FjnLRo27y4z12sB8pQ= Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4YZ2t045Qwz4f3l24 for ; Fri, 17 Jan 2025 09:57:12 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 47EAA1A0E11 for ; Fri, 17 Jan 2025 09:57:34 +0800 (CST) Received: from hulk-vt.huawei.com (unknown [10.67.174.121]) by APP2 (Coremail) with SMTP id Syh0CgCnsWT7uIln8NWrBA--.20802S6; Fri, 17 Jan 2025 09:57:34 +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 v3 next 4/5] memcg: factor out stat(event)/stat_local(event_local) reading functions Date: Fri, 17 Jan 2025 01:46:44 +0000 Message-Id: <20250117014645.1673127-5-chenridong@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250117014645.1673127-1-chenridong@huaweicloud.com> References: <20250117014645.1673127-1-chenridong@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: Syh0CgCnsWT7uIln8NWrBA--.20802S6 X-Coremail-Antispam: 1UD129KBjvJXoWxKw4Dtw4kKrWDAw4fZFW3trb_yoWxtFy7pF sxtayY93y3J3yFgr13KFWUZ34rAw1xX3y5JrWxJ3yfZasxt3W5W3ZxKFW7ZrW5Cr95XF13 Jayqyr1DJ3y2qa7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPSb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E 14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7 xfMcIj6xIIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Y z7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1lc7CjxVAaw2 AFwI0_Jw0_GFyl42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAq x4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r 43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF 7I0E14v26F4j6r4UJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI 0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7I UbXAw7UUUUU== X-CM-SenderInfo: hfkh02xlgr0w46kxt4xhlfz01xgou0bp/ X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 07401140010 X-Stat-Signature: siscpra7wwcdtrezzxj36natrryucyth X-HE-Tag: 1737079059-230550 X-HE-Meta: U2FsdGVkX1/xsby+tP7419vdlJFuCwpmCNETbjcnkjsymj6sOy36e8h9AEeckv4yY1QYqOuF5rF3qgphW/d7RwOeHCTCu2zpO3JO3ukMjavn1DJDhFXtgb8DeSn373XB5aBWW09hIfdq0SSCfsroQpwILu1iVYRUZNMuIS4aVYfvRHGj8lvNv3Q6wprpTONfTOhfUzLaP+4Mi5vsJhzIY4UotQJHL90t5Z8ohVuGRUtD+k5FPueoaFZhlqR4UN5s0IheBZl/xBUeCKwzxgwi9A3g79By6RKgpX+zUinjwTwTjlB8nJhv+IHfO/UgZLlo1u7inybM0Nf8CfCgRyNfNiKx4FJrTWfInb3UXtOh2/Og67AGV7Kw5Dph7Dxd8cz2t/eo2gtWQSvGdPzeb3uqkKha0ZSfTZze5kB1AL/3F/iLj6HVqBxGyz4G0KCtu41TWa7dB7CIatPrsevEUUsEevNB9KXvRPDukd8OyilSkp0YLnj/1/5gsJtyGUCi/VdINaiAy2TXFq+SA3cFIJnUhQTHJyToGf0TNDuzjqfArm/Zn4HPF6jcjB3QNRJw166/kbVP6e1ImDpIPeZ9VOdBK+1jrpioimHlcMsVWAVf3+CxCOjM/3KCESW7bMdILwVj5rlAG6FVGoM7adWHgogJJmQhx7b1jFjUhWNDA0JhWzbNYHpxxaqGWWD/3PUKVks2A69aGRoKX+UWpNDhwVll6frJR9SN8AfYE21Zqzrb/884PKVBplsyWnq9xEuzvb18g6I42vMr9PyV7cBDYqUzJ/3/iM/8QlKRVSqJEQWXrZvmQgAYq/SxDF64aCwGp6DyTjVSdwvobM7YDubRc6wztwEmWvKJKCpw0sF+ZsleuwcHXUDnCkCZt7C1TjysyOxIcyDULBIoIw5e3yDd++rG7hd2OaPLIvNiW4q42EMvhuCe55ZrPCbd8r4aE8VAVZYuuOYdbnDBPzyH9ioGmco VFccqBdK oJ5MCb1IjXTvCq4Oe9GRo6tzpJlQ57ocner0jBEWAa0/Nf2dBTVHNFajNHNwv5uMcHbbaRuhXOdEx8FctLSmG/+oFK0sTbdDDpK8QAkackwwfgwGxTrkE5vGR5kO1j4MhTusVPZCp+Z1/DqkcMU2BPMMXDK0RwduTo1KXdnxz5uvimDnWwmJaj1rwJQ== 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 --- include/linux/memcontrol.h | 31 +++++++++++++++--- mm/memcontrol-v1.h | 14 ++++++-- mm/memcontrol.c | 67 +++++++------------------------------- 3 files changed, 49 insertions(+), 63 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 6e74b8254d9b..ec469c5f7491 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -936,10 +936,33 @@ static inline void mod_memcg_page_state(struct page *page, rcu_read_unlock(); } -unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx); -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); +unsigned long __memcg_page_state(struct mem_cgroup *memcg, int idx, bool local); + +/* idx can be of type enum memcg_stat_item or node_stat_item. */ +static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg, int idx) +{ + return __memcg_page_state(memcg, idx, true); +} + +static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) +{ + return __memcg_page_state(memcg, idx, false); +} + +unsigned long __lruvec_page_state(struct lruvec *lruvec, + enum node_stat_item idx, bool local); + +static inline unsigned long lruvec_page_state(struct lruvec *lruvec, + enum node_stat_item idx) +{ + return __lruvec_page_state(lruvec, idx, false); +} + +static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec, + enum node_stat_item idx) +{ + return __lruvec_page_state(lruvec, idx, true); +} void mem_cgroup_flush_stats(struct mem_cgroup *memcg); void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg); diff --git a/mm/memcontrol-v1.h b/mm/memcontrol-v1.h index 144d71b65907..f68c0064d674 100644 --- a/mm/memcontrol-v1.h +++ b/mm/memcontrol-v1.h @@ -59,9 +59,17 @@ unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap); void drain_all_stock(struct mem_cgroup *root_memcg); -unsigned long memcg_events(struct mem_cgroup *memcg, int event); -unsigned long memcg_events_local(struct mem_cgroup *memcg, int event); -unsigned long memcg_page_state_local(struct mem_cgroup *memcg, int idx); +unsigned long __memcg_events(struct mem_cgroup *memcg, int event, bool local); +static inline unsigned long memcg_events(struct mem_cgroup *memcg, int event) +{ + return __memcg_events(memcg, event, false); +} + +static inline unsigned long memcg_events_local(struct mem_cgroup *memcg, int event) +{ + return __memcg_events(memcg, event, true); +} + unsigned long memcg_page_state_output(struct mem_cgroup *memcg, int item); unsigned long memcg_page_state_local_output(struct mem_cgroup *memcg, int item); int memory_stat_show(struct seq_file *m, void *v); diff --git a/mm/memcontrol.c b/mm/memcontrol.c index b10e0a8f3375..404bbdfa352f 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) +unsigned long __lruvec_page_state(struct lruvec *lruvec, + enum node_stat_item idx, bool local) { struct mem_cgroup_per_node *pn; long x; @@ -389,30 +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]); -#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; - 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]); + 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; @@ -651,7 +630,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) +unsigned long __memcg_page_state(struct mem_cgroup *memcg, int idx, bool local) { long x; int i = memcg_stats_index(idx); @@ -659,7 +638,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; @@ -706,23 +687,6 @@ void __mod_memcg_state(struct mem_cgroup *memcg, enum memcg_stat_item idx, trace_mod_memcg_state(memcg, idx, val); } -/* 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; -} - static void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, int val) @@ -859,24 +823,15 @@ 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) -{ - 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]); -} - -unsigned long memcg_events_local(struct mem_cgroup *memcg, int event) +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_local[i]); + return local ? READ_ONCE(memcg->vmstats->events_local[i]) : + READ_ONCE(memcg->vmstats->events[i]); } struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)