[PATCH 2/9] cgroup/freezer: check own tasks and descendants for the frozen state

From: Shakeel Butt

Date: Sun Nov 29 2026 - 13:36:00 EST


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@xxxxxxxxxxxxxxx # v5.2+
Signed-off-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>
---
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