From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.155 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795977352; b=nZJlxg+ZldlFMIYqzWUanKK0VP590oVEesfSii8Aj0XvtXbJTF7CQDJyT4vLmcV3FxSD krPPmwfGbr2dNKDwqIVDIrsfF2EbuDbv+2zOXxCYtB2zI1zKBX5dywMoThhnywrIKoFKA PuzAkb5Qi2CQe7y0B0fzIIFNXjjWvdPM+6IBU5yvbaBwsS3TCm8wUZrp/8bQ7KSXQfY/d Dj9KOp98gIn33C2IyJj7FACvJJJb4c/GqY0xWsFszE0NK/Hr5OtIV88sLGKjiLRRpEjbZ rMgNsFCcDuqCuIaAGeB4LEItX7FvMvNtSZ6gTojNakQrIKwJCRlGzOTbmQJ+rtaGHvw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795977352; 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=bbF7HY+5QxQaUlNq8jlWeI2XkA+0bWhktzPDp7tSTYA=; b=sA5BHhcBcWtGcbLKiRd2J+VzBMkkgAkGE1jgrtvIXdTnYz3RIqQPJ+ufSpsKY2etETAA 2bek//eNH/2W18609H6KcEtRdh0VodYdcTqNEed8uwtLcmc8xCnlmVtzEAPo9DUkyMXGR Y/V3xCJk1EDC5KoCHNoK3iZGDjgEB2NCoUuDWQ0KZs8+xyu8YqPCJ2GRk3Gz43JHykSNO FyCkObyTJeru4XmTlL2M63A1vRnY+UfbsprDAxT+82Elfbd6+wjWbZECA9+anBgW7ri29 zBUlnzgeXjNrF2ftkB7sGGge9pRdBzPUcytz6nZ0fl1phUdhldLJU/cL7PeIYx9c/8w== 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=jdW9ZBjX; arc=none smtp.remote-ip=95.215.58.155 Received: from mta1.migadu.com (out-155.mta1.migadu.com [95.215.58.155]) (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 73121606A332 for ; Sun, 29 Nov 2026 18:35:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 73121606A332 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=jdW9ZBjX X-Envelope-To: stable@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=KBMgTb7cHo8yNzJi3/fSAcjxM71Q9XxisgVnBSUiWgw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795977349; v=1; x=1796582149; b=jdW9ZBjXWbaIwau9T1EqgVdWsBSTsjaLiTAvhsBo3grRVhbzk3IP3gBxR2ty6u6TPOmj8gHv IWWT4CrIrMRfMmRSccmputpWipJj6eencq5VcJj8x0fkb2eroQLUh7soWi8FH4XX7Q4R61yrUC3 R+ymd9SK6Hvc2pdRPu9wtMBs= X-Envelope-To: stable@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 4cc90064aed20ee1; Sun, 29 Nov 2026 18:35:49 +0000 X-Mizu-Trace-ID: 4cc90064aed20ee1 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 1/9] cgroup: kill a child forked while cgroup.kill runs Date: Sun, 29 Nov 2026 10:35:16 -0800 Message-ID: <20261129183524.1052436-2-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261129183524.1052436-1-shakeel.butt@linux.dev> References: <20261129183524.1052436-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 f6df19241772..3d56f7417fb6 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -4300,6 +4300,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); @@ -4317,6 +4325,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) @@ -7179,7 +7191,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