Skip to contentSkip to conversation

sched/fair: remove redundant check in select_idle_smt

PATCH Superseded Abel Wu 2 participants 9 messages Sep 1, 2022 – Sep 7, 2022 Scheduler

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Abel Wu

author
#0 lore raw

PATCH v2 1/5 sched/fair: remove redundant check in select_idle_smt

If two cpus share LLC cache, then the two cores they belong to are also in the same LLC domain.

---
 kernel/sched/fair.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
kernel/sched/fair.c +4 -7
index efceb670e755..9657c7de5f57 100644
@@ -6350,14 +6350,11 @@ static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu
 /*
  * Scan the local SMT mask for idle CPUs.
  */
-static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
+static int select_idle_smt(struct task_struct *p, int target)
 {
 	int cpu;
 
-	for_each_cpu(cpu, cpu_smt_mask(target)) {
-		if (!cpumask_test_cpu(cpu, p->cpus_ptr) ||
-		    !cpumask_test_cpu(cpu, sched_domain_span(sd)))
-			continue;
+	for_each_cpu_and(cpu, cpu_smt_mask(target), p->cpus_ptr) {
 		if (available_idle_cpu(cpu) || sched_idle_cpu(cpu))
 			return cpu;
 	}
@@ -6381,7 +6378,7 @@ static inline int select_idle_core(struct task_struct *p, int core, struct cpuma
 	return __select_idle_cpu(core, p);
 }
 
-static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
+static inline int select_idle_smt(struct task_struct *p, int target)
 {
 	return -1;
 }
@@ -6615,7 +6612,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
 		has_idle_core = test_idle_cores(target, false);
 
 		if (!has_idle_core && cpus_share_cache(prev, target)) {
-			i = select_idle_smt(p, sd, prev);
+			i = select_idle_smt(p, prev);
 			if ((unsigned int)i < nr_cpumask_bits)
 				return i;
 		}
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v2 2/5 sched/fair: avoid double search on same cpu

The prev cpu is checked at the beginning of SIS, and it's unlikely to be idle before the second check in select_idle_smt(). So we'd better focus on its SMT siblings.

---
 kernel/sched/fair.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/sched/fair.c +2 -0
index 9657c7de5f57..1ad79aaaaf93 100644
@@ -6355,6 +6355,8 @@ static int select_idle_smt(struct task_struct *p, int target)
 	int cpu;
 
 	for_each_cpu_and(cpu, cpu_smt_mask(target), p->cpus_ptr) {
+		if (cpu == target)
+			continue;
 		if (available_idle_cpu(cpu) || sched_idle_cpu(cpu))
 			return cpu;
 	}
Signature · 1 line
2.31.1
author
#2 lore raw

PATCH v2 3/5 sched/fair: remove useless check in select_idle_core

In reply to Abel Wu

The function only gets called when sds->has_idle_cores is true which can be possible only when sched_smt_present is enabled. This change also aligns select_idle_core with select_idle_smt that the caller do the check if necessary.

---
 kernel/sched/fair.c | 3 ---
 1 file changed, 3 deletions(-)
kernel/sched/fair.c +0 -3
index 1ad79aaaaf93..03ce65068333 100644
@@ -6321,9 +6321,6 @@ static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpu
 	bool idle = true;
 	int cpu;
 
-	if (!static_branch_likely(&sched_smt_present))
-		return __select_idle_cpu(core, p);
-
 	for_each_cpu(cpu, cpu_smt_mask(core)) {
 		if (!available_idle_cpu(cpu)) {
 			idle = false;
Signature · 1 line
2.31.1
author
#3 lore raw

PATCH v2 4/5 sched/fair: default to false in test_idle_cores

In reply to Abel Wu

It's uncertain whether idle cores exist or not if shared sched- domains are not ready, so returning "no idle cores" usually makes sense.

While __update_idle_core() is an exception, it checks status of this core and set to shared sched-domain if necessary. So the whole logic depends on the existence of sds, and can bail out early if !sds.

It's somehow a little tricky, and as Josh suggested that it should be transient while the domain isn't ready. So remove the self-defined default value to make things more clearer, while introduce little overhead in idle path.

---
 kernel/sched/fair.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
kernel/sched/fair.c +8 -8
index 03ce65068333..23b020c3d3a0 100644
@@ -1588,11 +1588,11 @@ numa_type numa_classify(unsigned int imbalance_pct,
 
 #ifdef CONFIG_SCHED_SMT
 /* Forward declarations of select_idle_sibling helpers */
-static inline bool test_idle_cores(int cpu, bool def);
+static inline bool test_idle_cores(int cpu);
 static inline int numa_idle_core(int idle_core, int cpu)
 {
 	if (!static_branch_likely(&sched_smt_present) ||
-	    idle_core >= 0 || !test_idle_cores(cpu, false))
+	    idle_core >= 0 || !test_idle_cores(cpu))
 		return idle_core;
 
 	/*
@@ -6271,7 +6271,7 @@ static inline void set_idle_cores(int cpu, int val)
 		WRITE_ONCE(sds->has_idle_cores, val);
 }
 
-static inline bool test_idle_cores(int cpu, bool def)
+static inline bool test_idle_cores(int cpu)
 {
 	struct sched_domain_shared *sds;
 
@@ -6279,7 +6279,7 @@ static inline bool test_idle_cores(int cpu, bool def)
 	if (sds)
 		return READ_ONCE(sds->has_idle_cores);
 
-	return def;
+	return false;
 }
 
 /*
@@ -6295,7 +6295,7 @@ void __update_idle_core(struct rq *rq)
 	int cpu;
 
 	rcu_read_lock();
-	if (test_idle_cores(core, true))
+	if (test_idle_cores(core))
 		goto unlock;
 
 	for_each_cpu(cpu, cpu_smt_mask(core)) {
@@ -6367,9 +6367,9 @@ static inline void set_idle_cores(int cpu, int val)
 {
 }
 
-static inline bool test_idle_cores(int cpu, bool def)
+static inline bool test_idle_cores(int cpu)
 {
-	return def;
+	return false;
 }
 
 static inline int select_idle_core(struct task_struct *p, int core, struct cpumask *cpus, int *idle_cpu)
@@ -6608,7 +6608,7 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target)
 		return target;
 
 	if (sched_smt_active()) {
-		has_idle_core = test_idle_cores(target, false);
+		has_idle_core = test_idle_cores(target);
 
 		if (!has_idle_core && cpus_share_cache(prev, target)) {
 			i = select_idle_smt(p, prev);
Signature · 1 line
2.31.1
author
#4 lore raw

PATCH v2 5/5 sched/fair: cleanup for SIS_PROP

In reply to Abel Wu

The sched-domain of this cpu is only used when SIS_PROP is enabled, and it should be irrelevant whether the local sd_llc is valid or not, since all we care about is target sd_llc if !SIS_PROP.

---
 kernel/sched/fair.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/sched/fair.c +4 -4
index 23b020c3d3a0..3561b18bfe9f 100644
@@ -6399,16 +6399,16 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
 	struct sched_domain *this_sd;
 	u64 time = 0;
 
-	this_sd = rcu_dereference(*this_cpu_ptr(&sd_llc));
-	if (!this_sd)
-		return -1;
-
 	cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr);
 
 	if (sched_feat(SIS_PROP) && !has_idle_core) {
 		u64 avg_cost, avg_idle, span_avg;
 		unsigned long now = jiffies;
 
+		this_sd = rcu_dereference(*this_cpu_ptr(&sd_llc));
+		if (!this_sd)
+			return -1;
+
 		/*
 		 * If we're busy, the assumption that the last idle period
 		 * predicts the future is flawed; age away the remaining
Signature · 1 line
2.31.1
reviewer
#5 lore raw

Re: [PATCH v2 5/5] sched/fair: cleanup for SIS_PROP

Quoted 5 lines · Abel Wu

The sched-domain of this cpu is only used when SIS_PROP is enabled, and it should be irrelevant whether the local sd_llc is valid or not, since all we care about is target sd_llc if !SIS_PROP.

This could conceivably result in an uninitialised memory access if SIS_PROP was enabled while select_idle_cpu is running. I'm not sure if it can happen when jump labels are in use but I think it could happen for !CONFIG_JUMP_LABEL updating the sysctl_sched_features bitmap updated via sysctl.

The patch is still a good idea because it moves an unlikely rcu_deference out of the default path for sched features but either this_sd needs to be initialised to NULL and checked or the this_sd lookup needs to happen twice at a slight additional cost to the default-disabled SIS_PROP path.

Signature · 2 lines
Mel Gorman
SUSE Labs
author
#6 lore raw

Re: Re: [PATCH v2 5/5] sched/fair: cleanup for SIS_PROP

On 9/1/22 10:03 PM, Mel Gorman Wrote:

Quoted 5 lines · Abel Wu
Quoted 4 lines

The sched-domain of this cpu is only used when SIS_PROP is enabled, and it should be irrelevant whether the local sd_llc is valid or not, since all we care about is target sd_llc if !SIS_PROP.

Quoted 7 lines
Quoted 1 line

This could conceivably result in an uninitialised memory access if SIS_PROP was enabled while select_idle_cpu is running. I'm not sure if it can happen when jump labels are in use but I think it could happen for !CONFIG_JUMP_LABEL updating the sysctl_sched_features bitmap updated via sysctl.

Nice catch!

Quoted 6 lines

The patch is still a good idea because it moves an unlikely rcu_deference out of the default path for sched features but either this_sd needs to be initialised to NULL and checked or the this_sd lookup needs to happen twice at a slight additional cost to the default-disabled SIS_PROP path.

I'd prefer the former.

Thanks & BR,
Abel

author
#7 lore raw

Re: [PATCH v2 5/5] sched/fair: cleanup for SIS_PROP

In reply to Mel Gorman
Quoted 13 lines · Mel Gorman
Quoted 5 lines · Abel Wu

The sched-domain of this cpu is only used when SIS_PROP is enabled, and it should be irrelevant whether the local sd_llc is valid or not, since all we care about is target sd_llc if !SIS_PROP.

This could conceivably result in an uninitialised memory access if SIS_PROP was enabled while select_idle_cpu is running. I'm not sure if it can happen when jump labels are in use but I think it could happen for !CONFIG_JUMP_LABEL updating the sysctl_sched_features bitmap updated via sysctl.

Quoted 5 lines

The patch is still a good idea because it moves an unlikely rcu_deference out of the default path for sched features but either this_sd needs to be initialised to NULL and checked or the this_sd lookup needs to happen twice at a slight additional cost to the default-disabled SIS_PROP path.

Hi Mel, please check the following resent patch, Thanks!

https://lore.kernel.org/lkml/[email protected]/

reviewer Acked-by
#8 lore raw

Re: [PATCH v2 5/5] sched/fair: cleanup for SIS_PROP

Quoted 20 lines · Abel Wu
Quoted 13 lines · Mel Gorman
Quoted 5 lines · Abel Wu

The sched-domain of this cpu is only used when SIS_PROP is enabled, and it should be irrelevant whether the local sd_llc is valid or not, since all we care about is target sd_llc if !SIS_PROP.

This could conceivably result in an uninitialised memory access if SIS_PROP was enabled while select_idle_cpu is running. I'm not sure if it can happen when jump labels are in use but I think it could happen for !CONFIG_JUMP_LABEL updating the sysctl_sched_features bitmap updated via sysctl.

Quoted 5 lines

The patch is still a good idea because it moves an unlikely rcu_deference out of the default path for sched features but either this_sd needs to be initialised to NULL and checked or the this_sd lookup needs to happen twice at a slight additional cost to the default-disabled SIS_PROP path.

Quoted 3 lines

Hi Mel, please check the following resent patch, Thanks!

https://lore.kernel.org/lkml/[email protected]/

Weird, I don't remember seeing this patch even though I'm cc'd on it. It looks fine so even though it's the wrong thread;

Signature · 2 lines
Mel Gorman
SUSE Labs