From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.14 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796409664; b=oemtwZizGwnCZ5kO0pHH8P53DUw9lpAjAigfeKTCJfAIMl3aPWghSyBI6NZmQxV6iaJs 6CyyanyTGPam5bHayq90SaJ6+Su/2Ymcj/3NBQYA8dQqKe/8BfqvXzNucrTCN5VEOLSnp IL37bOS0/l90nVAUdNPKJFWO5A1fNkRrUN06+t76CeGqW7+eCaSULYP5mlHwthS6egWcv nAKfH3LNlUTikxZMTdOXd4delfdD3jv+JuwHBiTW5cyuXkiqseGKWk1iGocikt/CLI6PZ PZaH2CxAobdlrOkMRlKjjumRo6RC1SOAArrFDD8phcNK0fw/kBulA+uv/+R6BnPQseQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796409664; 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=xBEhGk7iXgkbgRdsLiL24uCTL5ArBqLiws0RNCI74kYgXir/ZGROVTwsiPiUXLuHINbm cOmQ1cB/kbWVVJfOqzrH1SGBxRoomuFeR56gpomlD0RR3yo5YZZ/00peWscKWrh/fjIG6 3t2YIeFV7ov9NKqwjjp/SRBAdl2hw8sOuczVc6cyQ+vnLa6FcvuewhMA4fkfVXncBPJSo w5dbsEYHDTps5h7C+bkSnHD4Dw7qSzA75p0L/CE/nFTD5/8xlbFnVA6JAL5d9OsxcYUJe L5id+WXzGrjXFaY37C7ZkgGpRnGbs9K5krMxV0wiw9Y1cPN5jGqlsmI99om45cFXiJQ== 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=ILsh7bz9; arc=none smtp.remote-ip=91.218.175.14 Received: from mta0.migadu.com (out-14.mta0.migadu.com [91.218.175.14]) (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 C552A60A260F for ; Fri, 4 Dec 2026 18:41:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C552A60A260F 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=ILsh7bz9 X-Envelope-To: stable@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=1796409662; v=1; x=1797014462; b=ILsh7bz9aUo3kOUU0Qf7bRgRMqORVRTjfMtW0ErxZ1tu0h33w1zOMHnvfeFQYnc3UyXVYe9U o3UfB/QmdoBd+W7cowpjm1Ds+3GH6gzy2s4bWpljxKCciyau9xv31g1wiv4qBuIMVX7UP+xlAZI zmtKUZw1PKDWi1aIwscR1Kf8= X-Envelope-To: stable@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 10d6f9c5e706545c; Fri, 04 Dec 2026 18:41:01 +0000 X-Mizu-Trace-ID: 10d6f9c5e706545c 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 v3 02/12] cgroup: kill a child forked while cgroup.kill runs Date: Fri, 4 Dec 2026 10:40:31 -0800 Message-ID: <20261204184041.1489976-3-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: stable@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