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 v3 11/12] cgroup: don't hold cgroup_mutex for cgroup.kill
Date: Sat, 05 Dec 2026 18:42:25 +0000	[thread overview]
Message-ID: <sashiko-outbox-251808@kernel.org> (raw)
In-Reply-To: <20261204184041.1489976-12-shakeel.butt@linux.dev>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] cgroup: unbounded O(T*W) cgroup_kills walk under css_set_lock
--

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

cgroup: don't hold cgroup_mutex for cgroup.kill

This commit removes cgroup_mutex from cgroup.kill, tracking active kills on a
global list under css_set_lock and killing tasks that migrate or fork out of
a subtree being killed.

> diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
> index 644bc653b1805..070d15f4031df 100644
> --- a/kernel/cgroup/cgroup.c
> +++ b/kernel/cgroup/cgroup.c

[ ... ]

> +static bool cgroup_kill_forced(struct task_struct *task, struct cgroup *cgrp,
> +			       bool *killing)
> +{
> +	struct pid_namespace *ns = task_active_pid_ns(task);
> +	struct cgroup_kill_ctx *kill;
> +	bool force = false;
> +
> +	lockdep_assert_held(&css_set_lock);
> +
> +	list_for_each_entry(kill, &cgroup_kills, node) {
> +		if (!cgroup_is_descendant(cgrp, kill->cgrp))
> +			continue;
> +		if (killing)
> +			*killing = true;
> +		if (!pid_nr_ns(kill->writer, ns))
> +			force = true;
> +	}
> +	return force;
> +}

[ ... ]

> +static void cgroup_kill_leaving_task(struct task_struct *task,
> +				     struct cgroup *cgrp)
> +{
> +	bool killing = false, force;
> +	unsigned long flags;
> +
> +	lockdep_assert_held(&css_set_lock);
> +
> +	force = cgroup_kill_forced(task, cgrp, &killing);
> +	if (!killing || (task->flags & PF_KTHREAD) ||
> +	    !lock_task_sighand(task, &flags))
> +		return;
> +	if (force || !(task->signal->flags & SIGNAL_UNKILLABLE))
> +		send_signal_locked(SIGKILL, SEND_SIG_PRIV, task, PIDTYPE_TGID);
> +	unlock_task_sighand(task, &flags);
> +}

[ ... ]

> @@ -2820,6 +2930,10 @@ static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx)
>  			struct css_set *from_cset = task_css_set(task);
>  			struct css_set *to_cset = cset->mg_dst_cset;
>  
> +			/* a task can't leave a subtree being killed */
> +			if (unlikely(!list_empty(&cgroup_kills)))
> +				cgroup_kill_leaving_task(task, cset->dfl_cgrp);
> +
>  			get_css_set(to_cset);
>  			to_cset->nr_tasks++;
>  			css_set_move_task(task, from_cset, to_cset, true);

[Severity: High]
Can concurrent cgroup.kill writers turn the css_set_lock critical section in
cgroup_migrate_execute() into an unbounded O(T * W) walk?

cgroup_kill() links a stack-allocated cgroup_kill_ctx onto cgroup_kills for
every concurrent writer without deduplication or a concurrency limit.  An
unprivileged user in a delegated subtree can keep many writers on the list
by repeatedly killing a cgroup holding a same-namespace pid-ns init.

In cgroup_migrate_execute(), cgroup_kill_leaving_task() scans cgroup_kills
for every migrating task under IRQ-disabled css_set_lock, even when
cset->dfl_cgrp has no ancestor with nr_subtree_kills > 0.  This stalls
fork(), exit(), and cgroup operations and can trigger RCU stalls.

cgroup_kill_forced() also computes task_active_pid_ns(task) unconditionally
and keeps scanning after force becomes true.  cgroup_kill_leaving_task()
checks PF_KTHREAD only after the scan and omits __fatal_signal_pending(),
causing redundant scans and lock_task_sighand() contention per thread.

Checking nr_subtree_kills once per cset, filtering PF_KTHREAD and
__fatal_signal_pending() before scanning, breaking once force is true, and
deduplicating or bounding cgroup_kills entries would avoid this.

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


  reply	other threads:[~2026-12-05 18:42 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 18:40 [PATCH v3 00/12] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 01/12] cgroup: don't lose a task in css_task_iter when a migration fails Shakeel Butt
2026-12-05  7:54   ` Tejun Heo
2026-12-04 18:40 ` [PATCH v3 02/12] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 03/12] cgroup: refuse cgroup.kill below a threaded cgroup Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 04/12] cgroup/freezer: check own tasks and descendants for the frozen state Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 05/12] cgroup/freezer: don't update the frozen state of removed cgroups Shakeel Butt
2026-12-05 18:42   ` sashiko-bot
2026-12-04 18:40 ` [PATCH v3 06/12] cgroup/freezer: test the freeze trap bit under siglock Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 07/12] selftests/cgroup: test cgroup.procs reads across a failed migration Shakeel Butt
2026-12-05 18:42   ` sashiko-bot
2026-12-04 18:40 ` [PATCH v3 08/12] selftests/cgroup: test the frozen state of a cgroup with tasks and children Shakeel Butt
2026-12-05 18:42   ` sashiko-bot
2026-12-04 18:40 ` [PATCH v3 09/12] selftests/cgroup: test cgroup.kill in and below a threaded cgroup Shakeel Butt
2026-12-05 18:42   ` sashiko-bot
2026-12-04 18:40 ` [PATCH v3 10/12] cgroup: factor out cgroup_kn_break_active() and cgroup_kn_unbreak_active() Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 11/12] cgroup: don't hold cgroup_mutex for cgroup.kill Shakeel Butt
2026-12-05 18:42   ` sashiko-bot [this message]
2026-12-04 18:40 ` [PATCH v3 12/12] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze Shakeel Butt
2026-12-05 18:42   ` sashiko-bot
2026-12-04 18:54 ` [PATCH v3 00/12] 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=sashiko-outbox-251808@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