Skip to contentSkip to conversation

x86/io-apic: Loop changes

PATCH In review Jason Andryuk 2 participants 7 messages Oct 15, 2025 – Oct 17, 2025

lore
Share & tools
lore
#1 lore raw

PATCH v3 1/2 x86/apic: Avoid infinite loop in io_apic_level_ack_pending()

In reply to Jason Andryuk

io_apic_level_ack_pending() will end up in an infinite loop if entry->pin == -1. entry does not change, so it will keep reading -1.

Convert to a proper for loop so that continue works. Add a new helper, next_entry(), to handle advancing to the next irq_pin_list entry.

Noticed during code inspection. The infinite loop was not observed.

Fixes f821102450a1 ("x86: IRQ Migration logic enhancement.")

---
v3:
const on next_entry() parameter
Remove spaces inside for loop braces
Remove inner if (!entry) check
Expand commit message to state noticed during code inspection

v2:
continue (not break) for pin == -1.

I added the next_entry() helper since putting the expression in the for loop is a little cluttered. The helper can also be re-used for other instances within the file.

---
 xen/arch/x86/io_apic.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
xen/arch/x86/io_apic.c +9 -8
index c384f10c1b..c35d611ecf 100644
@@ -1586,20 +1586,24 @@ static int __init cf_check setup_ioapic_ack(const char *s)
 }
 custom_param("ioapic_ack", setup_ioapic_ack);
 
+static struct irq_pin_list *next_entry(const struct irq_pin_list *entry)
+{
+    if ( !entry->next )
+        return NULL;
+
+    return irq_2_pin + entry->next;
+}
+
 static bool io_apic_level_ack_pending(unsigned int irq)
 {
     struct irq_pin_list *entry;
     unsigned long flags;
 
     spin_lock_irqsave(&ioapic_lock, flags);
-    entry = &irq_2_pin[irq];
-    for (;;) {
+    for (entry = &irq_2_pin[irq]; entry; entry = next_entry(entry)) {
         unsigned int reg;
         int pin;
 
-        if (!entry)
-            break;
-
         pin = entry->pin;
         if (pin == -1)
             continue;
@@ -1609,9 +1613,6 @@ static bool io_apic_level_ack_pending(unsigned int irq)
             spin_unlock_irqrestore(&ioapic_lock, flags);
             return 1;
         }
-        if (!entry->next)
-            break;
-        entry = irq_2_pin + entry->next;
     }
     spin_unlock_irqrestore(&ioapic_lock, flags);
 
Signature · 1 line
2.51.0
#4 lore raw

PATCH v3 2/2 x86/io_apic: Use next_entry() in loops

In reply to Jason Andryuk

io_apic.c has a lot of ad-hoc for(;;) and while(1) loops for iterating over irq_pin_list entries. Replace them with a standardized for loop using next_entry() to advance entry.

---
 xen/arch/x86/io_apic.c | 49 ++++++++++++------------------------------
 1 file changed, 14 insertions(+), 35 deletions(-)
xen/arch/x86/io_apic.c +14 -35
index c35d611ecf..73b48a9cb8 100644
@@ -191,6 +191,14 @@ static void remove_pin_from_irq(unsigned int irq, int apic, int pin)
     irq_2_pin_free_entry = entry - irq_2_pin;
 }
 
+static struct irq_pin_list *next_entry(const struct irq_pin_list *entry)
+{
+    if ( !entry->next )
+        return NULL;
+
+    return irq_2_pin + entry->next;
+}
+
 /*
  * Reroute an IRQ to a different pin.
  */
@@ -200,15 +208,12 @@ static void __init replace_pin_at_irq(unsigned int irq,
 {
     struct irq_pin_list *entry = irq_2_pin + irq;
 
-    while (1) {
+    for (; entry; entry = next_entry(entry)) {
         if (entry->apic == oldapic && entry->pin == oldpin) {
             entry->apic = newapic;
             entry->pin = newpin;
             share_vector_maps(oldapic, newapic);
         }
-        if (!entry->next)
-            break;
-        entry = irq_2_pin + entry->next;
     }
 }
 
@@ -482,7 +487,7 @@ static void modify_IO_APIC_irq(unsigned int irq, unsigned int enable,
 {
     struct irq_pin_list *entry = irq_2_pin + irq;
 
-    for (;;) {
+    for (; entry; entry = next_entry(entry)) {
         unsigned int pin = entry->pin;
         struct IO_APIC_route_entry rte;
 
@@ -492,9 +497,6 @@ static void modify_IO_APIC_irq(unsigned int irq, unsigned int enable,
         rte.raw &= ~(uint64_t)disable;
         rte.raw |= enable;
         __ioapic_write_entry(entry->apic, pin, false, rte);
-        if (!entry->next)
-            break;
-        entry = irq_2_pin + entry->next;
     }
 }
 
@@ -545,14 +547,11 @@ static void __eoi_IO_APIC_irq(struct irq_desc *desc)
     struct irq_pin_list *entry = irq_2_pin + desc->irq;
     unsigned int pin, vector = desc->arch.vector;
 
-    for (;;) {
+    for (; entry; entry = next_entry(entry)) {
         pin = entry->pin;
         if (pin == -1)
             break;
         __io_apic_eoi(entry->apic, vector, pin);
-        if (!entry->next)
-            break;
-        entry = irq_2_pin + entry->next;
     }
 }
 
@@ -632,7 +631,7 @@ set_ioapic_affinity_irq(struct irq_desc *desc, const cpumask_t *mask)
         if ( !iommu_intremap || !x2apic_enabled )
             dest = SET_APIC_LOGICAL_ID(dest);
         entry = irq_2_pin + irq;
-        for (;;) {
+        for (; entry; entry = next_entry(entry)) {
             struct IO_APIC_route_entry rte;
 
             pin = entry->pin;
@@ -643,10 +642,6 @@ set_ioapic_affinity_irq(struct irq_desc *desc, const cpumask_t *mask)
             rte.dest.dest32 = dest;
             rte.vector = desc->arch.vector;
             __ioapic_write_entry(entry->apic, pin, false, rte);
-
-            if (!entry->next)
-                break;
-            entry = irq_2_pin + entry->next;
         }
     }
 
@@ -1308,12 +1303,8 @@ static void /*__init*/ __print_IO_APIC(bool boot)
         if (entry->pin < 0)
             continue;
         printk(KERN_DEBUG "IRQ%d ", irq_to_desc(i)->arch.vector);
-        for (;;) {
+        for (; entry; entry = next_entry(entry))
             printk("-> %d:%d", entry->apic, entry->pin);
-            if (!entry->next)
-                break;
-            entry = irq_2_pin + entry->next;
-        }
         printk("\n");
     }
 
@@ -1586,14 +1577,6 @@ static int __init cf_check setup_ioapic_ack(const char *s)
 }
 custom_param("ioapic_ack", setup_ioapic_ack);
 
-static struct irq_pin_list *next_entry(const struct irq_pin_list *entry)
-{
-    if ( !entry->next )
-        return NULL;
-
-    return irq_2_pin + entry->next;
-}
-
 static bool io_apic_level_ack_pending(unsigned int irq)
 {
     struct irq_pin_list *entry;
@@ -2415,7 +2398,7 @@ void dump_ioapic_irq_info(void)
 
         printk("    IRQ%3d Vec%3d:\n", irq, irq_to_vector(irq));
 
-        for ( ; ; )
+        for ( ; entry; entry = next_entry(entry))
         {
             pin = entry->pin;
 
@@ -2432,10 +2415,6 @@ void dump_ioapic_irq_info(void)
                    (x2apic_enabled && iommu_intremap) ? 8 : 2,
                    (x2apic_enabled && iommu_intremap) ?
                        rte.dest.dest32 : rte.dest.logical.logical_dest);
-
-            if ( entry->next == 0 )
-                break;
-            entry = &irq_2_pin[entry->next];
         }
     }
 }
Signature · 1 line
2.51.0
#0 lore raw

v3 cover x86/io-apic: Loop changes

The first patch is the v3 resubmission for 4.21.

The second patch shows the conversion of the rest of the file to use next_entry() in the loops. It is targetting 4.22.

$ bloat-o-meter xen/xen-syms.baseline xen/xen-syms.patch-1
add/remove: 0/0 grow/shrink: 1/0 up/down: 4/0 (4)
Function                                     old     new   delta
io_apic_level_ack_pending                    218     222      +4
Total: Before=3419485, After=3419489, chg +0.00%
$ bloat-o-meter xen/xen-syms.patch-1 xen/xen-syms.patch-2
add/remove: 1/0 grow/shrink: 0/7 up/down: 31/-62 (-31)
Function                                     old     new   delta
next_entry                                     -      31     +31
setup_IO_APIC                               3435    3430      -5
__print_IO_APIC                             1010    1004      -6
set_ioapic_affinity_irq                      288     281      -7
io_apic_level_ack_pending                    222     214      -8
eoi_IO_APIC_irq                              128     117     -11
dump_ioapic_irq_info                         850     838     -12
modify_IO_APIC_irq                           132     119     -13
Total: Before=3419489, After=3419458, chg -0.00%

Jason Andryuk (2):
x86/apic: Avoid infinite loop in io_apic_level_ack_pending() x86/io_apic: Use next_entry() in loops

 xen/arch/x86/io_apic.c | 50 +++++++++++++-----------------------------
 1 file changed, 15 insertions(+), 35 deletions(-)
Signature · 1 line
2.51.0
#2 lore raw

Re: [PATCH for-4.21 v3 1/2] x86/apic: Avoid infinite loop in io_apic_level_ack_pending()

In reply to Jason Andryuk
Quoted 33 lines · Jason Andryuk

io_apic_level_ack_pending() will end up in an infinite loop if entry->pin == -1. entry does not change, so it will keep reading -1.

Convert to a proper for loop so that continue works. Add a new helper, next_entry(), to handle advancing to the next irq_pin_list entry.

Noticed during code inspection. The infinite loop was not observed.

Fixes f821102450a1 ("x86: IRQ Migration logic enhancement.")

---
v3:
const on next_entry() parameter
Remove spaces inside for loop braces
Remove inner if (!entry) check
Expand commit message to state noticed during code inspection

v2:
continue (not break) for pin == -1.

I added the next_entry() helper since putting the expression in the for loop is a little cluttered. The helper can also be re-used for other instances within the file.

---
 xen/arch/x86/io_apic.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
xen/arch/x86/io_apic.c +0 -0
index c384f10c1b..c35d611ecf 100644

xen/arch/x86/io_apic.c static int __init cf_check setup_ioapic_ack(const char *s) · line 1595

Quoted 10 lines
xen/arch/x86/io_apic.c +7 -0
@@ -1586,20 +1586,24 @@ static int __init cf_check setup_ioapic_ack(const char *s)
 }
 custom_param("ioapic_ack", setup_ioapic_ack);
 
+static struct irq_pin_list *next_entry(const struct irq_pin_list *entry)
+{
+    if ( !entry->next )
+        return NULL;
+
+    return irq_2_pin + entry->next;
+}

When replying to the v2 thread I hadn't spotted yet that a v3 was already posted. As indicated, imo this name to too generic (now). I'd be happy to make adjustments while committing, as long as we can agree on some less generic name.

Jan

Acked-by on v3 2/2
#5 lore raw

Re: [PATCH for-4.22 v3 2/2] x86/io_apic: Use next_entry() in loops

In reply to Jason Andryuk
Quoted 14 lines · Jason Andryuk

io_apic.c has a lot of ad-hoc for(;;) and while(1) loops for iterating over irq_pin_list entries. Replace them with a standardized for loop using next_entry() to advance entry.

---
 xen/arch/x86/io_apic.c | 49 ++++++++++++------------------------------
 1 file changed, 14 insertions(+), 35 deletions(-)
xen/arch/x86/io_apic.c +0 -0
index c35d611ecf..73b48a9cb8 100644

xen/arch/x86/io_apic.c static void remove_pin_from_irq(unsigned int irq, int apic, int pin) · line 200

Quoted 10 lines
xen/arch/x86/io_apic.c +7 -0
@@ -191,6 +191,14 @@ static void remove_pin_from_irq(unsigned int irq, int apic, int pin)
     irq_2_pin_free_entry = entry - irq_2_pin;
 }
 
+static struct irq_pin_list *next_entry(const struct irq_pin_list *entry)
+{
+    if ( !entry->next )
+        return NULL;
+
+    return irq_2_pin + entry->next;
+}

Preferably with the function put in its final place right in patch 1:

Quoted 10 lines
xen/arch/x86/io_apic.c +1 -1
@@ -482,7 +487,7 @@ static void modify_IO_APIC_irq(unsigned int irq, unsigned int enable,
 {
     struct irq_pin_list *entry = irq_2_pin + irq;
 
-    for (;;) {
+    for (; entry; entry = next_entry(entry)) {
         unsigned int pin = entry->pin;
         struct IO_APIC_route_entry rte;
 

xen/arch/x86/io_apic.c static void modify_IO_APIC_irq(unsigned int irq, unsigned int enable, · line 501

Quoted 8 lines
xen/arch/x86/io_apic.c +0 -3
@@ -492,9 +497,6 @@ static void modify_IO_APIC_irq(unsigned int irq, unsigned int enable,
         rte.raw &= ~(uint64_t)disable;
         rte.raw |= enable;
         __ioapic_write_entry(entry->apic, pin, false, rte);
-        if (!entry->next)
-            break;
-        entry = irq_2_pin + entry->next;
     }
 }

I notice that within here there's also a "break" upon ->pin being -1. Seeing that io_apic_level_ack_pending() has continue there, I think we will want to be consistent. Which way isn't quite clear to me (yet).

xen/arch/x86/io_apic.c static void __eoi_IO_APIC_irq(struct irq_desc *desc) · line 553

Quoted 9 lines
xen/arch/x86/io_apic.c +1 -1
@@ -545,14 +547,11 @@ static void __eoi_IO_APIC_irq(struct irq_desc *desc)
     struct irq_pin_list *entry = irq_2_pin + desc->irq;
     unsigned int pin, vector = desc->arch.vector;
 
-    for (;;) {
+    for (; entry; entry = next_entry(entry)) {
         pin = entry->pin;
         if (pin == -1)
             break;

Same here.

Quoted 10 lines
xen/arch/x86/io_apic.c +1 -1
@@ -632,7 +631,7 @@ set_ioapic_affinity_irq(struct irq_desc *desc, const cpumask_t *mask)
         if ( !iommu_intremap || !x2apic_enabled )
             dest = SET_APIC_LOGICAL_ID(dest);
         entry = irq_2_pin + irq;
-        for (;;) {
+        for (; entry; entry = next_entry(entry)) {
             struct IO_APIC_route_entry rte;
 
             pin = entry->pin;

xen/arch/x86/io_apic.c set_ioapic_affinity_irq(struct irq_desc *desc, const cpumask_t *mask) · line 646

Quoted 9 lines
xen/arch/x86/io_apic.c +0 -4
@@ -643,10 +642,6 @@ set_ioapic_affinity_irq(struct irq_desc *desc, const cpumask_t *mask)
             rte.dest.dest32 = dest;
             rte.vector = desc->arch.vector;
             __ioapic_write_entry(entry->apic, pin, false, rte);
-
-            if (!entry->next)
-                break;
-            entry = irq_2_pin + entry->next;
         }
     }

And here.

Jan

#3 lore raw

Re: [PATCH for-4.21 v3 1/2] x86/apic: Avoid infinite loop in io_apic_level_ack_pending()

In reply to Jan Beulich
Quoted 37 lines · Jan Beulich
Quoted 36 lines · Jason Andryuk

io_apic_level_ack_pending() will end up in an infinite loop if entry->pin == -1. entry does not change, so it will keep reading -1.

Convert to a proper for loop so that continue works. Add a new helper, next_entry(), to handle advancing to the next irq_pin_list entry.

Noticed during code inspection. The infinite loop was not observed.

Fixes f821102450a1 ("x86: IRQ Migration logic enhancement.")

---
v3:
const on next_entry() parameter
Remove spaces inside for loop braces
Remove inner if (!entry) check
Expand commit message to state noticed during code inspection

v2:
continue (not break) for pin == -1.

I added the next_entry() helper since putting the expression in the for loop is a little cluttered. The helper can also be re-used for other instances within the file.

---
  xen/arch/x86/io_apic.c | 17 +++++++++--------
  1 file changed, 9 insertions(+), 8 deletions(-)
xen/arch/x86/io_apic.c +0 -0
index c384f10c1b..c35d611ecf 100644
@@ -1586,20 +1586,24 @@ static int __init cf_check setup_ioapic_ack(const char *s)
  }
  custom_param("ioapic_ack", setup_ioapic_ack);
 
Quoted 12 lines

xen/arch/x86/io_apic.c static int __init cf_check setup_ioapic_ack(const char *s) · line 1595

Quoted 7 lines
xen/arch/x86/io_apic.c +7 -0
+static struct irq_pin_list *next_entry(const struct irq_pin_list *entry)
+{
+    if ( !entry->next )
+        return NULL;
+
+    return irq_2_pin + entry->next;
+}

When replying to the v2 thread I hadn't spotted yet that a v3 was already posted. As indicated, imo this name to too generic (now). I'd be happy to make adjustments while committing, as long as we can agree on some less generic name.

pin_list_next() works for me.

Thanks,
Jason

#6 lore raw

Re: [PATCH for-4.22 v3 2/2] x86/io_apic: Use next_entry() in loops

In reply to Jan Beulich
Quoted 38 lines · Jan Beulich
Quoted 14 lines · Jason Andryuk

io_apic.c has a lot of ad-hoc for(;;) and while(1) loops for iterating over irq_pin_list entries. Replace them with a standardized for loop using next_entry() to advance entry.

---
  xen/arch/x86/io_apic.c | 49 ++++++++++++------------------------------
  1 file changed, 14 insertions(+), 35 deletions(-)
xen/arch/x86/io_apic.c +0 -0
index c35d611ecf..73b48a9cb8 100644

xen/arch/x86/io_apic.c static void remove_pin_from_irq(unsigned int irq, int apic, int pin) · line 200

Quoted 10 lines
xen/arch/x86/io_apic.c +7 -0
@@ -191,6 +191,14 @@ static void remove_pin_from_irq(unsigned int irq, int apic, int pin)
      irq_2_pin_free_entry = entry - irq_2_pin;
  }
 
+static struct irq_pin_list *next_entry(const struct irq_pin_list *entry)
+{
+    if ( !entry->next )
+        return NULL;
+
+    return irq_2_pin + entry->next;
+}

Preferably with the function put in its final place right in patch 1:

Quoted 9 lines
xen/arch/x86/io_apic.c +1 -1
@@ -482,7 +487,7 @@ static void modify_IO_APIC_irq(unsigned int irq, unsigned int enable,
  {
      struct irq_pin_list *entry = irq_2_pin + irq;
 
-    for (;;) {
+    for (; entry; entry = next_entry(entry)) {
          unsigned int pin = entry->pin;
          struct IO_APIC_route_entry rte;
 
Quoted 13 lines

xen/arch/x86/io_apic.c static void modify_IO_APIC_irq(unsigned int irq, unsigned int enable, · line 501

Quoted 9 lines
xen/arch/x86/io_apic.c +0 -3
@@ -492,9 +497,6 @@ static void modify_IO_APIC_irq(unsigned int irq, unsigned int enable,
          rte.raw &= ~(uint64_t)disable;
          rte.raw |= enable;
          __ioapic_write_entry(entry->apic, pin, false, rte);
-        if (!entry->next)
-            break;
-        entry = irq_2_pin + entry->next;
      }
  }

I notice that within here there's also a "break" upon ->pin being -1. Seeing that io_apic_level_ack_pending() has continue there, I think we will want to be consistent. Which way isn't quite clear to me (yet).

Right. I don't know. It seems like ->pin == -1 indicates an unused entry, so stopping in the case makes sense. I've wondered if io_apic_level_ack_pending() continues just in case an entry->next points to another pin to ack. i.e. it's not the expected case, but it also might help if entry->next points to something valid.

Regards,
Jason