* [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Fix and tighten RCU usage in kfuncs
@ 2026-11-23 15:00 Kuba Piecuch
2026-11-23 15:00 ` [PATCH 1/2] sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs Kuba Piecuch
2026-11-23 15:00 ` [PATCH 2/2] sched_ext: Take rcu_read_lock() explicitly in sub-cap kfuncs Kuba Piecuch
0 siblings, 2 replies; 7+ messages in thread
From: Kuba Piecuch @ 2026-11-23 15:00 UTC (permalink / raw)
To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min
Cc: Kuba Piecuch, Ryan Newton, sched-ext, linux-kernel
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() == 0
With 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
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH 1/2] sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs 2026-11-23 15:00 [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Fix and tighten RCU usage in kfuncs Kuba Piecuch @ 2026-11-23 15:00 ` Kuba Piecuch 2026-11-24 8:00 ` Andrea Righi 2026-11-23 15:00 ` [PATCH 2/2] sched_ext: Take rcu_read_lock() explicitly in sub-cap kfuncs Kuba Piecuch 1 sibling, 1 reply; 7+ messages in thread From: Kuba Piecuch @ 2026-11-23 15:00 UTC (permalink / raw) To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min Cc: Kuba Piecuch, Ryan Newton, sched-ext, linux-kernel 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. 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 <jpiecuch@google.com> --- kernel/sched/ext/ext.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c index e8c30a21e03e..2b9324c31de9 100644 --- a/kernel/sched/ext/ext.c +++ b/kernel/sched/ext/ext.c @@ -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); -- 2.56.1.750.gf70dc682cf-goog ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs 2026-11-23 15:00 ` [PATCH 1/2] sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs Kuba Piecuch @ 2026-11-24 8:00 ` Andrea Righi 2026-11-24 10:29 ` Kuba Piecuch 0 siblings, 1 reply; 7+ messages in thread From: Andrea Righi @ 2026-11-24 8:00 UTC (permalink / raw) To: Kuba Piecuch Cc: Tejun Heo, David Vernet, Changwoo Min, Ryan Newton, sched-ext, linux-kernel Hi Kuba, On Mon, Nov 23, 2026 at 03:00:37PM +0000, Kuba Piecuch wrote: > 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. Can we express the existing protection in the dereference checks instead of adding an RCU lock/unlock pair to every call? > > 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 <jpiecuch@google.com> > --- > kernel/sched/ext/ext.c | 4 ++++ > 1 file changed, 4 insertions(+) > > diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c > index e8c30a21e03e..2b9324c31de9 100644 > --- a/kernel/sched/ext/ext.c > +++ b/kernel/sched/ext/ext.c > @@ -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. > @@ -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 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs 2026-11-24 8:00 ` Andrea Righi @ 2026-11-24 10:29 ` Kuba Piecuch 0 siblings, 0 replies; 7+ messages in thread From: Kuba Piecuch @ 2026-11-24 10:29 UTC (permalink / raw) To: Andrea Righi Cc: Tejun Heo, David Vernet, Changwoo Min, Ryan Newton, sched-ext, linux-kernel Hi Andrea, On Tue, 24 Nov 2026 08:00, Andrea Righi wrote: > Can we express the existing protection in the dereference checks instead > of adding an RCU lock/unlock pair to every call? ... > 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. > >> @@ -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. 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 ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/2] sched_ext: Take rcu_read_lock() explicitly in sub-cap kfuncs 2026-11-23 15:00 [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Fix and tighten RCU usage in kfuncs Kuba Piecuch 2026-11-23 15:00 ` [PATCH 1/2] sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs Kuba Piecuch @ 2026-11-23 15:00 ` Kuba Piecuch 2026-11-24 8:02 ` Andrea Righi 1 sibling, 1 reply; 7+ messages in thread From: Kuba Piecuch @ 2026-11-23 15:00 UTC (permalink / raw) To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min Cc: Kuba Piecuch, Ryan Newton, sched-ext, linux-kernel scx_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. Signed-off-by: Kuba Piecuch <jpiecuch@google.com> --- kernel/sched/ext/sub.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kernel/sched/ext/sub.c b/kernel/sched/ext/sub.c index d2a20114eaa2..45aaa333f9c2 100644 --- a/kernel/sched/ext/sub.c +++ b/kernel/sched/ext/sub.c @@ -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); -- 2.56.1.750.gf70dc682cf-goog ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] sched_ext: Take rcu_read_lock() explicitly in sub-cap kfuncs 2026-11-23 15:00 ` [PATCH 2/2] sched_ext: Take rcu_read_lock() explicitly in sub-cap kfuncs Kuba Piecuch @ 2026-11-24 8:02 ` Andrea Righi 2026-11-24 10:29 ` Kuba Piecuch 0 siblings, 1 reply; 7+ messages in thread From: Andrea Righi @ 2026-11-24 8:02 UTC (permalink / raw) To: Kuba Piecuch Cc: Tejun Heo, David Vernet, Changwoo Min, Ryan Newton, sched-ext, linux-kernel Hi Kuba, On Mon, Nov 23, 2026 at 03:00:38PM +0000, Kuba Piecuch wrote: > scx_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. 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 > > Signed-off-by: Kuba Piecuch <jpiecuch@google.com> > --- > kernel/sched/ext/sub.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/kernel/sched/ext/sub.c b/kernel/sched/ext/sub.c > index d2a20114eaa2..45aaa333f9c2 100644 > --- a/kernel/sched/ext/sub.c > +++ b/kernel/sched/ext/sub.c > @@ -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); > -- > 2.56.1.750.gf70dc682cf-goog > > ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] sched_ext: Take rcu_read_lock() explicitly in sub-cap kfuncs 2026-11-24 8:02 ` Andrea Righi @ 2026-11-24 10:29 ` Kuba Piecuch 0 siblings, 0 replies; 7+ messages in thread From: Kuba Piecuch @ 2026-11-24 10:29 UTC (permalink / raw) To: Andrea Righi Cc: Tejun Heo, David Vernet, Changwoo Min, Ryan Newton, sched-ext, linux-kernel Hi Andrea, On Tue, 24 Nov 2026 08:02, Andrea Righi wrote: > 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 ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-24 10:29 UTC | newest] Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-11-23 15:00 [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Fix and tighten RCU usage in kfuncs Kuba Piecuch 2026-11-23 15:00 ` [PATCH 1/2] sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs Kuba Piecuch 2026-11-24 8:00 ` Andrea Righi 2026-11-24 10:29 ` Kuba Piecuch 2026-11-23 15:00 ` [PATCH 2/2] sched_ext: Take rcu_read_lock() explicitly in sub-cap kfuncs Kuba Piecuch 2026-11-24 8:02 ` Andrea Righi 2026-11-24 10:29 ` Kuba Piecuch
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®