Skip to contentSkip to conversation

sched/fair: Don't pick recent_used_cpu if it has no sched domain

PATCH New Keisuke Nishimura 1 participant 1 message Nov 26 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 sched/fair: Don't pick recent_used_cpu if it has no sched domain

select_idle_sibling() can return recent_used_cpu if it is idle and shares the LLC with target. However, it does not check if recent_used_cpu is an isolated CPU where the scheduler should not move tasks.

Consider the following situation:
* isolcpus=1,2
* recent_used_cpu=1
* target=prev=2
* affinity=1,2

When CPU 1 is idle but CPU 2 is busy, select_idle_sibling() returns recent_used_cpu (CPU 1).

A similar issue can occur when recent_used_cpu becomes isolated at runtime while prev_cpu remains non-isolated.

To prevent these migrations, check that recent_used_cpu has a sched domain. Also, return early from select_task_rq_fair() if prev_cpu has no sched domain.

This bug was found with the help of verification using Frama-C.

Fixes 32e839dda3ba ("sched/fair: Use a recently used CPU as an idle candidate and the basis for SIS")

---
Changes in v3:
- Move on_null_domain() up and reuse it.
- (Vincent) Move the early return to select_task_rq_fair() and check prev_cpu.
- Check that recent_used_cpu has a sched domain, which also covers CPUs isolated at runtime.

Changes in v2:
- Return early at the beginning of select_idle_sibling() if target has no sched domain.
- Instead of per_cpu(sd_llc, target), use cpu_rq(target)->sd to check if target is isolated or not. A CPU might be in an asymmetric capacity domain, but not in an LLC domain. In this case, returning early based only on the LLC domain would miss the chance to run select_idle_capacity().
- Rewrite the explanation, because sd_llc is not the main issue anymore.

---
 kernel/sched/fair.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
kernel/sched/fair.c +11 -5
index da0d9e19c457..ca29c7afd67a 100644
@@ -8468,6 +8468,11 @@ static unsigned long capacity_of(int cpu)
 	return cpu_rq(cpu)->cpu_capacity;
 }
 
+static inline int on_null_domain(struct rq *rq)
+{
+	return unlikely(!rcu_dereference_sched(rq->sd));
+}
+
 static void record_wakee(struct task_struct *p)
 {
 	/*
@@ -9214,6 +9219,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
 	if (recent_used_cpu != prev &&
 	    recent_used_cpu != target &&
 	    cpus_share_cache(recent_used_cpu, target) &&
+	    !on_null_domain(cpu_rq(recent_used_cpu)) &&
 	    choose_idle_cpu(recent_used_cpu, p) &&
 	    cpumask_test_cpu(recent_used_cpu, p->cpus_ptr) &&
 	    asym_fits_cpu(task_util, util_min, util_max, recent_used_cpu)) {
@@ -9926,6 +9932,11 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)
 	 * required for stable ->cpus_allowed
 	 */
 	lockdep_assert_held(&p->pi_lock);
+
+	/* Keep the task on prev_cpu if it has no sched domain. */
+	if (on_null_domain(cpu_rq(prev_cpu)))
+		return prev_cpu;
+
 	if (wake_flags & WF_TTWU) {
 		record_wakee(p);
 
@@ -14325,11 +14336,6 @@ static void sched_balance_domains(struct rq *rq, enum cpu_idle_type idle)
 
 }
 
-static inline int on_null_domain(struct rq *rq)
-{
-	return unlikely(!rcu_dereference_sched(rq->sd));
-}
-
 #ifdef CONFIG_NO_HZ_COMMON
 /*
  * NOHZ idle load balancing (ILB) details:
Signature · 1 line
2.34.1