Skip to contentSkip to conversation

sched_ext: allow scx_bpf_task_cgroup() in ops.dispatch()

PATCH In review Changwoo Min 2 participants 5 messages Sep 15, 2025 – Sep 20, 2025

Subsystems (2)
lore
Share & tools
lore

Changwoo Min

author reviewer
#0 lore raw

PATCH 1/1 sched_ext: allow scx_bpf_task_cgroup() in ops.dispatch()

Calling scx_bpf_task_cgroup() on the prev task from ops.dispatch() currently triggers a runtime error:

------
CodecWorker[92989] triggered exit kind 1024:
runtime error (called on a task not being operated on)

Backtrace:
  scx_bpf_task_cgroup+0xbb/0xd0
  bpf_prog_d7d2e9d404cf3602_lavd_dispatch+0xc45/0xccc
  bpf__sched_ext_ops_dispatch+0x4b/0xab
  balance_one+0x25f/0x550
  balance_scx+0x37/0x160
  prev_balance+0x46/0xb0
  __schedule+0x702/0x23d0
  schedule+0x27/0xd0
  irqentry_exit_to_user_mode+0x1ac/0x200
  asm_sysvec_apic_timer_interrupt+0x1a/0x20

------

This happens because the prev task is not registered as a task argument of ops.dispatch(). As a result, scx_kf_allowed_on_arg_tasks() rejects scx_bpf_task_cgroup() calls on the prev task.

Fix this by adding the prev task to scx.kf_tasks so that task-related BPF helpers such as scx_bpf_task_cgroup() can be called safely. Since the SCX_CALL_OP_TASK family assumes the first argument is the task, introduce a new SCX_CALL_OP_TASK_ANY macro without that restriction. Also update __SCX_KF_TERMINAL to include SCX_KF_DISPATCH.

---
 include/linux/sched/ext.h |  3 ++-
 kernel/sched/ext.c        | 13 +++++++++++--
 2 files changed, 13 insertions(+), 3 deletions(-)
include/linux/sched/ext.h +2 -1
index 7047101dbf58..b404a93d371c 100644
@@ -116,7 +116,8 @@ enum scx_kf_mask {
 
 	__SCX_KF_RQ_LOCKED	= SCX_KF_CPU_RELEASE | SCX_KF_DISPATCH |
 				  SCX_KF_ENQUEUE | SCX_KF_SELECT_CPU | SCX_KF_REST,
-	__SCX_KF_TERMINAL	= SCX_KF_ENQUEUE | SCX_KF_SELECT_CPU | SCX_KF_REST,
+	__SCX_KF_TERMINAL	= SCX_KF_DISPATCH | SCX_KF_ENQUEUE |
+				  SCX_KF_SELECT_CPU | SCX_KF_REST,
 };
 
 enum scx_dsq_lnode_flags {
kernel/sched/ext.c +11 -2
index 3eb6be889da6..f704d2e5dce2 100644
@@ -328,6 +328,14 @@ do {										\
 	__ret;									\
 })
 
+#define SCX_CALL_OP_TASK_ANY(sch, mask, op, rq, task, args...)			\
+do {										\
+	BUILD_BUG_ON((mask) & ~__SCX_KF_TERMINAL);				\
+	current->scx.kf_tasks[0] = task;					\
+	SCX_CALL_OP((sch), mask, op, rq, ##args);				\
+	current->scx.kf_tasks[0] = NULL;					\
+} while (0)
+
 /* @mask is constant, always inline to cull unnecessary branches */
 static __always_inline bool scx_kf_allowed(u32 mask)
 {
@@ -2105,8 +2113,9 @@ static int balance_one(struct rq *rq, struct task_struct *prev)
 	do {
 		dspc->nr_tasks = 0;
 
-		SCX_CALL_OP(sch, SCX_KF_DISPATCH, dispatch, rq,
-			    cpu_of(rq), prev_on_scx ? prev : NULL);
+		SCX_CALL_OP_TASK_ANY(sch, SCX_KF_DISPATCH, dispatch, rq,
+				     prev_on_scx ? prev : NULL,
+				     cpu_of(rq), prev_on_scx ? prev : NULL);
 
 		flush_dispatch_buf(sch, rq);
 
Signature · 1 line
2.51.0
reviewer
#1 lore raw

Hello,

On Mon, Sep 15, 2025 at 03:52:36PM +0900, Changwoo Min wrote:
...

Quoted 5 lines

Fix this by adding the prev task to scx.kf_tasks so that task-related BPF helpers such as scx_bpf_task_cgroup() can be called safely. Since the SCX_CALL_OP_TASK family assumes the first argument is the task, introduce a new SCX_CALL_OP_TASK_ANY macro without that restriction. Also update __SCX_KF_TERMINAL to include SCX_KF_DISPATCH.

I'm not sure this is safe tho. ops.dispatch() can release the rq lock it's holding to migrate tasks across rq's, which means that other operations can nest inside - ie. there can be an irq which triggers ops.enqueue() while ops.dispatch() is in progress. That can in turn overwrite current->scx.kf_tasks[].

I wonder whether a better approach would be tracking cgroup membership from BPF side. ops.init_task() tells you the initial cgroup it's joining and if the task later moves to another cgroup, ops.cgroup_move() will be invoked. Would that work?

Thanks.

Signature · 1 line
tejun
author reviewer
#2 lore raw

Hello Tejun,

Thanks for the feedback.

Quoted 10 lines · Tejun Heo

Hello,

On Mon, Sep 15, 2025 at 03:52:36PM +0900, Changwoo Min wrote:
...

Quoted 5 lines

Fix this by adding the prev task to scx.kf_tasks so that task-related BPF helpers such as scx_bpf_task_cgroup() can be called safely. Since the SCX_CALL_OP_TASK family assumes the first argument is the task, introduce a new SCX_CALL_OP_TASK_ANY macro without that restriction. Also update __SCX_KF_TERMINAL to include SCX_KF_DISPATCH.

Quoted 5 lines

I'm not sure this is safe tho. ops.dispatch() can release the rq lock it's holding to migrate tasks across rq's, which means that other operations can nest inside - ie. there can be an irq which triggers ops.enqueue() while ops.dispatch() is in progress. That can in turn overwrite current->scx.kf_tasks[].

I thought that ops.dispatch() always holds an rq lock since there is a lockdep_assert_rq_held(rq) check at the beginning of balance_one(), which invokes the BPF scheduler's dispatch. I guess I am missing an edge case?

Quoted 3 lines

I wonder whether a better approach would be tracking cgroup membership from BPF side. ops.init_task() tells you the initial cgroup it's joining and if

Currently, it is also not allowed to call scx_bpf_task_cgroup() at ops.init_task() because the rq lock is not held at ops.init_task(). The earliest possible moment to get a task's cgroup ID is ops.enable().

Quoted 2 lines

the task later moves to another cgroup, ops.cgroup_move() will be invoked. Would that work?

Checking cgroup ID at ops.enable() is not ideal because there would be no cgroup ID changes between ops.enable() calls. However, the additional overhead should be marginal. So tracking cgroup ID at ops.enable() and ops.cgroup_move() will work.

Regards,
Changwoo Min

reviewer
#3 lore raw

Hello,

Quoted 10 lines · Changwoo Min
Quoted 5 lines

I'm not sure this is safe tho. ops.dispatch() can release the rq lock it's holding to migrate tasks across rq's, which means that other operations can nest inside - ie. there can be an irq which triggers ops.enqueue() while ops.dispatch() is in progress. That can in turn overwrite current->scx.kf_tasks[].

I thought that ops.dispatch() always holds an rq lock since there is a lockdep_assert_rq_held(rq) check at the beginning of balance_one(), which invokes the BPF scheduler's dispatch. I guess I am missing an edge case?

ops.dispatch() is always called and returns with rq locked; however, it sometimes needs to migrate tasks across rq boundaries and thus can temporarily release the rq lock it was called with. Please take a look at dispatch_to_local_dsq() which is called from balance_one() -> flush_dispatch_buf() -> finish_dispatch().

Quoted 7 lines
Quoted 2 lines

I wonder whether a better approach would be tracking cgroup membership from BPF side. ops.init_task() tells you the initial cgroup it's joining and if

Currently, it is also not allowed to call scx_bpf_task_cgroup() at ops.init_task() because the rq lock is not held at ops.init_task(). The earliest possible moment to get a task's cgroup ID is ops.enable().

ops.init_task() is called with scx_init_task_args, which contains .cgroup which points to the CPU controller cgroup the task belongs to.

Thanks.

Signature · 1 line
tejun
author reviewer
#4 lore raw

Hello,

Thanks for the clarification.

Quoted 14 lines · Tejun Heo

Hello,

Quoted 10 lines · Changwoo Min
Quoted 5 lines

I'm not sure this is safe tho. ops.dispatch() can release the rq lock it's holding to migrate tasks across rq's, which means that other operations can nest inside - ie. there can be an irq which triggers ops.enqueue() while ops.dispatch() is in progress. That can in turn overwrite current->scx.kf_tasks[].

I thought that ops.dispatch() always holds an rq lock since there is a lockdep_assert_rq_held(rq) check at the beginning of balance_one(), which invokes the BPF scheduler's dispatch. I guess I am missing an edge case?

Quoted 5 lines

ops.dispatch() is always called and returns with rq locked; however, it sometimes needs to migrate tasks across rq boundaries and thus can temporarily release the rq lock it was called with. Please take a look at dispatch_to_local_dsq() which is called from balance_one() -> flush_dispatch_buf() -> finish_dispatch().

You are right. I overlooked that path.

Quoted 10 lines
Quoted 7 lines
Quoted 2 lines

I wonder whether a better approach would be tracking cgroup membership from BPF side. ops.init_task() tells you the initial cgroup it's joining and if

Currently, it is also not allowed to call scx_bpf_task_cgroup() at ops.init_task() because the rq lock is not held at ops.init_task(). The earliest possible moment to get a task's cgroup ID is ops.enable().

ops.init_task() is called with scx_init_task_args, which contains .cgroup which points to the CPU controller cgroup the task belongs to.

Yeah, that's better than relying on ops.enable().

Regards,
Changwoo Min