Re: [PATCH v9 04/11] sched/core: Try to use a preferred CPU in is_cpu_allowed

Mete Durlu <[email protected]> Mon, 10 Aug 2026 13:58:17 +0200
Newsgroups dev.linux.lists.virtualization,org.kernel.vger.linux-doc,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
Hi,

...
> For majority of the cases this would still keep select_fallback_rq
> as O(N). task_has_preferred_cpus which is O(N) is called only if
> !cpu_preferred. Then task running there is expected to move out.
> So subsequent it should run on preferred CPU. This becomes O(N**2)
> only for tasks pinned only non preferred CPUs. That is rare case.
> 
> Signed-off-by: Shrikanth Hegde <[email protected]>
> ---
>   kernel/sched/core.c  | 12 ++++++++++--
>   kernel/sched/sched.h | 12 ++++++++++++
>   2 files changed, 22 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index a45f7c308329..9e8eec4451b6 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -2509,8 +2509,12 @@ static inline bool is_cpu_allowed(struct task_struct *p, int cpu)
>   		return cpu_online(cpu);
>   
>   	/* Non kernel threads are not allowed during either online or offline. */
> -	if (!(p->flags & PF_KTHREAD))
> +	if (!(p->flags & PF_KTHREAD)) {
> +		/* Try to use preferred CPU if task's affinity allows */
> +		if (task_can_sched_on_preferred(cpu, p))
> +			return false;
>   		return cpu_active(cpu);
> +	}
>   
>   	/* KTHREAD_IS_PER_CPU is always allowed. */
>   	if (kthread_is_per_cpu(p))
> @@ -2520,7 +2524,11 @@ static inline bool is_cpu_allowed(struct task_struct *p, int cpu)
>   	if (cpu_dying(cpu))
>   		return false;
>   
> -	/* But are allowed during online. */
> +	/* Try to keep unbound kthreads on a preferred CPU if possible. */
> +	if (task_can_sched_on_preferred(cpu, p))
> +		return false;
> +
> +	/* Otherwise, they are allowed to run on online CPU. */
>   	return cpu_online(cpu);
>   }
>   
> diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
> index 26ae13c86b69..6de6366f2faa 100644
> --- a/kernel/sched/sched.h
> +++ b/kernel/sched/sched.h
> @@ -4230,4 +4230,16 @@ DEFINE_CLASS_IS_UNCONDITIONAL(sched_change)
>   
>   #include "ext/ext.h"
>   
> +static inline bool task_can_sched_on_preferred(int cpu, struct task_struct *p)


Sorry, I glanced to the earlier versions but I couldn't find
the answer to my question. Trivial one but still.

FWICT, task_can_sched_on_preferred() is only being used in
core.c, is there a reason why it is defined in
kernel/sched/sched.h and not in core.c ?

Thanks.
-Mete