From: Tejun Heo <tj@kernel.org>
To: "Christian Brauner" <brauner@kernel.org>,
"Michal Koutný" <mkoutny@suse.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
John Stultz <john.stultz@linaro.org>,
Dmitry Shmidt <dimitrysh@google.com>,
Oleg Nesterov <oleg@redhat.com>,
linux-kernel@vger.kernel.org, cgroups@vger.kernel.org
Subject: [PATCH 1/3 cgroup/for-5.20] cgroup: Elide write-locking threadgroup_rwsem when updating csses on an empty subtree
Date: Thu, 14 Jul 2022 18:38:15 -1000 [thread overview]
Message-ID: <YtDvN0wJ6CKaEPN8@slm.duckdns.org> (raw)
cgroup_update_dfl_csses() write-lock the threadgroup_rwsem as updating the
csses can trigger process migrations. However, if the subtree doesn't
contain any tasks, there aren't gonna be any cgroup migrations. This
condition can be trivially detected by testing whether
mgctx.preloaded_src_csets is empty. Elide write-locking threadgroup_rwsem if
the subtree is empty.
After this optimization, the usage pattern of creating a cgroup, enabling
the necessary controllers, and then seeding it with CLONE_INTO_CGROUP and
then removing the cgroup after it becomes empty doesn't need to write-lock
threadgroup_rwsem at all.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Michal Koutný <mkoutny@suse.com>
---
kernel/cgroup/cgroup.c | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -2933,12 +2933,11 @@ static int cgroup_update_dfl_csses(struc
struct cgroup_subsys_state *d_css;
struct cgroup *dsct;
struct css_set *src_cset;
+ bool has_tasks;
int ret;
lockdep_assert_held(&cgroup_mutex);
- percpu_down_write(&cgroup_threadgroup_rwsem);
-
/* look up all csses currently attached to @cgrp's subtree */
spin_lock_irq(&css_set_lock);
cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
@@ -2949,6 +2948,16 @@ static int cgroup_update_dfl_csses(struc
}
spin_unlock_irq(&css_set_lock);
+ /*
+ * We need to write-lock threadgroup_rwsem while migrating tasks.
+ * However, if there are no source csets for @cgrp, changing its
+ * controllers isn't gonna produce any task migrations and the
+ * write-locking can be skipped safely.
+ */
+ has_tasks = !list_empty(&mgctx.preloaded_src_csets);
+ if (has_tasks)
+ percpu_down_write(&cgroup_threadgroup_rwsem);
+
/* NULL dst indicates self on default hierarchy */
ret = cgroup_migrate_prepare_dst(&mgctx);
if (ret)
@@ -2967,7 +2976,8 @@ static int cgroup_update_dfl_csses(struc
ret = cgroup_migrate_execute(&mgctx);
out_finish:
cgroup_migrate_finish(&mgctx);
- percpu_up_write(&cgroup_threadgroup_rwsem);
+ if (has_tasks)
+ percpu_up_write(&cgroup_threadgroup_rwsem);
return ret;
}
next reply other threads:[~2022-07-15 4:38 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-15 4:38 Tejun Heo [this message]
2022-07-15 4:38 ` [PATCH 2/3 cgroup/for-5.20] cgroup: Add "no" prefixed mount options Tejun Heo
2022-07-15 4:39 ` [PATCH 3/3 cgroup/for-5.20] cgroup: Make !percpu threadgroup_rwsem operations optional Tejun Heo
2022-07-23 5:12 ` Tejun Heo
2022-07-23 14:28 ` [PATCH RESEND " Tejun Heo
2022-07-25 12:12 ` Oleg Nesterov
2022-07-26 23:14 ` Tejun Heo
2022-07-27 17:39 ` Oleg Nesterov
2022-07-25 14:16 ` Christian Brauner
2022-07-26 14:32 ` Michal Koutný
2022-07-26 17:33 ` Tejun Heo
2022-07-26 14:32 ` [PATCH 2/3 cgroup/for-5.20] cgroup: Add "no" prefixed mount options Michal Koutný
2022-07-26 20:01 ` Tejun Heo
2022-07-26 23:30 ` Tejun Heo
2022-07-26 23:48 ` [PATCH cgroup/for-5.20] cgroup: remove "no" prefixed mount options options Tejun Heo
2022-07-27 9:27 ` Michal Koutný
2022-07-27 17:55 ` Tejun Heo
2022-07-26 14:31 ` [PATCH 1/3 cgroup/for-5.20] cgroup: Elide write-locking threadgroup_rwsem when updating csses on an empty subtree Michal Koutný
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=YtDvN0wJ6CKaEPN8@slm.duckdns.org \
--to=tj@kernel.org \
--cc=brauner@kernel.org \
--cc=cgroups@vger.kernel.org \
--cc=dimitrysh@google.com \
--cc=john.stultz@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mkoutny@suse.com \
--cc=oleg@redhat.com \
--cc=peterz@infradead.org \
/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®