[PATCH] irq/matrix: simplify CPU selection logic

Zhan Xusheng posted 1 patch 2 weeks, 3 days ago
kernel/irq/matrix.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)
[PATCH] irq/matrix: simplify CPU selection logic
Posted by Zhan Xusheng 2 weeks, 3 days ago
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@xiaomi.com>
---
 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
Re: [PATCH] irq/matrix: simplify CPU selection logic
Posted by Thomas Gleixner 1 week, 5 days ago
On Wed, Jan 21 2026 at 11:05, Zhan Xusheng wrote:

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

You are describing what your patch is doing, but fail to provide any
explanation why this patch is required and which problem it actucally
solves. See:

  https://www.kernel.org/doc/html/latest/process/maintainer-tip.html#changelog

When you read that document, please read the paragraph about selecting
the subsystem prefix too.

Thanks,

        tglx