Re: [PATCH v2] sched/topology: Check average distances to remote packages

From: Peter Zijlstra

Date: Wed Feb 25 2026 - 11:41:22 EST


On Wed, Feb 25, 2026 at 05:32:46PM +0100, Peter Zijlstra wrote:
> +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.
> + */
> +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.

> + /*
> + * 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);
> }

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.