Skip to contentSkip to conversation

sched/topology: improve topology_span_sane speed

PATCH Superseded Steve Wahl 6 participants 13 messages Oct 31, 2024 – Dec 17, 2024 Scheduler

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Steve Wahl

author
#0 lore raw

PATCH v2 1/1 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 ("id") by the lowest bit set in this mask. Mark that we've seen a mask with this id, 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, identified once again by the lowest bit the current mask has set. It's an error if we haven't seen a mask with that id, or if the current mask doesn't match the one we get by looking up that id.

Move the topology_span_sane() check out of the existing topology level loop, let it do its own looping to match the needs of this algorithm.

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.)

---

Version 2: Adopted suggestion by K Prateek Nayak that removes an array and simplifies the code, and eliminates the erroneous use of num_possible_cpus() that Peter Zijlstra noted.

Version 1 discussion:

    https://lore.kernel.org/all/[email protected]/
 kernel/sched/topology.c | 73 +++++++++++++++++++++++++++--------------
 1 file changed, 48 insertions(+), 25 deletions(-)
kernel/sched/topology.c +48 -25
index 9748a4c8d668..6a2a3e91d59e 100644
@@ -2356,35 +2356,58 @@ static struct sched_domain *build_sched_domain(struct sched_domain_topology_leve
 
 /*
  * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
- * any two given CPUs at this (non-NUMA) topology level.
+ * any two given CPUs on non-NUMA topology levels.
  */
-static bool topology_span_sane(struct sched_domain_topology_level *tl,
-			      const struct cpumask *cpu_map, int cpu)
+static bool topology_span_sane(const struct cpumask *cpu_map)
 {
-	int i = cpu + 1;
+	struct sched_domain_topology_level *tl;
+	struct cpumask *covered, *id_seen;
+	int cpu;
 
-	/* NUMA levels are allowed to overlap */
-	if (tl->flags & SDTL_OVERLAP)
-		return true;
+	lockdep_assert_held(&sched_domains_mutex);
+	covered = sched_domains_tmpmask;
+	id_seen = sched_domains_tmpmask2;
+
+	for_each_sd_topology(tl) {
+
+		/* NUMA levels are allowed to overlap */
+		if (tl->flags & SDTL_OVERLAP)
+			continue;
+
+		cpumask_clear(covered);
+		cpumask_clear(id_seen);
 
-	/*
-	 * Non-NUMA levels cannot partially overlap - they must be either
-	 * completely equal or completely disjoint. Otherwise we can end up
-	 * breaking the sched_group lists - i.e. a later get_group() pass
-	 * breaks the linking done for an earlier span.
-	 */
-	for_each_cpu_from(i, cpu_map) {
 		/*
-		 * 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
+		 * Non-NUMA levels cannot partially overlap - they must be either
+		 * completely equal or completely disjoint. Otherwise we can end up
+		 * breaking the sched_group lists - i.e. a later get_group() pass
+		 * breaks the linking done for an earlier span.
 		 */
-		if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
-		    cpumask_intersects(tl->mask(cpu), tl->mask(i)))
-			return false;
+		for_each_cpu(cpu, cpu_map) {
+			const struct cpumask *tl_cpu_mask = tl->mask(cpu);
+			int id;
+
+			/* lowest bit set in this mask is used as a unique id */
+			id = cpumask_first(tl_cpu_mask);
+
+			/* if this mask doesn't collide with what we've already seen */
+			if (!cpumask_intersects(tl_cpu_mask, covered)) {
+				/* Really odd case when cpu != id, likely not sane */
+				if ((cpu != id) && !cpumask_equal(tl_cpu_mask, tl->mask(id)))
+					return false;
+				if (cpumask_test_and_set_cpu(id, id_seen))
+					return false;
+				cpumask_or(covered, tl_cpu_mask, covered);
+			} else if ((!cpumask_test_cpu(id, id_seen)) ||
+				    !cpumask_equal(tl->mask(id), tl_cpu_mask)) {
+				/*
+				 * a collision with covered should have exactly matched
+				 * a previously seen mask with the same id
+				 */
+				return false;
+			}
+		}
 	}
-
 	return true;
 }
 
@@ -2417,9 +2440,6 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att
 		sd = NULL;
 		for_each_sd_topology(tl) {
 
-			if (WARN_ON(!topology_span_sane(tl, cpu_map, i)))
-				goto error;
-
 			sd = build_sched_domain(tl, cpu_map, attr, sd, i);
 
 			has_asym |= sd->flags & SD_ASYM_CPUCAPACITY;
@@ -2433,6 +2453,9 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att
 		}
 	}
 
+	if (WARN_ON(!topology_span_sane(cpu_map)))
+		goto error;
+
 	/* Build the groups for the domains */
 	for_each_cpu(i, cpu_map) {
 		for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
Signature · 1 line
2.26.2
reviewer Tested-by on v2 1/1
#1 lore raw

Hello Steve,

Quoted 98 lines · Steve Wahl

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 ("id") by the lowest bit set in this mask. Mark that we've seen a mask with this id, 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, identified once again by the lowest bit the current mask has set. It's an error if we haven't seen a mask with that id, or if the current mask doesn't match the one we get by looking up that id.

Move the topology_span_sane() check out of the existing topology level loop, let it do its own looping to match the needs of this algorithm.

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.)

---

Version 2: Adopted suggestion by K Prateek Nayak that removes an array and simplifies the code, and eliminates the erroneous use of num_possible_cpus() that Peter Zijlstra noted.

Version 1 discussion:

     https://lore.kernel.org/all/[email protected]/
  kernel/sched/topology.c | 73 +++++++++++++++++++++++++++--------------
  1 file changed, 48 insertions(+), 25 deletions(-)
kernel/sched/topology.c +22 -21
index 9748a4c8d668..6a2a3e91d59e 100644
@@ -2356,35 +2356,58 @@ static struct sched_domain *build_sched_domain(struct sched_domain_topology_leve
 
  /*
   * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
- * any two given CPUs at this (non-NUMA) topology level.
+ * any two given CPUs on non-NUMA topology levels.
   */
-static bool topology_span_sane(struct sched_domain_topology_level *tl,
-			      const struct cpumask *cpu_map, int cpu)
+static bool topology_span_sane(const struct cpumask *cpu_map)
  {
-	int i = cpu + 1;
+	struct sched_domain_topology_level *tl;
+	struct cpumask *covered, *id_seen;
+	int cpu;
 
-	/* NUMA levels are allowed to overlap */
-	if (tl->flags & SDTL_OVERLAP)
-		return true;
+	lockdep_assert_held(&sched_domains_mutex);
+	covered = sched_domains_tmpmask;
+	id_seen = sched_domains_tmpmask2;
+
+	for_each_sd_topology(tl) {
+
+		/* NUMA levels are allowed to overlap */
+		if (tl->flags & SDTL_OVERLAP)
+			continue;
+
+		cpumask_clear(covered);
+		cpumask_clear(id_seen);
 
-	/*
-	 * Non-NUMA levels cannot partially overlap - they must be either
-	 * completely equal or completely disjoint. Otherwise we can end up
-	 * breaking the sched_group lists - i.e. a later get_group() pass
-	 * breaks the linking done for an earlier span.
-	 */
-	for_each_cpu_from(i, cpu_map) {
  		/*
-		 * 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
+		 * Non-NUMA levels cannot partially overlap - they must be either
+		 * completely equal or completely disjoint. Otherwise we can end up
+		 * breaking the sched_group lists - i.e. a later get_group() pass
+		 * breaks the linking done for an earlier span.
  		 */
-		if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
-		    cpumask_intersects(tl->mask(cpu), tl->mask(i)))
-			return false;
+		for_each_cpu(cpu, cpu_map) {

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

Quoted 10 lines
kernel/sched/topology.c +10 -0
+			const struct cpumask *tl_cpu_mask = tl->mask(cpu);
+			int id;
+
+			/* lowest bit set in this mask is used as a unique id */
+			id = cpumask_first(tl_cpu_mask);
+
+			/* if this mask doesn't collide with what we've already seen */
+			if (!cpumask_intersects(tl_cpu_mask, covered)) {
+				/* Really odd case when cpu != id, likely not sane */
+				if ((cpu != id) && !cpumask_equal(tl_cpu_mask, tl->mask(id)))

I was wondering, since we are doing a "for_each_cpu(cpu, cpu_map)", wouldn't we always see the "id" cpu first since "id" is nothing but the first CPU of the topology level mask? Maybe I'm not thinking creatively enough but I cannot see a scenario where the sanity check will trip here and not in the "else if" clause down below :)

I've done some basic testing on my system on all NPS modes and also with "numa=fake" cmdline and I do not see any splats in my case. Please feel free to include:

I'll try to hack the kernel to trip the topology check, with and without the patch, and also try to get back with some hotplug perf numbers but for the time being I do not see any false negatives with the approach on my dual socket 3rd Generation EPYC system (2 x 64C/128T)

Signature · 2 lines
Thanks and Regards,
Prateek
Quoted 38 lines
kernel/sched/topology.c +16 -4
+					return false;
+				if (cpumask_test_and_set_cpu(id, id_seen))
+					return false;
+				cpumask_or(covered, tl_cpu_mask, covered);
+			} else if ((!cpumask_test_cpu(id, id_seen)) ||
+				    !cpumask_equal(tl->mask(id), tl_cpu_mask)) {
+				/*
+				 * a collision with covered should have exactly matched
+				 * a previously seen mask with the same id
+				 */
+				return false;
+			}
+		}
  	}
-
  	return true;
  }
 
@@ -2417,9 +2440,6 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att
  		sd = NULL;
  		for_each_sd_topology(tl) {
 
-			if (WARN_ON(!topology_span_sane(tl, cpu_map, i)))
-				goto error;
-
  			sd = build_sched_domain(tl, cpu_map, attr, sd, i);
 
  			has_asym |= sd->flags & SD_ASYM_CPUCAPACITY;
@@ -2433,6 +2453,9 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att
  		}
  	}
 
+	if (WARN_ON(!topology_span_sane(cpu_map)))
+		goto error;
+
  	/* Build the groups for the domains */
  	for_each_cpu(i, cpu_map) {
  		for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
author
#2 lore raw
Quoted 1 line · K Prateek Nayak

Hello Steve,

Hi, Prateek; thanks for looking at this patch.

Quoted 104 lines · Steve Wahl
Quoted 103 lines

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 ("id") by the lowest bit set in this mask. Mark that we've seen a mask with this id, 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, identified once again by the lowest bit the current mask has set. It's an error if we haven't seen a mask with that id, or if the current mask doesn't match the one we get by looking up that id.

Move the topology_span_sane() check out of the existing topology level loop, let it do its own looping to match the needs of this algorithm.

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.)

---

Version 2: Adopted suggestion by K Prateek Nayak that removes an array and simplifies the code, and eliminates the erroneous use of num_possible_cpus() that Peter Zijlstra noted.

Version 1 discussion:

     https://lore.kernel.org/all/[email protected]/
  kernel/sched/topology.c | 73 +++++++++++++++++++++++++++--------------
  1 file changed, 48 insertions(+), 25 deletions(-)
kernel/sched/topology.c +30 -21
index 9748a4c8d668..6a2a3e91d59e 100644
@@ -2356,35 +2356,58 @@ static struct sched_domain *build_sched_domain(struct sched_domain_topology_leve
  /*
   * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
- * any two given CPUs at this (non-NUMA) topology level.
+ * any two given CPUs on non-NUMA topology levels.
   */
-static bool topology_span_sane(struct sched_domain_topology_level *tl,
-			      const struct cpumask *cpu_map, int cpu)
+static bool topology_span_sane(const struct cpumask *cpu_map)
  {
-	int i = cpu + 1;
+	struct sched_domain_topology_level *tl;
+	struct cpumask *covered, *id_seen;
+	int cpu;
-	/* NUMA levels are allowed to overlap */
-	if (tl->flags & SDTL_OVERLAP)
-		return true;
+	lockdep_assert_held(&sched_domains_mutex);
+	covered = sched_domains_tmpmask;
+	id_seen = sched_domains_tmpmask2;
+
+	for_each_sd_topology(tl) {
+
+		/* NUMA levels are allowed to overlap */
+		if (tl->flags & SDTL_OVERLAP)
+			continue;
+
+		cpumask_clear(covered);
+		cpumask_clear(id_seen);
-	/*
-	 * Non-NUMA levels cannot partially overlap - they must be either
-	 * completely equal or completely disjoint. Otherwise we can end up
-	 * breaking the sched_group lists - i.e. a later get_group() pass
-	 * breaks the linking done for an earlier span.
-	 */
-	for_each_cpu_from(i, cpu_map) {
  		/*
-		 * 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
+		 * Non-NUMA levels cannot partially overlap - they must be either
+		 * completely equal or completely disjoint. Otherwise we can end up
+		 * breaking the sched_group lists - i.e. a later get_group() pass
+		 * breaks the linking done for an earlier span.
  		 */
-		if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
-		    cpumask_intersects(tl->mask(cpu), tl->mask(i)))
-			return false;
+		for_each_cpu(cpu, cpu_map) {
+			const struct cpumask *tl_cpu_mask = tl->mask(cpu);
+			int id;
+
+			/* lowest bit set in this mask is used as a unique id */
+			id = cpumask_first(tl_cpu_mask);
+
+			/* if this mask doesn't collide with what we've already seen */
+			if (!cpumask_intersects(tl_cpu_mask, covered)) {
Quoted 8 lines

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

Quoted 2 lines
kernel/sched/topology.c +2 -0
+				/* Really odd case when cpu != id, likely not sane */
+				if ((cpu != id) && !cpumask_equal(tl_cpu_mask, tl->mask(id)))

I was wondering, since we are doing a "for_each_cpu(cpu, cpu_map)", wouldn't we always see the "id" cpu first since "id" is nothing but the first CPU of the topology level mask? Maybe I'm not thinking creatively enough but I cannot see a scenario where the sanity check will trip here and not in the "else if" clause down below :)

The scenario I was thinking of that could trip this is if tl->mask(cpu) does not actually include this CPU. It should not happen, but we are looking to detect a malformed set of masks, so I added the check.

Quoted 4 lines

I've done some basic testing on my system on all NPS modes and also with "numa=fake" cmdline and I do not see any splats in my case. Please feel free to include:

Quoted 10 lines

I'll try to hack the kernel to trip the topology check, with and without the patch, and also try to get back with some hotplug perf numbers but for the time being I do not see any false negatives with the approach on my dual socket 3rd Generation EPYC system (2 x 64C/128T)

Signature · 2 lines
Thanks and Regards,
Prateek

Thanks so much!

--> Steve Wahl

Quoted 35 lines
Quoted 34 lines
kernel/sched/topology.c +16 -4
+					return false;
+				if (cpumask_test_and_set_cpu(id, id_seen))
+					return false;
+				cpumask_or(covered, tl_cpu_mask, covered);
+			} else if ((!cpumask_test_cpu(id, id_seen)) ||
+				    !cpumask_equal(tl->mask(id), tl_cpu_mask)) {
+				/*
+				 * a collision with covered should have exactly matched
+				 * a previously seen mask with the same id
+				 */
+				return false;
+			}
+		}
  	}
-
  	return true;
  }
@@ -2417,9 +2440,6 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att
  		sd = NULL;
  		for_each_sd_topology(tl) {
-			if (WARN_ON(!topology_span_sane(tl, cpu_map, i)))
-				goto error;
-
  			sd = build_sched_domain(tl, cpu_map, attr, sd, i);
  			has_asym |= sd->flags & SD_ASYM_CPUCAPACITY;
@@ -2433,6 +2453,9 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att
  		}
  	}
+	if (WARN_ON(!topology_span_sane(cpu_map)))
+		goto error;
+
  	/* Build the groups for the domains */
  	for_each_cpu(i, cpu_map) {
  		for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
Signature · 1 line
Steve Wahl, Hewlett Packard Enterprise
#3 lore raw
In reply to Steve Wahl
Quoted 111 lines · Steve Wahl

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 ("id") by the lowest bit set in this mask. Mark that we've seen a mask with this id, 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, identified once again by the lowest bit the current mask has set. It's an error if we haven't seen a mask with that id, or if the current mask doesn't match the one we get by looking up that id.

Move the topology_span_sane() check out of the existing topology level loop, let it do its own looping to match the needs of this algorithm.

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.)

---

Version 2: Adopted suggestion by K Prateek Nayak that removes an array and simplifies the code, and eliminates the erroneous use of num_possible_cpus() that Peter Zijlstra noted.

Version 1 discussion:

    https://lore.kernel.org/all/[email protected]/
 kernel/sched/topology.c | 73 +++++++++++++++++++++++++++--------------
 1 file changed, 48 insertions(+), 25 deletions(-)
kernel/sched/topology.c +35 -21
index 9748a4c8d668..6a2a3e91d59e 100644
@@ -2356,35 +2356,58 @@ static struct sched_domain *build_sched_domain(struct sched_domain_topology_leve
 
 /*
  * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
- * any two given CPUs at this (non-NUMA) topology level.
+ * any two given CPUs on non-NUMA topology levels.
  */
-static bool topology_span_sane(struct sched_domain_topology_level *tl,
-			      const struct cpumask *cpu_map, int cpu)
+static bool topology_span_sane(const struct cpumask *cpu_map)
 {
-	int i = cpu + 1;
+	struct sched_domain_topology_level *tl;
+	struct cpumask *covered, *id_seen;
+	int cpu;
 
-	/* NUMA levels are allowed to overlap */
-	if (tl->flags & SDTL_OVERLAP)
-		return true;
+	lockdep_assert_held(&sched_domains_mutex);
+	covered = sched_domains_tmpmask;
+	id_seen = sched_domains_tmpmask2;
+
+	for_each_sd_topology(tl) {
+
+		/* NUMA levels are allowed to overlap */
+		if (tl->flags & SDTL_OVERLAP)
+			continue;
+
+		cpumask_clear(covered);
+		cpumask_clear(id_seen);
 
-	/*
-	 * Non-NUMA levels cannot partially overlap - they must be either
-	 * completely equal or completely disjoint. Otherwise we can end up
-	 * breaking the sched_group lists - i.e. a later get_group() pass
-	 * breaks the linking done for an earlier span.
-	 */
-	for_each_cpu_from(i, cpu_map) {
 		/*
-		 * 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
+		 * Non-NUMA levels cannot partially overlap - they must be either
+		 * completely equal or completely disjoint. Otherwise we can end up
+		 * breaking the sched_group lists - i.e. a later get_group() pass
+		 * breaks the linking done for an earlier span.
 		 */
-		if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
-		    cpumask_intersects(tl->mask(cpu), tl->mask(i)))
-			return false;
+		for_each_cpu(cpu, cpu_map) {
+			const struct cpumask *tl_cpu_mask = tl->mask(cpu);
+			int id;
+
+			/* lowest bit set in this mask is used as a unique id */
+			id = cpumask_first(tl_cpu_mask);
+
+			/* if this mask doesn't collide with what we've already seen */
+			if (!cpumask_intersects(tl_cpu_mask, covered)) {
+				/* Really odd case when cpu != id, likely not sane */
+				if ((cpu != id) && !cpumask_equal(tl_cpu_mask, tl->mask(id)))
+					return false;
+				if (cpumask_test_and_set_cpu(id, id_seen))
+					return false;

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

Quoted 10 lines
kernel/sched/topology.c +10 -0
+				cpumask_or(covered, tl_cpu_mask, covered);
+			} else if ((!cpumask_test_cpu(id, id_seen)) ||
+				    !cpumask_equal(tl->mask(id), tl_cpu_mask)) {
+				/*
+				 * a collision with covered should have exactly matched
+				 * a previously seen mask with the same id
+				 */
+				return false;
+			}
+		}

Ok so you're speeding it up, but you still get a O(nr_cpu_ids) walk every hotplug when the check itself only needs to be done at most once per possible online CPU combination (~ 2^(nr_cpu_ids)). If all CPUs are kicked to life at boot, then the check only needs to be done once. If you only boot with a subset of present CPUs to speed things up, the check still becomes irrelevant once you've kicked the rest to life.

I would reiterate my suggestion to get to a state where the check can be entirely short-circuited [1].

[1]: http://lore.kernel.org/r/[email protected]

author
#4 lore raw
Quoted 130 lines · Valentin Schneider
Quoted 111 lines · Steve Wahl

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 ("id") by the lowest bit set in this mask. Mark that we've seen a mask with this id, 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, identified once again by the lowest bit the current mask has set. It's an error if we haven't seen a mask with that id, or if the current mask doesn't match the one we get by looking up that id.

Move the topology_span_sane() check out of the existing topology level loop, let it do its own looping to match the needs of this algorithm.

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.)

---

Version 2: Adopted suggestion by K Prateek Nayak that removes an array and simplifies the code, and eliminates the erroneous use of num_possible_cpus() that Peter Zijlstra noted.

Version 1 discussion:

    https://lore.kernel.org/all/[email protected]/
 kernel/sched/topology.c | 73 +++++++++++++++++++++++++++--------------
 1 file changed, 48 insertions(+), 25 deletions(-)
kernel/sched/topology.c +35 -21
index 9748a4c8d668..6a2a3e91d59e 100644
@@ -2356,35 +2356,58 @@ static struct sched_domain *build_sched_domain(struct sched_domain_topology_leve
 
 /*
  * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
- * any two given CPUs at this (non-NUMA) topology level.
+ * any two given CPUs on non-NUMA topology levels.
  */
-static bool topology_span_sane(struct sched_domain_topology_level *tl,
-			      const struct cpumask *cpu_map, int cpu)
+static bool topology_span_sane(const struct cpumask *cpu_map)
 {
-	int i = cpu + 1;
+	struct sched_domain_topology_level *tl;
+	struct cpumask *covered, *id_seen;
+	int cpu;
 
-	/* NUMA levels are allowed to overlap */
-	if (tl->flags & SDTL_OVERLAP)
-		return true;
+	lockdep_assert_held(&sched_domains_mutex);
+	covered = sched_domains_tmpmask;
+	id_seen = sched_domains_tmpmask2;
+
+	for_each_sd_topology(tl) {
+
+		/* NUMA levels are allowed to overlap */
+		if (tl->flags & SDTL_OVERLAP)
+			continue;
+
+		cpumask_clear(covered);
+		cpumask_clear(id_seen);
 
-	/*
-	 * Non-NUMA levels cannot partially overlap - they must be either
-	 * completely equal or completely disjoint. Otherwise we can end up
-	 * breaking the sched_group lists - i.e. a later get_group() pass
-	 * breaks the linking done for an earlier span.
-	 */
-	for_each_cpu_from(i, cpu_map) {
 		/*
-		 * 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
+		 * Non-NUMA levels cannot partially overlap - they must be either
+		 * completely equal or completely disjoint. Otherwise we can end up
+		 * breaking the sched_group lists - i.e. a later get_group() pass
+		 * breaks the linking done for an earlier span.
 		 */
-		if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
-		    cpumask_intersects(tl->mask(cpu), tl->mask(i)))
-			return false;
+		for_each_cpu(cpu, cpu_map) {
+			const struct cpumask *tl_cpu_mask = tl->mask(cpu);
+			int id;
+
+			/* lowest bit set in this mask is used as a unique id */
+			id = cpumask_first(tl_cpu_mask);
+
+			/* if this mask doesn't collide with what we've already seen */
+			if (!cpumask_intersects(tl_cpu_mask, covered)) {
+				/* Really odd case when cpu != id, likely not sane */
+				if ((cpu != id) && !cpumask_equal(tl_cpu_mask, tl->mask(id)))
+					return false;
+				if (cpumask_test_and_set_cpu(id, id_seen))
+					return false;

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

Quoted 10 lines
kernel/sched/topology.c +10 -0
+				cpumask_or(covered, tl_cpu_mask, covered);
+			} else if ((!cpumask_test_cpu(id, id_seen)) ||
+				    !cpumask_equal(tl->mask(id), tl_cpu_mask)) {
+				/*
+				 * a collision with covered should have exactly matched
+				 * a previously seen mask with the same id
+				 */
+				return false;
+			}
+		}

Ok so you're speeding it up, but you still get a O(nr_cpu_ids) walk every hotplug when the check itself only needs to be done at most once per possible online CPU combination (~ 2^(nr_cpu_ids)). If all CPUs are kicked to life at boot, then the check only needs to be done once. If you only boot with a subset of present CPUs to speed things up, the check still becomes irrelevant once you've kicked the rest to life.

Quoted 4 lines

I would reiterate my suggestion to get to a state where the check can be entirely short-circuited [1].

[1]: http://lore.kernel.org/r/[email protected]

Bringing forward a bit of that conversation:

Quoted 7 lines
Quoted 4 lines
kernel/sched/topology.c +0 -0
 I tried adding this, surprisingly I saw no effect on the time taken,

perhaps even a small slowdown, when combined with my patch. So at this point I don't intend to add it to v2 of the patch.

Thanks for testing, I assume your cpu_possible_mask reports more CPUs than you have physically plugged in...

That assumption is wrong. I started with all CPUs enabled. Disabled and re-enabled cpus from there. The timings I got were as I stated, no effect, perhaps a small slowdown.

Quoted 7 lines

I guess it would make sense to short-circuit the function when cpu_map is a subset of what we've previously checked, and then re-kick the testing once new CPU(s) are plugged in. Something like the untested below?

Optimisations notwithstanding, IMO we shouldn't be repeating checks if we can avoid it.

I will attempt to measure it once more. I was surprised at my measured results, but that's why we take them, right?

If I can't measure a difference, though, I am not sure it's appropriate to include the change with this patch, the point of which *is* optimization.

--> Steve Wahl

Signature · 1 line
Steve Wahl, Hewlett Packard Enterprise
author
#5 lore raw
Quoted 158 lines · Steve Wahl
Quoted 130 lines · Valentin Schneider
Quoted 111 lines · Steve Wahl

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 ("id") by the lowest bit set in this mask. Mark that we've seen a mask with this id, 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, identified once again by the lowest bit the current mask has set. It's an error if we haven't seen a mask with that id, or if the current mask doesn't match the one we get by looking up that id.

Move the topology_span_sane() check out of the existing topology level loop, let it do its own looping to match the needs of this algorithm.

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.)

---

Version 2: Adopted suggestion by K Prateek Nayak that removes an array and simplifies the code, and eliminates the erroneous use of num_possible_cpus() that Peter Zijlstra noted.

Version 1 discussion:

    https://lore.kernel.org/all/[email protected]/
 kernel/sched/topology.c | 73 +++++++++++++++++++++++++++--------------
 1 file changed, 48 insertions(+), 25 deletions(-)
kernel/sched/topology.c +35 -21
index 9748a4c8d668..6a2a3e91d59e 100644
@@ -2356,35 +2356,58 @@ static struct sched_domain *build_sched_domain(struct sched_domain_topology_leve
 
 /*
  * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
- * any two given CPUs at this (non-NUMA) topology level.
+ * any two given CPUs on non-NUMA topology levels.
  */
-static bool topology_span_sane(struct sched_domain_topology_level *tl,
-			      const struct cpumask *cpu_map, int cpu)
+static bool topology_span_sane(const struct cpumask *cpu_map)
 {
-	int i = cpu + 1;
+	struct sched_domain_topology_level *tl;
+	struct cpumask *covered, *id_seen;
+	int cpu;
 
-	/* NUMA levels are allowed to overlap */
-	if (tl->flags & SDTL_OVERLAP)
-		return true;
+	lockdep_assert_held(&sched_domains_mutex);
+	covered = sched_domains_tmpmask;
+	id_seen = sched_domains_tmpmask2;
+
+	for_each_sd_topology(tl) {
+
+		/* NUMA levels are allowed to overlap */
+		if (tl->flags & SDTL_OVERLAP)
+			continue;
+
+		cpumask_clear(covered);
+		cpumask_clear(id_seen);
 
-	/*
-	 * Non-NUMA levels cannot partially overlap - they must be either
-	 * completely equal or completely disjoint. Otherwise we can end up
-	 * breaking the sched_group lists - i.e. a later get_group() pass
-	 * breaks the linking done for an earlier span.
-	 */
-	for_each_cpu_from(i, cpu_map) {
 		/*
-		 * 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
+		 * Non-NUMA levels cannot partially overlap - they must be either
+		 * completely equal or completely disjoint. Otherwise we can end up
+		 * breaking the sched_group lists - i.e. a later get_group() pass
+		 * breaks the linking done for an earlier span.
 		 */
-		if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
-		    cpumask_intersects(tl->mask(cpu), tl->mask(i)))
-			return false;
+		for_each_cpu(cpu, cpu_map) {
+			const struct cpumask *tl_cpu_mask = tl->mask(cpu);
+			int id;
+
+			/* lowest bit set in this mask is used as a unique id */
+			id = cpumask_first(tl_cpu_mask);
+
+			/* if this mask doesn't collide with what we've already seen */
+			if (!cpumask_intersects(tl_cpu_mask, covered)) {
+				/* Really odd case when cpu != id, likely not sane */
+				if ((cpu != id) && !cpumask_equal(tl_cpu_mask, tl->mask(id)))
+					return false;
+				if (cpumask_test_and_set_cpu(id, id_seen))
+					return false;

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

Quoted 10 lines
kernel/sched/topology.c +10 -0
+				cpumask_or(covered, tl_cpu_mask, covered);
+			} else if ((!cpumask_test_cpu(id, id_seen)) ||
+				    !cpumask_equal(tl->mask(id), tl_cpu_mask)) {
+				/*
+				 * a collision with covered should have exactly matched
+				 * a previously seen mask with the same id
+				 */
+				return false;
+			}
+		}

Ok so you're speeding it up, but you still get a O(nr_cpu_ids) walk every hotplug when the check itself only needs to be done at most once per possible online CPU combination (~ 2^(nr_cpu_ids)). If all CPUs are kicked to life at boot, then the check only needs to be done once. If you only boot with a subset of present CPUs to speed things up, the check still becomes irrelevant once you've kicked the rest to life.

Quoted 4 lines

I would reiterate my suggestion to get to a state where the check can be entirely short-circuited [1].

[1]: http://lore.kernel.org/r/[email protected]

Bringing forward a bit of that conversation:

Quoted 7 lines
Quoted 4 lines
kernel/sched/topology.c +0 -0
 I tried adding this, surprisingly I saw no effect on the time taken,

perhaps even a small slowdown, when combined with my patch. So at this point I don't intend to add it to v2 of the patch.

Thanks for testing, I assume your cpu_possible_mask reports more CPUs than you have physically plugged in...

That assumption is wrong. I started with all CPUs enabled. Disabled and re-enabled cpus from there. The timings I got were as I stated, no effect, perhaps a small slowdown.

Quoted 7 lines

I guess it would make sense to short-circuit the function when cpu_map is a subset of what we've previously checked, and then re-kick the testing once new CPU(s) are plugged in. Something like the untested below?

Optimisations notwithstanding, IMO we shouldn't be repeating checks if we can avoid it.

Quoted 6 lines

I will attempt to measure it once more. I was surprised at my measured results, but that's why we take them, right?

If I can't measure a difference, though, I am not sure it's appropriate to include the change with this patch, the point of which *is* optimization.

I completed timing tests; test system has 1920 logical CPUS, 2 threads per core, 60 cores per NUMA node, and the script offlined then onlined both threads of half the cores in each node. Four runs each were timed. Times in seconds.

Unpatched kernel:
Offline Online
min 3991.57 3967.22
max 4025.59 4028.66
avg 4013.79 3996.75
stddev 15.28 29.90

With my patch:
Offline Online
min 1987.97 2130.7
max 2032.25 2150.93
avg 2017.43 2140.18
stddev 20.12 10.25

With my patch + Valentin's extra short-circuit patch:
min 2019.58 2137.43
max 2056.89 2223.86
avg 2033.04 2171.91
stddev 16.83 37.73

I'm at a loss as to why adding the short circuit slowed things down, but it's in the noise. If you want a new version of the patch incorporating both changes after viewing these timings, I'm willing to make that change. Let me know how you feel.

Thanks for your time,

--> Steve Wahl

Signature · 1 line
Steve Wahl, Hewlett Packard Enterprise
#6 lore raw
Quoted 191 lines · Steve Wahl
Quoted 158 lines · Steve Wahl
Quoted 130 lines · Valentin Schneider
Quoted 111 lines · Steve Wahl

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 ("id") by the lowest bit set in this mask. Mark that we've seen a mask with this id, 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, identified once again by the lowest bit the current mask has set. It's an error if we haven't seen a mask with that id, or if the current mask doesn't match the one we get by looking up that id.

Move the topology_span_sane() check out of the existing topology level loop, let it do its own looping to match the needs of this algorithm.

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.)

---

Version 2: Adopted suggestion by K Prateek Nayak that removes an array and simplifies the code, and eliminates the erroneous use of num_possible_cpus() that Peter Zijlstra noted.

Version 1 discussion:

    https://lore.kernel.org/all/[email protected]/
 kernel/sched/topology.c | 73 +++++++++++++++++++++++++++--------------
 1 file changed, 48 insertions(+), 25 deletions(-)
kernel/sched/topology.c +35 -21
index 9748a4c8d668..6a2a3e91d59e 100644
@@ -2356,35 +2356,58 @@ static struct sched_domain *build_sched_domain(struct sched_domain_topology_leve
 
 /*
  * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
- * any two given CPUs at this (non-NUMA) topology level.
+ * any two given CPUs on non-NUMA topology levels.
  */
-static bool topology_span_sane(struct sched_domain_topology_level *tl,
-			      const struct cpumask *cpu_map, int cpu)
+static bool topology_span_sane(const struct cpumask *cpu_map)
 {
-	int i = cpu + 1;
+	struct sched_domain_topology_level *tl;
+	struct cpumask *covered, *id_seen;
+	int cpu;
 
-	/* NUMA levels are allowed to overlap */
-	if (tl->flags & SDTL_OVERLAP)
-		return true;
+	lockdep_assert_held(&sched_domains_mutex);
+	covered = sched_domains_tmpmask;
+	id_seen = sched_domains_tmpmask2;
+
+	for_each_sd_topology(tl) {
+
+		/* NUMA levels are allowed to overlap */
+		if (tl->flags & SDTL_OVERLAP)
+			continue;
+
+		cpumask_clear(covered);
+		cpumask_clear(id_seen);
 
-	/*
-	 * Non-NUMA levels cannot partially overlap - they must be either
-	 * completely equal or completely disjoint. Otherwise we can end up
-	 * breaking the sched_group lists - i.e. a later get_group() pass
-	 * breaks the linking done for an earlier span.
-	 */
-	for_each_cpu_from(i, cpu_map) {
                 /*
-		 * 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
+		 * Non-NUMA levels cannot partially overlap - they must be either
+		 * completely equal or completely disjoint. Otherwise we can end up
+		 * breaking the sched_group lists - i.e. a later get_group() pass
+		 * breaks the linking done for an earlier span.
                  */
-		if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
-		    cpumask_intersects(tl->mask(cpu), tl->mask(i)))
-			return false;
+		for_each_cpu(cpu, cpu_map) {
+			const struct cpumask *tl_cpu_mask = tl->mask(cpu);
+			int id;
+
+			/* lowest bit set in this mask is used as a unique id */
+			id = cpumask_first(tl_cpu_mask);
+
+			/* if this mask doesn't collide with what we've already seen */
+			if (!cpumask_intersects(tl_cpu_mask, covered)) {
+				/* Really odd case when cpu != id, likely not sane */
+				if ((cpu != id) && !cpumask_equal(tl_cpu_mask, tl->mask(id)))
+					return false;
+				if (cpumask_test_and_set_cpu(id, id_seen))
+					return false;

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

Quoted 10 lines
kernel/sched/topology.c +10 -0
+				cpumask_or(covered, tl_cpu_mask, covered);
+			} else if ((!cpumask_test_cpu(id, id_seen)) ||
+				    !cpumask_equal(tl->mask(id), tl_cpu_mask)) {
+				/*
+				 * a collision with covered should have exactly matched
+				 * a previously seen mask with the same id
+				 */
+				return false;
+			}
+		}

Ok so you're speeding it up, but you still get a O(nr_cpu_ids) walk every hotplug when the check itself only needs to be done at most once per possible online CPU combination (~ 2^(nr_cpu_ids)). If all CPUs are kicked to life at boot, then the check only needs to be done once. If you only boot with a subset of present CPUs to speed things up, the check still becomes irrelevant once you've kicked the rest to life.

Quoted 4 lines

I would reiterate my suggestion to get to a state where the check can be entirely short-circuited [1].

[1]: http://lore.kernel.org/r/[email protected]

Bringing forward a bit of that conversation:

Quoted 7 lines
Quoted 4 lines
kernel/sched/topology.c +0 -0
 I tried adding this, surprisingly I saw no effect on the time taken,

perhaps even a small slowdown, when combined with my patch. So at this point I don't intend to add it to v2 of the patch.

Thanks for testing, I assume your cpu_possible_mask reports more CPUs than you have physically plugged in...

That assumption is wrong. I started with all CPUs enabled. Disabled and re-enabled cpus from there. The timings I got were as I stated, no effect, perhaps a small slowdown.

Quoted 7 lines

I guess it would make sense to short-circuit the function when cpu_map is a subset of what we've previously checked, and then re-kick the testing once new CPU(s) are plugged in. Something like the untested below?

Optimisations notwithstanding, IMO we shouldn't be repeating checks if we can avoid it.

Quoted 6 lines

I will attempt to measure it once more. I was surprised at my measured results, but that's why we take them, right?

If I can't measure a difference, though, I am not sure it's appropriate to include the change with this patch, the point of which *is* optimization.

I completed timing tests; test system has 1920 logical CPUS, 2 threads per core, 60 cores per NUMA node, and the script offlined then onlined both threads of half the cores in each node. Four runs each were timed. Times in seconds.

Unpatched kernel:
Offline Online
min 3991.57 3967.22
max 4025.59 4028.66
avg 4013.79 3996.75
stddev 15.28 29.90

With my patch:
Offline Online
min 1987.97 2130.7
max 2032.25 2150.93
avg 2017.43 2140.18
stddev 20.12 10.25

With my patch + Valentin's extra short-circuit patch:
min 2019.58 2137.43
max 2056.89 2223.86
avg 2033.04 2171.91
stddev 16.83 37.73

Quoted 5 lines

I'm at a loss as to why adding the short circuit slowed things down, but it's in the noise. If you want a new version of the patch incorporating both changes after viewing these timings, I'm willing to make that change. Let me know how you feel.

Huh, weird. Obviously your patch improves the situation and the short-circuit as proposed doesn't, so I'd say go forth with your patch and I'll poke around to figure out what's going on and submit a hopefully working short-circuit.

author
#7 lore raw
Quoted 198 lines · Valentin Schneider
Quoted 191 lines · Steve Wahl
Quoted 158 lines · Steve Wahl
Quoted 130 lines · Valentin Schneider
Quoted 111 lines · Steve Wahl

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 ("id") by the lowest bit set in this mask. Mark that we've seen a mask with this id, 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, identified once again by the lowest bit the current mask has set. It's an error if we haven't seen a mask with that id, or if the current mask doesn't match the one we get by looking up that id.

Move the topology_span_sane() check out of the existing topology level loop, let it do its own looping to match the needs of this algorithm.

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.)

---

Version 2: Adopted suggestion by K Prateek Nayak that removes an array and simplifies the code, and eliminates the erroneous use of num_possible_cpus() that Peter Zijlstra noted.

Version 1 discussion:

    https://lore.kernel.org/all/[email protected]/
 kernel/sched/topology.c | 73 +++++++++++++++++++++++++++--------------
 1 file changed, 48 insertions(+), 25 deletions(-)
kernel/sched/topology.c +35 -21
index 9748a4c8d668..6a2a3e91d59e 100644
@@ -2356,35 +2356,58 @@ static struct sched_domain *build_sched_domain(struct sched_domain_topology_leve
 
 /*
  * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
- * any two given CPUs at this (non-NUMA) topology level.
+ * any two given CPUs on non-NUMA topology levels.
  */
-static bool topology_span_sane(struct sched_domain_topology_level *tl,
-			      const struct cpumask *cpu_map, int cpu)
+static bool topology_span_sane(const struct cpumask *cpu_map)
 {
-	int i = cpu + 1;
+	struct sched_domain_topology_level *tl;
+	struct cpumask *covered, *id_seen;
+	int cpu;
 
-	/* NUMA levels are allowed to overlap */
-	if (tl->flags & SDTL_OVERLAP)
-		return true;
+	lockdep_assert_held(&sched_domains_mutex);
+	covered = sched_domains_tmpmask;
+	id_seen = sched_domains_tmpmask2;
+
+	for_each_sd_topology(tl) {
+
+		/* NUMA levels are allowed to overlap */
+		if (tl->flags & SDTL_OVERLAP)
+			continue;
+
+		cpumask_clear(covered);
+		cpumask_clear(id_seen);
 
-	/*
-	 * Non-NUMA levels cannot partially overlap - they must be either
-	 * completely equal or completely disjoint. Otherwise we can end up
-	 * breaking the sched_group lists - i.e. a later get_group() pass
-	 * breaks the linking done for an earlier span.
-	 */
-	for_each_cpu_from(i, cpu_map) {
                 /*
-		 * 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
+		 * Non-NUMA levels cannot partially overlap - they must be either
+		 * completely equal or completely disjoint. Otherwise we can end up
+		 * breaking the sched_group lists - i.e. a later get_group() pass
+		 * breaks the linking done for an earlier span.
                  */
-		if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
-		    cpumask_intersects(tl->mask(cpu), tl->mask(i)))
-			return false;
+		for_each_cpu(cpu, cpu_map) {
+			const struct cpumask *tl_cpu_mask = tl->mask(cpu);
+			int id;
+
+			/* lowest bit set in this mask is used as a unique id */
+			id = cpumask_first(tl_cpu_mask);
+
+			/* if this mask doesn't collide with what we've already seen */
+			if (!cpumask_intersects(tl_cpu_mask, covered)) {
+				/* Really odd case when cpu != id, likely not sane */
+				if ((cpu != id) && !cpumask_equal(tl_cpu_mask, tl->mask(id)))
+					return false;
+				if (cpumask_test_and_set_cpu(id, id_seen))
+					return false;

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

Quoted 10 lines
kernel/sched/topology.c +10 -0
+				cpumask_or(covered, tl_cpu_mask, covered);
+			} else if ((!cpumask_test_cpu(id, id_seen)) ||
+				    !cpumask_equal(tl->mask(id), tl_cpu_mask)) {
+				/*
+				 * a collision with covered should have exactly matched
+				 * a previously seen mask with the same id
+				 */
+				return false;
+			}
+		}

Ok so you're speeding it up, but you still get a O(nr_cpu_ids) walk every hotplug when the check itself only needs to be done at most once per possible online CPU combination (~ 2^(nr_cpu_ids)). If all CPUs are kicked to life at boot, then the check only needs to be done once. If you only boot with a subset of present CPUs to speed things up, the check still becomes irrelevant once you've kicked the rest to life.

Quoted 4 lines

I would reiterate my suggestion to get to a state where the check can be entirely short-circuited [1].

[1]: http://lore.kernel.org/r/[email protected]

Bringing forward a bit of that conversation:

Quoted 7 lines
Quoted 4 lines
kernel/sched/topology.c +0 -0
 I tried adding this, surprisingly I saw no effect on the time taken,

perhaps even a small slowdown, when combined with my patch. So at this point I don't intend to add it to v2 of the patch.

Thanks for testing, I assume your cpu_possible_mask reports more CPUs than you have physically plugged in...

That assumption is wrong. I started with all CPUs enabled. Disabled and re-enabled cpus from there. The timings I got were as I stated, no effect, perhaps a small slowdown.

Quoted 7 lines

I guess it would make sense to short-circuit the function when cpu_map is a subset of what we've previously checked, and then re-kick the testing once new CPU(s) are plugged in. Something like the untested below?

Optimisations notwithstanding, IMO we shouldn't be repeating checks if we can avoid it.

Quoted 6 lines

I will attempt to measure it once more. I was surprised at my measured results, but that's why we take them, right?

If I can't measure a difference, though, I am not sure it's appropriate to include the change with this patch, the point of which *is* optimization.

I completed timing tests; test system has 1920 logical CPUS, 2 threads per core, 60 cores per NUMA node, and the script offlined then onlined both threads of half the cores in each node. Four runs each were timed. Times in seconds.

Unpatched kernel:
Offline Online
min 3991.57 3967.22
max 4025.59 4028.66
avg 4013.79 3996.75
stddev 15.28 29.90

With my patch:
Offline Online
min 1987.97 2130.7
max 2032.25 2150.93
avg 2017.43 2140.18
stddev 20.12 10.25

With my patch + Valentin's extra short-circuit patch:
min 2019.58 2137.43
max 2056.89 2223.86
avg 2033.04 2171.91
stddev 16.83 37.73

Quoted 5 lines

I'm at a loss as to why adding the short circuit slowed things down, but it's in the noise. If you want a new version of the patch incorporating both changes after viewing these timings, I'm willing to make that change. Let me know how you feel.

Quoted 4 lines

Huh, weird. Obviously your patch improves the situation and the short-circuit as proposed doesn't, so I'd say go forth with your patch and I'll poke around to figure out what's going on and submit a hopefully working short-circuit.

I think the short-circuit is actually working. In the previous email iteration on this, after I did the timings saying it was a tad slower, I added some printk's to make sure the short circuit was actually happening, and it appeared to be true. And this time around, I came up with a version of your patch that functions without mine; it's not as pretty before combining all the topology levels into one call to topology_span_sane, but the result of the bypass is a similar time saving.

I am thinking that the memory touched by the full topology_span_sane run may be pre-loading the cache for some of the subsequent operations. That's the best I can come up with, at least for the moment.

--> Steve

Signature · 1 line
Steve Wahl, Hewlett Packard Enterprise
#8 lore raw
In reply to Steve Wahl
Quoted 150 lines · Steve Wahl

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 ("id") by the lowest bit set in this mask. Mark that we've seen a mask with this id, 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, identified once again by the lowest bit the current mask has set. It's an error if we haven't seen a mask with that id, or if the current mask doesn't match the one we get by looking up that id.

Move the topology_span_sane() check out of the existing topology level loop, let it do its own looping to match the needs of this algorithm.

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.)

---

Version 2: Adopted suggestion by K Prateek Nayak that removes an array and simplifies the code, and eliminates the erroneous use of num_possible_cpus() that Peter Zijlstra noted.

Version 1 discussion:

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

 kernel/sched/topology.c | 73 +++++++++++++++++++++++++++--------------
 1 file changed, 48 insertions(+), 25 deletions(-)
kernel/sched/topology.c +0 -0
index 9748a4c8d668..6a2a3e91d59e 100644
@@ -2356,35 +2356,58 @@ static struct sched_domain
 *build_sched_domain(struct sched_domain_topology_leve
 
 /*
  * Ensure topology masks are sane, i.e. there are no conflicts 

(overlaps) for
- * any two given CPUs at this (non-NUMA) topology level.
+ * any two given CPUs on non-NUMA topology levels. */

kernel/sched/topology.c +44 -24
-static bool topology_span_sane(struct sched_domain_topology_level *tl,
-			      const struct cpumask *cpu_map, int cpu)
+static bool topology_span_sane(const struct cpumask *cpu_map)
 {
-	int i = cpu + 1;
+	struct sched_domain_topology_level *tl;
+	struct cpumask *covered, *id_seen;
+	int cpu;
 
-	/* NUMA levels are allowed to overlap */
-	if (tl->flags & SDTL_OVERLAP)
-		return true;
+	lockdep_assert_held(&sched_domains_mutex);
+	covered = sched_domains_tmpmask;
+	id_seen = sched_domains_tmpmask2;
+
+	for_each_sd_topology(tl) {
+
+		/* NUMA levels are allowed to overlap */
+		if (tl->flags & SDTL_OVERLAP)
+			continue;
+
+		cpumask_clear(covered);
+		cpumask_clear(id_seen);
 
-	/*
-	 * Non-NUMA levels cannot partially overlap - they must be either
-	 * completely equal or completely disjoint. Otherwise we can end up
-	 * breaking the sched_group lists - i.e. a later get_group() pass
-	 * breaks the linking done for an earlier span.
-	 */
-	for_each_cpu_from(i, cpu_map) {
 		/*
-		 * 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
+		 * Non-NUMA levels cannot partially overlap - they must be either
+		 * completely equal or completely disjoint. Otherwise we can end up
+		 * breaking the sched_group lists - i.e. a later get_group() pass
+		 * breaks the linking done for an earlier span.
 		 */
-		if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
-		    cpumask_intersects(tl->mask(cpu), tl->mask(i)))
-			return false;
+		for_each_cpu(cpu, cpu_map) {
+			const struct cpumask *tl_cpu_mask = tl->mask(cpu);
+			int id;
+
+			/* lowest bit set in this mask is used as a unique id */
+			id = cpumask_first(tl_cpu_mask);
+
+			/* if this mask doesn't collide with what we've already seen */
+			if (!cpumask_intersects(tl_cpu_mask, covered)) {
+				/* Really odd case when cpu != id, likely not sane */
+				if ((cpu != id) && !cpumask_equal(tl_cpu_mask, tl->mask(id)))
+					return false;
+				if (cpumask_test_and_set_cpu(id, id_seen))
+					return false;
+				cpumask_or(covered, tl_cpu_mask, covered);
+			} else if ((!cpumask_test_cpu(id, id_seen)) ||
+				    !cpumask_equal(tl->mask(id), tl_cpu_mask)) {
+				/*
+				 * a collision with covered should have exactly matched
+				 * a previously seen mask with the same id
+				 */
+				return false;
+			}
+		}
 	}
-
 	return true;
 }
@@ -2417,9 +2440,6 @@ build_sched_domains(const struct cpumask
 *cpu_map, struct sched_domain_attr *att
 		sd = NULL;
 		for_each_sd_topology(tl) {
 
-			if (WARN_ON(!topology_span_sane(tl, cpu_map, i)))
-				goto error;
-
 			sd = build_sched_domain(tl, cpu_map, attr, sd, i);
 
kernel/sched/topology.c +3 -0
 			has_asym |= sd->flags & SD_ASYM_CPUCAPACITY;
@@ -2433,6 +2453,9 @@ build_sched_domains(const struct cpumask
 *cpu_map, struct sched_domain_attr *att
 		}
 	}
 
+	if (WARN_ON(!topology_span_sane(cpu_map)))
+		goto error;
+
 	/* Build the groups for the domains */
 	for_each_cpu(i, cpu_map) {
Quoted 1 line

for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {

Hello Steve,

I have verified the above patch on PowerPC, and here are my findings:
Below are the 5 iteration data for “time ppc64_cpu --smt=off/4” command(min, max, Average, and Std Dev).

——lscpu—-
CPU(s):                   360
  On-line CPU(s) list:    0-359

Without Patch:
————uname -a————
6.12.0+

Metric        SMT Off (s)         SMT 4 (s)

Min 71.123 37.629

Max 75.031 40.785

Average 73.175 39.992

Std Dev 1.619 1.191

With patch:
————uname -a————
6.12.0+

Metric        SMT Off (s)         SMT 4 (s)

Min 63.638 37.114

Max 73.707 40.773

Average 70.106 38.641

Std Dev 3.443 1.472

The logs indicate minimal improvement in the SMT 4 state with the patch. However, a slight improvement is observed in the SMT OFF state with the patch, as reflected in all three metrics: Min, Max, and Average.

SMT 4 State:
• Across all metrics (Min, Max, and Average), there is negligible
improvement with the patch.
• The differences are within the margin of measurement variability, as
reflected in the similar Std Dev values (~1.1–1.4). SMT Off State:
• A noticeable improvement is observed in the Min, Max, and Average
values with the patch:

	◦	Min: Improved by ~10.5% (from 71.123 to 63.638 seconds).
	◦	Max: Improved by ~1.8% (from 75.031 to 73.707 seconds).
	◦	Average: Improved by ~4.2% (from 73.175 to 70.106 seconds).
	•	The Std Dev increased significantly (from 1.619 to 3.443).

Thanks,
Samir

Tested-by on v2 1/1
#9 lore raw
In reply to Steve Wahl

From: Steve Wahl <[email protected]> Sent: Thursday, October 31, 2024 1:05 PM

Quoted 148 lines

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 ("id") by the lowest bit set in this mask. Mark that we've seen a mask with this id, 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, identified once again by the lowest bit the current mask has set. It's an error if we haven't seen a mask with that id, or if the current mask doesn't match the one we get by looking up that id.

Move the topology_span_sane() check out of the existing topology level loop, let it do its own looping to match the needs of this algorithm.

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.)

---

Version 2: Adopted suggestion by K Prateek Nayak that removes an array and simplifies the code, and eliminates the erroneous use of num_possible_cpus() that Peter Zijlstra noted.

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

 kernel/sched/topology.c | 73 +++++++++++++++++++++++++++--------------
 1 file changed, 48 insertions(+), 25 deletions(-)
kernel/sched/topology.c +48 -26
index 9748a4c8d668..6a2a3e91d59e 100644
@@ -2356,35 +2356,58 @@ static struct sched_domain *build_sched_domain(struct sched_domain_topology_leve
 
 /*
  * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
- * any two given CPUs at this (non-NUMA) topology level.
+ * any two given CPUs on non-NUMA topology levels.
  */
-static bool topology_span_sane(struct sched_domain_topology_level *tl,
-			      const struct cpumask *cpu_map, int cpu)
+static bool topology_span_sane(const struct cpumask *cpu_map)
 {
-	int i = cpu + 1;
+	struct sched_domain_topology_level *tl;
+	struct cpumask *covered, *id_seen;
+	int cpu;
 
-	/* NUMA levels are allowed to overlap */
-	if (tl->flags & SDTL_OVERLAP)
-		return true;
+	lockdep_assert_held(&sched_domains_mutex);
+	covered = sched_domains_tmpmask;
+	id_seen = sched_domains_tmpmask2;
+
+	for_each_sd_topology(tl) {
+
+		/* NUMA levels are allowed to overlap */
+		if (tl->flags & SDTL_OVERLAP)
+			continue;
+
+		cpumask_clear(covered);
+		cpumask_clear(id_seen);
 
-	/*
-	 * Non-NUMA levels cannot partially overlap - they must be either
-	 * completely equal or completely disjoint. Otherwise we can end up
-	 * breaking the sched_group lists - i.e. a later get_group() pass
-	 * breaks the linking done for an earlier span.
-	 */
-	for_each_cpu_from(i, cpu_map) {
 		/*
-		 * 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
+		 * Non-NUMA levels cannot partially overlap - they must be either
+		 * completely equal or completely disjoint. Otherwise we can end up
+		 * breaking the sched_group lists - i.e. a later get_group() pass
+		 * breaks the linking done for an earlier span.
 		 */
-		if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
-		    cpumask_intersects(tl->mask(cpu), tl->mask(i)))
-			return false;
+		for_each_cpu(cpu, cpu_map) {
+			const struct cpumask *tl_cpu_mask = tl->mask(cpu);
+			int id;
+
+			/* lowest bit set in this mask is used as a unique id */
+			id = cpumask_first(tl_cpu_mask);
+
+			/* if this mask doesn't collide with what we've already seen */
+			if (!cpumask_intersects(tl_cpu_mask, covered)) {
+				/* Really odd case when cpu != id, likely not sane */
+				if ((cpu != id) && !cpumask_equal(tl_cpu_mask, tl->mask(id)))
+					return false;
+				if (cpumask_test_and_set_cpu(id, id_seen))
+					return false;
+				cpumask_or(covered, tl_cpu_mask, covered);
+			} else if ((!cpumask_test_cpu(id, id_seen)) ||
+				    !cpumask_equal(tl->mask(id), tl_cpu_mask)) {
+				/*
+				 * a collision with covered should have exactly matched
+				 * a previously seen mask with the same id
+				 */
+				return false;
+			}
+		}
 	}
-
 	return true;
 }
 
@@ -2417,9 +2440,6 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att
 		sd = NULL;
 		for_each_sd_topology(tl) {
 
-			if (WARN_ON(!topology_span_sane(tl, cpu_map, i)))
-				goto error;
-
 			sd = build_sched_domain(tl, cpu_map, attr, sd, i);
 
 			has_asym |= sd->flags & SD_ASYM_CPUCAPACITY;
@@ -2433,6 +2453,9 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att
 		}
 	}
 
+	if (WARN_ON(!topology_span_sane(cpu_map)))
+		goto error;
+
 	/* Build the groups for the domains */
 	for_each_cpu(i, cpu_map) {
 		for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
--
Quoted 2 lines

2.26.2

FWIW, I tried this patch against linux-next-20241107 in an Azure public cloud VM (x86/x64) with 832 vCPUs (8 sockets * 52 cores/node * 2 threads/core).

In a simple test taking a single CPU offline and back online, the elapsed time is about 0.61 seconds without the patch. With the patch, this is reduced to 0.29 seconds. These times are stable across multiple iterations, and the improvement is consistent with what you reported.

My test is pretty limited. I mainly wanted to confirm that nothing unexpected happens in a VM on the Hyper-V hypervisor. So,

Tested-by on v2 1/1
#10 lore raw
In reply to Steve Wahl
Quoted 47 lines · Steve Wahl

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 ("id") by the lowest bit set in this mask. Mark that we've seen a mask with this id, 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, identified once again by the lowest bit the current mask has set. It's an error if we haven't seen a mask with that id, or if the current mask doesn't match the one we get by looking up that id.

Move the topology_span_sane() check out of the existing topology level loop, let it do its own looping to match the needs of this algorithm.

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.)

---
Version 2: Adopted suggestion by K Prateek Nayak that removes an array and simplifies the code, and eliminates the erroneous use of num_possible_cpus() that Peter Zijlstra noted.

Version 1 discussion:

    https://lore.kernel.org/all/[email protected]/
 kernel/sched/topology.c | 73 +++++++++++++++++++++++++++--------------
 1 file changed, 48 insertions(+), 25 deletions(-)
kernel/sched/topology.c +0 -0
index 9748a4c8d668..6a2a3e91d59e 100644

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

Quoted 7 lines
kernel/sched/topology.c +1 -2
@@ -2356,35 +2356,58 @@ static struct sched_domain *build_sched_domain(struct sched_domain_topology_leve
 
 /*
  * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
- * any two given CPUs at this (non-NUMA) topology level.
+ * any two given CPUs on non-NUMA topology levels.
  */
-static bool topology_span_sane(struct sched_domain_topology_level *tl,

<snip>

Subject: RE: [PATCH v2] sched/topology: improve topology_span_sane speed

Apologies for the delayed response, finding such machines for testing is challenging.

I finally managed to test this patch on a large VM with 1792 vCPUs and 28 NUMA nodes. The CPUs in the test reported BogoMIPS of 3800. For this test, I measured the time taken by the build_sched_domains function during the boot-up.

Here are the results:

Without this patch, the build_sched_domains function took approximately 2.33 seconds on the above system. With this patch applied, the time reduced to 1.14 seconds, resulting in a savings of around 1.2 seconds. I understand that systems with less powerful CPUs may see even greater improvements, but I do not currently have access to such hardware. Therefore, this testing should be considered in relative terms.

I tested this patch purely for performance evaluation. If suitable, please feel free to add:

Are there any remaining concerns with this patch which we can address ?

- Saurabh

#11 lore raw
In reply to Steve Wahl

Hi,

Sorry it took me this long to get to this.

Quoted 94 lines · Steve Wahl

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 ("id") by the lowest bit set in this mask. Mark that we've seen a mask with this id, 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, identified once again by the lowest bit the current mask has set. It's an error if we haven't seen a mask with that id, or if the current mask doesn't match the one we get by looking up that id.

Move the topology_span_sane() check out of the existing topology level loop, let it do its own looping to match the needs of this algorithm.

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.)

---

Version 2: Adopted suggestion by K Prateek Nayak that removes an array and simplifies the code, and eliminates the erroneous use of num_possible_cpus() that Peter Zijlstra noted.

Version 1 discussion:

    https://lore.kernel.org/all/[email protected]/
 kernel/sched/topology.c | 73 +++++++++++++++++++++++++++--------------
 1 file changed, 48 insertions(+), 25 deletions(-)
kernel/sched/topology.c +21 -18
index 9748a4c8d668..6a2a3e91d59e 100644
@@ -2356,35 +2356,58 @@ static struct sched_domain *build_sched_domain(struct sched_domain_topology_leve
 
 /*
  * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
- * any two given CPUs at this (non-NUMA) topology level.
+ * any two given CPUs on non-NUMA topology levels.
  */
-static bool topology_span_sane(struct sched_domain_topology_level *tl,
-			      const struct cpumask *cpu_map, int cpu)
+static bool topology_span_sane(const struct cpumask *cpu_map)
 {
-	int i = cpu + 1;
+	struct sched_domain_topology_level *tl;
+	struct cpumask *covered, *id_seen;
+	int cpu;
 
-	/* NUMA levels are allowed to overlap */
-	if (tl->flags & SDTL_OVERLAP)
-		return true;
+	lockdep_assert_held(&sched_domains_mutex);
+	covered = sched_domains_tmpmask;
+	id_seen = sched_domains_tmpmask2;
+
+	for_each_sd_topology(tl) {
+
+		/* NUMA levels are allowed to overlap */
+		if (tl->flags & SDTL_OVERLAP)
+			continue;
+
+		cpumask_clear(covered);
+		cpumask_clear(id_seen);
 
-	/*
-	 * Non-NUMA levels cannot partially overlap - they must be either
-	 * completely equal or completely disjoint. Otherwise we can end up
-	 * breaking the sched_group lists - i.e. a later get_group() pass
-	 * breaks the linking done for an earlier span.
-	 */
-	for_each_cpu_from(i, cpu_map) {
 		/*
-		 * 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
+		 * Non-NUMA levels cannot partially overlap - they must be either
+		 * completely equal or completely disjoint. Otherwise we can end up
+		 * breaking the sched_group lists - i.e. a later get_group() pass
+		 * breaks the linking done for an earlier span.
 		 */

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

Quoted 10 lines
kernel/sched/topology.c +7 -3
-		if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
-		    cpumask_intersects(tl->mask(cpu), tl->mask(i)))
-			return false;
+		for_each_cpu(cpu, cpu_map) {
+			const struct cpumask *tl_cpu_mask = tl->mask(cpu);
+			int id;
+
+			/* lowest bit set in this mask is used as a unique id */
+			id = cpumask_first(tl_cpu_mask);
+

Using the first CPU like we do in get_group() is clever; if we've seen the first CPU then the spans must exactly match; else it must be a completely new span.

Quoted 6 lines
kernel/sched/topology.c +6 -0
+			/* if this mask doesn't collide with what we've already seen */
+			if (!cpumask_intersects(tl_cpu_mask, covered)) {
+				/* Really odd case when cpu != id, likely not sane */
+				if ((cpu != id) && !cpumask_equal(tl_cpu_mask, tl->mask(id)))
+					return false;
+				if (cpumask_test_and_set_cpu(id, id_seen))

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

Quoted 10 lines
kernel/sched/topology.c +10 -0
+					return false;
+				cpumask_or(covered, tl_cpu_mask, covered);
+			} else if ((!cpumask_test_cpu(id, id_seen)) ||
+				    !cpumask_equal(tl->mask(id), tl_cpu_mask)) {
+				/*
+				 * a collision with covered should have exactly matched
+				 * a previously seen mask with the same id
+				 */
+				return false;
+			}

Per the previous comment, if you hinge the condition on @id itself, couldn't you "simplify" it as below?

	if (cpumask_test_cpu(id, id_seen)) {
		/* First CPU has already been seen, ensure identical spans */
		if (!cpumask_equal(tl->mask(id), tl_cpu_mask))
			return false;
	} else {
		/* First CPU hasn't been seen before, ensure it's a completely new span */
		if (cpumask_intersects(tl_cpu_mask, covered))
			return false;

		cpumask_or(covered, covered, tl_cpu_mask);
		cpumask_set_cpu(id, id_seen);
	}
author
#12 lore raw
Quoted 5 lines · Valentin Schneider

Hi,

Sorry it took me this long to get to this.

...

Quoted 15 lines

Per the previous comment, if you hinge the condition on @id itself, couldn't you "simplify" it as below?

	if (cpumask_test_cpu(id, id_seen)) {
		/* First CPU has already been seen, ensure identical spans */
		if (!cpumask_equal(tl->mask(id), tl_cpu_mask))
			return false;
	} else {
		/* First CPU hasn't been seen before, ensure it's a completely new span */
		if (cpumask_intersects(tl_cpu_mask, covered))
			return false;

		cpumask_or(covered, covered, tl_cpu_mask);
		cpumask_set_cpu(id, id_seen);
	}

After further evaluation, I wish to re-work this patch once again. I ask that nobody consider merging this patch until I re-work it and post v3.

Thanks,

--> Steve Wahl

Signature · 1 line
Steve Wahl, Hewlett Packard Enterprise