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 2/9] cgroup/freezer: check own tasks and descendants for the frozen state
Date: Sun, 29 Nov 2026 10:35:17 -0800 [thread overview]
Message-ID: <20261129183524.1052436-3-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261129183524.1052436-1-shakeel.butt@linux.dev>
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
next prev parent reply other threads:[~2026-11-29 18:36 UTC|newest]
Thread overview: 15+ 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 ` [PATCH 1/9] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
2026-11-29 18:35 ` Shakeel Butt [this message]
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-30 18:36 ` sashiko-bot
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-30 18:36 ` sashiko-bot
2026-11-30 21:21 ` Shakeel Butt
2026-11-29 18:35 ` [PATCH 9/9] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze Shakeel Butt
2026-11-30 4:25 ` [PATCH 0/9] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Tejun Heo
2026-11-30 17:04 ` 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-3-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.