From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796147756; b=xpRndQfsi2St37I4+GevutOwPH48HWw5EwqvUWBHfnc0/Mfu+8CXH+GU7W71E5p18+Mm cxAP1yG04gKkbndOpwT5yKS1usQnE3V1TYnBDuSBjnRftlPAwViy6wuP6/x46cP67RI0D HSXfzqZGFGy69UUVDdHZf/HuZeokvqKxC/2NhUh45Btkr29twDvx7uKYKbdm+HOoRGQTX PbdTX0FxawlXAGeXHz7ze0LOidoD6O3o5voGdZXEQWKwShPfQ3o21bWiAdsvpnAajZoHQ UX3LZi0llF9MbNyG/98yFuTFZFHaO+m2t8aWfexMtoSKZ4OXe5XaorzG2bNYeSjbGHQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796147756; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-Id: In-Reply-To:References:X-Mailer:Mime-Version:Content-Type: Content-Transfer-Encoding; bh=+viV9IKVgtNVk9aM1uel7H9tOHgghJ/dnPf4Hf4RNUw=; b=ycTg0hC4nOo6yqX6qhDbJ8CzrHfig5SMfN6RMBSrrhVNH8yx60PQgU9CIaOMM5/+Kc21 N3BtdKKRwMgo0GhGeecea2Wkllw3FQoInUDiu2SvtunPKinT0C79aZwpBjAS7eiRbueRn WmDQCp8YApcphbJLZdqUsvuVVXLmiivxrWPPcpId3LVGNVAsPK5I10DP++QoDsAJydUOx Ek7iprTBJ1HR8Cql5mZUK+yTu6fY1J4TaL9tRpRhhOX1eF2x6yzIPHkoz6ImD5I5DiAVY Z9/UtpuSFQANC3RF+Op6/yDdqCO1fDyrc8wXJLyOjh25qDR/aK/K5gz/nkzHrhNUbVA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=Rx2t+4p8; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1072A606A330; Tue, 1 Dec 2026 17:55:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=Rx2t+4p8 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 28FB31F000C1; Tue, 1 Dec 2026 17:55:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796147755; bh=+viV9IKVgtNVk9aM1uel7H9tOHgghJ/dnPf4Hf4RNUw=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=Rx2t+4p8o8IQPrsqtj6Pzi/GyKLDmjjrd1CznoX4LXWkxG2qqeVlgBdLCHI6XFz+J l0/sDkZScvzI6y+OFs5jNTKWAAipk+osnnXS9gE8ukuiG/AeOQ1fubWcHwS/YVXC9U mY8iPjl7OBXi2Qh6QG2BoM6WJ7nAOWb1pN/sinf0= Date: Tue, 1 Dec 2026 09:55:54 -0800 From: Andrew Morton To: Shakeel Butt Cc: Tejun Heo , 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: Re: [PATCH v2 10/11] cgroup: don't hold cgroup_mutex for cgroup.kill Message-Id: <20261201095554.faf34ccb08077ff486e74cb9@linux-foundation.org> In-Reply-To: <20261130173425.4117363-11-shakeel.butt@linux.dev> References: <20261130173425.4117363-1-shakeel.butt@linux.dev> <20261130173425.4117363-11-shakeel.butt@linux.dev> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) X-Mailing-List: cgroups@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Mon, 30 Nov 2026 09:34:24 -0800 Shakeel Butt wr= ote: > 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. >=20 > 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: From: Andrew Morton Subject: cgroup: reject a threaded switch during a descendant kill Date: Tue, 01 Dec 2026 01:35:32 -0800 A kill of a descendant can outlive its last task. The ancestor then looks empty, and cgroup_enable_threaded() checks only the ancestor's own nr_subtree_kills. It can switch the ancestor to threaded while the descendant kill is still active. The switch changes the domain of threaded descendants, allowing threads from outside the killed subtree to enter it during the kill. This fixes Shakeel Butt's "cgroup: don't hold cgroup_mutex for cgroup.kill" patch. Check the active kill list for the cgroup and all its descendants under css_set_lock. cgroup_mutex keeps the ancestry stable. Once the empty ancestor passes the check, migrations cannot populate it until the switch finishes, and a new kill of an empty subtree returns immediately. A 64 CPU KASAN/lockdep test held a descendant kill open for one second after its last task exited. The v2 kernel accepted the ancestor's cgroup.type=3Dthreaded write during that interval. With this change the write returns EBUSY. Link: https://lore.kernel.org/r/20261130173425.4117363-11-shakeel.butt@linu= x.dev Signed-off-by: Andrew Morton Cc: Tejun Heo Cc: Johannes Weiner Cc: Michal Koutn=FD kernel/cgroup/cgroup.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) --- a/kernel/cgroup/cgroup.c~cgroup-dont-hold-cgroup_mutex-for-cgroupkill-f= ix +++ a/kernel/cgroup/cgroup.c @@ -3865,6 +3865,7 @@ static int cgroup_enable_threaded(struct struct cgroup *dom_cgrp =3D parent->dom_cgrp; struct cgroup *dsct; struct cgroup_subsys_state *d_css; + struct cgroup_kill_ctx *kill; int ret; =20 lockdep_assert_held(&cgroup_mutex); @@ -3889,15 +3890,20 @@ static int cgroup_enable_threaded(struct return -EOPNOTSUPP; =20 /* - * 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. + * A kill of @cgrp or a descendant may still be running after its last + * task exits. Switching now changes threaded descendants' domains and + * can let outside threads enter a subtree whose kill is still active. + * No new kill can start after this check: @cgrp is empty, migrations + * wait for cgroup_mutex, and cgroup_kill() skips empty subtrees. */ spin_lock_irq(&css_set_lock); - ret =3D cgrp->nr_subtree_kills ? -EBUSY : 0; + ret =3D 0; + list_for_each_entry(kill, &cgroup_kills, node) { + if (cgroup_is_descendant(kill->cgrp, cgrp)) { + ret =3D -EBUSY; + break; + } + } spin_unlock_irq(&css_set_lock); if (ret) return ret; _