[PATCH] lib/group_cpus: sequentially bind CPUs within node to groups.

From: brookxu.cn

Date: Mon Sep 07 2026 - 05:01:40 EST


From: Chunguang Xu <chunguang.xu@xxxxxxxxxx>

grp_spread_init_one() tends to bind adjacent CPUs to a queue, which may
leads to performance regresstion in some case, such as SPDK, business
often need to bind SPDK to static CPUs to poll data, such as 3,4,5,6,
this will result in some SPDK instance may only have one queue active.
So I think we should tends to spread the CPUs within node to all queue
sequentially as what we do before.

Signed-off-by: Chunguang Xu <chunguang.xu@xxxxxxxxxx>
---
lib/group_cpus.c | 42 +++++++++++++++++++++++-------------------
1 file changed, 23 insertions(+), 19 deletions(-)

diff --git a/lib/group_cpus.c b/lib/group_cpus.c
index 18d43a406114..b40e885f6fbf 100644
--- a/lib/group_cpus.c
+++ b/lib/group_cpus.c
@@ -11,19 +11,28 @@

#ifdef CONFIG_SMP

-static void grp_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,
- unsigned int cpus_per_grp)
+struct node_groups {
+ unsigned id;
+
+ union {
+ unsigned ngroups;
+ unsigned ncpus;
+ };
+};
+
+static void grp_spread_init_one(struct node_groups *nv, struct cpumask *irqmsk,
+ struct cpumask *nmsk, unsigned int cpus_per_grp)
{
const struct cpumask *siblmsk;
int cpu, sibl;

+ cpu = cpumask_first(nmsk);
+
+ /* Should not happen, but I'm too lazy to think about it */
+ if (cpu >= nr_cpu_ids)
+ return;
+
for ( ; cpus_per_grp > 0; ) {
- cpu = cpumask_first(nmsk);
-
- /* Should not happen, but I'm too lazy to think about it */
- if (cpu >= nr_cpu_ids)
- return;
-
cpumask_clear_cpu(cpu, nmsk);
cpumask_set_cpu(cpu, irqmsk);
cpus_per_grp--;
@@ -39,6 +48,10 @@ static void grp_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,
cpumask_set_cpu(sibl, irqmsk);
cpus_per_grp--;
}
+
+ cpu += nv->ngroups;
+ if (!cpumask_test_cpu(cpu, nmsk))
+ return;
}
}

@@ -97,15 +110,6 @@ static int get_nodes_in_cpumask(cpumask_var_t *node_to_cpumask,
return nodes;
}

-struct node_groups {
- unsigned id;
-
- union {
- unsigned ngroups;
- unsigned ncpus;
- };
-};
-
static int ncpus_cmp_func(const void *l, const void *r)
{
const struct node_groups *ln = l;
@@ -320,8 +324,8 @@ static int __group_cpus_evenly(unsigned int startgrp, unsigned int numgrps,
*/
if (curgrp >= last_grp)
curgrp = 0;
- grp_spread_init_one(&masks[curgrp], nmsk,
- cpus_per_grp);
+
+ grp_spread_init_one(nv, &masks[curgrp], nmsk, cpus_per_grp);
}
done += nv->ngroups;
}
--
2.43.0