All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/3 cgroup/for-5.20] cgroup: Elide write-locking threadgroup_rwsem when updating csses on an empty subtree
@ 2022-07-15  4:38 Tejun Heo
  2022-07-15  4:38   ` Tejun Heo
  2022-07-26 14:31   ` Michal Koutný
  0 siblings, 2 replies; 32+ messages in thread
From: Tejun Heo @ 2022-07-15  4:38 UTC (permalink / raw)
  To: Christian Brauner, Michal Koutný
  Cc: Peter Zijlstra, John Stultz, Dmitry Shmidt, Oleg Nesterov,
	linux-kernel, cgroups

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;
 }
 

^ permalink raw reply	[flat|nested] 32+ messages in thread

end of thread, other threads:[~2022-07-27 18:56 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-15  4:38 [PATCH 1/3 cgroup/for-5.20] cgroup: Elide write-locking threadgroup_rwsem when updating csses on an empty subtree Tejun Heo
2022-07-15  4:38 ` [PATCH 2/3 cgroup/for-5.20] cgroup: Add "no" prefixed mount options Tejun Heo
2022-07-15  4:38   ` 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-15  4:39     ` Tejun Heo
2022-07-23  5:12     ` Tejun Heo
2022-07-23  5:12       ` Tejun Heo
2022-07-23 14:28     ` [PATCH RESEND " Tejun Heo
2022-07-23 14:28       ` Tejun Heo
2022-07-25 12:12       ` Oleg Nesterov
2022-07-25 12:12         ` Oleg Nesterov
2022-07-26 23:14         ` Tejun Heo
2022-07-26 23:14           ` Tejun Heo
2022-07-27 17:39           ` Oleg Nesterov
2022-07-25 14:16       ` Christian Brauner
2022-07-25 14:16         ` Christian Brauner
2022-07-26 14:32       ` Michal Koutný
2022-07-26 17:33         ` Tejun Heo
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 14:32     ` Michal Koutný
2022-07-26 20:01     ` Tejun Heo
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-26 23:48           ` Tejun Heo
2022-07-27  9:27           ` Michal Koutný
2022-07-27  9:27             ` Michal Koutný
2022-07-27 17:55             ` Tejun Heo
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ý
2022-07-26 14:31   ` Michal Koutný

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.