Skip to contentSkip to conversation

Enable Cluster Scheduling for x86 Hybrid CPUs

PATCH In review Tim Chen 6 participants 55 messages Jul 7, 2023 – Sep 13, 2023

Subsystems (2)
lore
Share & tools
lore

Tim Chen

author
#0 lore raw

v3 cover Enable Cluster Scheduling for x86 Hybrid CPUs

This is the third version of patches to fix issues to allow cluster scheduling on x86 hybrid CPUs. They address concerns raised by Peter on the second version. Please refer to the cover letter in the first version for the motivation behind this patch series.

Changes from v2:
1. Peter pointed out that biasing asym packing in sibling imbalance computation is unnecessary. We will negate extra turbo headroom advantage by concentrating tasks in the preferred group. In v3, we simplify computing sibling imbalance only in proportion to the number of cores, and remove asym packing bias. We do not lose any performance and do a bit better than v2.

2. Peter asked the question of whether it is better to round the sibling_imbalance() computation or floor the sibling_imbalanace() as in the v2 implementation. I did find the rounding to be better in threaded tensor computation, hence v3 adopt rounding in sibling_imbalance(). The performance of both versions are listed in the performance data below.

3. Fix patch 1 to take SMT thread number more than 2 into consideration.

4. Various style clean ups suggested by Peter.

Past Versions:
[v1] https://lore.kernel.org/lkml/CAKfTPtD1W6vJQBsNKEt_4tn2EeAs=73CeH4LoCwENrh2JUDwnQ@mail.gmail.com/T/
[v2] https://lore.kernel.org/all/[email protected]/

v3 Performance numbers:

This version
Single Threaded 6.3-rc5 with cluster Improvement Alternative Improvement
Benchmark Baseline scheduling in Performance implementation in Performance
(round imbalance) (floor imbalance)
(run-run deviation) (run-run deviation) (run-run deviation)
------------------------------------------------------------------------------------------------------------

tjbench		(+/- 0.08%)          (+/- 0.12%)           0.03%          (+/- 0.11%)         0.00%	  
PhPbench	(+/- 0.31%)          (+/- 0.50%)          +0.19%          (+/- 0.87%)        +0.21%         

flac (+/- 0.58%) (+/- 0.41%) +0.48% (+/- 0.41%) +1.02%
pybench (+/- 3.16%) (+/- 2.87%) +2.04% (+/- 2.22%) +4.25%

This version
with cluster Improvement Alternative Improvement
Multi Threaded 6.3-rc5 scheduling in Performance implementation in Performance

Benchmark       Baseline             (round imbalance)                    (floor imbalance)
(-#threads)     (run-run deviation)  (run-run deviation)                  (run-run deviation)               
------------------------------------------------------------------------------------------------------------
Kbuild-8	(+/- 2.90%)          (+/- 0.23%)          -1.10%          (+/- 0.40%)        -1.01%         
Kbuild-10	(+/- 3.08%)          (+/- 0.51%)          -1.93%          (+/- 0.49%)        -1.57%
Kbuild-12	(+/- 3.28%)          (+/- 0.39%)          -1.10%          (+/- 0.23%)        -0.98%
Tensor Lite-8	(+/- 4.84%)          (+/- 0.86%)          -1.32%          (+/- 0.58%)        -0.78%
Tensor Lite-10	(+/- 0.87%)          (+/- 0.30%)          +0.68%          (+/- 1.24%)        -0.13%
Tensor Lite-12	(+/- 1.37%)          (+/- 0.82%)          +4.16%          (+/- 1.65%)        +1.19%         

Tim

Peter Zijlstra (Intel) (1):
sched/debug: Dump domains' sched group flags

Ricardo Neri (1):
sched/fair: Consider the idle state of the whole core for load balance

Tim C Chen (4):
sched/fair: Determine active load balance for SMT sched groups sched/topology: Record number of cores in sched group sched/fair: Implement prefer sibling imbalance calculation between
asymmetric groups
sched/x86: Add cluster topology to hybrid CPU

 arch/x86/kernel/smpboot.c |   3 +
 kernel/sched/debug.c      |   1 +
 kernel/sched/fair.c       | 137 +++++++++++++++++++++++++++++++++++---
 kernel/sched/sched.h      |   1 +
 kernel/sched/topology.c   |  10 ++-
 5 files changed, 143 insertions(+), 9 deletions(-)
Signature · 1 line
2.32.0
author
#1 lore raw

PATCH v3 1/6 sched/fair: Determine active load balance for SMT sched groups

From: Tim C Chen <[email protected]>

On hybrid CPUs with scheduling cluster enabled, we will need to consider balancing between SMT CPU cluster, and Atom core cluster.

Below shows such a hybrid x86 CPU with 4 big cores and 8 atom cores. Each scheduling cluster span a L2 cache.

--L2-- --L2-- --L2-- --L2-- ----L2---- -----L2------ [0, 1] [2, 3] [4, 5] [5, 6] [7 8 9 10] [11 12 13 14]
Big Big Big Big Atom Atom
core core core core Module Module

If the busiest group is a big core with both SMT CPUs busy, we should active load balance if destination group has idle CPU cores. Such condition is considered by asym_active_balance() in load balancing but not considered when looking for busiest group and computing load imbalance. Add this consideration in find_busiest_group() and calculate_imbalance().

In addition, update the logic determining the busier group when one group is SMT and the other group is non SMT but both groups are partially busy with idle CPU. The busier group should be the group with idle cores rather than the group with one busy SMT CPU. We do not want to make the SMT group the busiest one to pull the only task off SMT CPU and causing the whole core to go empty.

Otherwise suppose in the search for the busiest group, we first encounter an SMT group with 1 task and set it as the busiest. The destination group is an atom cluster with 1 task and we next encounter an atom cluster group with 3 tasks, we will not pick this atom cluster over the SMT group, even though we should. As a result, we do not load balance the busier Atom cluster (with 3 tasks) towards the local atom cluster (with 1 task). And it doesn't make sense to pick the 1 task SMT group as the busier group as we also should not pull task off the SMT towards the 1 task atom cluster and make the SMT core completely empty.

---
 kernel/sched/fair.c | 80 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 77 insertions(+), 3 deletions(-)
kernel/sched/fair.c +77 -3
index 87317634fab2..f636d6c09dc6 100644
@@ -8279,6 +8279,11 @@ enum group_type {
 	 * more powerful CPU.
 	 */
 	group_misfit_task,
+	/*
+	 * Balance SMT group that's fully busy. Can benefit from migration
+	 * a task on SMT with busy sibling to another CPU on idle core.
+	 */
+	group_smt_balance,
 	/*
 	 * SD_ASYM_PACKING only: One local CPU with higher capacity is available,
 	 * and the task should be migrated to it instead of running on the
@@ -8987,6 +8992,7 @@ struct sg_lb_stats {
 	unsigned int group_weight;
 	enum group_type group_type;
 	unsigned int group_asym_packing; /* Tasks should be moved to preferred CPU */
+	unsigned int group_smt_balance;  /* Task on busy SMT be moved */
 	unsigned long group_misfit_task_load; /* A CPU has a task too big for its capacity */
 #ifdef CONFIG_NUMA_BALANCING
 	unsigned int nr_numa_running;
@@ -9260,6 +9266,9 @@ group_type group_classify(unsigned int imbalance_pct,
 	if (sgs->group_asym_packing)
 		return group_asym_packing;
 
+	if (sgs->group_smt_balance)
+		return group_smt_balance;
+
 	if (sgs->group_misfit_task_load)
 		return group_misfit_task;
 
@@ -9333,6 +9342,36 @@ sched_asym(struct lb_env *env, struct sd_lb_stats *sds,  struct sg_lb_stats *sgs
 	return sched_asym_prefer(env->dst_cpu, group->asym_prefer_cpu);
 }
 
+/* One group has more than one SMT CPU while the other group does not */
+static inline bool smt_vs_nonsmt_groups(struct sched_group *sg1,
+				    struct sched_group *sg2)
+{
+	if (!sg1 || !sg2)
+		return false;
+
+	return (sg1->flags & SD_SHARE_CPUCAPACITY) !=
+		(sg2->flags & SD_SHARE_CPUCAPACITY);
+}
+
+static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats *sgs,
+			       struct sched_group *group)
+{
+	if (env->idle == CPU_NOT_IDLE)
+		return false;
+
+	/*
+	 * For SMT source group, it is better to move a task
+	 * to a CPU that doesn't have multiple tasks sharing its CPU capacity.
+	 * Note that if a group has a single SMT, SD_SHARE_CPUCAPACITY
+	 * will not be on.
+	 */
+	if (group->flags & SD_SHARE_CPUCAPACITY &&
+	    sgs->sum_h_nr_running > 1)
+		return true;
+
+	return false;
+}
+
 static inline bool
 sched_reduced_capacity(struct rq *rq, struct sched_domain *sd)
 {
@@ -9425,6 +9464,10 @@ static inline void update_sg_lb_stats(struct lb_env *env,
 		sgs->group_asym_packing = 1;
 	}
 
+	/* Check for loaded SMT group to be balanced to dst CPU */
+	if (!local_group && smt_balance(env, sgs, group))
+		sgs->group_smt_balance = 1;
+
 	sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs);
 
 	/* Computing avg_load makes sense only when group is overloaded */
@@ -9509,6 +9552,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 			return false;
 		break;
 
+	case group_smt_balance:
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In
@@ -9537,6 +9581,18 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_has_spare:
+		/*
+		 * Do not pick sg with SMT CPUs over sg with pure CPUs,
+		 * as we do not want to pull task off SMT core with one task
+		 * and make the core idle.
+		 */
+		if (smt_vs_nonsmt_groups(sds->busiest, sg)) {
+			if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1)
+				return false;
+			else
+				return true;
+		}
+
 		/*
 		 * Select not overloaded group with lowest number of idle cpus
 		 * and highest number of running tasks. We could also compare
@@ -9733,6 +9789,7 @@ static bool update_pick_idlest(struct sched_group *idlest,
 
 	case group_imbalanced:
 	case group_asym_packing:
+	case group_smt_balance:
 		/* Those types are not used in the slow wakeup path */
 		return false;
 
@@ -9864,6 +9921,7 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
 
 	case group_imbalanced:
 	case group_asym_packing:
+	case group_smt_balance:
 		/* Those type are not used in the slow wakeup path */
 		return NULL;
 
@@ -10118,6 +10176,13 @@ static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *s
 		return;
 	}
 
+	if (busiest->group_type == group_smt_balance) {
+		/* Reduce number of tasks sharing CPU capacity */
+		env->migration_type = migrate_task;
+		env->imbalance = 1;
+		return;
+	}
+
 	if (busiest->group_type == group_imbalanced) {
 		/*
 		 * In the group_imb case we cannot rely on group-wide averages
@@ -10363,16 +10428,23 @@ static struct sched_group *find_busiest_group(struct lb_env *env)
 		goto force_balance;
 
 	if (busiest->group_type != group_overloaded) {
-		if (env->idle == CPU_NOT_IDLE)
+		if (env->idle == CPU_NOT_IDLE) {
 			/*
 			 * If the busiest group is not overloaded (and as a
 			 * result the local one too) but this CPU is already
 			 * busy, let another idle CPU try to pull task.
 			 */
 			goto out_balanced;
+		}
+
+		if (busiest->group_type == group_smt_balance &&
+		    smt_vs_nonsmt_groups(sds.local, sds.busiest)) {
+			/* Let non SMT CPU pull from SMT CPU sharing with sibling */
+			goto force_balance;
+		}
 
 		if (busiest->group_weight > 1 &&
-		    local->idle_cpus <= (busiest->idle_cpus + 1))
+		    local->idle_cpus <= (busiest->idle_cpus + 1)) {
 			/*
 			 * If the busiest group is not overloaded
 			 * and there is no imbalance between this and busiest
@@ -10383,12 +10455,14 @@ static struct sched_group *find_busiest_group(struct lb_env *env)
 			 * there is more than 1 CPU per group.
 			 */
 			goto out_balanced;
+		}
 
-		if (busiest->sum_h_nr_running == 1)
+		if (busiest->sum_h_nr_running == 1) {
 			/*
 			 * busiest doesn't have any tasks waiting to run
 			 */
 			goto out_balanced;
+		}
 	}
 
 force_balance:
Signature · 1 line
2.32.0
#2 lore raw

Re: [Patch v3 1/6] sched/fair: Determine active load balance for SMT sched groups

Quoted 2 lines · Tim Chen

From: Tim C Chen <[email protected]>

Hi Tim. Sorry for the delayed response.

Quoted 44 lines

On hybrid CPUs with scheduling cluster enabled, we will need to consider balancing between SMT CPU cluster, and Atom core cluster.

Below shows such a hybrid x86 CPU with 4 big cores and 8 atom cores. Each scheduling cluster span a L2 cache.

--L2-- --L2-- --L2-- --L2-- ----L2---- -----L2------ [0, 1] [2, 3] [4, 5] [5, 6] [7 8 9 10] [11 12 13 14]
Big Big Big Big Atom Atom
core core core core Module Module

If the busiest group is a big core with both SMT CPUs busy, we should active load balance if destination group has idle CPU cores. Such condition is considered by asym_active_balance() in load balancing but not considered when looking for busiest group and computing load imbalance. Add this consideration in find_busiest_group() and calculate_imbalance().

In addition, update the logic determining the busier group when one group is SMT and the other group is non SMT but both groups are partially busy with idle CPU. The busier group should be the group with idle cores rather than the group with one busy SMT CPU. We do not want to make the SMT group the busiest one to pull the only task off SMT CPU and causing the whole core to go empty.

Otherwise suppose in the search for the busiest group, we first encounter an SMT group with 1 task and set it as the busiest. The destination group is an atom cluster with 1 task and we next encounter an atom cluster group with 3 tasks, we will not pick this atom cluster over the SMT group, even though we should. As a result, we do not load balance the busier Atom cluster (with 3 tasks) towards the local atom cluster (with 1 task). And it doesn't make sense to pick the 1 task SMT group as the busier group as we also should not pull task off the SMT towards the 1 task atom cluster and make the SMT core completely empty.

---
 kernel/sched/fair.c | 80 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 77 insertions(+), 3 deletions(-)
kernel/sched/fair.c +0 -0
index 87317634fab2..f636d6c09dc6 100644

kernel/sched/fair.c enum group_type { · line 8286

Quoted 8 lines
kernel/sched/fair.c +5 -0
@@ -8279,6 +8279,11 @@ enum group_type {
 	 * more powerful CPU.
 	 */
 	group_misfit_task,
+	/*
+	 * Balance SMT group that's fully busy. Can benefit from migration
+	 * a task on SMT with busy sibling to another CPU on idle core.
+	 */
+	group_smt_balance,

Could you please explain what group_smt_balance does differently? AFAIU it is doing the same thing as group_fully_busy but for one domain above SMT domains right?

Quoted 42 lines
kernel/sched/fair.c +21 -0
 	/*
 	 * SD_ASYM_PACKING only: One local CPU with higher capacity is available,
 	 * and the task should be migrated to it instead of running on the
@@ -8987,6 +8992,7 @@ struct sg_lb_stats {
 	unsigned int group_weight;
 	enum group_type group_type;
 	unsigned int group_asym_packing; /* Tasks should be moved to preferred CPU */
+	unsigned int group_smt_balance;  /* Task on busy SMT be moved */
 	unsigned long group_misfit_task_load; /* A CPU has a task too big for its capacity */
 #ifdef CONFIG_NUMA_BALANCING
 	unsigned int nr_numa_running;
@@ -9260,6 +9266,9 @@ group_type group_classify(unsigned int imbalance_pct,
 	if (sgs->group_asym_packing)
 		return group_asym_packing;
 
+	if (sgs->group_smt_balance)
+		return group_smt_balance;
+
 	if (sgs->group_misfit_task_load)
 		return group_misfit_task;
 
@@ -9333,6 +9342,36 @@ sched_asym(struct lb_env *env, struct sd_lb_stats *sds,  struct sg_lb_stats *sgs
 	return sched_asym_prefer(env->dst_cpu, group->asym_prefer_cpu);
 }
 
+/* One group has more than one SMT CPU while the other group does not */
+static inline bool smt_vs_nonsmt_groups(struct sched_group *sg1,
+				    struct sched_group *sg2)
+{
+	if (!sg1 || !sg2)
+		return false;
+
+	return (sg1->flags & SD_SHARE_CPUCAPACITY) !=
+		(sg2->flags & SD_SHARE_CPUCAPACITY);
+}
+
+static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats *sgs,
+			       struct sched_group *group)
+{
+	if (env->idle == CPU_NOT_IDLE)
+		return false;
+

kernel/sched/fair.c sched_asym(struct lb_env *env, struct sd_lb_stats *sds, struct sg_lb_stats *sgs · line 9371

Quoted 10 lines
kernel/sched/fair.c +10 -0
+	/*
+	 * For SMT source group, it is better to move a task
+	 * to a CPU that doesn't have multiple tasks sharing its CPU capacity.
+	 * Note that if a group has a single SMT, SD_SHARE_CPUCAPACITY
+	 * will not be on.
+	 */
+	if (group->flags & SD_SHARE_CPUCAPACITY &&
+	    sgs->sum_h_nr_running > 1)
+		return true;
+

If we consider symmetric platforms which have SMT4 such as power10. we have a topology like below. multiple such MC will form DIE(PKG)

[0 2 4 6][1 3 5 7][8 10 12 14][9 11 13 15]
[--SMT--][--SMT--][----SMT---][---SMT----]
[--sg1--][--sg1--][---sg1----][---sg1----]
[--------------MC------------------------]

In case of SMT4, if there is any group which has 2 or more tasks, that group will be marked as group_smt_balance. previously, if that group had 2 or 3 tasks, it would have been marked as group_has_spare. Since all the groups have SMT that means behavior would be same fully busy right? That can cause some corner cases. No?

One example is Lets say sg1 has 4 tasks. and sg2 has 0 tasks and is trying to do load balance. Previously imbalance would have been 2, instead now imbalance would be 1. But in subsequent lb it would be balanced.

Quoted 115 lines
kernel/sched/fair.c +40 -3
+	return false;
+}
+
 static inline bool
 sched_reduced_capacity(struct rq *rq, struct sched_domain *sd)
 {
@@ -9425,6 +9464,10 @@ static inline void update_sg_lb_stats(struct lb_env *env,
 		sgs->group_asym_packing = 1;
 	}
 
+	/* Check for loaded SMT group to be balanced to dst CPU */
+	if (!local_group && smt_balance(env, sgs, group))
+		sgs->group_smt_balance = 1;
+
 	sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs);
 
 	/* Computing avg_load makes sense only when group is overloaded */
@@ -9509,6 +9552,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 			return false;
 		break;
 
+	case group_smt_balance:
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In
@@ -9537,6 +9581,18 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_has_spare:
+		/*
+		 * Do not pick sg with SMT CPUs over sg with pure CPUs,
+		 * as we do not want to pull task off SMT core with one task
+		 * and make the core idle.
+		 */
+		if (smt_vs_nonsmt_groups(sds->busiest, sg)) {
+			if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1)
+				return false;
+			else
+				return true;> +		}
+
 		/*
 		 * Select not overloaded group with lowest number of idle cpus
 		 * and highest number of running tasks. We could also compare
@@ -9733,6 +9789,7 @@ static bool update_pick_idlest(struct sched_group *idlest,
 
 	case group_imbalanced:
 	case group_asym_packing:
+	case group_smt_balance:
 		/* Those types are not used in the slow wakeup path */
 		return false;
 
@@ -9864,6 +9921,7 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
 
 	case group_imbalanced:
 	case group_asym_packing:
+	case group_smt_balance:
 		/* Those type are not used in the slow wakeup path */
 		return NULL;
 
@@ -10118,6 +10176,13 @@ static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *s
 		return;
 	}
 
+	if (busiest->group_type == group_smt_balance) {
+		/* Reduce number of tasks sharing CPU capacity */
+		env->migration_type = migrate_task;
+		env->imbalance = 1;
+		return;
+	}
+
 	if (busiest->group_type == group_imbalanced) {
 		/*
 		 * In the group_imb case we cannot rely on group-wide averages
@@ -10363,16 +10428,23 @@ static struct sched_group *find_busiest_group(struct lb_env *env)
 		goto force_balance;
 
 	if (busiest->group_type != group_overloaded) {
-		if (env->idle == CPU_NOT_IDLE)
+		if (env->idle == CPU_NOT_IDLE) {
 			/*
 			 * If the busiest group is not overloaded (and as a
 			 * result the local one too) but this CPU is already
 			 * busy, let another idle CPU try to pull task.
 			 */
 			goto out_balanced;
+		}
+
+		if (busiest->group_type == group_smt_balance &&
+		    smt_vs_nonsmt_groups(sds.local, sds.busiest)) {
+			/* Let non SMT CPU pull from SMT CPU sharing with sibling */
+			goto force_balance;
+		}
 
 		if (busiest->group_weight > 1 &&
-		    local->idle_cpus <= (busiest->idle_cpus + 1))
+		    local->idle_cpus <= (busiest->idle_cpus + 1)) {
 			/*
 			 * If the busiest group is not overloaded
 			 * and there is no imbalance between this and busiest
@@ -10383,12 +10455,14 @@ static struct sched_group *find_busiest_group(struct lb_env *env)
 			 * there is more than 1 CPU per group.
 			 */
 			goto out_balanced;
+		}
 
-		if (busiest->sum_h_nr_running == 1)
+		if (busiest->sum_h_nr_running == 1) {
 			/*
 			 * busiest doesn't have any tasks waiting to run
 			 */
 			goto out_balanced;
+		}
 	}
 
 force_balance:
author
#3 lore raw

Re: [Patch v3 1/6] sched/fair: Determine active load balance for SMT sched groups

Quoted 11 lines · Shrikanth Hegde

If we consider symmetric platforms which have SMT4 such as power10. we have a topology like below. multiple such MC will form DIE(PKG)

[0 2 4 6][1 3 5 7][8 10 12 14][9 11 13 15]
[--SMT--][--SMT--][----SMT---][---SMT----]
[--sg1--][--sg1--][---sg1----][---sg1----]
[--------------MC------------------------]

Quoted 5 lines

In case of SMT4, if there is any group which has 2 or more tasks, that group will be marked as group_smt_balance. previously, if that group had 2 or 3 tasks, it would have been marked as group_has_spare. Since all the groups have SMT that means behavior would be same fully busy right? That can cause some corner cases. No?

You raised a good point. I was looking from SMT2 perspective so group_smt_balance implies group_fully_busy. That is no longer true for SMT4.

I am thinking of the following fix on the current patch to take care of SMT4. Do you think this addresses concerns from you and Tobias?

kernel/sched/fair.c +11 -0
index 294a662c9410..3fc8d3a3bd22 100644
@@ -9588,6 +9588,17 @@ static bool update_sd_pick_busiest(struct lb_env *env,
                break;
 
        case group_smt_balance:
+               /* no idle cpus on both groups handled by group_fully_busy below */
+               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+                       if (sgs->idle_cpus > busiest->idle_cpus)
+                               return false;
+                       if (sgs->idle_cpus < busiest->idle_cpus)
+                               return true;
+                       if (sgs->sum_nr_running <= busiest_sum_nr_running)
+                               return false;
+                       else
+                               return true;
+               }
 
 

I will be on vacation next three weeks so my response will be slow.

Tim

Quoted 122 lines

One example is Lets say sg1 has 4 tasks. and sg2 has 0 tasks and is trying to do load balance. Previously imbalance would have been 2, instead now imbalance would be 1. But in subsequent lb it would be balanced.

Quoted 115 lines
(quoted diff) +40 -3
+	return false;
+}
+
 static inline bool
 sched_reduced_capacity(struct rq *rq, struct sched_domain *sd)
 {
@@ -9425,6 +9464,10 @@ static inline void update_sg_lb_stats(struct lb_env *env,
 		sgs->group_asym_packing = 1;
 	}
 
+	/* Check for loaded SMT group to be balanced to dst CPU */
+	if (!local_group && smt_balance(env, sgs, group))
+		sgs->group_smt_balance = 1;
+
 	sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs);
 
 	/* Computing avg_load makes sense only when group is overloaded */
@@ -9509,6 +9552,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 			return false;
 		break;
 
+	case group_smt_balance:
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In
@@ -9537,6 +9581,18 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_has_spare:
+		/*
+		 * Do not pick sg with SMT CPUs over sg with pure CPUs,
+		 * as we do not want to pull task off SMT core with one task
+		 * and make the core idle.
+		 */
+		if (smt_vs_nonsmt_groups(sds->busiest, sg)) {
+			if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1)
+				return false;
+			else
+				return true;> +		}
+
 		/*
 		 * Select not overloaded group with lowest number of idle cpus
 		 * and highest number of running tasks. We could also compare
@@ -9733,6 +9789,7 @@ static bool update_pick_idlest(struct sched_group *idlest,
 
 	case group_imbalanced:
 	case group_asym_packing:
+	case group_smt_balance:
 		/* Those types are not used in the slow wakeup path */
 		return false;
 
@@ -9864,6 +9921,7 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
 
 	case group_imbalanced:
 	case group_asym_packing:
+	case group_smt_balance:
 		/* Those type are not used in the slow wakeup path */
 		return NULL;
 
@@ -10118,6 +10176,13 @@ static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *s
 		return;
 	}
 
+	if (busiest->group_type == group_smt_balance) {
+		/* Reduce number of tasks sharing CPU capacity */
+		env->migration_type = migrate_task;
+		env->imbalance = 1;
+		return;
+	}
+
 	if (busiest->group_type == group_imbalanced) {
 		/*
 		 * In the group_imb case we cannot rely on group-wide averages
@@ -10363,16 +10428,23 @@ static struct sched_group *find_busiest_group(struct lb_env *env)
 		goto force_balance;
 
 	if (busiest->group_type != group_overloaded) {
-		if (env->idle == CPU_NOT_IDLE)
+		if (env->idle == CPU_NOT_IDLE) {
 			/*
 			 * If the busiest group is not overloaded (and as a
 			 * result the local one too) but this CPU is already
 			 * busy, let another idle CPU try to pull task.
 			 */
 			goto out_balanced;
+		}
+
+		if (busiest->group_type == group_smt_balance &&
+		    smt_vs_nonsmt_groups(sds.local, sds.busiest)) {
+			/* Let non SMT CPU pull from SMT CPU sharing with sibling */
+			goto force_balance;
+		}
 
 		if (busiest->group_weight > 1 &&
-		    local->idle_cpus <= (busiest->idle_cpus + 1))
+		    local->idle_cpus <= (busiest->idle_cpus + 1)) {
 			/*
 			 * If the busiest group is not overloaded
 			 * and there is no imbalance between this and busiest
@@ -10383,12 +10455,14 @@ static struct sched_group *find_busiest_group(struct lb_env *env)
 			 * there is more than 1 CPU per group.
 			 */
 			goto out_balanced;
+		}
 
-		if (busiest->sum_h_nr_running == 1)
+		if (busiest->sum_h_nr_running == 1) {
 			/*
 			 * busiest doesn't have any tasks waiting to run
 			 */
 			goto out_balanced;
+		}
 	}
 
 force_balance:
author
#4 lore raw

Re: [Patch v3 1/6] sched/fair: Determine active load balance for SMT sched groups

Quoted 7 lines
kernel/sched/fair.c +0 -0
index 294a662c9410..3fc8d3a3bd22 100644

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9597

Quoted 10 lines
kernel/sched/fair.c +7 -0
@@ -9588,6 +9588,17 @@ static bool update_sd_pick_busiest(struct lb_env *env,
                break;
 
        case group_smt_balance:
+               /* no idle cpus on both groups handled by group_fully_busy below */
+               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+                       if (sgs->idle_cpus > busiest->idle_cpus)
+                               return false;
+                       if (sgs->idle_cpus < busiest->idle_cpus)
+                               return true;
+                       if (sgs->sum_nr_running <= busiest_sum_nr_running)

typo: should be busiest->sum->nr_running

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9602

Quoted 5 lines
kernel/sched/fair.c +4 -0
+                               return false;
+                       else
+                               return true;
+               }
 

Tim

#5 lore raw

Re: [Patch v3 1/6] sched/fair: Determine active load balance for SMT sched groups

In reply to Tim Chen
Quoted 19 lines · Tim Chen
Quoted 11 lines · Shrikanth Hegde

If we consider symmetric platforms which have SMT4 such as power10. we have a topology like below. multiple such MC will form DIE(PKG)

[0 2 4 6][1 3 5 7][8 10 12 14][9 11 13 15]
[--SMT--][--SMT--][----SMT---][---SMT----]
[--sg1--][--sg1--][---sg1----][---sg1----]
[--------------MC------------------------]

Quoted 5 lines

In case of SMT4, if there is any group which has 2 or more tasks, that group will be marked as group_smt_balance. previously, if that group had 2 or 3 tasks, it would have been marked as group_has_spare. Since all the groups have SMT that means behavior would be same fully busy right? That can cause some corner cases. No?

Quoted 6 lines

You raised a good point. I was looking from SMT2 perspective so group_smt_balance implies group_fully_busy. That is no longer true for SMT4.

I am thinking of the following fix on the current patch to take care of SMT4. Do you think this addresses

Thanks Tim for taking a look at it again.

Yes. I think this would address some of the corner cases.
Any SMT4 group having 2,3,4 will have smt_balance as the group type, and busiest one is the one which has least number of idle cpu's. (same conditions as group_has_spare)

Quoted 25 lines

concerns from you and Tobias?

kernel/sched/fair.c +11 -0
index 294a662c9410..3fc8d3a3bd22 100644
@@ -9588,6 +9588,17 @@ static bool update_sd_pick_busiest(struct lb_env *env,
                break;
 
        case group_smt_balance:
+               /* no idle cpus on both groups handled by group_fully_busy below */
+               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+                       if (sgs->idle_cpus > busiest->idle_cpus)
+                               return false;
+                       if (sgs->idle_cpus < busiest->idle_cpus)
+                               return true;
+                       if (sgs->sum_nr_running <= busiest_sum_nr_running)
+                               return false;
+                       else
+                               return true;
+               }
 
 

I will be on vacation next three weeks so my response will be slow.

Quoted 3 lines

Tim

Quoted 1 line

Small suggestion to above code to avoid compiler warning of switch case falling through and else case can be removed, since update_sd_pick_busiest by default returns true.

kernel/sched/fair.c +2 -2
index e5a75c76bcaa..ae364ac6f22e 100644
@@ -9728,9 +9728,9 @@ static bool update_sd_pick_busiest(struct lb_env *env,
                                return true;
                        if (sgs->sum_nr_running <= busiest->sum_nr_running)
                                return false;
-                       else
-                               return true;
                }
+               break;
+
        case group_fully_busy:
                /*
                 * Select the fully busy group with highest avg_load. In
Quoted 122 lines
Quoted 121 lines

One example is Lets say sg1 has 4 tasks. and sg2 has 0 tasks and is trying to do load balance. Previously imbalance would have been 2, instead now imbalance would be 1. But in subsequent lb it would be balanced.

Quoted 115 lines
(quoted diff) +40 -3
+	return false;
+}
+
 static inline bool
 sched_reduced_capacity(struct rq *rq, struct sched_domain *sd)
 {
@@ -9425,6 +9464,10 @@ static inline void update_sg_lb_stats(struct lb_env *env,
 		sgs->group_asym_packing = 1;
 	}
 
+	/* Check for loaded SMT group to be balanced to dst CPU */
+	if (!local_group && smt_balance(env, sgs, group))
+		sgs->group_smt_balance = 1;
+
 	sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs);
 
 	/* Computing avg_load makes sense only when group is overloaded */
@@ -9509,6 +9552,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 			return false;
 		break;
 
+	case group_smt_balance:
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In
@@ -9537,6 +9581,18 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_has_spare:
+		/*
+		 * Do not pick sg with SMT CPUs over sg with pure CPUs,
+		 * as we do not want to pull task off SMT core with one task
+		 * and make the core idle.
+		 */
+		if (smt_vs_nonsmt_groups(sds->busiest, sg)) {
+			if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1)
+				return false;
+			else
+				return true;> +		}
+
 		/*
 		 * Select not overloaded group with lowest number of idle cpus
 		 * and highest number of running tasks. We could also compare
@@ -9733,6 +9789,7 @@ static bool update_pick_idlest(struct sched_group *idlest,
 
 	case group_imbalanced:
 	case group_asym_packing:
+	case group_smt_balance:
 		/* Those types are not used in the slow wakeup path */
 		return false;
 
@@ -9864,6 +9921,7 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
 
 	case group_imbalanced:
 	case group_asym_packing:
+	case group_smt_balance:
 		/* Those type are not used in the slow wakeup path */
 		return NULL;
 
@@ -10118,6 +10176,13 @@ static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *s
 		return;
 	}
 
+	if (busiest->group_type == group_smt_balance) {
+		/* Reduce number of tasks sharing CPU capacity */
+		env->migration_type = migrate_task;
+		env->imbalance = 1;
+		return;
+	}
+
 	if (busiest->group_type == group_imbalanced) {
 		/*
 		 * In the group_imb case we cannot rely on group-wide averages
@@ -10363,16 +10428,23 @@ static struct sched_group *find_busiest_group(struct lb_env *env)
 		goto force_balance;
 
 	if (busiest->group_type != group_overloaded) {
-		if (env->idle == CPU_NOT_IDLE)
+		if (env->idle == CPU_NOT_IDLE) {
 			/*
 			 * If the busiest group is not overloaded (and as a
 			 * result the local one too) but this CPU is already
 			 * busy, let another idle CPU try to pull task.
 			 */
 			goto out_balanced;
+		}
+
+		if (busiest->group_type == group_smt_balance &&
+		    smt_vs_nonsmt_groups(sds.local, sds.busiest)) {
+			/* Let non SMT CPU pull from SMT CPU sharing with sibling */
+			goto force_balance;
+		}
 
 		if (busiest->group_weight > 1 &&
-		    local->idle_cpus <= (busiest->idle_cpus + 1))
+		    local->idle_cpus <= (busiest->idle_cpus + 1)) {
 			/*
 			 * If the busiest group is not overloaded
 			 * and there is no imbalance between this and busiest
@@ -10383,12 +10455,14 @@ static struct sched_group *find_busiest_group(struct lb_env *env)
 			 * there is more than 1 CPU per group.
 			 */
 			goto out_balanced;
+		}
 
-		if (busiest->sum_h_nr_running == 1)
+		if (busiest->sum_h_nr_running == 1) {
 			/*
 			 * busiest doesn't have any tasks waiting to run
 			 */
 			goto out_balanced;
+		}
 	}
 
 force_balance:
maintainer
#6 lore raw

Re: [Patch v3 1/6] sched/fair: Determine active load balance for SMT sched groups

Quoted 78 lines · Shrikanth Hegde
Quoted 19 lines · Tim Chen
Quoted 11 lines · Shrikanth Hegde

If we consider symmetric platforms which have SMT4 such as power10. we have a topology like below. multiple such MC will form DIE(PKG)

[0 2 4 6][1 3 5 7][8 10 12 14][9 11 13 15]
[--SMT--][--SMT--][----SMT---][---SMT----]
[--sg1--][--sg1--][---sg1----][---sg1----]
[--------------MC------------------------]

Quoted 5 lines

In case of SMT4, if there is any group which has 2 or more tasks, that group will be marked as group_smt_balance. previously, if that group had 2 or 3 tasks, it would have been marked as group_has_spare. Since all the groups have SMT that means behavior would be same fully busy right? That can cause some corner cases. No?

Quoted 6 lines

You raised a good point. I was looking from SMT2 perspective so group_smt_balance implies group_fully_busy. That is no longer true for SMT4.

I am thinking of the following fix on the current patch to take care of SMT4. Do you think this addresses

Thanks Tim for taking a look at it again.

Yes. I think this would address some of the corner cases.
Any SMT4 group having 2,3,4 will have smt_balance as the group type, and busiest one is the one which has least number of idle cpu's. (same conditions as group_has_spare)

Quoted 25 lines

concerns from you and Tobias?

kernel/sched/fair.c +11 -0
index 294a662c9410..3fc8d3a3bd22 100644
@@ -9588,6 +9588,17 @@ static bool update_sd_pick_busiest(struct lb_env *env,
                break;
 
        case group_smt_balance:
+               /* no idle cpus on both groups handled by group_fully_busy below */
+               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+                       if (sgs->idle_cpus > busiest->idle_cpus)
+                               return false;
+                       if (sgs->idle_cpus < busiest->idle_cpus)
+                               return true;
+                       if (sgs->sum_nr_running <= busiest_sum_nr_running)
+                               return false;
+                       else
+                               return true;
+               }
 
 

I will be on vacation next three weeks so my response will be slow.

Quoted 3 lines

Tim

Quoted 1 line

Small suggestion to above code to avoid compiler warning of switch case falling through and else case can be removed, since update_sd_pick_busiest by default returns true.

kernel/sched/fair.c +0 -0
index e5a75c76bcaa..ae364ac6f22e 100644
@@ -9728,9 +9728,9 @@ static bool update_sd_pick_busiest(struct lb_env *env,
                                return true;

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9736

Quoted 10 lines
kernel/sched/fair.c +2 -2
                        if (sgs->sum_nr_running <= busiest->sum_nr_running)
                                return false;
-                       else
-                               return true;
                }
+               break;
+
        case group_fully_busy:
                /*
                 * Select the fully busy group with highest avg_load. In

Can someone please send a full patch for this? I've already queued Tim's patches in tip/sched/core (tip-bot seems to have died somewhere last week, it's being worked on).

#7 lore raw

Re: [Patch v3 1/6] sched/fair: Determine active load balance for SMT sched groups

Quoted 85 lines · Peter Zijlstra
Quoted 84 lines · Shrikanth Hegde
Quoted 19 lines · Tim Chen
Quoted 11 lines · Shrikanth Hegde

If we consider symmetric platforms which have SMT4 such as power10. we have a topology like below. multiple such MC will form DIE(PKG)

[0 2 4 6][1 3 5 7][8 10 12 14][9 11 13 15]
[--SMT--][--SMT--][----SMT---][---SMT----]
[--sg1--][--sg1--][---sg1----][---sg1----]
[--------------MC------------------------]

Quoted 5 lines

In case of SMT4, if there is any group which has 2 or more tasks, that group will be marked as group_smt_balance. previously, if that group had 2 or 3 tasks, it would have been marked as group_has_spare. Since all the groups have SMT that means behavior would be same fully busy right? That can cause some corner cases. No?

Quoted 6 lines

You raised a good point. I was looking from SMT2 perspective so group_smt_balance implies group_fully_busy. That is no longer true for SMT4.

I am thinking of the following fix on the current patch to take care of SMT4. Do you think this addresses

Thanks Tim for taking a look at it again.

Yes. I think this would address some of the corner cases.
Any SMT4 group having 2,3,4 will have smt_balance as the group type, and busiest one is the one which has least number of idle cpu's. (same conditions as group_has_spare)

Quoted 25 lines

concerns from you and Tobias?

kernel/sched/fair.c +11 -0
index 294a662c9410..3fc8d3a3bd22 100644
@@ -9588,6 +9588,17 @@ static bool update_sd_pick_busiest(struct lb_env *env,
                break;
 
        case group_smt_balance:
+               /* no idle cpus on both groups handled by group_fully_busy below */
+               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+                       if (sgs->idle_cpus > busiest->idle_cpus)
+                               return false;
+                       if (sgs->idle_cpus < busiest->idle_cpus)
+                               return true;
+                       if (sgs->sum_nr_running <= busiest_sum_nr_running)
+                               return false;
+                       else
+                               return true;
+               }
 
 

I will be on vacation next three weeks so my response will be slow.

Quoted 3 lines

Tim

Quoted 1 line

Small suggestion to above code to avoid compiler warning of switch case falling through and else case can be removed, since update_sd_pick_busiest by default returns true.

kernel/sched/fair.c +2 -2
index e5a75c76bcaa..ae364ac6f22e 100644
@@ -9728,9 +9728,9 @@ static bool update_sd_pick_busiest(struct lb_env *env,
                                return true;
                        if (sgs->sum_nr_running <= busiest->sum_nr_running)
                                return false;
-                       else
-                               return true;
                }
+               break;
+
        case group_fully_busy:
Quoted 8 lines

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9736

Quoted 4 lines
kernel/sched/fair.c +0 -0
                /*
                 * Select the fully busy group with highest avg_load. In

Can someone please send a full patch for this? I've already queued Tim's patches in tip/sched/core (tip-bot seems to have died somewhere last week, it's being worked on).

Hi Peter.

Sending on behalf of tim. I have included my suggestion as well. Hope that's ok.
Please find below the patch as of now. it includes the couple of changes that are discussed. (in 1/6 and in 3/6)

---
 kernel/sched/fair.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
kernel/sched/fair.c +12 -1
index 932e7b78894a..9502013abe33 100644
@@ -9532,7 +9532,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9720,6 +9720,17 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/* no idle cpus on both groups handled by group_fully_busy below */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+			if (sgs->idle_cpus > busiest->idle_cpus)
+				return false;
+			if (sgs->idle_cpus < busiest->idle_cpus)
+				return true;
+			if (sgs->sum_nr_running <= busiest->sum_nr_running)
+				return false;
+		}
+		break;
+
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In
Signature · 1 line
2.31.1
maintainer
#8 lore raw

Re: [Patch v3 1/6] sched/fair: Determine active load balance for SMT sched groups

Quoted 5 lines · Shrikanth Hegde

Hi Peter.

Sending on behalf of tim. I have included my suggestion as well. Hope that's ok. Please find below the patch as of now. it includes the couple of changes that are discussed. (in 1/6 and in 3/6)

Could you please add a Changelog and SoB thingies such that I can apply the thing?

Given Tim is on holidays, perhaps do something like:

Originally-by: Tim Chen <...>

After all, you did some changes and verified it actually works etc..

Quoted 37 lines
---
 kernel/sched/fair.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
kernel/sched/fair.c +12 -1
index 932e7b78894a..9502013abe33 100644
@@ -9532,7 +9532,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9720,6 +9720,17 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/* no idle cpus on both groups handled by group_fully_busy below */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+			if (sgs->idle_cpus > busiest->idle_cpus)
+				return false;
+			if (sgs->idle_cpus < busiest->idle_cpus)
+				return true;
+			if (sgs->sum_nr_running <= busiest->sum_nr_running)
+				return false;
+		}
+		break;
+
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In
Signature · 1 line
2.31.1
#9 lore raw

PATCH 1/1 sched/fair: Add SMT4 group_smt_balance handling

From: Tim Chen <[email protected]>

For SMT4, any group with more than 2 tasks will be marked as group_smt_balance. Retain the behaviour of group_has_spare by marking the busiest group as the group which has the least number of idle_cpus.

Also, handle rounding effect of adding (ncores_local + ncores_busy) when the local is fully idle and busy group has more than 2 tasks. Local group should try to pull at least 1 task in this case.

---
 kernel/sched/fair.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
kernel/sched/fair.c +12 -1
index 932e7b78894a..9502013abe33 100644
@@ -9532,7 +9532,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9720,6 +9720,17 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/* no idle cpus on both groups handled by group_fully_busy below */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+			if (sgs->idle_cpus > busiest->idle_cpus)
+				return false;
+			if (sgs->idle_cpus < busiest->idle_cpus)
+				return true;
+			if (sgs->sum_nr_running <= busiest->sum_nr_running)
+				return false;
+		}
+		break;
+
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In

--
2.31.1

author Acked-by on v1 1/1
#10 lore raw

Re: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

Quoted 38 lines · Shrikanth Hegde

From: Tim Chen <[email protected]>

For SMT4, any group with more than 2 tasks will be marked as group_smt_balance. Retain the behaviour of group_has_spare by marking the busiest group as the group which has the least number of idle_cpus.

Also, handle rounding effect of adding (ncores_local + ncores_busy) when the local is fully idle and busy group has more than 2 tasks. Local group should try to pull at least 1 task in this case.

---
 kernel/sched/fair.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
kernel/sched/fair.c +5 -1
index 932e7b78894a..9502013abe33 100644
@@ -9532,7 +9532,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9720,6 +9720,17 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/* no idle cpus on both groups handled by group_fully_busy below */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+			if (sgs->idle_cpus > busiest->idle_cpus)
+				return false;

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9736

Quoted 10 lines
kernel/sched/fair.c +7 -0
+			if (sgs->idle_cpus < busiest->idle_cpus)
+				return true;
+			if (sgs->sum_nr_running <= busiest->sum_nr_running)
+				return false;
+		}
+		break;
+
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In

Thanks for the fix up patch.

author
#11 lore raw

Re: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

Quoted 34 lines · Tim Chen
Quoted 33 lines · Shrikanth Hegde

From: Tim Chen <[email protected]>

For SMT4, any group with more than 2 tasks will be marked as group_smt_balance. Retain the behaviour of group_has_spare by marking the busiest group as the group which has the least number of idle_cpus.

Also, handle rounding effect of adding (ncores_local + ncores_busy) when the local is fully idle and busy group has more than 2 tasks. Local group should try to pull at least 1 task in this case.

---
 kernel/sched/fair.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
kernel/sched/fair.c +1 -1
index 932e7b78894a..9502013abe33 100644
@@ -9532,7 +9532,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9720,6 +9720,17 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
Quoted 11 lines

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9732

Quoted 11 lines
kernel/sched/fair.c +10 -0
 	case group_smt_balance:
+		/* no idle cpus on both groups handled by group_fully_busy below */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+			if (sgs->idle_cpus > busiest->idle_cpus)
+				return false;
+			if (sgs->idle_cpus < busiest->idle_cpus)
+				return true;
+			if (sgs->sum_nr_running <= busiest->sum_nr_running)
+				return false;
+		}
+		break;

Shrikanth and Peter,

Sorry, I acked Shrikanth's fixup patch too quickly without seeing that Shrikanth added a "break" in the patch above. My original code did not have that break statement as I did intend the code to fall through to the "group_fully_busy" code path when there are no idle cpus in both groups. To make the compiler happy and putting in the correct logic, I refresh the patch as below.

Thanks.

Tim

From: Tim Chen <[email protected]>
Date: Fri, 14 Jul 2023 16:09:30 -0700
Subject: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

For SMT4, any group with more than 2 tasks will be marked as group_smt_balance. Retain the behaviour of group_has_spare by marking the busiest group as the group which has the least number of idle_cpus.

Also, handle rounding effect of adding (ncores_local + ncores_busy) when the local is fully idle and busy group has more than 2 tasks. Local group should try to pull at least 1 task in this case.

---
 kernel/sched/fair.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
kernel/sched/fair.c +16 -2
index a87988327f88..566686c5f2bd 100644
@@ -9563,7 +9563,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9751,6 +9751,20 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/* no idle cpus on both groups handled by group_fully_busy below */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+			if (sgs->idle_cpus > busiest->idle_cpus)
+				return false;
+			if (sgs->idle_cpus < busiest->idle_cpus)
+				return true;
+			if (sgs->sum_nr_running <= busiest->sum_nr_running)
+				return false;
+			else
+				return true;
+		}
+		goto fully_busy;
+		break;
+
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In
@@ -9763,7 +9777,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		 * select the 1st one, except if @sg is composed of SMT
 		 * siblings.
 		 */
-
+fully_busy:
 		if (sgs->avg_load < busiest->avg_load)
 			return false;
 
Signature · 1 line
2.32.0
#12 lore raw

Re: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

Quoted 111 lines · Tim Chen
Quoted 34 lines · Tim Chen
Quoted 33 lines · Shrikanth Hegde

From: Tim Chen <[email protected]>

For SMT4, any group with more than 2 tasks will be marked as group_smt_balance. Retain the behaviour of group_has_spare by marking the busiest group as the group which has the least number of idle_cpus.

Also, handle rounding effect of adding (ncores_local + ncores_busy) when the local is fully idle and busy group has more than 2 tasks. Local group should try to pull at least 1 task in this case.

---
 kernel/sched/fair.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
kernel/sched/fair.c +1 -1
index 932e7b78894a..9502013abe33 100644
@@ -9532,7 +9532,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9720,6 +9720,17 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
Quoted 11 lines

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9766

Quoted 11 lines
kernel/sched/fair.c +10 -0
 	case group_smt_balance:
+		/* no idle cpus on both groups handled by group_fully_busy below */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+			if (sgs->idle_cpus > busiest->idle_cpus)
+				return false;
+			if (sgs->idle_cpus < busiest->idle_cpus)
+				return true;
+			if (sgs->sum_nr_running <= busiest->sum_nr_running)
+				return false;
+		}
+		break;

Shrikanth and Peter,

Sorry, I acked Shrikanth's fixup patch too quickly without seeing that Shrikanth added a "break" in the patch above. My original code did not have that break statement as I did intend the code to fall through to the "group_fully_busy" code path when there are no idle cpus in both groups. To make the compiler happy and putting in the correct logic, I refresh the patch as below.

Thanks.

Tim

From: Tim Chen <[email protected]>
Date: Fri, 14 Jul 2023 16:09:30 -0700
Subject: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

For SMT4, any group with more than 2 tasks will be marked as group_smt_balance. Retain the behaviour of group_has_spare by marking the busiest group as the group which has the least number of idle_cpus.

Also, handle rounding effect of adding (ncores_local + ncores_busy) when the local is fully idle and busy group has more than 2 tasks. Local group should try to pull at least 1 task in this case.

---
 kernel/sched/fair.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
kernel/sched/fair.c +15 -1
index a87988327f88..566686c5f2bd 100644
@@ -9563,7 +9563,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9751,6 +9751,20 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/* no idle cpus on both groups handled by group_fully_busy below */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+			if (sgs->idle_cpus > busiest->idle_cpus)
+				return false;
+			if (sgs->idle_cpus < busiest->idle_cpus)
+				return true;
+			if (sgs->sum_nr_running <= busiest->sum_nr_running)
+				return false;
+			else
+				return true;
+		}
+		goto fully_busy;
+		break;
+
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9783

Quoted 8 lines
kernel/sched/fair.c +1 -1
@@ -9763,7 +9777,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		 * select the 1st one, except if @sg is composed of SMT
 		 * siblings.
 		 */
-
+fully_busy:
 		if (sgs->avg_load < busiest->avg_load)
 			return false;
 

Hi Tim, Peter.

group_smt_balance(cluster scheduling), patches are in tip/sched/core. I dont see this above patch there yet. Currently as is, this can cause function difference in SMT4 systems( such as Power10).

Can we please have the above patch as well in tip/sched/core?

author
#13 lore raw

Re: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

Quoted 64 lines · Shrikanth Hegde
Quoted 53 lines

From: Tim Chen <[email protected]>
Date: Fri, 14 Jul 2023 16:09:30 -0700
Subject: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

For SMT4, any group with more than 2 tasks will be marked as group_smt_balance. Retain the behaviour of group_has_spare by marking the busiest group as the group which has the least number of idle_cpus.

Also, handle rounding effect of adding (ncores_local + ncores_busy) when the local is fully idle and busy group has more than 2 tasks. Local group should try to pull at least 1 task in this case.

---
 kernel/sched/fair.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
kernel/sched/fair.c +15 -1
index a87988327f88..566686c5f2bd 100644
@@ -9563,7 +9563,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9751,6 +9751,20 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/* no idle cpus on both groups handled by group_fully_busy below */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+			if (sgs->idle_cpus > busiest->idle_cpus)
+				return false;
+			if (sgs->idle_cpus < busiest->idle_cpus)
+				return true;
+			if (sgs->sum_nr_running <= busiest->sum_nr_running)
+				return false;
+			else
+				return true;
+		}
+		goto fully_busy;
+		break;
+
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9783

Quoted 8 lines
kernel/sched/fair.c +1 -1
@@ -9763,7 +9777,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		 * select the 1st one, except if @sg is composed of SMT
 		 * siblings.
 		 */
-
+fully_busy:
 		if (sgs->avg_load < busiest->avg_load)
 			return false;
 

Hi Tim, Peter.

Quoted 7 lines

group_smt_balance(cluster scheduling), patches are in tip/sched/core. I dont see this above patch there yet. Currently as is, this can cause function difference in SMT4 systems( such as Power10).

Can we please have the above patch as well in tip/sched/core?

Hi Peter,

Just back from my long vacation. Wonder if you have any comments on the above patch for fixing the SMT4 case?

Tim

#14 lore raw

Re: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

Quoted 71 lines · Tim Chen
Quoted 70 lines · Shrikanth Hegde
Quoted 53 lines

From: Tim Chen <[email protected]>
Date: Fri, 14 Jul 2023 16:09:30 -0700
Subject: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

For SMT4, any group with more than 2 tasks will be marked as group_smt_balance. Retain the behaviour of group_has_spare by marking the busiest group as the group which has the least number of idle_cpus.

Also, handle rounding effect of adding (ncores_local + ncores_busy) when the local is fully idle and busy group has more than 2 tasks. Local group should try to pull at least 1 task in this case.

---
 kernel/sched/fair.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
kernel/sched/fair.c +15 -1
index a87988327f88..566686c5f2bd 100644
@@ -9563,7 +9563,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9751,6 +9751,20 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/* no idle cpus on both groups handled by group_fully_busy below */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+			if (sgs->idle_cpus > busiest->idle_cpus)
+				return false;
+			if (sgs->idle_cpus < busiest->idle_cpus)
+				return true;
+			if (sgs->sum_nr_running <= busiest->sum_nr_running)
+				return false;
+			else
+				return true;
+		}
+		goto fully_busy;
+		break;
+
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9783

Quoted 8 lines
kernel/sched/fair.c +1 -1
@@ -9763,7 +9777,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		 * select the 1st one, except if @sg is composed of SMT
 		 * siblings.
 		 */
-
+fully_busy:
 		if (sgs->avg_load < busiest->avg_load)
 			return false;
 

Hi Tim, Peter.

group_smt_balance(cluster scheduling), patches are in tip/sched/core. I dont see this above patch there yet. Currently as is, this can cause function difference in SMT4 systems( such as Power10).

Can we please have the above patch as well in tip/sched/core?

Quoted 8 lines
Quoted 1 line

Hi Peter,

Just back from my long vacation. Wonder if you have any comments on the above patch for fixing the SMT4 case?

Tim

Hi Tim, Peter.

are there any concerns with the above patch for fixing the SMT4 case. Currently the behavior is group_smt_balance is set for having even 2 tasks in SMT4, ideally it should be same as the group_has_spare.

The above patch copies the same behavior to group_smt_balance.

Quoted 1 line
maintainer
#15 lore raw

Re: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

Quoted 1 line · Shrikanth Hegde

Hi Tim, Peter.

Back from PTO; mailbox is a disaster area, but I'll try and have a look soon.

author
#16 lore raw

Re: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

In reply to Shrikanth Hegde
Quoted 82 lines · Shrikanth Hegde
Quoted 71 lines · Tim Chen
Quoted 70 lines · Shrikanth Hegde
Quoted 53 lines

From: Tim Chen <[email protected]>
Date: Fri, 14 Jul 2023 16:09:30 -0700
Subject: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

For SMT4, any group with more than 2 tasks will be marked as group_smt_balance. Retain the behaviour of group_has_spare by marking the busiest group as the group which has the least number of idle_cpus.

Also, handle rounding effect of adding (ncores_local + ncores_busy) when the local is fully idle and busy group has more than 2 tasks. Local group should try to pull at least 1 task in this case.

---
 kernel/sched/fair.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
kernel/sched/fair.c +15 -1
index a87988327f88..566686c5f2bd 100644
@@ -9563,7 +9563,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9751,6 +9751,20 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/* no idle cpus on both groups handled by group_fully_busy below */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+			if (sgs->idle_cpus > busiest->idle_cpus)
+				return false;
+			if (sgs->idle_cpus < busiest->idle_cpus)
+				return true;
+			if (sgs->sum_nr_running <= busiest->sum_nr_running)
+				return false;
+			else
+				return true;
+		}
+		goto fully_busy;
+		break;
+
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9783

Quoted 8 lines
kernel/sched/fair.c +1 -1
@@ -9763,7 +9777,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		 * select the 1st one, except if @sg is composed of SMT
 		 * siblings.
 		 */
-
+fully_busy:
 		if (sgs->avg_load < busiest->avg_load)
 			return false;
 

Hi Tim, Peter.

group_smt_balance(cluster scheduling), patches are in tip/sched/core. I dont see this above patch there yet. Currently as is, this can cause function difference in SMT4 systems( such as Power10).

Can we please have the above patch as well in tip/sched/core?

Quoted 8 lines
Quoted 1 line

Hi Peter,

Just back from my long vacation. Wonder if you have any comments on the above patch for fixing the SMT4 case?

Tim

Quoted 8 lines

Hi Tim, Peter.

are there any concerns with the above patch for fixing the SMT4 case. Currently the behavior is group_smt_balance is set for having even 2 tasks in SMT4, ideally it should be same as the group_has_spare.

The above patch copies the same behavior to group_smt_balance.

Quoted 1 line

You mean simplify the patch as below? I think that should be fine. Can you make sure it works for SMT4? And I can update the patch once you confirm it works properly.

Tim

kernel/sched/fair.c +4 -10
index 6e7ee2efc1ba..48e9ab7f8a87 100644
@@ -9764,16 +9764,9 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 
        case group_smt_balance:
                /* no idle cpus on both groups handled by group_fully_busy below */
-               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
-                       if (sgs->idle_cpus > busiest->idle_cpus)
-                               return false;
-                       if (sgs->idle_cpus < busiest->idle_cpus)
-                               return true;
-                       if (sgs->sum_nr_running <= busiest->sum_nr_running)
-                               return false;
-                       else
-                               return true;
-               }
+               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0)
+                       goto has_spare;
+
                goto fully_busy;
 
        case group_fully_busy:
@@ -9809,6 +9802,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
                 * as we do not want to pull task off SMT core with one task
                 * and make the core idle.
                 */
+has_spare:
                if (smt_vs_nonsmt_groups(sds->busiest, sg)) {
                        if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1)
                                return false;
#17 lore raw

Re: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

Quoted 89 lines · Tim Chen
Quoted 88 lines · Shrikanth Hegde
Quoted 71 lines · Tim Chen
Quoted 70 lines · Shrikanth Hegde
Quoted 53 lines

From: Tim Chen <[email protected]>
Date: Fri, 14 Jul 2023 16:09:30 -0700
Subject: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

For SMT4, any group with more than 2 tasks will be marked as group_smt_balance. Retain the behaviour of group_has_spare by marking the busiest group as the group which has the least number of idle_cpus.

Also, handle rounding effect of adding (ncores_local + ncores_busy) when the local is fully idle and busy group has more than 2 tasks. Local group should try to pull at least 1 task in this case.

---
 kernel/sched/fair.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
kernel/sched/fair.c +15 -1
index a87988327f88..566686c5f2bd 100644
@@ -9563,7 +9563,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9751,6 +9751,20 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/* no idle cpus on both groups handled by group_fully_busy below */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+			if (sgs->idle_cpus > busiest->idle_cpus)
+				return false;
+			if (sgs->idle_cpus < busiest->idle_cpus)
+				return true;
+			if (sgs->sum_nr_running <= busiest->sum_nr_running)
+				return false;
+			else
+				return true;
+		}
+		goto fully_busy;
+		break;
+
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9783

Quoted 8 lines
kernel/sched/fair.c +1 -1
@@ -9763,7 +9777,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		 * select the 1st one, except if @sg is composed of SMT
 		 * siblings.
 		 */
-
+fully_busy:
 		if (sgs->avg_load < busiest->avg_load)
 			return false;
 

Hi Tim, Peter.

group_smt_balance(cluster scheduling), patches are in tip/sched/core. I dont see this above patch there yet. Currently as is, this can cause function difference in SMT4 systems( such as Power10).

Can we please have the above patch as well in tip/sched/core?

Quoted 8 lines
Quoted 1 line

Hi Peter,

Just back from my long vacation. Wonder if you have any comments on the above patch for fixing the SMT4 case?

Tim

Hi Tim, Peter.

are there any concerns with the above patch for fixing the SMT4 case. Currently the behavior is group_smt_balance is set for having even 2 tasks in SMT4, ideally it should be same as the group_has_spare.

Quoted 7 lines
Quoted 2 lines

The above patch copies the same behavior to group_smt_balance.

Quoted 1 line

You mean simplify the patch as below? I think that should be fine. Can you make sure it works for SMT4? And I can update the patch once you confirm it works properly.

This looks fine. likely better as it would avoid duplication. A few nit below.

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9766

Quoted 10 lines

Tim

kernel/sched/fair.c +0 -0
index 6e7ee2efc1ba..48e9ab7f8a87 100644
@@ -9764,16 +9764,9 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 
        case group_smt_balance:
                /* no idle cpus on both groups handled by group_fully_busy below */

Please add a comment here explaining the fall-through and spare logic.

Quoted 4 lines
kernel/sched/fair.c +0 -4
-               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
-                       if (sgs->idle_cpus > busiest->idle_cpus)
-                               return false;
-                       if (sgs->idle_cpus < busiest->idle_cpus)

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9770

Quoted 10 lines
kernel/sched/fair.c +3 -6
-                               return true;
-                       if (sgs->sum_nr_running <= busiest->sum_nr_running)
-                               return false;
-                       else
-                               return true;
-               }
+               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0)
+                       goto has_spare;
+
                goto fully_busy;

This can fall through without the additional goto statement no?

Quoted 13 lines
kernel/sched/fair.c +1 -0
        case group_fully_busy:
@@ -9809,6 +9802,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
                 * as we do not want to pull task off SMT core with one task
                 * and make the core idle.
                 */
+has_spare:
                if (smt_vs_nonsmt_groups(sds->busiest, sg)) {
                        if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1)
                                return false;
author
#18 lore raw

Re: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

Quoted 7 lines · Shrikanth Hegde
Quoted 7 lines
kernel/sched/fair.c +0 -0
index 6e7ee2efc1ba..48e9ab7f8a87 100644
@@ -9764,16 +9764,9 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 
Quoted 5 lines

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9766

Quoted 2 lines
kernel/sched/fair.c +0 -0
        case group_smt_balance:
                /* no idle cpus on both groups handled by group_fully_busy below */

Please add a comment here explaining the fall-through and spare logic.

Sure.

Quoted 15 lines
Quoted 4 lines
kernel/sched/fair.c +0 -4
-               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
-                       if (sgs->idle_cpus > busiest->idle_cpus)
-                               return false;
-                       if (sgs->idle_cpus < busiest->idle_cpus)

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9770

Quoted 10 lines
kernel/sched/fair.c +3 -6
-                               return true;
-                       if (sgs->sum_nr_running <= busiest->sum_nr_running)
-                               return false;
-                       else
-                               return true;
-               }
+               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0)
+                       goto has_spare;
+
                goto fully_busy;
Quoted 2 lines

This can fall through without the additional goto statement no?

There is an unconditional goto fully_busy so won't fall through and compiler won't complain.

Quoted 13 lines

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9808

Quoted 13 lines
kernel/sched/fair.c +1 -0
        case group_fully_busy:
@@ -9809,6 +9802,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
                 * as we do not want to pull task off SMT core with one task
                 * and make the core idle.
                 */
+has_spare:
                if (smt_vs_nonsmt_groups(sds->busiest, sg)) {
                        if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1)
                                return false;

Tim

#19 lore raw

Re: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

In reply to Tim Chen
Quoted 126 lines · Tim Chen
Quoted 82 lines · Shrikanth Hegde
Quoted 71 lines · Tim Chen
Quoted 70 lines · Shrikanth Hegde
Quoted 53 lines

From: Tim Chen <[email protected]>
Date: Fri, 14 Jul 2023 16:09:30 -0700
Subject: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

For SMT4, any group with more than 2 tasks will be marked as group_smt_balance. Retain the behaviour of group_has_spare by marking the busiest group as the group which has the least number of idle_cpus.

Also, handle rounding effect of adding (ncores_local + ncores_busy) when the local is fully idle and busy group has more than 2 tasks. Local group should try to pull at least 1 task in this case.

---
 kernel/sched/fair.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
kernel/sched/fair.c +15 -1
index a87988327f88..566686c5f2bd 100644
@@ -9563,7 +9563,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9751,6 +9751,20 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/* no idle cpus on both groups handled by group_fully_busy below */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+			if (sgs->idle_cpus > busiest->idle_cpus)
+				return false;
+			if (sgs->idle_cpus < busiest->idle_cpus)
+				return true;
+			if (sgs->sum_nr_running <= busiest->sum_nr_running)
+				return false;
+			else
+				return true;
+		}
+		goto fully_busy;
+		break;
+
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9783

Quoted 8 lines
kernel/sched/fair.c +1 -1
@@ -9763,7 +9777,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		 * select the 1st one, except if @sg is composed of SMT
 		 * siblings.
 		 */
-
+fully_busy:
 		if (sgs->avg_load < busiest->avg_load)
 			return false;
 

Hi Tim, Peter.

group_smt_balance(cluster scheduling), patches are in tip/sched/core. I dont see this above patch there yet. Currently as is, this can cause function difference in SMT4 systems( such as Power10).

Can we please have the above patch as well in tip/sched/core?

Quoted 8 lines
Quoted 1 line

Hi Peter,

Just back from my long vacation. Wonder if you have any comments on the above patch for fixing the SMT4 case?

Tim

Quoted 8 lines

Hi Tim, Peter.

are there any concerns with the above patch for fixing the SMT4 case. Currently the behavior is group_smt_balance is set for having even 2 tasks in SMT4, ideally it should be same as the group_has_spare.

The above patch copies the same behavior to group_smt_balance.

Quoted 1 line

You mean simplify the patch as below? I think that should be fine. Can you make sure it works for SMT4? And I can update the patch once you confirm it works properly.

Tim

kernel/sched/fair.c +3 -10
index 6e7ee2efc1ba..48e9ab7f8a87 100644
@@ -9764,16 +9764,9 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 
        case group_smt_balance:
                /* no idle cpus on both groups handled by group_fully_busy below */
-               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
-                       if (sgs->idle_cpus > busiest->idle_cpus)
-                               return false;
-                       if (sgs->idle_cpus < busiest->idle_cpus)
-                               return true;
-                       if (sgs->sum_nr_running <= busiest->sum_nr_running)
-                               return false;
-                       else
-                               return true;
-               }
+               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0)
+                       goto has_spare;
+
                goto fully_busy;
 
        case group_fully_busy:

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9808

Quoted 7 lines
kernel/sched/fair.c +1 -0
@@ -9809,6 +9802,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
                 * as we do not want to pull task off SMT core with one task
                 * and make the core idle.
                 */
+has_spare:
                if (smt_vs_nonsmt_groups(sds->busiest, sg)) {
                        if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1)
                                return false;

Hi Tim,

In case you were waiting for my reply as inferred from other email. The above change looks fine as well. This would avoid duplication of code for group_smt_balance.

author Acked-by Shrikanth Hegde
#20 lore raw

Re: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

Quoted 45 lines · Shrikanth Hegde
Quoted 35 lines

You mean simplify the patch as below? I think that should be fine. Can you make sure it works for SMT4? And I can update the patch once you confirm it works properly.

Tim

kernel/sched/fair.c +3 -10
index 6e7ee2efc1ba..48e9ab7f8a87 100644
@@ -9764,16 +9764,9 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 
        case group_smt_balance:
                /* no idle cpus on both groups handled by group_fully_busy below */
-               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
-                       if (sgs->idle_cpus > busiest->idle_cpus)
-                               return false;
-                       if (sgs->idle_cpus < busiest->idle_cpus)
-                               return true;
-                       if (sgs->sum_nr_running <= busiest->sum_nr_running)
-                               return false;
-                       else
-                               return true;
-               }
+               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0)
+                       goto has_spare;
+
                goto fully_busy;
 
        case group_fully_busy:

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9808

Quoted 7 lines
kernel/sched/fair.c +1 -0
@@ -9809,6 +9802,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
                 * as we do not want to pull task off SMT core with one task
                 * and make the core idle.
                 */
+has_spare:
                if (smt_vs_nonsmt_groups(sds->busiest, sg)) {
                        if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1)
                                return false;

Hi Tim,

Quoted 5 lines

In case you were waiting for my reply as inferred from other email. The above change looks fine as well. This would avoid duplication of code for group_smt_balance.

Peter,

Here's the updated patch. Please consider it for inclusion.

Thanks.

Tim

From 979e261fed6e3765316a4de794f595f93c02cef0 Mon Sep 17 00:00:00 2001
From: Tim Chen <[email protected]>
Subject: [PATCH] sched/fair: Fix SMT4 group_smt_balance handling
To: Peter Zijlstra <[email protected]>
Cc: Juri Lelli <[email protected]>, Vincent Guittot <[email protected]>, Ricardo Neri <[email protected]>, Ravi V. Shankar <[email protected]>, Ben Segall <[email protected]>, Daniel Bristot de Oliveira <[email protected]>, Dietmar Eggemann <[email protected]>, Len Brown <[email protected]>, Mel Gorman <[email protected]>, Rafael J. Wysocki <[email protected]>, Srinivas Pandruvada <[email protected]>, Steven Rostedt <[email protected]>, Tim Chen <[email protected]>, Valentin Schneider <[email protected]>, Ionela Voinescu <[email protected]>, [email protected], [email protected], Shrikanth Hegde <[email protected]>, Srikar Dronamraju <[email protected]>, [email protected], Yicong Yang <[email protected]>, Barry Song <[email protected]>, Chen Yu <[email protected]>, Hillf Danton <[email protected]>

For SMT4, any group with more than 2 tasks will be marked as group_smt_balance. Retain the behaviour of group_has_spare by marking the busiest group as the group which has the least number of idle_cpus.

Also, handle rounding effect of adding (ncores_local + ncores_busy) when the local is fully idle and busy group imbalance is less than 2 tasks. Local group should try to pull at least 1 task in this case so imbalance should be set to 2 instead.

Fixes fee1759e4f04 ("sched/fair: Determine active load balance for SMT sched groups")
---
 kernel/sched/fair.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
kernel/sched/fair.c +11 -1
index 0b7445cd5af9..fd9e594b5623 100644
@@ -9575,7 +9575,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9763,6 +9763,15 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/*
+		 * Check if we have spare CPUs on either SMT group to
+		 * choose has spare or fully busy handling.
+		 */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0)
+			goto has_spare;
+
+		fallthrough;
+
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In
@@ -9802,6 +9811,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 			else
 				return true;
 		}
+has_spare:
 
 		/*
 		 * Select not overloaded group with lowest number of idle cpus
Signature · 1 line
2.32.0
merge bot Applied
#21 lore raw

[tip: sched/urgent] sched/fair: Fix SMT4 group_smt_balance handling

The following commit has been merged into the sched/urgent branch of tip:

The following commit has been merged into the sched/urgent branch of tip:

Commit-ID:     ad468232c3eb1dab163672f98a1ab2363be7981e
Gitweb:        https://git.kernel.org/tip/ad468232c3eb1dab163672f98a1ab2363be7981e
Author:        Tim Chen <[email protected]>
AuthorDate:    Thu, 07 Sep 2023 10:42:21 -07:00
Committer:     root <[email protected]>
CommitterDate: Sat, 09 Sep 2023 15:10:10 +02:00

sched/fair: Fix SMT4 group_smt_balance handling

For SMT4, any group with more than 2 tasks will be marked as group_smt_balance. Retain the behaviour of group_has_spare by marking the busiest group as the group which has the least number of idle_cpus.

Also, handle rounding effect of adding (ncores_local + ncores_busy) when the local is fully idle and busy group imbalance is less than 2 tasks. Local group should try to pull at least 1 task in this case so imbalance should be set to 2 instead.

Fixes fee1759e4f04 ("sched/fair: Determine active load balance for SMT sched groups")
---
 kernel/sched/fair.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
kernel/sched/fair.c +11 -1
index 33a2b6b..cb22592 100644
@@ -9580,7 +9580,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9768,6 +9768,15 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/*
+		 * Check if we have spare CPUs on either SMT group to
+		 * choose has spare or fully busy handling.
+		 */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0)
+			goto has_spare;
+
+		fallthrough;
+
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In
@@ -9807,6 +9816,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 			else
 				return true;
 		}
+has_spare:
 
 		/*
 		 * Select not overloaded group with lowest number of idle cpus
merge bot Applied
#22 lore raw

[tip: sched/urgent] sched/fair: Fix SMT4 group_smt_balance handling

In reply to Tim Chen
The following commit has been merged into the sched/urgent branch of tip:

The following commit has been merged into the sched/urgent branch of tip:

Commit-ID:     450e749707bc1755f22b505d9cd942d4869dc535
Gitweb:        https://git.kernel.org/tip/450e749707bc1755f22b505d9cd942d4869dc535
Author:        Tim Chen <[email protected]>
AuthorDate:    Thu, 07 Sep 2023 10:42:21 -07:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 13 Sep 2023 15:03:06 +02:00

sched/fair: Fix SMT4 group_smt_balance handling

For SMT4, any group with more than 2 tasks will be marked as group_smt_balance. Retain the behaviour of group_has_spare by marking the busiest group as the group which has the least number of idle_cpus.

Also, handle rounding effect of adding (ncores_local + ncores_busy) when the local is fully idle and busy group imbalance is less than 2 tasks. Local group should try to pull at least 1 task in this case so imbalance should be set to 2 instead.

Fixes fee1759e4f04 ("sched/fair: Determine active load balance for SMT sched groups")
---
 kernel/sched/fair.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
kernel/sched/fair.c +11 -1
index 33a2b6b..cb22592 100644
@@ -9580,7 +9580,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9768,6 +9768,15 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/*
+		 * Check if we have spare CPUs on either SMT group to
+		 * choose has spare or fully busy handling.
+		 */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0)
+			goto has_spare;
+
+		fallthrough;
+
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In
@@ -9807,6 +9816,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 			else
 				return true;
 		}
+has_spare:
 
 		/*
 		 * Select not overloaded group with lowest number of idle cpus
maintainer
#23 lore raw

Re: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

In reply to Tim Chen
Quoted 2 lines · Tim Chen

Just back from my long vacation. Wonder if you have any comments on the above patch for fixing the SMT4 case?

This should have:

Fixes fee1759e4f04 ("sched/fair: Determine active load balance for SMT sched groups")

Right?

maintainer
#24 lore raw

Re: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

In reply to Tim Chen
Quoted 23 lines · Tim Chen
 kernel/sched/fair.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
kernel/sched/fair.c +4 -1
index a87988327f88..566686c5f2bd 100644
@@ -9563,7 +9563,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9751,6 +9751,20 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/* no idle cpus on both groups handled by group_fully_busy below */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+			if (sgs->idle_cpus > busiest->idle_cpus)

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9766

Quoted 10 lines
kernel/sched/fair.c +10 -0
+				return false;
+			if (sgs->idle_cpus < busiest->idle_cpus)
+				return true;
+			if (sgs->sum_nr_running <= busiest->sum_nr_running)
+				return false;
+			else
+				return true;
+		}
+		goto fully_busy;
+		break;

This is really daft; why can't this simply be: fallthrough; ? At the very least that break must go.

Quoted 17 lines
kernel/sched/fair.c +2 -1
+
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In
@@ -9763,7 +9777,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		 * select the 1st one, except if @sg is composed of SMT
 		 * siblings.
 		 */
-
+fully_busy:
 		if (sgs->avg_load < busiest->avg_load)
 			return false;
 
Signature · 1 line
2.32.0
author
#25 lore raw

Re: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

Quoted 30 lines · Peter Zijlstra
Quoted 28 lines · Tim Chen
 kernel/sched/fair.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
kernel/sched/fair.c +9 -1
index a87988327f88..566686c5f2bd 100644
@@ -9563,7 +9563,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9751,6 +9751,20 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/* no idle cpus on both groups handled by group_fully_busy below */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+			if (sgs->idle_cpus > busiest->idle_cpus)
+				return false;
+			if (sgs->idle_cpus < busiest->idle_cpus)
+				return true;
+			if (sgs->sum_nr_running <= busiest->sum_nr_running)
+				return false;
Quoted 10 lines

kernel/sched/fair.c static bool update_sd_pick_busiest(struct lb_env *env, · line 9766

Quoted 5 lines
kernel/sched/fair.c +5 -0
+			else
+				return true;
+		}
+		goto fully_busy;
+		break;

This is really daft; why can't this simply be: fallthrough; ? At the very least that break must go.

Yes, the break should go. I was adding the goto to prevent compiler from complaining about fall through code. The break no longer is needed.

Tim

From 81971a0b1eb64059756f00d8497b1865af2c0792 Mon Sep 17 00:00:00 2001
From: Tim Chen <[email protected]>
Date: Fri, 14 Jul 2023 16:09:30 -0700
Subject: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

For SMT4, any group with more than 2 tasks will be marked as group_smt_balance. Retain the behaviour of group_has_spare by marking the busiest group as the group which has the least number of idle_cpus.

Also, handle rounding effect of adding (ncores_local + ncores_busy) when the local is fully idle and busy group has more than 2 tasks. Local group should try to pull at least 1 task in this case.

Fixes fee1759e4f04 ("sched/fair: Determine active load balance for SMT sched groups")
---
 kernel/sched/fair.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
kernel/sched/fair.c +15 -2
index 0b7445cd5af9..6e7ee2efc1ba 100644
@@ -9575,7 +9575,7 @@ static inline long sibling_imbalance(struct lb_env *env,
 	imbalance /= ncores_local + ncores_busiest;
 
 	/* Take advantage of resource in an empty sched group */
-	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	if (imbalance <= 1 && local->sum_nr_running == 0 &&
 	    busiest->sum_nr_running > 1)
 		imbalance = 2;
 
@@ -9763,6 +9763,19 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_smt_balance:
+		/* no idle cpus on both groups handled by group_fully_busy below */
+		if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+			if (sgs->idle_cpus > busiest->idle_cpus)
+				return false;
+			if (sgs->idle_cpus < busiest->idle_cpus)
+				return true;
+			if (sgs->sum_nr_running <= busiest->sum_nr_running)
+				return false;
+			else
+				return true;
+		}
+		goto fully_busy;
+
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In
@@ -9775,7 +9788,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		 * select the 1st one, except if @sg is composed of SMT
 		 * siblings.
 		 */
-
+fully_busy:
 		if (sgs->avg_load < busiest->avg_load)
 			return false;
 
Signature · 1 line
2.32.0
maintainer
#26 lore raw

Re: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

Quoted 10 lines · Tim Chen
Quoted 7 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		goto fully_busy;
+		break;

This is really daft; why can't this simply be: fallthrough; ? At the very least that break must go.

Yes, the break should go. I was adding the goto to prevent compiler from complaining about fall through code.

But that's what we have the fallthrough keyword for, no?

author
#27 lore raw

Re: [PATCH] sched/fair: Add SMT4 group_smt_balance handling

Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Tim Chen
Quoted 7 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		goto fully_busy;
+		break;

This is really daft; why can't this simply be: fallthrough; ? At the very least that break must go.

Quoted 4 lines
Quoted 2 lines

Yes, the break should go. I was adding the goto to prevent compiler from complaining about fall through code.

But that's what we have the fallthrough keyword for, no?

Okay. Will update patch to use fallthrough once Shrikanth has a chance to test the update to use has_spare path for SMT4.

Tim

#28 lore raw

Re: [Patch v3 1/6] sched/fair: Determine active load balance for SMT sched groups

In reply to Tim Chen
Quoted 51 lines · Tim Chen
Quoted 11 lines · Shrikanth Hegde

If we consider symmetric platforms which have SMT4 such as power10. we have a topology like below. multiple such MC will form DIE(PKG)

[0 2 4 6][1 3 5 7][8 10 12 14][9 11 13 15]
[--SMT--][--SMT--][----SMT---][---SMT----]
[--sg1--][--sg1--][---sg1----][---sg1----]
[--------------MC------------------------]

Quoted 5 lines

In case of SMT4, if there is any group which has 2 or more tasks, that group will be marked as group_smt_balance. previously, if that group had 2 or 3 tasks, it would have been marked as group_has_spare. Since all the groups have SMT that means behavior would be same fully busy right? That can cause some corner cases. No?

You raised a good point. I was looking from SMT2 perspective so group_smt_balance implies group_fully_busy. That is no longer true for SMT4.

I am thinking of the following fix on the current patch to take care of SMT4. Do you think this addresses concerns from you and Tobias?

kernel/sched/fair.c +1 -0
index 294a662c9410..3fc8d3a3bd22 100644
@@ -9588,6 +9588,17 @@ static bool update_sd_pick_busiest(struct lb_env 
 *env,
                break;
 
        case group_smt_balance:
+               /* no idle cpus on both groups handled by

group_fully_busy below */

kernel/sched/fair.c +6 -0
+               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) {
+                       if (sgs->idle_cpus > busiest->idle_cpus)
+                               return false;
+                       if (sgs->idle_cpus < busiest->idle_cpus)
+                               return true;
+                       if (sgs->sum_nr_running <= 

busiest_sum_nr_running)

kernel/sched/fair.c +4 -0
+                               return false;
+                       else
+                               return true;
+               }
Quoted 4 lines

I will be on vacation next three weeks so my response will be slow.

Tim

What if the setup is asymmetric, where SMT2 and SMT4 would mix, e.g.

[0 1][2 3 4 5]
[SMT][--SMT--]

If now CPUs 0,2,3 have a running task, both groups would be classified as smt_balance. But if it comes to the selection of the busiest group, the smaller group would be selected, as it has less idle CPUs, right? Which could lead to the smaller group being left with no tasks.
Using the absolute numbers of task is what made the prefer_sibling path problematic, I would assume that the same holds true here. Therefore, I would prefer avg_load, or, similar to prefer_siblings, a ratio over the number of cores.

I can't really test that on s390 as we always have SMT2. But, we can have these asymmetries on higher levels, e.g.

[0 1][2 3][4 5][6 7][8 9]
[SMT][SMT][SMT][SMT][SMT]
[-----core----][--core--]

For large configurations this can be true for even higher levels. Therefore, the idea was to move the smt_balance state around and adapt its conditions to something like this (which would require to reorder the commits):

@@ -8330,6 +8330,11 @@ enum fbq_type { regular, remote, all };

 enum group_type {
        /* The group has spare capacity that can be used to run more 
tasks.  */
        group_has_spare = 0,
+       /*
+        * Balance SMT group that's fully busy. Can benefit from migration
+        * a task on SMT with busy sibling to another CPU on idle core.
+        */

+ group_smt_balance,
/*
* The group is fully used and the tasks don't compete for more
CPU
* cycles. Nevertheless, some tasks might wait before running.
@@ -8340,11 +8345,6 @@ enum group_type {
* more powerful CPU.
*/
group_misfit_task,
- /*

-        * Balance SMT group that's fully busy. Can benefit from migration
-        * a task on SMT with busy sibling to another CPU on idle core.
-        */

- group_smt_balance,
/*
* SD_ASYM_PACKING only: One local CPU with higher capacity is
available,
* and the task should be migrated to it instead of running on
the
@@ -9327,15 +9327,15 @@ group_type group_classify(unsigned int imbalance_pct,

        if (sgs->group_asym_packing)
                return group_asym_packing;

- if (sgs->group_smt_balance)
- return group_smt_balance;
-

        if (sgs->group_misfit_task_load)
                return group_misfit_task;

        if (!group_has_capacity(imbalance_pct, sgs))
                return group_fully_busy;

+ if (sgs->group_smt_balance)
+ return group_smt_balance;
+

        return group_has_spare;

}

@@ -9457,8 +9457,7 @@ static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats *sgs,
* Note that if a group has a single SMT, SD_SHARE_CPUCAPACITY
* will not be on.
*/
- if (group->flags & SD_SHARE_CPUCAPACITY &&
- sgs->sum_h_nr_running > 1)
+ if (sgs->sum_h_nr_running > group->cores)

                return true;

        return false;

The s390 problem is currently solved by changing the prefer_sibling path. When disabling that flag, we might have an issue, will have to verify that though.

Quoted 122 lines
Quoted 122 lines

One example is Lets say sg1 has 4 tasks. and sg2 has 0 tasks and is trying to do load balance. Previously imbalance would have been 2, instead now imbalance would be 1. But in subsequent lb it would be balanced.

Quoted 115 lines
(quoted diff) +40 -3
+	return false;
+}
+
 static inline bool
 sched_reduced_capacity(struct rq *rq, struct sched_domain *sd)
 {
@@ -9425,6 +9464,10 @@ static inline void update_sg_lb_stats(struct lb_env *env,
 		sgs->group_asym_packing = 1;
 	}
 
+	/* Check for loaded SMT group to be balanced to dst CPU */
+	if (!local_group && smt_balance(env, sgs, group))
+		sgs->group_smt_balance = 1;
+
 	sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs);
 
 	/* Computing avg_load makes sense only when group is overloaded */
@@ -9509,6 +9552,7 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 			return false;
 		break;
 
+	case group_smt_balance:
 	case group_fully_busy:
 		/*
 		 * Select the fully busy group with highest avg_load. In
@@ -9537,6 +9581,18 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 		break;
 
 	case group_has_spare:
+		/*
+		 * Do not pick sg with SMT CPUs over sg with pure CPUs,
+		 * as we do not want to pull task off SMT core with one task
+		 * and make the core idle.
+		 */
+		if (smt_vs_nonsmt_groups(sds->busiest, sg)) {
+			if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1)
+				return false;
+			else
+				return true;> +		}
+
 		/*
 		 * Select not overloaded group with lowest number of idle cpus
 		 * and highest number of running tasks. We could also compare
@@ -9733,6 +9789,7 @@ static bool update_pick_idlest(struct sched_group *idlest,
 
 	case group_imbalanced:
 	case group_asym_packing:
+	case group_smt_balance:
 		/* Those types are not used in the slow wakeup path */
 		return false;
 
@@ -9864,6 +9921,7 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
 
 	case group_imbalanced:
 	case group_asym_packing:
+	case group_smt_balance:
 		/* Those type are not used in the slow wakeup path */
 		return NULL;
 
@@ -10118,6 +10176,13 @@ static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *s
 		return;
 	}
 
+	if (busiest->group_type == group_smt_balance) {
+		/* Reduce number of tasks sharing CPU capacity */
+		env->migration_type = migrate_task;
+		env->imbalance = 1;
+		return;
+	}
+
 	if (busiest->group_type == group_imbalanced) {
 		/*
 		 * In the group_imb case we cannot rely on group-wide averages
@@ -10363,16 +10428,23 @@ static struct sched_group *find_busiest_group(struct lb_env *env)
 		goto force_balance;
 
 	if (busiest->group_type != group_overloaded) {
-		if (env->idle == CPU_NOT_IDLE)
+		if (env->idle == CPU_NOT_IDLE) {
 			/*
 			 * If the busiest group is not overloaded (and as a
 			 * result the local one too) but this CPU is already
 			 * busy, let another idle CPU try to pull task.
 			 */
 			goto out_balanced;
+		}
+
+		if (busiest->group_type == group_smt_balance &&
+		    smt_vs_nonsmt_groups(sds.local, sds.busiest)) {
+			/* Let non SMT CPU pull from SMT CPU sharing with sibling */
+			goto force_balance;
+		}
 
 		if (busiest->group_weight > 1 &&
-		    local->idle_cpus <= (busiest->idle_cpus + 1))
+		    local->idle_cpus <= (busiest->idle_cpus + 1)) {
 			/*
 			 * If the busiest group is not overloaded
 			 * and there is no imbalance between this and busiest
@@ -10383,12 +10455,14 @@ static struct sched_group *find_busiest_group(struct lb_env *env)
 			 * there is more than 1 CPU per group.
 			 */
 			goto out_balanced;
+		}
 
-		if (busiest->sum_h_nr_running == 1)
+		if (busiest->sum_h_nr_running == 1) {
 			/*
 			 * busiest doesn't have any tasks waiting to run
 			 */
 			goto out_balanced;
+		}
 	}
 
 force_balance:
#29 lore raw

Re: [Patch v3 1/6] sched/fair: Determine active load balance for SMT sched groups

Quoted 65 lines · Tobias Huschle
Quoted 54 lines · Tim Chen
Quoted 11 lines · Shrikanth Hegde

If we consider symmetric platforms which have SMT4 such as power10. we have a topology like below. multiple such MC will form DIE(PKG)

[0 2 4 6][1 3 5 7][8 10 12 14][9 11 13 15]
[--SMT--][--SMT--][----SMT---][---SMT----]
[--sg1--][--sg1--][---sg1----][---sg1----]
[--------------MC------------------------]

Quoted 8 lines

In case of SMT4, if there is any group which has 2 or more tasks, that group will be marked as group_smt_balance. previously, if that group had 2
or 3 tasks, it would have been marked as group_has_spare. Since all the groups have
SMT that means behavior would be same fully busy right? That can cause some
corner cases. No?

You raised a good point. I was looking from SMT2 perspective so group_smt_balance implies group_fully_busy. That is no longer true for SMT4.

I am thinking of the following fix on the current patch to take care of SMT4. Do you think this addresses concerns from you and Tobias?

kernel/sched/fair.c +0 -0
index 294a662c9410..3fc8d3a3bd22 100644
@@ -9588,6 +9588,17 @@ static bool update_sd_pick_busiest(struct
 lb_env *env,
                 break;
 

        case group_smt_balance:
+               /* no idle cpus on both groups handled by group_fully_busy below */
+               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) { +                       if (sgs->idle_cpus > busiest->idle_cpus) +                               return false;
+                       if (sgs->idle_cpus < busiest->idle_cpus) +                               return true;
+                       if (sgs->sum_nr_running <= busiest_sum_nr_running)
+                               return false;
+                       else
+                               return true;
+               }

Quoted 4 lines

I will be on vacation next three weeks so my response will be slow.

Tim

What if the setup is asymmetric, where SMT2 and SMT4 would mix, e.g.

[0 1][2 3 4 5]
[SMT][--SMT--]

Quoted 7 lines

If now CPUs 0,2,3 have a running task, both groups would be classified as smt_balance. But if it comes to the selection of the busiest group, the smaller
group would be selected, as it has less idle CPUs, right? Which could lead to the smaller group being left with no tasks.
Using the absolute numbers of task is what made the prefer_sibling path problematic,

Yes. But Not sure how realistic is that configuration. on power10, we typically have all cores in either SMT1, SMT2 or SMT4. But not mixed configs. One can offline a CPUs to get into that cases in SMT4.

Quoted 7 lines

I would assume that the same holds true here. Therefore, I would prefer avg_load,
or, similar to prefer_siblings, a ratio over the number of cores.

I can't really test that on s390 as we always have SMT2. But, we can have these
asymmetries on higher levels, e.g

IIUC, on higher levels, group will not have SD_SHARE_CPUCAPACITY, so it shouldn't run into group_smt_balance.

Quoted 25 lines

[0 1][2 3][4 5][6 7][8 9]
[SMT][SMT][SMT][SMT][SMT]
[-----core----][--core--]

For large configurations this can be true for even higher levels. Therefore, the idea was to move the smt_balance state around and adapt its conditions to something like this (which would require to reorder the commits):

(quoted diff) +0 -0
@@ -8330,6 +8330,11 @@ enum fbq_type { regular, remote, all };
  enum group_type {
         /* The group has spare capacity that can be used to run more

tasks.  */
        group_has_spare = 0,
+       /*
+        * Balance SMT group that's fully busy. Can benefit from migration +        * a task on SMT with busy sibling to another CPU on idle core. +        */
+       group_smt_balance,
        /*
         * The group is fully used and the tasks don't compete for more CPU
         * cycles. Nevertheless, some tasks might wait before running.

(quoted diff) +0 -0
@@ -8340,11 +8345,6 @@ enum group_type {
          * more powerful CPU.

Review enum group_type { · line 8350

Quoted 10 lines
(quoted diff) +0 -5
          */
         group_misfit_task,
-       /*
-        * Balance SMT group that's fully busy. Can benefit from migration
-        * a task on SMT with busy sibling to another CPU on idle core.
-        */
-       group_smt_balance,
         /*
          * SD_ASYM_PACKING only: One local CPU with higher capacity is
 available,

IIUC, for cluster topology of this patch, busiest group should be a SMT if it has 2 threads compared to an Atom cluster having 4 threads. Atom cluster will be group_fully_busy, whereas SMT group will be group_smt_balance. For that to happen group_smt_balance should have higher group_type.

Quoted 22 lines

         * and the task should be migrated to it instead of running on the

(quoted diff) +3 -3
@@ -9327,15 +9327,15 @@ group_type group_classify(unsigned int
 imbalance_pct,
         if (sgs->group_asym_packing)
                 return group_asym_packing;
 
-       if (sgs->group_smt_balance)
-               return group_smt_balance;
-
         if (sgs->group_misfit_task_load)
                 return group_misfit_task;
 
         if (!group_has_capacity(imbalance_pct, sgs))
                 return group_fully_busy;
 
+       if (sgs->group_smt_balance)
+               return group_smt_balance;
+
         return group_has_spare;
  }

Review static inline bool smt_balance(struct lb_env *env, · line 9461

Quoted 7 lines
(quoted diff) +1 -2
@@ -9457,8 +9457,7 @@ static inline bool smt_balance(struct lb_env *env,
 struct sg_lb_stats *sgs,
          * Note that if a group has a single SMT, SD_SHARE_CPUCAPACITY
          * will not be on.
          */
-       if (group->flags & SD_SHARE_CPUCAPACITY &&
-           sgs->sum_h_nr_running > 1)
+       if (sgs->sum_h_nr_running > group->cores)

In case of Power10, where we have SMT4, group->cores will be 1. I dont see a difference here.

Quoted 147 lines
(quoted diff) +0 -0
                 return true;
 

        return false;

The s390 problem is currently solved by changing the prefer_sibling path. When
disabling that flag, we might have an issue, will have to verify that though.

Quoted 138 lines
Quoted 138 lines

One example is Lets say sg1 has 4 tasks. and sg2 has 0 tasks and is trying to do
load balance. Previously imbalance would have been 2, instead now imbalance would be 1.
But in subsequent lb it would be balanced.

Review static inline void update_sg_lb_stats(struct

Quoted 7 lines

+    return false;

(quoted diff) +2 -0
+}
+

  static inline bool
  sched_reduced_capacity(struct rq *rq, struct sched_domain *sd) {

(quoted diff) +0 -0
@@ -9425,6 +9464,10 @@ static inline void update_sg_lb_stats(struct

lb_env *env,

Review static inline void update_sg_lb_stats(struct · line 9471

Quoted 8 lines
(quoted diff) +4 -0
           sgs->group_asym_packing = 1;
       }
 
+    /* Check for loaded SMT group to be balanced to dst CPU */
+    if (!local_group && smt_balance(env, sgs, group))
+        sgs->group_smt_balance = 1;
+
       sgs->group_type = group_classify(env->sd->imbalance_pct,

group, sgs);

Review static inline void update_sg_lb_stats(struct · line 9472

Quoted 2 lines
(quoted diff) +0 -0
       /* Computing avg_load makes sense only when group is

overloaded */

Review static bool update_sd_pick_busiest(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -9509,6 +9552,7 @@ static bool update_sd_pick_busiest(struct

lb_env *env,

Review static bool update_sd_pick_busiest(struct

Quoted 8 lines
(quoted diff) +1 -0
               return false;
           break;
 
+    case group_smt_balance:

      case group_fully_busy:
          /*
           * Select the fully busy group with highest avg_load. In

(quoted diff) +0 -0
@@ -9537,6 +9581,18 @@ static bool update_sd_pick_busiest(struct

lb_env *env,

Quoted 10 lines
(quoted diff) +0 -0
           break;
 

      case group_has_spare:
+        /*
+         * Do not pick sg with SMT CPUs over sg with pure CPUs, +         * as we do not want to pull task off SMT core with one task +         * and make the core idle.
+         */
+        if (smt_vs_nonsmt_groups(sds->busiest, sg)) {
+            if (sg->flags & SD_SHARE_CPUCAPACITY &&

sgs->sum_h_nr_running <= 1)

Quoted 6 lines

+                return false;
+            else
+                return true;> +        }
+
          /*
           * Select not overloaded group with lowest number of idle

cpus

Review static bool update_pick_idlest(struct

Quoted 2 lines

           * and highest number of running tasks. We could also compare

(quoted diff) +0 -0
@@ -9733,6 +9789,7 @@ static bool update_pick_idlest(struct

sched_group *idlest,

Review find_idlest_group(struct sched_domain *sd,

Quoted 8 lines

      case group_imbalanced:
      case group_asym_packing:
+    case group_smt_balance:
          /* Those types are not used in the slow wakeup path */ return false;

(quoted diff) +0 -0
@@ -9864,6 +9921,7 @@ find_idlest_group(struct sched_domain *sd,

struct task_struct *p, int this_cpu)

Review static inline void

Quoted 8 lines

      case group_imbalanced:
      case group_asym_packing:
+    case group_smt_balance:
          /* Those type are not used in the slow wakeup path */ return NULL;

(quoted diff) +0 -0
@@ -10118,6 +10176,13 @@ static inline void

calculate_imbalance(struct lb_env *env, struct sd_lb_stats *s

Review static inline void · line 10188

Quoted 13 lines
(quoted diff) +7 -0
           return;
       }
 
+    if (busiest->group_type == group_smt_balance) {
+        /* Reduce number of tasks sharing CPU capacity */
+        env->migration_type = migrate_task;
+        env->imbalance = 1;
+        return;
+    }
+
       if (busiest->group_type == group_imbalanced) {
           /*
            * In the group_imb case we cannot rely on group-wide

averages

Review static struct sched_group

Quoted 1 line
(quoted diff) +0 -0
@@ -10363,16 +10428,23 @@ static struct sched_group

*find_busiest_group(struct lb_env *env)

Quoted 6 lines
(quoted diff) +1 -1
           goto force_balance;
 
       if (busiest->group_type != group_overloaded) {
-        if (env->idle == CPU_NOT_IDLE)
+        if (env->idle == CPU_NOT_IDLE) {
               /*

Review static struct sched_group · line 10442

Quoted 10 lines
(quoted diff) +5 -0
                * If the busiest group is not overloaded (and as a
                * result the local one too) but this CPU is already
                * busy, let another idle CPU try to pull task.
                */
               goto out_balanced;
+        }
+
+        if (busiest->group_type == group_smt_balance &&
+            smt_vs_nonsmt_groups(sds.local, sds.busiest)) {
+            /* Let non SMT CPU pull from SMT CPU sharing with

sibling */

Review static struct sched_group

Quoted 10 lines
kernel/sched/fair.c +2 -0
+            goto force_balance;
+        }
 

          if (busiest->group_weight > 1 &&
-            local->idle_cpus <= (busiest->idle_cpus + 1)) +            local->idle_cpus <= (busiest->idle_cpus + 1)) { /*
               * If the busiest group is not overloaded
               * and there is no imbalance between this and busiest

(quoted diff) +0 -0
@@ -10383,12 +10455,14 @@ static struct sched_group

*find_busiest_group(struct lb_env *env)

Quoted 15 lines
(quoted diff) +3 -1
                * there is more than 1 CPU per group.
                */
               goto out_balanced;
+        }
 
-        if (busiest->sum_h_nr_running == 1)
+        if (busiest->sum_h_nr_running == 1) {
               /*
                * busiest doesn't have any tasks waiting to run
                */
               goto out_balanced;
+        }
       }
 

  force_balance:

#30 lore raw

Re: [Patch v3 1/6] sched/fair: Determine active load balance for SMT sched groups

Quoted 75 lines · Shrikanth Hegde
Quoted 65 lines · Tobias Huschle
Quoted 54 lines · Tim Chen
Quoted 11 lines · Shrikanth Hegde

If we consider symmetric platforms which have SMT4 such as power10. we have a topology like below. multiple such MC will form DIE(PKG)

[0 2 4 6][1 3 5 7][8 10 12 14][9 11 13 15]
[--SMT--][--SMT--][----SMT---][---SMT----]
[--sg1--][--sg1--][---sg1----][---sg1----]
[--------------MC------------------------]

Quoted 8 lines

In case of SMT4, if there is any group which has 2 or more tasks, that group will be marked as group_smt_balance. previously, if that group had 2
or 3 tasks, it would have been marked as group_has_spare. Since all the groups have
SMT that means behavior would be same fully busy right? That can cause some
corner cases. No?

You raised a good point. I was looking from SMT2 perspective so group_smt_balance implies group_fully_busy. That is no longer true for SMT4.

I am thinking of the following fix on the current patch to take care of SMT4. Do you think this addresses concerns from you and Tobias?

kernel/sched/fair.c +0 -0
index 294a662c9410..3fc8d3a3bd22 100644
@@ -9588,6 +9588,17 @@ static bool update_sd_pick_busiest(struct
 lb_env *env,
                 break;
 

        case group_smt_balance:
+               /* no idle cpus on both groups handled by group_fully_busy below */
+               if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) { +                       if (sgs->idle_cpus > busiest->idle_cpus) +                               return false;
+                       if (sgs->idle_cpus < busiest->idle_cpus) +                               return true;
+                       if (sgs->sum_nr_running <= busiest_sum_nr_running)
+                               return false;
+                       else
+                               return true;
+               }

Quoted 4 lines

I will be on vacation next three weeks so my response will be slow.

Tim

What if the setup is asymmetric, where SMT2 and SMT4 would mix, e.g.

[0 1][2 3 4 5]
[SMT][--SMT--]

Quoted 7 lines

If now CPUs 0,2,3 have a running task, both groups would be classified as smt_balance. But if it comes to the selection of the busiest group, the smaller
group would be selected, as it has less idle CPUs, right? Which could lead to the smaller group being left with no tasks.
Using the absolute numbers of task is what made the prefer_sibling path problematic,

Quoted 3 lines

Yes. But Not sure how realistic is that configuration. on power10, we typically have all cores in either SMT1, SMT2 or SMT4. But not mixed configs. One can offline a CPUs to get into that cases in SMT4.

I'm also not sure if there is a real case for that. The assumption that two groups are always of equal size was the issue why the prefer_sibling path did not work as expected. I just wanted to point out that we might introduce a similar assumption here again. It might be valid to assume that if there are no usecases for having two cores with a different number of SMT threads.

Quoted 55 lines
Quoted 8 lines

I would assume that the same holds true here. Therefore, I would prefer avg_load,
or, similar to prefer_siblings, a ratio over the number of cores.

I can't really test that on s390 as we always have SMT2. But, we can have these
asymmetries on higher levels, e.g

IIUC, on higher levels, group will not have SD_SHARE_CPUCAPACITY, so it shouldn't
run into group_smt_balance.

Quoted 31 lines

[0 1][2 3][4 5][6 7][8 9]
[SMT][SMT][SMT][SMT][SMT]
[-----core----][--core--]

For large configurations this can be true for even higher levels. Therefore, the idea was to move the smt_balance state around and adapt its conditions to something like this (which would require to reorder the commits):

(quoted diff) +0 -0
@@ -8330,6 +8330,11 @@ enum fbq_type { regular, remote, all };
  enum group_type {
         /* The group has spare capacity that can be used to run more

tasks.  */
        group_has_spare = 0,
+       /*
+        * Balance SMT group that's fully busy. Can benefit from migration
+        * a task on SMT with busy sibling to another CPU on idle core.
+        */
+       group_smt_balance,
        /*
         * The group is fully used and the tasks don't compete for more CPU
         * cycles. Nevertheless, some tasks might wait before running.

(quoted diff) +0 -2
@@ -8340,11 +8345,6 @@ enum group_type {
          * more powerful CPU.
          */
         group_misfit_task,
-       /*
-        * Balance SMT group that's fully busy. Can benefit from 
Quoted 8 lines

migration
-        * a task on SMT with busy sibling to another CPU on idle core.
-        */
-       group_smt_balance,
        /*
         * SD_ASYM_PACKING only: One local CPU with higher capacity is available,

Quoted 7 lines

IIUC, for cluster topology of this patch, busiest group should be a SMT if it has 2
threads compared to an Atom cluster having 4 threads. Atom cluster will be group_fully_busy,
whereas SMT group will be group_smt_balance. For that to happen group_smt_balance should have
higher group_type.

Makes sense.

Quoted 22 lines
Quoted 22 lines

         * and the task should be migrated to it instead of running on the

(quoted diff) +3 -3
@@ -9327,15 +9327,15 @@ group_type group_classify(unsigned int
 imbalance_pct,
         if (sgs->group_asym_packing)
                 return group_asym_packing;
 
-       if (sgs->group_smt_balance)
-               return group_smt_balance;
-
         if (sgs->group_misfit_task_load)
                 return group_misfit_task;
 
         if (!group_has_capacity(imbalance_pct, sgs))
                 return group_fully_busy;
 
+       if (sgs->group_smt_balance)
+               return group_smt_balance;
+
         return group_has_spare;
  }
Quoted 12 lines

Review static inline bool smt_balance(struct lb_env · line 9462

Quoted 9 lines
(quoted diff) +1 -2
@@ -9457,8 +9457,7 @@ static inline bool smt_balance(struct lb_env 
 *env,
 struct sg_lb_stats *sgs,
          * Note that if a group has a single SMT, SD_SHARE_CPUCAPACITY
          * will not be on.
          */
-       if (group->flags & SD_SHARE_CPUCAPACITY &&
-           sgs->sum_h_nr_running > 1)
+       if (sgs->sum_h_nr_running > group->cores)

In case of Power10, where we have SMT4, group->cores will be 1. I dont see a difference here.

The aim of this change was to also make use of this further up in the hierarchy, where SD_SHARE_CPUCAPACITY is not set. Up there, it would be possible to have more than one core, also potentially different numbers (at least on s390).

It appears to work fine without these changes though, so I think there is nothing to do for now.

Quoted 148 lines
Quoted 148 lines
(quoted diff) +0 -0
                 return true;

        return false;

The s390 problem is currently solved by changing the prefer_sibling path. When
disabling that flag, we might have an issue, will have to verify that though.

Quoted 138 lines
Quoted 138 lines

One example is Lets say sg1 has 4 tasks. and sg2 has 0 tasks and is trying to do
load balance. Previously imbalance would have been 2, instead now imbalance would be 1.
But in subsequent lb it would be balanced.

Review static inline void update_sg_lb_stats(struct

Quoted 7 lines

+    return false;

(quoted diff) +2 -0
+}
+

  static inline bool
  sched_reduced_capacity(struct rq *rq, struct sched_domain *sd) {

(quoted diff) +0 -0
@@ -9425,6 +9464,10 @@ static inline void update_sg_lb_stats(struct

lb_env *env,

Review static inline void update_sg_lb_stats(struct · line 9471

Quoted 8 lines
(quoted diff) +4 -0
           sgs->group_asym_packing = 1;
       }
 
+    /* Check for loaded SMT group to be balanced to dst CPU */
+    if (!local_group && smt_balance(env, sgs, group))
+        sgs->group_smt_balance = 1;
+
       sgs->group_type = group_classify(env->sd->imbalance_pct,

group, sgs);

Review static inline void update_sg_lb_stats(struct · line 9472

Quoted 2 lines
(quoted diff) +0 -0
       /* Computing avg_load makes sense only when group is

overloaded */

Review static bool update_sd_pick_busiest(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -9509,6 +9552,7 @@ static bool update_sd_pick_busiest(struct

lb_env *env,

Review static bool update_sd_pick_busiest(struct

Quoted 8 lines
(quoted diff) +1 -0
               return false;
           break;
 
+    case group_smt_balance:

      case group_fully_busy:
          /*
           * Select the fully busy group with highest avg_load. In

(quoted diff) +0 -0
@@ -9537,6 +9581,18 @@ static bool update_sd_pick_busiest(struct

lb_env *env,

Quoted 10 lines
(quoted diff) +0 -0
           break;
 

      case group_has_spare:
+        /*
+         * Do not pick sg with SMT CPUs over sg with pure CPUs, +         * as we do not want to pull task off SMT core with one task +         * and make the core idle.
+         */
+        if (smt_vs_nonsmt_groups(sds->busiest, sg)) {
+            if (sg->flags & SD_SHARE_CPUCAPACITY &&

sgs->sum_h_nr_running <= 1)

Quoted 6 lines

+                return false;
+            else
+                return true;> +        }
+
          /*
           * Select not overloaded group with lowest number of idle

cpus

Review static bool update_pick_idlest(struct

Quoted 2 lines

           * and highest number of running tasks. We could also compare

(quoted diff) +0 -0
@@ -9733,6 +9789,7 @@ static bool update_pick_idlest(struct

sched_group *idlest,

Review find_idlest_group(struct sched_domain *sd,

Quoted 8 lines

      case group_imbalanced:
      case group_asym_packing:
+    case group_smt_balance:
          /* Those types are not used in the slow wakeup path */ return false;

(quoted diff) +0 -0
@@ -9864,6 +9921,7 @@ find_idlest_group(struct sched_domain *sd,

struct task_struct *p, int this_cpu)

Review static inline void

Quoted 8 lines

      case group_imbalanced:
      case group_asym_packing:
+    case group_smt_balance:
          /* Those type are not used in the slow wakeup path */ return NULL;

(quoted diff) +0 -0
@@ -10118,6 +10176,13 @@ static inline void

calculate_imbalance(struct lb_env *env, struct sd_lb_stats *s

Review static inline void · line 10188

Quoted 13 lines
(quoted diff) +7 -0
           return;
       }
 
+    if (busiest->group_type == group_smt_balance) {
+        /* Reduce number of tasks sharing CPU capacity */
+        env->migration_type = migrate_task;
+        env->imbalance = 1;
+        return;
+    }
+
       if (busiest->group_type == group_imbalanced) {
           /*
            * In the group_imb case we cannot rely on group-wide

averages

Review static struct sched_group

Quoted 1 line
(quoted diff) +0 -0
@@ -10363,16 +10428,23 @@ static struct sched_group

*find_busiest_group(struct lb_env *env)

Quoted 6 lines
(quoted diff) +1 -1
           goto force_balance;
 
       if (busiest->group_type != group_overloaded) {
-        if (env->idle == CPU_NOT_IDLE)
+        if (env->idle == CPU_NOT_IDLE) {
               /*

Review static struct sched_group · line 10442

Quoted 10 lines
(quoted diff) +5 -0
                * If the busiest group is not overloaded (and as a
                * result the local one too) but this CPU is already
                * busy, let another idle CPU try to pull task.
                */
               goto out_balanced;
+        }
+
+        if (busiest->group_type == group_smt_balance &&
+            smt_vs_nonsmt_groups(sds.local, sds.busiest)) {
+            /* Let non SMT CPU pull from SMT CPU sharing with

sibling */

Review static struct sched_group

Quoted 10 lines
kernel/sched/fair.c +2 -0
+            goto force_balance;
+        }
 

          if (busiest->group_weight > 1 &&
-            local->idle_cpus <= (busiest->idle_cpus + 1)) +            local->idle_cpus <= (busiest->idle_cpus + 1)) { /*
               * If the busiest group is not overloaded
               * and there is no imbalance between this and busiest

(quoted diff) +0 -0
@@ -10383,12 +10455,14 @@ static struct sched_group

*find_busiest_group(struct lb_env *env)

Quoted 15 lines
(quoted diff) +3 -1
                * there is more than 1 CPU per group.
                */
               goto out_balanced;
+        }
 
-        if (busiest->sum_h_nr_running == 1)
+        if (busiest->sum_h_nr_running == 1) {
               /*
                * busiest doesn't have any tasks waiting to run
                */
               goto out_balanced;
+        }
       }
 

  force_balance:

#31 lore raw

Re: [Patch v3 1/6] sched/fair: Determine active load balance for SMT sched groups

In reply to Tim Chen
Quoted 46 lines · Tim Chen

From: Tim C Chen <[email protected]>

On hybrid CPUs with scheduling cluster enabled, we will need to consider balancing between SMT CPU cluster, and Atom core cluster.

Below shows such a hybrid x86 CPU with 4 big cores and 8 atom cores. Each scheduling cluster span a L2 cache.

--L2-- --L2-- --L2-- --L2-- ----L2---- -----L2------ [0, 1] [2, 3] [4, 5] [5, 6] [7 8 9 10] [11 12 13 14]
Big Big Big Big Atom Atom
core core core core Module Module

If the busiest group is a big core with both SMT CPUs busy, we should active load balance if destination group has idle CPU cores. Such condition is considered by asym_active_balance() in load balancing but not considered when looking for busiest group and computing load imbalance. Add this consideration in find_busiest_group() and calculate_imbalance().

In addition, update the logic determining the busier group when one group is SMT and the other group is non SMT but both groups are partially busy with idle CPU. The busier group should be the group with idle cores rather than the group with one busy SMT CPU. We do not want to make the SMT group the busiest one to pull the only task off SMT CPU and causing the whole core to go empty.

Otherwise suppose in the search for the busiest group, we first encounter an SMT group with 1 task and set it as the busiest. The destination group is an atom cluster with 1 task and we next encounter an atom cluster group with 3 tasks, we will not pick this atom cluster over the SMT group, even though we should. As a result, we do not load balance the busier Atom cluster (with 3 tasks) towards the local atom cluster (with 1 task). And it doesn't make sense to pick the 1 task SMT group as the busier group as we also should not pull task off the SMT towards the 1 task atom cluster and make the SMT core completely empty.

---
 kernel/sched/fair.c | 80 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 77 insertions(+), 3 deletions(-)
kernel/sched/fair.c +0 -0
index 87317634fab2..f636d6c09dc6 100644

kernel/sched/fair.c enum group_type { · line 8286

Quoted 8 lines
kernel/sched/fair.c +5 -0
@@ -8279,6 +8279,11 @@ enum group_type {
 	 * more powerful CPU.
 	 */
 	group_misfit_task,
+	/*
+	 * Balance SMT group that's fully busy. Can benefit from migration
+	 * a task on SMT with busy sibling to another CPU on idle core.
+	 */
+	group_smt_balance,

Would it make sense to move smt_balance?, s.t. we get:

group_has_spare < group_smt_balance < group_fully_busy

Conceptually I would be more intuitive to me like this as the smt_balance groups are more busy than has_spare ones, but less busy then fully_busy ones.

From a functional perspective I could also see some impact when update_sd_pick_busiest compares the group types. In that case we would remove tasks from fully busy groups before moving them from smt_balance groups. Not sure which way would be to prefer to increase overall throughput.

Since smt_balance is only selected if the group has SMT, this should still not pull the last task off of a non-SMT CPU.

Quoted 184 lines
kernel/sched/fair.c +0 -0
 	/*
 	 * SD_ASYM_PACKING only: One local CPU with higher capacity is 
 available,
kernel/sched/fair.c +0 -0
 	 * and the task should be migrated to it instead of running on the
@@ -8987,6 +8992,7 @@ struct sg_lb_stats {
 	unsigned int group_weight;
 	enum group_type group_type;
 	unsigned int group_asym_packing; /* Tasks should be moved to 

preferred CPU */

kernel/sched/fair.c +1 -0
+	unsigned int group_smt_balance;  /* Task on busy SMT be moved */
 	unsigned long group_misfit_task_load; /* A CPU has a task too big

for its capacity */
#ifdef CONFIG_NUMA_BALANCING

kernel/sched/fair.c +3 -0
 	unsigned int nr_numa_running;
@@ -9260,6 +9266,9 @@ group_type group_classify(unsigned int 
 imbalance_pct,
 	if (sgs->group_asym_packing)
 		return group_asym_packing;
 
+	if (sgs->group_smt_balance)
+		return group_smt_balance;
+
 	if (sgs->group_misfit_task_load)
 		return group_misfit_task;
@@ -9333,6 +9342,36 @@ sched_asym(struct lb_env *env, struct

sd_lb_stats *sds, struct sg_lb_stats *sgs

kernel/sched/fair.c +1 -0
 	return sched_asym_prefer(env->dst_cpu, group->asym_prefer_cpu);
 }
 
+/* One group has more than one SMT CPU while the other group does not 

*/

kernel/sched/fair.c +11 -0
+static inline bool smt_vs_nonsmt_groups(struct sched_group *sg1,
+				    struct sched_group *sg2)
+{
+	if (!sg1 || !sg2)
+		return false;
+
+	return (sg1->flags & SD_SHARE_CPUCAPACITY) !=
+		(sg2->flags & SD_SHARE_CPUCAPACITY);
+}
+
+static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats 

*sgs,

kernel/sched/fair.c +8 -0
+			       struct sched_group *group)
+{
+	if (env->idle == CPU_NOT_IDLE)
+		return false;
+
+	/*
+	 * For SMT source group, it is better to move a task
+	 * to a CPU that doesn't have multiple tasks sharing its CPU 

capacity.

kernel/sched/fair.c +14 -0
+	 * Note that if a group has a single SMT, SD_SHARE_CPUCAPACITY
+	 * will not be on.
+	 */
+	if (group->flags & SD_SHARE_CPUCAPACITY &&
+	    sgs->sum_h_nr_running > 1)
+		return true;
+
+	return false;
+}
+
 static inline bool
 sched_reduced_capacity(struct rq *rq, struct sched_domain *sd)
 {
@@ -9425,6 +9464,10 @@ static inline void update_sg_lb_stats(struct 
 lb_env *env,
 		sgs->group_asym_packing = 1;
 	}
 
+	/* Check for loaded SMT group to be balanced to dst CPU */
+	if (!local_group && smt_balance(env, sgs, group))
+		sgs->group_smt_balance = 1;
+
 	sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs);
 
kernel/sched/fair.c +1 -0
 	/* Computing avg_load makes sense only when group is overloaded */
@@ -9509,6 +9552,7 @@ static bool update_sd_pick_busiest(struct lb_env 
 *env,
 			return false;
 		break;
 
+	case group_smt_balance:
 	case group_fully_busy:
 		/*
kernel/sched/fair.c +12 -0
 		 * Select the fully busy group with highest avg_load. In
@@ -9537,6 +9581,18 @@ static bool update_sd_pick_busiest(struct lb_env 
 *env,
 		break;
 
 	case group_has_spare:
+		/*
+		 * Do not pick sg with SMT CPUs over sg with pure CPUs,
+		 * as we do not want to pull task off SMT core with one task
+		 * and make the core idle.
+		 */
+		if (smt_vs_nonsmt_groups(sds->busiest, sg)) {
+			if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1)
+				return false;
+			else
+				return true;
+		}
+
 		/*
 		 * Select not overloaded group with lowest number of idle cpus
kernel/sched/fair.c +2 -0
 		 * and highest number of running tasks. We could also compare
@@ -9733,6 +9789,7 @@ static bool update_pick_idlest(struct sched_group 
 *idlest,
 
 	case group_imbalanced:
 	case group_asym_packing:
+	case group_smt_balance:
 		/* Those types are not used in the slow wakeup path */
 		return false;
@@ -9864,6 +9921,7 @@ find_idlest_group(struct sched_domain *sd,
 struct task_struct *p, int this_cpu)
 
 	case group_imbalanced:
 	case group_asym_packing:
+	case group_smt_balance:
 		/* Those type are not used in the slow wakeup path */
 		return NULL;
@@ -10118,6 +10176,13 @@ static inline void calculate_imbalance(struct

lb_env *env, struct sd_lb_stats *s

kernel/sched/fair.c +16 -2
 		return;
 	}
 
+	if (busiest->group_type == group_smt_balance) {
+		/* Reduce number of tasks sharing CPU capacity */
+		env->migration_type = migrate_task;
+		env->imbalance = 1;
+		return;
+	}
+
 	if (busiest->group_type == group_imbalanced) {
 		/*
 		 * In the group_imb case we cannot rely on group-wide averages
@@ -10363,16 +10428,23 @@ static struct sched_group
 *find_busiest_group(struct lb_env *env)
 		goto force_balance;
 
 	if (busiest->group_type != group_overloaded) {
-		if (env->idle == CPU_NOT_IDLE)
+		if (env->idle == CPU_NOT_IDLE) {
 			/*
 			 * If the busiest group is not overloaded (and as a
 			 * result the local one too) but this CPU is already
 			 * busy, let another idle CPU try to pull task.
 			 */
 			goto out_balanced;
+		}
+
+		if (busiest->group_type == group_smt_balance &&
+		    smt_vs_nonsmt_groups(sds.local, sds.busiest)) {
+			/* Let non SMT CPU pull from SMT CPU sharing with sibling */
+			goto force_balance;
+		}
 
 		if (busiest->group_weight > 1 &&
-		    local->idle_cpus <= (busiest->idle_cpus + 1))
+		    local->idle_cpus <= (busiest->idle_cpus + 1)) {
 			/*
 			 * If the busiest group is not overloaded
kernel/sched/fair.c +3 -1
 			 * and there is no imbalance between this and busiest
@@ -10383,12 +10455,14 @@ static struct sched_group
 *find_busiest_group(struct lb_env *env)
 			 * there is more than 1 CPU per group.
 			 */
 			goto out_balanced;
+		}
 
-		if (busiest->sum_h_nr_running == 1)
+		if (busiest->sum_h_nr_running == 1) {
 			/*
 			 * busiest doesn't have any tasks waiting to run
 			 */
 			goto out_balanced;
+		}
 	}
 

force_balance:

author
#32 lore raw

Re: [Patch v3 1/6] sched/fair: Determine active load balance for SMT sched groups

Quoted 24 lines
Quoted 7 lines
kernel/sched/fair.c +0 -0
index 87317634fab2..f636d6c09dc6 100644

kernel/sched/fair.c enum group_type { · line 8286

Quoted 8 lines
kernel/sched/fair.c +5 -0
@@ -8279,6 +8279,11 @@ enum group_type {
 	 * more powerful CPU.
 	 */
 	group_misfit_task,
+	/*
+	 * Balance SMT group that's fully busy. Can benefit from migration
+	 * a task on SMT with busy sibling to another CPU on idle core.
+	 */
+	group_smt_balance,

Would it make sense to move smt_balance?, s.t. we get:

group_has_spare < group_smt_balance < group_fully_busy

Conceptually I would be more intuitive to me like this as the smt_balance groups are more busy than has_spare ones, but less busy then fully_busy ones.

Quoted 10 lines

From a functional perspective I could also see some impact when
update_sd_pick_busiest compares the group types. In that case we would remove tasks from fully busy groups before moving them from smt_balance groups. Not sure which way would be to prefer to increase overall throughput.

Since smt_balance is only selected if the group has SMT, this should still not pull the last task off of a non-SMT CPU.

I think you have similar concerns as Shrikanth on this patch. Can you see if my fix to update_sd_pick_busiest() in my reply to Shrikanth addresses what you have in mind.

Tim

author
#33 lore raw

PATCH v3 2/6 sched/topology: Record number of cores in sched group

In reply to Tim Chen

From: Tim C Chen <[email protected]>

When balancing sibling domains that have different number of cores, tasks in respective sibling domain should be proportional to the number of cores in each domain. In preparation of implementing such a policy, record the number of tasks in a scheduling group.

---
 kernel/sched/sched.h    |  1 +
 kernel/sched/topology.c | 10 +++++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)
kernel/sched/sched.h +1 -0
index 3d0eb36350d2..5f7f36e45b87 100644
@@ -1860,6 +1860,7 @@ struct sched_group {
 	atomic_t		ref;
 
 	unsigned int		group_weight;
+	unsigned int		cores;
 	struct sched_group_capacity *sgc;
 	int			asym_prefer_cpu;	/* CPU of highest priority in group */
 	int			flags;
kernel/sched/topology.c +9 -1
index 6d5628fcebcf..6b099dbdfb39 100644
@@ -1275,14 +1275,22 @@ build_sched_groups(struct sched_domain *sd, int cpu)
 static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
 {
 	struct sched_group *sg = sd->groups;
+	struct cpumask *mask = sched_domains_tmpmask2;
 
 	WARN_ON(!sg);
 
 	do {
-		int cpu, max_cpu = -1;
+		int cpu, cores = 0, max_cpu = -1;
 
 		sg->group_weight = cpumask_weight(sched_group_span(sg));
 
+		cpumask_copy(mask, sched_group_span(sg));
+		for_each_cpu(cpu, mask) {
+			cores++;
+			cpumask_andnot(mask, mask, cpu_smt_mask(cpu));
+		}
+		sg->cores = cores;
+
 		if (!(sd->flags & SD_ASYM_PACKING))
 			goto next;
 
Signature · 1 line
2.32.0
#34 lore raw

Re: [Patch v3 2/6] sched/topology: Record number of cores in sched group

Quoted 38 lines · Tim Chen

From: Tim C Chen <[email protected]>

When balancing sibling domains that have different number of cores, tasks in respective sibling domain should be proportional to the number of cores in each domain. In preparation of implementing such a policy, record the number of tasks in a scheduling group.

---
 kernel/sched/sched.h    |  1 +
 kernel/sched/topology.c | 10 +++++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)
kernel/sched/sched.h +1 -0
index 3d0eb36350d2..5f7f36e45b87 100644
@@ -1860,6 +1860,7 @@ struct sched_group {
      atomic_t		ref;
 
      unsigned int		group_weight;
+	unsigned int		cores;
      struct sched_group_capacity *sgc;
      int			asym_prefer_cpu;	/* CPU of highest priority in group */
      int			flags;
kernel/sched/topology.c +1 -0
index 6d5628fcebcf..6b099dbdfb39 100644
@@ -1275,14 +1275,22 @@ build_sched_groups(struct sched_domain *sd, int cpu)
 static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
 {
      struct sched_group *sg = sd->groups;
+	struct cpumask *mask = sched_domains_tmpmask2;
 
      WARN_ON(!sg);
 
      do {

kernel/sched/topology.c build_sched_groups(struct sched_domain *sd, int cpu) · line 1291

Quoted 10 lines
kernel/sched/topology.c +6 -1
-		int cpu, max_cpu = -1;
+		int cpu, cores = 0, max_cpu = -1;
 
              sg->group_weight = cpumask_weight(sched_group_span(sg));
 
+		cpumask_copy(mask, sched_group_span(sg));
+		for_each_cpu(cpu, mask) {
+			cores++;
+			cpumask_andnot(mask, mask, cpu_smt_mask(cpu));
+		}

This rekindled my desire for an SMT core cpumask/iterator. I played around with a global mask but that's a headache: what if we end up with a core whose SMT threads are split across two exclusive cpusets?

I ended up necro'ing a patch from Peter [1], but didn't get anywhere nice (the LLC shared storage caused me issues).

All that to say, I couldn't think of a nicer way :(

[1]: https://lore.kernel.org/all/[email protected]/#t

author
#35 lore raw

Re: [Patch v3 2/6] sched/topology: Record number of cores in sched group

Quoted 44 lines · Valentin Schneider
Quoted 43 lines · Tim Chen

From: Tim C Chen <[email protected]>

When balancing sibling domains that have different number of cores, tasks in respective sibling domain should be proportional to the number of cores in each domain. In preparation of implementing such a policy, record the number of tasks in a scheduling group.

---
 kernel/sched/sched.h    |  1 +
 kernel/sched/topology.c | 10 +++++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)
kernel/sched/sched.h +1 -0
index 3d0eb36350d2..5f7f36e45b87 100644
@@ -1860,6 +1860,7 @@ struct sched_group {
      atomic_t		ref;
 
      unsigned int		group_weight;
+	unsigned int		cores;
      struct sched_group_capacity *sgc;
      int			asym_prefer_cpu;	/* CPU of highest priority in group */
      int			flags;
kernel/sched/topology.c +2 -1
index 6d5628fcebcf..6b099dbdfb39 100644
@@ -1275,14 +1275,22 @@ build_sched_groups(struct sched_domain *sd, int cpu)
 static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
 {
      struct sched_group *sg = sd->groups;
+	struct cpumask *mask = sched_domains_tmpmask2;
 
      WARN_ON(!sg);
 
      do {
-		int cpu, max_cpu = -1;
+		int cpu, cores = 0, max_cpu = -1;
 
              sg->group_weight = cpumask_weight(sched_group_span(sg));
 
Quoted 10 lines

kernel/sched/topology.c build_sched_groups(struct sched_domain *sd, int cpu) · line 1291

Quoted 5 lines
kernel/sched/topology.c +5 -0
+		cpumask_copy(mask, sched_group_span(sg));
+		for_each_cpu(cpu, mask) {
+			cores++;
+			cpumask_andnot(mask, mask, cpu_smt_mask(cpu));
+		}

This rekindled my desire for an SMT core cpumask/iterator. I played around with a global mask but that's a headache: what if we end up with a core whose SMT threads are split across two exclusive cpusets?

Peter and I pondered that for a while. But it seems like partitioning threads in a core between two different sched domains is not a very reasonable thing to do.

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

Tim

Quoted 8 lines

I ended up necro'ing a patch from Peter [1], but didn't get anywhere nice (the LLC shared storage caused me issues).

All that to say, I couldn't think of a nicer way :(

[1]: https://lore.kernel.org/all/[email protected]/#t

reviewer Reviewed-by
#36 lore raw

Re: [Patch v3 2/6] sched/topology: Record number of cores in sched group

Quoted 56 lines · Tim Chen
Quoted 44 lines · Valentin Schneider
Quoted 43 lines · Tim Chen

From: Tim C Chen <[email protected]>

When balancing sibling domains that have different number of cores, tasks in respective sibling domain should be proportional to the number of cores in each domain. In preparation of implementing such a policy, record the number of tasks in a scheduling group.

---
 kernel/sched/sched.h    |  1 +
 kernel/sched/topology.c | 10 +++++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)
kernel/sched/sched.h +1 -0
index 3d0eb36350d2..5f7f36e45b87 100644
@@ -1860,6 +1860,7 @@ struct sched_group {
      atomic_t		ref;
 
      unsigned int		group_weight;
+	unsigned int		cores;
      struct sched_group_capacity *sgc;
      int			asym_prefer_cpu;	/* CPU of highest priority in group */
      int			flags;
kernel/sched/topology.c +2 -1
index 6d5628fcebcf..6b099dbdfb39 100644
@@ -1275,14 +1275,22 @@ build_sched_groups(struct sched_domain *sd, int cpu)
 static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
 {
      struct sched_group *sg = sd->groups;
+	struct cpumask *mask = sched_domains_tmpmask2;
 
      WARN_ON(!sg);
 
      do {
-		int cpu, max_cpu = -1;
+		int cpu, cores = 0, max_cpu = -1;
 
              sg->group_weight = cpumask_weight(sched_group_span(sg));
 
Quoted 10 lines

kernel/sched/topology.c build_sched_groups(struct sched_domain *sd, int cpu) · line 1291

Quoted 5 lines
kernel/sched/topology.c +5 -0
+		cpumask_copy(mask, sched_group_span(sg));
+		for_each_cpu(cpu, mask) {
+			cores++;
+			cpumask_andnot(mask, mask, cpu_smt_mask(cpu));
+		}

This rekindled my desire for an SMT core cpumask/iterator. I played around with a global mask but that's a headache: what if we end up with a core whose SMT threads are split across two exclusive cpusets?

Quoted 6 lines

Peter and I pondered that for a while. But it seems like partitioning threads in a core between two different sched domains is not a very reasonable thing to do.

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

Thanks for the link. I'll poke at this a bit more, but regardless:

author
#37 lore raw

Re: [Patch v3 2/6] sched/topology: Record number of cores in sched group

In reply to Tim Chen
Quoted 6 lines · Tim Chen

From: Tim C Chen <[email protected]>

When balancing sibling domains that have different number of cores, tasks in respective sibling domain should be proportional to the number of cores in each domain. In preparation of implementing such a policy, record the number of tasks in a scheduling group.

Caught a typo. Should be "the number of cores" instead of "the number of tasks" in a scheduling group.

Peter, should I send you another patch with the corrected commit log?

Tim

Quoted 47 lines
---
 kernel/sched/sched.h    |  1 +
 kernel/sched/topology.c | 10 +++++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)
kernel/sched/sched.h +1 -0
index 3d0eb36350d2..5f7f36e45b87 100644
@@ -1860,6 +1860,7 @@ struct sched_group {
 	atomic_t		ref;
 
 	unsigned int		group_weight;
+	unsigned int		cores;
 	struct sched_group_capacity *sgc;
 	int			asym_prefer_cpu;	/* CPU of highest priority in group */
 	int			flags;
kernel/sched/topology.c +9 -1
index 6d5628fcebcf..6b099dbdfb39 100644
@@ -1275,14 +1275,22 @@ build_sched_groups(struct sched_domain *sd, int cpu)
 static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
 {
 	struct sched_group *sg = sd->groups;
+	struct cpumask *mask = sched_domains_tmpmask2;
 
 	WARN_ON(!sg);
 
 	do {
-		int cpu, max_cpu = -1;
+		int cpu, cores = 0, max_cpu = -1;
 
 		sg->group_weight = cpumask_weight(sched_group_span(sg));
 
+		cpumask_copy(mask, sched_group_span(sg));
+		for_each_cpu(cpu, mask) {
+			cores++;
+			cpumask_andnot(mask, mask, cpu_smt_mask(cpu));
+		}
+		sg->cores = cores;
+
 		if (!(sd->flags & SD_ASYM_PACKING))
 			goto next;
 
maintainer
#38 lore raw

Re: [Patch v3 2/6] sched/topology: Record number of cores in sched group

Quoted 8 lines · Tim Chen
Quoted 6 lines · Tim Chen

From: Tim C Chen <[email protected]>

When balancing sibling domains that have different number of cores, tasks in respective sibling domain should be proportional to the number of cores in each domain. In preparation of implementing such a policy, record the number of tasks in a scheduling group.

Quoted 4 lines

Caught a typo. Should be "the number of cores" instead of "the number of tasks" in a scheduling group.

Peter, should I send you another patch with the corrected commit log?

I'll fix it up, already had to fix the patch because due to robot finding a compile fail for SCHED_SMT=n builds.

Quoted 12 lines
Quoted 12 lines
kernel/sched/topology.c +2 -1
@@ -1275,14 +1275,22 @@ build_sched_groups(struct sched_domain *sd, int cpu)
 static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
 {
 	struct sched_group *sg = sd->groups;
+	struct cpumask *mask = sched_domains_tmpmask2;
 
 	WARN_ON(!sg);
 
 	do {
-		int cpu, max_cpu = -1;
+		int cpu, cores = 0, max_cpu = -1;
 
Quoted 5 lines

kernel/sched/topology.c build_sched_groups(struct sched_domain *sd, int cpu) · line 1289

Quoted 5 lines
kernel/sched/topology.c +3 -0
 		sg->group_weight = cpumask_weight(sched_group_span(sg));
 
+		cpumask_copy(mask, sched_group_span(sg));
+		for_each_cpu(cpu, mask) {
+			cores++;

#ifdef CONFIG_SCHED_SMT

Quoted 1 line

kernel/sched/topology.c build_sched_groups(struct sched_domain *sd, int cpu) · line 1290

Quoted 1 line
kernel/sched/topology.c +1 -0
+			cpumask_andnot(mask, mask, cpu_smt_mask(cpu));
#else
			__cpumask_clear_cpu(cpu, mask);

#endif

or something along them lines -- should be in queue.git/sched/core already.

Quoted 7 lines
Quoted 6 lines
kernel/sched/topology.c +3 -0
+		}
+		sg->cores = cores;
+
 		if (!(sd->flags & SD_ASYM_PACKING))
 			goto next;
 
author
#39 lore raw

Re: [Patch v3 2/6] sched/topology: Record number of cores in sched group

Quoted 33 lines · Peter Zijlstra
Quoted 8 lines · Tim Chen
Quoted 6 lines · Tim Chen

From: Tim C Chen <[email protected]>

When balancing sibling domains that have different number of cores, tasks in respective sibling domain should be proportional to the number of cores in each domain. In preparation of implementing such a policy, record the number of tasks in a scheduling group.

Quoted 4 lines

Caught a typo. Should be "the number of cores" instead of "the number of tasks" in a scheduling group.

Peter, should I send you another patch with the corrected commit log?

I'll fix it up, already had to fix the patch because due to robot finding a compile fail for SCHED_SMT=n builds.

Quoted 14 lines
Quoted 14 lines
(quoted diff) +2 -1
@@ -1275,14 +1275,22 @@ build_sched_groups(struct sched_domain *sd, int cpu)
 static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
 {
 	struct sched_group *sg = sd->groups;
+	struct cpumask *mask = sched_domains_tmpmask2;
 
 	WARN_ON(!sg);
 
 	do {
-		int cpu, max_cpu = -1;
+		int cpu, cores = 0, max_cpu = -1;
 
 		sg->group_weight = cpumask_weight(sched_group_span(sg));
 
Quoted 7 lines
Quoted 3 lines

Review build_sched_groups(struct sched_domain *sd, int cpu) · line 1289

Quoted 3 lines
(quoted diff) +3 -0
+		cpumask_copy(mask, sched_group_span(sg));
+		for_each_cpu(cpu, mask) {
+			cores++;

#ifdef CONFIG_SCHED_SMT

Quoted 1 line

Review build_sched_groups(struct sched_domain *sd, int cpu) · line 1290

Quoted 1 line
(quoted diff) +1 -0
+			cpumask_andnot(mask, mask, cpu_smt_mask(cpu));
#else
			__cpumask_clear_cpu(cpu, mask);

Thanks for fixing up the non SCHED_SMT.

I think "__cpumask_clear_cpu(cpu, mask);" can be removed.

Since we have already considered the CPU in the iterator, clearing it is unnecessay. So effectively

for_each_cpu(cpu, mask) {
	cores++;
}

should be good enough for the non SCHED_SMT case.

Or replace the patch with the patch below so we don't have #ifdef in the middle of code body. Either way is fine.

---

From 9f19714db69739a7985e46bc1f8334d70a69cf2e Mon Sep 17 00:00:00 2001
Message-Id: <9f19714db69739a7985e46bc1f8334d70a69cf2e.1689092923.git.tim.c.chen@linux.intel.com>
In-Reply-To: <[email protected]>
References: <[email protected]>
From: Tim C Chen <[email protected]>
Date: Wed, 17 May 2023 09:09:54 -0700
Subject: [Patch v3 2/6] sched/topology: Record number of cores in sched group
To: Peter Zijlstra <[email protected]>
Cc: Juri Lelli <[email protected]>, Vincent Guittot <[email protected]>, Ricardo Neri <[email protected]>, Ravi V. Shankar <[email protected]>, Ben Segall <[email protected]>, Daniel Bristot de Oliveira <[email protected]>, Dietmar Eggemann <[email protected]>, Len Brown <[email protected]>, Mel Gorman <[email protected]>, Rafael J. Wysocki <[email protected]>, Srinivas Pandruvada <[email protected]>, Steven Rostedt <[email protected]>, Tim Chen <[email protected]>, Valentin Schneider <[email protected]>, Ionela Voinescu <[email protected]>, [email protected], [email protected], Shrikanth Hegde <[email protected]>, Srikar Dronamraju <[email protected]>, [email protected], Yicong Yang <[email protected]>, Barry Song <[email protected]>, Chen Yu <[email protected]>, Hillf Danton <[email protected]>

When balancing sibling domains that have different number of cores, tasks in respective sibling domain should be proportional to the number of cores in each domain. In preparation of implementing such a policy, record the number of cores in a scheduling group.

---
 kernel/sched/sched.h    |  1 +
 kernel/sched/topology.c | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)
kernel/sched/sched.h +1 -0
index 3d0eb36350d2..5f7f36e45b87 100644
@@ -1860,6 +1860,7 @@ struct sched_group {
 	atomic_t		ref;
 
 	unsigned int		group_weight;
+	unsigned int		cores;
 	struct sched_group_capacity *sgc;
 	int			asym_prefer_cpu;	/* CPU of highest priority in group */
 	int			flags;
kernel/sched/topology.c +21 -0
index 6d5628fcebcf..4ecdaef3f8ab 100644
@@ -1262,6 +1262,26 @@ build_sched_groups(struct sched_domain *sd, int cpu)
 	return 0;
 }
 
+#ifdef CONFIG_SCHED_SMT
+static inline int sched_group_cores(struct sched_group *sg)
+{
+	struct cpumask *mask = sched_domains_tmpmask2;
+	int cpu, cores = 0;
+
+	cpumask_copy(mask, sched_group_span(sg));
+	for_each_cpu(cpu, mask) {
+		cores++;
+		cpumask_andnot(mask, mask, cpu_smt_mask(cpu));
+	}
+	return cores;
+}
+#else
+static inline int sched_group_cores(struct sched_group *sg)
+{
+	return sg->group_weight;
+}
+#endif
+
 /*
  * Initialize sched groups cpu_capacity.
  *
@@ -1282,6 +1302,7 @@ static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
 		int cpu, max_cpu = -1;
 
 		sg->group_weight = cpumask_weight(sched_group_span(sg));
+		sg->cores = sched_group_cores(sg);
 
 		if (!(sd->flags & SD_ASYM_PACKING))
 			goto next;
Signature · 1 line
2.32.0
author
#40 lore raw

PATCH v3 3/6 sched/fair: Implement prefer sibling imbalance calculation between asymmetric groups

In reply to Tim Chen

From: Tim C Chen <[email protected]>

In the current prefer sibling load balancing code, there is an implicit assumption that the busiest sched group and local sched group are equivalent, hence the tasks to be moved is simply the difference in number of tasks between the two groups (i.e. imbalance) divided by two.

However, we may have different number of cores between the cluster groups, say when we take CPU offline or we have hybrid groups. In that case, we should balance between the two groups such that #tasks/#cores ratio is the same between the same between both groups. Hence the imbalance computed will need to reflect this.

Adjust the sibling imbalance computation to take into account of the above considerations.

---
 kernel/sched/fair.c | 41 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 37 insertions(+), 4 deletions(-)
kernel/sched/fair.c +37 -4
index f636d6c09dc6..f491b94908bf 100644
@@ -9372,6 +9372,41 @@ static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats *sgs,
 	return false;
 }
 
+static inline long sibling_imbalance(struct lb_env *env,
+				    struct sd_lb_stats *sds,
+				    struct sg_lb_stats *busiest,
+				    struct sg_lb_stats *local)
+{
+	int ncores_busiest, ncores_local;
+	long imbalance;
+
+	if (env->idle == CPU_NOT_IDLE || !busiest->sum_nr_running)
+		return 0;
+
+	ncores_busiest = sds->busiest->cores;
+	ncores_local = sds->local->cores;
+
+	if (ncores_busiest == ncores_local) {
+		imbalance = busiest->sum_nr_running;
+		lsub_positive(&imbalance, local->sum_nr_running);
+		return imbalance;
+	}
+
+	/* Balance such that nr_running/ncores ratio are same on both groups */
+	imbalance = ncores_local * busiest->sum_nr_running;
+	lsub_positive(&imbalance, ncores_busiest * local->sum_nr_running);
+	/* Normalize imbalance and do rounding on normalization */
+	imbalance = 2 * imbalance + ncores_local + ncores_busiest;
+	imbalance /= ncores_local + ncores_busiest;
+
+	/* Take advantage of resource in an empty sched group */
+	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	    busiest->sum_nr_running > 1)
+		imbalance = 2;
+
+	return imbalance;
+}
+
 static inline bool
 sched_reduced_capacity(struct rq *rq, struct sched_domain *sd)
 {
@@ -10230,14 +10265,12 @@ static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *s
 		}
 
 		if (busiest->group_weight == 1 || sds->prefer_sibling) {
-			unsigned int nr_diff = busiest->sum_nr_running;
 			/*
 			 * When prefer sibling, evenly spread running tasks on
 			 * groups.
 			 */
 			env->migration_type = migrate_task;
-			lsub_positive(&nr_diff, local->sum_nr_running);
-			env->imbalance = nr_diff;
+			env->imbalance = sibling_imbalance(env, sds, busiest, local);
 		} else {
 
 			/*
@@ -10424,7 +10457,7 @@ static struct sched_group *find_busiest_group(struct lb_env *env)
 	 * group's child domain.
 	 */
 	if (sds.prefer_sibling && local->group_type == group_has_spare &&
-	    busiest->sum_nr_running > local->sum_nr_running + 1)
+	    sibling_imbalance(env, &sds, busiest, local) > 1)
 		goto force_balance;
 
 	if (busiest->group_type != group_overloaded) {
Signature · 1 line
2.32.0
Reviewed-by on v3 3/6
#41 lore raw

Re: [Patch v3 3/6] sched/fair: Implement prefer sibling imbalance calculation between asymmetric groups

Quoted 11 lines · Tim Chen

From: Tim C Chen <[email protected]>

In the current prefer sibling load balancing code, there is an implicit assumption that the busiest sched group and local sched group are equivalent, hence the tasks to be moved is simply the difference in number of tasks between the two groups (i.e. imbalance) divided by two.

However, we may have different number of cores between the cluster groups, say when we take CPU offline or we have hybrid groups. In that case, we should balance between the two groups such that #tasks/#cores ratio is the same between the same between both groups. Hence the imbalance

nit: type here. the same between is repeated.

Quoted 15 lines

computed will need to reflect this.

Adjust the sibling imbalance computation to take into account of the above considerations.

---
 kernel/sched/fair.c | 41 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 37 insertions(+), 4 deletions(-)
kernel/sched/fair.c +0 -0
index f636d6c09dc6..f491b94908bf 100644

kernel/sched/fair.c static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats *sgs, · line 9381

Quoted 10 lines
kernel/sched/fair.c +7 -0
@@ -9372,6 +9372,41 @@ static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats *sgs,
 	return false;
 }
 
+static inline long sibling_imbalance(struct lb_env *env,
+				    struct sd_lb_stats *sds,
+				    struct sg_lb_stats *busiest,
+				    struct sg_lb_stats *local)
+{
+	int ncores_busiest, ncores_local;
+	long imbalance;

can imbalance be unsigned int or unsigned long? as sum_nr_running is unsigned int.

Quoted 10 lines
kernel/sched/fair.c +10 -0
+
+	if (env->idle == CPU_NOT_IDLE || !busiest->sum_nr_running)
+		return 0;
+
+	ncores_busiest = sds->busiest->cores;
+	ncores_local = sds->local->cores;
+
+	if (ncores_busiest == ncores_local) {
+		imbalance = busiest->sum_nr_running;
+		lsub_positive(&imbalance, local->sum_nr_running);

kernel/sched/fair.c static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats *sgs, · line 9401

Quoted 10 lines
kernel/sched/fair.c +10 -0
+		return imbalance;
+	}
+
+	/* Balance such that nr_running/ncores ratio are same on both groups */
+	imbalance = ncores_local * busiest->sum_nr_running;
+	lsub_positive(&imbalance, ncores_busiest * local->sum_nr_running);
+	/* Normalize imbalance and do rounding on normalization */
+	imbalance = 2 * imbalance + ncores_local + ncores_busiest;
+	imbalance /= ncores_local + ncores_busiest;
+

Could this work for case where number of CPU/cores would differ between two sched groups in a sched domain? Such as problem pointed by tobias on S390. It would be nice if this patch can work for that case as well. Ran numbers for a few cases. It looks to work.
https://lore.kernel.org/lkml/[email protected]/T/#rb0a7dcd28532cafc24101e1d0aed79e6342e3901

kernel/sched/fair.c static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats *sgs, · line 9406

Quoted 5 lines
kernel/sched/fair.c +5 -0
+	/* Take advantage of resource in an empty sched group */
+	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	    busiest->sum_nr_running > 1)
+		imbalance = 2;
+

I don't see how this case would be true. When there are unequal number of cores and local->sum_nr_ruuning is 0, and busiest->sum_nr_running is atleast 2, imbalance will be atleast 1.

Quoted 31 lines
kernel/sched/fair.c +5 -4
+	return imbalance;
+}
+
 static inline bool
 sched_reduced_capacity(struct rq *rq, struct sched_domain *sd)
 {
@@ -10230,14 +10265,12 @@ static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *s
 		}
 
 		if (busiest->group_weight == 1 || sds->prefer_sibling) {
-			unsigned int nr_diff = busiest->sum_nr_running;
 			/*
 			 * When prefer sibling, evenly spread running tasks on
 			 * groups.
 			 */
 			env->migration_type = migrate_task;
-			lsub_positive(&nr_diff, local->sum_nr_running);
-			env->imbalance = nr_diff;
+			env->imbalance = sibling_imbalance(env, sds, busiest, local);
 		} else {
 
 			/*
@@ -10424,7 +10457,7 @@ static struct sched_group *find_busiest_group(struct lb_env *env)
 	 * group's child domain.
 	 */
 	if (sds.prefer_sibling && local->group_type == group_has_spare &&
-	    busiest->sum_nr_running > local->sum_nr_running + 1)
+	    sibling_imbalance(env, &sds, busiest, local) > 1)
 		goto force_balance;
 
 	if (busiest->group_type != group_overloaded) {
#42 lore raw

Re: [Patch v3 3/6] sched/fair: Implement prefer sibling imbalance calculation between asymmetric groups

Quoted 64 lines · Shrikanth Hegde
Quoted 11 lines · Tim Chen

From: Tim C Chen <[email protected]>

In the current prefer sibling load balancing code, there is an implicit assumption that the busiest sched group and local sched group are equivalent, hence the tasks to be moved is simply the difference in number of tasks between the two groups (i.e. imbalance) divided by two.

However, we may have different number of cores between the cluster groups, say when we take CPU offline or we have hybrid groups. In that case, we should balance between the two groups such that #tasks/#cores ratio is the same between the same between both groups. Hence the imbalance

nit: type here. the same between is repeated.

Quoted 16 lines

computed will need to reflect this.

Adjust the sibling imbalance computation to take into account of the above considerations.

---
 kernel/sched/fair.c | 41 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 37 insertions(+), 4 deletions(-)
kernel/sched/fair.c +0 -0
index f636d6c09dc6..f491b94908bf 100644
@@ -9372,6 +9372,41 @@ static inline bool smt_balance(struct lb_env 
 *env, struct sg_lb_stats *sgs,

kernel/sched/fair.c static inline bool smt_balance(struct lb_env · line 9382

Quoted 10 lines
kernel/sched/fair.c +7 -0
 	return false;
 }
 
+static inline long sibling_imbalance(struct lb_env *env,
+				    struct sd_lb_stats *sds,
+				    struct sg_lb_stats *busiest,
+				    struct sg_lb_stats *local)
+{
+	int ncores_busiest, ncores_local;
+	long imbalance;

can imbalance be unsigned int or unsigned long? as sum_nr_running is unsigned int.

Quoted 19 lines
kernel/sched/fair.c +18 -0
+
+	if (env->idle == CPU_NOT_IDLE || !busiest->sum_nr_running)
+		return 0;
+
+	ncores_busiest = sds->busiest->cores;
+	ncores_local = sds->local->cores;
+
+	if (ncores_busiest == ncores_local) {
+		imbalance = busiest->sum_nr_running;
+		lsub_positive(&imbalance, local->sum_nr_running);
+		return imbalance;
+	}
+
+	/* Balance such that nr_running/ncores ratio are same on both groups 
 */
+	imbalance = ncores_local * busiest->sum_nr_running;
+	lsub_positive(&imbalance, ncores_busiest * local->sum_nr_running);
+	/* Normalize imbalance and do rounding on normalization */
+	imbalance = 2 * imbalance + ncores_local + ncores_busiest;
Quoted 9 lines

kernel/sched/fair.c static inline bool smt_balance(struct lb_env · line 9403

Quoted 2 lines
kernel/sched/fair.c +2 -0
+	imbalance /= ncores_local + ncores_busiest;
+

Could this work for case where number of CPU/cores would differ between two sched groups in a sched domain? Such as problem pointed by tobias on S390. It would be nice if this patch can work for that case as well. Ran numbers for a few cases. It looks to work.
https://lore.kernel.org/lkml/[email protected]/T/#rb0a7dcd28532cafc24101e1d0aed79e6342e3901

Just stumbled upon this patch series as well. In this version it looks similar to the prototypes I played around with, but more complete. So I'm happy that my understanding of the load balancer was kinda correct :)

From a functional perspective this appears to address the issues we saw on s390.

Quoted 48 lines

kernel/sched/fair.c static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats *sgs, · line 9406

Quoted 7 lines
kernel/sched/fair.c +5 -0
+	/* Take advantage of resource in an empty sched group */
+	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	    busiest->sum_nr_running > 1)
+		imbalance = 2;
+

I don't see how this case would be true. When there are unequal number of cores and local->sum_nr_ruuning
is 0, and busiest->sum_nr_running is atleast 2, imbalance will be atleast 1.

Quoted 33 lines
kernel/sched/fair.c +3 -0
+	return imbalance;
+}
+
 static inline bool

sched_reduced_capacity(struct rq *rq, struct sched_domain *sd) {

kernel/sched/fair.c +0 -0
@@ -10230,14 +10265,12 @@ static inline void 

calculate_imbalance(struct lb_env *env, struct sd_lb_stats *s

kernel/sched/fair.c +2 -4
 		}
 
 		if (busiest->group_weight == 1 || sds->prefer_sibling) {
-			unsigned int nr_diff = busiest->sum_nr_running;
 			/*
 			 * When prefer sibling, evenly spread running tasks on
 			 * groups.
 			 */
 			env->migration_type = migrate_task;
-			lsub_positive(&nr_diff, local->sum_nr_running);
-			env->imbalance = nr_diff;
+			env->imbalance = sibling_imbalance(env, sds, busiest, local);
 		} else {
 
 			/*
@@ -10424,7 +10457,7 @@ static struct sched_group 
 *find_busiest_group(struct lb_env *env)
 	 * group's child domain.
 	 */
 	if (sds.prefer_sibling && local->group_type == group_has_spare &&
-	    busiest->sum_nr_running > local->sum_nr_running + 1)
+	    sibling_imbalance(env, &sds, busiest, local) > 1)
 		goto force_balance;
 

if (busiest->group_type != group_overloaded) {

author
#43 lore raw

Re: [Patch v3 3/6] sched/fair: Implement prefer sibling imbalance calculation between asymmetric groups

Quoted 11 lines · Tobias Huschle
Quoted 9 lines

Could this work for case where number of CPU/cores would differ between two sched groups in a sched domain? Such as problem pointed by tobias on S390. It would be nice if this patch can work for that case
as well. Ran numbers for a few cases. It looks to work.
https://lore.kernel.org/lkml/[email protected]/T/#rb0a7dcd28532cafc24101e1d0aed79e6342e3901

Quoted 7 lines

Just stumbled upon this patch series as well. In this version it looks similar to the prototypes I played around with, but more complete. So I'm happy that my understanding of the load balancer was kinda correct :)

From a functional perspective this appears to address the issues we saw
on s390.

Glad that this patch addresses this common issue across architectures. I did aim to address the asymmetric groups balancing in general. Peter pointed out this problem that's inherent when he reviewed the first version of my patchset.

Tim

Quoted 50 lines
Quoted 50 lines

kernel/sched/fair.c static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats *sgs, · line 9406

Quoted 8 lines
kernel/sched/fair.c +5 -0
+	/* Take advantage of resource in an empty sched group */
+	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	    busiest->sum_nr_running > 1)
+		imbalance = 2;
+

I don't see how this case would be true. When there are unequal number of cores and local->sum_nr_ruuning
is 0, and busiest->sum_nr_running is atleast 2, imbalance will be atleast 1.

Quoted 33 lines
kernel/sched/fair.c +3 -0
+	return imbalance;
+}
+
 static inline bool
 sched_reduced_capacity(struct rq *rq, struct sched_domain *sd)
 {
@@ -10230,14 +10265,12 @@ static inline void 

calculate_imbalance(struct lb_env *env, struct sd_lb_stats *s

kernel/sched/fair.c +2 -4
 		}
 
 		if (busiest->group_weight == 1 || sds->prefer_sibling) {
-			unsigned int nr_diff = busiest->sum_nr_running;
 			/*
 			 * When prefer sibling, evenly spread running tasks on
 			 * groups.
 			 */
 			env->migration_type = migrate_task;
-			lsub_positive(&nr_diff, local->sum_nr_running);
-			env->imbalance = nr_diff;
+			env->imbalance = sibling_imbalance(env, sds, busiest, local);
 		} else {
 
 			/*
@@ -10424,7 +10457,7 @@ static struct sched_group 
 *find_busiest_group(struct lb_env *env)
 	 * group's child domain.
 	 */
 	if (sds.prefer_sibling && local->group_type == group_has_spare &&
-	    busiest->sum_nr_running > local->sum_nr_running + 1)
+	    sibling_imbalance(env, &sds, busiest, local) > 1)
 		goto force_balance;
 

if (busiest->group_type != group_overloaded) {

author
#44 lore raw

Re: [Patch v3 3/6] sched/fair: Implement prefer sibling imbalance calculation between asymmetric groups

In reply to Shrikanth Hegde
Quoted 9 lines · Shrikanth Hegde
Quoted 7 lines · Tim Chen

From: Tim C Chen <[email protected]>

In the current prefer sibling load balancing code, there is an implicit assumption that the busiest sched group and local sched group are equivalent, hence the tasks to be moved is simply the difference in number of tasks between the two groups (i.e. imbalance) divided by two.

Quoted 6 lines
Quoted 4 lines

However, we may have different number of cores between the cluster groups, say when we take CPU offline or we have hybrid groups. In that case, we should balance between the two groups such that #tasks/#cores ratio is the same between the same between both groups. Hence the imbalance

nit: type here. the same between is repeated.

Thanks for catching.

Quoted 19 lines
Quoted 19 lines

computed will need to reflect this.

Adjust the sibling imbalance computation to take into account of the above considerations.

---
 kernel/sched/fair.c | 41 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 37 insertions(+), 4 deletions(-)
kernel/sched/fair.c +0 -0
index f636d6c09dc6..f491b94908bf 100644
@@ -9372,6 +9372,41 @@ static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats *sgs,
 	return false;
 }
 
Quoted 9 lines

kernel/sched/fair.c static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats *sgs, · line 9381

Quoted 7 lines
kernel/sched/fair.c +7 -0
+static inline long sibling_imbalance(struct lb_env *env,
+				    struct sd_lb_stats *sds,
+				    struct sg_lb_stats *busiest,
+				    struct sg_lb_stats *local)
+{
+	int ncores_busiest, ncores_local;
+	long imbalance;

can imbalance be unsigned int or unsigned long? as sum_nr_running is unsigned int.

It could be made unsigned long.

Quoted 16 lines
Quoted 16 lines
kernel/sched/fair.c +15 -0
+
+	if (env->idle == CPU_NOT_IDLE || !busiest->sum_nr_running)
+		return 0;
+
+	ncores_busiest = sds->busiest->cores;
+	ncores_local = sds->local->cores;
+
+	if (ncores_busiest == ncores_local) {
+		imbalance = busiest->sum_nr_running;
+		lsub_positive(&imbalance, local->sum_nr_running);
+		return imbalance;
+	}
+
+	/* Balance such that nr_running/ncores ratio are same on both groups */
+	imbalance = ncores_local * busiest->sum_nr_running;
Quoted 9 lines

kernel/sched/fair.c static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats *sgs, · line 9401

Quoted 5 lines
kernel/sched/fair.c +5 -0
+	lsub_positive(&imbalance, ncores_busiest * local->sum_nr_running);
+	/* Normalize imbalance and do rounding on normalization */
+	imbalance = 2 * imbalance + ncores_local + ncores_busiest;
+	imbalance /= ncores_local + ncores_busiest;
+

Could this work for case where number of CPU/cores would differ between two sched groups in a sched domain?

Yes. That's the problem I was targeting.

Quoted 17 lines

Such as problem pointed
by tobias on S390. It would be nice if this patch can work for that case as well. Ran numbers for a few cases. It looks to work.
https://lore.kernel.org/lkml/[email protected]/T/#rb0a7dcd28532cafc24101e1d0aed79e6342e3901

kernel/sched/fair.c static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats *sgs, · line 9406

Quoted 5 lines
kernel/sched/fair.c +5 -0
+	/* Take advantage of resource in an empty sched group */
+	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	    busiest->sum_nr_running > 1)
+		imbalance = 2;
+

I don't see how this case would be true. When there are unequal number of cores and local->sum_nr_ruuning is 0, and busiest->sum_nr_running is atleast 2, imbalance will be atleast 1.

Quoted 1 line

Thanks for the review.

Quoted 33 lines
Quoted 32 lines
kernel/sched/fair.c +5 -4
+	return imbalance;
+}
+
 static inline bool
 sched_reduced_capacity(struct rq *rq, struct sched_domain *sd)
 {
@@ -10230,14 +10265,12 @@ static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *s
 		}
 
 		if (busiest->group_weight == 1 || sds->prefer_sibling) {
-			unsigned int nr_diff = busiest->sum_nr_running;
 			/*
 			 * When prefer sibling, evenly spread running tasks on
 			 * groups.
 			 */
 			env->migration_type = migrate_task;
-			lsub_positive(&nr_diff, local->sum_nr_running);
-			env->imbalance = nr_diff;
+			env->imbalance = sibling_imbalance(env, sds, busiest, local);
 		} else {
 
 			/*
@@ -10424,7 +10457,7 @@ static struct sched_group *find_busiest_group(struct lb_env *env)
 	 * group's child domain.
 	 */
 	if (sds.prefer_sibling && local->group_type == group_has_spare &&
-	    busiest->sum_nr_running > local->sum_nr_running + 1)
+	    sibling_imbalance(env, &sds, busiest, local) > 1)
 		goto force_balance;
 
 	if (busiest->group_type != group_overloaded) {
author
#45 lore raw

Re: [Patch v3 3/6] sched/fair: Implement prefer sibling imbalance calculation between asymmetric groups

Quoted 11 lines · Tim Chen
Quoted 10 lines

kernel/sched/fair.c static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats *sgs, · line 9381

Quoted 8 lines
kernel/sched/fair.c +7 -0
+static inline long sibling_imbalance(struct lb_env *env,
+				    struct sd_lb_stats *sds,
+				    struct sg_lb_stats *busiest,
+				    struct sg_lb_stats *local)
+{
+	int ncores_busiest, ncores_local;
+	long imbalance;

can imbalance be unsigned int or unsigned long? as sum_nr_running is unsigned int.

Quoted 3 lines

It could be made unsigned long.

Though in theory the imbalance can be both positive or negative. We are considering only positive imbalance here as we only pull task to local group and do not push task from local group.

Tim

author
#46 lore raw

Re: [Patch v3 3/6] sched/fair: Implement prefer sibling imbalance calculation between asymmetric groups

In reply to Shrikanth Hegde
Quoted 9 lines · Shrikanth Hegde

Review

Quoted 6 lines
(quoted diff) +5 -0
+	/* Take advantage of resource in an empty sched group */
+	if (imbalance == 0 && local->sum_nr_running == 0 &&
+	    busiest->sum_nr_running > 1)
+		imbalance = 2;
+

I don't see how this case would be true. When there are unequal number of cores and local->sum_nr_ruuning is 0, and busiest->sum_nr_running is atleast 2, imbalance will be atleast 1.

I think you are correct. With at least 2 task in the busiest group, imbalance will be at least 1. This is the effect of doing rounding when adding the (ncores_local + ncores_busy) rounding factor.

Returning an imbalance value of 1 will not be correct as we will be dividing imbalance by 2 and we will still not move task to the empty group as intended.

So this code should be updated as below:

kernel/sched/fair.c +1 -1
index 3fc8d3a3bd22..16bf75e6a775 100644
@@ -9400,7 +9400,7 @@ static inline long sibling_imbalance(struct lb_env *env,
        imbalance /= ncores_local + ncores_busiest;
 
        /* Take advantage of resource in an empty sched group */
-       if (imbalance == 0 && local->sum_nr_running == 0 &&
+       if (imbalance <= 1 && local->sum_nr_running == 0 &&
            busiest->sum_nr_running > 1)
                imbalance = 2;
 

Tim

author
#47 lore raw

PATCH v3 4/6 sched/fair: Consider the idle state of the whole core for load balance

In reply to Tim Chen

From: Ricardo Neri <[email protected]>

should_we_balance() traverses the group_balance_mask (AND'ed with lb_env::
cpus) starting from lower numbered CPUs looking for the first idle CPU.

In hybrid x86 systems, the siblings of SMT cores get CPU numbers, before non-SMT cores:

	[0, 1] [2, 3] [4, 5] 6 7 8 9
         b  i   b  i   b  i  b i i i

In the figure above, CPUs in brackets are siblings of an SMT core. The rest are non-SMT cores. 'b' indicates a busy CPU, 'i' indicates an idle CPU.

We should let a CPU on a fully idle core get the first chance to idle load balance as it has more CPU capacity than a CPU on an idle SMT CPU with busy sibling. So for the figure above, if we are running should_we_balance() to CPU 1, we should return false to let CPU 7 on idle core to have a chance first to idle load balance.

A partially busy (i.e., of type group_has_spare) local group with SMT cores will often have only one SMT sibling busy. If the destination CPU is a non-SMT core, partially busy, lower-numbered, SMT cores should not be considered when finding the first idle CPU.

However, in should_we_balance(), when we encounter idle SMT first in partially busy core, we prematurely break the search for the first idle CPU.

Higher-numbered, non-SMT cores is not given the chance to have idle balance done on their behalf. Those CPUs will only be considered for idle balancing by chance via CPU_NEWLY_IDLE.

Instead, consider the idle state of the whole SMT core.

---
 kernel/sched/fair.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
kernel/sched/fair.c +15 -1
index f491b94908bf..294a662c9410 100644
@@ -10729,7 +10729,7 @@ static int active_load_balance_cpu_stop(void *data);
 static int should_we_balance(struct lb_env *env)
 {
 	struct sched_group *sg = env->sd->groups;
-	int cpu;
+	int cpu, idle_smt = -1;
 
 	/*
 	 * Ensure the balancing environment is consistent; can happen
@@ -10756,10 +10756,24 @@ static int should_we_balance(struct lb_env *env)
 		if (!idle_cpu(cpu))
 			continue;
 
+		/*
+		 * Don't balance to idle SMT in busy core right away when
+		 * balancing cores, but remember the first idle SMT CPU for
+		 * later consideration.  Find CPU on an idle core first.
+		 */
+		if (!(env->sd->flags & SD_SHARE_CPUCAPACITY) && !is_core_idle(cpu)) {
+			if (idle_smt == -1)
+				idle_smt = cpu;
+			continue;
+		}
+
 		/* Are we the first idle CPU? */
 		return cpu == env->dst_cpu;
 	}
 
+	if (idle_smt == env->dst_cpu)
+		return true;
+
 	/* Are we the first CPU of this group ? */
 	return group_balance_cpu(sg) == env->dst_cpu;
 }
Signature · 1 line
2.32.0
Tested-by on v3 4/6
#48 lore raw

Re: [Patch v3 4/6] sched/fair: Consider the idle state of the whole core for load balance

Quoted 66 lines · Tim Chen

From: Ricardo Neri <[email protected]>

should_we_balance() traverses the group_balance_mask (AND'ed with lb_env::
cpus) starting from lower numbered CPUs looking for the first idle CPU.

In hybrid x86 systems, the siblings of SMT cores get CPU numbers, before non-SMT cores:

	[0, 1] [2, 3] [4, 5] 6 7 8 9
         b  i   b  i   b  i  b i i i

In the figure above, CPUs in brackets are siblings of an SMT core. The rest are non-SMT cores. 'b' indicates a busy CPU, 'i' indicates an idle CPU.

We should let a CPU on a fully idle core get the first chance to idle load balance as it has more CPU capacity than a CPU on an idle SMT CPU with busy sibling. So for the figure above, if we are running should_we_balance() to CPU 1, we should return false to let CPU 7 on idle core to have a chance first to idle load balance.

A partially busy (i.e., of type group_has_spare) local group with SMT cores will often have only one SMT sibling busy. If the destination CPU is a non-SMT core, partially busy, lower-numbered, SMT cores should not be considered when finding the first idle CPU.

However, in should_we_balance(), when we encounter idle SMT first in partially busy core, we prematurely break the search for the first idle CPU.

Higher-numbered, non-SMT cores is not given the chance to have idle balance done on their behalf. Those CPUs will only be considered for idle balancing by chance via CPU_NEWLY_IDLE.

Instead, consider the idle state of the whole SMT core.

---
 kernel/sched/fair.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
kernel/sched/fair.c +8 -1
index f491b94908bf..294a662c9410 100644
@@ -10729,7 +10729,7 @@ static int active_load_balance_cpu_stop(void *data);
 static int should_we_balance(struct lb_env *env)
 {
 	struct sched_group *sg = env->sd->groups;
-	int cpu;
+	int cpu, idle_smt = -1;
 
 	/*
 	 * Ensure the balancing environment is consistent; can happen
@@ -10756,10 +10756,24 @@ static int should_we_balance(struct lb_env *env)
 		if (!idle_cpu(cpu))
 			continue;
 
+		/*
+		 * Don't balance to idle SMT in busy core right away when
+		 * balancing cores, but remember the first idle SMT CPU for
+		 * later consideration.  Find CPU on an idle core first.
+		 */
+		if (!(env->sd->flags & SD_SHARE_CPUCAPACITY) && !is_core_idle(cpu)) {
+			if (idle_smt == -1)

kernel/sched/fair.c static int should_we_balance(struct lb_env *env) · line 10775

Quoted 10 lines
kernel/sched/fair.c +6 -0
+				idle_smt = cpu;
+			continue;
+		}
+
 		/* Are we the first idle CPU? */
 		return cpu == env->dst_cpu;
 	}
 
+	if (idle_smt == env->dst_cpu)
+		return true;

This is nice. It helps in reducing the migrations and improve the performance of CPU oriented benchmarks slightly. This could be due to less migrations.

Tested a bit on power10 with SMT=4. Offlined a CPU to make a few cores SMT=1. There is no regression observed. Slight improvement in throughput oriented workload such as stress-ng. migrations are reduced by quite a bit, likely due to patch. I have attached the test results there. [4/6] sched/fair: Consider the idle state of the whole core for load balance

Test results:

# lscpu
Architecture:            ppc64le
  Byte Order:            Little Endian
CPU(s):                  96
  On-line CPU(s) list:   0-17,24,25,32-49,56-89
  Off-line CPU(s) list:  18-23,26-31,50-55,90-95
Model name:              POWER10 (architected), altivec supported

--------------------------------------------------------------------------------------------------
baseline:

Performance counter stats for 'stress-ng --cpu=72 -l 50 --cpu-ops=100000 --cpu-load-slice=1' (5 runs):

        260,813.13 msec task-clock                #   33.390 CPUs utilized            ( +-  0.10% )
            42,535      context-switches          #  163.543 /sec                     ( +-  0.13% )
             9,060      cpu-migrations            #   34.835 /sec                     ( +-  1.07% )
            12,947      page-faults               #   49.780 /sec                     ( +-  1.76% )
   948,061,954,432      cycles                    #    3.645 GHz                      ( +-  0.09% )
   926,045,701,578      instructions              #    0.98  insn per cycle           ( +-  0.00% )
   146,418,075,496      branches                  #  562.964 M/sec                    ( +-  0.00% )
     1,197,661,965      branch-misses             #    0.82% of all branches          ( +-  0.17% )
            7.8111 +- 0.0162 seconds time elapsed  ( +-  0.21% )

Performance counter stats for 'stress-ng --cpu=60 -l 50 --cpu-ops=100000 --cpu-load-slice=1' (5 runs):

        253,351.70 msec task-clock                #   28.207 CPUs utilized            ( +-  0.21% )
            41,046      context-switches          #  162.828 /sec                     ( +-  0.16% )
             6,674      cpu-migrations            #   26.475 /sec                     ( +-  3.42% )
            10,879      page-faults               #   43.157 /sec                     ( +-  1.68% )
   931,014,218,983      cycles                    #    3.693 GHz                      ( +-  0.22% )
   919,717,564,454      instructions              #    0.99  insn per cycle           ( +-  0.00% )
   145,480,596,331      branches                  #  577.116 M/sec                    ( +-  0.00% )
     1,175,362,979      branch-misses             #    0.81% of all branches          ( +-  0.12% )
            8.9818 +- 0.0288 seconds time elapsed  ( +-  0.32% )

---------------------------------------------------------------------------------------------------
with patch:

Performance counter stats for 'stress-ng --cpu=72 -l 50 --cpu-ops=100000 --cpu-load-slice=1' (5 runs):

        254,652.01 msec task-clock                #   33.449 CPUs utilized            ( +-  0.11% )
            40,970      context-switches          #  160.974 /sec                     ( +-  0.10% )
             5,397      cpu-migrations            #   21.205 /sec                     ( +-  2.01% )
            11,705      page-faults               #   45.990 /sec                     ( +-  1.21% )
   911,115,537,080      cycles                    #    3.580 GHz                      ( +-  0.11% )
   925,635,958,489      instructions              #    1.02  insn per cycle           ( +-  0.00% )
   146,450,995,164      branches                  #  575.416 M/sec                    ( +-  0.00% )
     1,188,906,011      branch-misses             #    0.81% of all branches          ( +-  0.28% )
            7.6132 +- 0.0381 seconds time elapsed  ( +-  0.50% )

Performance counter stats for 'stress-ng --cpu=60 -l 50 --cpu-ops=100000 --cpu-load-slice=1' (5 runs):

        236,962.38 msec task-clock                #   27.948 CPUs utilized            ( +-  0.05% )
            40,030      context-switches          #  168.869 /sec                     ( +-  0.04% )
             3,156      cpu-migrations            #   13.314 /sec                     ( +-  1.37% )
             9,448      page-faults               #   39.857 /sec                     ( +-  1.72% )
   856,444,937,794      cycles                    #    3.613 GHz                      ( +-  0.06% )
   919,459,795,805      instructions              #    1.07  insn per cycle           ( +-  0.00% )
   145,654,799,033      branches                  #  614.452 M/sec                    ( +-  0.00% )
     1,177,464,719      branch-misses             #    0.81% of all branches          ( +-  0.23% )
            8.4788 +- 0.0323 seconds time elapsed  ( +-  0.38% )

--------------------------------------------------------------------------------------------------------

Tried on a symmetric system with all cores having SMT=4 as well. There was reduction in migrations here as well. Didnt observe any major regressions when microbenchmarks run alone. Such as hackbench, stress-ng.

So. Here is tested-by.

kernel/sched/fair.c static int should_we_balance(struct lb_env *env) · line 10779

Quoted 4 lines
kernel/sched/fair.c +1 -0
+
 	/* Are we the first CPU of this group ? */
 	return group_balance_cpu(sg) == env->dst_cpu;
 }

One doubt though, Here a fully idle core would be chosen instead of first idle cpu in the group (if there is one). Since coming out of idle of SMT is faster compared to a fully idle core, would latency increase? Or that concerns mainly wakeup path?

author
#49 lore raw

Re: [Patch v3 4/6] sched/fair: Consider the idle state of the whole core for load balance

Quoted 8 lines · Shrikanth Hegde

Tried on a symmetric system with all cores having SMT=4 as well. There was reduction in migrations here as well. Didnt observe any major regressions when microbenchmarks run alone. Such as hackbench, stress-ng.

So. Here is tested-by.

Thanks for testing.

Quoted 10 lines

kernel/sched/fair.c static int should_we_balance(struct lb_env *env) · line 10779

Quoted 6 lines
kernel/sched/fair.c +1 -0
+
 	/* Are we the first CPU of this group ? */
 	return group_balance_cpu(sg) == env->dst_cpu;
 }

One doubt though, Here a fully idle core would be chosen instead of first idle cpu in the group (if there is one). Since coming out of idle of SMT is faster compared to a fully idle core, would latency increase? Or that concerns mainly wakeup path?

Yeah, I think that concern is for the wakeup path and not for the balance path.

Tim

author
#50 lore raw

PATCH v3 5/6 sched/x86: Add cluster topology to hybrid CPU

In reply to Tim Chen

From: Tim C Chen <[email protected]>

Cluster topology was not enabled on hybrid x86 CPU as load balance was not properly working for cluster domain. That has been fixed and cluster domain can be enabled for hybrid CPU.

---
 arch/x86/kernel/smpboot.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kernel/smpboot.c +3 -0
index cea297d97034..2489d767c398 100644
@@ -575,6 +575,9 @@ static struct sched_domain_topology_level x86_hybrid_topology[] = {
 #ifdef CONFIG_SCHED_SMT
 	{ cpu_smt_mask, x86_smt_flags, SD_INIT_NAME(SMT) },
 #endif
+#ifdef CONFIG_SCHED_CLUSTER
+	{ cpu_clustergroup_mask, x86_cluster_flags, SD_INIT_NAME(CLS) },
+#endif
 #ifdef CONFIG_SCHED_MC
 	{ cpu_coregroup_mask, x86_core_flags, SD_INIT_NAME(MC) },
 #endif
Signature · 1 line
2.32.0
maintainer
#51 lore raw

Re: [Patch v3 5/6] sched/x86: Add cluster topology to hybrid CPU

Quoted 8 lines · Tim Chen

From: Tim C Chen <[email protected]>

Cluster topology was not enabled on hybrid x86 CPU as load balance was not properly working for cluster domain. That has been fixed and cluster domain can be enabled for hybrid CPU.

Yeah, you didn't actually try appling this to something recent did ya :-)

You missed 8f2d6c41e5a6 ("x86/sched: Rewrite topology setup").

I'll replace this patch with the below.

---
 arch/x86/kernel/smpboot.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
arch/x86/kernel/smpboot.c +3 -8
index ed2d51960a7d..3b751d79cdfb 100644
@@ -632,14 +632,9 @@ static void __init build_sched_topology(void)
 	};
 #endif
 #ifdef CONFIG_SCHED_CLUSTER
-	/*
-	 * For now, skip the cluster domain on Hybrid.
-	 */
-	if (!cpu_feature_enabled(X86_FEATURE_HYBRID_CPU)) {
-		x86_topology[i++] = (struct sched_domain_topology_level){
-			cpu_clustergroup_mask, x86_cluster_flags, SD_INIT_NAME(CLS)
-		};
-	}
+	x86_topology[i++] = (struct sched_domain_topology_level){
+		cpu_clustergroup_mask, x86_cluster_flags, SD_INIT_NAME(CLS)
+	};
 #endif
 #ifdef CONFIG_SCHED_MC
 	x86_topology[i++] = (struct sched_domain_topology_level){
author
#52 lore raw

Re: [Patch v3 5/6] sched/x86: Add cluster topology to hybrid CPU

Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Tim Chen

From: Tim C Chen <[email protected]>

Cluster topology was not enabled on hybrid x86 CPU as load balance was not properly working for cluster domain. That has been fixed and cluster domain can be enabled for hybrid CPU.

Quoted 6 lines

Yeah, you didn't actually try appling this to something recent did ya :-)

You missed 8f2d6c41e5a6 ("x86/sched: Rewrite topology setup").

I'll replace this patch with the below.

Thanks for catching it.

Tim

Quoted 27 lines
---
 arch/x86/kernel/smpboot.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
arch/x86/kernel/smpboot.c +3 -8
index ed2d51960a7d..3b751d79cdfb 100644
@@ -632,14 +632,9 @@ static void __init build_sched_topology(void)
 	};
 #endif
 #ifdef CONFIG_SCHED_CLUSTER
-	/*
-	 * For now, skip the cluster domain on Hybrid.
-	 */
-	if (!cpu_feature_enabled(X86_FEATURE_HYBRID_CPU)) {
-		x86_topology[i++] = (struct sched_domain_topology_level){
-			cpu_clustergroup_mask, x86_cluster_flags, SD_INIT_NAME(CLS)
-		};
-	}
+	x86_topology[i++] = (struct sched_domain_topology_level){
+		cpu_clustergroup_mask, x86_cluster_flags, SD_INIT_NAME(CLS)
+	};
 #endif
 #ifdef CONFIG_SCHED_MC
 	x86_topology[i++] = (struct sched_domain_topology_level){
author
#53 lore raw

PATCH v3 6/6 sched/debug: Dump domains' sched group flags

In reply to Tim Chen

From: "Peter Zijlstra (Intel)" <[email protected]>

There have been a case where the SD_SHARE_CPUCAPACITY sched group flag in a parent domain were not set and propagated properly when a degenerate domain is removed.

Add dump of domain sched group flags of a CPU to make debug easier in the future.

Usage:
cat /debug/sched/domains/cpu0/domain1/groups_flags to dump cpu0 domain1's sched group flags.

---
 kernel/sched/debug.c | 1 +
 1 file changed, 1 insertion(+)
kernel/sched/debug.c +1 -0
index 1637b65ba07a..55b50f940feb 100644
@@ -389,6 +389,7 @@ static void register_sd(struct sched_domain *sd, struct dentry *parent)
 #undef SDM
 
 	debugfs_create_file("flags", 0444, parent, &sd->flags, &sd_flags_fops);
+	debugfs_create_file("groups_flags", 0444, parent, &sd->groups->flags, &sd_flags_fops);
 }
 
 void update_sched_domain_debugfs(void)
Signature · 1 line
2.32.0
reviewer Reviewed-by on v3 6/6
#54 lore raw

Re: [Patch v3 6/6] sched/debug: Dump domains' sched group flags

Quoted 6 lines · Tim Chen

From: "Peter Zijlstra (Intel)" <[email protected]>

There have been a case where the SD_SHARE_CPUCAPACITY sched group flag in a parent domain were not set and propagated properly when a degenerate domain is removed.

Quoted 8 lines

Add dump of domain sched group flags of a CPU to make debug easier in the future.

Usage:
cat /debug/sched/domains/cpu0/domain1/groups_flags to dump cpu0 domain1's sched group flags.