Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Tejun Heo <tj@kernel.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 v2 10/11] cgroup: don't hold cgroup_mutex for cgroup.kill
Date: Tue, 1 Dec 2026 09:55:54 -0800	[thread overview]
Message-ID: <20261201095554.faf34ccb08077ff486e74cb9@linux-foundation.org> (raw)
In-Reply-To: <20261130173425.4117363-11-shakeel.butt@linux.dev>

On Mon, 30 Nov 2026 09:34:24 -0800 Shakeel Butt <shakeel.butt@linux.dev> wrote:

> It is often used when the system is already in trouble, so it should not
> wait for other cgroup operations, and they should not wait for it.
> 
> cgroup_mutex kept the cgroup and its subtree stable, and blocked
> migrations, CLONE_INTO_CGROUP forks and switches to threaded during the
> kill. Replace it:

From: Andrew Morton <akpm@linux-foundation.org>
Subject: cgroup: reject a threaded switch during a descendant kill
Date: Tue, 01 Dec 2026 01:35:32 -0800

A kill of a descendant can outlive its last task.  The ancestor then
looks empty, and cgroup_enable_threaded() checks only the ancestor's own
nr_subtree_kills.  It can switch the ancestor to threaded while the
descendant kill is still active.  The switch changes the domain of
threaded descendants, allowing threads from outside the killed subtree
to enter it during the kill.

This fixes Shakeel Butt's "cgroup: don't hold cgroup_mutex for cgroup.kill"
patch.

Check the active kill list for the cgroup and all its descendants under
css_set_lock.  cgroup_mutex keeps the ancestry stable.  Once the empty
ancestor passes the check, migrations cannot populate it until the
switch finishes, and a new kill of an empty subtree returns immediately.

A 64 CPU KASAN/lockdep test held a descendant kill open for one second
after its last task exited.  The v2 kernel accepted the ancestor's
cgroup.type=threaded write during that interval.  With this change the
write returns EBUSY.

Link: https://lore.kernel.org/r/20261130173425.4117363-11-shakeel.butt@linux.dev
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Tejun Heo <tj@kernel.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Koutný <mkoutny@suse.com>

 kernel/cgroup/cgroup.c |   20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

--- a/kernel/cgroup/cgroup.c~cgroup-dont-hold-cgroup_mutex-for-cgroupkill-fix
+++ a/kernel/cgroup/cgroup.c
@@ -3865,6 +3865,7 @@ static int cgroup_enable_threaded(struct
 	struct cgroup *dom_cgrp = parent->dom_cgrp;
 	struct cgroup *dsct;
 	struct cgroup_subsys_state *d_css;
+	struct cgroup_kill_ctx *kill;
 	int ret;
 
 	lockdep_assert_held(&cgroup_mutex);
@@ -3889,15 +3890,20 @@ static int cgroup_enable_threaded(struct
 		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.
+	 * A kill of @cgrp or a descendant may still be running after its last
+	 * task exits.  Switching now changes threaded descendants' domains and
+	 * can let outside threads enter a subtree whose kill is still active.
+	 * No new kill can start after this check: @cgrp is empty, migrations
+	 * wait for cgroup_mutex, and cgroup_kill() skips empty subtrees.
 	 */
 	spin_lock_irq(&css_set_lock);
-	ret = cgrp->nr_subtree_kills ? -EBUSY : 0;
+	ret = 0;
+	list_for_each_entry(kill, &cgroup_kills, node) {
+		if (cgroup_is_descendant(kill->cgrp, cgrp)) {
+			ret = -EBUSY;
+			break;
+		}
+	}
 	spin_unlock_irq(&css_set_lock);
 	if (ret)
 		return ret;
_



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

Thread overview: 20+ 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
2026-12-01 17:55   ` Andrew Morton [this message]
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

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=20261201095554.faf34ccb08077ff486e74cb9@linux-foundation.org \
    --to=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=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