From: Chen Ridong <chenridong@huaweicloud.com>
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 [thread overview]
Message-ID: <20241206013512.2883617-6-chenridong@huaweicloud.com> (raw)
In-Reply-To: <20241206013512.2883617-1-chenridong@huaweicloud.com>
From: Chen Ridong <chenridong@huawei.com>
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 <chenridong@huawei.com>
---
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)
--
2.34.1
next prev parent reply other threads:[~2024-12-06 1:45 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-06 1:35 [next -v1 0/5] Some cleanup for memcg Chen Ridong
2024-12-06 1:35 ` [next -v1 1/5] memcg: use OFP_PEAK_UNSET instead of -1 Chen Ridong
2024-12-17 12:27 ` Michal Koutný
2024-12-17 16:55 ` David Finkel
2024-12-17 17:47 ` Shakeel Butt
2024-12-06 1:35 ` [next -v1 2/5] memcg: call the free function when allocation of pn fails Chen Ridong
2024-12-17 12:27 ` Michal Koutný
2024-12-17 17:47 ` Shakeel Butt
2024-12-06 1:35 ` [next -v1 3/5] memcg: simplify the mem_cgroup_update_lru_size function Chen Ridong
2024-12-06 5:33 ` Yu Zhao
2024-12-06 6:40 ` Chen Ridong
2024-12-06 8:24 ` Hugh Dickins
2024-12-06 10:02 ` Chen Ridong
2024-12-06 21:20 ` Shakeel Butt
2024-12-10 11:35 ` Chen Ridong
2024-12-06 1:35 ` [next -v1 4/5] memcg: factor out the __refill_obj_stock function Chen Ridong
2024-12-17 18:19 ` Shakeel Butt
2024-12-19 1:54 ` Chen Ridong
2024-12-06 1:35 ` Chen Ridong [this message]
2024-12-20 9:51 ` [next -v1 5/5] memcg: factor out stat(event)/stat_local(event_local) reading functions Chen Ridong
2024-12-14 1:47 ` [next -v1 0/5] Some cleanup for memcg Chen Ridong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241206013512.2883617-6-chenridong@huaweicloud.com \
--to=chenridong@huaweicloud.com \
--cc=akpm@linux-foundation.org \
--cc=cgroups@vger.kernel.org \
--cc=chenridong@huawei.com \
--cc=davidf@vimeo.com \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=vbabka@suse.cz \
--cc=wangweiyang2@huawei.com \
--cc=yosryahmed@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox