From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.140 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796060106; b=Tw1raHIxDsIu6h8lTEvbTfhL2DWnkbaLBSMkgun0hWC6Nadsf0/oROU9/Jz9DZLc33D5 a9IAS1nO3yzVfJxa+ReSowRaRcw8GbC9NY8ECH8Dgi+4FPddYa4Q9IIYZ1vmJ0RVVDycT pZPG9QR1pK0+36SvkXEFofc1HZ+m5YUkztQJA2U6Htxw9lo88wuHFVKC01QSP0r5I013K C+GY2d0vhI1QRimoqsxgg3hjQkcQiMgSCZN6Ww/YYhRBb1YXH2nZxh4awOgSYrUQQqd5S XgL+aSyQexiZumod4+HHbzKY5qPrgiL6rI9LzRV3DNquJ+yXYsRFlLa9SKJNUzVz4GQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796060106; 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=IjHamXUboZwDKftzxIS2pZp9J/noWX2TVerpuxqN8X4=; b=Ny88LfUKuBOIKvomxlP3usPDCWXGth6SOXpAMi2lCB4DjEIiIP1WiQGylzmFecHX4Zqb 6aqJlgWTDuBxdqJ+v7QagV4qPkw3tMoPQ1AiMQ080UsmbNyizkj0iMXtug+K0h/qqDWhV +bcbfq9X1qXkBpJ545kHqTm3zZAR20r+76GnbuZDdYT4p4tMQ2r39u6XKSkBwAlovNl2k 9EdGobjQRR2EszRAJcl+6iKpDzvOZ3Dy9i8ni2thBA/cAftgcQxno4XXUuWlR/Hvk7Jl+ xQOJggzeqd9c0WvwfjYf/ELt+xY4MzM14UMjg1fEpbyP7RAJOFp546e2vqR6UF0243Q== 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=LQNsRlHJ; arc=none smtp.remote-ip=95.215.58.140 Received: from mta1.migadu.com (out-140.mta1.migadu.com [95.215.58.140]) (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 58347606A329 for ; Mon, 30 Nov 2026 17:35:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 58347606A329 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=LQNsRlHJ X-Envelope-To: cgroups@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=VLuinDXpBBj99zDJp/SXXhm65yECQlMeElq1t5OeGb0=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796060103; v=1; x=1796664903; b=LQNsRlHJxCDjWbblcnSeK9zhC/ZyU7fjdnLS/wm/6nVNxz2L+G6RGStNlxQXdaB68V3EFdDa 4MM9G1OMSXkEfkfw7ubPJiuJ0BMwQvayYDYVlhvW8vvfxKqg+Vt/OLaG/jt/gLm9uv2yj1aLSOQ 7VlL9g/MMIccUG4rQPnpFvIo= X-Envelope-To: cgroups@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id fdf6257cfc0e6658; Mon, 30 Nov 2026 17:35:02 +0000 X-Mizu-Trace-ID: fdf6257cfc0e6658 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, stable@vger.kernel.org Subject: [PATCH v2 02/11] cgroup: kill a child forked while cgroup.kill runs Date: Mon, 30 Nov 2026 09:34:16 -0800 Message-ID: <20261130173425.4117363-3-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-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-Transfer-Encoding: 8bit A process forked while cgroup.kill is walking its parent's cgroup can survive the kill: kill fork ---- ---- kill_seq++ cgroup_can_fork() reads kill_seq iterator returns the parent and moves on copy_process() sees no SIGKILL yet and commits to the fork send SIGKILL to the parent cgroup_post_fork() links the child behind the iterator; kill_seq is unchanged, so the child lives The fork read kill_seq after the kill started, so the check from commit b69bb476dee9 ("cgroup: fix race between fork and cgroup.kill") misses it. The window is the gap between the iterator step and send_sig(), and preemption can stretch it. Make kill_seq odd while __cgroup_kill() walks the tasks. Kill the child if kill_seq was odd when the fork read it, or if it changed since. A walk that ended before the read has already sent SIGKILL to the parent, so the fork fails. A walk that starts after the child is linked finds the child. CLONE_INTO_CGROUP forks hold cgroup_mutex across both reads, so they always see an even, unchanged kill_seq. Reproduced in a VM with a sleep between the iterator and send_sig(): the child survived in 5 of 5 runs, and in 0 of 5 with this patch. Fixes: b69bb476dee9 ("cgroup: fix race between fork and cgroup.kill") Cc: stable@vger.kernel.org # v5.14+ Signed-off-by: Shakeel Butt --- include/linux/cgroup-defs.h | 5 +++-- kernel/cgroup/cgroup.c | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h index cbfe8937647c..0dae54b033e9 100644 --- a/include/linux/cgroup-defs.h +++ b/include/linux/cgroup-defs.h @@ -528,8 +528,9 @@ struct cgroup { int nr_threaded_children; /* # of live threaded child cgroups */ /* - * Sequence number for cgroup.kill. Incremented with both cgroup_mutex - * and css_set_lock held. Readers hold either one. + * 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. */ unsigned int kill_seq; diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index a5a7395dc5b8..0fcd2c1b0da9 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -4329,6 +4329,14 @@ static void __cgroup_kill(struct cgroup *cgrp) 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. + */ spin_lock_irq(&css_set_lock); cgrp->kill_seq++; spin_unlock_irq(&css_set_lock); @@ -4346,6 +4354,10 @@ static void __cgroup_kill(struct cgroup *cgrp) send_sig(SIGKILL, task, 0); } css_task_iter_end(&it); + + spin_lock_irq(&css_set_lock); + cgrp->kill_seq++; + spin_unlock_irq(&css_set_lock); } static void cgroup_kill(struct cgroup *cgrp) @@ -7208,7 +7220,8 @@ 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; + kill = kargs->kill_seq != cgrp_kill_seq || + (kargs->kill_seq & 1); } spin_unlock_irq(&css_set_lock); -- 2.53.1-Meta