From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.186 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796060150; b=OJ42JZ+5U60B9pdG9xgIPYeAOu0wIp1QbHy2efnSphadVXC1w/2mPu6+t85M604Kmmfq 1C6mE1Jd7mP+GVUTixy5HYOPaeJazt6KUR8AXfQkMCWUtIRlWg7ykEIKpv/vpzXfK2yxQ BGV8l4EKrvGc28BqO3oK+sczLP2GJuYNHul99K2LdWIXphJFrJE8ayC9MOwmjAAgkK746 73ZaoY5awXEHkL1mmz0eEHVl5iEOtNGF91W/ulbl8y3bc+O/f6zv6EnEkYPJ0plpVO2Nv qSqMzjgywaCjlSS4FMtVK4ziJvyslQ5bNK1MsHLau1b9FOBDDm/yW8/1gzn/YMQIQ8A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796060150; 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=yZi0Ms3JZk0HHFhZzcPPrMvLvSYW9OwbMMNBJXqX5rU=; b=1rwLoq5f99MLtp21hqewnm+HWoXq3nfwKhcJkNYjriKIUfch+QZGHSyPbN+aTkcHMR2x XuyYZydqSW6hwl3EXv5Lk9T9ssvTrGPYIDCLbkP7XJoJuMGRYTpL9QkvRCtJz1GYenpCR qn3VqYLKn+CIZrbYnqgT/a0PhHaNiYNF3gXGKxIW6qMIfHy+uFSkpoy0dW7mynHBVzYZH 7hYbwOPCGsbcv8LezrgSS0E8R5ekiyMpUZm9olUUhcNyhArEKJHzuUqos2VKcoYhp6IPq ppeseGSZ0XhRmZc3gMsfNp9DNEovvBYE0ddWmjKSatG+tGcbEW4ZS10M9ZJ8MHak4+A== 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=aij6Asus; arc=none smtp.remote-ip=95.215.58.186 Received: from mta1.migadu.com (out-186.mta1.migadu.com [95.215.58.186]) (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 823D1606A330 for ; Mon, 30 Nov 2026 17:35:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 823D1606A330 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=aij6Asus X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=Xtl0eS968j9WQEApkVg+jj1oXHwErGYuORHy4gAGYM8=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796060146; v=1; x=1796664946; b=aij6AsusvDyfiZk9rk8EvSgSi6Qs0BjQB4UXuwVoSzyU2I7E3f46QxRgNl3/OLNuVMRLLp3A eS7LPVrpDEjRQAASG64rFJZoWtjzyDGqxck6AHVXR6EvTkAfIcHGWFy3+Mu5LTAIfe4fKbMb1Wh 7YHLdLVI8rTCs9TuK7/Wmprk= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 76e38addec297353; Mon, 30 Nov 2026 17:35:42 +0000 X-Mizu-Trace-ID: 76e38addec297353 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 v2 11/11] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze Date: Mon, 30 Nov 2026 09:34:25 -0800 Message-ID: <20261130173425.4117363-12-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261130173425.4117363-1-shakeel.butt@linux.dev> References: <20261130173425.4117363-1-shakeel.butt@linux.dev> X-Mailing-List: linux-kernel@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. - Set the new state of every cgroup in the subtree first, and walk the tasks after that. Otherwise a task that this write already froze could move to a cgroup that the walk hasn't reached and run again until the walk gets there. A task frozen by an earlier freeze of a nested cgroup can still run again if it moves while the states are being set, until the task walk reaches 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. The walk relies on the task iterator to return every task that stays in a cgroup during the walk. This needs "cgroup: don't lose a task in css_task_iter when a migration fails". 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, a frozen task moved ahead of the walk stays frozen, a task whose migration fails during the walk ends up in its 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 | 152 +++++++++++++++++++++++++++--------- 4 files changed, 162 insertions(+), 63 deletions(-) diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h index a6f42dede900..7c96a2d6cfe9 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 7279613d6804..7d051aad0f14 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 cb8d9165ead2..4a4de2b7b552 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -4414,7 +4414,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; } @@ -4433,15 +4433,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) @@ -4768,7 +4773,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; @@ -5026,7 +5036,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)) @@ -6274,23 +6285,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); @@ -6309,6 +6304,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++; @@ -6321,9 +6337,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..41c8e22668cd 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,75 @@ 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. Set the new state of + * every cgroup first and walk the tasks after that, so that a task that + * moves inside the subtree during the task walk lands in a cgroup that + * already has the new state. 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; } - - /* - * Do change actual state: freeze or unfreeze. - */ - cgroup_do_freeze(dsct, freeze, ts_nsec); + dsct->freezer.e_freeze = e_freeze; + cgroup_set_freeze(dsct, e_freeze); + spin_unlock_irq(&css_set_lock); applied = true; } + rcu_read_unlock(); /* * Even if the actual state hasn't changed, let's notify a user. @@ -319,5 +366,34 @@ void cgroup_freeze(struct cgroup *cgrp, bool freeze) TRACE_CGROUP_PATH(notify_frozen, cgrp, test_bit(CGRP_FROZEN, &cgrp->flags)); cgroup_file_notify(&cgrp->events_file); + goto out_unlock; + } + + /* + * Do change actual state: freeze or unfreeze the tasks of the cgroups + * changed above. A descendant with its own freeze set kept its state, + * and so did its subtree. Only writers, which hold + * cgroup_freeze_mutex, change freeze. + */ + rcu_read_lock(); + css_for_each_descendant_pre(css, &cgrp->self) { + if (css != &cgrp->self && css->cgroup->freezer.freeze) { + css = css_rightmost_descendant(css); + continue; + } + if (!css_tryget_online(css)) + continue; + rcu_read_unlock(); + + cgroup_do_freeze(css->cgroup, freeze); + cond_resched(); + + rcu_read_lock(); + css_put(css); } + rcu_read_unlock(); + +out_unlock: + mutex_unlock(&cgroup_freeze_mutex); + return ret; } -- 2.53.1-Meta