Skip to content

Commit 3554f7f

Browse files
committed
sched/topology: Refinement to topology_span_sane speedup
JIRA: https://issues.redhat.com/browse/RHEL-110301 commit ce29a7d Author: Steve Wahl <[email protected]> Date: Tue Mar 4 10:08:44 2025 -0600 sched/topology: Refinement to topology_span_sane speedup Simplify the topology_span_sane code further, removing the need to allocate an array and gotos used to make sure the array gets freed. This version is in a separate commit because it could return a different sanity result than the previous code, but only in odd circumstances that are not expected to actually occur; for example, when a CPU is not listed in its own mask. 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] Signed-off-by: Phil Auld <[email protected]>
1 parent f124c0e commit 3554f7f

File tree

1 file changed

+19
-33
lines changed

1 file changed

+19
-33
lines changed

kernel/sched/topology.c

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,17 +2400,12 @@ static struct sched_domain *build_sched_domain(struct sched_domain_topology_leve
24002400
static bool topology_span_sane(const struct cpumask *cpu_map)
24012401
{
24022402
struct sched_domain_topology_level *tl;
2403-
const struct cpumask **masks;
2404-
struct cpumask *covered;
2405-
int cpu, id;
2406-
bool ret = false;
2403+
struct cpumask *covered, *id_seen;
2404+
int cpu;
24072405

24082406
lockdep_assert_held(&sched_domains_mutex);
24092407
covered = sched_domains_tmpmask;
2410-
2411-
masks = kmalloc_array(nr_cpu_ids, sizeof(struct cpumask *), GFP_KERNEL);
2412-
if (!masks)
2413-
return ret;
2408+
id_seen = sched_domains_tmpmask2;
24142409

24152410
for_each_sd_topology(tl) {
24162411

@@ -2419,7 +2414,7 @@ static bool topology_span_sane(const struct cpumask *cpu_map)
24192414
continue;
24202415

24212416
cpumask_clear(covered);
2422-
memset(masks, 0, nr_cpu_ids * sizeof(struct cpumask *));
2417+
cpumask_clear(id_seen);
24232418

24242419
/*
24252420
* Non-NUMA levels cannot partially overlap - they must be either
@@ -2428,36 +2423,27 @@ static bool topology_span_sane(const struct cpumask *cpu_map)
24282423
* breaks the linking done for an earlier span.
24292424
*/
24302425
for_each_cpu(cpu, cpu_map) {
2431-
/* lowest bit set in this mask is used as a unique id */
2432-
id = cpumask_first(tl->mask(cpu));
2426+
const struct cpumask *tl_cpu_mask = tl->mask(cpu);
2427+
int id;
24332428

2434-
/* zeroed masks cannot possibly collide */
2435-
if (id >= nr_cpu_ids)
2436-
continue;
2429+
/* lowest bit set in this mask is used as a unique id */
2430+
id = cpumask_first(tl_cpu_mask);
24372431

2438-
/* if this mask doesn't collide with what we've already seen */
2439-
if (!cpumask_intersects(tl->mask(cpu), covered)) {
2440-
/* this failing would be an error in this algorithm */
2441-
if (WARN_ON(masks[id]))
2442-
goto notsane;
2432+
if (cpumask_test_cpu(id, id_seen)) {
2433+
/* First CPU has already been seen, ensure identical spans */
2434+
if (!cpumask_equal(tl->mask(id), tl_cpu_mask))
2435+
return false;
2436+
} else {
2437+
/* First CPU hasn't been seen before, ensure it's a completely new span */
2438+
if (cpumask_intersects(tl_cpu_mask, covered))
2439+
return false;
24432440

2444-
/* record the mask we saw for this id */
2445-
masks[id] = tl->mask(cpu);
2446-
cpumask_or(covered, tl->mask(cpu), covered);
2447-
} else if ((!masks[id]) || !cpumask_equal(masks[id], tl->mask(cpu))) {
2448-
/*
2449-
* a collision with covered should have exactly matched
2450-
* a previously seen mask with the same id
2451-
*/
2452-
goto notsane;
2441+
cpumask_or(covered, covered, tl_cpu_mask);
2442+
cpumask_set_cpu(id, id_seen);
24532443
}
24542444
}
24552445
}
2456-
ret = true;
2457-
2458-
notsane:
2459-
kfree(masks);
2460-
return ret;
2446+
return true;
24612447
}
24622448

24632449
/*

0 commit comments

Comments
 (0)