Skip to contentSkip to conversation

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

PATCH Superseded Keisuke Nishimura 2 participants 5 messages Nov 25 – Nov 26 Scheduler

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 sched/fair: Don't pick recent_used_cpu if target 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 target 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).

To prevent the scheduler from moving the task among isolated CPUs, check whether target has a sched domain before searching for an idle CPU.

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 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 | 4 ++++
 1 file changed, 4 insertions(+)
kernel/sched/fair.c +4 -0
index da0d9e19c457..9746fc55241b 100644
@@ -9155,6 +9155,10 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
 	unsigned long task_util, util_min, util_max;
 	int i, recent_used_cpu, prev_aff = -1;
 
+	/* Do not search for idle CPUs if target has no sched domain. */
+	if (!rcu_dereference_all(cpu_rq(target)->sd))
+		return target;
+
 	/*
 	 * On asymmetric system, update task utilization because we will check
 	 * that the task fits with CPU's capacity.
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 40 lines · Keisuke Nishimura

select_idle_sibling() can return recent_used_cpu if it is idle and shares the LLC with target. However, it does not check if target 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).

To prevent the scheduler from moving the task among isolated CPUs, check whether target has a sched domain before searching for an idle CPU.

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 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 | 4 ++++
 1 file changed, 4 insertions(+)
kernel/sched/fair.c +0 -0
index da0d9e19c457..9746fc55241b 100644

kernel/sched/fair.c static int select_idle_sibling(struct task_struct *p, int prev, int target) · line 9160

Quoted 6 lines
kernel/sched/fair.c +3 -0
@@ -9155,6 +9155,10 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
        unsigned long task_util, util_min, util_max;
        int i, recent_used_cpu, prev_aff = -1;
 
+       /* Do not search for idle CPUs if target has no sched domain. */
+       if (!rcu_dereference_all(cpu_rq(target)->sd))
+               return target;

After more thought, You have to move this check one level up at the beg of select_task_rq_fair().
Something like:

kernel/sched/fair.c +0 -0
@@ -9979,6 +9979,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);

+
+ /* Do not search for a CPU if prev has no sched domain. */
+ if (!rcu_dereference_sched_domain(cpu_rq(target)->sd))
+ return;
+

        if (wake_flags & WF_TTWU) {
                record_wakee(p);
Quoted 7 lines
kernel/sched/fair.c +1 -1
+
        /*
         * On asymmetric system, update task utilization because we will check
         * that the task fits with CPU's capacity.
--

2.34.1

maintainer
#2 lore raw
Quoted 59 lines · Vincent Guittot
Quoted 40 lines · Keisuke Nishimura

select_idle_sibling() can return recent_used_cpu if it is idle and shares the LLC with target. However, it does not check if target 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).

To prevent the scheduler from moving the task among isolated CPUs, check whether target has a sched domain before searching for an idle CPU.

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 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 | 4 ++++
 1 file changed, 4 insertions(+)
kernel/sched/fair.c +0 -0
index da0d9e19c457..9746fc55241b 100644

kernel/sched/fair.c static int select_idle_sibling(struct task_struct *p, int prev, int target) · line 9160

Quoted 6 lines
kernel/sched/fair.c +3 -0
@@ -9155,6 +9155,10 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
        unsigned long task_util, util_min, util_max;
        int i, recent_used_cpu, prev_aff = -1;
 
+       /* Do not search for idle CPUs if target has no sched domain. */
+       if (!rcu_dereference_all(cpu_rq(target)->sd))
+               return target;

After more thought, You have to move this check one level up at the beg of select_task_rq_fair().
Something like:

kernel/sched/fair.c +0 -0
@@ -9979,6 +9979,11 @@ select_task_rq_fair(struct task_struct *p, int
 prev_cpu, int wake_flags)

kernel/sched/fair.c select_task_rq_fair(struct task_struct *p, int · line 9989

Quoted 10 lines
kernel/sched/fair.c +5 -0
         * required for stable ->cpus_allowed
         */
        lockdep_assert_held(&p->pi_lock);
+
+       /* Do not search for a CPU if prev has no sched domain. */
+       if (!rcu_dereference_sched_domain(cpu_rq(target)->sd))
+               return;
+
        if (wake_flags & WF_TTWU) {
                record_wakee(p);

And this still doesn't fix the case when cpus are isolated at runtime

Quoted 8 lines
Quoted 8 lines
kernel/sched/fair.c +1 -1
+
        /*
         * On asymmetric system, update task utilization because we will check
         * that the task fits with CPU's capacity.
--

2.34.1

#3 lore raw
Quoted 65 lines · Vincent Guittot
Quoted 63 lines · Vincent Guittot
Quoted 40 lines · Keisuke Nishimura

select_idle_sibling() can return recent_used_cpu if it is idle and shares the LLC with target. However, it does not check if target 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).

To prevent the scheduler from moving the task among isolated CPUs, check whether target has a sched domain before searching for an idle CPU.

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 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 | 4 ++++
  1 file changed, 4 insertions(+)
kernel/sched/fair.c +0 -0
index da0d9e19c457..9746fc55241b 100644

kernel/sched/fair.c static int select_idle_sibling(struct task_struct *p, int prev, int target) · line 9160

Quoted 6 lines
kernel/sched/fair.c +3 -0
@@ -9155,6 +9155,10 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
         unsigned long task_util, util_min, util_max;
         int i, recent_used_cpu, prev_aff = -1;
 
+       /* Do not search for idle CPUs if target has no sched domain. */
+       if (!rcu_dereference_all(cpu_rq(target)->sd))
+               return target;

After more thought, You have to move this check one level up at the beg of select_task_rq_fair().
Something like:

kernel/sched/fair.c +2 -0
@@ -9979,6 +9979,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);
+
+       /* Do not search for a CPU if prev has no sched domain. */
Quoted 8 lines

kernel/sched/fair.c select_task_rq_fair(struct task_struct *p, int · line 9989

Quoted 6 lines
kernel/sched/fair.c +3 -0
+       if (!rcu_dereference_sched_domain(cpu_rq(target)->sd))
+               return;
+
         if (wake_flags & WF_TTWU) {
                 record_wakee(p);

And this still doesn't fix the case when cpus are isolated at runtime

Thanks. Moving the check to select_task_rq_fair() makes sense.

Could you elaborate on the case where CPUs are isolated at runtime? I would expect rq->sd to be NULL in that case as well?

Best,
Keisuke

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
kernel/sched/fair.c +1 -1
+
         /*
          * On asymmetric system, update task utilization because we will check
          * that the task fits with CPU's capacity.
--

2.34.1

maintainer
#4 lore raw
Quoted 78 lines · Keisuke Nishimura
Quoted 65 lines · Vincent Guittot
Quoted 63 lines · Vincent Guittot
Quoted 40 lines · Keisuke Nishimura

select_idle_sibling() can return recent_used_cpu if it is idle and shares the LLC with target. However, it does not check if target 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).

To prevent the scheduler from moving the task among isolated CPUs, check whether target has a sched domain before searching for an idle CPU.

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 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 | 4 ++++
  1 file changed, 4 insertions(+)
kernel/sched/fair.c +0 -0
index da0d9e19c457..9746fc55241b 100644

kernel/sched/fair.c static int select_idle_sibling(struct task_struct *p, int prev, int target) · line 9160

Quoted 6 lines
kernel/sched/fair.c +3 -0
@@ -9155,6 +9155,10 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
         unsigned long task_util, util_min, util_max;
         int i, recent_used_cpu, prev_aff = -1;
 
+       /* Do not search for idle CPUs if target has no sched domain. */
+       if (!rcu_dereference_all(cpu_rq(target)->sd))
+               return target;

After more thought, You have to move this check one level up at the beg of select_task_rq_fair().
Something like:

kernel/sched/fair.c +2 -0
@@ -9979,6 +9979,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);
+
+       /* Do not search for a CPU if prev has no sched domain. */
Quoted 8 lines

kernel/sched/fair.c select_task_rq_fair(struct task_struct *p, int · line 9989

Quoted 6 lines
kernel/sched/fair.c +3 -0
+       if (!rcu_dereference_sched_domain(cpu_rq(target)->sd))
+               return;
+
         if (wake_flags & WF_TTWU) {
                 record_wakee(p);

And this still doesn't fix the case when cpus are isolated at runtime

Quoted 4 lines

Thanks. Moving the check to select_task_rq_fair() makes sense.

Could you elaborate on the case where CPUs are isolated at runtime? I would expect rq->sd to be NULL in that case as well?

llc_id has been set in case of runtime isolation. So any reference to this newly isolated cpu that is saved in a task (p->recent_used_cpu as an example) waking on a non isolated cpu, sharing same llc_id, can match the cpus_share_cache() test and select_idle_sibling() can return the newly isolated cpu.

Quoted 14 lines

Best,
Keisuke

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
kernel/sched/fair.c +1 -1
+
         /*
          * On asymmetric system, update task utilization because we will check
          * that the task fits with CPU's capacity.
--

2.34.1