[PATCH] irq/matrix: simplify CPU selection logic

From: Zhan Xusheng

Date: Tue Jan 20 2026 - 22:06:06 EST


The functions matrix_find_best_cpu() and matrix_find_best_cpu_managed()
were refactored to simplify the CPU selection logic and improve
readability.

- In matrix_find_best_cpu(), the selection logic was clarified by ensuring
that the `best_cpu` is only updated when a CPU has more available IRQ
vectors than the previously selected CPU.

- In matrix_find_best_cpu_managed(), the logic was modified to select the
CPU with the fewest `managed_allocated` IRQ vectors, ensuring that the
`best_cpu` is only updated when a CPU has a strictly fewer number of
allocated vectors. Previously, the `best_cpu` was updated even when the
`managed_allocated` values were equal, which was unnecessary and
inconsistent.

This change improves code clarity and ensures that CPU selection is more
predictable, especially in high-load or resource-constrained scenarios.

Impact:
- The change improves the readability of both functions and makes the CPU
selection behavior more explicit and predictable.
- There is no functional change under normal operating conditions. The
behavior change only affects scenarios where the `managed_allocated`
value is equal between CPUs, which is rare and would have previously led
to unnecessary updates to `best_cpu`.

Signed-off-by: Zhan Xusheng <zhanxusheng@xxxxxxxxxx>
---
kernel/irq/matrix.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/kernel/irq/matrix.c b/kernel/irq/matrix.c
index a50f2305a8dc..ed4b1e44dc1e 100644
--- a/kernel/irq/matrix.c
+++ b/kernel/irq/matrix.c
@@ -140,14 +140,17 @@ static unsigned int matrix_find_best_cpu(struct irq_matrix *m,

best_cpu = UINT_MAX;

+ /* Select the online CPU with the most available vectors */
for_each_cpu(cpu, msk) {
cm = per_cpu_ptr(m->maps, cpu);

- if (!cm->online || cm->available <= maxavl)
+ if (!cm->online)
continue;

- best_cpu = cpu;
- maxavl = cm->available;
+ if (cm->available > maxavl) {
+ best_cpu = cpu;
+ maxavl = cm->available;
+ }
}
return best_cpu;
}
@@ -161,14 +164,17 @@ static unsigned int matrix_find_best_cpu_managed(struct irq_matrix *m,

best_cpu = UINT_MAX;

+ /* Select the online CPU with the fewest managed allocated vectors */
for_each_cpu(cpu, msk) {
cm = per_cpu_ptr(m->maps, cpu);

- if (!cm->online || cm->managed_allocated > allocated)
+ if (!cm->online)
continue;

- best_cpu = cpu;
- allocated = cm->managed_allocated;
+ if (cm->managed_allocated < allocated) {
+ best_cpu = cpu;
+ allocated = cm->managed_allocated;
+ }
}
return best_cpu;
}
--
2.43.0