Skip to contentSkip to conversation

sched_ext: Fix NULL pointer dereferences in put_prev_task_scx

PATCH In review liuwenfang 2 participants 5 messages Jun 9, 2025 – Jun 11, 2025 Scheduler

lore
Share & tools
lore

liuwenfang

author
#0 lore raw

PATCH 1/1 sched_ext: Fix NULL pointer dereferences in put_prev_task_scx

As put_prev_task can be used in other kernel modules which can lead to a NULL pointer. Fix this by checking for a valid next.

---
 kernel/sched/ext.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/ext.c +1 -1
index f5133249f..6a579babd 100644
@@ -3262,7 +3262,7 @@ static void put_prev_task_scx(struct rq *rq, struct task_struct *p,
 		 * ops.enqueue() that @p is the only one available for this cpu,
 		 * which should trigger an explicit follow-up scheduling event.
 		 */
-		if (sched_class_above(&ext_sched_class, next->sched_class)) {
+		if (next && sched_class_above(&ext_sched_class, next->sched_class)) {
 			WARN_ON_ONCE(!static_branch_unlikely(&scx_ops_enq_last));
 			do_enqueue_task(rq, p, SCX_ENQ_LAST, -1);
 		} else {
Signature · 1 line
2.17.1
#1 lore raw
Quoted 2 lines · liuwenfang

As put_prev_task can be used in other kernel modules which can lead to a NULL pointer. Fix this by checking for a valid next.

Actually, put_prev_task() should be used only within kernel/sched/ and, in theory, you should have done a dequeue_task() before put_prev_task() in this scenario, so SCX_TASK_QUEUED shouldn't be set in p->scx.flags.

The change might still make sense, but can you clarify how you triggered the NULL pointer dereference?

Thanks,
-Andrea

Quoted 21 lines
---
 kernel/sched/ext.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/ext.c +1 -1
index f5133249f..6a579babd 100644
@@ -3262,7 +3262,7 @@ static void put_prev_task_scx(struct rq *rq, struct task_struct *p,
 		 * ops.enqueue() that @p is the only one available for this cpu,
 		 * which should trigger an explicit follow-up scheduling event.
 		 */
-		if (sched_class_above(&ext_sched_class, next->sched_class)) {
+		if (next && sched_class_above(&ext_sched_class, next->sched_class)) {
 			WARN_ON_ONCE(!static_branch_unlikely(&scx_ops_enq_last));
 			do_enqueue_task(rq, p, SCX_ENQ_LAST, -1);
 		} else {
Signature · 1 line
2.17.1
author
#2 lore raw

Thanks for your feedback.
This is triggered in kernel modules developed In the mobile scenario:
tasks on rq are migrated while the current cpu should be halted for power saving policy. Its migration logic:
drain_rq_cpu_stop -- migrate_all_tasks :

for (;;) {
	if (rq->nr_running == 1)
		break;
	for_each_class(class) {
		next = class->pick_next_task(rq);
		if (next) {
			next->sched_class->put_prev_task(rq, next, NULL);
			break;
		}
	}
	if (is_idle_task(next))
		break;
	dest_cpu = select_task_rq(next...);
	move_queued_task(rq, rf, next, dest_cpu);
	...
}

put_prev_task in this function is selected to update util and statistics info for each runnable tasks, here they are not dequeued yet.

Best regards,

Quoted 37 lines · liuwenfang
Quoted 2 lines

As put_prev_task can be used in other kernel modules which can lead to a NULL pointer. Fix this by checking for a valid next.

Actually, put_prev_task() should be used only within kernel/sched/ and, in theory, you should have done a dequeue_task() before put_prev_task() in this scenario, so SCX_TASK_QUEUED shouldn't be set in p->scx.flags.

The change might still make sense, but can you clarify how you triggered the NULL pointer dereference?

Thanks,
-Andrea

kernel/sched/ext.c static void put_prev_task_scx(struct rq *rq, struct

Quoted 11 lines
---
 kernel/sched/ext.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/ext.c index renamed from kernel/sched/ext.c +0 -0

f5133249f..6a579babd 100644

kernel/sched/ext.c +0 -0
@@ -3262,7 +3262,7 @@ static void put_prev_task_scx(struct rq *rq, struct

task_struct *p,

Quoted 11 lines
kernel/sched/ext.c +2 -2
 		 * ops.enqueue() that @p is the only one available for this cpu,
 		 * which should trigger an explicit follow-up scheduling event.
 		 */
-		if (sched_class_above(&ext_sched_class, next->sched_class)) {
+		if (next && sched_class_above(&ext_sched_class, next->sched_class))
+{
 			WARN_ON_ONCE(!static_branch_unlikely(&scx_ops_enq_last));
 			do_enqueue_task(rq, p, SCX_ENQ_LAST, -1);
 		} else {
--

2.17.1

#3 lore raw

Hi liuwenfang,

Quoted 5 lines · liuwenfang

Thanks for your feedback.
This is triggered in kernel modules developed In the mobile scenario:
tasks on rq are migrated while the current cpu should be halted for power saving policy. Its migration logic:
drain_rq_cpu_stop -- migrate_all_tasks :

Quoted 7 lines
for (;;) {
	if (rq->nr_running == 1)
		break;
	for_each_class(class) {
		next = class->pick_next_task(rq);
		if (next) {
			next->sched_class->put_prev_task(rq, next, NULL);

Can you do something like this instead?

  next->sched_class->put_prev_task(rq, next, next);

this should give you the same behavior you're relying on, without requiring the extra check in ext.c.

Quoted 10 lines
			break;
		}
	}
	if (is_idle_task(next))
		break;
	dest_cpu = select_task_rq(next...);
	move_queued_task(rq, rf, next, dest_cpu);
	...
}
Quoted 2 lines

put_prev_task in this function is selected to update util and statistics info for each runnable tasks, here they are not dequeued yet.

I see, so it's an optimization/workaround to update utilization info without fully dequeuing the tasks. Is this out-of-tree code, I guess?

When you say the CPU is halted, we're not talking about CPU hotplugging, right? We're just migrating tasks off the CPU?

Also, if you're running sched_ext there are ways to exclude certain CPUs at the scheduler's level via ops.select_cpu() / ops.dispatch(). Do you think this could be a viable solution for your specific use case?

Thanks,
-Andrea

PS https://subspace.kernel.org/etiquette.html#do-not-top-post-when-replying

Quoted 40 lines

Best regards,

Quoted 37 lines · liuwenfang
Quoted 2 lines

As put_prev_task can be used in other kernel modules which can lead to a NULL pointer. Fix this by checking for a valid next.

Actually, put_prev_task() should be used only within kernel/sched/ and, in theory, you should have done a dequeue_task() before put_prev_task() in this scenario, so SCX_TASK_QUEUED shouldn't be set in p->scx.flags.

The change might still make sense, but can you clarify how you triggered the NULL pointer dereference?

Thanks,
-Andrea

kernel/sched/ext.c static void put_prev_task_scx(struct rq *rq, struct

Quoted 11 lines
---
 kernel/sched/ext.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/ext.c index renamed from kernel/sched/ext.c +0 -0

f5133249f..6a579babd 100644

kernel/sched/ext.c +0 -0
@@ -3262,7 +3262,7 @@ static void put_prev_task_scx(struct rq *rq, struct

task_struct *p,

Quoted 11 lines
kernel/sched/ext.c +2 -2
 		 * ops.enqueue() that @p is the only one available for this cpu,
 		 * which should trigger an explicit follow-up scheduling event.
 		 */
-		if (sched_class_above(&ext_sched_class, next->sched_class)) {
+		if (next && sched_class_above(&ext_sched_class, next->sched_class))
+{
 			WARN_ON_ONCE(!static_branch_unlikely(&scx_ops_enq_last));
 			do_enqueue_task(rq, p, SCX_ENQ_LAST, -1);
 		} else {
--

2.17.1

author
#4 lore raw
Quoted 18 lines

Hi liuwenfang,

Quoted 3 lines · liuwenfang

Thanks for your feedback.
This is triggered in kernel modules developed In the mobile scenario:
tasks on rq are migrated while the current cpu should be halted for power

saving policy.

Quoted 9 lines

Its migration logic:
drain_rq_cpu_stop -- migrate_all_tasks :

for (;;) {
	if (rq->nr_running == 1)
		break;
	for_each_class(class) {
		next = class->pick_next_task(rq);
		if (next) {
			next->sched_class->put_prev_task(rq, next, NULL);
Quoted 6 lines

Can you do something like this instead?

  next->sched_class->put_prev_task(rq, next, next);

this should give you the same behavior you're relying on, without requiring the extra check in ext.c.

Yes, this can help me.

Quoted 11 lines
Quoted 11 lines
			break;
		}
	}
	if (is_idle_task(next))
		break;
	dest_cpu = select_task_rq(next...);
	move_queued_task(rq, rf, next, dest_cpu);
	...
}
Quoted 6 lines
Quoted 2 lines

put_prev_task in this function is selected to update util and statistics info for each runnable tasks, here they are not dequeued yet.

I see, so it's an optimization/workaround to update utilization info without fully dequeuing the tasks. Is this out-of-tree code, I guess?

Yes, this is out-of-tree code.

Quoted 3 lines

When you say the CPU is halted, we're not talking about CPU hotplugging, right? We're just migrating tasks off the CPU?

Yes, this is one power-saving method to control tasks placement by bypassing Certain CPUs in software, simply letting the halted CPU enter an idle state. It replaces CPU hotplugging to reduce costs in mobile gaming scenario.

Quoted 4 lines

Also, if you're running sched_ext there are ways to exclude certain CPUs at the scheduler's level via ops.select_cpu() / ops.dispatch(). Do you think this could be a viable solution for your specific use case?

Yes, Thanks very much, This is exactly the approach we have implemented.

Best regards

Quoted 48 lines

Thanks,
-Andrea

PS https://subspace.kernel.org/etiquette.html#do-not-top-post-when-replying

Quoted 10 lines

Best regards,

Quoted 7 lines · liuwenfang
Quoted 2 lines

As put_prev_task can be used in other kernel modules which can lead to a NULL pointer. Fix this by checking for a valid next.

Actually, put_prev_task() should be used only within kernel/sched/ and, in theory, you should have done a dequeue_task() before put_prev_task() in this scenario, so SCX_TASK_QUEUED shouldn't be set in

p->scx.flags.

Quoted 20 lines
Quoted 20 lines

The change might still make sense, but can you clarify how you triggered the NULL pointer dereference?

Thanks,
-Andrea

Quoted 11 lines
---
 kernel/sched/ext.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/ext.c index renamed from kernel/sched/ext.c +0 -0

f5133249f..6a579babd 100644

kernel/sched/ext.c +0 -0
@@ -3262,7 +3262,7 @@ static void put_prev_task_scx(struct rq *rq,
Quoted 1 line

struct

task_struct *p,

Quoted 7 lines
Quoted 7 lines

kernel/sched/ext.c static void put_prev_task_scx(struct rq *rq, struct task_struct *p,

Quoted 7 lines
kernel/sched/ext.c +2 -1
 		 * ops.enqueue() that @p is the only one available for this cpu,
 		 * which should trigger an explicit follow-up scheduling event.
 		 */
-		if (sched_class_above(&ext_sched_class, next->sched_class)) {
+		if (next && sched_class_above(&ext_sched_class,
+next->sched_class)) {
	WARN_ON_ONCE(!static_branch_unlikely(&scx_ops_enq_last));
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
kernel/sched/ext.c +0 -1
 			do_enqueue_task(rq, p, SCX_ENQ_LAST, -1);
 		} else {
--

2.17.1