Skip to contentSkip to conversation

sched/topology: optimize topology_span_sane()

PATCH Superseded Yury Norov 3 participants 8 messages Aug 2, 2024 – Aug 7, 2024 Scheduler

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yury Norov

author
#0 lore raw

cover sched/topology: optimize topology_span_sane()

Pre-compute pre-compute topology_span_sane() loop params and optimize the functtion to avoid calling cpumask_equal() when masks are the same.

This series follows up comments from here:

https://lore.kernel.org/lkml/ZqqV5OxZPHUgjhag@LeoBras/T/#md6b2b6bdd09e63740bbf010530211842a79b5f57

Yury Norov (2):
sched/topology: pre-compute topology_span_sane() loop params sched/topology: optimize topology_span_sane()

 kernel/sched/topology.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/2 sched/topology: pre-compute topology_span_sane() loop params

tl->mask(cpu) is used unchanged in the loop, and tl->mask(i) in worst case may be calculated twice as parameters for cpumask_equal() and cpumask_intersects(). So, precalculate both.

---
 kernel/sched/topology.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/sched/topology.c +4 -2
index 76504b776d03..754ad5fa3c99 100644
@@ -2353,6 +2353,7 @@ static struct sched_domain *build_sched_domain(struct sched_domain_topology_leve
 static bool topology_span_sane(struct sched_domain_topology_level *tl,
 			      const struct cpumask *cpu_map, int cpu)
 {
+	const struct cpumask *mi, *mc = tl->mask(cpu);
 	int i = cpu + 1;
 
 	/* NUMA levels are allowed to overlap */
@@ -2366,14 +2367,15 @@ static bool topology_span_sane(struct sched_domain_topology_level *tl,
 	 * breaks the linking done for an earlier span.
 	 */
 	for_each_cpu_from(i, cpu_map) {
+		mi = tl->mask(i);
+
 		/*
 		 * We should 'and' all those masks with 'cpu_map' to exactly
 		 * match the topology we're about to build, but that can only
 		 * remove CPUs, which only lessens our ability to detect
 		 * overlaps
 		 */
-		if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
-		    cpumask_intersects(tl->mask(cpu), tl->mask(i)))
+		if (!cpumask_equal(mc, mi) && cpumask_intersects(mc, mi))
 			return false;
 	}
 
Signature · 1 line
2.43.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] sched/topology: pre-compute topology_span_sane() loop params

Quoted 16 lines · Yury Norov

tl->mask(cpu) is used unchanged in the loop, and tl->mask(i) in worst case may be calculated twice as parameters for cpumask_equal() and cpumask_intersects(). So, precalculate both.

---
 kernel/sched/topology.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/sched/topology.c +0 -0
index 76504b776d03..754ad5fa3c99 100644

kernel/sched/topology.c static struct sched_domain *build_sched_domain(struct sched_domain_topology_leve · line 2356

Quoted 4 lines
kernel/sched/topology.c +1 -0
@@ -2353,6 +2353,7 @@ static struct sched_domain *build_sched_domain(struct sched_domain_topology_leve
 static bool topology_span_sane(struct sched_domain_topology_level *tl,
 			      const struct cpumask *cpu_map, int cpu)
 {
+	const struct cpumask *mi, *mc = tl->mask(cpu);

Could it avoid the calculation by putting
mc = tl->mask(cpu) after the
if (tl->flags & SDTL_OVERLAP)?

Other than that,

thanks,
Chenyu

author
#3 lore raw

PATCH 2/2 sched/topology: optimize topology_span_sane()

In reply to Yury Norov

The function may call cpumask_equal with tl->mask(cpu) == tl->mask(i), even when cpu != i. In such case, cpumask_equal() would always return true, and we can proceed to the next iteration immediately.

---
 kernel/sched/topology.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/sched/topology.c +2 -0
index 754ad5fa3c99..571759606954 100644
@@ -2368,6 +2368,8 @@ static bool topology_span_sane(struct sched_domain_topology_level *tl,
 	 */
 	for_each_cpu_from(i, cpu_map) {
 		mi = tl->mask(i);
+		if (mi == mc)
+			continue;
 
 		/*
 		 * We should 'and' all those masks with 'cpu_map' to exactly
Signature · 1 line
2.43.0
#4 lore raw
Quoted 2 lines · Yury Norov

The function may call cpumask_equal with tl->mask(cpu) == tl->mask(i), even when cpu != i.

For which architecture have you observed this? AFAIA all implementations of tl->sched_domain_mask_f are built on a per-CPU cpumask.

e.g. for the default topology, we have:

cpu_smt_mask() -> topology_sibling_cpumask()

which is implemented as:

arch/loongarch/include/asm/topology.h:35:#define topology_sibling_cpumask(cpu) (&cpu_sibling_map[cpu])
arch/mips/include/asm/topology.h:18:#define topology_sibling_cpumask(cpu) (&cpu_sibling_map[cpu])
arch/powerpc/include/asm/topology.h:139:#define topology_sibling_cpumask(cpu) (per_cpu(cpu_sibling_map, cpu))
arch/s390/include/asm/topology.h:31:#define topology_sibling_cpumask(cpu) (&cpu_topology[cpu].thread_mask)
arch/sparc/include/asm/topology_64.h:50:#define topology_sibling_cpumask(cpu) (&per_cpu(cpu_sibling_map, cpu))
arch/x86/include/asm/topology.h:186:#define topology_sibling_cpumask(cpu) (per_cpu(cpu_sibling_map, cpu))
include/linux/arch_topology.h:91:#define topology_sibling_cpumask(cpu) (&cpu_topology[cpu].thread_sibling)
include/linux/topology.h:218:#define topology_sibling_cpumask(cpu) cpumask_of(cpu)

and ditto for cpu_coregroup_mask() & cpu_cpu_mask().

author
#5 lore raw
Quoted 6 lines · Valentin Schneider
Quoted 2 lines · Yury Norov

The function may call cpumask_equal with tl->mask(cpu) == tl->mask(i), even when cpu != i.

For which architecture have you observed this? AFAIA all implementations of tl->sched_domain_mask_f are built on a per-CPU cpumask.

x86_64, qemu emulating 16 CPUs in 4 nodes, Linux 6.10, approximately defconfig.

reviewer Reviewed-by
#6 lore raw
Quoted 10 lines · Yury Norov
Quoted 6 lines · Valentin Schneider
Quoted 2 lines · Yury Norov

The function may call cpumask_equal with tl->mask(cpu) == tl->mask(i), even when cpu != i.

For which architecture have you observed this? AFAIA all implementations of tl->sched_domain_mask_f are built on a per-CPU cpumask.

x86_64, qemu emulating 16 CPUs in 4 nodes, Linux 6.10, approximately defconfig.

For the default_topology:
cpu_smt_mask() # SMT
(per_cpu(cpu_sibling_map, cpu))

cpu_clustergroup_mask() # CLS
per_cpu(cpu_l2c_shared_map, cpu);

cpu_coregroup_mask() # MC
per_cpu(cpu_llc_shared_map, cpu);

cpu_cpu_mask() # PKG
cpumask_of_node(cpu_to_node(cpu));

Ok so PKG can potentially hit that condition, and so can any sched_domain_mask_f that relies on the node masks...

I'm thinking ideally we should have checks in place to ensure all node_to_cpumask_map[] masks are disjoint, then we could entirely skip the levels that use these masks in topology_span_sane(), but there's unfortunately no nice way to flag them... Also there would cases where there's no real difference between PKG and NODE other than NODE is still based on a per-cpu cpumask and PKG isn't, so I don't see a nicer way to go about this.

Please add something like the following to the changelog, and with that:

"""
Some topology levels (e.g. PKG in default_topology[]) have a sched_domain_mask_f implementation that reuses the same mask for several CPUs (in PKG's case, one mask for all CPUs in the same NUMA node).

For such topology levels, repeating cpumask_equal() checks is wasteful - check that the tl->mask(i) pointers aren't the same first. """

author
#7 lore raw
Quoted 35 lines · Valentin Schneider
Quoted 10 lines · Yury Norov
Quoted 6 lines · Valentin Schneider
Quoted 2 lines · Yury Norov

The function may call cpumask_equal with tl->mask(cpu) == tl->mask(i), even when cpu != i.

For which architecture have you observed this? AFAIA all implementations of tl->sched_domain_mask_f are built on a per-CPU cpumask.

x86_64, qemu emulating 16 CPUs in 4 nodes, Linux 6.10, approximately defconfig.

For the default_topology:
cpu_smt_mask() # SMT
(per_cpu(cpu_sibling_map, cpu))

cpu_clustergroup_mask() # CLS
per_cpu(cpu_l2c_shared_map, cpu);

cpu_coregroup_mask() # MC
per_cpu(cpu_llc_shared_map, cpu);

cpu_cpu_mask() # PKG
cpumask_of_node(cpu_to_node(cpu));

Ok so PKG can potentially hit that condition, and so can any sched_domain_mask_f that relies on the node masks...

I'm thinking ideally we should have checks in place to ensure all node_to_cpumask_map[] masks are disjoint, then we could entirely skip the levels that use these masks in topology_span_sane(), but there's unfortunately no nice way to flag them... Also there would cases where there's no real difference between PKG and NODE other than NODE is still based on a per-cpu cpumask and PKG isn't, so I don't see a nicer way to go about this.

Quoted 2 lines

Please add something like the following to the changelog, and with that:

Sure, will do.

Quoted 8 lines

"""
Some topology levels (e.g. PKG in default_topology[]) have a sched_domain_mask_f implementation that reuses the same mask for several CPUs (in PKG's case, one mask for all CPUs in the same NUMA node).

For such topology levels, repeating cpumask_equal() checks is wasteful - check that the tl->mask(i) pointers aren't the same first. """