sched/topology: optimize topology_span_sane()
v2 cover sched/topology: optimize topology_span_sane()
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.
Valentin Schneider shares on it:
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 be 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.
v1: https://lore.kernel.org/lkml/ZrJk00cmVaUIAr4G@yury-ThinkPad/T/
v2:
- defer initialization of 'mc' in patch #1 @Chen Yu;
- more comments from Valentin Schneider.
Yury Norov (2):
sched/topology: pre-compute topology_span_sane() loop params sched/topology: optimize topology_span_sane()
kernel/sched/topology.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/2 sched/topology: pre-compute topology_span_sane() loop params
tl->mask() is called inside the loop with the same parameters more than once. We can pre-calculate it.
--- kernel/sched/topology.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/sched/topology.c +6 -2
@@ -2353,12 +2353,15 @@ static struct sched_domain *build_sched_domain(struct sched_domain_topology_levestatic bool topology_span_sane (struct sched_domain_topology_level *tl,const struct cpumask *cpu_map,int cpu) { +const struct cpumask *mi, *mc;int i = cpu +1 ;/* NUMA levels are allowed to overlap */ if (tl->flags &SDTL_OVERLAP )return true; + mc = tl->mask (cpu); +/* * Non-NUMA levels cannot partially overlap - they must be either * completely equal or completely disjoint. Otherwise we can end up
@@ -2366,14 +2369,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
PATCH v2 2/2 sched/topology: optimize topology_span_sane()
In reply to Yury NorovThe function may call cpumask_equal with tl->mask(cpu) == tl->mask(i), even though cpu != i. In such case, cpumask_equal() would always return true, and we can proceed to the next iteration immediately.
Comment is provided by Valentin Schneider.
--- kernel/sched/topology.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
kernel/sched/topology.c +12 -0
@@ -2370,6 +2370,18 @@ static bool topology_span_sane(struct sched_domain_topology_level *tl,*/ for_each_cpu_from (i, cpu_map) { mi = tl->mask (i); +/* +* 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. Instead, we first check that the +* tl->mask(i) pointers aren't the same. +*/ +if (mi == mc) +continue ;/* * We should 'and' all those masks with 'cpu_map' to exactly
Signature · 1 line
2.43.0
Ping?
Quoted 33 lines · Yury Norov
The function may call
cpumask_equalwithtl->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.Valentin Schneider shares on it:
PKG can potentially hit that condition, and so can any
sched_domain_mask_fthat 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 be 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.v1: https://lore.kernel.org/lkml/ZrJk00cmVaUIAr4G@yury-ThinkPad/T/
v2:
- defer initialization of 'mc' in patch #1 @Chen Yu;
- more comments from Valentin Schneider.Yury Norov (2):
sched/topology: pre-computetopology_span_sane()loop params sched/topology: optimizetopology_span_sane()kernel/sched/topology.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)Signature · 1 line
2.43.0
Ping again?
Quoted 36 lines · Yury Norov
Ping?
Quoted 33 lines · Yury Norov
The function may call
cpumask_equalwithtl->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.Valentin Schneider shares on it:
PKG can potentially hit that condition, and so can any
sched_domain_mask_fthat 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 be 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.v1: https://lore.kernel.org/lkml/ZrJk00cmVaUIAr4G@yury-ThinkPad/T/
v2:
- defer initialization of 'mc' in patch #1 @Chen Yu;
- more comments from Valentin Schneider.Yury Norov (2):
sched/topology: pre-computetopology_span_sane()loop params sched/topology: optimizetopology_span_sane()kernel/sched/topology.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)Signature · 1 line
2.43.0
Le 28/08/2024 à 16:09, Yury Norov a écrit :
Quoted 2 lines
Ping again?
Hi,
The covert letter is v2, but the patch below it are without v2.
In patch 1/2, the Suggested-by: tags in v1 have been removed in this new version :(.
CJ
Quoted 39 lines · Yury Norov
Quoted 36 lines
Ping?
Quoted 33 lines · Yury Norov
The function may call
cpumask_equalwithtl->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.Valentin Schneider shares on it:
PKG can potentially hit that condition, and so can any
sched_domain_mask_fthat 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 be 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.v1: https://lore.kernel.org/lkml/ZrJk00cmVaUIAr4G@yury-ThinkPad/T/
v2:
- defer initialization of 'mc' in patch #1 @Chen Yu;
- more comments from Valentin Schneider.Yury Norov (2):
sched/topology: pre-computetopology_span_sane()loop params sched/topology: optimizetopology_span_sane()kernel/sched/topology.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)Signature · 1 line
2.43.0
Quoted 10 lines · Christophe JAILLET
Le 28/08/2024 à 16:09, Yury Norov a écrit :
Quoted 2 lines
Ping again?
Hi,
The covert letter is v2, but the patch below it are without v2.
In patch 1/2, the Suggested-by: tags in v1 have been removed in this new version :(.
My bad. I'll send v3.