Linux cgroups development
 help / color / mirror / Atom feed
From: Shakeel Butt <shakeel.butt@linux.dev>
To: Tejun Heo <tj@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Michal Koutny <mkoutny@suse.com>,
	Michal Hocko <mhocko@kernel.org>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Christian Brauner <brauner@kernel.org>,
	Oleg Nesterov <oleg@redhat.com>,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	Suren Baghdasaryan <surenb@google.com>,
	"T . J . Mercier" <tjmercier@google.com>,
	Usama Arif <usama.arif@linux.dev>,
	Meta kernel team <kernel-team@meta.com>,
	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	[thread overview]
Message-ID: <20261129183524.1052436-2-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261129183524.1052436-1-shakeel.butt@linux.dev>

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 <shakeel.butt@linux.dev>
---
 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


  reply	other threads:[~2026-11-29 18:35 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 18:35 [PATCH 0/9] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Shakeel Butt
2026-11-29 18:35 ` Shakeel Butt [this message]
2026-11-29 18:35 ` [PATCH 2/9] cgroup/freezer: check own tasks and descendants for the frozen state Shakeel Butt
2026-11-29 18:35 ` [PATCH 3/9] cgroup/freezer: don't update the frozen state of removed cgroups Shakeel Butt
2026-11-29 18:35 ` [PATCH 4/9] cgroup/freezer: test the freeze trap bit under siglock Shakeel Butt
2026-11-29 18:35 ` [PATCH 5/9] selftests/cgroup: test the frozen state of a cgroup with tasks and children Shakeel Butt
2026-11-29 18:35 ` [PATCH 6/9] selftests/cgroup: test cgroup.kill in a threaded cgroup Shakeel Butt
2026-11-29 18:35 ` [PATCH 7/9] cgroup: factor out cgroup_kn_break_active() and cgroup_kn_unbreak_active() Shakeel Butt
2026-11-29 18:35 ` [PATCH 8/9] cgroup: don't hold cgroup_mutex for cgroup.kill Shakeel Butt
2026-11-29 18:35 ` [PATCH 9/9] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze Shakeel Butt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261129183524.1052436-2-shakeel.butt@linux.dev \
    --to=shakeel.butt@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=bigeasy@linutronix.de \
    --cc=brauner@kernel.org \
    --cc=cgroups@vger.kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=mkoutny@suse.com \
    --cc=oleg@redhat.com \
    --cc=roman.gushchin@linux.dev \
    --cc=stable@vger.kernel.org \
    --cc=surenb@google.com \
    --cc=tj@kernel.org \
    --cc=tjmercier@google.com \
    --cc=usama.arif@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox