All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Shakeel Butt" <shakeel.butt@linux.dev>, "Tejun Heo" <tj@kernel.org>
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>,
	linux-mm@kvack.org
Subject: Re: [PATCH v2 10/11] cgroup: don't hold cgroup_mutex for cgroup.kill
Date: Tue, 01 Dec 2026 17:36:10 +0000	[thread overview]
Message-ID: <sashiko-outbox-243867@kernel.org> (raw)
In-Reply-To: <20261130173425.4117363-11-shakeel.butt@linux.dev>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] cgroup: unforced SIGKILL for CLONE_THREAD init in
  cgroup_post_fork()
- [Medium] cgroup: missing ancestor and descendant kill checks in
  cgroup_enable_threaded()
--

commit 2ee64c463b982db75c66b04ae8624ae020db6992
Author: Shakeel Butt <shakeel.butt@linux.dev>

cgroup: don't hold cgroup_mutex for cgroup.kill

This commit removes the cgroup_mutex dependency from cgroup.kill by
pinning the target cgroup via cgroup_kn_break_active(), walking the
subtree under RCU, and tracking active subtree kills to catch concurrent
migrations, forks, and switches to threaded mode.

> diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
> index bdc044ad21d34..cb8d9165ead24 100644
> --- a/kernel/cgroup/cgroup.c
> +++ b/kernel/cgroup/cgroup.c
[ ... ]
> @@ -3810,6 +3889,20 @@ static int cgroup_enable_threaded(struct cgroup *cgrp)
>  	    !cgroup_can_be_thread_root(dom_cgrp))
>  		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.
> +	 */
> +	spin_lock_irq(&css_set_lock);
> +	ret = cgrp->nr_subtree_kills ? -EBUSY : 0;
> +	spin_unlock_irq(&css_set_lock);
> +	if (ret)
> +		return ret;
> +
>  	/*
>  	 * The following shouldn't cause actual migrations and should
>  	 * always succeed.
> @@ -3825,6 +3918,13 @@ static int cgroup_enable_threaded(struct cgroup *cgrp)
>  		parent->nr_threaded_children++;
>  
>  	cgroup_finalize_control(cgrp, ret);
> +
> +	/* new kills of @cgrp fail from now on, see cgroup_kill() */
> +	if (!ret) {
> +		spin_lock_irq(&css_set_lock);
> +		set_bit(CGRP_THREADED, &cgrp->flags);
> +		spin_unlock_irq(&css_set_lock);
> +	}
>  	return ret;
>  }

[Severity: Medium]
Does checking only cgrp->nr_subtree_kills miss active kills rooted at an
ancestor or a descendant thread root?

cgroup_kill() increments nr_subtree_kills only on the top cgroup of the
walk and drops css_set_lock while iterating over the subtree.  Once the
signaled tasks exit, cgroups in that subtree become unpopulated while the
walk is still in progress:

1. If an ancestor A is being killed (A->nr_subtree_kills > 0) and a
   descendant B becomes empty before cgroup_kill(A) finishes,
   cgroup_enable_threaded(B) sees B->nr_subtree_kills == 0 and switches B
   to threaded while B is still inside a subtree being killed.

2. In a hierarchy root -> A (domain) -> B (thread root, B->dom_cgrp == B)
   -> C (threaded, C->dom_cgrp == B), cgroup_kill(B) can start while B or
   C is populated (setting B->nr_subtree_kills = 1).  If the tasks in B
   and C exit before cgroup_kill(B) finishes, cgroup_enable_threaded(A)
   sees A->nr_subtree_kills == 0 and updates dsct->dom_cgrp = dom_cgrp
   (root) for both A and C, because cgroup_is_threaded(C) is already true.
   An outside thread in root can then join C via cgroup.threads while
   cgroup_kill(B) is still running over B's subtree and have its whole
   process killed.

CPU0                                    CPU1
cgroup_kill(B)
  B->nr_subtree_kills++
  ... tasks in B and C exit ...
                                        cgroup_enable_threaded(A)
                                          A->nr_subtree_kills == 0
                                          C->dom_cgrp = root
                                        /* outside thread in root joins C */
  __cgroup_kill(C)
    send_sig(SIGKILL, task, 0)
    /* kills outside process */

Should cgroup_enable_threaded() check for active kills on ancestors and on
live descendants of cgrp under css_set_lock?

[ ... ]
> @@ -7206,13 +7374,22 @@ void cgroup_post_fork(struct task_struct *child,
>  
>  	/* init tasks are special, only link regular threads */
>  	if (likely(child->pid)) {
> -		if (kargs->cgrp) {
> -			cgrp_flags = kargs->cgrp->flags;
> -			cgrp_kill_seq = kargs->cgrp->kill_seq;
> -		} else {
> -			cgrp_flags = cset->dfl_cgrp->flags;
> -			cgrp_kill_seq = cset->dfl_cgrp->kill_seq;
> -		}
> +		struct cgroup *cgrp = kargs->cgrp ?: cset->dfl_cgrp;
> +
> +		cgrp_flags = cgrp->flags;
> +		cgrp_kill_seq = cgrp->kill_seq;
> +		killing = cgrp->nr_killing;
> +
> +		/*
> +		 * A CLONE_INTO_CGROUP child leaves the parent's cgroup.  Don't
> +		 * let it out of a subtree that was being killed at any time
> +		 * during the fork.  @current's cgroup is pinned by the
> +		 * threadgroup lock.
> +		 */
> +		if (kargs->cgrp &&
> +		    cgroup_subtree_kill_seq(task_dfl_cgroup(current),
> +					    &killing) != kargs->subtree_kill_seq)
> +			killing = true;
>  
>  		WARN_ON_ONCE(!list_empty(&child->cg_list));
>  		cset->nr_tasks++;
> @@ -7247,8 +7424,7 @@ void cgroup_post_fork(struct task_struct *child,
>  		 * child down right after we finished preparing it for
>  		 * userspace.
>  		 */
> -		kill = kargs->kill_seq != cgrp_kill_seq ||
> -		       (kargs->kill_seq & 1);
> +		kill = killing || kargs->kill_seq != cgrp_kill_seq;
>  	}
>  
>  	spin_unlock_irq(&css_set_lock);

[Severity: Medium]
Can a PID namespace init process survive a host-initiated cgroup.kill when
one of its threads spawns a new thread concurrently with the walk?

When kill is true, cgroup_post_fork() signals the child with
SEND_SIG_NOINFO:

kernel/cgroup/cgroup.c:cgroup_post_fork() {
	...
	/* Cgroup has to be killed so take down child immediately. */
	if (unlikely(kill))
		do_send_sig_info(SIGKILL, SEND_SIG_NOINFO, child, PIDTYPE_TGID);
}

In send_signal_locked(), SEND_SIG_NOINFO sets force = !task_pid_nr_ns(current,
task_active_pid_ns(t)).  In cgroup_post_fork(), current is the forking
parent rather than the cgroup.kill writer.  When a thread of a PID
namespace init (which has SIGNAL_UNKILLABLE set on signal->flags) calls
clone(CLONE_THREAD) or clone3(CLONE_THREAD | CLONE_INTO_CGROUP) within a
threaded domain, child shares current->signal and lives in the same PID
namespace as current, so force is false and sig_task_ignored() drops the
SIGKILL even when the cgroup.kill writer is in an ancestor PID namespace.

If the parent thread in a threaded child cgroup T spawns a sibling thread
into the already-walked domain cgroup D with CLONE_THREAD |
CLONE_INTO_CGROUP and then exits via sys_exit() before cgroup_kill(D)
reaches T, the new thread in D ignores the unforced SIGKILL from
cgroup_post_fork() and the init process survives.  Should
cgroup_post_fork() check the writers on cgroup_kills and force SIGKILL the
way cgroup_kill_leaving_task() does?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130173425.4117363-1-shakeel.butt@linux.dev?part=10


  reply	other threads:[~2026-12-01 17:36 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 17:34 [PATCH v2 00/11] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 01/11] cgroup: don't lose a task in css_task_iter when a migration fails Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 02/11] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 03/11] cgroup/freezer: check own tasks and descendants for the frozen state Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 04/11] cgroup/freezer: don't update the frozen state of removed cgroups Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 05/11] cgroup/freezer: test the freeze trap bit under siglock Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 06/11] selftests/cgroup: test cgroup.procs reads across a failed migration Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 07/11] selftests/cgroup: test the frozen state of a cgroup with tasks and children Shakeel Butt
2026-12-01 17:36   ` sashiko-bot
2026-12-01 17:55   ` Andrew Morton
2026-11-30 17:34 ` [PATCH v2 08/11] selftests/cgroup: test cgroup.kill in a threaded cgroup Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 09/11] cgroup: factor out cgroup_kn_break_active() and cgroup_kn_unbreak_active() Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 10/11] cgroup: don't hold cgroup_mutex for cgroup.kill Shakeel Butt
2026-12-01 17:36   ` sashiko-bot [this message]
2026-12-01 17:55   ` Andrew Morton
2026-12-01 17:56   ` Andrew Morton
2026-11-30 17:34 ` [PATCH v2 11/11] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze Shakeel Butt
2026-12-01 17:36   ` sashiko-bot
2026-12-01 17:56   ` Andrew Morton
2026-12-01 17:55 ` [PATCH v2 00/11] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Andrew Morton
2026-12-01 18:15   ` 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=sashiko-outbox-243867@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=bigeasy@linutronix.de \
    --cc=brauner@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=kernel-team@meta.com \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=mkoutny@suse.com \
    --cc=oleg@redhat.com \
    --cc=roman.gushchin@linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=surenb@google.com \
    --cc=tj@kernel.org \
    --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.