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 0390660A3807 for ; Fri, 4 Dec 2026 18:41:10 +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=DtUb5M1/ Received: by kanga.kvack.org (Postfix) id 778486B05B5; Fri, 4 Dec 2026 13:41:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7288B6B05B6; Fri, 4 Dec 2026 13:41:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 665B76B05B7; Fri, 4 Dec 2026 13:41:06 -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 259586B05B5 for ; Fri, 4 Dec 2026 13:41:05 -0500 (EST) Received: from smtpin11.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2E0161A0360 for ; Fri, 4 Dec 2026 18:41:05 +0000 (UTC) X-FDA: 85507167690.11.3863B15 Received: from mta0.migadu.com (out-15.mta0.migadu.com [91.218.175.15]) by imf02.hostedemail.com (Postfix) with ESMTP id 340C280011 for ; Fri, 4 Dec 2026 18:41:02 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="DtUb5M1/"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf02.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.15 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=1796409663; b=3urrMlgw+kQZOp9YUXJF3P9A91LnmbpzKPDsGiwexqWL1CFpkgyQi8pyOnY9AsWSX057RD XCLqAG9WYxuJpkV4uhssi7phJraCBkdrE6ZQKv3nHdfATlIO1tL7K2VVuT2Q8nC1jfBU8R U8aHJqM+ha8ndfnl7TDriBgdT5WFaAA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796409663; 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=IjHamXUboZwDKftzxIS2pZp9J/noWX2TVerpuxqN8X4=; b=aIrkzi6lnUo5p1nnYS75AMD2GYL0VLcfqVdLx6bBh6I/smiXi9d/cSGET23yg4nqePljrV /5HqAEIs2JP3Ov03rnX5M5XP+MxPq4HBK1AVgPJikSR+1/2Y3tJC10Ekr48ClZzs56GdwB nt4nJgRjZic70yZIk+WG4nvMwOQEab4= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="DtUb5M1/"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf02.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.15 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev X-Envelope-To: linux-mm@kvack.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=1796409661; v=1; x=1797014461; b=DtUb5M1/Qabm4HN1Us8BpjaQQslcUZwUpYrvZeEKfPyKSjb61QwRyoirz6lQsG13wc4bSlq0 aDdA4ChDwob1qX8ZikJb6dn46x/KO/SN6yPS/zaLU17vDGfcP3tOq6Y7McK92mZJD0mCEYfyd+X sfp/HKzJltxxkBkYQD8FCDBo= X-Envelope-To: linux-mm@kvack.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-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam02 X-Rspam-User: X-Spam-Status: No, score=-19.0 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-Stat-Signature: x114cc8933f3pqzyrj5bdjeztiqiptdg X-Rspamd-Queue-Id: 340C280011 X-HE-Tag: 1796409662-467492 X-HE-Meta: U2FsdGVkX1/mGpQuxYw6ofn5sV1Udgq+2S1TFqlEyjsAbkUckr9nfCyI2PANOmzthxa16p1630kj9aOu7hzQ8vsbVxqjf6m0P2CUyrvGyTXLj+Gmp96677xMyFK/Wjq6KpBlqGMIbelnw6Sj8oKH6zDKv2AV2GS4WkxwujhaBIkeLeuw7MT91jBzmuXbEfa36+PyezwAU0dILazMpPgpvhgrHC3oWDRWUxhT16RRlk/474LplSK0Vz5E5JBQQwzoC/0BrMfaMTUfZXROlIKy2W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VWbuE5Znt/I4F9RhQ5r91a+gmpyb8u3NeG+JhMrymfXf1ZZWsEZ6yFyVquGnGNV0AYn/Cee7QO4+AiFNVdNJlHJWzSJ7G2cTEb8ulvDKciBYZ2r8QQ== 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 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