Re: [PATCH 1/4] sched/fair: Prefer fully-idle SMT cores in asym-capacity idle selection
From: Andrea Righi
Date: Fri Mar 27 2026 - 12:40:19 EST
Hi Prateek,
On Fri, Mar 27, 2026 at 04:44:01PM +0530, K Prateek Nayak wrote:
> Hello Andrea,
>
> On 3/27/2026 4:28 PM, Andrea Righi wrote:
> > On Fri, Mar 27, 2026 at 04:14:57PM +0530, K Prateek Nayak wrote:
> >> Hello Andrea,
> >>
> >> On 3/26/2026 8:32 PM, Andrea Righi wrote:
> >>> /* This CPU fits with all requirements */
> >>> - if (fits > 0)
> >>> - return cpu;
> >>> + if (fits > 0) {
> >>> + if (prefer_idle_cores && on_idle_core)
> >>> + return cpu;
> >>> + if (!prefer_idle_cores)
> >>> + return cpu;
> >>
> >> nit.
> >>
> >> Can the above two be re-wittern as:
> >>
> >> if (!prefer_idle_cores || on_idle_core)
> >> return cpu;
> >>
> >> since they are equivalent.
> >
> > Oh yes, indeed.
>
> Also, can we just rewrite this Patch as:
>
> (Includes feedback from Vincent; Only build tested)
>
> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index 700d0f145ca6..cffd5649b54e 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -7946,6 +7946,7 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
> static int
> select_idle_capacity(struct task_struct *p, struct sched_domain *sd, int target)
> {
> + bool prefers_idle_core = sched_smt_active() && test_idle_cores(target);
> unsigned long task_util, util_min, util_max, best_cap = 0;
> int fits, best_fits = 0;
> int cpu, best_cpu = -1;
> @@ -7959,6 +7960,7 @@ select_idle_capacity(struct task_struct *p, struct sched_domain *sd, int target)
> util_max = uclamp_eff_value(p, UCLAMP_MAX);
>
> for_each_cpu_wrap(cpu, cpus, target) {
> + bool preferred_core = !prefers_idle_core || is_core_idle(cpu);
> unsigned long cpu_cap = capacity_of(cpu);
>
> if (!choose_idle_cpu(cpu, p))
> @@ -7967,7 +7969,7 @@ select_idle_capacity(struct task_struct *p, struct sched_domain *sd, int target)
> fits = util_fits_cpu(task_util, util_min, util_max, cpu);
>
> /* This CPU fits with all requirements */
> - if (fits > 0)
> + if (fits > 0 && preferred_core)
> return cpu;
> /*
> * Only the min performance hint (i.e. uclamp_min) doesn't fit.
> @@ -7976,6 +7978,14 @@ select_idle_capacity(struct task_struct *p, struct sched_domain *sd, int target)
> else if (fits < 0)
> cpu_cap = get_actual_cpu_capacity(cpu);
>
> + /*
> + * If we are on an preferred core, translate the range of fits
> + * from [-1, 1] to [-4, -2]. This ensures that an idle core
> + * is always given priority over (paritally) busy core.
> + */
> + if (preferred_core)
> + fits -= 3;
> +
Ah, I like this trick. Yes, this definitely makes the patch more compact.
> /*
> * First, select CPU which fits better (-1 being better than 0).
> * Then, select the one with best capacity at same level.
> ---
>
> My naive eyes say it should be equivalent of what you have but maybe
> I'm wrong?
It seems correct to my naive eyes as well. Will test this out to make sure.
Unfortunately I just lost access to my system (bummer), I found another
Vera machine, but this one has a version of the firmware that exposes all
CPUs with the same highest_perf... so I can still do some testing, but not
the same one with SD_ASYM_CPUCAPACITY + SMT. I should get access to the
previous system with the different highest_perf values on Monday.
Thanks,
-Andrea