[PATCH 2/4] sched/fair: Still look for the idlest cpu with no matching cookie

Fernand Sieber
2025-09-22T12:40:25Z

The slow path for waking tasks currently discards all potential targets
when no cookie-matching CPU is found, leading to suboptimal task placement.

Fall back to selecting the idlest CPU when no cookie-matching target is
available, ensuring better CPU utilization while maintaining the preference
for cookie-compatible placements.

Signed-off-by: Fernand Sieber <sieberf@amazon.com>
---
kernel/sched/fair.c | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 43ddfc25af99..67746899809e 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -7335,7 +7335,8 @@ sched_balance_find_dst_group(struct sched_domain *sd, struct task_struct *p, int
* sched_balance_find_dst_group_cpu - find the idlest CPU among the CPUs in the group.
*/
static int
-sched_balance_find_dst_group_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
+__sched_balance_find_dst_group_cpu(struct sched_group *group, struct task_struct *p,
+ int this_cpu, bool cookie_match)
{
unsigned long load, min_load = ULONG_MAX;
unsigned int min_exit_latency = UINT_MAX;
@@ -7352,7 +7353,8 @@ sched_balance_find_dst_group_cpu(struct sched_group *group, struct task_struct *
for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) {
struct rq *rq = cpu_rq(i);

- if (!sched_core_cookie_match(rq, p))
+ /* Only matching tasks if cookie_match, else only unmatching tasks */
+ if (cookie_match ^ sched_core_cookie_match(rq, p))
continue;

if (sched_idle_cpu(i))
@@ -7391,6 +7393,17 @@ sched_balance_find_dst_group_cpu(struct sched_group *group, struct task_struct *
return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu;
}

+/*
+ * sched_balance_find_dst_group_cpu - find the idlest CPU among the CPUs in the group.
+ */
+static inline int
+sched_balance_find_dst_group_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
+{
+ int cpu = __sched_balance_find_dst_group_cpu(group, p, this_cpu, true);
+
+ return cpu >= 0 ? cpu : __sched_balance_find_dst_group_cpu(group, p, this_cpu, false);
+}
+
static inline int sched_balance_find_dst_cpu(struct sched_domain *sd, struct task_struct *p,
int cpu, int prev_cpu, int sd_flag)
{
--
2.43.0