Skip to contentSkip to conversation

sched_ext: Validate prev_cpu in scx_bpf_select_cpu_dfl()

PATCH Superseded Andrea Righi 2 participants 3 messages Mar 2, 2025 – Mar 3, 2025 Scheduler

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched_ext: Validate prev_cpu in scx_bpf_select_cpu_dfl()

If a BPF scheduler provides an invalid CPU (outside the nr_cpu_ids range) as prev_cpu to scx_bpf_select_cpu_dfl() it can cause a kernel crash.

To prevent this, validate prev_cpu in scx_bpf_select_cpu_dfl() and trigger an scx error if an invalid CPU is specified.

Fixes f0e1a0643a59b ("sched_ext: Implement BPF extensible scheduler class")
Cc [email protected] # v6.12+
---
 kernel/sched/ext.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/sched/ext.c +5 -0
index 0f1da199cfc7c..88b2ea58ff942 100644
@@ -6422,6 +6422,11 @@ static bool check_builtin_idle_enabled(void)
 __bpf_kfunc s32 scx_bpf_select_cpu_dfl(struct task_struct *p, s32 prev_cpu,
 				       u64 wake_flags, bool *is_idle)
 {
+	if (!ops_cpu_valid(prev_cpu, NULL)) {
+		prev_cpu = cpumask_any(p->cpus_ptr);
+		goto prev_cpu;
+	}
+
 	if (!check_builtin_idle_enabled())
 		goto prev_cpu;
 
Signature · 1 line
2.48.1
#1 lore raw

Hello,

Quoted 19 lines · Andrea Righi

If a BPF scheduler provides an invalid CPU (outside the nr_cpu_ids range) as prev_cpu to scx_bpf_select_cpu_dfl() it can cause a kernel crash.

To prevent this, validate prev_cpu in scx_bpf_select_cpu_dfl() and trigger an scx error if an invalid CPU is specified.

Fixes f0e1a0643a59b ("sched_ext: Implement BPF extensible scheduler class")
Cc [email protected] # v6.12+
---
 kernel/sched/ext.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/sched/ext.c +0 -0
index 0f1da199cfc7c..88b2ea58ff942 100644

kernel/sched/ext.c static bool check_builtin_idle_enabled(void) · line 6427

Quoted 6 lines
kernel/sched/ext.c +3 -0
@@ -6422,6 +6422,11 @@ static bool check_builtin_idle_enabled(void)
 __bpf_kfunc s32 scx_bpf_select_cpu_dfl(struct task_struct *p, s32 prev_cpu,
 				       u64 wake_flags, bool *is_idle)
 {
+	if (!ops_cpu_valid(prev_cpu, NULL)) {
+		prev_cpu = cpumask_any(p->cpus_ptr);
+		goto prev_cpu;

I'd just return the invalid prev_cpu. The scheduler is getting aborted anyway.

Thanks.

Signature · 1 line
tejun
#2 lore raw
Quoted 32 lines · Tejun Heo

Hello,

Quoted 19 lines · Andrea Righi

If a BPF scheduler provides an invalid CPU (outside the nr_cpu_ids range) as prev_cpu to scx_bpf_select_cpu_dfl() it can cause a kernel crash.

To prevent this, validate prev_cpu in scx_bpf_select_cpu_dfl() and trigger an scx error if an invalid CPU is specified.

Fixes f0e1a0643a59b ("sched_ext: Implement BPF extensible scheduler class")
Cc [email protected] # v6.12+
---
 kernel/sched/ext.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/sched/ext.c +0 -0
index 0f1da199cfc7c..88b2ea58ff942 100644

kernel/sched/ext.c static bool check_builtin_idle_enabled(void) · line 6427

Quoted 6 lines
kernel/sched/ext.c +3 -0
@@ -6422,6 +6422,11 @@ static bool check_builtin_idle_enabled(void)
 __bpf_kfunc s32 scx_bpf_select_cpu_dfl(struct task_struct *p, s32 prev_cpu,
 				       u64 wake_flags, bool *is_idle)
 {
+	if (!ops_cpu_valid(prev_cpu, NULL)) {
+		prev_cpu = cpumask_any(p->cpus_ptr);
+		goto prev_cpu;

I'd just return the invalid prev_cpu. The scheduler is getting aborted anyway.

Quoted 1 line

Thanks.

Yeah, makes sense. I'll send a new patch soon.

Thanks,
-Andrea