From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796012723; b=Vx5yzxRm2sqB7AyPA5b6D68n7NNJXkZTgjiCXIUmfnZVlOUqOOmv0mO3dT9k4jO1febm M48kuqNGfZmvKlS75iNFitaRsvuWHXLnCpb4PtW//PmBDWZBDw5n9NI0RK+0whZucQr3/ VFbmkBHPcxndXLgJOBrPJYjlsBCJ9ROQxJlqBN35BrwtF+kZFW+gQMGiA5FnvzVR1htzX zbbgHnGJHaiv1g4bC7Kz5ffyJIw9+si+V2Cr11M5ZqU0l8wbcuSvhYLe1qNnyz5zJwcg1 e8+2saCRtxmYlD0ztuIIrUsaKgPOnIEyG6RjTHgDFDf64hxZQ+pA/zHHj7bWVKLbfkg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796012723; h=Received:DKIM-Signature:Date:Message-ID:From:To:Cc:Subject: In-Reply-To:References:MIME-Version:Content-Type; bh=7EL1pOYRUPpJNj0YS69B99XoB+wrFmh/14OWsz0Kjig=; b=YSH9drYL/US/7pMB/3gzEKf1gHI0DIWW5qMhuXk2xwZmu+kcKXKAnV+nmnTGIgGCYupO TmjZJkiXgrwogOedKqVV0eUB6oB5iOBCwh3lHjXanYhrkPW29ibmBeyzGtnLq/u6T0ZA4 saOeXokVlVIGrxKuKlD6H/9KM+z6m0JEN/GT5EizOUuY24ytd2ILewpDChl9LdeDuxRsg YZWvZIBB/GlvbbQIwkiSeLrJb2sYgQr7dCUve1/rbC4A3IyxvtNqZtq0mIYl8rRSn/u8e lVVhmvIER10eNDRCAPHcl/8/gfif+1avlCjp8LuQRPzf8/qBKSdPEuB+TnFyv5TYTVg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=AOGIHnyS; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E5822606A31C; Mon, 30 Nov 2026 04:25:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=AOGIHnyS Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D2A61F008A0; Mon, 30 Nov 2026 04:25:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796012722; bh=7EL1pOYRUPpJNj0YS69B99XoB+wrFmh/14OWsz0Kjig=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=AOGIHnySL2Q/j13S0AojjIehttJPxYg5aZNMqJuJrUWYDXiAqIyBgK0slN1zoX4+V Lp18NKapAa0XAfljxluVPWqD+TsJ4uX/cbpffD9JoXkGBgwyBEBIivpn36CxtcUGpY G6TO/V3KV+g9+p0LKudLI88PSAuIIp0rpexiJ36FXfAVUV8PUuvofzs9cHJWuXtCiN tnldu4/Fj8GZ+sFwJ/R1BLAHijpWDnJ4RJCfgJIqPG6SpYsrEBgP4p5gEHRuwMcS/N h7QI4DNudwxDxyhQEUgKNWt4KjAWOW96T2j0uVRJBF5eIO5BcnuzgkNEhyMdNuVPEa my36YG3Atp7ug== Date: Sun, 29 Nov 2026 18:25:21 -1000 Message-ID: From: Tejun Heo To: Shakeel Butt Cc: Andrew Morton , Johannes Weiner , Michal Koutny , Michal Hocko , Roman Gushchin , Christian Brauner , Oleg Nesterov , Sebastian Andrzej Siewior , Suren Baghdasaryan , "T . J . Mercier" , Usama Arif , Meta kernel team , 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 In-Reply-To: <20261129183524.1052436-1-shakeel.butt@linux.dev> References: <20261129183524.1052436-1-shakeel.butt@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-cgkf-review-1-=boundary" --=-cgkf-review-1-=boundary Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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 --=-cgkf-review-1-=boundary Content-Type: text/x-patch; charset=us-ascii; name="debug-delays.diff" Content-Disposition: attachment; filename="debug-delays.diff" Content-Transfer-Encoding: 7bit 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 +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 +#include +static unsigned int cgfreeze_dbg_iter_ms; +core_param(cgfreeze_dbg_iter_ms, cgfreeze_dbg_iter_ms, uint, 0644); + #include /* @@ -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 --=-cgkf-review-1-=boundary Content-Type: text/x-shellscript; charset=us-ascii; name="cgroup-kill-freeze-repro.sh" Content-Disposition: attachment; filename="cgroup-kill-freeze-repro.sh" Content-Transfer-Encoding: 7bit #!/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 --=-cgkf-review-1-=boundary--