* [PATCH v2 01/11] cgroup: don't lose a task in css_task_iter when a migration fails
[not found] <20261130173425.4117363-1-shakeel.butt@linux.dev>
@ 2026-11-30 17:34 ` Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 02/11] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
` (2 subsequent siblings)
3 siblings, 0 replies; 4+ messages in thread
From: Shakeel Butt @ 2026-11-30 17:34 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,
Qingye Zhao, stable
A cgroup.procs reader can miss a process that never left the cgroup.
With processes 148, 150 and 151 in src:
read one byte of src/cgroup.procs
echo 151 > empty/cgroup.procs fails with ENOSPC
read the rest "148 150", 151 is missing
Since commit 5ee01f1a7343 ("cgroup: fix race between task migration and
iteration"), cgroup_migrate_add_task() skips an iterator past a task
before it moves the task to mg_tasks. If the task was the last one on
->tasks, the iterator stops at the head of ->tasks and goes to mg_tasks
next. If the migration then fails, out_release_tset splices mg_tasks
back onto the tail of ->tasks. The iterator finds mg_tasks empty and
moves on to the next css_set, so it never returns the task.
Any task walk that doesn't hold cgroup_mutex can miss a task this way.
cgroup.procs reads are the easiest case, because they keep the iterator
between reads. A ->can_attach() callback can fail the migration, for
example cpuset's for a cgroup without CPUs.
Point such an iterator at the first task that is moved back. The
iterator can then return a task twice, which a migration to a css_set
it hasn't walked yet can already cause.
The selftest added later in this series fails without this patch.
Fixes: 5ee01f1a7343 ("cgroup: fix race between task migration and iteration")
Reported-by: Tejun Heo <tj@kernel.org>
Closes: https://lore.kernel.org/r/a6553d7921f3f47b7b890bfd232de644@kernel.org
Cc: Qingye Zhao <zhaoqingye@honor.com>
Cc: stable@vger.kernel.org
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
kernel/cgroup/cgroup.c | 31 ++++++++++++++++++++++++++++++-
1 file changed, 30 insertions(+), 1 deletion(-)
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index f6df19241772..a5a7395dc5b8 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -865,6 +865,35 @@ static void css_set_skip_task_iters(struct css_set *cset,
css_task_iter_skip(it, task);
}
+/**
+ * css_set_splice_mg_tasks - move the tasks on a css_set's mg_tasks to its tasks
+ * @cset: css_set of interest
+ *
+ * The tasks go to the tail of @cset->tasks. An iterator that is done with
+ * @cset->tasks and is about to move on to @cset->mg_tasks would miss them.
+ * This happens when cgroup_migrate_add_task() skipped the iterator past the
+ * last task on @cset->tasks and the migration then failed. Point such an
+ * iterator at the first task that is moved. It may return tasks that it
+ * returned before, as it may when a task migrates to a css_set it hasn't
+ * walked yet.
+ */
+static void css_set_splice_mg_tasks(struct css_set *cset)
+{
+ struct css_task_iter *it;
+
+ lockdep_assert_held(&css_set_lock);
+
+ if (list_empty(&cset->mg_tasks))
+ return;
+
+ /* only a skipped iterator stops at the head, see css_task_iter_skip() */
+ list_for_each_entry(it, &cset->task_iters, iters_node)
+ if (it->task_pos == &cset->tasks)
+ it->task_pos = cset->mg_tasks.next;
+
+ list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
+}
+
/**
* css_set_move_task - move a task from one css_set to another
* @task: task being moved
@@ -2851,7 +2880,7 @@ static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx)
spin_lock_irq(&css_set_lock);
list_splice_init(&tset->dst_csets, &tset->src_csets);
list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
- list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
+ css_set_splice_mg_tasks(cset);
list_del_init(&cset->mg_node);
}
spin_unlock_irq(&css_set_lock);
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 02/11] cgroup: kill a child forked while cgroup.kill runs
[not found] <20261130173425.4117363-1-shakeel.butt@linux.dev>
2026-11-30 17:34 ` [PATCH v2 01/11] cgroup: don't lose a task in css_task_iter when a migration fails Shakeel Butt
@ 2026-11-30 17:34 ` Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 03/11] cgroup/freezer: check own tasks and descendants for the frozen state Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 04/11] cgroup/freezer: don't update the frozen state of removed cgroups Shakeel Butt
3 siblings, 0 replies; 4+ messages in thread
From: Shakeel Butt @ 2026-11-30 17:34 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 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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 03/11] cgroup/freezer: check own tasks and descendants for the frozen state
[not found] <20261130173425.4117363-1-shakeel.butt@linux.dev>
2026-11-30 17:34 ` [PATCH v2 01/11] cgroup: don't lose a task in css_task_iter when a migration fails Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 02/11] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
@ 2026-11-30 17:34 ` Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 04/11] cgroup/freezer: don't update the frozen state of removed cgroups Shakeel Butt
3 siblings, 0 replies; 4+ messages in thread
From: Shakeel Butt @ 2026-11-30 17:34 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] 4+ messages in thread
* [PATCH v2 04/11] cgroup/freezer: don't update the frozen state of removed cgroups
[not found] <20261130173425.4117363-1-shakeel.butt@linux.dev>
` (2 preceding siblings ...)
2026-11-30 17:34 ` [PATCH v2 03/11] cgroup/freezer: check own tasks and descendants for the frozen state Shakeel Butt
@ 2026-11-30 17:34 ` Shakeel Butt
3 siblings, 0 replies; 4+ messages in thread
From: Shakeel Butt @ 2026-11-30 17:34 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 needs the frozen state to cover descendants, so a
backport needs "cgroup/freezer: check own tasks and descendants for the
frozen state" first.
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+, needs "cgroup/freezer: check own tasks and descendants for the frozen state"
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 f3c65991acac..7279613d6804 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 0fcd2c1b0da9..3972cf413797 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -6426,25 +6426,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++;
@@ -6455,8 +6447,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] 4+ messages in thread
end of thread, other threads:[~2026-11-30 17:35 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261130173425.4117363-1-shakeel.butt@linux.dev>
2026-11-30 17:34 ` [PATCH v2 01/11] cgroup: don't lose a task in css_task_iter when a migration fails Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 02/11] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 03/11] cgroup/freezer: check own tasks and descendants for the frozen state Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 04/11] cgroup/freezer: don't update the frozen state of removed cgroups Shakeel Butt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox