From: Joshua Hahn <joshua.hahnjy@gmail.com>
To: Johannes Weiner <hannes@cmpxchg.org>, Michal Hocko <mhocko@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>,
Shakeel Butt <shakeel.butt@linux.dev>,
Muchun Song <muchun.song@linux.dev>,
Andrew Morton <akpm@linux-foundation.org>,
cgroups@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, kernel-team@meta.com
Subject: [PATCH 6/7 v3] mm/memcontrol: optimize stock usage for cgroup v2
Date: Mon, 25 May 2026 12:04:53 -0700 [thread overview]
Message-ID: <20260525190455.2843786-7-joshua.hahnjy@gmail.com> (raw)
In-Reply-To: <20260525190455.2843786-1-joshua.hahnjy@gmail.com>
In cgroup v2, it is unlikely for memcg charges to happen directly on
non-leaf cgroups. There are a few exceptions, such as processes that
have yet to be migrated to children, and tasks that are reparented on
memcg destruction, that charge to the parent.
Because it is rare for parent cgroups to receive direct charges, stock
that remains in them are wasted memory.
Drain parent stocks when the first child is created to return those
pages for other memcgs to use.
This optimization is not for cgroup v1, where tasks can be attached to
any cgroup in the hierarchy, meaning stock can be consumed & refilled
for non-leaf cgroups as well.
Signed-off-by: Joshua Hahn <joshua.hahnjy@gmail.com>
---
mm/memcontrol.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index f20c9b829224a..64b82f1782720 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -4280,6 +4280,21 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css)
*/
xa_store(&mem_cgroup_private_ids, memcg->id.id, memcg, GFP_KERNEL);
+ /*
+ * It is unlikely for non-leaf memcgs to get direct charges on v2.
+ * Drain the parent's stock if we are the first child.
+ */
+ if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
+ struct mem_cgroup *parent = parent_mem_cgroup(memcg);
+ int cpu;
+
+ if (parent && !mem_cgroup_is_root(parent) &&
+ !css_has_online_children(&parent->css)) {
+ for_each_online_cpu(cpu)
+ work_on_cpu(cpu, drain_stock_on_cpu, parent);
+ }
+ }
+
return 0;
free_objcg:
for_each_node(nid) {
--
2.53.0-Meta
next prev parent reply other threads:[~2026-05-25 19:05 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-25 19:04 [PATCH 0/7 v3] mm/memcontrol, page_counter: move stock from mem_cgroup to page_counter Joshua Hahn
2026-05-25 19:04 ` [PATCH 1/7 v3] mm/page_counter: introduce per-page_counter stock Joshua Hahn
2026-05-25 19:45 ` Joshua Hahn
2026-05-25 19:04 ` [PATCH 2/7 v3] mm/page_counter: use page_counter_stock in page_counter_try_charge Joshua Hahn
2026-05-25 19:04 ` [PATCH 3/7 v3] mm/page_counter: introduce stock drain APIs Joshua Hahn
2026-05-25 19:04 ` [PATCH 4/7 v3] mm/memcontrol: convert memcg to use page_counter_stock Joshua Hahn
2026-05-25 19:04 ` [PATCH 5/7 v3] mm/memcontrol: optimize memsw stock for cgroup v1 Joshua Hahn
2026-05-25 19:04 ` Joshua Hahn [this message]
2026-05-25 19:04 ` [PATCH 7/7 v3] mm/memcontrol: remove unused memcg_stock code Joshua Hahn
2026-05-26 15:16 ` [PATCH 0/7 v3] mm/memcontrol, page_counter: move stock from mem_cgroup to page_counter Joshua Hahn
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=20260525190455.2843786-7-joshua.hahnjy@gmail.com \
--to=joshua.hahnjy@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=cgroups@vger.kernel.org \
--cc=hannes@cmpxchg.org \
--cc=kernel-team@meta.com \
--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 \
/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