Skip to contentSkip to conversation

Drivers: hv: Avoid infinite retry loop in init_vp_index()

PATCH Superseded Waiman Long 3 participants 3 messages Aug 26 – Aug 27

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Drivers: hv: Avoid infinite retry loop in init_vp_index()

There is a retry loop in init_vp_index() where the CPUs from a certain node are stripped out if they have already been in the allocated cpumask or not in HK_TYPE_MANAGED_IRQ housekeeping cpumask. If there is no CPU left, the allocated cpumask is ignored and the process is retried again. However, if the HK_TYPE_MANAGED_IRQ housekeeping cpumask turns out not to contain any CPU in that particular node, that will become an infinite retry loop. This particular problem was reported by sashiko [1]. This should rarely happen, but we still need to guard against this.

Fix this infinite loop problem by skipping to the next numa node if the allocated cpumask has already been cleared before. Set target_cpu to the default VMBUS_CONNECT_CPU instead if the for loop is ending.

Fixes 6640b5df1a38 ("Drivers: hv: vmbus: Don't assign VMbus channel interrupts to isolated CPUs")
---
 drivers/hv/channel_mgmt.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/hv/channel_mgmt.c +13 -3
index 89d214dda360..e80ac9fb5ed3 100644
@@ -793,10 +793,20 @@ static void init_vp_index(struct vmbus_channel *channel)
 		if (cpumask_empty(available_mask)) {
 			/*
 			 * We have cycled through all the CPUs in the node;
-			 * reset the allocated map.
+			 * reset the allocated map. If the allocated map
+			 * has already been cleared, we will try the next numa
+			 * node. Set target_cpu to the default VMBUS_CONNECT_CPU
+			 * instead if the for loop is going to end.
 			 */
-			cpumask_clear(allocated_mask);
-			goto retry;
+			if (!cpumask_empty(allocated_mask)) {
+				cpumask_clear(allocated_mask);
+				goto retry;
+			}
+			if (i > ncpu) {
+				target_cpu = VMBUS_CONNECT_CPU;
+				break;
+			}
+			continue;	/* Try next numa node */
 		}
 
 		target_cpu = cpumask_first(available_mask);
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Incrementing the bounded loop counter when skipping empty NUMA nodes causes premature search…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Incrementing the bounded loop counter when skipping empty NUMA nodes causes premature search termination, falling back to CPU 0.
- [Medium] When the loop falls back to VMBUS_CONNECT_CPU, it fails to register this allocation in the hv_numa_map, breaking the round-robin distribution tracker. --

commit 5b552e920b17d64534cf7a69fd8a78145809498a
Author: Waiman Long <[email protected]>
Drivers: hv: Avoid infinite retry loop in init_vp_index()

This commit fixes an infinite retry loop in init_vp_index() that occurs when the housekeeping cpumask for managed IRQs contains no CPUs in a particular NUMA node. It resolves this by skipping to the next node if the allocated cpumask is already cleared, and falling back to VMBUS_CONNECT_CPU if the loop is about to end.

Quoted 12 lines
drivers/hv/channel_mgmt.c +4 -1
@@ -793,10 +793,20 @@ static void init_vp_index(struct vmbus_channel *channel)
 		if (cpumask_empty(available_mask)) {
 			/*
 			 * We have cycled through all the CPUs in the node;
-			 * reset the allocated map.
+			 * reset the allocated map. If the allocated map
+			 * has already been cleared, we will try the next numa
+			 * node. Set target_cpu to the default VMBUS_CONNECT_CPU
+			 * instead if the for loop is going to end.

drivers/hv/channel_mgmt.c static void init_vp_index(struct vmbus_channel *channel) · line 807

Quoted 10 lines
drivers/hv/channel_mgmt.c +7 -2
 			 */
-			cpumask_clear(allocated_mask);
-			goto retry;
+			if (!cpumask_empty(allocated_mask)) {
+				cpumask_clear(allocated_mask);
+				goto retry;
+			}
+			if (i > ncpu) {
+				target_cpu = VMBUS_CONNECT_CPU;
+				break;

[Severity: Medium]
When the loop falls back to VMBUS_CONNECT_CPU here, does this bypass updating the round-robin allocation tracker?

The fallback block earlier in init_vp_index() correctly calls hv_set_allocated_cpu(VMBUS_CONNECT_CPU) to maintain the global allocation state:

    channel->target_cpu = VMBUS_CONNECT_CPU;
    if (perf_chn)
        hv_set_allocated_cpu(VMBUS_CONNECT_CPU);
    return;

Could breaking out directly here without making the corresponding state update cause a load imbalance by stacking extra channels on CPU 0 without the balancer knowing?

drivers/hv/channel_mgmt.c static void init_vp_index(struct vmbus_channel *channel) · line 809

Quoted 2 lines
drivers/hv/channel_mgmt.c +2 -0
+			}
+			continue;	/* Try next numa node */

[Severity: High]
Does this continue statement prematurely exhaust the loop iterations when encountering nodes with no housekeeping CPUs?

Since the loop counter i limits the total search across ncpu + 1 iterations, incrementing it while skipping an empty NUMA node burns through the search budget without actually evaluating any CPUs.

For example, in a topology with several isolated NUMA nodes, could skipping these empty nodes cause the loop budget to run out early, incorrectly forcing a fallback to VMBUS_CONNECT_CPU before checking all valid housekeeping CPUs?

Quoted 3 lines
drivers/hv/channel_mgmt.c +0 -0
 		}
 
 		target_cpu = cpumask_first(available_mask);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Waiman Long

From: Waiman Long <[email protected]> Sent: Wednesday, August 26, 2026 12:25 PM

Quoted 31 lines

There is a retry loop in init_vp_index() where the CPUs from a certain node are stripped out if they have already been in the allocated cpumask or not in HK_TYPE_MANAGED_IRQ housekeeping cpumask. If there is no CPU left, the allocated cpumask is ignored and the process is retried again. However, if the HK_TYPE_MANAGED_IRQ housekeeping cpumask turns out not to contain any CPU in that particular node, that will become an infinite retry loop. This particular problem was reported by sashiko [1]. This should rarely happen, but we still need to guard against this.

Fix this infinite loop problem by skipping to the next numa node if the allocated cpumask has already been cleared before. Set target_cpu to the default VMBUS_CONNECT_CPU instead if the for loop is ending.

Fixes 6640b5df1a38 ("Drivers: hv: vmbus: Don't assign VMbus channel interrupts to isolated CPUs")
---
 drivers/hv/channel_mgmt.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/hv/channel_mgmt.c +1 -1
index 89d214dda360..e80ac9fb5ed3 100644
@@ -793,10 +793,20 @@ static void init_vp_index(struct vmbus_channel *channel)
 		if (cpumask_empty(available_mask)) {
 			/*
 			 * We have cycled through all the CPUs in the node;
-			 * reset the allocated map.
+			 * reset the allocated map. If the allocated map

drivers/hv/channel_mgmt.c static void init_vp_index(struct vmbus_channel *channel) · line 804

Quoted 10 lines
drivers/hv/channel_mgmt.c +7 -2
+			 * has already been cleared, we will try the next numa
+			 * node. Set target_cpu to the default VMBUS_CONNECT_CPU
+			 * instead if the for loop is going to end.
 			 */
-			cpumask_clear(allocated_mask);
-			goto retry;
+			if (!cpumask_empty(allocated_mask)) {
+				cpumask_clear(allocated_mask);
+				goto retry;
+			}

As you describe in the commit message, the core problem is a NUMA node that has no housekeeping CPUs. But rather than add a test here for that condition, consider that there is a "while (true)" loop a few lines above that cycles thru the NUMA nodes and already skips NUMA nodes that have no CPUs. What if that loop was enhanced so that NUMA nodes with no housekeeping CPUs are skipped? Then all the existing machinery just works. I think this approach also addresses the two Sashiko comments.

drivers/hv/channel_mgmt.c static void init_vp_index(struct vmbus_channel *channel) · line 807

Quoted 3 lines
drivers/hv/channel_mgmt.c +3 -0
+			if (i > ncpu) {
+				target_cpu = VMBUS_CONNECT_CPU;
+				break;

Can this condition really happen? Won't the "for" loop always find a housekeeping CPU in some NUMA node? If you want some additional robustness in case the "for" loop doesn't pick a CPU, I think initializing target_cpu to VMBUS_CONNECT_CPU up in the local variable definitions would be sufficient.

Michael

Quoted 8 lines
drivers/hv/channel_mgmt.c +2 -1
+			}
+			continue;	/* Try next numa node */
 		}
 
 		target_cpu = cpumask_first(available_mask);
--

2.55.0