From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 022BB606A33D for ; Mon, 30 Nov 2026 17:35:53 +0000 (UTC) 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=jFpNDw0+ Received: by kanga.kvack.org (Postfix) id 7B7ED6B05D5; Mon, 30 Nov 2026 12:35:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 796996B05D6; Mon, 30 Nov 2026 12:35:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6A5BF6B05D6; Mon, 30 Nov 2026 12:35:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 241716B05D3 for ; Mon, 30 Nov 2026 12:35:50 -0500 (EST) Received: from smtpin19.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A636D1A0A7C for ; Mon, 30 Nov 2026 17:35:44 +0000 (UTC) X-FDA: 85492487808.19.711EBED Received: from mta0.migadu.com (out-54.mta0.migadu.com [91.218.175.54]) by imf22.hostedemail.com (Postfix) with ESMTP id A60BCC0012 for ; Mon, 30 Nov 2026 17:35:42 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=jFpNDw0+; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf22.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.54 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796060143; b=GZ81URQxTkr5hnh1EoZw42NkCbBEyg9bpwSFbWGjudks7Z23Pv/6z6VOgAgMhLyuWsJlKe 9zcDZ3dgVI442WwChon7gt1oyuLANcSp6DhxS7uhaCwi4+E1FO25Bvp8RJ1f8WTrw63Vpa 2QzDPX0lgts8PORSYPMMETzXL/Zrwpc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796060143; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/TEzVK+gdSwpHDLEIbBTi0l+t2GWhLt0tRyJAI93oVc=; b=nNjdldrde6k3tg4k9mOqZLRBxt3yoblhuIyHiqc0b2tTEIQvNU0OY7CitGJexDo0/PBm3L wRt/UKXCuO7S1pD5WsMEn2xOxes0k3WXfAP1A43k8MraLT8vnB3p0TPeR+lA3uQVFw7fPW x9daexItGVlcyZ3RxCLXCF+tZyHLBV4= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=jFpNDw0+; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf22.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.54 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=8yFk74gim7vQaIk52waqZL3JvxhQBaSefbZbVAPYdGc=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796060141; v=1; x=1796664941; b=jFpNDw0+Ms59BBPN8Sp1HYYGxuIoYUO7Z5RXW3o1Slq6PimPoO8SfHpEgvB/wP5AY7TnBzEo k/Bf7BdyIDFt7GJfkG7BpOVT59Z3CPrG08qW/ObdBfWOlZbsq93eKVTbWgdvZq0dm8RrKLNxJPm POmJlblUwY/dvdr+J+ytUFkI= X-Envelope-To: linux-mm@kvack.org Received: by smtp.migadu.com with ESMTPS id 56b0f2577195d7eb; Mon, 30 Nov 2026 17:35:38 +0000 X-Mizu-Trace-ID: 56b0f2577195d7eb 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 v2 10/11] cgroup: don't hold cgroup_mutex for cgroup.kill Date: Mon, 30 Nov 2026 09:34:24 -0800 Message-ID: <20261130173425.4117363-11-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261130173425.4117363-1-shakeel.butt@linux.dev> References: <20261130173425.4117363-1-shakeel.butt@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: 1jh9664ef1owk85x6m5p98ga7gj6dz1c X-Rspamd-Queue-Id: A60BCC0012 X-Rspamd-Server: rspam06 X-Rspam-User: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796060142-119930 X-HE-Meta: U2FsdGVkX19jdEi8QSywrDrYamwOXotCyRX0MnZCTjewnjAe33d8KRdfrJHFU3whGLrJ0TKaWPuhI3thSRNN6s5BkFQPkHwW2GFT/gN9PW8MY6Nm7y4b3oVX0fLlQoRbKdkhkXUy4uzcrFKqjfig6CS1LVVtOtUIe/Vd4RnCmWHYkWfZbg/c+mdWBjJeXRnBgT0d3doNl5lldjQUGi1H9UGlEFpasmpXa6NABTKON36Akv6CX+7Jw5KtUenGdflLWGWnLmzfeQScvTuifmu++xBAMHKDkYA2F68y8j1Lzg3qRJ0NeSRqtEu54VEOtx/pifoEqEAJhdHGeA0Z8N0FcZDDtgC0YQ7wmljyfXr46+ZtfsNTfXvbxclezsiI+g5wGf34cSyR2l/hWYO502O/+4jwNMuq7iP5zupVigR5g20ILCJIAocH8mwibHq8hSulwJY9iJdNYH+mCH8a1oOUrNe6WlQyNUFr8M3wHg5Tqwsfiz3RN0RvnmS/HgSs/vvfPVerHRJFQTMSyp3ycRsMDRZZ1TM8VIZKy2hGBM/7Na7fzRWS7ELU/oGq3X9FClvZ5Lsa8OoqVMGU/bPocOG/qkqtZQwlFawlNTY9dZ9CUcz5e+DpWuC+vLCQDUpH5LUgxZMoaYg6GIBoBYMqAp+jZnOVGN4bwGr+cVclKQu01MGrnBIer3CAcZ1PHgmMDP7/rv1lsh/niNdw5zDh+AZrpKx10ZkCwMtJfjc4U4j+zNnrY/ZI5toPKvKFlair3N4mCznoh/pSRaw4kvZeP1qX+mkDzndY7l9HXXhfULjnqU3dEzY76gu0wwBgTKHoCskfHq0zR7iF+iSNGlZcHG0wyMIwTflKD5bijnpsh5Lpj6rhbfOgzX4bvguQHepct+UAw7dTMdl8MuCEsrD9hiXHu08gsouYToRq2Bj3nGywrTyQkrJk9xAmUgsFOY0agPqWs8vrfnqDXn5/UaXxK73 vyu79t0t u2ji2xIhG5HHhqwSqa+OM7WdCqFthQtgT8hLN1RJ/r1Bf4MaasTH4VMxRYnGP3DPmOvgHfxe87PAvyl3ltSfAgAGuDql0svcy4FjP+qLFcoUMGZUUuyHn89FIMnrrQcselSq4kvvFw7nIUlhgcnQOCLTw0NS/3U1dHYwqYWSbsQqgZ6yl3dG7D/D572odrBnIse4ez7tY0qNkcRBJtfEpwI0fSW7kWpmCAH95afAD+VJ8TyPCo4rH8NEOWgQYkv4TTAuhf97rM5IClGt6u4F6PyIQZgBEY++YNAenWqsX7wxKbZDs2iQEiIZiHIKLHEdmJ5eAl/LuvYZdc3rmkGfIruputd8Z9hiNoF+DdP8xyeN4NvRSIgUzIYiTAeWIsBXLmeBm6dNsQYp48RIsd7Pap8XEhgV8VDzw4rP9w36cYoNks0xqs6XORwD+yO8LTDbWGpMm+jJxgquEVJMDln2Q1+/VT1BWnNnuzLiUKAMICU5z6xxMTmjI4x+rCL34Vlu6PMAixYAaaSQ+Qy8FlqOonqAoRBhfwah8Lz4/ Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 cgroup.kill holds cgroup_mutex while it signals every task in the subtree. It is often used when the system is already in trouble, so it should not wait for other cgroup operations, and they should not wait for it. cgroup_mutex kept the cgroup and its subtree stable, and blocked migrations, CLONE_INTO_CGROUP forks and switches to threaded during the kill. Replace it: - Pin the cgroup with cgroup_kn_break_active(), which also drops the kernfs active reference so that rmdir doesn't wait for the kill. rmdir can now remove the cgroup during the kill. The reference keeps the cgroup in memory, the walk skips removed cgroups, and kernfs lets the write outlive the removal of its file. Commit 071d8e4c2a3b ("kernfs: Relax constraint in draining guard") removed a warning that this overlap could trigger. Walk the subtree under RCU, pin each cgroup while its tasks are killed, and skip empty subtrees. - Count the running kills in the top cgroup (nr_subtree_kills). When a task leaves a css_set in a subtree being killed, cgroup_migrate_execute() kills it. This covers moves out of the subtree, moves inside it, and css_set changes from controller changes. Tasks that move in are left alone. A list of running kills lets migrations skip the check when no kill runs. - The walk sends SIGKILL from the writer, and a pid namespace init ignores it unless the writer is outside the init's pid namespace. In cgroup_migrate_execute(), current is the migrating task instead. So each kill records its writer's pid, and a task that leaves the subtree gets a forced SIGKILL only if one of the writers is outside its pid namespace. Otherwise a pid namespace init is left alone, as the walk would leave it. - Kill a CLONE_INTO_CGROUP child if its parent's subtree was being killed at any time during the fork. - Several kills can now walk one cgroup at once. Instead of an odd kill_seq, bump it when a walk starts and when it ends, and count the walks in nr_killing. - Only an empty cgroup can switch to threaded, but threads from outside could join it while a kill of it still runs, and the checks above would kill their whole process. So the switch fails with EBUSY while the cgroup is being killed. cgroup.kill returns right away for an empty subtree, which has nothing to kill, and nothing can enter the cgroup during the switch, so no kill can start during the switch. A CGRP_THREADED flag is set under css_set_lock when the switch succeeds, and cgroup.kill fails with EOPNOTSUPP once it is set. cgroup.kill checks for a removed cgroup in the same section. The walk relies on the task iterator to return every task that stays in a cgroup during the walk. This needs "cgroup: don't lose a task in css_task_iter when a migration fails". Document that a process moved out of the tree or inside it during a kill is killed, and so is a CLONE_INTO_CGROUP child that a process in the tree creates outside of it. Also document that a process moved in may or may not be killed, and the new EBUSY. Tested in a VM, with sleeps added by a debug patch that is not part of this series. Tasks moved inside or out of the subtree, and a CLONE_INTO_CGROUP child of a task in it, all died, and so did a task whose migration failed during the walk. A pid namespace init that moved during a kill died when the kill was written from the host, and survived when it was written inside its namespace, as it does with the walk. With the matching check turned off, these tests failed. A switch to threaded during a kill failed with EBUSY, and a kill during a switch that failed returned 0. With the EBUSY check turned off, an outside thread that joined and created a thread took its whole process down. The test_kill and test_core selftests pass with lockdep on. Signed-off-by: Shakeel Butt --- Documentation/admin-guide/cgroup-v2.rst | 10 +- include/linux/cgroup-defs.h | 18 +- include/linux/sched/task.h | 1 + kernel/cgroup/cgroup.c | 246 ++++++++++++++++++++---- 4 files changed, 235 insertions(+), 40 deletions(-) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index 2f792ff29425..7d60d405d508 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1057,11 +1057,17 @@ All cgroup core files are prefixed with "cgroup." tree will be killed via SIGKILL. Killing a cgroup tree will deal with concurrent forks appropriately and - is protected against migrations. + is protected against migrations: a process that is moved out of the + tree, or between its cgroups, while the tree is being killed is killed + as well, and so is a process that a process in the tree creates + outside of it with CLONE_INTO_CGROUP. A process that is moved into + the tree, or created in it with CLONE_INTO_CGROUP from outside, while + the tree is being killed may or may not be killed. 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. + the whole thread-group. Writing "threaded" to "cgroup.type" fails + with EBUSY while the cgroup is being killed. cgroup.pressure A read-write single value file that allowed values are "0" and "1". diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h index 0dae54b033e9..a6f42dede900 100644 --- a/include/linux/cgroup-defs.h +++ b/include/linux/cgroup-defs.h @@ -72,6 +72,13 @@ enum { /* Cgroup is frozen. */ CGRP_FROZEN, + + /* + * cgroup.type is threaded. Set under css_set_lock when a switch to + * threaded succeeds, so that a kill of the cgroup and the switch exclude + * each other, see cgroup_enable_threaded() and cgroup_kill(). + */ + CGRP_THREADED, }; /* cgroup_root->flags */ @@ -528,11 +535,16 @@ struct cgroup { int nr_threaded_children; /* # of live threaded child cgroups */ /* - * Sequence number for cgroup.kill, odd while a kill is walking the - * cgroup's tasks. Incremented with both cgroup_mutex and css_set_lock - * held. Readers hold either one. + * cgroup.kill state, protected by css_set_lock. A kill bumps + * kill_seq when it starts and when it ends walking this cgroup's + * tasks, and nr_killing counts such walks in progress. + * subtree_kill_seq and nr_subtree_kills do the same for whole kills + * of the subtree rooted here. See cgroup_kill(). */ unsigned int kill_seq; + unsigned int nr_killing; + unsigned int subtree_kill_seq; + unsigned int nr_subtree_kills; struct kernfs_node *kn; /* cgroup kernfs entry */ struct cgroup_file procs_file; /* handle for "cgroup.procs" */ diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h index 90ed5bc3c7af..bc1c17fcafea 100644 --- a/include/linux/sched/task.h +++ b/include/linux/sched/task.h @@ -45,6 +45,7 @@ struct kernel_clone_args { struct cgroup *cgrp; struct css_set *cset; unsigned int kill_seq; + unsigned int subtree_kill_seq; }; /* diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index bdc044ad21d3..cb8d9165ead2 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -2635,6 +2635,81 @@ void cgroup_attach_unlock(enum cgroup_attach_lock_mode lock_mode, cpus_read_unlock(); } +/* + * A cgroup.kill write in progress. It lives on the writer's stack and is on + * cgroup_kills, which is protected by css_set_lock. + */ +struct cgroup_kill_ctx { + struct list_head node; + struct cgroup *cgrp; /* the cgroup written to */ + struct pid *writer; /* see cgroup_kill_leaving_task() */ +}; + +static LIST_HEAD(cgroup_kills); + +/** + * cgroup_subtree_kill_seq - sample the cgroup.kill state of a cgroup's subtrees + * @cgrp: cgroup of interest + * @killing: if not %NULL, set to %true if a kill of @cgrp's subtree or of an + * ancestor's subtree is in progress + * + * Returns a value that changes whenever such a kill starts or ends. The root + * can't be killed. + */ +static unsigned int cgroup_subtree_kill_seq(struct cgroup *cgrp, bool *killing) +{ + unsigned int seq = 0; + int i; + + lockdep_assert_held(&css_set_lock); + + for (i = cgrp->level; i > 0; i--) { + struct cgroup *anc = cgrp->ancestors[i]; + + seq += anc->subtree_kill_seq; + if (killing && anc->nr_subtree_kills) + *killing = true; + } + return seq; +} + +/** + * cgroup_kill_leaving_task - kill a task that leaves a subtree being killed + * @task: task leaving a css_set of @cgrp + * @cgrp: cgroup of interest + * + * If a kill of @cgrp's subtree or of an ancestor's subtree is in progress, + * kill @task the way the walk of that kill would. The walk sends SIGKILL from + * the writer, and a pid namespace init ignores it unless the writer is outside + * its pid namespace, see send_signal_locked(). current is the migrating task, + * so decide for the writers here. + */ +static void cgroup_kill_leaving_task(struct task_struct *task, + struct cgroup *cgrp) +{ + struct pid_namespace *ns = task_active_pid_ns(task); + bool killing = false, force = false; + struct cgroup_kill_ctx *kill; + unsigned long flags; + + lockdep_assert_held(&css_set_lock); + + list_for_each_entry(kill, &cgroup_kills, node) { + if (!cgroup_is_descendant(cgrp, kill->cgrp)) + continue; + killing = true; + if (!pid_nr_ns(kill->writer, ns)) + force = true; + } + + if (!killing || (task->flags & PF_KTHREAD) || + !lock_task_sighand(task, &flags)) + return; + if (force || !(task->signal->flags & SIGNAL_UNKILLABLE)) + send_signal_locked(SIGKILL, SEND_SIG_PRIV, task, PIDTYPE_TGID); + unlock_task_sighand(task, &flags); +} + /** * cgroup_migrate_add_task - add a migration target task to a migration context * @task: target task @@ -2820,6 +2895,10 @@ static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx) struct css_set *from_cset = task_css_set(task); struct css_set *to_cset = cset->mg_dst_cset; + /* a task can't leave a subtree being killed */ + if (unlikely(!list_empty(&cgroup_kills))) + cgroup_kill_leaving_task(task, cset->dfl_cgrp); + get_css_set(to_cset); to_cset->nr_tasks++; css_set_move_task(task, from_cset, to_cset, true); @@ -3810,6 +3889,20 @@ static int cgroup_enable_threaded(struct cgroup *cgrp) !cgroup_can_be_thread_root(dom_cgrp)) return -EOPNOTSUPP; + /* + * cgroup.kill doesn't hold cgroup_mutex. Threads that join @cgrp + * after the switch could be killed with their whole process by a kill + * of @cgrp that is still running, so don't switch during such a kill. + * No new kill of @cgrp can start until we're done: @cgrp is empty, + * nothing can enter it while we hold cgroup_mutex, and cgroup_kill() + * returns right away for an empty subtree. + */ + spin_lock_irq(&css_set_lock); + ret = cgrp->nr_subtree_kills ? -EBUSY : 0; + spin_unlock_irq(&css_set_lock); + if (ret) + return ret; + /* * The following shouldn't cause actual migrations and should * always succeed. @@ -3825,6 +3918,13 @@ static int cgroup_enable_threaded(struct cgroup *cgrp) parent->nr_threaded_children++; cgroup_finalize_control(cgrp, ret); + + /* new kills of @cgrp fail from now on, see cgroup_kill() */ + if (!ret) { + spin_lock_irq(&css_set_lock); + set_bit(CGRP_THREADED, &cgrp->flags); + spin_unlock_irq(&css_set_lock); + } return ret; } @@ -4349,18 +4449,16 @@ static void __cgroup_kill(struct cgroup *cgrp) struct css_task_iter it; struct task_struct *task; - lockdep_assert_held(&cgroup_mutex); - /* - * kill_seq is odd while this walk is in progress. The walk can signal - * a forking parent after copy_process() passed its - * fatal_signal_pending() check, and can pass the parent before the - * child is linked. cgroup_post_fork() therefore kills the child if - * kill_seq was odd when cgroup_css_set_fork() sampled it, or if it - * changed since. + * The walk below can signal a forking parent after copy_process() + * passed its fatal_signal_pending() check, and can pass the parent + * before the child is linked. cgroup_post_fork() kills a child that + * is linked while a walk is in progress (nr_killing), or after one + * started or ended since cgroup_css_set_fork() sampled kill_seq. */ spin_lock_irq(&css_set_lock); cgrp->kill_seq++; + cgrp->nr_killing++; spin_unlock_irq(&css_set_lock); css_task_iter_start(&cgrp->self, CSS_TASK_ITER_THREADED, &it); @@ -4379,18 +4477,83 @@ static void __cgroup_kill(struct cgroup *cgrp) spin_lock_irq(&css_set_lock); cgrp->kill_seq++; + cgrp->nr_killing--; spin_unlock_irq(&css_set_lock); } -static void cgroup_kill(struct cgroup *cgrp) +/* + * cgroup.kill doesn't hold cgroup_mutex, so tasks can migrate, the subtree can + * grow and CLONE_INTO_CGROUP forks can run while it walks. Tasks that are in + * the subtree when the kill starts (nr_subtree_kills raised) can't get out: + * + * - a regular fork links the child next to the parent, see __cgroup_kill(); + * - cgroup_migrate_execute() kills a task leaving a css_set of a cgroup in the + * subtree, which covers moves out of the subtree, moves between its cgroups + * and css_set changes from controller changes. It decides like the walk + * would, see cgroup_kill_leaving_task(); + * - cgroup_post_fork() kills a CLONE_INTO_CGROUP child whose parent is in the + * subtree while it is being killed. + * + * Tasks that join the subtree from outside after the kill started may or may + * not be killed. So a kill of an empty subtree has nothing to do. + * + * Killing is a process directed operation, i.e. the whole thread-group is + * taken down, so act like we do for cgroup.procs and refuse threaded cgroups. + * A cgroup can't switch to threaded while it is being killed, see + * cgroup_enable_threaded(), so every process with a thread in the subtree is + * entirely inside it. + */ +static int cgroup_kill(struct cgroup *cgrp) { + struct cgroup_kill_ctx kill = { .cgrp = cgrp }; struct cgroup_subsys_state *css; - struct cgroup *dsct; + int ret = 0; - lockdep_assert_held(&cgroup_mutex); + spin_lock_irq(&css_set_lock); + if (cgroup_is_dead(cgrp)) + ret = -ENODEV; + else if (test_bit(CGRP_THREADED, &cgrp->flags)) + ret = -EOPNOTSUPP; + /* an empty subtree has nothing to kill, see above */ + if (ret || !cgroup_is_populated(cgrp)) { + spin_unlock_irq(&css_set_lock); + return ret; + } + kill.writer = get_pid(task_pid(current)); + list_add(&kill.node, &cgroup_kills); + cgrp->subtree_kill_seq++; + cgrp->nr_subtree_kills++; + spin_unlock_irq(&css_set_lock); + + rcu_read_lock(); + css_for_each_descendant_pre(css, &cgrp->self) { + /* + * A task that enters an empty subtree later comes from + * outside, or from the part being killed and is killed then. + */ + if (!cgroup_is_populated(css->cgroup)) { + css = css_rightmost_descendant(css); + continue; + } + if (!css_tryget_online(css)) + continue; + rcu_read_unlock(); + + __cgroup_kill(css->cgroup); + cond_resched(); - cgroup_for_each_live_descendant_pre(dsct, css, cgrp) - __cgroup_kill(dsct); + rcu_read_lock(); + css_put(css); + } + rcu_read_unlock(); + + spin_lock_irq(&css_set_lock); + list_del(&kill.node); + cgrp->subtree_kill_seq++; + cgrp->nr_subtree_kills--; + spin_unlock_irq(&css_set_lock); + put_pid(kill.writer); + return 0; } static ssize_t cgroup_kill_write(struct kernfs_open_file *of, char *buf, @@ -4407,21 +4570,17 @@ static ssize_t cgroup_kill_write(struct kernfs_open_file *of, char *buf, if (kill != 1) return -ERANGE; - cgrp = cgroup_kn_lock_live(of->kn, false); + /* + * No cgroup_mutex, see cgroup_kill(). Still drop the active + * reference, so that removing the cgroup doesn't wait for a long walk. + */ + cgrp = cgroup_kn_break_active(of->kn); if (IS_ERR(cgrp)) return PTR_ERR(cgrp); - /* - * 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. - */ - if (cgroup_is_threaded(cgrp)) - ret = -EOPNOTSUPP; - else - cgroup_kill(cgrp); + ret = cgroup_kill(cgrp); - cgroup_kn_unlock(of->kn); + cgroup_kn_unbreak_active(of->kn, cgrp); return ret ?: nbytes; } @@ -7070,9 +7229,18 @@ static int cgroup_css_set_fork(struct kernel_clone_args *kargs) goto err; } + /* + * cgroup.kill doesn't hold cgroup_mutex. Sample the target, which the + * child joins, and the subtrees containing the parent, which the child + * leaves, see cgroup_post_fork(). + */ + spin_lock_irq(&css_set_lock); + kargs->kill_seq = dst_cgrp->kill_seq; + kargs->subtree_kill_seq = cgroup_subtree_kill_seq(cset->dfl_cgrp, NULL); + spin_unlock_irq(&css_set_lock); + put_css_set(cset); kargs->cgrp = dst_cgrp; - kargs->kill_seq = dst_cgrp->kill_seq; return ret; err: @@ -7194,7 +7362,7 @@ void cgroup_post_fork(struct task_struct *child, { unsigned int cgrp_kill_seq = 0; unsigned long cgrp_flags = 0; - bool kill = false; + bool kill = false, killing = false; struct cgroup_subsys *ss; struct css_set *cset; int i; @@ -7206,13 +7374,22 @@ void cgroup_post_fork(struct task_struct *child, /* init tasks are special, only link regular threads */ if (likely(child->pid)) { - if (kargs->cgrp) { - cgrp_flags = kargs->cgrp->flags; - cgrp_kill_seq = kargs->cgrp->kill_seq; - } else { - cgrp_flags = cset->dfl_cgrp->flags; - cgrp_kill_seq = cset->dfl_cgrp->kill_seq; - } + struct cgroup *cgrp = kargs->cgrp ?: cset->dfl_cgrp; + + cgrp_flags = cgrp->flags; + cgrp_kill_seq = cgrp->kill_seq; + killing = cgrp->nr_killing; + + /* + * A CLONE_INTO_CGROUP child leaves the parent's cgroup. Don't + * let it out of a subtree that was being killed at any time + * during the fork. @current's cgroup is pinned by the + * threadgroup lock. + */ + if (kargs->cgrp && + cgroup_subtree_kill_seq(task_dfl_cgroup(current), + &killing) != kargs->subtree_kill_seq) + killing = true; WARN_ON_ONCE(!list_empty(&child->cg_list)); cset->nr_tasks++; @@ -7247,8 +7424,7 @@ void cgroup_post_fork(struct task_struct *child, * child down right after we finished preparing it for * userspace. */ - kill = kargs->kill_seq != cgrp_kill_seq || - (kargs->kill_seq & 1); + kill = killing || kargs->kill_seq != cgrp_kill_seq; } spin_unlock_irq(&css_set_lock); -- 2.53.1-Meta