All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrea Righi <arighi@nvidia.com>
To: Hui Su <sh_def@163.com>
Cc: tj@kernel.org, void@manifault.com, changwoo@igalia.com,
	mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com,
	vincent.guittot@linaro.org, dietmar.eggemann@arm.com,
	rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de,
	vschneid@redhat.com, kprateek.nayak@amd.com,
	sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org,
	bpf@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH v3] sched_ext: idle: Respect task affinity in restricted CPU selection
Date: Sun, 11 Oct 2026 20:22:22 +0200	[thread overview]
Message-ID: <asvT3st3qtn05dur@gpd4> (raw)
In-Reply-To: <20261011173100.2090211-1-sh_def@163.com>

Hi Hui,

On Mon, Oct 12, 2026 at 02:31:00AM +0900, Hui Su wrote:
> The restricted select_cpu kfunc path handles single-CPU and
> migration-disabled tasks without going through scx_select_cpu_dfl().
> 
> It currently uses prev_cpu as the candidate, even though prev_cpu is only a
> hint and may no longer be usable after an affinity change. With a
> caller-provided @allowed mask, this can also return an idle CPU outside
> p->cpus_ptr.
> 
> Derive the candidate from task state instead: use the sole CPU in
> p->cpus_ptr for single-CPU tasks and task_cpu(p) for migration-disabled
> tasks. Validate it against p->cpus_ptr and @allowed before claiming its
> idle state.
> 
> Since the derived candidate may be on a different NUMA node from
> prev_cpu, also preserve SCX_PICK_IDLE_IN_NODE by rejecting cross-node
> candidates.
> 
> Perform all checks before scx_idle_test_and_clear_cpu(), as a successful
> call consumes the CPU's idle bit.
 
> Fixes: 683d2d0faba1 ("sched_ext: idle: Introduce scx_bpf_select_cpu_and()")
> Cc: stable@vger.kernel.org
> Signed-off-by: Hui Su <sh_def@163.com>

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

Thanks,
-Andrea

> ---
> Tested on x86_64 KVM/QEMU:
> - Private BPF harness passed affinity, stale-prev_cpu, caller-mask, and
>   cross-NUMA SCX_PICK_IDLE_IN_NODE cases; v2 returned CPU2 across nodes,
>   while v3 returned -EBUSY.
> - Build coverage passed with CONFIG_PREEMPT_RCU=y/n and CONFIG_SMP=n.
> 
> Changes in v3:
> - Factor restricted selection into select_cpu_restricted().
> - Preserve SCX_PICK_IDLE_IN_NODE and add cross-NUMA validation.
> 
> v2: https://lore.kernel.org/lkml/20261011131700.1351541-1-sh_def@163.com/
> 
>  kernel/sched/ext/idle.c | 50 ++++++++++++++++++++++++++++++++---------
>  1 file changed, 39 insertions(+), 11 deletions(-)
> 
> diff --git a/kernel/sched/ext/idle.c b/kernel/sched/ext/idle.c
> index aa9fb6de0ad6..28ed9e327085 100644
> --- a/kernel/sched/ext/idle.c
> +++ b/kernel/sched/ext/idle.c
> @@ -952,12 +952,43 @@ static bool is_bpf_migration_disabled(const struct task_struct *p)
>  	return p->migration_disabled;
>  }
>  
> +static s32 select_cpu_restricted(struct task_struct *p, s32 prev_cpu,
> +				 const struct cpumask *allowed, u64 flags,
> +				 bool migration_disabled)
> +{
> +	s32 cpu;
> +
> +	if (migration_disabled)
> +		cpu = task_cpu(p);
> +	else
> +		cpu = cpumask_first(p->cpus_ptr);
> +
> +	if (!cpumask_test_cpu(cpu, p->cpus_ptr))
> +		return -EBUSY;
> +
> +	if (allowed && !cpumask_test_cpu(cpu, allowed))
> +		return -EBUSY;
> +
> +	if (flags & SCX_PICK_IDLE_IN_NODE) {
> +		int node = scx_cpu_node_if_enabled(prev_cpu);
> +
> +		if (scx_cpu_node_if_enabled(cpu) != node)
> +			return -EBUSY;
> +	}
> +
> +	if (!scx_idle_test_and_clear_cpu(cpu))
> +		return -EBUSY;
> +
> +	return cpu;
> +}
> +
>  static s32 select_cpu_from_kfunc(struct scx_sched *sch, struct task_struct *p,
>  				 s32 prev_cpu, u64 wake_flags,
>  				 const struct cpumask *allowed, u64 flags)
>  {
>  	unsigned long irq_flags;
>  	bool we_locked = false;
> +	bool migration_disabled;
>  	s32 cpu;
>  
>  	if (!scx_cpu_valid(sch, prev_cpu, NULL))
> @@ -994,20 +1025,17 @@ static s32 select_cpu_from_kfunc(struct scx_sched *sch, struct task_struct *p,
>  
>  	/*
>  	 * This may also be called from ops.enqueue(), so we need to handle
> -	 * per-CPU tasks as well. For these tasks, we can skip all idle CPU
> -	 * selection optimizations and simply check whether the previously
> -	 * used CPU is idle and within the allowed cpumask.
> +	 * per-CPU tasks as well. For these tasks, skip general idle CPU
> +	 * selection and validate the task's only usable CPU directly.
>  	 */
> -	if (p->nr_cpus_allowed == 1 || is_bpf_migration_disabled(p)) {
> -		if (cpumask_test_cpu(prev_cpu, allowed ?: p->cpus_ptr) &&
> -		    scx_idle_test_and_clear_cpu(prev_cpu))
> -			cpu = prev_cpu;
> -		else
> -			cpu = -EBUSY;
> -	} else {
> +	migration_disabled = is_bpf_migration_disabled(p);
> +
> +	if (p->nr_cpus_allowed == 1 || migration_disabled)
> +		cpu = select_cpu_restricted(p, prev_cpu, allowed, flags,
> +					    migration_disabled);
> +	else
>  		cpu = scx_select_cpu_dfl(p, prev_cpu, wake_flags,
>  					 allowed ?: p->cpus_ptr, flags);
> -	}
>  
>  	if (we_locked)
>  		raw_spin_unlock_irqrestore(&p->pi_lock, irq_flags);
> 
> base-commit: 7b63ef2d55f24519e7e9e5f4d15dbea03f126e40
> 
> 

  reply	other threads:[~2026-10-11 18:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-11 17:31 [PATCH v3] sched_ext: idle: Respect task affinity in restricted CPU selection Hui Su
2026-10-11 18:22 ` Andrea Righi [this message]
2026-10-12 18:48 ` Tejun Heo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=asvT3st3qtn05dur@gpd4 \
    --to=arighi@nvidia.com \
    --cc=bpf@vger.kernel.org \
    --cc=bsegall@google.com \
    --cc=changwoo@igalia.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=sh_def@163.com \
    --cc=stable@vger.kernel.org \
    --cc=tj@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=void@manifault.com \
    --cc=vschneid@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.