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 v2 04/11] cgroup/freezer: don't update the frozen state of removed cgroups
Date: Mon, 30 Nov 2026 09:34:18 -0800 [thread overview]
Message-ID: <20261130173425.4117363-5-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261130173425.4117363-1-shakeel.butt@linux.dev>
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
next prev parent reply other threads:[~2026-11-30 17:35 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 17:34 [PATCH v2 00/11] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Shakeel Butt
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 ` Shakeel Butt [this message]
2026-11-30 17:34 ` [PATCH v2 05/11] cgroup/freezer: test the freeze trap bit under siglock Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 06/11] selftests/cgroup: test cgroup.procs reads across a failed migration Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 07/11] selftests/cgroup: test the frozen state of a cgroup with tasks and children Shakeel Butt
2026-12-01 17:36 ` sashiko-bot
2026-12-01 17:55 ` Andrew Morton
2026-11-30 17:34 ` [PATCH v2 08/11] selftests/cgroup: test cgroup.kill in a threaded cgroup Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 09/11] cgroup: factor out cgroup_kn_break_active() and cgroup_kn_unbreak_active() Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 10/11] cgroup: don't hold cgroup_mutex for cgroup.kill Shakeel Butt
2026-12-01 17:36 ` sashiko-bot
2026-12-01 17:55 ` Andrew Morton
2026-12-01 17:56 ` Andrew Morton
2026-11-30 17:34 ` [PATCH v2 11/11] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze Shakeel Butt
2026-12-01 17:36 ` sashiko-bot
2026-12-01 17:56 ` Andrew Morton
2026-12-01 17:55 ` [PATCH v2 00/11] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Andrew Morton
2026-12-01 18:15 ` 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=20261130173425.4117363-5-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.