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 > 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 Closes: https://lore.kernel.org/r/sashiko-outbox-241838@kernel.org Cc: stable@vger.kernel.org # v5.14+ Signed-off-by: Shakeel Butt --- 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