Linux cgroups development
 help / color / mirror / Atom feed
From: Shakeel Butt <shakeel.butt@linux.dev>
To: 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>,
	cgroups@vger.kernel.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org, Sashiko <sashiko-bot@kernel.org>,
	stable@vger.kernel.org
Subject: [PATCH v3 03/12] cgroup: refuse cgroup.kill below a threaded cgroup
Date: Fri,  4 Dec 2026 10:40:32 -0800	[thread overview]
Message-ID: <20261204184041.1489976-4-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261204184041.1489976-1-shakeel.butt@linux.dev>

cgroup.kill refuses threaded cgroups, because killing is a process
directed operation: killing one thread takes down its whole process.
But it accepts a cgroup below a threaded cgroup, which can hold threads
of processes that live outside of it:

  mkdir -p A/B/C
  echo threaded > A/B/C/cgroup.type    B becomes a thread root
  echo threaded > A/cgroup.type        C joins the root's domain, and B
                                       becomes "domain invalid"
  echo <tid> > A/B/C/cgroup.threads    a thread of a process in the root
  echo 1 > A/B/cgroup.kill             succeeds and kills that process

When a cgroup turns threaded, the threaded cgroups below it join its new
domain, also those below a thread root such as B. Every cgroup below a
threaded cgroup is threaded or an invalid domain, so refuse cgroup.kill
for all of them, as it is refused for threaded cgroups.

Reproduced in a VM. A selftest follows later in this series.

Fixes: 661ee6280931 ("cgroup: introduce cgroup.kill")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/r/sashiko-outbox-241838@kernel.org
Cc: stable@vger.kernel.org # v5.14+
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
 Documentation/admin-guide/cgroup-v2.rst |  6 +++---
 kernel/cgroup/cgroup.c                  | 19 +++++++++++++++++--
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst
index 2f792ff29425..9539f3a99a2f 100644
--- a/Documentation/admin-guide/cgroup-v2.rst
+++ b/Documentation/admin-guide/cgroup-v2.rst
@@ -1059,9 +1059,9 @@ All cgroup core files are prefixed with "cgroup."
 	Killing a cgroup tree will deal with concurrent forks appropriately and
 	is protected against migrations.
 
-	In a threaded cgroup, writing this file fails with EOPNOTSUPP as
-	killing cgroups is a process directed operation, i.e. it affects
-	the whole thread-group.
+	In a threaded cgroup, or a cgroup below one, writing this file fails
+	with EOPNOTSUPP as killing cgroups is a process directed operation,
+	i.e. it affects the whole thread-group.
 
   cgroup.pressure
 	A read-write single value file that allowed values are "0" and "1".
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 0fcd2c1b0da9..f38da8b810f1 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -4322,6 +4322,20 @@ static ssize_t cgroup_freeze_write(struct kernfs_open_file *of,
 	return nbytes;
 }
 
+/*
+ * Is @cgrp threaded, or below a threaded cgroup?  When a cgroup turns threaded,
+ * the threaded cgroups below it join its new domain, also those below a thread
+ * root.  So a cgroup below a threaded cgroup can hold threads of processes that
+ * live outside of it.
+ */
+static bool cgroup_in_threaded_subtree(struct cgroup *cgrp)
+{
+	for (; cgrp; cgrp = cgroup_parent(cgrp))
+		if (cgroup_is_threaded(cgrp))
+			return true;
+	return false;
+}
+
 static void __cgroup_kill(struct cgroup *cgrp)
 {
 	struct css_task_iter it;
@@ -4392,9 +4406,10 @@ static ssize_t cgroup_kill_write(struct kernfs_open_file *of, char *buf,
 	/*
 	 * Killing is a process directed operation, i.e. the whole thread-group
 	 * is taken down so act like we do for cgroup.procs and only make this
-	 * writable in non-threaded cgroups.
+	 * writable in non-threaded cgroups.  The cgroups below a threaded cgroup
+	 * can hold threads of processes outside of them, so refuse those too.
 	 */
-	if (cgroup_is_threaded(cgrp))
+	if (cgroup_in_threaded_subtree(cgrp))
 		ret = -EOPNOTSUPP;
 	else
 		cgroup_kill(cgrp);
-- 
2.53.1-Meta


  parent reply	other threads:[~2026-12-04 18:41 UTC|newest]

Thread overview: 14+ 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-04 18:40 ` [PATCH v3 02/12] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
2026-12-04 18:40 ` Shakeel Butt [this message]
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-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-04 18:40 ` [PATCH v3 08/12] selftests/cgroup: test the frozen state of a cgroup with tasks and children Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 09/12] selftests/cgroup: test cgroup.kill in and below a threaded cgroup Shakeel Butt
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-04 18:40 ` [PATCH v3 12/12] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze Shakeel Butt
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=20261204184041.1489976-4-shakeel.butt@linux.dev \
    --to=shakeel.butt@linux.dev \
    --cc=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=sashiko-bot@kernel.org \
    --cc=stable@vger.kernel.org \
    --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