Linux-mm Archive on 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 8/9] cgroup: don't hold cgroup_mutex for cgroup.kill
Date: Mon, 30 Nov 2026 18:36:20 +0000	[thread overview]
Message-ID: <sashiko-outbox-241838@kernel.org> (raw)
In-Reply-To: <20261129183524.1052436-9-shakeel.butt@linux.dev>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] cgroup: tasks skipped by __cgroup_kill() on concurrent
  migration rollback
- [Medium] cgroup: missing ancestor and descendant kill checks in
  cgroup_enable_threaded()
--

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

cgroup: don't hold cgroup_mutex for cgroup.kill

This commit removes cgroup_mutex from cgroup.kill by walking the subtree
under RCU and using css_set_lock-protected counters and sequence numbers
to synchronize against concurrent migrations, forks, and switches to
threaded mode.

> diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
> index e16595905edf2..d3407064b07ba 100644
> --- a/kernel/cgroup/cgroup.c
> +++ b/kernel/cgroup/cgroup.c
[ ... ]
> @@ -2787,10 +2816,19 @@ static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx)
>  	 */
>  	spin_lock_irq(&css_set_lock);
>  	list_for_each_entry(cset, &tset->src_csets, mg_node) {
> +		bool kill = false;
> +
> +		/* don't let tasks out of a subtree being killed, see cgroup_kill() */
> +		if (cgroup_nr_kills)
> +			cgroup_subtree_kill_seq(cset->dfl_cgrp, &kill);
> +
>  		list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
>  			struct css_set *from_cset = task_css_set(task);
>  			struct css_set *to_cset = cset->mg_dst_cset;
>  
> +			if (unlikely(kill) && !(task->flags & PF_KTHREAD))
> +				send_sig(SIGKILL, task, 0);
> +
>  			get_css_set(to_cset);
>  			to_cset->nr_tasks++;
>  			css_set_move_task(task, from_cset, to_cset, true);

[Severity: High]
Can a task in a subtree being killed escape SIGKILL if its migration
fails before this commit point?

Without cgroup_mutex, __cgroup_kill()'s css_task_iter can interleave
with cgroup_migrate_add_task() and cgroup_migrate_execute():

CPU0                                  CPU1
__cgroup_kill()
  css_task_iter_start()
    /* it->task_pos = &task->cg_list
       on cset->tasks */
                                      cgroup_migrate_add_task(task)
                                        css_set_skip_task_iters(cset, task)
                                          /* it->task_pos = &cset->tasks,
                                             CSS_TASK_ITER_SKIPPED set */
                                        list_move_tail(..., &cset->mg_tasks)
                                      cgroup_migrate_execute()
                                        ss->can_attach() -> fails
                                        goto out_release_tset;
                                        list_splice_tail_init(&cset->mg_tasks,
                                                              &cset->tasks)
  css_task_iter_next()
    css_task_iter_advance()
      /* sees it->task_pos == &cset->tasks,
         moves to empty cset->mg_tasks */
      <-- task is never visited or killed

When cgroup_migrate_add_task() moves the last (or only) task from
cset->tasks to cset->mg_tasks, css_set_skip_task_iters() advances
it->task_pos to the list head &cset->tasks and sets
CSS_TASK_ITER_SKIPPED.  If ss->can_attach() or the
CGROUP_TASK_MIGRATING notifier fails, cgroup_migrate_execute() skips
the commit-point SIGKILL and out_release_tset splices cset->mg_tasks
back to the tail of cset->tasks (behind it->task_pos == &cset->tasks)
without updating cset->task_iters.  The next css_task_iter_next() in
__cgroup_kill() then transitions from &cset->tasks to the now-empty
cset->mg_tasks and finishes cset without signaling the task.

> @@ -3781,6 +3819,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.
> +	 * Set the flag now, so that new kills of @cgrp fail, see cgroup_kill().
> +	 */
> +	spin_lock_irq(&css_set_lock);
> +	if (cgrp->nr_subtree_kills) {
> +		spin_unlock_irq(&css_set_lock);
> +		return -EBUSY;
> +	}
> +	set_bit(CGRP_THREADED, &cgrp->flags);
> +	spin_unlock_irq(&css_set_lock);
> +
>  	/*
>  	 * The following shouldn't cause actual migrations and should
>  	 * always succeed.

[Severity: Medium]
Does checking only cgrp->nr_subtree_kills miss kills running on an
ancestor or a descendant of cgrp?

cgroup_kill() increments nr_subtree_kills only on the top cgroup of the
kill.  If an ancestor of cgrp is being killed, cgrp->nr_subtree_kills is
0, so cgroup_enable_threaded(cgrp) succeeds instead of returning -EBUSY
while the ancestor's kill walks cgrp.

Conversely, in a hierarchy root -> A -> B -> C where B is a thread root
(B->dom_cgrp == B, CGRP_THREADED clear) and C is threaded (C->dom_cgrp
== B), a kill on B sets B->nr_subtree_kills = 1 while
A->nr_subtree_kills is 0.  If A, B, and C are empty,
cgroup_enable_threaded(A) passes this check and updates dsct->dom_cgrp
for all threaded descendants of A, moving C->dom_cgrp from B to root.  A
thread of a process in root can then join C via cgroup.threads and have
its whole outside process killed by the ongoing kill of B.

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


  reply	other threads:[~2026-11-30 18:36 UTC|newest]

Thread overview: 14+ 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 [this message]
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 ` [PATCH 0/9] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Tejun Heo
2026-11-30 17:04   ` 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-241838@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox