[PATCH v2] sched/fair: Fix the decision for load balance

From: Keisuke Nishimura
Date: Mon Oct 30 2023 - 13:32:54 EST


should_we_balance is called for the decision to do load-balancing.
When sched ticks invoke this function, only one CPU should return
true. However, in the current code, two CPUs can return true. The
following situation, where b means busy and i means idle, is an
example, because CPU 0 and CPU 2 return true.

[0, 1] [2, 3]
b b i b

This fix checks if there exists an idle CPU with busy sibling(s)
after looking for a CPU on an idle core. If some idle CPUs with busy
siblings are found, just the first one should do load-balancing.

Fixes: b1bfeab9b002 ("sched/fair: Consider the idle state of the whole core for load balance")
Signed-off-by: Keisuke Nishimura <keisuke.nishimura@xxxxxxxx>
---
kernel/sched/fair.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 2048138ce54b..69d63fae34f4 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -11079,12 +11079,16 @@ static int should_we_balance(struct lb_env *env)
continue;
}

- /* Are we the first idle CPU? */
+ /*
+ * Are we the first idle core in a MC or higher domain
+ * or the first idle CPU in a SMT domain?
+ */
return cpu == env->dst_cpu;
}

- if (idle_smt == env->dst_cpu)
- return true;
+ /* Are we the first idle CPU with busy siblings? */
+ if (idle_smt != -1)
+ return idle_smt == env->dst_cpu;

/* Are we the first CPU of this group ? */
return group_balance_cpu(sg) == env->dst_cpu;
--
2.34.1