[PATCH] sched_ext: Verify RCU protection in scx_bpf_cpu_curr()

Andrea Righi posted 1 patch 1 week, 1 day ago
kernel/sched/ext.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] sched_ext: Verify RCU protection in scx_bpf_cpu_curr()
Posted by Andrea Righi 1 week, 1 day ago
scx_bpf_cpu_curr() has been introduced to retrieve the current task of a
given runqueue, allowing schedulers to interact with that task.

The kfunc assumes that it is always called in an RCU context, but this
is not always guaranteed and some BPF schedulers can trigger the
following warning:

  WARNING: suspicious RCU usage
  sched_ext: BPF scheduler "cosmos_1.0.2_gd0e71ca_x86_64_unknown_linux_gnu_debug" enabled
  6.17.0-rc1 #1-NixOS Not tainted
  -----------------------------
  kernel/sched/ext.c:6415 suspicious rcu_dereference_check() usage!
  ...
 Call Trace:
  <IRQ>
  dump_stack_lvl+0x6f/0xb0
  lockdep_rcu_suspicious.cold+0x4e/0x96
  scx_bpf_cpu_curr+0x7e/0x80
  bpf_prog_c68b2b6b6b1b0ff8_sched_timerfn+0xce/0x1dc
  bpf_timer_cb+0x7b/0x130
  __hrtimer_run_queues+0x1ea/0x380
  hrtimer_run_softirq+0x8c/0xd0
  handle_softirqs+0xc9/0x3b0
  __irq_exit_rcu+0x96/0xc0
  irq_exit_rcu+0xe/0x20
  sysvec_apic_timer_interrupt+0x73/0x80
  </IRQ>
  <TASK>

To address this, mark the kfunc with KF_RCU_PROTECTED, so the verifier
can enforce its usage only inside RCU-protected sections.

Note: this also requires commit 1512231b6cc86 ("bpf: Enforce RCU protection
for KF_RCU_PROTECTED"), currently in bpf-next, to enforce the proper
KF_RCU_PROTECTED.

Fixes: 20b158094a1ad ("sched_ext: Introduce scx_bpf_cpu_curr()")
Cc: Christian Loehle <christian.loehle@arm.com>
Cc: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Andrea Righi <arighi@nvidia.com>
---
 kernel/sched/ext.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
index 37d9eff3fab5b..838bdc09baa1f 100644
--- a/kernel/sched/ext.c
+++ b/kernel/sched/ext.c
@@ -6576,7 +6576,7 @@ BTF_ID_FLAGS(func, scx_bpf_task_running, KF_RCU)
 BTF_ID_FLAGS(func, scx_bpf_task_cpu, KF_RCU)
 BTF_ID_FLAGS(func, scx_bpf_cpu_rq)
 BTF_ID_FLAGS(func, scx_bpf_locked_rq, KF_RET_NULL)
-BTF_ID_FLAGS(func, scx_bpf_cpu_curr, KF_RET_NULL | KF_RCU)
+BTF_ID_FLAGS(func, scx_bpf_cpu_curr, KF_RET_NULL | KF_RCU_PROTECTED)
 #ifdef CONFIG_CGROUP_SCHED
 BTF_ID_FLAGS(func, scx_bpf_task_cgroup, KF_RCU | KF_ACQUIRE)
 #endif
-- 
2.51.0
Re: [PATCH] sched_ext: Verify RCU protection in scx_bpf_cpu_curr()
Posted by Tejun Heo 1 week, 1 day ago
> Andrea Righi (1):
>   sched_ext: Verify RCU protection in scx_bpf_cpu_curr()

Applied to sched_ext/for-6.18.

Thanks.

--
tejun
Re: [PATCH] sched_ext: Verify RCU protection in scx_bpf_cpu_curr()
Posted by Kumar Kartikeya Dwivedi 1 week, 1 day ago
On Tue, 23 Sept 2025 at 11:16, Andrea Righi <arighi@nvidia.com> wrote:
>
> scx_bpf_cpu_curr() has been introduced to retrieve the current task of a
> given runqueue, allowing schedulers to interact with that task.
>
> The kfunc assumes that it is always called in an RCU context, but this
> is not always guaranteed and some BPF schedulers can trigger the
> following warning:
>
>   WARNING: suspicious RCU usage
>   sched_ext: BPF scheduler "cosmos_1.0.2_gd0e71ca_x86_64_unknown_linux_gnu_debug" enabled
>   6.17.0-rc1 #1-NixOS Not tainted
>   -----------------------------
>   kernel/sched/ext.c:6415 suspicious rcu_dereference_check() usage!
>   ...
>  Call Trace:
>   <IRQ>
>   dump_stack_lvl+0x6f/0xb0
>   lockdep_rcu_suspicious.cold+0x4e/0x96
>   scx_bpf_cpu_curr+0x7e/0x80
>   bpf_prog_c68b2b6b6b1b0ff8_sched_timerfn+0xce/0x1dc
>   bpf_timer_cb+0x7b/0x130
>   __hrtimer_run_queues+0x1ea/0x380
>   hrtimer_run_softirq+0x8c/0xd0
>   handle_softirqs+0xc9/0x3b0
>   __irq_exit_rcu+0x96/0xc0
>   irq_exit_rcu+0xe/0x20
>   sysvec_apic_timer_interrupt+0x73/0x80
>   </IRQ>
>   <TASK>
>
> To address this, mark the kfunc with KF_RCU_PROTECTED, so the verifier
> can enforce its usage only inside RCU-protected sections.
>
> Note: this also requires commit 1512231b6cc86 ("bpf: Enforce RCU protection
> for KF_RCU_PROTECTED"), currently in bpf-next, to enforce the proper
> KF_RCU_PROTECTED.
>
> Fixes: 20b158094a1ad ("sched_ext: Introduce scx_bpf_cpu_curr()")
> Cc: Christian Loehle <christian.loehle@arm.com>
> Cc: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> Signed-off-by: Andrea Righi <arighi@nvidia.com>
> ---

Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>