Skip to contentSkip to conversation

sched_ext: Use cgroup_lock/unlock() to synchronize against cgroup operations

PATCH Applied Tejun Heo 3 participants 8 messages Sep 2, 2025 – Sep 3, 2025

Subsystems (2)
lore
Share & tools
lore

Tejun Heo

author reviewer
#0 lore raw

PATCH 1/1 sched_ext: Use cgroup_lock/unlock() to synchronize against cgroup operations

SCX hooks into CPU cgroup controller operations and read-locks scx_cgroup_rwsem to exclude them while enabling and disable schedulers. While this works, it's unnecessarily complicated given that cgroup_[un]lock() are available and thus the cgroup operations can be locked out that way.

Drop scx_cgroup_rwsem locking from the tg on/offline and cgroup [can_]attach operations. Instead, grab cgroup_lock() from scx_cgroup_lock(). Drop scx_cgroup_finish_attach() which is no longer necessary. Drop the now unnecessary rcu locking and css ref bumping in scx_cgroup_init() and scx_cgroup_exit().

As scx_cgroup_set_weight/bandwidth() paths aren't protected by cgroup_lock(), rename scx_cgroup_rwsem to scx_cgroup_ops_rwsem and retain the locking there.

This is overall simpler and will also allow enable/disable paths to synchronize against cgroup changes independent of the CPU controller.

Cc Peter Zijlstra <[email protected]>

---
This results in a tiny simplification on the core side. Peter, if you don't object, I'll route this through sched_ext/for-6.18.

Thanks.

 kernel/sched/core.c |    2 -
 kernel/sched/ext.c  |   66 +++++++++++-----------------------------------------
 kernel/sched/ext.h  |    2 -
 3 files changed, 14 insertions(+), 56 deletions(-)
kernel/sched/core.c +0 -2
@@ -9362,8 +9362,6 @@ static void cpu_cgroup_attach(struct cgr
 
 	cgroup_taskset_for_each(task, css, tset)
 		sched_move_task(task, false);
-
-	scx_cgroup_finish_attach();
 }
 
 static void cpu_cgroup_cancel_attach(struct cgroup_taskset *tset)
kernel/sched/ext.c +14 -52

203 lines not on this page · raw message

kernel/sched/ext.h +0 -2
@@ -100,7 +100,6 @@ int scx_tg_online(struct task_group *tg)
 void scx_tg_offline(struct task_group *tg);
 int scx_cgroup_can_attach(struct cgroup_taskset *tset);
 void scx_cgroup_move_task(struct task_struct *p);
-void scx_cgroup_finish_attach(void);
 void scx_cgroup_cancel_attach(struct cgroup_taskset *tset);
 void scx_group_set_weight(struct task_group *tg, unsigned long cgrp_weight);
 void scx_group_set_idle(struct task_group *tg, bool idle);
@@ -111,7 +110,6 @@ static inline int scx_tg_online(struct t
 static inline void scx_tg_offline(struct task_group *tg) {}
 static inline int scx_cgroup_can_attach(struct cgroup_taskset *tset) { return 0; }
 static inline void scx_cgroup_move_task(struct task_struct *p) {}
-static inline void scx_cgroup_finish_attach(void) {}
 static inline void scx_cgroup_cancel_attach(struct cgroup_taskset *tset) {}
 static inline void scx_group_set_weight(struct task_group *tg, unsigned long cgrp_weight) {}
 static inline void scx_group_set_idle(struct task_group *tg, bool idle) {}
maintainer
#1 lore raw
Quoted 16 lines · Tejun Heo

SCX hooks into CPU cgroup controller operations and read-locks scx_cgroup_rwsem to exclude them while enabling and disable schedulers. While this works, it's unnecessarily complicated given that cgroup_[un]lock() are available and thus the cgroup operations can be locked out that way.

Drop scx_cgroup_rwsem locking from the tg on/offline and cgroup [can_]attach operations. Instead, grab cgroup_lock() from scx_cgroup_lock(). Drop scx_cgroup_finish_attach() which is no longer necessary. Drop the now unnecessary rcu locking and css ref bumping in scx_cgroup_init() and scx_cgroup_exit().

As scx_cgroup_set_weight/bandwidth() paths aren't protected by cgroup_lock(), rename scx_cgroup_rwsem to scx_cgroup_ops_rwsem and retain the locking there.

Quoted 8 lines

This is overall simpler and will also allow enable/disable paths to synchronize against cgroup changes independent of the CPU controller.

Cc Peter Zijlstra <[email protected]>

---
This results in a tiny simplification on the core side. Peter, if you don't object, I'll route this through sched_ext/for-6.18.

No problem, that's fine.

reviewer
#2 lore raw
In reply to Tejun Heo

Hi Tejun,

Quoted 22 lines · Tejun Heo

SCX hooks into CPU cgroup controller operations and read-locks scx_cgroup_rwsem to exclude them while enabling and disable schedulers. While this works, it's unnecessarily complicated given that cgroup_[un]lock() are available and thus the cgroup operations can be locked out that way.

Drop scx_cgroup_rwsem locking from the tg on/offline and cgroup [can_]attach operations. Instead, grab cgroup_lock() from scx_cgroup_lock(). Drop scx_cgroup_finish_attach() which is no longer necessary. Drop the now unnecessary rcu locking and css ref bumping in scx_cgroup_init() and scx_cgroup_exit().

As scx_cgroup_set_weight/bandwidth() paths aren't protected by cgroup_lock(), rename scx_cgroup_rwsem to scx_cgroup_ops_rwsem and retain the locking there.

This is overall simpler and will also allow enable/disable paths to synchronize against cgroup changes independent of the CPU controller.

Cc Peter Zijlstra <[email protected]>

---

...

kernel/sched/ext.c void scx_group_set_bandwidth(struct task · line 4271

Quoted 7 lines

static void scx_cgroup_lock(void)
{

kernel/sched/ext.c +2 -1
-	percpu_down_write(&scx_cgroup_rwsem);
+	percpu_down_write(&scx_cgroup_ops_rwsem);
+	cgroup_lock();
 }

Shouldn't we acquire cgroup_lock() before scx_cgroup_ops_rwsem to avoid a potential AB-BA deadlock?

kernel/sched/ext.c void scx_group_set_bandwidth(struct task · line 4277

Quoted 7 lines

static void scx_cgroup_unlock(void)
{

kernel/sched/ext.c +2 -1
-	percpu_up_write(&scx_cgroup_rwsem);
+	cgroup_unlock();
+	percpu_up_write(&scx_cgroup_ops_rwsem);
 }

Thanks,
-Andrea

author reviewer
#3 lore raw

Hello,

Quoted 9 lines · Andrea Righi

kernel/sched/ext.c void scx_group_set_bandwidth(struct task · line 4271

Quoted 6 lines

static void scx_cgroup_lock(void)
{

kernel/sched/ext.c +2 -1
-	percpu_down_write(&scx_cgroup_rwsem);
+	percpu_down_write(&scx_cgroup_ops_rwsem);
+	cgroup_lock();
 }

Shouldn't we acquire cgroup_lock() before scx_cgroup_ops_rwsem to avoid a potential AB-BA deadlock?

There's no existing ordering between the two locks, so any order should be safe. The reason why I put it in this particular order is because any cgroup_lock() holder has no reason to grab ops_rwsem now or in the future while the opposite direction is still unlikely but theoretically more possible.

Thanks.

Signature · 1 line
tejun
reviewer
#4 lore raw
Quoted 13 lines · Tejun Heo

Hello,

Quoted 9 lines · Andrea Righi

kernel/sched/ext.c void scx_group_set_bandwidth(struct task · line 4271

Quoted 6 lines

static void scx_cgroup_lock(void)
{

kernel/sched/ext.c +2 -1
-	percpu_down_write(&scx_cgroup_rwsem);
+	percpu_down_write(&scx_cgroup_ops_rwsem);
+	cgroup_lock();
 }

Shouldn't we acquire cgroup_lock() before scx_cgroup_ops_rwsem to avoid a potential AB-BA deadlock?

Quoted 5 lines

There's no existing ordering between the two locks, so any order should be safe. The reason why I put it in this particular order is because any cgroup_lock() holder has no reason to grab ops_rwsem now or in the future while the opposite direction is still unlikely but theoretically more possible.

Isn't scx_group_set_weight() called with cgroup_mutex held? In this case the order is cgroup_lock() -> scx_cgroup_ops_rwsem, or am I missing something?

Thanks,
-Andrea

author reviewer
#5 lore raw
Quoted 20 lines · Andrea Righi
Quoted 13 lines · Tejun Heo

Hello,

Quoted 9 lines · Andrea Righi

kernel/sched/ext.c void scx_group_set_bandwidth(struct task · line 4271

Quoted 6 lines

static void scx_cgroup_lock(void)
{

kernel/sched/ext.c +2 -1
-	percpu_down_write(&scx_cgroup_rwsem);
+	percpu_down_write(&scx_cgroup_ops_rwsem);
+	cgroup_lock();
 }

Shouldn't we acquire cgroup_lock() before scx_cgroup_ops_rwsem to avoid a potential AB-BA deadlock?

Quoted 5 lines

There's no existing ordering between the two locks, so any order should be safe. The reason why I put it in this particular order is because any cgroup_lock() holder has no reason to grab ops_rwsem now or in the future while the opposite direction is still unlikely but theoretically more possible.

Quoted 3 lines

Isn't scx_group_set_weight() called with cgroup_mutex held? In this case the order is cgroup_lock() -> scx_cgroup_ops_rwsem, or am I missing something?

Oh, no, cgroup_lock() is only held for operations that change the cgroup hierarchy - cgroup creation, deletion, controller enable/disable, task migration and so on. Writes to control knobs doesn't acquire cgroup_lock().

Thanks.

Signature · 1 line
tejun
reviewer Acked-by
#6 lore raw
Quoted 21 lines · Tejun Heo
Quoted 20 lines · Andrea Righi
Quoted 18 lines · Tejun Heo

Hello,

Quoted 9 lines · Andrea Righi

kernel/sched/ext.c void scx_group_set_bandwidth(struct task · line 4271

Quoted 6 lines

static void scx_cgroup_lock(void)
{

kernel/sched/ext.c +2 -1
-	percpu_down_write(&scx_cgroup_rwsem);
+	percpu_down_write(&scx_cgroup_ops_rwsem);
+	cgroup_lock();
 }

Shouldn't we acquire cgroup_lock() before scx_cgroup_ops_rwsem to avoid a potential AB-BA deadlock?

There's no existing ordering between the two locks, so any order should be safe. The reason why I put it in this particular order is because any cgroup_lock() holder has no reason to grab ops_rwsem now or in the future while the opposite direction is still unlikely but theoretically more possible.

Quoted 7 lines
Quoted 3 lines

Isn't scx_group_set_weight() called with cgroup_mutex held? In this case the order is cgroup_lock() -> scx_cgroup_ops_rwsem, or am I missing something?

Oh, no, cgroup_lock() is only held for operations that change the cgroup hierarchy - cgroup creation, deletion, controller enable/disable, task migration and so on. Writes to control knobs doesn't acquire cgroup_lock().

Ah! That's the part I was missing, thanks for clarifying it. In that case:

-Andrea

author reviewer Applied
#7 lore raw
In reply to Tejun Heo
Quoted 16 lines · Tejun Heo

SCX hooks into CPU cgroup controller operations and read-locks scx_cgroup_rwsem to exclude them while enabling and disable schedulers. While this works, it's unnecessarily complicated given that cgroup_[un]lock() are available and thus the cgroup operations can be locked out that way.

Drop scx_cgroup_rwsem locking from the tg on/offline and cgroup [can_]attach operations. Instead, grab cgroup_lock() from scx_cgroup_lock(). Drop scx_cgroup_finish_attach() which is no longer necessary. Drop the now unnecessary rcu locking and css ref bumping in scx_cgroup_init() and scx_cgroup_exit().

As scx_cgroup_set_weight/bandwidth() paths aren't protected by cgroup_lock(), rename scx_cgroup_rwsem to scx_cgroup_ops_rwsem and retain the locking there.

Quoted 5 lines

This is overall simpler and will also allow enable/disable paths to synchronize against cgroup changes independent of the CPU controller.

Cc Peter Zijlstra <[email protected]>

Applied to sched_ext/for-6.18.

Thanks.

Signature · 1 line
tejun