From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.231 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795977370; b=dn4L21/8Bth9gK1w9tdpmPIubqjkUhacOp6mt0dbC2o0FkngcCo9D+Ahwe1iUK1myO/Y 6h/skFqyRS/Wa59y7wAtOIDUlKMqxIWdeWTzVEfK8W61RMX8N1KUZcVRgk3iAdDlSJj3m 3ijlA3OUT35Hz1iWJ4SI911nA2TqIS3vzeu0hdTyCmeJjhMDTKMweXkx81kRPBug+8KIO jiJXaAZnGF+klb6LBtz/2mjARrXMSleGg1lu3k5amkqKL5ztJmI+1joLmuP631vCveptB ydnt1MCj8CSvwCyLnvnp0RrIx5zdm4nBMfOaXvToCO4azBVUIPXc5FG0rXbe3ldPbVQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795977370; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=GuULwu0bamGUo6VMjDfTFg4tlgQ4KatVdos6psQzCZU=; b=b7SrcK02IxVpIKMcxPNS5EYB8yGP/o8O/MOO8RObYSpzgFsEqG8BRVA9VWT2RPRySOr1 GcAoGzqMjxEFwCMyaPn9gjQyrCFtKBhbl7w/JIDLJjaNHdJkmB5Ma0Zd4wRyxN7IFg8jM N13H9kslwYVmfwDOl510dmksp2LzbjqE+wPpmR/VrqODkf9E9R8sIlcTrDORWCP+y7L3H Xenqr6XkG2b54JQ5dugxfFNexs5WtnlDw8QNhNA7D3IFD4dLLJxZQX5/TF0JNvAl5qZIy oDvXFgqwyNhoHfiLpER6KJaByEZie2+SNCmmwmgmwmJmW44Q/JracU9Uv0n3N8fBHdA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=UgV3axVR; arc=none smtp.remote-ip=91.218.175.231 Received: from mta0.migadu.com (out-231.mta0.migadu.com [91.218.175.231]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 55F4D606A683 for ; Sun, 29 Nov 2026 18:36:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 55F4D606A683 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=UgV3axVR X-Envelope-To: cgroups@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=unSYRI+HoySjJ2HN0KFy5WLOeNtxVOnQFf04AHDIGck=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795977367; v=1; x=1796582167; b=UgV3axVRTBnrTexJJLRSktw4TJW/XViUcIKHBAzm80u2cuBfmZWCSk/W1pP0JgU+zDcpgRtk fOcPwmTdh5qCTcsgQZ1pO6Yh/Wy90LfgjaS3u7R9jS5zmUlXdbEBBWXfBHxx+4O6RYvDOUGaP9p 9TV6/25W5mfBBOuy+ckpikSY= X-Envelope-To: cgroups@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 8062d3789f820df0; Sun, 29 Nov 2026 18:36:07 +0000 X-Mizu-Trace-ID: 8062d3789f820df0 X-Migadu-Flow: FLOW_OUT From: Shakeel Butt To: Tejun Heo Cc: Andrew Morton , Johannes Weiner , Michal Koutny , Michal Hocko , Roman Gushchin , Christian Brauner , Oleg Nesterov , Sebastian Andrzej Siewior , Suren Baghdasaryan , "T . J . Mercier" , Usama Arif , Meta kernel team , 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 Message-ID: <20261129183524.1052436-10-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261129183524.1052436-1-shakeel.butt@linux.dev> References: <20261129183524.1052436-1-shakeel.butt@linux.dev> X-Mailing-List: cgroups@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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