All of lore.kernel.org
 help / color / mirror / Atom feed
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 3/9] cgroup/freezer: don't update the frozen state of removed cgroups
Date: Sun, 29 Nov 2026 10:35:18 -0800	[thread overview]
Message-ID: <20261129183524.1052436-4-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261129183524.1052436-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 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


  parent reply	other threads:[~2026-11-29 18:35 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 ` [PATCH 2/9] cgroup/freezer: check own tasks and descendants for the frozen state Shakeel Butt
2026-11-29 18:35 ` Shakeel Butt [this message]
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-4-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.