sched_ext: Fix and tighten RCU usage in kfuncs
Subsystems (2)
PATCH 1/1 sched_ext: Fix and tighten RCU usage in kfuncs
Several sched_ext kfuncs dereference RCU-protected state without taking rcu_read_lock() themselves. They depend on the caller's context instead, but that context does not always satisfy rcu_read_lock_held().
The verifier treats every non-sleepable program as being inside an RCU read-side section, and KF_RCU_PROTECTED only checks that. bpf_timer callbacks are non-sleepable, but bpf_timer_cb() calls them directly from hrtimer softirq, with no trampoline and so no rcu_read_lock():
hrtimer softirq
bpf_timer_cb()
timer callback (BPF)
scx_bpf_dsq_peek()
rcu_dereference(dsq->first_task) <- rcu_read_lock_held() == 0With CONFIG_PROVE_RCU=y this triggers:
WARNING: suspicious RCU usage
kernel/sched/ext/ext.c:11170 suspicious rcu_dereference_check() usage!
...
Call Trace: lockdep_rcu_suspicious+0x11a/0x160 scx_bpf_dsq_peek+0x7d/0xd0 bpf_timer_cb+0xf9/0x130
WARNING: suspicious RCU usage
kernel/sched/ext/ext.c:11961 suspicious rcu_dereference_check() usage!
...
Call Trace: lockdep_rcu_suspicious+0x11a/0x160 scx_bpf_tid_to_task+0x2ae/0x2e0 bpf_timer_cb+0xf9/0x130
A sleepable program inside bpf_preempt_disable() hits the same check. Both contexts are RCU readers, so these are lockdep false positives, not use-after-frees, but they make PROVE_RCU kernels noisy for any scheduler that peeks a DSQ or resolves a tid from a timer.
The sub-cap kfuncs (scx_bpf_sub_grant(), scx_bpf_sub_revoke() and scx_bpf_sub_caps()) are reachable from the same contexts. They avoid the warning only because they disable IRQs and every RCU check on their paths (rcu_dereference_all(), bpf_rcu_lock_held(), rcu_read_lock_any_held()) accepts that. In scx_qmap, for example, the round-robin bpf_timer callback calls scx_bpf_sub_revoke() and scx_bpf_sub_grant(), which look up the child sched in scx_sched_hash with rcu_read_lock_held() == 0.
Patch 1 adds guard(rcu)() to scx_bpf_dsq_peek() and scx_bpf_tid_to_task(), matching scx_bpf_cpu_curr() and scx_bpf_cid_curr(). This fixes both warnings.
Patch 2 adds guard(rcu)() to the three sub-cap kfuncs, matching scx_bpf_sub_dispatch() and scx_bpf_sub_kill_bstr(). This makes their RCU section explicit, so a future plain rcu_dereference() on those paths does not reintroduce the warning. No functional change.
The patches were written with the help of an AI coding assistant.
Kuba Piecuch (2):
sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs
sched_ext: Take rcu_read_lock() explicitly in sub-cap kfuncs
kernel/sched/ext/ext.c | 4 ++++ kernel/sched/ext/sub.c | 3 +++ 2 files changed, 7 insertions(+)
base-commit: 2f7928516314f37d7cb334675656b4412edc8be3
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH 1/2 sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs
scx_bpf_dsq_peek() and scx_bpf_tid_to_task() call rcu_dereference() without taking rcu_read_lock() and rely on KF_RCU_PROTECTED instead. However, the verifier treats every non-sleepable program as being in an RCU read-side section, including bpf_timer callbacks. bpf_timer_cb() runs those from hrtimer softirq without rcu_read_lock(), so calling either kfunc from a bpf_timer callback triggers:
WARNING: suspicious RCU usage
kernel/sched/ext/ext.c:11170 suspicious rcu_dereference_check() usage!
...
scx_bpf_dsq_peek+0x7d/0xd0 bpf_timer_cb+0xf9/0x130
The same happens from a sleepable program inside bpf_preempt_disable(). Both contexts are RCU readers, so this is a lockdep false positive, not a use-after-free.
Take guard(rcu)() as scx_bpf_cpu_curr() and scx_bpf_cid_curr() already do.
--- kernel/sched/ext/ext.c | 4 ++++ 1 file changed, 4 insertions(+)
kernel/sched/ext/ext.c +4 -0
@@ -11152,6 +11152,8 @@ __bpf_kfunc struct task_struct *scx_bpf_dsq_peek(u64 dsq_id,struct scx_sched *sch;struct scx_dispatch_q *dsq; +guard (rcu)(); + sch =scx_prog_sched (aux);if (unlikely (!sch))return NULL ;
@@ -11957,6 +11959,8 @@ __bpf_kfunc struct task_struct *scx_bpf_tid_to_task(u64 tid) {struct sched_ext_entity *scx; +guard (rcu)(); +if (!scx_tid_to_task_enabled ()) {struct scx_sched *sch =rcu_dereference (scx_root);
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH 2/2 sched_ext: Take rcu_read_lock() explicitly in sub-cap kfuncs
In reply to Kuba Piecuchscx_bpf_sub_grant(), scx_bpf_sub_revoke() and scx_bpf_sub_caps() look up scheds via scx_prog_sched() and scx_find_sub_sched(), read the cid tables and, in revoke, walk ->children while holding only guard(irqsave)(). They can be called without rcu_read_lock(), e.g. from sleepable ops or bpf_timer callbacks.
This works only because IRQs are disabled and none of the RCU checks on these paths require rcu_read_lock_held(). A plain rcu_dereference() added to these paths would trigger a lockdep splat when called without rcu_read_lock().
Take guard(rcu)() as scx_bpf_sub_dispatch() and scx_bpf_sub_kill_bstr() already do. No functional change.
--- kernel/sched/ext/sub.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/sched/ext/sub.c +3 -0
@@ -2870,6 +2870,7 @@ __bpf_kfunc s32 scx_bpf_sub_grant(u64 cgroup_id, u64 caps,LIST_HEAD (to_deliver);s32 si, ret; +guard (rcu)();guard (irqsave)(); ret =sub_cap_preamble (cgroup_id, caps, aux, &parent, &child);
@@ -2999,6 +3000,7 @@ __bpf_kfunc void scx_bpf_sub_revoke(u64 cgroup_id, u64 caps,LIST_HEAD (to_deliver);s32 si, ret; +guard (rcu)();guard (irqsave)();if (sub_cap_preamble (cgroup_id, caps, aux, &parent, &child))
@@ -3092,6 +3094,7 @@ __bpf_kfunc s32 scx_bpf_sub_caps(u64 cgroup_id, u64 caps, struct scx_cmask *out_struct scx_pshard **pshard;s32 si, ret; +guard (rcu)();guard (irqsave)(); sch =scx_prog_sched (aux);
Signature · 1 line
2.56.1.750.gf70dc682cf-goog