[PATCH v2 3/3] sched/fair: break out of newidle balancing if an RT task appears

From: Scott Wood
Date: Wed Apr 28 2021 - 19:28:49 EST


The CFS load balancer can take a little while, to the point of it having
a special LBF_NEED_BREAK flag, when the task moving code takes a
breather.

However, at that point it will jump right back in to load balancing,
without checking whether the CPU has gained any runnable real time
(or deadline) tasks.

Break out of load balancing in the CPU_NEWLY_IDLE case, to allow the
scheduling of the RT task. Without this, latencies of over 1ms are
seen on large systems.

Signed-off-by: Rik van Riel <riel@xxxxxxxxxx>
Reported-by: Clark Williams <williams@xxxxxxxxxx>
Signed-off-by: Clark Williams <williams@xxxxxxxxxx>
[swood: Limit change to newidle]
Signed-off-by: Scott Wood <swood@xxxxxxxxxx>
---
v2: Only break out of newidle balancing

kernel/sched/fair.c | 24 ++++++++++++++++++++----
kernel/sched/sched.h | 6 ++++++
2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index aa8c87b6aff8..c3500c963af2 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -9502,10 +9502,21 @@ imbalanced_active_balance(struct lb_env *env)
return 0;
}

-static int need_active_balance(struct lb_env *env)
+static bool stop_balance_early(struct lb_env *env)
+{
+ return env->idle == CPU_NEWLY_IDLE && rq_has_higher_tasks(env->dst_rq);
+}
+
+static int need_active_balance(struct lb_env *env, int *continue_balancing)
{
struct sched_domain *sd = env->sd;

+ /* Run the realtime task now; load balance later. */
+ if (stop_balance_early(env)) {
+ *continue_balancing = 0;
+ return 0;
+ }
+
if (asym_active_balance(env))
return 1;

@@ -9550,7 +9561,7 @@ static int should_we_balance(struct lb_env *env)
* to do the newly idle load balance.
*/
if (env->idle == CPU_NEWLY_IDLE)
- return 1;
+ return !rq_has_higher_tasks(env->dst_rq);

/* Try to find first idle CPU */
for_each_cpu_and(cpu, group_balance_mask(sg), env->cpus) {
@@ -9660,6 +9671,11 @@ static int load_balance(int this_cpu, struct rq *this_rq,

local_irq_restore(rf.flags);

+ if (stop_balance_early(&env)) {
+ *continue_balancing = 0;
+ goto out;
+ }
+
if (env.flags & LBF_NEED_BREAK) {
env.flags &= ~LBF_NEED_BREAK;
goto more_balance;
@@ -9743,7 +9759,7 @@ static int load_balance(int this_cpu, struct rq *this_rq,
if (idle != CPU_NEWLY_IDLE)
sd->nr_balance_failed++;

- if (need_active_balance(&env)) {
+ if (need_active_balance(&env, continue_balancing)) {
unsigned long flags;

raw_spin_lock_irqsave(&busiest->lock, flags);
@@ -9787,7 +9803,7 @@ static int load_balance(int this_cpu, struct rq *this_rq,
sd->nr_balance_failed = 0;
}

- if (likely(!active_balance) || need_active_balance(&env)) {
+ if (likely(!active_balance) || need_active_balance(&env, continue_balancing)) {
/* We were unbalanced, so reset the balancing interval */
sd->balance_interval = sd->min_interval;
}
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 10a1522b1e30..88be4ed58924 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -1987,6 +1987,12 @@ static inline struct cpuidle_state *idle_get_state(struct rq *rq)

return rq->idle_state;
}
+
+/* Is there a task of a high priority class? */
+static inline bool rq_has_higher_tasks(struct rq *rq)
+{
+ return unlikely(rq->nr_running != rq->cfs.h_nr_running);
+}
#else
static inline void idle_set_state(struct rq *rq,
struct cpuidle_state *idle_state)
--
2.27.0