Skip to contentSkip to conversation

sched/topology: Check average distances to remote packages

PATCH In review Kyle Meyer 4 participants 19 messages Feb 5 – Feb 26 x86

lore
Share & tools
lore

Kyle Meyer

author
#0 lore raw

PATCH v2 1/1 sched/topology: Check average distances to remote packages

Granite Rapids (GNR) and Clearwater Forest (CWF) average distances to remote packages to fix scheduler domains, see [1] for more information.

A warning and backtrace are printed when sub-NUMA clustering (SNC) is enabled and there are more than 2 packages because the average distances to remote packages could be different, skewing the single average remote distance.

This is unnecessary when the average distances to remote packages are the same.

Support single average remote distance on systems with more than 2 packages, preventing unnecessary warnings and backtraces, by checking if average distances to remote packages are the same.

[1] commit 4d6dd05d07d0 ("sched/topology: Fix sched domain build error for GNR, CWF in SNC-3 mode").

---

The warning and backtrace were noticed on a 16 socket GNR system with SNC-2 enabled.

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

v1 -> v2:
* Initialize pkg_total_distance and pkg_nr_remote to NULL, as suggested by Tim.

---
 arch/x86/kernel/smpboot.c | 69 ++++++++++++++++++++++++++++-----------
 1 file changed, 50 insertions(+), 19 deletions(-)
arch/x86/kernel/smpboot.c +50 -19
index 5cd6950ab672..dc8f15bd2e19 100644
@@ -518,27 +518,69 @@ static int avg_remote_numa_distance(void)
 {
 	int i, j;
 	int distance, nr_remote, total_distance;
+	int max_pkgs = topology_max_packages();
+	int cpu, pkg, pkg_avg_distance;
+	int *pkg_total_distance = NULL, *pkg_nr_remote = NULL;
 
 	if (sched_avg_remote_distance > 0)
 		return sched_avg_remote_distance;
 
+	sched_avg_remote_distance = REMOTE_DISTANCE;
+
 	nr_remote = 0;
 	total_distance = 0;
+
+	pkg_total_distance = kcalloc(max_pkgs, sizeof(int), GFP_KERNEL);
+	if (!pkg_total_distance)
+		goto cleanup;
+
+	pkg_nr_remote = kcalloc(max_pkgs, sizeof(int), GFP_KERNEL);
+	if (!pkg_nr_remote)
+		goto cleanup;
+
 	for_each_node_state(i, N_CPU) {
 		for_each_node_state(j, N_CPU) {
 			distance = node_distance(i, j);
 
-			if (distance >= REMOTE_DISTANCE) {
-				nr_remote++;
-				total_distance += distance;
-			}
+			if (distance < REMOTE_DISTANCE)
+				continue;
+
+			nr_remote++;
+			total_distance += distance;
+
+			cpu = cpumask_first(cpumask_of_node(j));
+			if (cpu >= nr_cpu_ids)
+				continue;
+
+			pkg = topology_physical_package_id(cpu);
+			pkg_total_distance[pkg] += distance;
+			pkg_nr_remote[pkg]++;
 		}
 	}
-	if (nr_remote)
-		sched_avg_remote_distance = total_distance / nr_remote;
-	else
-		sched_avg_remote_distance = REMOTE_DISTANCE;
 
+	if (!nr_remote)
+		goto cleanup;
+
+	sched_avg_remote_distance = total_distance / nr_remote;
+
+	/*
+	 * Single average remote distance won't be appropriate if different
+	 * packages have different distances to remote packages.
+	 */
+	for (i = 0; i < max_pkgs; i++) {
+		if (!pkg_nr_remote[i])
+			continue;
+
+		pkg_avg_distance = pkg_total_distance[i] / pkg_nr_remote[i];
+
+		pr_debug("sched: Avg. distance to remote package %d: %d\n", i, pkg_avg_distance);
+
+		if (pkg_avg_distance != sched_avg_remote_distance)
+			WARN_ONCE(1, "sched: Avg. distances to remote packages are different\n");
+	}
+cleanup:
+	kfree(pkg_nr_remote);
+	kfree(pkg_total_distance);
 	return sched_avg_remote_distance;
 }
 
@@ -564,18 +606,7 @@ int arch_sched_node_distance(int from, int to)
 		 * in the remote package in the same sched group.
 		 * Simplify NUMA domains and avoid extra NUMA levels including
 		 * different remote NUMA nodes and local nodes.
-		 *
-		 * GNR and CWF don't expect systems with more than 2 packages
-		 * and more than 2 hops between packages. Single average remote
-		 * distance won't be appropriate if there are more than 2
-		 * packages as average distance to different remote packages
-		 * could be different.
 		 */
-		WARN_ONCE(topology_max_packages() > 2,
-			  "sched: Expect only up to 2 packages for GNR or CWF, "
-			  "but saw %d packages when building sched domains.",
-			  topology_max_packages());
-
 		d = avg_remote_numa_distance();
 	}
 	return d;
Signature · 1 line
2.52.0
#1 lore raw
Quoted 7 lines · Kyle Meyer

Granite Rapids (GNR) and Clearwater Forest (CWF) average distances to remote packages to fix scheduler domains, see [1] for more information.

A warning and backtrace are printed when sub-NUMA clustering (SNC) is enabled and there are more than 2 packages because the average distances to remote packages could be different, skewing the single average remote distance.

But earlier Tim said these systems will not have more than 2 packages. So what's what?

So what do these new systems look like?

Quoted 18 lines

This is unnecessary when the average distances to remote packages are the same.

Support single average remote distance on systems with more than 2 packages, preventing unnecessary warnings and backtraces, by checking if average distances to remote packages are the same.

---
 arch/x86/kernel/smpboot.c | 69 ++++++++++++++++++++++++++++-----------
 1 file changed, 50 insertions(+), 19 deletions(-)
arch/x86/kernel/smpboot.c +0 -0
index 5cd6950ab672..dc8f15bd2e19 100644

arch/x86/kernel/smpboot.c static int avg_remote_numa_distance(void) · line 523

Quoted 6 lines
arch/x86/kernel/smpboot.c +3 -0
@@ -518,27 +518,69 @@ static int avg_remote_numa_distance(void)
 {
 	int i, j;
 	int distance, nr_remote, total_distance;
+	int max_pkgs = topology_max_packages();
+	int cpu, pkg, pkg_avg_distance;
+	int *pkg_total_distance = NULL, *pkg_nr_remote = NULL;

Can you make that the normal reverse xmas thing?

Quoted 27 lines
arch/x86/kernel/smpboot.c +14 -4
 	if (sched_avg_remote_distance > 0)
 		return sched_avg_remote_distance;
 
+	sched_avg_remote_distance = REMOTE_DISTANCE;
+
 	nr_remote = 0;
 	total_distance = 0;
+
+	pkg_total_distance = kcalloc(max_pkgs, sizeof(int), GFP_KERNEL);
+	if (!pkg_total_distance)
+		goto cleanup;
+
+	pkg_nr_remote = kcalloc(max_pkgs, sizeof(int), GFP_KERNEL);
+	if (!pkg_nr_remote)
+		goto cleanup;
+
 	for_each_node_state(i, N_CPU) {
 		for_each_node_state(j, N_CPU) {
 			distance = node_distance(i, j);
 
-			if (distance >= REMOTE_DISTANCE) {
-				nr_remote++;
-				total_distance += distance;
-			}
+			if (distance < REMOTE_DISTANCE)
+				continue;
+

arch/x86/kernel/smpboot.c static int avg_remote_numa_distance(void) · line 557

Quoted 10 lines
arch/x86/kernel/smpboot.c +10 -0
+			nr_remote++;
+			total_distance += distance;
+
+			cpu = cpumask_first(cpumask_of_node(j));
+			if (cpu >= nr_cpu_ids)
+				continue;
+
+			pkg = topology_physical_package_id(cpu);
+			pkg_total_distance[pkg] += distance;
+			pkg_nr_remote[pkg]++;

This is broken, physical_package_id is not guaranteed to be dense.

Quoted 17 lines
arch/x86/kernel/smpboot.c +10 -4
 		}
 	}
-	if (nr_remote)
-		sched_avg_remote_distance = total_distance / nr_remote;
-	else
-		sched_avg_remote_distance = REMOTE_DISTANCE;
 
+	if (!nr_remote)
+		goto cleanup;
+
+	sched_avg_remote_distance = total_distance / nr_remote;
+
+	/*
+	 * Single average remote distance won't be appropriate if different
+	 * packages have different distances to remote packages.
+	 */
+	for (i = 0; i < max_pkgs; i++) {

arch/x86/kernel/smpboot.c static int avg_remote_numa_distance(void) · line 580

Quoted 10 lines
arch/x86/kernel/smpboot.c +10 -0
+		if (!pkg_nr_remote[i])
+			continue;
+
+		pkg_avg_distance = pkg_total_distance[i] / pkg_nr_remote[i];
+
+		pr_debug("sched: Avg. distance to remote package %d: %d\n", i, pkg_avg_distance);
+
+		if (pkg_avg_distance != sched_avg_remote_distance)
+			WARN_ONCE(1, "sched: Avg. distances to remote packages are different\n");
+	}

This is pretty yuck.

Also, what's with the pr_debug() stuff?

Anyway, that function was fairly magical, and now it is nearly impenetrable. If we want this, it needs comments. Definitely more comments, with nice pictures on.

Quoted 28 lines
arch/x86/kernel/smpboot.c +3 -11
+cleanup:
+	kfree(pkg_nr_remote);
+	kfree(pkg_total_distance);
 	return sched_avg_remote_distance;
 }
 
@@ -564,18 +606,7 @@ int arch_sched_node_distance(int from, int to)
 		 * in the remote package in the same sched group.
 		 * Simplify NUMA domains and avoid extra NUMA levels including
 		 * different remote NUMA nodes and local nodes.
-		 *
-		 * GNR and CWF don't expect systems with more than 2 packages
-		 * and more than 2 hops between packages. Single average remote
-		 * distance won't be appropriate if there are more than 2
-		 * packages as average distance to different remote packages
-		 * could be different.
 		 */
-		WARN_ONCE(topology_max_packages() > 2,
-			  "sched: Expect only up to 2 packages for GNR or CWF, "
-			  "but saw %d packages when building sched domains.",
-			  topology_max_packages());
-
 		d = avg_remote_numa_distance();
 	}
 	return d;
Signature · 1 line
2.52.0
author
#2 lore raw
Quoted 11 lines · Peter Zijlstra
Quoted 7 lines · Kyle Meyer

Granite Rapids (GNR) and Clearwater Forest (CWF) average distances to remote packages to fix scheduler domains, see [1] for more information.

A warning and backtrace are printed when sub-NUMA clustering (SNC) is enabled and there are more than 2 packages because the average distances to remote packages could be different, skewing the single average remote distance.

But earlier Tim said these systems will not have more than 2 packages. So what's what?

We have Intel customer reference boards with 2, 4, and 8 sockets.

Quoted 1 line

So what do these new systems look like?

Here's an 8 socket (2 chassis) HPE system with SNC enabled:

node   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
  0:  10  12  16  16  16  16  18  18  40  40  40  40  40  40  40  40
  1:  12  10  16  16  16  16  18  18  40  40  40  40  40  40  40  40
  2:  16  16  10  12  18  18  16  16  40  40  40  40  40  40  40  40
  3:  16  16  12  10  18  18  16  16  40  40  40  40  40  40  40  40
  4:  16  16  18  18  10  12  16  16  40  40  40  40  40  40  40  40
  5:  16  16  18  18  12  10  16  16  40  40  40  40  40  40  40  40
  6:  18  18  16  16  16  16  10  12  40  40  40  40  40  40  40  40
  7:  18  18  16  16  16  16  12  10  40  40  40  40  40  40  40  40
  8:  40  40  40  40  40  40  40  40  10  12  16  16  16  16  18  18
  9:  40  40  40  40  40  40  40  40  12  10  16  16  16  16  18  18
 10:  40  40  40  40  40  40  40  40  16  16  10  12  18  18  16  16
 11:  40  40  40  40  40  40  40  40  16  16  12  10  18  18  16  16
 12:  40  40  40  40  40  40  40  40  16  16  18  18  10  12  16  16
 13:  40  40  40  40  40  40  40  40  16  16  18  18  12  10  16  16
 14:  40  40  40  40  40  40  40  40  18  18  16  16  16  16  10  12
 15:  40  40  40  40  40  40  40  40  18  18  16  16  16  16  12  10

10 = Same chassis and socket
12 = Same chassis and socket (SNC)
16 = Same chassis and adjacent socket
18 = Same chassis and non-adjacent socket
40 = Different chassis

Each processor connects to an ASIC (XNC) that acts as a multiplexer, extending the UPI interconnect across the entire system.

We don't experience the scheduler domain issue reported by Tim because our SLIT provides symmetric distances to remote NUMA nodes, but we trigger the WARN_ONCE because we exceed 2 packages.

Quoted 13 lines
Quoted 13 lines

This is unnecessary when the average distances to remote packages are the same.

Support single average remote distance on systems with more than 2 packages, preventing unnecessary warnings and backtraces, by checking if average distances to remote packages are the same.

---
 arch/x86/kernel/smpboot.c | 69 ++++++++++++++++++++++++++++-----------
 1 file changed, 50 insertions(+), 19 deletions(-)
Quoted 13 lines

arch/x86/kernel/smpboot.c static int avg_remote_numa_distance(void) · line 523

Quoted 11 lines
arch/x86/kernel/smpboot.c +3 -0
index 5cd6950ab672..dc8f15bd2e19 100644
@@ -518,27 +518,69 @@ static int avg_remote_numa_distance(void)
 {
 	int i, j;
 	int distance, nr_remote, total_distance;
+	int max_pkgs = topology_max_packages();
+	int cpu, pkg, pkg_avg_distance;
+	int *pkg_total_distance = NULL, *pkg_nr_remote = NULL;

Can you make that the normal reverse xmas thing?

Yes.

Quoted 31 lines
Quoted 31 lines
arch/x86/kernel/smpboot.c +18 -4
 	if (sched_avg_remote_distance > 0)
 		return sched_avg_remote_distance;
 
+	sched_avg_remote_distance = REMOTE_DISTANCE;
+
 	nr_remote = 0;
 	total_distance = 0;
+
+	pkg_total_distance = kcalloc(max_pkgs, sizeof(int), GFP_KERNEL);
+	if (!pkg_total_distance)
+		goto cleanup;
+
+	pkg_nr_remote = kcalloc(max_pkgs, sizeof(int), GFP_KERNEL);
+	if (!pkg_nr_remote)
+		goto cleanup;
+
 	for_each_node_state(i, N_CPU) {
 		for_each_node_state(j, N_CPU) {
 			distance = node_distance(i, j);
 
-			if (distance >= REMOTE_DISTANCE) {
-				nr_remote++;
-				total_distance += distance;
-			}
+			if (distance < REMOTE_DISTANCE)
+				continue;
+
+			nr_remote++;
+			total_distance += distance;
+
+			cpu = cpumask_first(cpumask_of_node(j));
Quoted 8 lines

arch/x86/kernel/smpboot.c static int avg_remote_numa_distance(void) · line 557

Quoted 6 lines
arch/x86/kernel/smpboot.c +6 -0
+			if (cpu >= nr_cpu_ids)
+				continue;
+
+			pkg = topology_physical_package_id(cpu);
+			pkg_total_distance[pkg] += distance;
+			pkg_nr_remote[pkg]++;

This is broken, physical_package_id is not guaranteed to be dense.

Thank you, I'll fix this.

Quoted 30 lines
Quoted 17 lines
arch/x86/kernel/smpboot.c +10 -4
 		}
 	}
-	if (nr_remote)
-		sched_avg_remote_distance = total_distance / nr_remote;
-	else
-		sched_avg_remote_distance = REMOTE_DISTANCE;
 
+	if (!nr_remote)
+		goto cleanup;
+
+	sched_avg_remote_distance = total_distance / nr_remote;
+
+	/*
+	 * Single average remote distance won't be appropriate if different
+	 * packages have different distances to remote packages.
+	 */
+	for (i = 0; i < max_pkgs; i++) {

arch/x86/kernel/smpboot.c static int avg_remote_numa_distance(void) · line 580

Quoted 10 lines
arch/x86/kernel/smpboot.c +10 -0
+		if (!pkg_nr_remote[i])
+			continue;
+
+		pkg_avg_distance = pkg_total_distance[i] / pkg_nr_remote[i];
+
+		pr_debug("sched: Avg. distance to remote package %d: %d\n", i, pkg_avg_distance);
+
+		if (pkg_avg_distance != sched_avg_remote_distance)
+			WARN_ONCE(1, "sched: Avg. distances to remote packages are different\n");
+	}

This is pretty yuck.

Quoted 5 lines

Also, what's with the pr_debug() stuff?

Anyway, that function was fairly magical, and now it is nearly impenetrable. If we want this, it needs comments. Definitely more comments, with nice pictures on.

OK, thank you for the feedback, I'll work on a v3.

Thanks,
Kyle Meyer

#3 lore raw

Hi Kyle,

Quoted 10 lines · Kyle Meyer
Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · Kyle Meyer

Granite Rapids (GNR) and Clearwater Forest (CWF) average distances to remote packages to fix scheduler domains, see [1] for more information.

A warning and backtrace are printed when sub-NUMA clustering (SNC) is enabled and there are more than 2 packages because the average distances to remote packages could be different, skewing the single average remote distance.

Quoted 5 lines
Quoted 2 lines

But earlier Tim said these systems will not have more than 2 packages. So what's what?

We have Intel customer reference boards with 2, 4, and 8 sockets.

Thanks for the info. We were not previously aware that the stock GNR platform will scale up to 8 sockets.

Quoted 22 lines
Quoted 1 line

So what do these new systems look like?

Here's an 8 socket (2 chassis) HPE system with SNC enabled:

node   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
   0:  10  12  16  16  16  16  18  18  40  40  40  40  40  40  40  40
   1:  12  10  16  16  16  16  18  18  40  40  40  40  40  40  40  40
   2:  16  16  10  12  18  18  16  16  40  40  40  40  40  40  40  40
   3:  16  16  12  10  18  18  16  16  40  40  40  40  40  40  40  40
   4:  16  16  18  18  10  12  16  16  40  40  40  40  40  40  40  40
   5:  16  16  18  18  12  10  16  16  40  40  40  40  40  40  40  40
   6:  18  18  16  16  16  16  10  12  40  40  40  40  40  40  40  40
   7:  18  18  16  16  16  16  12  10  40  40  40  40  40  40  40  40
   8:  40  40  40  40  40  40  40  40  10  12  16  16  16  16  18  18
   9:  40  40  40  40  40  40  40  40  12  10  16  16  16  16  18  18
  10:  40  40  40  40  40  40  40  40  16  16  10  12  18  18  16  16
  11:  40  40  40  40  40  40  40  40  16  16  12  10  18  18  16  16
  12:  40  40  40  40  40  40  40  40  16  16  18  18  10  12  16  16
  13:  40  40  40  40  40  40  40  40  16  16  18  18  12  10  16  16
  14:  40  40  40  40  40  40  40  40  18  18  16  16  16  16  10  12
  15:  40  40  40  40  40  40  40  40  18  18  16  16  16  16  12  10
Quoted 4 lines

10 = Same chassis and socket
12 = Same chassis and socket (SNC)
16 = Same chassis and adjacent socket
18 = Same chassis and non-adjacent socket

Previously, I thought that REMOTE_DISTANCE represents the distance between two nodes on different sockets, but that does not appear to be the case here. I could not find any definition of “20 or double” in the SLIT section of the ACPI specification. Thus, I assume this value of 20 is an artificial threshold. In my view, checking whether all distances above 20 are identical really depends on the specific platform. For example, if we have a distance value such as:
22 = Same chassis and non-adjacent socket then applying the current patch would trigger a warning regardless.

That said, since 20 is an artificial threshold, I have a tentative idea:we could normalize the SLIT distances by sorting the slit_dist values, finding the 75th percentile value, keeping all slit_dist values below the 75th percentile unchanged, and treating all slit_dist values above the 75th percentile as remote - assigning them the average remote distance. This way, we could eliminate the arbitrary value of 20. But that might require rewrite.. and for now it is ok to keep 20.

Quoted 18 lines

40 = Different chassis

Each processor connects to an ASIC (XNC) that acts as a multiplexer, extending the UPI interconnect across the entire system.

We don't experience the scheduler domain issue reported by Tim because our SLIT provides symmetric distances to remote NUMA nodes, but we trigger the WARN_ONCE because we exceed 2 packages.

Quoted 9 lines
Quoted 6 lines

This is unnecessary when the average distances to remote packages are the same.

Support single average remote distance on systems with more than 2 packages, preventing unnecessary warnings and backtraces, by checking if average distances to remote packages are the same.

[ ... ]

Quoted 8 lines
Quoted 5 lines

arch/x86/kernel/smpboot.c static int avg_remote_numa_distance(void) · line 557

Quoted 3 lines
arch/x86/kernel/smpboot.c +3 -0
+			pkg = topology_physical_package_id(cpu);
+			pkg_total_distance[pkg] += distance;
+			pkg_nr_remote[pkg]++;

This is broken, physical_package_id is not guaranteed to be dense.

Thank you, I'll fix this.

It seems that topology_logical_package_id() can work here.

thanks,
Chenyu

#4 lore raw
In reply to Kyle Meyer
Quoted 26 lines · Kyle Meyer

Here's an 8 socket (2 chassis) HPE system with SNC enabled:

node   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
  0:  10  12  16  16  16  16  18  18  40  40  40  40  40  40  40  40
  1:  12  10  16  16  16  16  18  18  40  40  40  40  40  40  40  40
  2:  16  16  10  12  18  18  16  16  40  40  40  40  40  40  40  40
  3:  16  16  12  10  18  18  16  16  40  40  40  40  40  40  40  40
  4:  16  16  18  18  10  12  16  16  40  40  40  40  40  40  40  40
  5:  16  16  18  18  12  10  16  16  40  40  40  40  40  40  40  40
  6:  18  18  16  16  16  16  10  12  40  40  40  40  40  40  40  40
  7:  18  18  16  16  16  16  12  10  40  40  40  40  40  40  40  40
  8:  40  40  40  40  40  40  40  40  10  12  16  16  16  16  18  18
  9:  40  40  40  40  40  40  40  40  12  10  16  16  16  16  18  18
 10:  40  40  40  40  40  40  40  40  16  16  10  12  18  18  16  16
 11:  40  40  40  40  40  40  40  40  16  16  12  10  18  18  16  16
 12:  40  40  40  40  40  40  40  40  16  16  18  18  10  12  16  16
 13:  40  40  40  40  40  40  40  40  16  16  18  18  12  10  16  16
 14:  40  40  40  40  40  40  40  40  18  18  16  16  16  16  10  12
 15:  40  40  40  40  40  40  40  40  18  18  16  16  16  16  12  10

10 = Same chassis and socket
12 = Same chassis and socket (SNC)
16 = Same chassis and adjacent socket
18 = Same chassis and non-adjacent socket
40 = Different chassis

Quoted 6 lines

Each processor connects to an ASIC (XNC) that acts as a multiplexer, extending the UPI interconnect across the entire system.

We don't experience the scheduler domain issue reported by Tim because our SLIT provides symmetric distances to remote NUMA nodes, but we trigger the WARN_ONCE because we exceed 2 packages.

The original case was for SNC-3, the above looks to be SNC-2. Does your system also support SNC-3?

Anyway, yes your SLIT table looks sane (unlike that SNC-3 monster Tim showed earlier).

And it also shows that using REMOTE_DISTANCE (20) was completely random and 'wrong'.

So per 4d6dd05d07d0 ("sched/topology: Fix sched domain build error for GNR, CWF in SNC-3 mode")

Tim's original crazy SNC-3 SLIT table was:

node distances:

node     0    1    2    3    4    5
    0:   10   15   17   21   28   26
    1:   15   10   15   23   26   23
    2:   17   15   10   26   23   21
    3:   21   28   26   10   15   17
    4:   23   26   23   15   10   15
    5:   26   23   21   17   15   10

And per:

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

My suggestion was to average the off-trace clusters to restore sanity.

So how about we go about implementing that without reference to magical numbers, something like so. This obviously needs a little TLC, but it might just work.

Hmm?

---

arch/x86/kernel/smpboot.c +45 -29
index 5cd6950ab672..cba3e4b14250 100644
@@ -513,33 +513,55 @@ static void __init build_sched_topology(void)
 }
 
 #ifdef CONFIG_NUMA
-static int sched_avg_remote_distance;
-static int avg_remote_numa_distance(void)
+
+/*
+ * Find the largest symmetric cluster in an attempt to identify the unit size.
+ *
+ * XXX doesn't respect N_CPU node classes and such.
+ */
+static int slit_cluster_size(void)
 {
-	int i, j;
-	int distance, nr_remote, total_distance;
+	int i, j, n, m = num_possible_nodes();
 
-	if (sched_avg_remote_distance > 0)
-		return sched_avg_remote_distance;
-
-	nr_remote = 0;
-	total_distance = 0;
-	for_each_node_state(i, N_CPU) {
-		for_each_node_state(j, N_CPU) {
-			distance = node_distance(i, j);
-
-			if (distance >= REMOTE_DISTANCE) {
-				nr_remote++;
-				total_distance += distance;
+	for (n = 2; n < m; n++) {
+		for (i = 0; i < n; i++) {
+			for (j = i; j < n; j++) {
+				if (node_distance(i, j) != node_distance(j, i))
+					return n - 1;
 			}
 		}
 	}
-	if (nr_remote)
-		sched_avg_remote_distance = total_distance / nr_remote;
-	else
-		sched_avg_remote_distance = REMOTE_DISTANCE;
 
-	return sched_avg_remote_distance;
+	return m;
+}
+
+static int slit_cluster_distance(int i, int j)
+{
+	static int u = 0;
+	long d = 0;
+	int x, y;
+
+	if (!u)
+		u = slit_cluster_size();
+
+	/*
+	 * Is this a unit cluster on the trace?
+	 */
+	if ((i / u) == (j / u))
+		return node_distance(i, j);
+
+	/*
+	 * Off-trace cluster, return average of the cluster to force symmetry.
+	 */
+	x = i - (i % u);
+	y = j - (j % u);
+
+	for (i = x; i < x + u; i++) {
+		for (j = y; j < y + u; j++)
+			d += node_distance(i, j);
+	}
+
+	return d / (u*u);
 }
 
 int arch_sched_node_distance(int from, int to)
@@ -550,8 +572,7 @@ int arch_sched_node_distance(int from, int to)
 	case INTEL_GRANITERAPIDS_X:
 	case INTEL_ATOM_DARKMONT_X:
 
-		if (!x86_has_numa_in_package || topology_max_packages() == 1 ||
-		    d < REMOTE_DISTANCE)
+		if (!x86_has_numa_in_package || topology_max_packages() == 1)
 			return d;
 
 		/*
@@ -571,12 +592,7 @@ int arch_sched_node_distance(int from, int to)
 		 * packages as average distance to different remote packages
 		 * could be different.
 		 */
-		WARN_ONCE(topology_max_packages() > 2,
-			  "sched: Expect only up to 2 packages for GNR or CWF, "
-			  "but saw %d packages when building sched domains.",
-			  topology_max_packages());
-
-		d = avg_remote_numa_distance();
+		return slit_cluster_distance(from, to);
 	}
 	return d;
 }
#5 lore raw
Quoted 19 lines · Peter Zijlstra
arch/x86/kernel/smpboot.c +9 -0
index 5cd6950ab672..cba3e4b14250 100644
@@ -513,33 +513,55 @@ static void __init build_sched_topology(void)
 }
 
 #ifdef CONFIG_NUMA
+
+/*
+ * Find the largest symmetric cluster in an attempt to identify the unit size.
+ *
+ * XXX doesn't respect N_CPU node classes and such.
+ */
+static int slit_cluster_size(void)
 {
+	int i, j, n, m = num_possible_nodes();
 
+	for (n = 2; n < m; n++) {

arch/x86/kernel/smpboot.c static void __init build_sched_topology(void) · line 536

Quoted 10 lines
arch/x86/kernel/smpboot.c +6 -0
+		for (i = 0; i < n; i++) {
+			for (j = i; j < n; j++) {
+				if (node_distance(i, j) != node_distance(j, i))
+					return n - 1;
 			}
 		}
 	}
 
+	return m;
+}

If we make x86_has_numa_in_package a counter of how many nodes in the package, we could use that number, rather than trying to guesstimate it.

Similarly, if the system would enumerate the SNC mode anywhere, that too could be used.

Quoted 28 lines
arch/x86/kernel/smpboot.c +27 -0
+static int slit_cluster_distance(int i, int j)
+{
+	static int u = 0;
+	long d = 0;
+	int x, y;
+
+	if (!u)
+		u = slit_cluster_size();
+
+	/*
+	 * Is this a unit cluster on the trace?
+	 */
+	if ((i / u) == (j / u))
+		return node_distance(i, j);
+
+	/*
+	 * Off-trace cluster, return average of the cluster to force symmetry.
+	 */
+	x = i - (i % u);
+	y = j - (j % u);
+
+	for (i = x; i < x + u; i++) {
+		for (j = y; j < y + u; j++)
+			d += node_distance(i, j);
+	}
+
+	return d / (u*u);
 }
#6 lore raw
In reply to Peter Zijlstra
Quoted 2 lines · Peter Zijlstra

On Tue, Feb 24, 2026 at 07:43:10PM -0600, Kyle Meyer wrote:

[ ... ]

Quoted 17 lines

And it also shows that using REMOTE_DISTANCE (20) was completely random and 'wrong'.

So per 4d6dd05d07d0 ("sched/topology: Fix sched domain build error for GNR, CWF in SNC-3 mode")

Tim's original crazy SNC-3 SLIT table was:

node distances:
node 0 1 2 3 4 5

     0:   10   15   17   21   28   26
     1:   15   10   15   23   26   23
     2:   17   15   10   26   23   21
     3:   21   28   26   10   15   17
     4:   23   26   23   15   10   15
     5:   26   23   21   17   15   10
Quoted 6 lines

And per:

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

My suggestion was to average the off-trace clusters to restore sanity.

In above example, the node distances become:
"Eg. since (21+28+26+23+26+23+26+23+21)/9 ~ 24, you end up with:

 node     0    1    2    3    4    5
     0:   10   15   17   24   24   24
     1:   15   10   15   24   24   24
     2:   17   15   10   24   24   24
     3:   24   24   24   10   15   17
     4:   24   24   24   15   10   15
     5:   24   24   24   17   15   10

"

Quoted 5 lines
arch/x86/kernel/smpboot.c +5 -0
+
+static int slit_cluster_distance(int i, int j)
+{
+	static int u = 0;
+	long d = 0;

arch/x86/kernel/smpboot.c static void __init build_sched_topology(void) · line 551

Quoted 10 lines
arch/x86/kernel/smpboot.c +10 -0
+	int x, y;
+
+	if (!u)
+		u = slit_cluster_size();
+
+	/*
+	 * Is this a unit cluster on the trace?
+	 */
+	if ((i / u) == (j / u))
+		return node_distance(i, j);

the u is 3 in above example, because slit_cluster_size() found that node0, node1 and node2 are in the same biggest symmetric cluster. Not sure if I understand it correctly, here we will treat node4 and node5 as the same cluster, but without checking whether node_distance(4, 5) and node_distance(5,4) are the same. If node_dist(4,5)!=node_dist(5,4), will we keep it as it is?

thanks,
Chenyu

#7 lore raw
Quoted 19 lines · Yu C
Quoted 4 lines
arch/x86/kernel/smpboot.c +4 -0
+static int slit_cluster_distance(int i, int j)
+{
+	static int u = 0;
+	long d = 0;

arch/x86/kernel/smpboot.c static void __init build_sched_topology(void) · line 551

Quoted 10 lines
arch/x86/kernel/smpboot.c +10 -0
+	int x, y;
+
+	if (!u)
+		u = slit_cluster_size();
+
+	/*
+	 * Is this a unit cluster on the trace?
+	 */
+	if ((i / u) == (j / u))
+		return node_distance(i, j);

the u is 3 in above example, because slit_cluster_size() found that node0, node1 and node2 are in the same biggest symmetric cluster.

Quoted 5 lines

Not sure if I understand it correctly,
here we will treat node4 and node5 as the same cluster, but without checking whether node_distance(4, 5) and node_distance(5,4) are the same. If node_dist(4,5)!=node_dist(5,4), will we keep it as it is?

Yes, so this assumes that all u sized clusters on the trace are similar and 'sane' without verification.

#8 lore raw
Quoted 2 lines · Peter Zijlstra

Yes, so this assumes that all u sized clusters on the trace are similar and 'sane' without verification.

That gave me an idea; how's this then?

---

arch/x86/kernel/smpboot.c +90 -36
index 5cd6950ab672..b1e464fd98c0 100644
@@ -513,33 +513,99 @@ static void __init build_sched_topology(void)
 }
 
 #ifdef CONFIG_NUMA
-static int sched_avg_remote_distance;
-static int avg_remote_numa_distance(void)
+
+static bool slit_cluster_symmetric(int i, int j, int n)
 {
-	int i, j;
-	int distance, nr_remote, total_distance;
+	WARN_ON_ONCE((i % n) || (j % n));
 
-	if (sched_avg_remote_distance > 0)
-		return sched_avg_remote_distance;
-
-	nr_remote = 0;
-	total_distance = 0;
-	for_each_node_state(i, N_CPU) {
-		for_each_node_state(j, N_CPU) {
-			distance = node_distance(i, j);
-
-			if (distance >= REMOTE_DISTANCE) {
-				nr_remote++;
-				total_distance += distance;
-			}
+	for (int k = i; k < i + n; k++) {
+		for (int l = k; l < j + n; l++) {
+			if (node_distance(k, l) != node_distance(k, l))
+				return false;
 		}
 	}
-	if (nr_remote)
-		sched_avg_remote_distance = total_distance / nr_remote;
-	else
-		sched_avg_remote_distance = REMOTE_DISTANCE;
 
-	return sched_avg_remote_distance;
+	return true;
+}
+
+static bool slit_cluster_match(int i, int j, int x, int y, int n)
+{
+	WARN_ON_ONCE((i % n) || (j % n) || (x % n) || (y % n));
+
+	for (int k = 0; k < n; k++) {
+		for (int l = k; l < n; l++) {
+			if (node_distance(i + k, j + l) != node_distance(x + k, y + l))
+				return false;
+		}
+	}
+
+	return true;
+}
+
+/*
+ * Find the largest symmetric,repeating cluster in an attempt to identify the
+ * unit size.
+ */
+static int slit_cluster_size(void)
+{
+	int nodes = num_possible_nodes();
+
+	/*
+	 * There are at least 2 packages; so half-nodes is the largest
+	 * possible unit, go down from that.
+	 */
+	for (int u = nodes / 2; u; u--) {
+		/*
+		 * If u doesn't divide nodes, it can't be a unit.
+		 */
+		if (nodes % u)
+			continue;
+
+		/*
+		 * Unit must be symmetric,
+		 */
+		if (!slit_cluster_symmetric(0, 0, u))
+			continue;
+
+		/*
+		 * and repeating.
+		 */
+		if (slit_cluster_match(0, 0, u, u, u))
+			return u;
+	}
+
+	return nodes;
+}
+
+static int slit_cluster_distance(int i, int j)
+{
+	static int u = 0;
+	long d = 0;
+	int x, y;
+
+	if (!u)
+		u = slit_cluster_size();
+
+	/*
+	 * Is this a unit cluster on the trace?
+	 */
+	if ((i / u) == (j / u))
+		return node_distance(i, j);
+
+	/*
+	 * Off-trace cluster, return average of the cluster to force symmetry.
+	 */
+	x = i - (i % u);
+	y = j - (j % u);
+
+	for (i = x; i < x + u; i++) {
+		for (j = y; j < y + u; j++) {
+			d += node_distance(i, j);
+			d += node_distance(j, i);
+		}
+	}
+
+	return d / (2*u*u);
 }
 
 int arch_sched_node_distance(int from, int to)
@@ -550,8 +616,7 @@ int arch_sched_node_distance(int from, int to)
 	case INTEL_GRANITERAPIDS_X:
 	case INTEL_ATOM_DARKMONT_X:
 
-		if (!x86_has_numa_in_package || topology_max_packages() == 1 ||
-		    d < REMOTE_DISTANCE)
+		if (!x86_has_numa_in_package || topology_max_packages() == 1)
 			return d;
 
 		/*
@@ -564,19 +629,8 @@ int arch_sched_node_distance(int from, int to)
 		 * in the remote package in the same sched group.
 		 * Simplify NUMA domains and avoid extra NUMA levels including
 		 * different remote NUMA nodes and local nodes.
-		 *
-		 * GNR and CWF don't expect systems with more than 2 packages
-		 * and more than 2 hops between packages. Single average remote
-		 * distance won't be appropriate if there are more than 2
-		 * packages as average distance to different remote packages
-		 * could be different.
 		 */
-		WARN_ONCE(topology_max_packages() > 2,
-			  "sched: Expect only up to 2 packages for GNR or CWF, "
-			  "but saw %d packages when building sched domains.",
-			  topology_max_packages());
-
-		d = avg_remote_numa_distance();
+		return slit_cluster_distance(from, to);
 	}
 	return d;
 }
#9 lore raw
Quoted 31 lines · Peter Zijlstra
arch/x86/kernel/smpboot.c +26 -0
+static bool slit_cluster_symmetric(int i, int j, int n)
 {
+	WARN_ON_ONCE((i % n) || (j % n));
 
+	for (int k = i; k < i + n; k++) {
+		for (int l = k; l < j + n; l++) {
+			if (node_distance(k, l) != node_distance(k, l))
+				return false;
 		}
 	}
 
+	return true;
+}
+
+static bool slit_cluster_match(int i, int j, int x, int y, int n)
+{
+	WARN_ON_ONCE((i % n) || (j % n) || (x % n) || (y % n));
+
+	for (int k = 0; k < n; k++) {
+		for (int l = k; l < n; l++) {
+			if (node_distance(i + k, j + l) != node_distance(x + k, y + l))
+				return false;
+		}
+	}
+
+	return true;
+}
+
+/*
+ * Find the largest symmetric,repeating cluster in an attempt to identify the
+ * unit size.

arch/x86/kernel/smpboot.c static void __init build_sched_topology(void) · line 557

Quoted 10 lines
arch/x86/kernel/smpboot.c +10 -0
+ */
+static int slit_cluster_size(void)
+{
+	int nodes = num_possible_nodes();
+
+	/*
+	 * There are at least 2 packages; so half-nodes is the largest
+	 * possible unit, go down from that.
+	 */
+	for (int u = nodes / 2; u; u--) {

nodes / topology_max_packages() might also do. And I worry about num_possible_nodes(), if that includes CXL or other such nonsense then we're up a creek.

But as stated before, ideally the hardware can actually just tell us the right number.

Quoted 42 lines
arch/x86/kernel/smpboot.c +42 -0
+		/*
+		 * If u doesn't divide nodes, it can't be a unit.
+		 */
+		if (nodes % u)
+			continue;
+
+		/*
+		 * Unit must be symmetric,
+		 */
+		if (!slit_cluster_symmetric(0, 0, u))
+			continue;
+
+		/*
+		 * and repeating.
+		 */
+		if (slit_cluster_match(0, 0, u, u, u))
+			return u;
+	}
+
+	return nodes;
+}
+
+static int slit_cluster_distance(int i, int j)
+{
+	static int u = 0;
+	long d = 0;
+	int x, y;
+
+	if (!u)
+		u = slit_cluster_size();
+
+	/*
+	 * Is this a unit cluster on the trace?
+	 */
+	if ((i / u) == (j / u))
+		return node_distance(i, j);
+
+	/*
+	 * Off-trace cluster, return average of the cluster to force symmetry.
+	 */
+	x = i - (i % u);
+	y = j - (j % u);

arch/x86/kernel/smpboot.c static void __init build_sched_topology(void) · line 609

Quoted 10 lines
arch/x86/kernel/smpboot.c +9 -0
+
+	for (i = x; i < x + u; i++) {
+		for (j = y; j < y + u; j++) {
+			d += node_distance(i, j);
+			d += node_distance(j, i);
+		}
+	}
+
+	return d / (2*u*u);
 }

Note that I changed this to average over the symmetric pair of off-trace clusters. Because if some BIOS monkey is really taking bong-hits, there is absolutely no guarantee the just the one cluster average will result in a symmetric system in the end.

#10 lore raw
In reply to Peter Zijlstra
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Peter Zijlstra

Yes, so this assumes that all u sized clusters on the trace are similar and 'sane' without verification.

That gave me an idea; how's this then?

Sorry I was sick for a few days. Just catching up on this thread here. I think your patch takes care of both GNR SNC-3 with 3 compute dies (with non-symmetric remote distances) and generic SNC-2 with 2 dies (symmetric distances) very well.

Minor suggestion below for the patch.

Will ask the original GNR teams with the problem to try it out.

Quoted 38 lines

---

arch/x86/kernel/smpboot.c +7 -17
index 5cd6950ab672..b1e464fd98c0 100644
@@ -513,33 +513,99 @@ static void __init build_sched_topology(void)
 }
 
 #ifdef CONFIG_NUMA
-static int sched_avg_remote_distance;
-static int avg_remote_numa_distance(void)
+
+static bool slit_cluster_symmetric(int i, int j, int n)
 {
-	int i, j;
-	int distance, nr_remote, total_distance;
+	WARN_ON_ONCE((i % n) || (j % n));
 
-	if (sched_avg_remote_distance > 0)
-		return sched_avg_remote_distance;
-
-	nr_remote = 0;
-	total_distance = 0;
-	for_each_node_state(i, N_CPU) {
-		for_each_node_state(j, N_CPU) {
-			distance = node_distance(i, j);
-
-			if (distance >= REMOTE_DISTANCE) {
-				nr_remote++;
-				total_distance += distance;
-			}
+	for (int k = i; k < i + n; k++) {
+		for (int l = k; l < j + n; l++) {
+			if (node_distance(k, l) != node_distance(k, l))
+				return false;
 		}
 	}

arch/x86/kernel/smpboot.c static void __init build_sched_topology(void) · line 531

Quoted 10 lines
arch/x86/kernel/smpboot.c +4 -5
-	if (nr_remote)
-		sched_avg_remote_distance = total_distance / nr_remote;
-	else
-		sched_avg_remote_distance = REMOTE_DISTANCE;
 
-	return sched_avg_remote_distance;
+	return true;
+}
+
+static bool slit_cluster_match(int i, int j, int x, int y, int n)

Seems like we only call this function with i==j and x==y (i.e. cluster at i and cluster at x). Can we simplify?

Thanks.

Tim

Quoted 111 lines
arch/x86/kernel/smpboot.c +79 -14
+{
+	WARN_ON_ONCE((i % n) || (j % n) || (x % n) || (y % n));
+
+	for (int k = 0; k < n; k++) {
+		for (int l = k; l < n; l++) {
+			if (node_distance(i + k, j + l) != node_distance(x + k, y + l))
+				return false;
+		}
+	}
+
+	return true;
+}
+
+/*
+ * Find the largest symmetric,repeating cluster in an attempt to identify the
+ * unit size.
+ */
+static int slit_cluster_size(void)
+{
+	int nodes = num_possible_nodes();
+
+	/*
+	 * There are at least 2 packages; so half-nodes is the largest
+	 * possible unit, go down from that.
+	 */
+	for (int u = nodes / 2; u; u--) {
+		/*
+		 * If u doesn't divide nodes, it can't be a unit.
+		 */
+		if (nodes % u)
+			continue;
+
+		/*
+		 * Unit must be symmetric,
+		 */
+		if (!slit_cluster_symmetric(0, 0, u))
+			continue;
+
+		/*
+		 * and repeating.
+		 */
+		if (slit_cluster_match(0, 0, u, u, u))
+			return u;
+	}
+
+	return nodes;
+}
+
+static int slit_cluster_distance(int i, int j)
+{
+	static int u = 0;
+	long d = 0;
+	int x, y;
+
+	if (!u)
+		u = slit_cluster_size();
+
+	/*
+	 * Is this a unit cluster on the trace?
+	 */
+	if ((i / u) == (j / u))
+		return node_distance(i, j);
+
+	/*
+	 * Off-trace cluster, return average of the cluster to force symmetry.
+	 */
+	x = i - (i % u);
+	y = j - (j % u);
+
+	for (i = x; i < x + u; i++) {
+		for (j = y; j < y + u; j++) {
+			d += node_distance(i, j);
+			d += node_distance(j, i);
+		}
+	}
+
+	return d / (2*u*u);
 }
 
 int arch_sched_node_distance(int from, int to)
@@ -550,8 +616,7 @@ int arch_sched_node_distance(int from, int to)
 	case INTEL_GRANITERAPIDS_X:
 	case INTEL_ATOM_DARKMONT_X:
 
-		if (!x86_has_numa_in_package || topology_max_packages() == 1 ||
-		    d < REMOTE_DISTANCE)
+		if (!x86_has_numa_in_package || topology_max_packages() == 1)
 			return d;
 
 		/*
@@ -564,19 +629,8 @@ int arch_sched_node_distance(int from, int to)
 		 * in the remote package in the same sched group.
 		 * Simplify NUMA domains and avoid extra NUMA levels including
 		 * different remote NUMA nodes and local nodes.
-		 *
-		 * GNR and CWF don't expect systems with more than 2 packages
-		 * and more than 2 hops between packages. Single average remote
-		 * distance won't be appropriate if there are more than 2
-		 * packages as average distance to different remote packages
-		 * could be different.
 		 */
-		WARN_ONCE(topology_max_packages() > 2,
-			  "sched: Expect only up to 2 packages for GNR or CWF, "
-			  "but saw %d packages when building sched domains.",
-			  topology_max_packages());
-
-		d = avg_remote_numa_distance();
+		return slit_cluster_distance(from, to);
 	}
 	return d;
 }
#11 lore raw
Quoted 14 lines · Tim Chen
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Peter Zijlstra

Yes, so this assumes that all u sized clusters on the trace are similar and 'sane' without verification.

That gave me an idea; how's this then?

Sorry I was sick for a few days. Just catching up on this thread here. I think your patch takes care of both GNR SNC-3 with 3 compute dies (with non-symmetric remote distances) and generic SNC-2 with 2 dies (symmetric distances) very well.

Quoted 4 lines

Minor suggestion below for the patch.

Will ask the original GNR teams with the problem to try it out.

Since HPE can obviously have a sane SLIT table; why can't we simply claim the SLIT table they had is broken and needs fixing?

Also, is there really no enumeration of the SNC mode available; must we really divinate?

#12 lore raw
Quoted 2 lines · Peter Zijlstra

Also, is there really no enumeration of the SNC mode available; must we really divinate?

There isn't; but we have existing divination in resctl instead of the topology code :-(

I'll try and fix that tomorrow.

#13 lore raw
In reply to Peter Zijlstra
Quoted 15 lines · Peter Zijlstra
Quoted 14 lines · Tim Chen
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Peter Zijlstra

Yes, so this assumes that all u sized clusters on the trace are similar and 'sane' without verification.

That gave me an idea; how's this then?

Sorry I was sick for a few days. Just catching up on this thread here. I think your patch takes care of both GNR SNC-3 with 3 compute dies (with non-symmetric remote distances) and generic SNC-2 with 2 dies (symmetric distances) very well.

Quoted 7 lines
Quoted 4 lines

Minor suggestion below for the patch.

Will ask the original GNR teams with the problem to try it out.

Since HPE can obviously have a sane SLIT table; why can't we simply claim the SLIT table they had is broken and needs fixing?

From what I can see HPE seems to use SNC-2 variant of GNR so the SLIT is symmetric.

Unfortunately in the topology for the 2 socket GNR that has 3 dies, there are truly unsymmetric paths from between die A to die B between remote sockets from what I'm told.

Quoted 3 lines

Also, is there really no enumeration of the SNC mode available; must we really divinate?

Let me dig into that a bit. I was also thinking with that information, it will make the code a lot simpler.

Tim

author
#14 lore raw
Quoted 18 lines · Tim Chen
Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Tim Chen
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Peter Zijlstra

Yes, so this assumes that all u sized clusters on the trace are similar and 'sane' without verification.

That gave me an idea; how's this then?

Sorry I was sick for a few days. Just catching up on this thread here. I think your patch takes care of both GNR SNC-3 with 3 compute dies (with non-symmetric remote distances) and generic SNC-2 with 2 dies (symmetric distances) very well.

Minor suggestion below for the patch.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Will ask the original GNR teams with the problem to try it out.

Since HPE can obviously have a sane SLIT table; why can't we simply claim the SLIT table they had is broken and needs fixing?

From what I can see HPE seems to use SNC-2 variant of GNR so the SLIT is symmetric.

Yes, and the SKUs that don't support SNC.

The SKUs that support SNC-3 are limited to 2 packages.

Quoted 3 lines

Unfortunately in the topology for the 2 socket GNR that has 3 dies, there are truly unsymmetric paths from between die A to die B between remote sockets from what I'm told.

What does MLC look like?

Quoted 5 lines
Quoted 2 lines

Also, is there really no enumeration of the SNC mode available; must we really divinate?

Let me dig into that a bit. I was also thinking with that information, it will make the code a lot simpler.

#15 lore raw
Quoted 25 lines · Kyle Meyer
Quoted 24 lines · Tim Chen
Quoted 22 lines · Peter Zijlstra
Quoted 14 lines · Tim Chen
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Peter Zijlstra

Yes, so this assumes that all u sized clusters on the trace are similar and 'sane' without verification.

That gave me an idea; how's this then?

Sorry I was sick for a few days. Just catching up on this thread here. I think your patch takes care of both GNR SNC-3 with 3 compute dies (with non-symmetric remote distances) and generic SNC-2 with 2 dies (symmetric distances) very well.

Quoted 4 lines

Minor suggestion below for the patch.

Will ask the original GNR teams with the problem to try it out.

Since HPE can obviously have a sane SLIT table; why can't we simply claim the SLIT table they had is broken and needs fixing?

Quoted 6 lines
Quoted 2 lines

From what I can see HPE seems to use SNC-2 variant of GNR so the SLIT is symmetric.

Yes, and the SKUs that don't support SNC.

The SKUs that support SNC-3 are limited to 2 packages.

Yes, I think there are only 2 packages SNC-3 out there.

Quoted 6 lines
Quoted 4 lines

Unfortunately in the topology for the 2 socket GNR that has 3 dies, there are truly unsymmetric paths from between die A to die B between remote sockets from what I'm told.

What does MLC look like?

I don't have access to one for measurements. Will have to ask colleagues to measure that.

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

Also, is there really no enumeration of the SNC mode available; must we really divinate?

Let me dig into that a bit. I was also thinking with that information, it will make the code a lot simpler.

There's truly no hardware bits to expose the SNC mode. Will have to rely on snc_get_config() and use the ratio of number of CPUs in node vs L3 to get an estimate.

Tim

author
#16 lore raw
In reply to Peter Zijlstra
Quoted 31 lines · Peter Zijlstra
Quoted 29 lines · Kyle Meyer

Here's an 8 socket (2 chassis) HPE system with SNC enabled:

node   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
  0:  10  12  16  16  16  16  18  18  40  40  40  40  40  40  40  40
  1:  12  10  16  16  16  16  18  18  40  40  40  40  40  40  40  40
  2:  16  16  10  12  18  18  16  16  40  40  40  40  40  40  40  40
  3:  16  16  12  10  18  18  16  16  40  40  40  40  40  40  40  40
  4:  16  16  18  18  10  12  16  16  40  40  40  40  40  40  40  40
  5:  16  16  18  18  12  10  16  16  40  40  40  40  40  40  40  40
  6:  18  18  16  16  16  16  10  12  40  40  40  40  40  40  40  40
  7:  18  18  16  16  16  16  12  10  40  40  40  40  40  40  40  40
  8:  40  40  40  40  40  40  40  40  10  12  16  16  16  16  18  18
  9:  40  40  40  40  40  40  40  40  12  10  16  16  16  16  18  18
 10:  40  40  40  40  40  40  40  40  16  16  10  12  18  18  16  16
 11:  40  40  40  40  40  40  40  40  16  16  12  10  18  18  16  16
 12:  40  40  40  40  40  40  40  40  16  16  18  18  10  12  16  16
 13:  40  40  40  40  40  40  40  40  16  16  18  18  12  10  16  16
 14:  40  40  40  40  40  40  40  40  18  18  16  16  16  16  10  12
 15:  40  40  40  40  40  40  40  40  18  18  16  16  16  16  12  10

10 = Same chassis and socket
12 = Same chassis and socket (SNC)
16 = Same chassis and adjacent socket
18 = Same chassis and non-adjacent socket
40 = Different chassis

Each processor connects to an ASIC (XNC) that acts as a multiplexer, extending the UPI interconnect across the entire system.

Quoted 6 lines
Quoted 3 lines

We don't experience the scheduler domain issue reported by Tim because our SLIT provides symmetric distances to remote NUMA nodes, but we trigger the WARN_ONCE because we exceed 2 packages.

The original case was for SNC-3, the above looks to be SNC-2. Does your system also support SNC-3?

We do not currently use SKUs that support SNC-3.

That distance would be set to 12:

node 0 1 2

  0:  10  12  12
  1:  12  10  12
  2:  12  12  10

That might be changed if there's actually a difference in distance.

Distances to adjacent sockets, non-adjacent sockets, and different chassis would remain the same.

Quoted 137 lines

Anyway, yes your SLIT table looks sane (unlike that SNC-3 monster Tim showed earlier).

And it also shows that using REMOTE_DISTANCE (20) was completely random and 'wrong'.

So per 4d6dd05d07d0 ("sched/topology: Fix sched domain build error for GNR, CWF in SNC-3 mode")

Tim's original crazy SNC-3 SLIT table was:

node distances:

node     0    1    2    3    4    5
    0:   10   15   17   21   28   26
    1:   15   10   15   23   26   23
    2:   17   15   10   26   23   21
    3:   21   28   26   10   15   17
    4:   23   26   23   15   10   15
    5:   26   23   21   17   15   10

And per:

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

My suggestion was to average the off-trace clusters to restore sanity.

So how about we go about implementing that without reference to magical numbers, something like so. This obviously needs a little TLC, but it might just work.

Hmm?

---

arch/x86/kernel/smpboot.c +45 -29
index 5cd6950ab672..cba3e4b14250 100644
@@ -513,33 +513,55 @@ static void __init build_sched_topology(void)
 }
 
 #ifdef CONFIG_NUMA
-static int sched_avg_remote_distance;
-static int avg_remote_numa_distance(void)
+
+/*
+ * Find the largest symmetric cluster in an attempt to identify the unit size.
+ *
+ * XXX doesn't respect N_CPU node classes and such.
+ */
+static int slit_cluster_size(void)
 {
-	int i, j;
-	int distance, nr_remote, total_distance;
+	int i, j, n, m = num_possible_nodes();
 
-	if (sched_avg_remote_distance > 0)
-		return sched_avg_remote_distance;
-
-	nr_remote = 0;
-	total_distance = 0;
-	for_each_node_state(i, N_CPU) {
-		for_each_node_state(j, N_CPU) {
-			distance = node_distance(i, j);
-
-			if (distance >= REMOTE_DISTANCE) {
-				nr_remote++;
-				total_distance += distance;
+	for (n = 2; n < m; n++) {
+		for (i = 0; i < n; i++) {
+			for (j = i; j < n; j++) {
+				if (node_distance(i, j) != node_distance(j, i))
+					return n - 1;
 			}
 		}
 	}
-	if (nr_remote)
-		sched_avg_remote_distance = total_distance / nr_remote;
-	else
-		sched_avg_remote_distance = REMOTE_DISTANCE;
 
-	return sched_avg_remote_distance;
+	return m;
+}
+
+static int slit_cluster_distance(int i, int j)
+{
+	static int u = 0;
+	long d = 0;
+	int x, y;
+
+	if (!u)
+		u = slit_cluster_size();
+
+	/*
+	 * Is this a unit cluster on the trace?
+	 */
+	if ((i / u) == (j / u))
+		return node_distance(i, j);
+
+	/*
+	 * Off-trace cluster, return average of the cluster to force symmetry.
+	 */
+	x = i - (i % u);
+	y = j - (j % u);
+
+	for (i = x; i < x + u; i++) {
+		for (j = y; j < y + u; j++)
+			d += node_distance(i, j);
+	}
+
+	return d / (u*u);
 }
 
 int arch_sched_node_distance(int from, int to)
@@ -550,8 +572,7 @@ int arch_sched_node_distance(int from, int to)
 	case INTEL_GRANITERAPIDS_X:
 	case INTEL_ATOM_DARKMONT_X:
 
-		if (!x86_has_numa_in_package || topology_max_packages() == 1 ||
-		    d < REMOTE_DISTANCE)
+		if (!x86_has_numa_in_package || topology_max_packages() == 1)
 			return d;
 
 		/*
@@ -571,12 +592,7 @@ int arch_sched_node_distance(int from, int to)
 		 * packages as average distance to different remote packages
 		 * could be different.
 		 */
-		WARN_ONCE(topology_max_packages() > 2,
-			  "sched: Expect only up to 2 packages for GNR or CWF, "
-			  "but saw %d packages when building sched domains.",
-			  topology_max_packages());
-
-		d = avg_remote_numa_distance();
+		return slit_cluster_distance(from, to);
 	}
 	return d;
 }
#17 lore raw
Quoted 10 lines · Kyle Meyer
Quoted 2 lines · Peter Zijlstra

The original case was for SNC-3, the above looks to be SNC-2. Does your system also support SNC-3?

We do not currently use SKUs that support SNC-3.

That distance would be set to 12:

node 0 1 2

Quoted 8 lines
  0:  10  12  12
  1:  12  10  12
  2:  12  12  10

That might be changed if there's actually a difference in distance.

Distances to adjacent sockets, non-adjacent sockets, and different chassis would remain the same.

OK, excellent. So its just the Intel reference systems that are crazy.

author
#18 lore raw
In reply to Kyle Meyer
Quoted 137 lines · Kyle Meyer

Granite Rapids (GNR) and Clearwater Forest (CWF) average distances to remote packages to fix scheduler domains, see [1] for more information.

A warning and backtrace are printed when sub-NUMA clustering (SNC) is enabled and there are more than 2 packages because the average distances to remote packages could be different, skewing the single average remote distance.

This is unnecessary when the average distances to remote packages are the same.

Support single average remote distance on systems with more than 2 packages, preventing unnecessary warnings and backtraces, by checking if average distances to remote packages are the same.

[1] commit 4d6dd05d07d0 ("sched/topology: Fix sched domain build error for GNR, CWF in SNC-3 mode").

---

The warning and backtrace were noticed on a 16 socket GNR system with SNC-2 enabled.

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

v1 -> v2:
* Initialize pkg_total_distance and pkg_nr_remote to NULL, as suggested by Tim.

---
 arch/x86/kernel/smpboot.c | 69 ++++++++++++++++++++++++++++-----------
 1 file changed, 50 insertions(+), 19 deletions(-)
arch/x86/kernel/smpboot.c +50 -19
index 5cd6950ab672..dc8f15bd2e19 100644
@@ -518,27 +518,69 @@ static int avg_remote_numa_distance(void)
 {
 	int i, j;
 	int distance, nr_remote, total_distance;
+	int max_pkgs = topology_max_packages();
+	int cpu, pkg, pkg_avg_distance;
+	int *pkg_total_distance = NULL, *pkg_nr_remote = NULL;
 
 	if (sched_avg_remote_distance > 0)
 		return sched_avg_remote_distance;
 
+	sched_avg_remote_distance = REMOTE_DISTANCE;
+
 	nr_remote = 0;
 	total_distance = 0;
+
+	pkg_total_distance = kcalloc(max_pkgs, sizeof(int), GFP_KERNEL);
+	if (!pkg_total_distance)
+		goto cleanup;
+
+	pkg_nr_remote = kcalloc(max_pkgs, sizeof(int), GFP_KERNEL);
+	if (!pkg_nr_remote)
+		goto cleanup;
+
 	for_each_node_state(i, N_CPU) {
 		for_each_node_state(j, N_CPU) {
 			distance = node_distance(i, j);
 
-			if (distance >= REMOTE_DISTANCE) {
-				nr_remote++;
-				total_distance += distance;
-			}
+			if (distance < REMOTE_DISTANCE)
+				continue;
+
+			nr_remote++;
+			total_distance += distance;
+
+			cpu = cpumask_first(cpumask_of_node(j));
+			if (cpu >= nr_cpu_ids)
+				continue;
+
+			pkg = topology_physical_package_id(cpu);
+			pkg_total_distance[pkg] += distance;
+			pkg_nr_remote[pkg]++;
 		}
 	}
-	if (nr_remote)
-		sched_avg_remote_distance = total_distance / nr_remote;
-	else
-		sched_avg_remote_distance = REMOTE_DISTANCE;
 
+	if (!nr_remote)
+		goto cleanup;
+
+	sched_avg_remote_distance = total_distance / nr_remote;
+
+	/*
+	 * Single average remote distance won't be appropriate if different
+	 * packages have different distances to remote packages.
+	 */
+	for (i = 0; i < max_pkgs; i++) {
+		if (!pkg_nr_remote[i])
+			continue;
+
+		pkg_avg_distance = pkg_total_distance[i] / pkg_nr_remote[i];
+
+		pr_debug("sched: Avg. distance to remote package %d: %d\n", i, pkg_avg_distance);
+
+		if (pkg_avg_distance != sched_avg_remote_distance)
+			WARN_ONCE(1, "sched: Avg. distances to remote packages are different\n");
+	}
+cleanup:
+	kfree(pkg_nr_remote);
+	kfree(pkg_total_distance);
 	return sched_avg_remote_distance;
 }
 
@@ -564,18 +606,7 @@ int arch_sched_node_distance(int from, int to)
 		 * in the remote package in the same sched group.
 		 * Simplify NUMA domains and avoid extra NUMA levels including
 		 * different remote NUMA nodes and local nodes.
-		 *
-		 * GNR and CWF don't expect systems with more than 2 packages
-		 * and more than 2 hops between packages. Single average remote
-		 * distance won't be appropriate if there are more than 2
-		 * packages as average distance to different remote packages
-		 * could be different.
 		 */
-		WARN_ONCE(topology_max_packages() > 2,
-			  "sched: Expect only up to 2 packages for GNR or CWF, "
-			  "but saw %d packages when building sched domains.",
-			  topology_max_packages());
-
 		d = avg_remote_numa_distance();
 	}
 	return d;
Signature · 1 line
2.52.0

Just a friendly ping.

Thanks,
Kyle Meyer