The Linux Kernel Mailing List
 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
Subject: [PATCH v3 09/12] selftests/cgroup: test cgroup.kill in and below a threaded cgroup
Date: Fri,  4 Dec 2026 10:40:38 -0800	[thread overview]
Message-ID: <20261204184041.1489976-10-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261204184041.1489976-1-shakeel.butt@linux.dev>

Check that writing cgroup.kill in a threaded cgroup fails with EOPNOTSUPP
and kills nothing, and that the thread root can still kill a process whose
thread was moved into the threaded cgroup.

This is existing behavior. The next patches change how cgroup.kill checks
for threaded cgroups, and this test shows that the behavior stays the
same.

Also check that cgroup.kill fails with EOPNOTSUPP below a threaded
cgroup: a thread root that became an invalid domain when its parent
turned threaded, with a thread from outside in its threaded child. This
test fails without "cgroup: refuse cgroup.kill below a threaded cgroup".

Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
 tools/testing/selftests/cgroup/test_kill.c | 127 +++++++++++++++++++++
 1 file changed, 127 insertions(+)

diff --git a/tools/testing/selftests/cgroup/test_kill.c b/tools/testing/selftests/cgroup/test_kill.c
index 756bc7193d4b..e8db5b3356a4 100644
--- a/tools/testing/selftests/cgroup/test_kill.c
+++ b/tools/testing/selftests/cgroup/test_kill.c
@@ -471,6 +471,131 @@ static int test_cgkill_exited_leader_migrated(const char *root)
 	return ret;
 }
 
+/*
+ * cgroup.kill is refused in a threaded cgroup, as killing is directed at
+ * whole processes.  The thread root can still kill a process whose thread
+ * was moved into the threaded cgroup.
+ */
+static int test_cgkill_threaded(const char *root)
+{
+	char *parent = NULL, *threaded = NULL;
+	int ret = KSFT_FAIL;
+	pid_t pid = -ESRCH;
+
+	parent = cg_name(root, "cg_test_kill_thread_root");
+	if (!parent)
+		goto cleanup;
+
+	threaded = cg_name(parent, "threaded");
+	if (!threaded)
+		goto cleanup;
+
+	if (cg_create(parent) || cg_create(threaded))
+		goto cleanup;
+
+	if (cg_write(threaded, "cgroup.type", "threaded"))
+		goto cleanup;
+
+	pid = cg_run_nowait(parent, child_fn, NULL);
+	if (pid < 0 || cg_wait_for_proc_count(parent, 1))
+		goto cleanup;
+
+	if (cg_write_numeric(threaded, "cgroup.threads", pid))
+		goto cleanup;
+
+	if (cg_write(threaded, "cgroup.kill", "1") != -EOPNOTSUPP)
+		goto cleanup;
+
+	/*
+	 * The refused write must not kill the child.  A SIGKILL from it would
+	 * end the child well within this delay.
+	 */
+	usleep(100000);
+	if (waitpid(pid, NULL, WNOHANG)) {
+		pid = -ESRCH;
+		goto cleanup;
+	}
+
+	if (cg_kill_wait(parent))
+		goto cleanup;
+
+	if (cg_read_strcmp_wait(parent, "cgroup.events", "populated 0\n"))
+		goto cleanup;
+
+	ret = KSFT_PASS;
+
+cleanup:
+	if (pid > 0) {
+		kill(pid, SIGKILL);
+		wait_for_pid(pid);
+	}
+	if (threaded)
+		cg_destroy(threaded);
+	if (parent)
+		cg_destroy(parent);
+	free(threaded);
+	free(parent);
+	return ret;
+}
+
+/*
+ * cgroup.kill also fails below a threaded cgroup.  Once a turns threaded, its
+ * child b, a thread root, becomes an invalid domain, and b's threaded child c
+ * joins the domain of a's parent.  A thread of a process outside of a can then
+ * join c, and killing b would kill that whole process.
+ */
+static int test_cgkill_below_threaded(const char *root)
+{
+	char *a = NULL, *b = NULL, *c = NULL;
+	int ret = KSFT_FAIL;
+	pid_t pid = -ESRCH;
+
+	a = cg_name(root, "cg_test_kill_below_threaded");
+	b = cg_name(root, "cg_test_kill_below_threaded/b");
+	c = cg_name(root, "cg_test_kill_below_threaded/b/c");
+	if (!a || !b || !c)
+		goto cleanup;
+
+	if (cg_create(a) || cg_create(b) || cg_create(c))
+		goto cleanup;
+
+	if (cg_write(c, "cgroup.type", "threaded") ||
+	    cg_write(a, "cgroup.type", "threaded"))
+		goto cleanup;
+
+	pid = cg_run_nowait(root, child_fn, NULL);
+	if (pid < 0)
+		goto cleanup;
+
+	if (cg_write_numeric(c, "cgroup.threads", pid))
+		goto cleanup;
+
+	if (cg_write(b, "cgroup.kill", "1") != -EOPNOTSUPP)
+		goto cleanup;
+
+	/* As in test_cgkill_threaded(), the refused write must not kill. */
+	usleep(100000);
+	if (waitpid(pid, NULL, WNOHANG)) {
+		pid = -ESRCH;
+		goto cleanup;
+	}
+
+	ret = KSFT_PASS;
+
+cleanup:
+	if (pid > 0) {
+		kill(pid, SIGKILL);
+		wait_for_pid(pid);
+	}
+	cg_destroy(c);
+	cg_destroy(b);
+	cg_destroy(a);
+	free(c);
+	free(b);
+	free(a);
+	return ret;
+}
+
 #define T(x) { x, #x }
 struct cgkill_test {
 	int (*fn)(const char *root);
@@ -482,6 +607,8 @@ struct cgkill_test {
 	T(test_cgkill_clone_into_killed),
 	T(test_cgkill_exited_leader),
 	T(test_cgkill_exited_leader_migrated),
+	T(test_cgkill_threaded),
+	T(test_cgkill_below_threaded),
 };
 #undef T
 
-- 
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 ` [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-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 ` Shakeel Butt [this message]
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-10-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=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