[PATCH v3 3/3] mm: memcg: skip the RCU lock when the memcg is not dying
From: Hui Zhu
Date: Fri Sep 04 2026 - 06:23:41 EST
From: Hui Zhu <zhuhui@xxxxxxxxxx>
get_non_dying_memcg_start() takes rcu_read_lock() on every stat update, but
the lock only protects the upward walk to a non-dying ancestor, which
happens solely while a memcg is being offlined. The dying check itself
reads the CSS_DYING flag of a memcg the caller already holds a reference
to, so it is safe without the lock.
Check memcg_is_dying() first and return immediately when the memcg is
alive, taking the RCU lock only on the rare dying path. On an anon
fault/charge churn workload in a memcg this recovers the ~0.6% overhead
added by the previous patch (4368077 vs 4343159 pages/s before, back to
~4377000 pages/s after).
Signed-off-by: Hui Zhu <zhuhui@xxxxxxxxxx>
Acked-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>
---
mm/memcontrol.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index b3d1ac3fe0aa..f454d02746e9 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -813,6 +813,17 @@ static long memcg_state_val_in_pages(int idx, long val)
static inline struct mem_cgroup *
get_non_dying_memcg_start(struct mem_cgroup *memcg, bool *rcu_locked)
{
+ /*
+ * Fast path: the caller holds a reference to @memcg, so reading
+ * its CSS_DYING flag without the RCU lock is safe. The RCU lock
+ * is only needed to walk up to a non-dying ancestor, which
+ * happens only while a memcg is actually being offlined.
+ */
+ if (!memcg_is_dying(memcg)) {
+ *rcu_locked = false;
+ return memcg;
+ }
+
rcu_read_lock();
*rcu_locked = true;
@@ -824,6 +835,9 @@ get_non_dying_memcg_start(struct mem_cgroup *memcg, bool *rcu_locked)
static inline void get_non_dying_memcg_end(bool rcu_locked)
{
+ if (!rcu_locked)
+ return;
+
rcu_read_unlock();
}
--
2.53.0