[PATCH 20/21] genirq: Switch to number_of_interrupts()

Bart Van Assche posted 21 patches 1 month, 4 weeks ago
There is a newer version of this series
[PATCH 20/21] genirq: Switch to number_of_interrupts()
Posted by Bart Van Assche 1 month, 4 weeks ago
Use the number_of_interrupts() function instead of the global variable
'nr_irqs'. This patch prepares for changing 'nr_irqs' from an exported
global variable into a variable with file scope.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 include/linux/irqnr.h  | 15 ++++++++-------
 kernel/irq/irqdomain.c |  2 +-
 kernel/irq/proc.c      |  5 +++--
 3 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/include/linux/irqnr.h b/include/linux/irqnr.h
index a8b2cb6146e8..81b76949c52e 100644
--- a/include/linux/irqnr.h
+++ b/include/linux/irqnr.h
@@ -12,7 +12,8 @@ extern struct irq_desc *irq_to_desc(unsigned int irq);
 unsigned int irq_get_next_irq(unsigned int offset);
 
 # define for_each_irq_desc(irq, desc)					\
-	for (irq = 0, desc = irq_to_desc(irq); irq < nr_irqs;		\
+	for (irq = 0, desc = irq_to_desc(irq);				\
+	     irq < number_of_interrupts();				\
 	     irq++, desc = irq_to_desc(irq))				\
 		if (!desc)						\
 			;						\
@@ -20,17 +21,17 @@ unsigned int irq_get_next_irq(unsigned int offset);
 
 
 # define for_each_irq_desc_reverse(irq, desc)				\
-	for (irq = nr_irqs - 1, desc = irq_to_desc(irq); irq >= 0;	\
-	     irq--, desc = irq_to_desc(irq))				\
+	for (irq = number_of_interrupts() - 1, desc = irq_to_desc(irq);	\
+	     irq >= 0; irq--, desc = irq_to_desc(irq))			\
 		if (!desc)						\
 			;						\
 		else
 
-# define for_each_active_irq(irq)			\
-	for (irq = irq_get_next_irq(0); irq < nr_irqs;	\
+# define for_each_active_irq(irq)					\
+	for (irq = irq_get_next_irq(0); irq < number_of_interrupts();	\
 	     irq = irq_get_next_irq(irq + 1))
 
-#define for_each_irq_nr(irq)                   \
-       for (irq = 0; irq < nr_irqs; irq++)
+#define for_each_irq_nr(irq)					\
+       for (irq = 0; irq < number_of_interrupts(); irq++)
 
 #endif
diff --git a/kernel/irq/irqdomain.c b/kernel/irq/irqdomain.c
index e0bff21f30e0..298930f2525a 100644
--- a/kernel/irq/irqdomain.c
+++ b/kernel/irq/irqdomain.c
@@ -1225,7 +1225,7 @@ int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
 		virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
 					 affinity);
 	} else {
-		hint = hwirq % nr_irqs;
+		hint = hwirq % number_of_interrupts();
 		if (hint == 0)
 			hint++;
 		virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
diff --git a/kernel/irq/proc.c b/kernel/irq/proc.c
index 9081ada81c3d..c301dfd7d9d0 100644
--- a/kernel/irq/proc.c
+++ b/kernel/irq/proc.c
@@ -457,7 +457,7 @@ int __weak arch_show_interrupts(struct seq_file *p, int prec)
 }
 
 #ifndef ACTUAL_NR_IRQS
-# define ACTUAL_NR_IRQS nr_irqs
+# define ACTUAL_NR_IRQS number_of_interrupts()
 #endif
 
 int show_interrupts(struct seq_file *p, void *v)
@@ -477,7 +477,8 @@ int show_interrupts(struct seq_file *p, void *v)
 
 	/* print header and calculate the width of the first column */
 	if (i == 0) {
-		for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
+		for (prec = 3, j = 1000;
+		     prec < 10 && j <= number_of_interrupts(); ++prec)
 			j *= 10;
 
 		seq_printf(p, "%*s", prec + 8, "");
Re: [PATCH 20/21] genirq: Switch to number_of_interrupts()
Posted by Bart Van Assche 1 month, 3 weeks ago
On 9/30/24 11:15 AM, Bart Van Assche wrote:
> Use the number_of_interrupts() function instead of the global variable
> 'nr_irqs'. This patch prepares for changing 'nr_irqs' from an exported
> global variable into a variable with file scope.

(replying to my own email)

Although no code should loop over all interrupts in the hot path, if 
nobody objects, I will replace this patch with the patch below. The 
patch below does not rely on CSE (common subexpression eliminiation) to
reduce the number of irq_get_nr_irqs() calls. The patch below should
support code that uses 'break' or 'continue' inside for_each_irq* loops.

Thanks,

Bart.


Subject: [PATCH] genirq: Switch to irq_get_nr_irqs()

Use the irq_get_nr_irqs() function instead of the global variable
'nr_irqs'. This patch prepares for changing 'nr_irqs' from an exported
global variable into a variable with file scope.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
  include/linux/irqnr.h  | 33 +++++++++++++++++++--------------
  kernel/irq/irqdomain.c |  2 +-
  kernel/irq/proc.c      |  5 +++--
  3 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/include/linux/irqnr.h b/include/linux/irqnr.h
index 7419b807b71b..a33088d27c54 100644
--- a/include/linux/irqnr.h
+++ b/include/linux/irqnr.h
@@ -11,26 +11,31 @@ unsigned int irq_set_nr_irqs(unsigned int nr);
  extern struct irq_desc *irq_to_desc(unsigned int irq);
  unsigned int irq_get_next_irq(unsigned int offset);

-# define for_each_irq_desc(irq, desc)					\
-	for (irq = 0, desc = irq_to_desc(irq); irq < nr_irqs;		\
-	     irq++, desc = irq_to_desc(irq))				\
-		if (!desc)						\
-			;						\
-		else
-
+#define for_each_irq_desc(irq, desc)                                      \
+	for (unsigned int __nr_irqs__ = irq_get_nr_irqs(); __nr_irqs__;   \
+	     __nr_irqs__ = 0)                                             \
+		for (irq = 0, desc = irq_to_desc(irq); irq < __nr_irqs__; \
+		     irq++, desc = irq_to_desc(irq))                      \
+			if (!desc)                                        \
+				;                                         \
+			else

  # define for_each_irq_desc_reverse(irq, desc)				\
-	for (irq = nr_irqs - 1, desc = irq_to_desc(irq); irq >= 0;	\
-	     irq--, desc = irq_to_desc(irq))				\
+	for (irq = irq_get_nr_irqs() - 1, desc = irq_to_desc(irq);	\
+	     irq >= 0; irq--, desc = irq_to_desc(irq))			\
  		if (!desc)						\
  			;						\
  		else

-# define for_each_active_irq(irq)			\
-	for (irq = irq_get_next_irq(0); irq < nr_irqs;	\
-	     irq = irq_get_next_irq(irq + 1))
+#define for_each_active_irq(irq)                                        \
+	for (unsigned int __nr_irqs__ = irq_get_nr_irqs(); __nr_irqs__; \
+	     __nr_irqs__ = 0)                                           \
+		for (irq = irq_get_next_irq(0); irq < __nr_irqs__;      \
+		     irq = irq_get_next_irq(irq + 1))

-#define for_each_irq_nr(irq)                   \
-       for (irq = 0; irq < nr_irqs; irq++)
+#define for_each_irq_nr(irq)                                            \
+	for (unsigned int __nr_irqs__ = irq_get_nr_irqs(); __nr_irqs__; \
+	     __nr_irqs__ = 0)                                           \
+		for (irq = 0; irq < __nr_irqs__; irq++)

  #endif
diff --git a/kernel/irq/irqdomain.c b/kernel/irq/irqdomain.c
index e0bff21f30e0..ec6d8e72d980 100644
--- a/kernel/irq/irqdomain.c
+++ b/kernel/irq/irqdomain.c
@@ -1225,7 +1225,7 @@ int irq_domain_alloc_descs(int virq, unsigned int 
cnt, irq_hw_number_t hwirq,
  		virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
  					 affinity);
  	} else {
-		hint = hwirq % nr_irqs;
+		hint = hwirq % irq_get_nr_irqs();
  		if (hint == 0)
  			hint++;
  		virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
diff --git a/kernel/irq/proc.c b/kernel/irq/proc.c
index 9081ada81c3d..36ffff4eb352 100644
--- a/kernel/irq/proc.c
+++ b/kernel/irq/proc.c
@@ -457,7 +457,7 @@ int __weak arch_show_interrupts(struct seq_file *p, 
int prec)
  }

  #ifndef ACTUAL_NR_IRQS
-# define ACTUAL_NR_IRQS nr_irqs
+# define ACTUAL_NR_IRQS irq_get_nr_irqs()
  #endif

  int show_interrupts(struct seq_file *p, void *v)
@@ -477,7 +477,8 @@ int show_interrupts(struct seq_file *p, void *v)

  	/* print header and calculate the width of the first column */
  	if (i == 0) {
-		for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
+		for (prec = 3, j = 1000;
+		     prec < 10 && j <= irq_get_nr_irqs(); ++prec)
  			j *= 10;

  		seq_printf(p, "%*s", prec + 8, "");