Re: [PATCH 0/9] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze
From: Tejun Heo
Date: Sun Nov 29 2026 - 23:25:26 EST
Hello, Shakeel.
The following is a Claude-generated review.
Two problems were found, in 8/9 and 9/9. Both were reproduced in a VM
with delays added to widen the windows. The debug patch and the
reproducer are attached.
1. A migration that fails before it commits makes the task walk skip a
task (8/9 and 9/9).
cgroup_migrate_add_task() calls css_set_skip_task_iters() and moves
the task to cset->mg_tasks. When ->can_attach() then fails,
out_release_tset in cgroup_migrate_execute() splices mg_tasks back
onto cset->tasks. An iterator whose next task was that one has
already moved past the end of cset->tasks and never returns it. The
task didn't change css_set, so neither the kill in
cgroup_migrate_execute() nor cgroup_freezer_migrate_task() runs for
it. cgroup_mutex used to keep migrations out of the walks.
The task has to be the last one on its css_set that the iterator
hasn't returned, so a cgroup with one task is enough. With the failed
attach between css_task_iter_start() and the first
css_task_iter_next():
- cgroup.kill returns success and the task survives.
- A freeze leaves cgroup.freeze at 1 with the task running, and the
cgroup never reports frozen.
- A thaw leaves cgroup.freeze at 0 and "frozen 0" in cgroup.events
with the task still frozen.
2. The SIGKILL sent from cgroup_migrate_execute() isn't forced (8/9).
send_sig(SIGKILL, task, 0) sends SEND_SIG_NOINFO, which
send_signal_locked() forces only when current is outside the target's
pid namespace. current is the migrator there, not the cgroup.kill
writer. A pid namespace init which writes its own pid to cgroup.procs
of a cgroup that the walk has already passed ignores the signal and
survives a kill written from the host. The same move made from the
host kills it.
The other direction follows from the code and wasn't run: a host
migration during a kill written inside the container kills an init
which that kill would have left alone.
A few smaller points, from reading the code only:
- 3/9 is only right on top of 2/9, and its stable tag doesn't name 2/9
as a prerequisite.
- 8/9: a CLONE_INTO_CGROUP child placed outside the tree by a parent
inside it is now killed. The cover letter says so but the
documentation update doesn't. cgroup.kill can also fail with
EOPNOTSUPP on a domain cgroup while a "threaded" write that later
fails is in progress.
- 9/9: a task frozen by a freeze that is still in progress runs again
when it is moved to a cgroup that the walk hasn't reached, until the
walk gets there. Maybe worth documenting?
Thanks.
--
tejun
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 28fd9b642f5a..d1be3b3c74bc 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -2609,6 +2609,13 @@ void cgroup_attach_unlock(enum cgroup_attach_lock_mode lock_mode,
/* cgroup.kill writes in progress, protected by css_set_lock */
static unsigned int cgroup_nr_kills;
+/* REVIEW DEBUG: widen race windows */
+#include <linux/delay.h>
+static unsigned int cgkill_dbg_iter_ms;
+core_param(cgkill_dbg_iter_ms, cgkill_dbg_iter_ms, uint, 0644);
+static unsigned int cgkill_dbg_cg_ms;
+core_param(cgkill_dbg_cg_ms, cgkill_dbg_cg_ms, uint, 0644);
+
/**
* cgroup_subtree_kill_seq - sample the cgroup.kill state of a cgroup's subtrees
* @cgrp: cgroup of interest
@@ -4396,6 +4403,8 @@ static void __cgroup_kill(struct cgroup *cgrp)
spin_unlock_irq(&css_set_lock);
css_task_iter_start(&cgrp->self, CSS_TASK_ITER_THREADED, &it);
+ if (cgkill_dbg_iter_ms)
+ msleep(cgkill_dbg_iter_ms);
while ((task = css_task_iter_next(&it))) {
/* Ignore kernel threads here. */
if (task->flags & PF_KTHREAD)
@@ -4470,6 +4479,8 @@ static int cgroup_kill(struct cgroup *cgrp)
rcu_read_unlock();
__cgroup_kill(css->cgroup);
+ if (cgkill_dbg_cg_ms)
+ msleep(cgkill_dbg_cg_ms);
cond_resched();
rcu_read_lock();
diff --git a/kernel/cgroup/freezer.c b/kernel/cgroup/freezer.c
index a5e3920f4ad2..4367cb3e3fc1 100644
--- a/kernel/cgroup/freezer.c
+++ b/kernel/cgroup/freezer.c
@@ -6,6 +6,12 @@
#include "cgroup-internal.h"
+/* REVIEW DEBUG: widen race windows */
+#include <linux/delay.h>
+#include <linux/moduleparam.h>
+static unsigned int cgfreeze_dbg_iter_ms;
+core_param(cgfreeze_dbg_iter_ms, cgfreeze_dbg_iter_ms, uint, 0644);
+
#include <trace/events/cgroup.h>
/*
@@ -205,6 +211,8 @@ static void cgroup_do_freeze(struct cgroup *cgrp, bool freeze)
TRACE_CGROUP_PATH(unfreeze, cgrp);
css_task_iter_start(&cgrp->self, 0, &it);
+ if (cgfreeze_dbg_iter_ms)
+ msleep(cgfreeze_dbg_iter_ms);
while ((task = css_task_iter_next(&it))) {
/*
* Ignore kernel threads here. Freezing cgroups containing
#!/bin/bash
#
# Reproducers for two problems in "cgroup: don't hold cgroup_mutex for
# cgroup.kill and cgroup.freeze":
#
# 1. A migration that fails before it commits makes the task walk of
# cgroup.kill and cgroup.freeze skip a task.
# 2. A pid namespace init that moves itself during a cgroup.kill written
# from an ancestor pid namespace survives the kill.
#
# Needs a kernel with debug-delays.diff applied, cpuset enabled, and root.
# Run it in a scratch VM. Each case has a control run that must behave.
P=/sys/module/kernel/parameters
CG=/run/cgtest
mkdir -p $CG
mount -t cgroup2 none $CG || exit 1
alive() { [ -d /proc/$1 ] && ! grep -q '^State:.*Z' /proc/$1/status; }
utime() { awk '{print $14}' /proc/$1/stat; }
runs() {
local a=$(utime $1)
sleep 1
local b=$(utime $1)
[ "$b" -gt "$a" ] && echo "running" || echo "not running"
}
# D/e is a cgroup that every attach fails on with ENOSPC: its cpuset has no
# effective CPUs because the sibling partition D/p took all of D's.
echo +cpuset > $CG/cgroup.subtree_control
mkdir $CG/D
echo 0-1 > $CG/D/cpuset.cpus
echo root > $CG/D/cpuset.cpus.partition
echo +cpuset > $CG/D/cgroup.subtree_control
mkdir $CG/D/p $CG/D/e
echo 0-1 > $CG/D/p/cpuset.cpus
echo root > $CG/D/p/cpuset.cpus.partition
sleep 1000 &
probe=$!
if echo $probe > $CG/D/e/cgroup.procs 2>/dev/null; then
echo "SETUP: attaching to D/e did not fail, case 1 is void"
fi
kill -9 $probe
echo "=== 1a. cgroup.kill, failed migration during the task walk"
for with_mig in 0 1; do
mkdir -p $CG/S/x
sleep 1000 &
t=$!
echo $t > $CG/S/x/cgroup.procs
echo 2000 > $P/cgkill_dbg_iter_ms
echo 1 > $CG/S/cgroup.kill &
w=$!
sleep 3 # the walk of S is done, the iterator of S/x has started
[ $with_mig = 1 ] && echo $t > $CG/D/e/cgroup.procs 2>/dev/null
wait $w
echo 0 > $P/cgkill_dbg_iter_ms
sleep 0.5
if alive $t; then
echo "failed migration=$with_mig: task SURVIVED, S $(grep populated $CG/S/cgroup.events)"
else
echo "failed migration=$with_mig: task killed"
fi
kill -9 $t 2>/dev/null
sleep 0.3
rmdir $CG/S/x $CG/S
done
echo "=== 1b. cgroup.freeze, failed migration during the task walk"
for with_mig in 0 1; do
mkdir -p $CG/G/x
bash -c 'while :; do :; done' &
t=$!
echo $t > $CG/G/x/cgroup.procs
echo 2000 > $P/cgfreeze_dbg_iter_ms
echo 1 > $CG/G/cgroup.freeze &
w=$!
sleep 3
[ $with_mig = 1 ] && echo $t > $CG/D/e/cgroup.procs 2>/dev/null
wait $w
echo 0 > $P/cgfreeze_dbg_iter_ms
sleep 5
echo "failed migration=$with_mig: freeze=$(cat $CG/G/cgroup.freeze), G $(grep frozen $CG/G/cgroup.events), task $(runs $t)"
# freeze it fully, then the same for the thaw
echo 0 > $CG/G/cgroup.freeze
sleep 0.5
echo 1 > $CG/G/cgroup.freeze
sleep 1
echo 2000 > $P/cgfreeze_dbg_iter_ms
echo 0 > $CG/G/cgroup.freeze &
w=$!
sleep 3
[ $with_mig = 1 ] && echo $t > $CG/D/e/cgroup.procs 2>/dev/null
wait $w
echo 0 > $P/cgfreeze_dbg_iter_ms
sleep 5
echo "failed migration=$with_mig: freeze=$(cat $CG/G/cgroup.freeze), G/x $(grep frozen $CG/G/x/cgroup.events), task $(runs $t)"
kill -9 $t 2>/dev/null
sleep 0.3
rmdir $CG/G/x $CG/G
done
echo "=== 2. cgroup.kill, pid namespace init moved during the kill"
for mover in host init; do
mkdir -p $CG/C/a $CG/C/b
sleep 1000 &
echo $! > $CG/C/a/cgroup.procs
if [ $mover = init ]; then
( echo $BASHPID > $CG/C/b/cgroup.procs
exec unshare -fp bash -c "sleep 4.5; echo 1 > $CG/C/a/cgroup.procs; sleep 1000; true" ) &
else
( echo $BASHPID > $CG/C/b/cgroup.procs
exec unshare -fp bash -c "sleep 1000; true" ) &
fi
sleep 0.5
init=
for p in $(cat $CG/C/b/cgroup.procs); do
grep -q -P '^NSpid:\t\d+\t1$' /proc/$p/status && init=$p
done
# sleep after each cgroup of the walk: C, then C/a, then C/b
echo 3000 > $P/cgkill_dbg_cg_ms
echo 1 > $CG/C/cgroup.kill &
w=$!
if [ $mover = host ]; then
sleep 4
echo $init > $CG/C/a/cgroup.procs
fi
wait $w
echo 0 > $P/cgkill_dbg_cg_ms
sleep 0.5
if alive $init; then
echo "moved by $mover: init SURVIVED in $(cat /proc/$init/cgroup), C $(grep populated $CG/C/cgroup.events)"
else
echo "moved by $mover: init killed"
fi
echo 1 > $CG/C/cgroup.kill
sleep 0.5
rmdir $CG/C/a $CG/C/b $CG/C
done