From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 834E5606A32C for ; Fri, 4 Dec 2026 18:42:04 +0000 (UTC) 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=Y/T/khnj Received: by kanga.kvack.org (Postfix) id 99C926B05C3; Fri, 4 Dec 2026 13:41:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 94C616B05C4; Fri, 4 Dec 2026 13:41:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 862A66B05C5; Fri, 4 Dec 2026 13:41:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 5149E6B05C3 for ; Fri, 4 Dec 2026 13:41:30 -0500 (EST) Received: from smtpin19.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D4B55A8049 for ; Fri, 4 Dec 2026 18:41:29 +0000 (UTC) X-FDA: 85507168698.19.7495DA6 Received: from mta0.migadu.com (out-61.mta0.migadu.com [91.218.175.61]) by imf09.hostedemail.com (Postfix) with ESMTP id C99A614000B for ; Fri, 4 Dec 2026 18:41:27 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="Y/T/khnj"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf09.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.61 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796409688; b=45w13n5BsPzVfSJvjnOUkzWgqQYH5uz7xpHYqQ1+EZR82sx5sZZFhxhdgZXx4ItQgz1xwq Y3v9zINTmUDuzq5di9yv1UxUXp0wihHLGP5f/ZinA5u5QqFuTI/1Mfuq1o4g06BlkTFQBq pn6lXbHI/URHnT+5SXngaOk1IyhuLeg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796409688; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=v4CQmD01VRrGFBO9gYLbZDh2p9GJcTlMjse8Dy+4W4c=; b=XXzFcnzydHNceiYA0v5Qxf1oFZTMSKYriAK/L7gfsxEBWNfymW7MKqLdxB6OS7MiZxT/gJ 0zI70Ulz50QTUCpJEUIKPu88RqhPlJ60jq3MmGsEUNk3ZvImk+Uq5wc8XqBGGUy0vDkU+9 SHMAsCgYtydxy4LMImHLDvKBLBUcFq0= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="Y/T/khnj"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf09.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.61 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=xdBFBNHHgGEFv8G7ZFPGrzEvKV5HDoCUUGVF94ps70s=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796409686; v=1; x=1797014486; b=Y/T/khnjHq9zpUzVGj2+iGsltzpxrqcfI+lPfwzkieXN5VnQt5IigGGOQnfJhVtvDQ2M6bd0 sQ7OCpDtIwqyrWtgWpJvJHn53SFbkQ8Q2KHGFCT2JjwhoKP3JaOp3zOA4dAmp3TUIBBEyUJIdWO Lw08VSWuZc0bc6BZHDc3d3Ts= X-Envelope-To: linux-mm@kvack.org Received: by smtp.migadu.com with ESMTPS id 570b27108ba843c0; Fri, 04 Dec 2026 18:41:26 +0000 X-Mizu-Trace-ID: 570b27108ba843c0 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 v3 12/12] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze Date: Fri, 4 Dec 2026 10:40:41 -0800 Message-ID: <20261204184041.1489976-13-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261204184041.1489976-1-shakeel.butt@linux.dev> References: <20261204184041.1489976-1-shakeel.butt@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: zch78tcmaiuj57gj4f3be5smm1casq3z X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Rspam-User: X-Rspamd-Queue-Id: C99A614000B X-Rspamd-Server: rspam07 X-HE-Tag: 1796409687-989727 X-HE-Meta: U2FsdGVkX1+FcYOpzFsoBrC5s6ZJ1cptvTHZXzaR6ep/evgrTA7W2kN9XC0Um0nuZ/nDTwSN/2eC+6Mc+gHYLRdmDg+G9WLfNh5KBoXxo/t2PegzwWJT+cYC06OU9XObUapVL4vG3sjmsmURq2xqY/WQUx5OWthO2xr4qPmiUndlKIM0szh6xa6sa0ZDafh2bGqvYieo/Q0LPksg3AlIE99+cOVKk1P2xH7kOtCHwzR8FhsRU1UuiWYJY7upCGRF20q5sX4TRm8NA76ehPzqJEhrok7UnDee9wHRTQO9cI3inWf0BSs7XFIl48Og6hjtBFxRNHePzwvN0E/fghJMlgPJmywtslXapGdu3p6ohAD7HAQG/GZNTzwjf9QaAZpNZML11TFJUeWW/gseOKEzL7SK8IQkGuNX4Oj+DjztFuru3mlAfqSdXeypKYVQkOGrGMQfUSBIy0tDaAnlCtw4PxtaoPD0VUh0XN4n4UT47B09t1x42WEwXlwXKhd6oMK0bWCsChQTXM8RHwnyKCVDoehNyYXAWYoy2UOHiwpYVWru9XFpUDW56DxTFz/BT4jdkWeD7qyHDD/4BhMFZ3ieQ15TVI6aFiRYHG3Al+kh/OS4AYd2krwsSQcoU2te+H25pb5mtjRZeRpH8QGyAkILZqBwfPfOFy7qNXOVICnU5VrPNHC99mgBaiPm7CQMTftSQaJTAMoh6sQOGTVpywGpVazKpI9KvU+SPnywLGcKN7olyjQxiYLrvsyVeWU22p/D4omyVLCH1s7Sld2vKTN19d49rE7AOaTTLW1GScOxSf5Dpi2x4cH9GCKU2b7j4X/RMrWqVqCUhwMiscEALGUbcOROPgBm9GrY5fkrmHKlw7+XOLoVqW4m2n6j272ioI1ErYsRPP06ZWBJO9UbFXZ3pEDG96nI5jfWLyxeo6HcsWGlYWcvwirKF9qIEvh3wfNGT6p+dIVzvalZQwqfAvP bwg10uQB YoKENAr5i8GU2OoiGrYecc7XHxblwPfDR5og4S06KxutccK4tPIy815JH1xDjxzeQcfoXynwHadlE1VdQ70J0kGnbQ5gEbNiV+2VelPfTD1qsk8hc0hdz34Lp2qeCCKV7JiwGchF3y9WYyagKtIXnjso6W6iRuafzr84cRma0RyQIpjcWFmjx/dmhSug4nh35zTHuNrXx3Mc/9wgEGiwfjh1+1NvJtczhu1BaISpgFSRE9br+TjPAiFguOBjFhkFnuD+0lrZzQzL3bT/+FkoFBfHTK2zxOHkOvCyjC9vIoqhHVwA8fdaPqw/7F68jOKd1xFDQA601a6Kz+Y8wdJpgBP38vMjlhNikhoUt36bZp+BitvmEv2n4nS/GPL2DPSGeFnF0eIiJmKJwGI8d/JMibLnlfTwFSptWps3mdxGp7du9LsQd+F7ei8ORxZ9bA5TXsI2NYt4Pl39OTGyaedJblmN2xc3XcWxYKQtwou3n6YsHQbXLTqe6Y73e8N0RVFw9UkT24ABBte0a+jUOogSadXJukRZEacQAnZEDw4vfG2shlHSZ109hmVKaf20e2K5di5FWTAabBKOIPmW74qShFjBluUWhS8w5zKLW Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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. - The first pass marks the cgroups whose state it changes, and the second pass walks only those. A cgroup created during the walk inherits the new state and needs no walk, also when it is created below a subtree that the walk skipped. For a changed cgroup whose subtree has no tasks, the first pass also emits the trace event and updates the frozen state, so the second pass can skip the branch. In an 8-CPU VM without lock debugging, a freeze or thaw write to a cgroup with 1,024 empty children took a median of 183 us, against 416 us with two full passes and 273 us before this patch. - 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. Co-developed-by: Andrew Morton Signed-off-by: Andrew Morton Signed-off-by: Shakeel Butt --- include/linux/cgroup-defs.h | 13 +++ include/linux/cgroup.h | 2 +- kernel/cgroup/cgroup.c | 64 +++++++++----- kernel/cgroup/freezer.c | 169 ++++++++++++++++++++++++++++-------- 4 files changed, 188 insertions(+), 60 deletions(-) diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h index 44e69e784057..aad599cf3283 100644 --- a/include/linux/cgroup-defs.h +++ b/include/linux/cgroup-defs.h @@ -455,12 +455,25 @@ 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; /* Should the cgroup actually be frozen? */ bool e_freeze; + /* + * The cgroup_freeze() call that last changed e_freeze. Only + * cgroup_freeze() uses it, with cgroup_freeze_mutex held. + */ + unsigned int walk_seq; + /* Fields below are protected by css_set_lock */ /* Number of frozen descendant cgroups */ 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 070d15f4031d..aa7832cc9e1d 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -4451,7 +4451,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; } @@ -4470,15 +4470,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; } /* @@ -4825,7 +4830,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; @@ -5083,7 +5093,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)) @@ -6331,23 +6342,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); @@ -6366,6 +6361,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++; @@ -6378,9 +6394,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..214704444906 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,96 @@ 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); + +/* Numbers the cgroup_freeze() calls, protected by cgroup_freeze_mutex */ +static unsigned int cgroup_freeze_seq; + +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; + unsigned int seq; + 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); + seq = ++cgroup_freeze_seq; /* - * 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. The first pass marks the + * cgroups it changes with @seq for the second pass. */ + 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; + dsct->freezer.walk_seq = seq; + cgroup_set_freeze(dsct, e_freeze); /* - * Do change actual state: freeze or unfreeze. + * No task in an empty subtree needs to be signalled. Update its + * frozen state now, so that the second pass can skip the whole + * branch. Tasks that enter it later get the new CGRP_FREEZE + * state when they fork or migrate. */ - cgroup_do_freeze(dsct, freeze, ts_nsec); + if (!cgroup_is_populated(dsct)) { + if (e_freeze) + TRACE_CGROUP_PATH(freeze, dsct); + else + TRACE_CGROUP_PATH(unfreeze, dsct); + cgroup_update_frozen(dsct); + } + 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 +387,36 @@ 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, and only of those. A cgroup that the first pass didn't + * mark kept its state, was removed, or was created after the first pass + * visited its parent, and so was its whole subtree. Subtrees without + * tasks were done in the first pass. + */ + rcu_read_lock(); + css_for_each_descendant_pre(css, &cgrp->self) { + if (css->cgroup->freezer.walk_seq != seq || + !cgroup_is_populated(css->cgroup)) { + 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