Re: [PATCH v2 10/11] cgroup: don't hold cgroup_mutex for cgroup.kill

From: Andrew Morton

Date: Tue Dec 01 2026 - 12:56:00 EST


On Mon, 30 Nov 2026 09:34:24 -0800 Shakeel Butt <shakeel.butt@xxxxxxxxx> wrote:

> It is often used when the system is already in trouble, so it should not
> wait for other cgroup operations, and they should not wait for it.
>
> cgroup_mutex kept the cgroup and its subtree stable, and blocked
> migrations, CLONE_INTO_CGROUP forks and switches to threaded during the
> kill. Replace it:

From: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Subject: cgroup: reject a threaded switch during a descendant kill
Date: Tue, 01 Dec 2026 01:35:32 -0800

A kill of a descendant can outlive its last task. The ancestor then
looks empty, and cgroup_enable_threaded() checks only the ancestor's own
nr_subtree_kills. It can switch the ancestor to threaded while the
descendant kill is still active. The switch changes the domain of
threaded descendants, allowing threads from outside the killed subtree
to enter it during the kill.

This fixes Shakeel Butt's "cgroup: don't hold cgroup_mutex for cgroup.kill"
patch.

Check the active kill list for the cgroup and all its descendants under
css_set_lock. cgroup_mutex keeps the ancestry stable. Once the empty
ancestor passes the check, migrations cannot populate it until the
switch finishes, and a new kill of an empty subtree returns immediately.

A 64 CPU KASAN/lockdep test held a descendant kill open for one second
after its last task exited. The v2 kernel accepted the ancestor's
cgroup.type=threaded write during that interval. With this change the
write returns EBUSY.

Link: https://lore.kernel.org/r/20261130173425.4117363-11-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/cgroup.c | 20 +++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)

--- a/kernel/cgroup/cgroup.c~cgroup-dont-hold-cgroup_mutex-for-cgroupkill-fix
+++ a/kernel/cgroup/cgroup.c
@@ -3865,6 +3865,7 @@ static int cgroup_enable_threaded(struct
struct cgroup *dom_cgrp = parent->dom_cgrp;
struct cgroup *dsct;
struct cgroup_subsys_state *d_css;
+ struct cgroup_kill_ctx *kill;
int ret;

lockdep_assert_held(&cgroup_mutex);
@@ -3889,15 +3890,20 @@ static int cgroup_enable_threaded(struct
return -EOPNOTSUPP;

/*
- * cgroup.kill doesn't hold cgroup_mutex. Threads that join @cgrp
- * after the switch could be killed with their whole process by a kill
- * of @cgrp that is still running, so don't switch during such a kill.
- * No new kill of @cgrp can start until we're done: @cgrp is empty,
- * nothing can enter it while we hold cgroup_mutex, and cgroup_kill()
- * returns right away for an empty subtree.
+ * A kill of @cgrp or a descendant may still be running after its last
+ * task exits. Switching now changes threaded descendants' domains and
+ * can let outside threads enter a subtree whose kill is still active.
+ * No new kill can start after this check: @cgrp is empty, migrations
+ * wait for cgroup_mutex, and cgroup_kill() skips empty subtrees.
*/
spin_lock_irq(&css_set_lock);
- ret = cgrp->nr_subtree_kills ? -EBUSY : 0;
+ ret = 0;
+ list_for_each_entry(kill, &cgroup_kills, node) {
+ if (cgroup_is_descendant(kill->cgrp, cgrp)) {
+ ret = -EBUSY;
+ break;
+ }
+ }
spin_unlock_irq(&css_set_lock);
if (ret)
return ret;
_