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