Re: [PATCH v2 11/11] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze
From: Andrew Morton
Date: Tue Dec 01 2026 - 12:56:18 EST
On Mon, 30 Nov 2026 09:34:25 -0800 Shakeel Butt <shakeel.butt@xxxxxxxxx> wrote:
> cgroup.freeze holds cgroup_mutex while it walks the subtree and its tasks.
> cgroup_mutex kept freeze writers apart, gave new cgroups a stable parent
> state, and blocked migrations. Replace it:
From: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Subject: cgroup/freezer: skip empty branches in the task pass
Date: Tue, 01 Dec 2026 01:44:33 -0800
The lockless cgroup.freeze implementation walks the subtree once to set
states and again to visit tasks. The second pass pins and iterates each
cgroup even when the whole branch is unpopulated. In three 64 CPU guest
trials, 60 straight-line writes over 1,024 empty descendants had a
median latency of 273 us on v2 versus 193 us before the series.
This fixes Shakeel Butt's "cgroup/freezer: don't hold cgroup_mutex for
cgroup.freeze" patch.
An empty branch still needs its CGRP_FROZEN state and trace event updated.
Do both in the state pass while holding css_set_lock, then skip that branch
in the task pass. A fork or migration into it after state propagation
uses the new CGRP_FREEZE bit; an exiting task will not run in user space.
The state and trace updates remain in the same order as cgroup_do_freeze().
Link: https://lore.kernel.org/r/20261130173425.4117363-12-shakeel.butt@xxxxxxxxx
Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Tejun Heo <tj@xxxxxxxxxx>
Cc: Johannes Weiner <hannes@xxxxxxxxxxx>
Cc: Michal Koutný <mkoutny@xxxxxxxx>
kernel/cgroup/freezer.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
--- a/kernel/cgroup/freezer.c~cgroup-freezer-dont-hold-cgroup_mutex-for-cgroupfreeze-fix
+++ a/kernel/cgroup/freezer.c
@@ -349,6 +349,18 @@ int cgroup_freeze(struct cgroup *cgrp, b
}
dsct->freezer.e_freeze = e_freeze;
cgroup_set_freeze(dsct, e_freeze);
+ /*
+ * No task in this subtree needs a signal. Revisit its frozen
+ * state here so the task pass can skip the whole empty branch.
+ * Forks and migrations into it use the new CGRP_FREEZE state.
+ */
+ if (!cgroup_is_populated(dsct)) {
+ if (e_freeze)
+ TRACE_CGROUP_PATH(freeze, dsct);
+ else
+ TRACE_CGROUP_PATH(unfreeze, dsct);
+ cgroup_update_frozen(dsct);
+ }
spin_unlock_irq(&css_set_lock);
applied = true;
}
@@ -377,6 +389,10 @@ int cgroup_freeze(struct cgroup *cgrp, b
*/
rcu_read_lock();
css_for_each_descendant_pre(css, &cgrp->self) {
+ if (!cgroup_is_populated(css->cgroup)) {
+ css = css_rightmost_descendant(css);
+ continue;
+ }
if (css != &cgrp->self && css->cgroup->freezer.freeze) {
css = css_rightmost_descendant(css);
continue;
_