[PATCH v2 sched_ext/for-6.16] sched_ext: Add RCU protection to scx_root in DSQ iterator

Andrea Righi posted 1 patch 7 months, 2 weeks ago
kernel/sched/ext.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH v2 sched_ext/for-6.16] sched_ext: Add RCU protection to scx_root in DSQ iterator
Posted by Andrea Righi 7 months, 2 weeks ago
Using a DSQ iterators from a timer callback can trigger the following
lockdep splat when accessing scx_root:

 =============================
 WARNING: suspicious RCU usage
 6.14.0-virtme #1 Not tainted
 -----------------------------
 kernel/sched/ext.c:6907 suspicious rcu_dereference_check() usage!

 other info that might help us debug this:

 rcu_scheduler_active = 2, debug_locks = 1
 no locks held by swapper/0/0.

 stack backtrace:
 CPU: 0 UID: 0 PID: 0 Comm: swapper/0 Not tainted 6.14.0-virtme #1 PREEMPT(full)
 Sched_ext: tickless (enabled+all)
 Call Trace:
 <IRQ>
 dump_stack_lvl+0x6f/0xb0
 lockdep_rcu_suspicious.cold+0x4e/0xa3
 bpf_iter_scx_dsq_new+0xb1/0xd0
 bpf_prog_63f4fd1bccc101e7_dispatch_cpu+0x3e/0x156
 bpf_prog_8320d4217989178c_dispatch_all_cpus+0x153/0x1b6
 bpf_prog_97f847d871513f95_sched_timerfn+0x4c/0x264
 ? hrtimer_run_softirq+0x4f/0xd0
 bpf_timer_cb+0x7a/0x140
 __hrtimer_run_queues+0x1f9/0x3a0
 hrtimer_run_softirq+0x8c/0xd0
 handle_softirqs+0xd3/0x3d0
 __irq_exit_rcu+0x9a/0xc0
 irq_exit_rcu+0xe/0x20
 sysvec_apic_timer_interrupt+0x73/0x80

Add a proper dereference check to explicitly validate RCU-safe access to
scx_root from rcu_read_lock() contexts and also from contexts that hold
rcu_read_lock_bh(), such as timer callbacks.

Fixes: cdf5a6faa8cf0 ("sched_ext: Move dsq_hash into scx_sched")
Signed-off-by: Andrea Righi <arighi@nvidia.com>
---
 kernel/sched/ext.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Changes in v2:
 - check rcu_read_lock_bh_held() instead of adding rcu_read_lock/unlock()
 - link to v1: https://lore.kernel.org/all/20250430090930.355732-1-arighi@nvidia.com/

diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
index 1ee5a0088b1a1..00e18eb072bf8 100644
--- a/kernel/sched/ext.c
+++ b/kernel/sched/ext.c
@@ -6913,7 +6913,7 @@ __bpf_kfunc int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id,
 	BUILD_BUG_ON(__alignof__(struct bpf_iter_scx_dsq_kern) !=
 		     __alignof__(struct bpf_iter_scx_dsq));
 
-	sch = rcu_dereference(scx_root);
+	sch = rcu_dereference_check(scx_root, rcu_read_lock_bh_held());
 	if (!sch)
 		return -ENODEV;
 
-- 
2.49.0
Re: [PATCH v2 sched_ext/for-6.16] sched_ext: Add RCU protection to scx_root in DSQ iterator
Posted by Tejun Heo 7 months, 2 weeks ago
On Wed, Apr 30, 2025 at 10:21:56PM +0200, Andrea Righi wrote:
> Using a DSQ iterators from a timer callback can trigger the following
> lockdep splat when accessing scx_root:
> 
>  =============================
>  WARNING: suspicious RCU usage
>  6.14.0-virtme #1 Not tainted
>  -----------------------------
>  kernel/sched/ext.c:6907 suspicious rcu_dereference_check() usage!
> 
>  other info that might help us debug this:
> 
>  rcu_scheduler_active = 2, debug_locks = 1
>  no locks held by swapper/0/0.
> 
>  stack backtrace:
>  CPU: 0 UID: 0 PID: 0 Comm: swapper/0 Not tainted 6.14.0-virtme #1 PREEMPT(full)
>  Sched_ext: tickless (enabled+all)
>  Call Trace:
>  <IRQ>
>  dump_stack_lvl+0x6f/0xb0
>  lockdep_rcu_suspicious.cold+0x4e/0xa3
>  bpf_iter_scx_dsq_new+0xb1/0xd0
>  bpf_prog_63f4fd1bccc101e7_dispatch_cpu+0x3e/0x156
>  bpf_prog_8320d4217989178c_dispatch_all_cpus+0x153/0x1b6
>  bpf_prog_97f847d871513f95_sched_timerfn+0x4c/0x264
>  ? hrtimer_run_softirq+0x4f/0xd0
>  bpf_timer_cb+0x7a/0x140
>  __hrtimer_run_queues+0x1f9/0x3a0
>  hrtimer_run_softirq+0x8c/0xd0
>  handle_softirqs+0xd3/0x3d0
>  __irq_exit_rcu+0x9a/0xc0
>  irq_exit_rcu+0xe/0x20
>  sysvec_apic_timer_interrupt+0x73/0x80
> 
> Add a proper dereference check to explicitly validate RCU-safe access to
> scx_root from rcu_read_lock() contexts and also from contexts that hold
> rcu_read_lock_bh(), such as timer callbacks.
> 
> Fixes: cdf5a6faa8cf0 ("sched_ext: Move dsq_hash into scx_sched")
> Signed-off-by: Andrea Righi <arighi@nvidia.com>

Applied to sched_ext/for-6.16.

Thanks.

-- 
tejun