[PATCH 20/20 v3] sched/fair: Fix tasks stacking on same idle CPU

From: Vincent Guittot

Date: Fri Nov 06 2026 - 10:27:41 EST


When tasks wake up simultaneously, they can easily stack on the same idle
CPU but only one will run immediately. For others, a different CPU might
finally be a better choice. Take advantage of the push callback to try to
find another CPU for those tasks which lost the fight and haven't been
picked.

Call also sched_class->wakeup_preempt for the higher class so fair can be
prepared of handling several tasks stacked on the CPU. Other class returns
immadiately.

Signed-off-by: Vincent Guittot <vincent.guittot@xxxxxxxxxx>
---
kernel/sched/core.c | 1 +
kernel/sched/fair.c | 16 ++++++++++++++--
2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index a1a3aa45ecf6..ef4cbcca2437 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2311,6 +2311,7 @@ void wakeup_preempt(struct rq *rq, struct task_struct *p, int flags)
rq->next_class->wakeup_preempt(rq, p, flags);
resched_curr(rq);
rq->next_class = p->sched_class;
+ rq->next_class->wakeup_preempt(rq, p, flags);
}

/*
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index da9c4416f1b5..262626455497 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -10649,10 +10649,22 @@ static void wakeup_preempt_fair(struct rq *rq, struct task_struct *p, int wake_f
/*
* XXX Getting preempted by higher class, try and find idle CPU?
*/
- if (p->sched_class != &fair_sched_class ||
- donor->sched_class != &fair_sched_class)
+ if (p->sched_class != &fair_sched_class)
return;

+ if (donor->sched_class != &fair_sched_class) {
+ /*
+ * When rq is idle, several tasks can select this rq at wakeup
+ * without knowing others did the same. Give another chance to
+ * select another rq if not picked.
+ */
+ if (sched_class_above(&fair_sched_class, donor->sched_class) &&
+ entity_eligible(cfs_rq, pse))
+ fair_add_pushable_short(rq, p);
+
+ return;
+ }
+
if (unlikely(se == pse))
return;

--
2.53.0