From 3133760aed498ba0cbba0a75d89b2f22e42acfca Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Mon, 23 Nov 2026 20:52:04 -0500 Subject: [PATCH] sched/fair: Wake brief tasks on the waking CPU when the waker is brief too A pipe or futex ping-pong between two threads costs 13-17 us per round trip on an M5 Max (T6050) against 2.4-2.7 us on macOS on the same machine, and 1.1 us when both threads are pinned to one CPU. Traced with sched_waking/ipi/cpu_idle/sched_switch events, every wake-up of the partner's idle CPU takes about 3 us from the IPI to the core leaving WFI (the hardware powers idle cores down in WFI) and about 5 us until the wakee runs; and with each CPU busy only ~10% of the time, schedutil keeps the clusters at 1.3-2.3 GHz instead of 4.4-4.6 GHz. Even with the performance governor a cross-CPU round trip stays at 5-7 us. When the wakee and the waker both run only briefly between sleeps and the waker is alone on its CPU, wake the wakee there. Such a pair takes turns, so the wakee runs as soon as the waker sleeps, without an IPI or a core wake-up, and the pair keeps one CPU busy at a high clock. "Briefly" is a decaying average of the run time between sleeps below sysctl_sched_migration_cost / 8 (62.5 us by default) for both tasks, and the waker's runtime since its last sleep below the same limit. Include the unaccounted portion from exec_start to the refreshed rq task clock under the local rq lock (clamping negative deltas like update_se()), so a scheduler tick is not needed to see it, and check sched_cpu_cookie_match() under the rq lock so core scheduling cookies are respected. Requiring the waker to be alone stacks at most one wakee on a CPU. wake_wide() still spreads tasks that wake many others. The run time between sleeps is accounted in dequeue_task_fair() when a task sleeps (including throttled sleeps) and prev_sleep_sum_runtime is resynced in switched_to_fair() so prior non-fair execution is not folded into the first fair burst. Give energy-aware scheduling first choice. This keeps its placement when it finds a CPU and leaves the short-wake heuristic for the normal fair wake path. The T6050 tables on this base have neither OPP power nor a CPU dynamic-power coefficient, so the DT-based cpufreq energy model is unavailable. The WAKE_SHORT_LOCAL scheduler feature turns it off at run time. Signed-off-by: Satya Benson --- include/linux/sched.h | 8 ++++ kernel/sched/core.c | 2 + kernel/sched/fair.c | 87 ++++++++++++++++++++++++++++++++++++++++- kernel/sched/features.h | 7 ++++ 4 files changed, 103 insertions(+), 1 deletion(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 57ef9a060eb65..a6f978d4f3fe9 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -598,6 +598,14 @@ struct sched_entity { u64 nr_migrations; + /* + * Run time between sleeps: sum_exec_runtime when the task last went + * to sleep, and the decaying average of the run time between two + * sleeps. See wake_short_local(). + */ + u64 prev_sleep_sum_runtime; + u64 burst_avg; + #ifdef CONFIG_FAIR_GROUP_SCHED int depth; struct sched_entity *parent; diff --git a/kernel/sched/core.c b/kernel/sched/core.c index c481404713aaa..b0719d4853818 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -4666,6 +4666,8 @@ static void __sched_fork(u64 clone_flags, struct task_struct *p) p->se.sum_exec_runtime = 0; p->misfit_runtime = 0; p->se.prev_sum_exec_runtime = 0; + p->se.prev_sleep_sum_runtime = 0; + p->se.burst_avg = 0; p->se.nr_migrations = 0; p->se.vruntime = 0; p->se.vlag = 0; diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 5c8f84186e2cc..f51fff11a9729 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -8414,6 +8414,15 @@ static bool __dequeue_task(struct rq *rq, struct task_struct *p, int flags) return true; } +/* Account the run time since @p last slept, as it goes to sleep again. */ +static inline void update_burst_avg(struct task_struct *p) +{ + u64 burst = p->se.sum_exec_runtime - p->se.prev_sleep_sum_runtime; + + p->se.prev_sleep_sum_runtime = p->se.sum_exec_runtime; + update_avg(&p->se.burst_avg, burst); +} + /* * The dequeue_task method is called before nr_running is * decreased. We remove the task from the rbtree and @@ -8421,16 +8430,29 @@ static bool __dequeue_task(struct rq *rq, struct task_struct *p, int flags) */ static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) { + bool sleep = (flags & (DEQUEUE_SLEEP | DEQUEUE_DELAYED)) == DEQUEUE_SLEEP; + if (task_is_throttled(p)) { dequeue_throttled_task(p, flags); + if (sleep) + update_burst_avg(p); return true; } if (!p->se.sched_delayed) util_est_dequeue(&rq->cfs, p); - if (!__dequeue_task(rq, p, flags)) + /* + * After the dequeue, which updated sum_exec_runtime. Without + * DEQUEUE_DELAYED, @p is the task going to sleep and stays valid. + */ + if (!__dequeue_task(rq, p, flags)) { + if (sleep) + update_burst_avg(p); return false; + } + if (sleep) + update_burst_avg(p); /* * Must not reference @p after __dequeue_task(DEQUEUE_DELAYED). @@ -9916,6 +9938,63 @@ static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu) return target; } +/* + * Wake @p on @cpu, the waking CPU, rather than on an idle CPU, when @p and the + * waker both run only briefly between sleeps and the waker is alone on @cpu. + * + * Such a pair takes turns: one wakes the other and soon sleeps, as in a pipe + * or futex ping-pong, or a thread waking the thread that joins it. On an idle + * CPU every turn costs an IPI and the wake-up of a sleeping core, several + * microseconds on Apple cores, whose WFI lets the hardware power them down, + * and each CPU stays so lightly loaded that schedutil keeps the cluster at a + * low clock. On the waking CPU the wakee runs as soon as the waker sleeps, + * and the pair keeps one CPU busy at a high clock. + * + * Only brief runs qualify, so a task that keeps running after a wake-up does + * not hold up its wakee for longer than that; and only a waker alone on its + * CPU, so at most one wakee is stacked on it. Tasks that wake many others are + * spread by wake_wide() before this is considered. + */ +static bool wake_short_local(struct task_struct *p, int cpu) +{ + u64 limit = sysctl_sched_migration_cost / 8; + struct task_struct *waker = current; + struct rq *rq = cpu_rq(cpu); + struct rq_flags rf; + bool brief = false; + + if (!in_task() || waker->sched_class != &fair_sched_class) + return false; + if (p->se.burst_avg > limit || waker->se.burst_avg > limit) + return false; + if (sched_asym_cpucap_active() && !task_fits_cpu(p, cpu)) + return false; + + /* + * The wakeup path has IRQs disabled and holds p->pi_lock. Skip this + * heuristic if the local rq lock is already held or contended. + */ + if (!raw_spin_rq_trylock(rq)) + return false; + rq_pin_lock(rq, &rf); + + if (rq->nr_running - cfs_h_nr_delayed(rq) == 1 && + task_current_donor(rq, waker) && + sched_cpu_cookie_match(rq, p)) { + u64 burst = waker->se.sum_exec_runtime - waker->se.prev_sleep_sum_runtime; + s64 delta; + + update_rq_clock(rq); + delta = rq_clock_task(rq) - waker->se.exec_start; + if (delta > 0) + burst += delta; + brief = burst <= limit; + } + rq_unlock(rq, &rf); + + return brief; +} + /* * select_task_rq_fair: Select target runqueue for the waking task in domains * that have the relevant SD flag set. In practice, this is SD_BALANCE_WAKE, @@ -9956,6 +10035,11 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags) } want_affine = !wake_wide(p) && cpumask_test_cpu(cpu, p->cpus_ptr); + + /* Preserve a placement selected by EAS above. */ + if (sched_feat(WAKE_SHORT_LOCAL) && want_affine && + wake_short_local(p, cpu)) + return cpu; } for_each_domain(cpu, tmp) { @@ -15653,6 +15737,7 @@ static void switched_to_fair(struct rq *rq, struct task_struct *p) WARN_ON_ONCE(p->se.sched_delayed); attach_task_cfs_rq(p); + p->se.prev_sleep_sum_runtime = p->se.sum_exec_runtime; set_task_max_allowed_capacity(p); diff --git a/kernel/sched/features.h b/kernel/sched/features.h index 07132f33d1e4a..ecab475582cf0 100644 --- a/kernel/sched/features.h +++ b/kernel/sched/features.h @@ -94,6 +94,13 @@ SCHED_FEAT(TTWU_QUEUE, true) */ SCHED_FEAT(SIS_UTIL, true) +/* + * Wake a task that runs only briefly between sleeps on the waking CPU when + * the waker also runs briefly and is alone there, instead of on an idle CPU. + * See wake_short_local(). + */ +SCHED_FEAT(WAKE_SHORT_LOCAL, true) + /* * Issue a WARN when we do multiple update_rq_clock() calls * in a single rq->lock section. Default disabled because the