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
Andrea Righi
reviewerRe: [PATCH 1/2] sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs
Hi Kuba,
Quoted 13 lines · Kuba Piecuch
scx_bpf_dsq_peek()andscx_bpf_tid_to_task()callrcu_dereference()without takingrcu_read_lock()and rely onKF_RCU_PROTECTEDinstead. However, the verifier treats every non-sleepable program as being in an RCU read-side section, includingbpf_timercallbacks.bpf_timer_cb()runs those from hrtimer softirq withoutrcu_read_lock(), so calling either kfunc from abpf_timercallback triggers:WARNING: suspicious RCU usage
kernel/sched/ext/ext.c:11170suspiciousrcu_dereference_check()usage!
...scx_bpf_dsq_peek+0x7d/0xd0 bpf_timer_cb+0xf9/0x130
Quoted 6 lines
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()andscx_bpf_cid_curr()already do.
Can we express the existing protection in the dereference checks instead of adding an RCU lock/unlock pair to every call?
Quoted 13 lines
Fixes 44f5c8ec5b9a ("sched_ext: Add lockless peek operation for DSQs")Fixes 41e3312861ea ("sched_ext: add p->scx.tid and SCX_OPS_TID_TO_TASK lookup")Signed-off-by Kuba Piecuch <[email protected]>--- kernel/sched/ext/ext.c | 4 ++++ 1 file changed, 4 insertions(+)kernel/sched/ext/ext.c +0
-0
kernel/sched/ext/ext.c __bpf_kfunc struct task_struct *scx_bpf_dsq_peek(u64 dsq_id, · line 11159
Quoted 8 lines
kernel/sched/ext/ext.c +2
-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 ;
Can we use rcu_dereference_all(dsq->first_task) for the return instead?
That accepts explicit RCU readers and the implicit protection provided by disabled preemption, softirqs or interrupts, while retaining the pointer-access ordering and lockdep checking.
kernel/sched/ext/ext.c __bpf_kfunc struct task_struct *scx_bpf_tid_to_task(u64 tid) · line 11965
Quoted 8 lines
kernel/sched/ext/ext.c +2
-0@@ -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);
Likewise, changing this to rcu_dereference_all(scx_root) should be enough.
Thanks,
-Andrea
Re: [PATCH 1/2] sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs
Hi Andrea,
Quoted 2 lines · Andrea Righi
Can we express the existing protection in the dereference checks instead of adding an RCU lock/unlock pair to every call?
...
Quoted 10 lines
Can we use
rcu_dereference_all(dsq->first_task)for the return instead?That accepts explicit RCU readers and the implicit protection provided by disabled preemption, softirqs or interrupts, while retaining the pointer-access ordering and lockdep checking.
Quoted 4 lines
kernel/sched/ext/ext.c +0
-0@@ -11957,6 +11959,8 @@ __bpf_kfunc struct task_struct *scx_bpf_tid_to_task(u64 tid) {struct sched_ext_entity *scx;
Quoted 7 lines
kernel/sched/ext/ext.c __bpf_kfunc struct task_struct *scx_bpf_tid_to_task(u64 tid) · line 11965
Quoted 4 lines
kernel/sched/ext/ext.c +2
-0+guard (rcu)(); +if (!scx_tid_to_task_enabled ()) {struct scx_sched *sch =rcu_dereference (scx_root);Likewise, changing this to
rcu_dereference_all(scx_root)should be enough.
Agreed, will switch both to rcu_dereference_all() in v2, and convert scx_bpf_cpu_curr() and scx_bpf_cid_curr() to match.
Thanks,
Kuba
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
Andrea Righi
reviewerRe: [PATCH 2/2] sched_ext: Take rcu_read_lock() explicitly in sub-cap kfuncs
Hi Kuba,
Quoted 6 lines · Kuba Piecuch
scx_bpf_sub_grant(),scx_bpf_sub_revoke()andscx_bpf_sub_caps()look up scheds viascx_prog_sched()andscx_find_sub_sched(), read the cid tables and, in revoke, walk ->children while holding only guard(irqsave)(). They can be called withoutrcu_read_lock(), e.g. from sleepable ops orbpf_timercallbacks.
Quoted 7 lines
This works only because IRQs are disabled and none of the RCU checks on these paths require
rcu_read_lock_held(). A plainrcu_dereference()added to these paths would trigger a lockdep splat when called withoutrcu_read_lock().Take guard(rcu)() as
scx_bpf_sub_dispatch()andscx_bpf_sub_kill_bstr()already do. No functional change.
I'd drop this patch. The IRQ-disabled region already provides the required lifetime protection and the existing checks recognize it.
If a future change introduces another dereference here, it should use the check appropriate to that protection, such as rcu_dereference_all().
I think we don't want to introduce additional RCU read-side bookkeeping on every call to accommodate that possibility.
Thanks,
-Andrea
Quoted 38 lines
Signed-off-by Kuba Piecuch <[email protected]>--- 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
Re: [PATCH 2/2] sched_ext: Take rcu_read_lock() explicitly in sub-cap kfuncs
Hi Andrea,
Quoted 8 lines · Andrea Righi
I'd drop this patch. The IRQ-disabled region already provides the required lifetime protection and the existing checks recognize it.
If a future change introduces another dereference here, it should use the check appropriate to that protection, such as
rcu_dereference_all().I think we don't want to introduce additional RCU read-side bookkeeping on every call to accommodate that possibility.
Makes sense, especially with patch 1 switching to rcu_dereference_all() as well. Will drop this patch in v2.
Thanks,
Kuba