The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v2 sched_ext/for-7.4-fixes] sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs
@ 2026-11-24 10:31 Kuba Piecuch
  2026-11-24 11:31 ` Andrea Righi
  2026-11-24 14:18 ` Tejun Heo
  0 siblings, 2 replies; 3+ messages in thread
From: Kuba Piecuch @ 2026-11-24 10:31 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.

Every context accepted by KF_RCU_PROTECTED satisfies
rcu_read_lock_any_held(). Use rcu_dereference_all() in both kfuncs,
matching scx_prog_sched() and rhashtable_lookup(), and convert
scx_bpf_cpu_curr() and scx_bpf_cid_curr() from guard(rcu)() +
rcu_dereference() to rcu_dereference_all() as well to avoid redundant
per-call RCU read-side lock/unlock overhead.

This patch was written with the help of an AI coding assistant.

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>
---
Changes in v2:
- Use rcu_dereference_all() instead of guard(rcu)() in
  scx_bpf_dsq_peek() and scx_bpf_tid_to_task(), and convert
  scx_bpf_cpu_curr() and scx_bpf_cid_curr() to match (Andrea).
- Drop patch 2 ("sched_ext: Take rcu_read_lock() explicitly in sub-cap
  kfuncs") (Andrea).

v1: https://lore.kernel.org/all/20261123145927.577162-1-jpiecuch@google.com/

 kernel/sched/ext/ext.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index e8c30a21e03e..b8702b81782a 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -11167,7 +11167,7 @@ __bpf_kfunc struct task_struct *scx_bpf_dsq_peek(u64 dsq_id,
 		return NULL;
 	}
 
-	return rcu_dereference(dsq->first_task);
+	return rcu_dereference_all(dsq->first_task);
 }
 
 /**
@@ -11908,8 +11908,6 @@ __bpf_kfunc struct task_struct *scx_bpf_cpu_curr(s32 cpu, const struct bpf_prog_
 {
 	struct scx_sched *sch;
 
-	guard(rcu)();
-
 	sch = scx_prog_sched(aux);
 	if (unlikely(!sch))
 		return NULL;
@@ -11917,7 +11915,7 @@ __bpf_kfunc struct task_struct *scx_bpf_cpu_curr(s32 cpu, const struct bpf_prog_
 	if (!scx_cpu_valid(sch, cpu, NULL))
 		return NULL;
 
-	return rcu_dereference(cpu_rq(cpu)->donor);
+	return rcu_dereference_all(cpu_rq(cpu)->donor);
 }
 
 /**
@@ -11933,15 +11931,13 @@ __bpf_kfunc struct task_struct *scx_bpf_cid_curr(s32 cid, const struct bpf_prog_
 	struct scx_sched *sch;
 	s32 cpu;
 
-	guard(rcu)();
-
 	sch = scx_prog_sched(aux);
 	if (unlikely(!sch))
 		return NULL;
 	cpu = scx_cid_to_cpu(sch, cid);
 	if (cpu < 0)
 		return NULL;
-	return rcu_dereference(cpu_rq(cpu)->donor);
+	return rcu_dereference_all(cpu_rq(cpu)->donor);
 }
 
 /**
@@ -11958,7 +11954,7 @@ __bpf_kfunc struct task_struct *scx_bpf_tid_to_task(u64 tid)
 	struct sched_ext_entity *scx;
 
 	if (!scx_tid_to_task_enabled()) {
-		struct scx_sched *sch = rcu_dereference(scx_root);
+		struct scx_sched *sch = rcu_dereference_all(scx_root);
 
 		if (sch)
 			scx_error(sch, "scx_bpf_tid_to_task() called without SCX_OPS_TID_TO_TASK");

base-commit: 2f7928516314f37d7cb334675656b4412edc8be3
-- 
2.56.1.831.g720f3f5937-goog


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 sched_ext/for-7.4-fixes] sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs
  2026-11-24 10:31 [PATCH v2 sched_ext/for-7.4-fixes] sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs Kuba Piecuch
@ 2026-11-24 11:31 ` Andrea Righi
  2026-11-24 14:18 ` Tejun Heo
  1 sibling, 0 replies; 3+ messages in thread
From: Andrea Righi @ 2026-11-24 11:31 UTC (permalink / raw)
  To: Kuba Piecuch
  Cc: Tejun Heo, David Vernet, Changwoo Min, Ryan Newton, sched-ext,
	linux-kernel

On Tue, Nov 24, 2026 at 10:31:14AM +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.
> 
> Every context accepted by KF_RCU_PROTECTED satisfies
> rcu_read_lock_any_held(). Use rcu_dereference_all() in both kfuncs,
> matching scx_prog_sched() and rhashtable_lookup(), and convert
> scx_bpf_cpu_curr() and scx_bpf_cid_curr() from guard(rcu)() +
> rcu_dereference() to rcu_dereference_all() as well to avoid redundant
> per-call RCU read-side lock/unlock overhead.
> 
> This patch was written with the help of an AI coding assistant.
> 
> 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>

Looks good to me.

Reviewed-by: Andrea Righi <arighi@nvidia.com>

Thanks,
-Andrea

> ---
> Changes in v2:
> - Use rcu_dereference_all() instead of guard(rcu)() in
>   scx_bpf_dsq_peek() and scx_bpf_tid_to_task(), and convert
>   scx_bpf_cpu_curr() and scx_bpf_cid_curr() to match (Andrea).
> - Drop patch 2 ("sched_ext: Take rcu_read_lock() explicitly in sub-cap
>   kfuncs") (Andrea).
> 
> v1: https://lore.kernel.org/all/20261123145927.577162-1-jpiecuch@google.com/
> 
>  kernel/sched/ext/ext.c | 12 ++++--------
>  1 file changed, 4 insertions(+), 8 deletions(-)
> 
> diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
> index e8c30a21e03e..b8702b81782a 100644
> --- a/kernel/sched/ext/ext.c
> +++ b/kernel/sched/ext/ext.c
> @@ -11167,7 +11167,7 @@ __bpf_kfunc struct task_struct *scx_bpf_dsq_peek(u64 dsq_id,
>  		return NULL;
>  	}
>  
> -	return rcu_dereference(dsq->first_task);
> +	return rcu_dereference_all(dsq->first_task);
>  }
>  
>  /**
> @@ -11908,8 +11908,6 @@ __bpf_kfunc struct task_struct *scx_bpf_cpu_curr(s32 cpu, const struct bpf_prog_
>  {
>  	struct scx_sched *sch;
>  
> -	guard(rcu)();
> -
>  	sch = scx_prog_sched(aux);
>  	if (unlikely(!sch))
>  		return NULL;
> @@ -11917,7 +11915,7 @@ __bpf_kfunc struct task_struct *scx_bpf_cpu_curr(s32 cpu, const struct bpf_prog_
>  	if (!scx_cpu_valid(sch, cpu, NULL))
>  		return NULL;
>  
> -	return rcu_dereference(cpu_rq(cpu)->donor);
> +	return rcu_dereference_all(cpu_rq(cpu)->donor);
>  }
>  
>  /**
> @@ -11933,15 +11931,13 @@ __bpf_kfunc struct task_struct *scx_bpf_cid_curr(s32 cid, const struct bpf_prog_
>  	struct scx_sched *sch;
>  	s32 cpu;
>  
> -	guard(rcu)();
> -
>  	sch = scx_prog_sched(aux);
>  	if (unlikely(!sch))
>  		return NULL;
>  	cpu = scx_cid_to_cpu(sch, cid);
>  	if (cpu < 0)
>  		return NULL;
> -	return rcu_dereference(cpu_rq(cpu)->donor);
> +	return rcu_dereference_all(cpu_rq(cpu)->donor);
>  }
>  
>  /**
> @@ -11958,7 +11954,7 @@ __bpf_kfunc struct task_struct *scx_bpf_tid_to_task(u64 tid)
>  	struct sched_ext_entity *scx;
>  
>  	if (!scx_tid_to_task_enabled()) {
> -		struct scx_sched *sch = rcu_dereference(scx_root);
> +		struct scx_sched *sch = rcu_dereference_all(scx_root);
>  
>  		if (sch)
>  			scx_error(sch, "scx_bpf_tid_to_task() called without SCX_OPS_TID_TO_TASK");
> 
> base-commit: 2f7928516314f37d7cb334675656b4412edc8be3
> -- 
> 2.56.1.831.g720f3f5937-goog
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 sched_ext/for-7.4-fixes] sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs
  2026-11-24 10:31 [PATCH v2 sched_ext/for-7.4-fixes] sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs Kuba Piecuch
  2026-11-24 11:31 ` Andrea Righi
@ 2026-11-24 14:18 ` Tejun Heo
  1 sibling, 0 replies; 3+ messages in thread
From: Tejun Heo @ 2026-11-24 14:18 UTC (permalink / raw)
  To: Kuba Piecuch
  Cc: David Vernet, Andrea Righi, Changwoo Min, Ryan Newton, sched-ext,
	linux-kernel

Applied to sched_ext/for-7.4-fixes with the following tags added:

  Reviewed-by: Andrea Righi <arighi@nvidia.com>

Thanks.

--
tejun

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-24 14:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 10:31 [PATCH v2 sched_ext/for-7.4-fixes] sched_ext: Fix suspicious RCU usage in KF_RCU_PROTECTED kfuncs Kuba Piecuch
2026-11-24 11:31 ` Andrea Righi
2026-11-24 14:18 ` Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox