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, 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 3/4] memcg: factor out the replace_stock_objcg function
Date: Tue, 14 Jan 2025 12:25:18 +0000 [thread overview]
Message-ID: <20250114122519.1404275-4-chenridong@huaweicloud.com> (raw)
In-Reply-To: <20250114122519.1404275-1-chenridong@huaweicloud.com>
From: Chen Ridong <chenridong@huawei.com>
Factor out the 'replace_stock_objcg' function to make the code more
cohesive.
Signed-off-by: Chen Ridong <chenridong@huawei.com>
---
mm/memcontrol.c | 26 ++++++++++++++++----------
1 file changed, 16 insertions(+), 10 deletions(-)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 98f84a9fa228..b10e0a8f3375 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2691,6 +2691,20 @@ void __memcg_kmem_uncharge_page(struct page *page, int order)
obj_cgroup_put(objcg);
}
+/* Replace the stock objcg with objcg, return the old objcg */
+static struct obj_cgroup *replace_stock_objcg(struct memcg_stock_pcp *stock,
+ struct obj_cgroup *objcg)
+{
+ struct obj_cgroup *old = NULL;
+
+ old = drain_obj_stock(stock);
+ obj_cgroup_get(objcg);
+ stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes)
+ ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0;
+ WRITE_ONCE(stock->cached_objcg, objcg);
+ return old;
+}
+
static void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat,
enum node_stat_item idx, int nr)
{
@@ -2708,11 +2722,7 @@ static void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat,
* changes.
*/
if (READ_ONCE(stock->cached_objcg) != objcg) {
- old = drain_obj_stock(stock);
- obj_cgroup_get(objcg);
- stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes)
- ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0;
- WRITE_ONCE(stock->cached_objcg, objcg);
+ old = replace_stock_objcg(stock, objcg);
stock->cached_pgdat = pgdat;
} else if (stock->cached_pgdat != pgdat) {
/* Flush the existing cached vmstat data */
@@ -2866,11 +2876,7 @@ static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes,
stock = this_cpu_ptr(&memcg_stock);
if (READ_ONCE(stock->cached_objcg) != objcg) { /* reset if necessary */
- old = drain_obj_stock(stock);
- obj_cgroup_get(objcg);
- WRITE_ONCE(stock->cached_objcg, objcg);
- stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes)
- ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0;
+ old = replace_stock_objcg(stock, objcg);
allow_uncharge = true; /* Allow uncharge when objcg changes */
}
stock->nr_bytes += nr_bytes;
--
2.34.1
next prev parent reply other threads:[~2025-01-14 12:36 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-14 12:25 [PATCH -v2 next 0/4] Some cleanup for memcg Chen Ridong
2025-01-14 12:25 ` [PATCH -v2 next 1/4] memcg: use OFP_PEAK_UNSET instead of -1 Chen Ridong
2025-01-14 18:36 ` Roman Gushchin
2025-01-14 12:25 ` [PATCH -v2 next 2/4] memcg: call the free function when allocation of pn fails Chen Ridong
2025-01-14 18:36 ` Roman Gushchin
2025-01-14 12:25 ` Chen Ridong [this message]
2025-01-14 18:39 ` [PATCH -v2 next 3/4] memcg: factor out the replace_stock_objcg function Roman Gushchin
2025-01-16 0:56 ` Shakeel Butt
2025-01-14 12:25 ` [PATCH -v2 next 4/4] memcg: factor out stat(event)/stat_local(event_local) reading functions Chen Ridong
2025-01-14 18:45 ` Roman Gushchin
2025-01-16 0:57 ` Shakeel Butt
2025-01-16 1:21 ` 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=20250114122519.1404275-4-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=mkoutny@suse.com \
--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