sched_ext: Fix NULL dereference in scx_bpf_cpu_rq() warning
Subsystems (2)
Andrea Righi
reviewerPATCH 1/1 sched_ext: Fix NULL dereference in scx_bpf_cpu_rq() warning
When printing the deprecation warning for scx_bpf_cpu_rq(), we may hit a NULL pointer dereference if the kfunc is called before a BPF scheduler is fully attached, for example, when invoked from a BPF timer or during ops.init():
[ 50.752775] BUG: kernel NULL pointer dereference, address: 0000000000000331 ... [ 50.764205] RIP: 0010:scx_bpf_cpu_rq+0x30/0xa0 ... [ 50.787661] Call Trace: [ 50.788398] <TASK> [ 50.789061] bpf_prog_08f7fd2dcb187aaf_wakeup_timerfn+0x75/0x1a8 [ 50.792477] bpf_timer_cb+0x7e/0x140 [ 50.796003] hrtimer_run_softirq+0x91/0xe0 [ 50.796952] handle_softirqs+0xce/0x3c0 [ 50.799087] run_ksoftirqd+0x3e/0x70 [ 50.800197] smpboot_thread_fn+0x133/0x290 [ 50.802320] kthread+0x115/0x220 [ 50.804984] ret_from_fork+0x17a/0x1d0 [ 50.806920] ret_from_fork_asm+0x1a/0x30 [ 50.807799] </TASK>
Fix this by only printing the warning once the scheduler is fully registered.
--- kernel/sched/ext.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
kernel/sched/ext.c +5 -2
@@ -6362,17 +6362,20 @@ __bpf_kfunc s32 scx_bpf_task_cpu(const struct task_struct *p)*/ __bpf_kfunc struct rq *scx_bpf_cpu_rq(s32 cpu) { -struct scx_sched *sch = scx_root; +struct scx_sched *sch;if (!kf_cpu_valid (cpu,NULL ))return NULL ; -if (!sch->warned_deprecated_rq) { +rcu_read_lock (); + sch =rcu_dereference (scx_root); +if (sch && sch->warned_deprecated_rq) {printk_deferred (KERN_WARNING "sched_ext: %s() is deprecated; " "use scx_bpf_locked_rq() when holding rq lock " "or scx_bpf_cpu_curr() to read remote curr safely.\n" , __func__); sch->warned_deprecated_rq = true; } +rcu_read_unlock ();return cpu_rq (cpu); }
Signature · 1 line
2.51.0
Quoted 49 lines · Andrea Righi
When printing the deprecation warning for
scx_bpf_cpu_rq(), we may hit a NULL pointer dereference if the kfunc is called before a BPF scheduler is fully attached, for example, when invoked from a BPF timer or duringops.init():[ 50.752775] BUG: kernel NULL pointer dereference, address: 0000000000000331 ... [ 50.764205] RIP: 0010:scx_bpf_cpu_rq+0x30/0xa0 ... [ 50.787661] Call Trace: [ 50.788398] <TASK> [ 50.789061] bpf_prog_08f7fd2dcb187aaf_wakeup_timerfn+0x75/0x1a8 [ 50.792477] bpf_timer_cb+0x7e/0x140 [ 50.796003] hrtimer_run_softirq+0x91/0xe0 [ 50.796952] handle_softirqs+0xce/0x3c0 [ 50.799087] run_ksoftirqd+0x3e/0x70 [ 50.800197] smpboot_thread_fn+0x133/0x290 [ 50.802320] kthread+0x115/0x220 [ 50.804984] ret_from_fork+0x17a/0x1d0 [ 50.806920] ret_from_fork_asm+0x1a/0x30 [ 50.807799] </TASK>Fix this by only printing the warning once the scheduler is fully registered.
Fixes 5c48d88fe0049 ("sched_ext: deprecation warn for scx_bpf_cpu_rq()")Cc Christian Loehle <[email protected]>Signed-off-by Andrea Righi <[email protected]>--- kernel/sched/ext.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)kernel/sched/ext.c +3
-2@@ -6362,17 +6362,20 @@ __bpf_kfunc s32 scx_bpf_task_cpu(const struct task_struct *p)*/ __bpf_kfunc struct rq *scx_bpf_cpu_rq(s32 cpu) { -struct scx_sched *sch = scx_root; +struct scx_sched *sch;if (!kf_cpu_valid (cpu,NULL ))return NULL ; -if (!sch->warned_deprecated_rq) { +rcu_read_lock (); + sch =rcu_dereference (scx_root);
kernel/sched/ext.c __bpf_kfunc s32 scx_bpf_task_cpu(const struct task_struct *p) · line 6381
Quoted 10 lines
kernel/sched/ext.c +2
-0+if (sch && sch->warned_deprecated_rq) {printk_deferred (KERN_WARNING "sched_ext: %s() is deprecated; " "use scx_bpf_locked_rq() when holding rq lock " "or scx_bpf_cpu_curr() to read remote curr safely.\n" , __func__); sch->warned_deprecated_rq = true; } +rcu_read_unlock ();return cpu_rq (cpu); }
Ah yes of course.
I guess the other ones that can happen during the actual lifetime of the scheduler have a likely(sch) around it, which we could add here as well.
Tejun Heo
reviewerQuoted 40 lines · Andrea Righi
When printing the deprecation warning for
scx_bpf_cpu_rq(), we may hit a NULL pointer dereference if the kfunc is called before a BPF scheduler is fully attached, for example, when invoked from a BPF timer or duringops.init():[ 50.752775] BUG: kernel NULL pointer dereference, address: 0000000000000331 ... [ 50.764205] RIP: 0010:scx_bpf_cpu_rq+0x30/0xa0 ... [ 50.787661] Call Trace: [ 50.788398] <TASK> [ 50.789061] bpf_prog_08f7fd2dcb187aaf_wakeup_timerfn+0x75/0x1a8 [ 50.792477] bpf_timer_cb+0x7e/0x140 [ 50.796003] hrtimer_run_softirq+0x91/0xe0 [ 50.796952] handle_softirqs+0xce/0x3c0 [ 50.799087] run_ksoftirqd+0x3e/0x70 [ 50.800197] smpboot_thread_fn+0x133/0x290 [ 50.802320] kthread+0x115/0x220 [ 50.804984] ret_from_fork+0x17a/0x1d0 [ 50.806920] ret_from_fork_asm+0x1a/0x30 [ 50.807799] </TASK>Fix this by only printing the warning once the scheduler is fully registered.
Fixes 5c48d88fe0049 ("sched_ext: deprecation warn for scx_bpf_cpu_rq()")Cc Christian Loehle <[email protected]>Signed-off-by Andrea Righi <[email protected]>--- kernel/sched/ext.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)kernel/sched/ext.c +0
-0@@ -6362,17 +6362,20 @@ __bpf_kfunc s32 scx_bpf_task_cpu(const struct task_struct *p)*/ __bpf_kfunc struct rq *scx_bpf_cpu_rq(s32 cpu) {
kernel/sched/ext.c __bpf_kfunc s32 scx_bpf_task_cpu(const struct task_struct *p) · line 6372
Quoted 10 lines
kernel/sched/ext.c +4
-2-struct scx_sched *sch = scx_root; +struct scx_sched *sch;if (!kf_cpu_valid (cpu,NULL ))return NULL ; -if (!sch->warned_deprecated_rq) { +rcu_read_lock (); + sch =rcu_dereference (scx_root); +if (sch && sch->warned_deprecated_rq) {
This drops ! from the condition.
Thanks.
Signature · 1 line
tejun
Andrea Righi
reviewerQuoted 47 lines · Tejun Heo
Quoted 46 lines · Andrea Righi
When printing the deprecation warning for
scx_bpf_cpu_rq(), we may hit a NULL pointer dereference if the kfunc is called before a BPF scheduler is fully attached, for example, when invoked from a BPF timer or duringops.init():[ 50.752775] BUG: kernel NULL pointer dereference, address: 0000000000000331 ... [ 50.764205] RIP: 0010:scx_bpf_cpu_rq+0x30/0xa0 ... [ 50.787661] Call Trace: [ 50.788398] <TASK> [ 50.789061] bpf_prog_08f7fd2dcb187aaf_wakeup_timerfn+0x75/0x1a8 [ 50.792477] bpf_timer_cb+0x7e/0x140 [ 50.796003] hrtimer_run_softirq+0x91/0xe0 [ 50.796952] handle_softirqs+0xce/0x3c0 [ 50.799087] run_ksoftirqd+0x3e/0x70 [ 50.800197] smpboot_thread_fn+0x133/0x290 [ 50.802320] kthread+0x115/0x220 [ 50.804984] ret_from_fork+0x17a/0x1d0 [ 50.806920] ret_from_fork_asm+0x1a/0x30 [ 50.807799] </TASK>Fix this by only printing the warning once the scheduler is fully registered.
Fixes 5c48d88fe0049 ("sched_ext: deprecation warn for scx_bpf_cpu_rq()")Cc Christian Loehle <[email protected]>Signed-off-by Andrea Righi <[email protected]>--- kernel/sched/ext.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)kernel/sched/ext.c +1
-1@@ -6362,17 +6362,20 @@ __bpf_kfunc s32 scx_bpf_task_cpu(const struct task_struct *p)*/ __bpf_kfunc struct rq *scx_bpf_cpu_rq(s32 cpu) { -struct scx_sched *sch = scx_root; +struct scx_sched *sch;if (!kf_cpu_valid (cpu,NULL ))return NULL ;
Quoted 8 lines
kernel/sched/ext.c __bpf_kfunc s32 scx_bpf_task_cpu(const struct task_struct *p) · line 6372
Quoted 4 lines
kernel/sched/ext.c +3
-1-if (!sch->warned_deprecated_rq) { +rcu_read_lock (); + sch =rcu_dereference (scx_root); +if (sch && sch->warned_deprecated_rq) {This drops ! from the condition.
Thanks.
Oops, thanks for noticing it Tejun. I'll send a v2.
-Andrea