All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tejun Heo <tj@kernel.org>
To: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Michal Koutny <mkoutny@suse.com>,
	Michal Hocko <mhocko@kernel.org>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Christian Brauner <brauner@kernel.org>,
	Oleg Nesterov <oleg@redhat.com>,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	Suren Baghdasaryan <surenb@google.com>,
	"T . J . Mercier" <tjmercier@google.com>,
	Usama Arif <usama.arif@linux.dev>,
	Meta kernel team <kernel-team@meta.com>,
	cgroups@vger.kernel.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 0/9] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze
Date: Sun, 29 Nov 2026 18:25:21 -1000	[thread overview]
Message-ID: <a6553d7921f3f47b7b890bfd232de644@kernel.org> (raw)
In-Reply-To: <20261129183524.1052436-1-shakeel.butt@linux.dev>

[-- Attachment #1: Type: text/plain, Size: 2651 bytes --]

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

[-- Attachment #2: debug-delays.diff --]
[-- Type: text/x-patch, Size: 2110 bytes --]

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

[-- Attachment #3: cgroup-kill-freeze-repro.sh --]
[-- Type: text/x-shellscript, Size: 4165 bytes --]

#!/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

  parent reply	other threads:[~2026-11-30  4:25 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 18:35 [PATCH 0/9] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Shakeel Butt
2026-11-29 18:35 ` [PATCH 1/9] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
2026-11-29 18:35 ` [PATCH 2/9] cgroup/freezer: check own tasks and descendants for the frozen state Shakeel Butt
2026-11-29 18:35 ` [PATCH 3/9] cgroup/freezer: don't update the frozen state of removed cgroups Shakeel Butt
2026-11-29 18:35 ` [PATCH 4/9] cgroup/freezer: test the freeze trap bit under siglock Shakeel Butt
2026-11-29 18:35 ` [PATCH 5/9] selftests/cgroup: test the frozen state of a cgroup with tasks and children Shakeel Butt
2026-11-30 18:36   ` sashiko-bot
2026-11-29 18:35 ` [PATCH 6/9] selftests/cgroup: test cgroup.kill in a threaded cgroup Shakeel Butt
2026-11-29 18:35 ` [PATCH 7/9] cgroup: factor out cgroup_kn_break_active() and cgroup_kn_unbreak_active() Shakeel Butt
2026-11-29 18:35 ` [PATCH 8/9] cgroup: don't hold cgroup_mutex for cgroup.kill Shakeel Butt
2026-11-30 18:36   ` sashiko-bot
2026-11-30 21:21     ` Shakeel Butt
2026-11-29 18:35 ` [PATCH 9/9] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze Shakeel Butt
2026-11-30  4:25 ` Tejun Heo [this message]
2026-11-30 17:04   ` [PATCH 0/9] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Shakeel Butt

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=a6553d7921f3f47b7b890bfd232de644@kernel.org \
    --to=tj@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=bigeasy@linutronix.de \
    --cc=brauner@kernel.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=mkoutny@suse.com \
    --cc=oleg@redhat.com \
    --cc=roman.gushchin@linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=surenb@google.com \
    --cc=tjmercier@google.com \
    --cc=usama.arif@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.