Skip to content

Commit f55dac1

Browse files
swahlhpePeter Zijlstra
authored andcommitted
sched/topology: improve topology_span_sane speed
Use a different approach to topology_span_sane(), that checks for the same constraint of no partial overlaps for any two CPU sets for non-NUMA topology levels, but does so in a way that is O(N) rather than O(N^2). Instead of comparing with all other masks to detect collisions, keep one mask that includes all CPUs seen so far and detect collisions with a single cpumask_intersects test. If the current mask has no collisions with previously seen masks, it should be a new mask, which can be uniquely identified by the lowest bit set in this mask. Keep a pointer to this mask for future reference (in an array indexed by the lowest bit set), and add the CPUs in this mask to the list of those seen. If the current mask does collide with previously seen masks, it should be exactly equal to a mask seen before, looked up in the same array indexed by the lowest bit set in the mask, a single comparison. Move the topology_span_sane() check out of the existing topology level loop, let it use its own loop so that the array allocation can be done only once, shared across levels. On a system with 1920 processors (16 sockets, 60 cores, 2 threads), the average time to take one processor offline is reduced from 2.18 seconds to 1.01 seconds. (Off-lining 959 of 1920 processors took 34m49.765s without this change, 16m10.038s with this change in place.) Signed-off-by: Steve Wahl <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Valentin Schneider <[email protected]> Reviewed-by: Madadi Vineeth Reddy <[email protected]> Tested-by: K Prateek Nayak <[email protected]> Tested-by: Valentin Schneider <[email protected]> Tested-by: Madadi Vineeth Reddy <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 8feb053 commit f55dac1

File tree

1 file changed

+58
-25
lines changed

1 file changed

+58
-25
lines changed

kernel/sched/topology.c

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,36 +2347,69 @@ static struct sched_domain *build_sched_domain(struct sched_domain_topology_leve
23472347

23482348
/*
23492349
* Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
2350-
* any two given CPUs at this (non-NUMA) topology level.
2350+
* any two given CPUs on non-NUMA topology levels.
23512351
*/
2352-
static bool topology_span_sane(struct sched_domain_topology_level *tl,
2353-
const struct cpumask *cpu_map, int cpu)
2352+
static bool topology_span_sane(const struct cpumask *cpu_map)
23542353
{
2355-
int i = cpu + 1;
2354+
struct sched_domain_topology_level *tl;
2355+
const struct cpumask **masks;
2356+
struct cpumask *covered;
2357+
int cpu, id;
2358+
bool ret = false;
23562359

2357-
/* NUMA levels are allowed to overlap */
2358-
if (tl->flags & SDTL_OVERLAP)
2359-
return true;
2360+
lockdep_assert_held(&sched_domains_mutex);
2361+
covered = sched_domains_tmpmask;
2362+
2363+
masks = kmalloc_array(nr_cpu_ids, sizeof(struct cpumask *), GFP_KERNEL);
2364+
if (!masks)
2365+
return ret;
2366+
2367+
for_each_sd_topology(tl) {
2368+
2369+
/* NUMA levels are allowed to overlap */
2370+
if (tl->flags & SDTL_OVERLAP)
2371+
continue;
2372+
2373+
cpumask_clear(covered);
2374+
memset(masks, 0, nr_cpu_ids * sizeof(struct cpumask *));
23602375

2361-
/*
2362-
* Non-NUMA levels cannot partially overlap - they must be either
2363-
* completely equal or completely disjoint. Otherwise we can end up
2364-
* breaking the sched_group lists - i.e. a later get_group() pass
2365-
* breaks the linking done for an earlier span.
2366-
*/
2367-
for_each_cpu_from(i, cpu_map) {
23682376
/*
2369-
* We should 'and' all those masks with 'cpu_map' to exactly
2370-
* match the topology we're about to build, but that can only
2371-
* remove CPUs, which only lessens our ability to detect
2372-
* overlaps
2377+
* Non-NUMA levels cannot partially overlap - they must be either
2378+
* completely equal or completely disjoint. Otherwise we can end up
2379+
* breaking the sched_group lists - i.e. a later get_group() pass
2380+
* breaks the linking done for an earlier span.
23732381
*/
2374-
if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
2375-
cpumask_intersects(tl->mask(cpu), tl->mask(i)))
2376-
return false;
2382+
for_each_cpu(cpu, cpu_map) {
2383+
/* lowest bit set in this mask is used as a unique id */
2384+
id = cpumask_first(tl->mask(cpu));
2385+
2386+
/* zeroed masks cannot possibly collide */
2387+
if (id >= nr_cpu_ids)
2388+
continue;
2389+
2390+
/* if this mask doesn't collide with what we've already seen */
2391+
if (!cpumask_intersects(tl->mask(cpu), covered)) {
2392+
/* this failing would be an error in this algorithm */
2393+
if (WARN_ON(masks[id]))
2394+
goto notsane;
2395+
2396+
/* record the mask we saw for this id */
2397+
masks[id] = tl->mask(cpu);
2398+
cpumask_or(covered, tl->mask(cpu), covered);
2399+
} else if ((!masks[id]) || !cpumask_equal(masks[id], tl->mask(cpu))) {
2400+
/*
2401+
* a collision with covered should have exactly matched
2402+
* a previously seen mask with the same id
2403+
*/
2404+
goto notsane;
2405+
}
2406+
}
23772407
}
2408+
ret = true;
23782409

2379-
return true;
2410+
notsane:
2411+
kfree(masks);
2412+
return ret;
23802413
}
23812414

23822415
/*
@@ -2408,9 +2441,6 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att
24082441
sd = NULL;
24092442
for_each_sd_topology(tl) {
24102443

2411-
if (WARN_ON(!topology_span_sane(tl, cpu_map, i)))
2412-
goto error;
2413-
24142444
sd = build_sched_domain(tl, cpu_map, attr, sd, i);
24152445

24162446
has_asym |= sd->flags & SD_ASYM_CPUCAPACITY;
@@ -2424,6 +2454,9 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att
24242454
}
24252455
}
24262456

2457+
if (WARN_ON(!topology_span_sane(cpu_map)))
2458+
goto error;
2459+
24272460
/* Build the groups for the domains */
24282461
for_each_cpu(i, cpu_map) {
24292462
for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {

0 commit comments

Comments
 (0)