public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] sched_ext: idle: Recheck prev_cpu after narrowing allowed mask
@ 2026-04-30  9:27 David Carlier
  2026-05-02  5:44 ` Andrea Righi
  2026-05-04 21:05 ` Tejun Heo
  0 siblings, 2 replies; 3+ messages in thread
From: David Carlier @ 2026-04-30  9:27 UTC (permalink / raw)
  To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min, Ingo Molnar,
	Peter Zijlstra, Juri Lelli, Vincent Guittot, Dietmar Eggemann,
	Steven Rostedt, Ben Segall, Mel Gorman, Valentin Schneider,
	K Prateek Nayak, open list:SCHEDULER - SCHED_EXT,
	open list:SCHEDULER
  Cc: David Carlier, Claude

scx_select_cpu_dfl() narrows @allowed to @cpus_allowed & @p->cpus_ptr
when the BPF caller supplies a @cpus_allowed that differs from
@p->cpus_ptr and @p doesn't have full affinity. However,
@is_prev_allowed was computed against the original (wider)
@cpus_allowed, so the prev_cpu fast paths could pick a @prev_cpu that
is in @cpus_allowed but not in @p->cpus_ptr, violating the intended
invariant that the returned CPU is always usable by @p. The kernel
masks this via the SCX_EV_SELECT_CPU_FALLBACK fallback, but the
behavior contradicts the documented contract.

Move the @is_prev_allowed evaluation past the narrowing block so it
tests against the final @allowed mask.

Fixes: ee9a4e92799d ("sched_ext: idle: Properly handle invalid prev_cpu during idle selection")
Assisted-by: Claude <noreply@anthropic.com>
Signed-off-by: David Carlier <devnexen@gmail.com>
---
 kernel/sched/ext_idle.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/kernel/sched/ext_idle.c b/kernel/sched/ext_idle.c
index 41785f65bbb2..bbb845f36a0a 100644
--- a/kernel/sched/ext_idle.c
+++ b/kernel/sched/ext_idle.c
@@ -464,12 +464,6 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 
 	preempt_disable();
 
-	/*
-	 * Check whether @prev_cpu is still within the allowed set. If not,
-	 * we can still try selecting a nearby CPU.
-	 */
-	is_prev_allowed = cpumask_test_cpu(prev_cpu, allowed);
-
 	/*
 	 * Determine the subset of CPUs usable by @p within @cpus_allowed.
 	 */
@@ -486,6 +480,12 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 		}
 	}
 
+	/*
+	 * Check whether @prev_cpu is still within the allowed set. If not,
+	 * we can still try selecting a nearby CPU.
+	 */
+	is_prev_allowed = cpumask_test_cpu(prev_cpu, allowed);
+
 	/*
 	 * This is necessary to protect llc_cpus.
 	 */
-- 
2.53.0


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

* Re: [PATCH] sched_ext: idle: Recheck prev_cpu after narrowing allowed mask
  2026-04-30  9:27 [PATCH] sched_ext: idle: Recheck prev_cpu after narrowing allowed mask David Carlier
@ 2026-05-02  5:44 ` Andrea Righi
  2026-05-04 21:05 ` Tejun Heo
  1 sibling, 0 replies; 3+ messages in thread
From: Andrea Righi @ 2026-05-02  5:44 UTC (permalink / raw)
  To: David Carlier
  Cc: Tejun Heo, David Vernet, Changwoo Min, Ingo Molnar,
	Peter Zijlstra, Juri Lelli, Vincent Guittot, Dietmar Eggemann,
	Steven Rostedt, Ben Segall, Mel Gorman, Valentin Schneider,
	K Prateek Nayak, open list:SCHEDULER - SCHED_EXT,
	open list:SCHEDULER

On Thu, Apr 30, 2026 at 10:27:47AM +0100, David Carlier wrote:
> scx_select_cpu_dfl() narrows @allowed to @cpus_allowed & @p->cpus_ptr
> when the BPF caller supplies a @cpus_allowed that differs from
> @p->cpus_ptr and @p doesn't have full affinity. However,
> @is_prev_allowed was computed against the original (wider)
> @cpus_allowed, so the prev_cpu fast paths could pick a @prev_cpu that
> is in @cpus_allowed but not in @p->cpus_ptr, violating the intended
> invariant that the returned CPU is always usable by @p. The kernel
> masks this via the SCX_EV_SELECT_CPU_FALLBACK fallback, but the
> behavior contradicts the documented contract.
> 
> Move the @is_prev_allowed evaluation past the narrowing block so it
> tests against the final @allowed mask.
> 
> Fixes: ee9a4e92799d ("sched_ext: idle: Properly handle invalid prev_cpu during idle selection")
> Assisted-by: Claude <noreply@anthropic.com>
> Signed-off-by: David Carlier <devnexen@gmail.com>

Makes sense, good catch.

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

Thanks,
-Andrea

> ---
>  kernel/sched/ext_idle.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/kernel/sched/ext_idle.c b/kernel/sched/ext_idle.c
> index 41785f65bbb2..bbb845f36a0a 100644
> --- a/kernel/sched/ext_idle.c
> +++ b/kernel/sched/ext_idle.c
> @@ -464,12 +464,6 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
>  
>  	preempt_disable();
>  
> -	/*
> -	 * Check whether @prev_cpu is still within the allowed set. If not,
> -	 * we can still try selecting a nearby CPU.
> -	 */
> -	is_prev_allowed = cpumask_test_cpu(prev_cpu, allowed);
> -
>  	/*
>  	 * Determine the subset of CPUs usable by @p within @cpus_allowed.
>  	 */
> @@ -486,6 +480,12 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
>  		}
>  	}
>  
> +	/*
> +	 * Check whether @prev_cpu is still within the allowed set. If not,
> +	 * we can still try selecting a nearby CPU.
> +	 */
> +	is_prev_allowed = cpumask_test_cpu(prev_cpu, allowed);
> +
>  	/*
>  	 * This is necessary to protect llc_cpus.
>  	 */
> -- 
> 2.53.0
> 

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

* Re: [PATCH] sched_ext: idle: Recheck prev_cpu after narrowing allowed mask
  2026-04-30  9:27 [PATCH] sched_ext: idle: Recheck prev_cpu after narrowing allowed mask David Carlier
  2026-05-02  5:44 ` Andrea Righi
@ 2026-05-04 21:05 ` Tejun Heo
  1 sibling, 0 replies; 3+ messages in thread
From: Tejun Heo @ 2026-05-04 21:05 UTC (permalink / raw)
  To: David Carlier
  Cc: David Vernet, Andrea Righi, Changwoo Min, Ingo Molnar,
	Peter Zijlstra, Juri Lelli, Vincent Guittot, Dietmar Eggemann,
	Steven Rostedt, Ben Segall, Mel Gorman, Valentin Schneider,
	K Prateek Nayak, sched-ext, linux-kernel

Hello,

Applied to sched_ext/for-7.1-fixes with Andrea's Reviewed-by and a
Cc: stable@vger.kernel.org # v6.16+ added.

Thanks.

--
tejun

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

end of thread, other threads:[~2026-05-04 21:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-30  9:27 [PATCH] sched_ext: idle: Recheck prev_cpu after narrowing allowed mask David Carlier
2026-05-02  5:44 ` Andrea Righi
2026-05-04 21:05 ` Tejun Heo

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