* [PATCH 1/9] cgroup: kill a child forked while cgroup.kill runs
[not found] <20261129183524.1052436-1-shakeel.butt@linux.dev>
@ 2026-11-29 18:35 ` Shakeel Butt
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
2 siblings, 0 replies; 3+ messages in thread
From: Shakeel Butt @ 2026-11-29 18:35 UTC (permalink / raw)
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, linux-mm, linux-kernel,
stable
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
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH 2/9] cgroup/freezer: check own tasks and descendants for the frozen state
[not found] <20261129183524.1052436-1-shakeel.butt@linux.dev>
2026-11-29 18:35 ` [PATCH 1/9] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
@ 2026-11-29 18:35 ` Shakeel Butt
2026-11-29 18:35 ` [PATCH 3/9] cgroup/freezer: don't update the frozen state of removed cgroups Shakeel Butt
2 siblings, 0 replies; 3+ messages in thread
From: Shakeel Butt @ 2026-11-29 18:35 UTC (permalink / raw)
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, linux-mm, linux-kernel,
stable
A cgroup with its own tasks and child cgroups can report frozen=1 while
part of its subtree still runs. cgroup2 allows this layout in threaded
cgroups, and in domain cgroups that don't enable a domain controller for
their children.
cgroup_update_frozen() only looks at the cgroup's own tasks, and
cgroup_propagate_frozen() only looks at its descendants. So a parent can
become frozen when only one of the two is frozen. The wrong state can also
stay after a thaw: cgroup_do_freeze() skips the update while a descendant
is unfrozen, and a stopped task is not woken to fix it.
Use one test everywhere: CGRP_FREEZE is set, all own tasks are frozen and
all descendants are frozen. Merge cgroup_propagate_frozen() into
cgroup_update_frozen() and always call it from cgroup_do_freeze().
Reproduced in a VM with a task stuck in D state, writing to an fsfreeze'd
filesystem. With the stuck task in the child or in the parent, the parent
reported frozen=1. With a stopped task moved into the parent, the parent
still reported frozen=1 after the thaw. With this patch it reports
frozen=0 in all three cases.
Fixes: 76f969e8948d ("cgroup: cgroup v2 freezer")
Cc: stable@vger.kernel.org # v5.2+
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
kernel/cgroup/freezer.c | 63 ++++++++++++++++-------------------------
1 file changed, 24 insertions(+), 39 deletions(-)
diff --git a/kernel/cgroup/freezer.c b/kernel/cgroup/freezer.c
index 35112d62318a..6ecb42084ecc 100644
--- a/kernel/cgroup/freezer.c
+++ b/kernel/cgroup/freezer.c
@@ -28,53 +28,39 @@ static bool cgroup_update_frozen_flag(struct cgroup *cgrp, bool frozen)
}
/*
- * Propagate the cgroup frozen state upwards by the cgroup tree.
+ * A cgroup is frozen when it has to be frozen (CGRP_FREEZE bit set), all of
+ * its own tasks are frozen and/or stopped, and all of its descendants are
+ * frozen. A non-leaf cgroup can have tasks of its own: threaded cgroups always
+ * can, and domain cgroups can while no domain controller is enabled in their
+ * subtree_control.
*/
-static void cgroup_propagate_frozen(struct cgroup *cgrp, bool frozen)
+static bool cgroup_frozen_state(struct cgroup *cgrp)
{
- int desc = 1;
-
- /*
- * If the new state is frozen, some freezing ancestor cgroups may change
- * their state too, depending on if all their descendants are frozen.
- *
- * Otherwise, all ancestor cgroups are forced into the non-frozen state.
- */
- while ((cgrp = cgroup_parent(cgrp))) {
- if (frozen) {
- cgrp->freezer.nr_frozen_descendants += desc;
- if (!test_bit(CGRP_FREEZE, &cgrp->flags) ||
- (cgrp->freezer.nr_frozen_descendants !=
- cgrp->nr_descendants))
- continue;
- } else {
- cgrp->freezer.nr_frozen_descendants -= desc;
- }
-
- if (cgroup_update_frozen_flag(cgrp, frozen))
- desc++;
- }
+ /* __cgroup_task_count() walks the css_sets, test it last */
+ return test_bit(CGRP_FREEZE, &cgrp->flags) &&
+ cgrp->freezer.nr_frozen_descendants == cgrp->nr_descendants &&
+ cgrp->freezer.nr_frozen_tasks == __cgroup_task_count(cgrp);
}
/*
- * Revisit the cgroup frozen state.
- * Checks if the cgroup is really frozen and perform all state transitions.
+ * Revisit the frozen state of @cgrp and propagate a change to the ancestors.
+ * delta counts the cgroups below the one being looked at whose frozen state
+ * changed in this update, negative if they were unfrozen.
*/
void cgroup_update_frozen(struct cgroup *cgrp)
{
- bool frozen;
+ int delta = 0;
- /*
- * If the cgroup has to be frozen (CGRP_FREEZE bit set),
- * and all tasks are frozen and/or stopped, let's consider
- * the cgroup frozen. Otherwise it's not frozen.
- */
- frozen = test_bit(CGRP_FREEZE, &cgrp->flags) &&
- cgrp->freezer.nr_frozen_tasks == __cgroup_task_count(cgrp);
+ lockdep_assert_held(&css_set_lock);
+
+ do {
+ bool frozen;
- /* If flags is updated, update the state of ancestor cgroups. */
- if (cgroup_update_frozen_flag(cgrp, frozen))
- cgroup_propagate_frozen(cgrp, frozen);
+ cgrp->freezer.nr_frozen_descendants += delta;
+ frozen = cgroup_frozen_state(cgrp);
+ if (cgroup_update_frozen_flag(cgrp, frozen))
+ delta += frozen ? 1 : -1;
+ } while (delta && (cgrp = cgroup_parent(cgrp)));
}
/*
@@ -210,8 +196,7 @@ static void cgroup_do_freeze(struct cgroup *cgrp, bool freeze, u64 ts_nsec)
* and cgroups which descendants are already in the desired state.
*/
spin_lock_irq(&css_set_lock);
- if (cgrp->nr_descendants == cgrp->freezer.nr_frozen_descendants)
- cgroup_update_frozen(cgrp);
+ cgroup_update_frozen(cgrp);
spin_unlock_irq(&css_set_lock);
}
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH 3/9] cgroup/freezer: don't update the frozen state of removed cgroups
[not found] <20261129183524.1052436-1-shakeel.butt@linux.dev>
2026-11-29 18:35 ` [PATCH 1/9] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
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 ` Shakeel Butt
2 siblings, 0 replies; 3+ messages in thread
From: Shakeel Butt @ 2026-11-29 18:35 UTC (permalink / raw)
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, linux-mm, linux-kernel,
stable
Since commit 93618edf7538 ("cgroup: Defer css percpu_ref kill on rmdir
until cgroup is depopulated"), rmdir can remove a cgroup while an exiting
task is still linked to it. The task is unlinked later, in
cgroup_task_dead(). This breaks the freezer counts:
- rmdir takes the cgroup out of the ancestors' nr_frozen_descendants only
if it is frozen, and a cgroup with an exiting task is not. When the task
is unlinked, cgroup_update_frozen() marks the dead cgroup frozen and
counts it again in every ancestor. The count stays one too high, so a
freezing ancestor reports frozen=1 too early or never.
- If the removed cgroup was the parent's last unfrozen descendant, the
parent is not checked again and stays at frozen=0.
Clear CSS_ONLINE, fix the ancestors' counts and check the parent again in
one css_set_lock section, and make cgroup_update_frozen() skip dead
cgroups. cgroup_is_dead() can now run with only css_set_lock held, while
other bits of the same flags word change under cgroup_mutex, so use
READ_ONCE() and WRITE_ONCE() for CSS_ONLINE. The parent's check relies on
the previous patch.
Reproduced in a VM: freeze G/P, kill the only task in G/P/C while its exit
is held on an fsfreeze'd filesystem, remove G/P/C, and let the task finish.
G/P, empty and freezing, reports frozen=0, and a later freeze of G never
completes. This patch fixes both.
Fixes: 93618edf7538 ("cgroup: Defer css percpu_ref kill on rmdir until cgroup is depopulated")
Cc: stable@vger.kernel.org # v7.0+
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
include/linux/cgroup.h | 2 +-
kernel/cgroup/cgroup.c | 33 +++++++++++++++++++--------------
kernel/cgroup/freezer.c | 8 ++++++++
3 files changed, 28 insertions(+), 15 deletions(-)
diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index 2afb4cb2bb4f..1fea62e5028c 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -462,7 +462,7 @@ static inline bool css_is_self(struct cgroup_subsys_state *css)
static inline bool cgroup_is_dead(const struct cgroup *cgrp)
{
- return !(cgrp->self.flags & CSS_ONLINE);
+ return !(READ_ONCE(cgrp->self.flags) & CSS_ONLINE);
}
static inline void cgroup_get(struct cgroup *cgrp)
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 3d56f7417fb6..73da6bfb9ebf 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -6397,25 +6397,17 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
* further task migration and child creation by disabling
* cgroup_kn_lock_live(). The latter makes the csets ignored by
* the migration path.
+ *
+ * Exiting tasks can stay linked to @cgrp until cgroup_task_dead().
+ * Take @cgrp out of its ancestors' descendant counts in the same
+ * css_set_lock section, so that cgroup_update_frozen() for those
+ * tasks sees @cgrp dead and leaves the counts alone.
*/
- cgrp->self.flags &= ~CSS_ONLINE;
-
spin_lock_irq(&css_set_lock);
+ WRITE_ONCE(cgrp->self.flags, cgrp->self.flags & ~CSS_ONLINE);
list_for_each_entry(link, &cgrp->cset_links, cset_link)
link->cset->dead = true;
- spin_unlock_irq(&css_set_lock);
-
- for_each_css(css, ssid, cgrp)
- kill_css_sync(css);
-
- /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */
- css_clear_dir(&cgrp->self);
- kernfs_remove(cgrp->kn);
-
- if (cgroup_is_threaded(cgrp))
- parent->nr_threaded_children--;
- spin_lock_irq(&css_set_lock);
for (tcgrp = parent; tcgrp; tcgrp = cgroup_parent(tcgrp)) {
tcgrp->nr_descendants--;
tcgrp->nr_dying_descendants++;
@@ -6426,8 +6418,21 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
if (test_bit(CGRP_FROZEN, &cgrp->flags))
tcgrp->freezer.nr_frozen_descendants--;
}
+
+ /* @cgrp may have been the last unfrozen descendant of a freezing parent */
+ cgroup_update_frozen(parent);
spin_unlock_irq(&css_set_lock);
+ for_each_css(css, ssid, cgrp)
+ kill_css_sync(css);
+
+ /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */
+ css_clear_dir(&cgrp->self);
+ kernfs_remove(cgrp->kn);
+
+ if (cgroup_is_threaded(cgrp))
+ parent->nr_threaded_children--;
+
cgroup1_check_for_release(parent);
ret = blocking_notifier_call_chain(&cgroup_lifetime_notifier,
diff --git a/kernel/cgroup/freezer.c b/kernel/cgroup/freezer.c
index 6ecb42084ecc..a85f8897ffa2 100644
--- a/kernel/cgroup/freezer.c
+++ b/kernel/cgroup/freezer.c
@@ -53,6 +53,14 @@ void cgroup_update_frozen(struct cgroup *cgrp)
lockdep_assert_held(&css_set_lock);
+ /*
+ * An exiting task can stay linked to a cgroup after rmdir.
+ * cgroup_destroy_locked() took the cgroup out of its ancestors' counts
+ * when it cleared CSS_ONLINE. Leave its state alone from then on.
+ */
+ if (cgroup_is_dead(cgrp))
+ return;
+
do {
bool frozen;
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 3+ messages in thread