sched/fair: Don't pick recent_used_cpu if target has no LLC domain
PATCH 1/1 sched/fair: Don't pick recent_used_cpu if target has no LLC domain
select_idle_sibling() can return prev or recent_used_cpu if they are idle and share the LLC with target. However, cpus_share_cache() returns true for any two isolated CPUs because their sd_llc_id is never set and keeps the initial value.
As a result, the original implementation can move a task between isolated CPUs. 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, add a check whether target has an LLC domain.
The path for prev is not impacted, because target == prev if target is isolated. Add the same check there for consistency.
This bug was found with the help of verification using Frama-C.
--- kernel/sched/fair.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
kernel/sched/fair.c +9 -7
@@ -9151,7 +9151,7 @@ static inline bool asym_fits_cpu(unsigned long util,static int select_idle_sibling (struct task_struct *p,int prev,int target) {bool has_idle_core = false; -struct sched_domain *sd; +struct sched_domain *sd, *sd_asym;unsigned long task_util, util_min, util_max;int i, recent_used_cpu, prev_aff =-1 ;
@@ -9175,10 +9175,13 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target) asym_fits_cpu(task_util, util_min, util_max, target)) goto select_smt_priority; +/* NULL if target has no LLC domain, e.g. isolcpus */ + sd =rcu_dereference_all (per_cpu (sd_llc, target)); +/* * If the previous CPU is cache affine and idle, don't be stupid: */ -if (prev != target &&cpus_share_cache (prev, target) && +if (prev != target && sd &&cpus_share_cache (prev, target) &&choose_idle_cpu (prev, p) &&asym_fits_cpu (task_util, util_min, util_max, prev)) {
@@ -9213,7 +9216,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target) p->recent_used_cpu = prev;if (recent_used_cpu != prev && recent_used_cpu != target && -cpus_share_cache (recent_used_cpu, target) && + sd &&cpus_share_cache (recent_used_cpu, target) &&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)) {
@@ -9233,7 +9236,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)* sd_asym_cpucapacity rather than sd_llc. */ if (sched_asym_cpucap_active ()) { - sd =rcu_dereference_all (per_cpu (sd_asym_cpucapacity, target)); + sd_asym =rcu_dereference_all (per_cpu (sd_asym_cpucapacity, target));/* * On an asymmetric CPU capacity system where an exclusive * cpuset defines a symmetric island (i.e. one unique
@@ -9242,8 +9245,8 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)* SD_ASYM_CPUCAPACITY. These should follow the usual symmetric * capacity path. */ -if (sd) { - i =select_idle_capacity (p, sd, target); +if (sd_asym) { + i =select_idle_capacity (p, sd_asym, target);if ((unsigned int )i < nr_cpumask_bits) { target = i; goto select_smt_priority;
@@ -9252,7 +9255,6 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target) } } - sd =rcu_dereference_all (per_cpu (sd_llc, target));if (!sd)return target;
Signature · 1 line
2.34.1
Vincent Guittot
maintainerQuoted 5 lines · Keisuke Nishimura
select_idle_sibling()can return prev orrecent_used_cpuif they are idle and share the LLC with target. However,cpus_share_cache()returns true for any two isolated CPUs because theirsd_llc_idis never set and keeps the initial value.
Are you sure that's because their sd_llc_id is never set and keeps the initial value? Or it's because they are in the same llc_mask() and we use the same sd_llc_id for all CPUs in the same llc_mask ?
Being able to set a dedicated sd_llc_id for isolated cpu seems the right option
Quoted 39 lines
As a result, the original implementation can move a task between isolated CPUs. Consider the following situation:
* isolcpus=1,2
* recent_used_cpu=1
* target=prev=2
* affinity=1,2When CPU 1 is idle but CPU 2 is busy,
select_idle_sibling()returnsrecent_used_cpu(CPU 1).To prevent the scheduler from moving the task among isolated CPUs, add a check whether target has an LLC domain.
The path for prev is not impacted, because target == prev if target is isolated. Add the same check there for consistency.
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")Signed-off-by Keisuke Nishimura <[email protected]>--- kernel/sched/fair.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)kernel/sched/fair.c +1
-1@@ -9151,7 +9151,7 @@ static inline bool asym_fits_cpu(unsigned long util,static int select_idle_sibling (struct task_struct *p,int prev,int target) {bool has_idle_core = false; -struct sched_domain *sd; +struct sched_domain *sd, *sd_asym;unsigned long task_util, util_min, util_max;int i, recent_used_cpu, prev_aff =-1 ;
kernel/sched/fair.c static int select_idle_sibling(struct task_struct *p, int prev, int target) · line 9179
Quoted 5 lines
kernel/sched/fair.c +2
-0@@ -9175,10 +9175,13 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target) asym_fits_cpu(task_util, util_min, util_max, target)) goto select_smt_priority; +/* NULL if target has no LLC domain, e.g. isolcpus */ + sd =rcu_dereference_all (per_cpu (sd_llc, target));
if sd_lls is null, can't we return immediately as there is no shared llc ?
Quoted 49 lines
kernel/sched/fair.c +6
-7+/* * If the previous CPU is cache affine and idle, don't be stupid: */ -if (prev != target &&cpus_share_cache (prev, target) && +if (prev != target && sd &&cpus_share_cache (prev, target) &&choose_idle_cpu (prev, p) &&asym_fits_cpu (task_util, util_min, util_max, prev)) {@@ -9213,7 +9216,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target) p->recent_used_cpu = prev;if (recent_used_cpu != prev && recent_used_cpu != target && -cpus_share_cache (recent_used_cpu, target) && + sd &&cpus_share_cache (recent_used_cpu, target) &&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)) {@@ -9233,7 +9236,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)* sd_asym_cpucapacity rather than sd_llc. */ if (sched_asym_cpucap_active ()) { - sd =rcu_dereference_all (per_cpu (sd_asym_cpucapacity, target)); + sd_asym =rcu_dereference_all (per_cpu (sd_asym_cpucapacity, target));/* * On an asymmetric CPU capacity system where an exclusive * cpuset defines a symmetric island (i.e. one unique @@ -9242,8 +9245,8 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)* SD_ASYM_CPUCAPACITY. These should follow the usual symmetric * capacity path. */ -if (sd) { - i =select_idle_capacity (p, sd, target); +if (sd_asym) { + i =select_idle_capacity (p, sd_asym, target);if ((unsigned int )i < nr_cpumask_bits) { target = i; goto select_smt_priority;@@ -9252,7 +9255,6 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target) } } - sd =rcu_dereference_all (per_cpu (sd_llc, target));if (!sd)return target; --2.34.1
Hello,
Thank you for the feedback.
Quoted 9 lines
Quoted 4 lines
select_idle_sibling()can return prev orrecent_used_cpuif they are idle and share the LLC with target. However,cpus_share_cache()returns true for any two isolated CPUs because theirsd_llc_idis never set and keeps the initial value.Are you sure that's because their
sd_llc_idis never set and keeps the initial value? Or it's because they are in the samellc_mask()and we use the samesd_llc_idfor all CPUs in the samellc_mask?
In the patch, I meant CPUs isolated by the boot parameter isolcpus, whose sd_llc_id keeps the initial value. But in the case of cgroup isolation, the following statement
Quoted 1 line
their
sd_llc_idis never set and keeps the initial value.
is not true, although the same problem occurs. I will change the description in v2.
Quoted 2 lines
Being able to set a dedicated
sd_llc_idfor isolated cpu seems the right option
That might be a cleaner fix, but I am afraid it would be a bigger change. The id may need to be updated whenever a CPU enters or leaves an isolated partition in cgroup? Since the id also seems to be used for other purposes, e.g. index of per-LLC arrays in cache-aware scheduling (llc_id), the change could affect more than this issue.
As checking sd_llc already covers both boot parameter(isolcpus) and cgroup's isolation, would it be okay to keep this fix in this function?
Quoted 9 lines
kernel/sched/fair.c static int select_idle_sibling(struct task_struct *p, int prev, int target) · line 9179
Quoted 6 lines
kernel/sched/fair.c +2
-0@@ -9175,10 +9175,13 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target) asym_fits_cpu(task_util, util_min, util_max, target)) goto select_smt_priority; +/* NULL if target has no LLC domain, e.g. isolcpus */ + sd =rcu_dereference_all (per_cpu (sd_llc, target));if
sd_llsis null, can't we return immediately as there is no shared llc ?
Yes. I will do early-return here in v2.
Best,
Keisuke