Skip to contentSkip to conversation

lib/group_cpus: fix cross-NUMA CPU assignment in group_cpus_evenly

PATCH In review Ming Lei 2 participants 5 messages Oct 20, 2025 – Dec 22, 2025

Subsystems (3)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 lib/group_cpus: fix cross-NUMA CPU assignment in group_cpus_evenly

When numgrps > nodes, group_cpus_evenly() can incorrectly assign CPUs from different NUMA nodes to the same group due to the wrapping logic. Then poor block IO performance is caused because of remote IO completion. And it can be avoided completely in case of numgrps > nodes because each numa node may includes more CPUs than group's.

The issue occurs when curgrp reaches last_grp and wraps to 0. This causes CPUs from later-processed nodes to be added to groups that already contain CPUs from earlier-processed nodes, violating NUMA locality.

Example with 8 NUMA nodes, 16 groups:
- Each node gets 2 groups allocated
- After processing nodes, curgrp reaches 16
- Wrapping to 0 causes CPUs from node N to be added to group 0 which already has CPUs from node 0

Fix this by adding find_next_node_group() helper that searches for the next group (starting from 0) that already contains CPUs from the same NUMA node. When wrapping is needed, use this helper instead of blindly wrapping to 0, ensuring CPUs are only added to groups within the same NUMA node.

---
 lib/group_cpus.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)
lib/group_cpus.c +25 -3
index 6d08ac05f371..54d70271e2dd 100644
@@ -246,6 +246,24 @@ static void alloc_nodes_groups(unsigned int numgrps,
 	}
 }
 
+/*
+ * Find next group in round-robin fashion that contains CPUs from the
+ * specified NUMA node. Used for wrapping to avoid cross-NUMA assignment.
+ */
+static unsigned int find_next_node_group(struct cpumask *masks,
+					 unsigned int numgrps,
+					 const struct cpumask *node_cpus)
+{
+	unsigned int i;
+
+	for (i = 0; i < numgrps; i++) {
+		if (cpumask_intersects(&masks[i], node_cpus))
+			return i;
+	}
+
+	return 0;
+}
+
 static int __group_cpus_evenly(unsigned int startgrp, unsigned int numgrps,
 			       cpumask_var_t *node_to_cpumask,
 			       const struct cpumask *cpu_mask,
@@ -315,11 +333,15 @@ static int __group_cpus_evenly(unsigned int startgrp, unsigned int numgrps,
 			}
 
 			/*
-			 * wrapping has to be considered given 'startgrp'
-			 * may start anywhere
+			 * Wrapping has to be considered given 'startgrp'
+			 * may start anywhere. When wrapping, find the next
+			 * group (in round-robin fashion) that already contains
+			 * CPUs from the same NUMA node to avoid mixing CPUs
+			 * from different NUMA nodes in the same group.
 			 */
 			if (curgrp >= last_grp)
-				curgrp = 0;
+				curgrp = find_next_node_group(masks, numgrps,
+							      node_to_cpumask[nv->id]);
 			grp_spread_init_one(&masks[curgrp], nmsk,
 						cpus_per_grp);
 		}
Signature · 1 line
2.51.0
author
#1 lore raw
Quoted 16 lines · Ming Lei

When numgrps > nodes, group_cpus_evenly() can incorrectly assign CPUs from different NUMA nodes to the same group due to the wrapping logic. Then poor block IO performance is caused because of remote IO completion. And it can be avoided completely in case of numgrps > nodes because each numa node may includes more CPUs than group's.

The issue occurs when curgrp reaches last_grp and wraps to 0. This causes CPUs from later-processed nodes to be added to groups that already contain CPUs from earlier-processed nodes, violating NUMA locality.

Example with 8 NUMA nodes, 16 groups:
- Each node gets 2 groups allocated
- After processing nodes, curgrp reaches 16
- Wrapping to 0 causes CPUs from node N to be added to group 0 which already has CPUs from node 0

Quoted 7 lines

Fix this by adding find_next_node_group() helper that searches for the next group (starting from 0) that already contains CPUs from the same NUMA node. When wrapping is needed, use this helper instead of blindly wrapping to 0, ensuring CPUs are only added to groups within the same NUMA node.

Hello,

ping...

Thanks,
Ming

author
#2 lore raw
Quoted 24 lines · Ming Lei
Quoted 22 lines · Ming Lei

When numgrps > nodes, group_cpus_evenly() can incorrectly assign CPUs from different NUMA nodes to the same group due to the wrapping logic. Then poor block IO performance is caused because of remote IO completion. And it can be avoided completely in case of numgrps > nodes because each numa node may includes more CPUs than group's.

The issue occurs when curgrp reaches last_grp and wraps to 0. This causes CPUs from later-processed nodes to be added to groups that already contain CPUs from earlier-processed nodes, violating NUMA locality.

Example with 8 NUMA nodes, 16 groups:
- Each node gets 2 groups allocated
- After processing nodes, curgrp reaches 16
- Wrapping to 0 causes CPUs from node N to be added to group 0 which already has CPUs from node 0

Fix this by adding find_next_node_group() helper that searches for the next group (starting from 0) that already contains CPUs from the same NUMA node. When wrapping is needed, use this helper instead of blindly wrapping to 0, ensuring CPUs are only added to groups within the same NUMA node.

Quoted 5 lines
Quoted 1 line

Hello,

ping...

Hello,

Ping...

Thanks,
Ming

maintainer
#3 lore raw
In reply to Ming Lei
Quoted 5 lines · On Mon, 20 Oct 2025 20:46:46 +0800 Ming Lei <[email protected]> wrote:

When numgrps > nodes, group_cpus_evenly() can incorrectly assign CPUs from different NUMA nodes to the same group due to the wrapping logic. Then poor block IO performance is caused because of remote IO completion. And it can be avoided completely in case of numgrps > nodes because each numa node may includes more CPUs than group's.

Please quantify "poor block IO performance", to help people understand the userspace-visible effect of this change.

Quoted 16 lines

The issue occurs when curgrp reaches last_grp and wraps to 0. This causes CPUs from later-processed nodes to be added to groups that already contain CPUs from earlier-processed nodes, violating NUMA locality.

Example with 8 NUMA nodes, 16 groups:
- Each node gets 2 groups allocated
- After processing nodes, curgrp reaches 16
- Wrapping to 0 causes CPUs from node N to be added to group 0 which already has CPUs from node 0

Fix this by adding find_next_node_group() helper that searches for the next group (starting from 0) that already contains CPUs from the same NUMA node. When wrapping is needed, use this helper instead of blindly wrapping to 0, ensuring CPUs are only added to groups within the same NUMA node.

Quoted 3 lines
---
 lib/group_cpus.c | 28 +++++++++++++++++++++++++---

The patch overlaps (a lot) with Wangyang Guo's "lib/group_cpus: make group CPU cluster aware". I did a lot of surgery but got stuck on the absence of node_to_cpumask, so I guess the patch has bitrotted.

Please update the changelog as above and redo this patch against Wangyang's patch (which will be in linux-next very soon).

Also, it would be great if you and Wangyang were to review and test each other's changes, thanks.

author
#4 lore raw
Quoted 10 lines · Andrew Morton
Quoted 5 lines · On Mon, 20 Oct 2025 20:46:46 +0800 Ming Lei <[email protected]> wrote:

When numgrps > nodes, group_cpus_evenly() can incorrectly assign CPUs from different NUMA nodes to the same group due to the wrapping logic. Then poor block IO performance is caused because of remote IO completion. And it can be avoided completely in case of numgrps > nodes because each numa node may includes more CPUs than group's.

Please quantify "poor block IO performance", to help people understand the userspace-visible effect of this change.

It is usually a bug, given fast nvme IO perf may drop to 1/2 or 1/3 in case of remote completion. queue mapping shouldn't cross CPUs from different numa nodes in case of nr_queues >= nr_nodes.

Quoted 21 lines
Quoted 17 lines

The issue occurs when curgrp reaches last_grp and wraps to 0. This causes CPUs from later-processed nodes to be added to groups that already contain CPUs from earlier-processed nodes, violating NUMA locality.

Example with 8 NUMA nodes, 16 groups:
- Each node gets 2 groups allocated
- After processing nodes, curgrp reaches 16
- Wrapping to 0 causes CPUs from node N to be added to group 0 which already has CPUs from node 0

Fix this by adding find_next_node_group() helper that searches for the next group (starting from 0) that already contains CPUs from the same NUMA node. When wrapping is needed, use this helper instead of blindly wrapping to 0, ensuring CPUs are only added to groups within the same NUMA node.

Quoted 3 lines
---
 lib/group_cpus.c | 28 +++++++++++++++++++++++++---
Quoted 6 lines

The patch overlaps (a lot) with Wangyang Guo's "lib/group_cpus: make group CPU cluster aware". I did a lot of surgery but got stuck on the absence of node_to_cpumask, so I guess the patch has bitrotted.

Please update the changelog as above and redo this patch against Wangyang's patch (which will be in linux-next very soon).

Please ignore this patch now because I can't reproduce the original issue on both v6.18 and v6.19-rc.

Quoted 3 lines

Also, it would be great if you and Wangyang were to review and test each other's changes, thanks.

OK.

Thanks,
Ming