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
Subject: [PATCH 9/9] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze
Date: Sun, 29 Nov 2026 10:35:24 -0800 [thread overview]
Message-ID: <20261129183524.1052436-10-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261129183524.1052436-1-shakeel.butt@linux.dev>
cgroup.freeze holds cgroup_mutex while it walks the subtree and its tasks.
cgroup_mutex kept freeze writers apart, gave new cgroups a stable parent
state, and blocked migrations. Replace it:
- Serialize cgroup.freeze writers with a new cgroup_freeze_mutex. Nothing
else takes it.
- Protect freeze and e_freeze with css_set_lock. cgroup_create() copies
the parent's e_freeze and links the new cgroup in one css_set_lock
section, so the walk either sees the new cgroup or the new cgroup sees
the new state. cgroup_freeze_show() reads freeze without a lock, so
mark its accesses.
- Check that the cgroup is alive in the section that changes freeze.
rmdir clears CSS_ONLINE under css_set_lock, so a write either takes
effect before the removal or fails with ENODEV, as before.
- Set each task to the state of the cgroup it is in at that moment, under
css_set_lock, instead of the walk's direction. A task can move after
the walk picked it.
- A task can also move before the walk reaches it, with
JOBCTL_TRAP_FREEZE still set from an earlier freeze.
cgroup_freezer_migrate_task() returned early for it, and the task froze
in a cgroup that isn't freezing. Don't return early while the bit is
set. All writers of the bit now hold css_set_lock.
- Read the clock for each cgroup when its state changes, under
css_set_lock. With one timestamp per walk, a cgroup created during an
unfreeze walk had its frozen time wrap around to a huge value.
- A walk can now notify a new cgroup before its files are populated.
Publish cfile->kn after its timer and lock are set up.
- Walk the subtree under RCU, pin each cgroup while its tasks are walked,
and skip removed cgroups.
Tested in a VM, with sleeps added by a debug patch that is not part of
this series: cgroups created during a freeze come out frozen, tasks that
move during a freeze or thaw end up in their new cgroup's state, and
frozen_usec stays sane. The test_freezer selftest passes with lockdep on.
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
include/linux/cgroup-defs.h | 7 ++
include/linux/cgroup.h | 2 +-
kernel/cgroup/cgroup.c | 64 +++++++++++-------
kernel/cgroup/freezer.c | 128 ++++++++++++++++++++++++++----------
4 files changed, 142 insertions(+), 59 deletions(-)
diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h
index e06c5384be1d..0e109f017ea2 100644
--- a/include/linux/cgroup-defs.h
+++ b/include/linux/cgroup-defs.h
@@ -455,6 +455,13 @@ struct cgroup_rstat_base_cpu {
};
struct cgroup_freezer_state {
+ /*
+ * freeze and e_freeze are protected by css_set_lock. cgroup_freeze()
+ * also holds cgroup_freeze_mutex when it changes them. cgroup_create()
+ * sets e_freeze of a new cgroup in the css_set_lock section that links
+ * it to its parent.
+ */
+
/* Should the cgroup and its descendants be frozen. */
bool freeze;
diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index 1fea62e5028c..9951b19a2e9b 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -931,7 +931,7 @@ static inline void cgroup_sk_free(struct sock_cgroup_data *skcd) {}
void cgroup_enter_frozen(void);
void cgroup_leave_frozen(bool always_leave);
void cgroup_update_frozen(struct cgroup *cgrp);
-void cgroup_freeze(struct cgroup *cgrp, bool freeze);
+int cgroup_freeze(struct cgroup *cgrp, bool freeze);
void cgroup_freezer_migrate_task(struct task_struct *task, struct cgroup *src,
struct cgroup *dst);
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index d3407064b07b..28fd9b642f5a 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -4343,7 +4343,7 @@ static int cgroup_freeze_show(struct seq_file *seq, void *v)
{
struct cgroup *cgrp = seq_css(seq)->cgroup;
- seq_printf(seq, "%d\n", cgrp->freezer.freeze);
+ seq_printf(seq, "%d\n", READ_ONCE(cgrp->freezer.freeze));
return 0;
}
@@ -4362,15 +4362,20 @@ static ssize_t cgroup_freeze_write(struct kernfs_open_file *of,
if (freeze < 0 || freeze > 1)
return -ERANGE;
- cgrp = cgroup_kn_lock_live(of->kn, false);
+ /*
+ * No cgroup_mutex: cgroup_freeze() has its own writer mutex. Still
+ * drop the active reference, so that removing the cgroup doesn't wait
+ * for a long walk or for another writer.
+ */
+ cgrp = cgroup_kn_break_active(of->kn);
if (IS_ERR(cgrp))
return PTR_ERR(cgrp);
- cgroup_freeze(cgrp, freeze);
+ ret = cgroup_freeze(cgrp, freeze);
- cgroup_kn_unlock(of->kn);
+ cgroup_kn_unbreak_active(of->kn, cgrp);
- return nbytes;
+ return ret ?: nbytes;
}
static void __cgroup_kill(struct cgroup *cgrp)
@@ -4692,7 +4697,12 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0);
spin_lock_init(&cfile->lock);
- cfile->kn = kn;
+ /*
+ * A cgroup.freeze walk can notify a new cgroup before its
+ * files are populated, see cgroup_create(). Publish the
+ * initialized timer and lock with the node.
+ */
+ smp_store_release(&cfile->kn, kn);
}
return 0;
@@ -4950,7 +4960,8 @@ void cgroup_file_notify(struct cgroup_file *cfile)
unsigned long flags, last, next;
struct kernfs_node *kn = NULL;
- if (!READ_ONCE(cfile->kn))
+ /* pairs with smp_store_release() in cgroup_add_file() */
+ if (!smp_load_acquire(&cfile->kn))
return;
if (timer_pending(&cfile->notify_timer))
@@ -6198,23 +6209,7 @@ static struct cgroup *cgroup_create(struct cgroup *parent, const char *name,
for_each_subsys(ss, i)
RCU_INIT_POINTER(cgrp->e_css[i], cgroup_e_css(parent, ss));
- /*
- * New cgroup inherits effective freeze counter, and
- * if the parent has to be frozen, the child has too.
- */
- cgrp->freezer.e_freeze = parent->freezer.e_freeze;
seqcount_spinlock_init(&cgrp->freezer.freeze_seq, &css_set_lock);
- if (cgrp->freezer.e_freeze) {
- /*
- * Set the CGRP_FREEZE flag, so when a process will be
- * attached to the child cgroup, it will become frozen.
- * At this point the new cgroup is unpopulated, so we can
- * consider it frozen immediately.
- */
- set_bit(CGRP_FREEZE, &cgrp->flags);
- cgrp->freezer.freeze_start_nsec = ktime_get_ns();
- set_bit(CGRP_FROZEN, &cgrp->flags);
- }
if (notify_on_release(parent))
set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
@@ -6233,6 +6228,27 @@ static struct cgroup *cgroup_create(struct cgroup *parent, const char *name,
/* allocation complete, commit to creation */
spin_lock_irq(&css_set_lock);
+
+ /*
+ * New cgroup inherits effective freeze counter, and if the parent has
+ * to be frozen, the child has too. cgroup_freeze() doesn't hold
+ * cgroup_mutex: inherit and become visible to its descendant walk in
+ * the same css_set_lock section, so that the walk either sees the new
+ * cgroup or the new cgroup sees the parent's new state.
+ */
+ cgrp->freezer.e_freeze = parent->freezer.e_freeze;
+ if (cgrp->freezer.e_freeze) {
+ /*
+ * Set the CGRP_FREEZE flag, so when a process will be
+ * attached to the child cgroup, it will become frozen.
+ * At this point the new cgroup is unpopulated, so we can
+ * consider it frozen immediately.
+ */
+ set_bit(CGRP_FREEZE, &cgrp->flags);
+ cgrp->freezer.freeze_start_nsec = ktime_get_ns();
+ set_bit(CGRP_FROZEN, &cgrp->flags);
+ }
+
for (i = 0; i < level; i++) {
tcgrp = cgrp->ancestors[i];
tcgrp->nr_descendants++;
@@ -6245,9 +6261,9 @@ static struct cgroup *cgroup_create(struct cgroup *parent, const char *name,
if (cgrp->freezer.e_freeze)
tcgrp->freezer.nr_frozen_descendants++;
}
- spin_unlock_irq(&css_set_lock);
list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
+ spin_unlock_irq(&css_set_lock);
atomic_inc(&root->nr_cgrps);
cgroup_get_live(parent);
diff --git a/kernel/cgroup/freezer.c b/kernel/cgroup/freezer.c
index b905df5e416b..a5e3920f4ad2 100644
--- a/kernel/cgroup/freezer.c
+++ b/kernel/cgroup/freezer.c
@@ -167,27 +167,37 @@ static void cgroup_freeze_task(struct task_struct *task, bool freeze)
}
/*
- * Freeze or unfreeze all tasks in the given cgroup.
+ * Set or clear CGRP_FREEZE on a cgroup whose effective freeze state changed.
+ * Read the clock here: a cgroup created during an unfreeze walk takes its
+ * freeze_start_nsec from the time it was created.
*/
-static void cgroup_do_freeze(struct cgroup *cgrp, bool freeze, u64 ts_nsec)
+static void cgroup_set_freeze(struct cgroup *cgrp, bool freeze)
{
- struct css_task_iter it;
- struct task_struct *task;
+ u64 now = ktime_get_ns();
- lockdep_assert_held(&cgroup_mutex);
+ lockdep_assert_held(&css_set_lock);
- spin_lock_irq(&css_set_lock);
write_seqcount_begin(&cgrp->freezer.freeze_seq);
if (freeze) {
set_bit(CGRP_FREEZE, &cgrp->flags);
- cgrp->freezer.freeze_start_nsec = ts_nsec;
+ cgrp->freezer.freeze_start_nsec = now;
} else {
clear_bit(CGRP_FREEZE, &cgrp->flags);
- cgrp->freezer.frozen_nsec += (ts_nsec -
- cgrp->freezer.freeze_start_nsec);
+ cgrp->freezer.frozen_nsec +=
+ now - cgrp->freezer.freeze_start_nsec;
}
write_seqcount_end(&cgrp->freezer.freeze_seq);
- spin_unlock_irq(&css_set_lock);
+}
+
+/*
+ * Freeze or unfreeze all tasks in the given cgroup after its CGRP_FREEZE
+ * changed.
+ */
+static void cgroup_do_freeze(struct cgroup *cgrp, bool freeze)
+{
+ struct css_task_iter it;
+ struct task_struct *task;
+ struct cgroup *tcgrp;
if (freeze)
TRACE_CGROUP_PATH(freeze, cgrp);
@@ -202,7 +212,17 @@ static void cgroup_do_freeze(struct cgroup *cgrp, bool freeze, u64 ts_nsec)
*/
if (task->flags & PF_KTHREAD)
continue;
- cgroup_freeze_task(task, freeze);
+
+ /*
+ * Without cgroup_mutex, @task can have migrated since the
+ * iterator returned it, and cgroup_freezer_migrate_task() has
+ * already set it to its new cgroup's state. Set it to the
+ * state of the cgroup it is in now.
+ */
+ spin_lock_irq(&css_set_lock);
+ tcgrp = task_dfl_cgroup(task);
+ cgroup_freeze_task(task, test_bit(CGRP_FREEZE, &tcgrp->flags));
+ spin_unlock_irq(&css_set_lock);
}
css_task_iter_end(&it);
@@ -232,11 +252,18 @@ void cgroup_freezer_migrate_task(struct task_struct *task,
/*
* It's not necessary to do changes if both of the src and dst cgroups
- * are not freezing and task is not frozen.
+ * are not freezing and task is not frozen, unless the task still has
+ * the freeze trap set: an unfreeze clears CGRP_FREEZE before it gets
+ * to the tasks.
*/
if (!test_bit(CGRP_FREEZE, &src->flags) &&
!test_bit(CGRP_FREEZE, &dst->flags) &&
- !task->frozen)
+ !task->frozen &&
+ /*
+ * The trap bit is set and cleared under css_set_lock, but other
+ * jobctl bits change under siglock alone.
+ */
+ !data_race(task->jobctl & JOBCTL_TRAP_FREEZE))
return;
/*
@@ -257,55 +284,85 @@ void cgroup_freezer_migrate_task(struct task_struct *task,
cgroup_freeze_task(task, test_bit(CGRP_FREEZE, &dst->flags));
}
-void cgroup_freeze(struct cgroup *cgrp, bool freeze)
+/*
+ * Serializes cgroup.freeze writers. The freeze state is protected by
+ * css_set_lock, which cgroup_create() uses to inherit it, so cgroup_mutex is
+ * not needed.
+ */
+static DEFINE_MUTEX(cgroup_freeze_mutex);
+
+int cgroup_freeze(struct cgroup *cgrp, bool freeze)
{
struct cgroup_subsys_state *css;
- struct cgroup *parent;
- struct cgroup *dsct;
bool applied = false;
- u64 ts_nsec;
- bool old_e;
+ int ret = 0;
- lockdep_assert_held(&cgroup_mutex);
+ mutex_lock(&cgroup_freeze_mutex);
/*
- * Nothing changed? Just exit.
+ * rmdir clears CSS_ONLINE under css_set_lock, so the write takes effect
+ * before the cgroup is removed or fails. Nothing changed? Just exit.
*/
- if (cgrp->freezer.freeze == freeze)
- return;
-
- cgrp->freezer.freeze = freeze;
- ts_nsec = ktime_get_ns();
+ spin_lock_irq(&css_set_lock);
+ if (cgroup_is_dead(cgrp))
+ ret = -ENODEV;
+ if (ret || cgrp->freezer.freeze == freeze) {
+ spin_unlock_irq(&css_set_lock);
+ goto out_unlock;
+ }
+ WRITE_ONCE(cgrp->freezer.freeze, freeze);
+ spin_unlock_irq(&css_set_lock);
/*
- * Propagate changes downwards the cgroup tree.
+ * Propagate changes downwards the cgroup tree. New cgroups inherit
+ * e_freeze from their parent under css_set_lock and become visible to
+ * this walk in the same critical section, see cgroup_create().
+ * Removed cgroups are skipped. A cgroup removed during the walk only
+ * has exiting tasks, and cgroup_update_frozen() leaves it alone.
*/
+ rcu_read_lock();
css_for_each_descendant_pre(css, &cgrp->self) {
- dsct = css->cgroup;
+ struct cgroup *dsct = css->cgroup;
+ bool e_freeze;
- if (cgroup_is_dead(dsct))
+ spin_lock_irq(&css_set_lock);
+ if (cgroup_is_dead(dsct)) {
+ spin_unlock_irq(&css_set_lock);
+ css = css_rightmost_descendant(css);
continue;
+ }
/*
* e_freeze is affected by parent's e_freeze and dst's freeze.
* If old e_freeze eq new e_freeze, no change, its children
* will not be affected. So do nothing and skip the subtree
*/
- old_e = dsct->freezer.e_freeze;
- parent = cgroup_parent(dsct);
- dsct->freezer.e_freeze = (dsct->freezer.freeze ||
- parent->freezer.e_freeze);
- if (dsct->freezer.e_freeze == old_e) {
+ e_freeze = dsct->freezer.freeze ||
+ cgroup_parent(dsct)->freezer.e_freeze;
+ if (dsct->freezer.e_freeze == e_freeze) {
+ spin_unlock_irq(&css_set_lock);
css = css_rightmost_descendant(css);
continue;
}
+ dsct->freezer.e_freeze = e_freeze;
+ cgroup_set_freeze(dsct, e_freeze);
+ spin_unlock_irq(&css_set_lock);
+
+ if (!css_tryget_online(css))
+ continue;
+ rcu_read_unlock();
/*
* Do change actual state: freeze or unfreeze.
*/
- cgroup_do_freeze(dsct, freeze, ts_nsec);
+ cgroup_do_freeze(dsct, e_freeze);
applied = true;
+ cond_resched();
+
+ rcu_read_lock();
+ css_put(css);
}
+ rcu_read_unlock();
/*
* Even if the actual state hasn't changed, let's notify a user.
@@ -320,4 +377,7 @@ void cgroup_freeze(struct cgroup *cgrp, bool freeze)
test_bit(CGRP_FROZEN, &cgrp->flags));
cgroup_file_notify(&cgrp->events_file);
}
+out_unlock:
+ mutex_unlock(&cgroup_freeze_mutex);
+ return ret;
}
--
2.53.1-Meta
prev parent reply other threads:[~2026-11-29 18:36 UTC|newest]
Thread overview: 10+ 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 ` [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-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-29 18:35 ` Shakeel Butt [this message]
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-10-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=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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®