[PATCH v2 08/11] selftests/cgroup: test cgroup.kill in a threaded cgroup

From: Shakeel Butt

Date: Mon Nov 30 2026 - 12:35:42 EST


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.

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

diff --git a/tools/testing/selftests/cgroup/test_kill.c b/tools/testing/selftests/cgroup/test_kill.c
index 756bc7193d4b..d3fab2ba732b 100644
--- a/tools/testing/selftests/cgroup/test_kill.c
+++ b/tools/testing/selftests/cgroup/test_kill.c
@@ -471,6 +471,73 @@ 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;
+}
+
#define T(x) { x, #x }
struct cgkill_test {
int (*fn)(const char *root);
@@ -482,6 +549,7 @@ struct cgkill_test {
T(test_cgkill_clone_into_killed),
T(test_cgkill_exited_leader),
T(test_cgkill_exited_leader_migrated),
+ T(test_cgkill_threaded),
};
#undef T

--
2.53.1-Meta