From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796409677; b=XuqNSPqFWY/MmCIgPna9TfKY3wOQAiUWTJqCOmyEPyRTQUZre0jZy1grIgh1ZQtbzxt7 0q+EUnev7DzJKnvv3Z+HaZq/+ZIfGshCMI17MzrS9NkxzJ+1lCHjtI9XE80Ue6wjE6Ewc vUdYYk5P7YC9rmu0pxOzeVnh0vRjWXom3hd5i7YAbhchVTnAspsSW01wRE1+D75pXe45Y CuzTtwg5Ocoe2ocYMp5bR/Giu3ylISAuF1D1Uqr5vFxqAmM3vWEWMOn33ZN1hz8+GVFY0 IVCLVh2vXXMfG5CG/70lU5sMCCqoKk2mZuANhPJVrUEwoZt3rDYLyZNdD0Y0oJLDl6g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796409677; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=tVT9n/76+1tFnMULs333qQmBWkuagSc52r6ZsYQ9ChQ=; b=rmWpeXDry0sMR/ldqKPGfM3mNUjSG0MKZDHY1JRLXGMTkgFxh3HuXIN0ez52rvxLp65+ gBVrSnlTXC5tiiZSuhSKnKaQP/7U0gjQm374/Qlo/TYob1NaUOpEhqMATyKDSd3srFWyU ITUKk+YII72BcsI1x27oiQqaLyF2w3EhsWYhm8TIbgJ7WaVqBnzvtRog77NxS4ulv8vDQ ShQE1eVOH41Q+Bbl0LMcW/hmz/26aAv8YPUsy6VN2Tdxe52G17b7EkVtO2oR0/Ow772UT oq2AM6XjRjo7lvEwKG03XSGU2+Fm5yNoO7oz1/mcjIefmHVsUi3jNqxX/F6v69K6IuQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=o55QYsJS; arc=none smtp.remote-ip=91.218.175.51 Received: from mta0.migadu.com (out-51.mta0.migadu.com [91.218.175.51]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9DC9B606A682 for ; Fri, 4 Dec 2026 18:41:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9DC9B606A682 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=o55QYsJS X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=awcqoPLlfmWIwFOPx6uxjWOj2EzD3MTzf9zyayuAOkU=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796409674; v=1; x=1797014474; b=o55QYsJS5pzwXlPymBt0dqWkawl6XwLgxzizmFtJI4g+Rx2LQa0NONkT50DNyICylunuGID0 Jn+HFEJPNEnqiXBZKCvKcHDYNsb+pn+JmH6wruCNZ8DUFbqmQcz8oLzVGkbrEGAVzg2YuLaCo+Z 1tLMs8p7hWayFNK4GUWNeYUk= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 0d7069832dfe2c42; Fri, 04 Dec 2026 18:41:14 +0000 X-Mizu-Trace-ID: 0d7069832dfe2c42 X-Migadu-Flow: FLOW_OUT From: Shakeel Butt To: Tejun Heo Cc: Andrew Morton , Johannes Weiner , Michal Koutny , Michal Hocko , Roman Gushchin , Christian Brauner , Oleg Nesterov , Sebastian Andrzej Siewior , Suren Baghdasaryan , "T . J . Mercier" , Usama Arif , Meta kernel team , 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 Message-ID: <20261204184041.1489976-10-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261204184041.1489976-1-shakeel.butt@linux.dev> References: <20261204184041.1489976-1-shakeel.butt@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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