Skip to contentSkip to conversation

xen: EOI on the correct GICC interface

PATCH In review Stefano Stabellini 4 participants 11 messages May 3, 2013

lore
Share & tools
lore
#0 lore raw

cover xen: EOI on the correct GICC interface

Hi all,
this small patch series fixes a serious issue spotted by Julien reading the GIC code: if the maintenance interrupt is received on a pcpu different from the one that received the original irq (for example because the vcpu has been migrated), Xen executes the irq EOI on the wrong GICC interface.

Fix the issue issuing an SGI.

The patch series is based on Julien's "xen/arm: implement smp_call_function".

Stefano Stabellini (2):
xen: allow on_selected_cpus with interrupts disabled xen/gic: EOI irqs on the right pcpu

 xen/arch/arm/gic.c           |   15 ++++++++++++++-
 xen/arch/arm/vgic.c          |    7 ++++++-
 xen/common/smp.c             |    7 +++----
 xen/include/asm-arm/domain.h |    2 ++
 4 files changed, 25 insertions(+), 6 deletions(-)

Cheers,

Stefano

#1 lore raw

PATCH 1/2 xen: allow on_selected_cpus with interrupts disabled

Allow on_selected_cpus with interrupts disabled, use it with care.

---
 xen/common/smp.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)
xen/common/smp.c +3 -4
index dcd93ad..7deb97c 100644
@@ -33,10 +33,9 @@ void on_selected_cpus(
     int wait)
 {
     unsigned int nr_cpus;
+    unsigned long flags;
 
-    ASSERT(local_irq_is_enabled());
-
-    spin_lock(&call_lock);
+    spin_lock_irqsave(&call_lock, flags);
 
     cpumask_copy(&call_data.selected, selected);
 
@@ -54,7 +53,7 @@ void on_selected_cpus(
         cpu_relax();
 
 out:
-    spin_unlock(&call_lock);
+    spin_unlock_irqrestore(&call_lock, flags);
 }
 
 void smp_call_function_interrupt(void)
Signature · 1 line
1.7.2.5
#2 lore raw

Re: [PATCH 1/2] xen: allow on_selected_cpus with interrupts disabled

Quoted 1 line · Stefano Stabellini

Allow on_selected_cpus with interrupts disabled, use it with care.

This is a deadlock waiting to happen. Can we not find a way to do cross CPU EOI without it? If we can guarantee that we only need to EOI on one CPU then does that make a specialised SGI vector more plausible?

Can the IPI call not be moved outside the lock? i.e. remove it from the list under the lock and then IPI outside?

Or could you queue the IRQ on a per-pcpu list of IRQs to EOI and then outside the lock send an IPI to the other CPU to check the list.

At the least this should assert that he current cpu isn't in the mask when wait == 1.

Quoted 32 lines
---
 xen/common/smp.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)
xen/common/smp.c +3 -4
index dcd93ad..7deb97c 100644
@@ -33,10 +33,9 @@ void on_selected_cpus(
     int wait)
 {
     unsigned int nr_cpus;
+    unsigned long flags;
 
-    ASSERT(local_irq_is_enabled());
-
-    spin_lock(&call_lock);
+    spin_lock_irqsave(&call_lock, flags);
 
     cpumask_copy(&call_data.selected, selected);
 
@@ -54,7 +53,7 @@ void on_selected_cpus(
         cpu_relax();
 
 out:
-    spin_unlock(&call_lock);
+    spin_unlock_irqrestore(&call_lock, flags);
 }
 
 void smp_call_function_interrupt(void)
#3 lore raw

Re: [PATCH 1/2] xen: allow on_selected_cpus with interrupts disabled

Quoted 7 lines · Ian Campbell
Quoted 1 line · Stefano Stabellini

Allow on_selected_cpus with interrupts disabled, use it with care.

This is a deadlock waiting to happen. Can we not find a way to do cross CPU EOI without it? If we can guarantee that we only need to EOI on one CPU then does that make a specialised SGI vector more plausible?

Quoted 8 lines

Can the IPI call not be moved outside the lock? i.e. remove it from the list under the lock and then IPI outside?

Or could you queue the IRQ on a per-pcpu list of IRQs to EOI and then outside the lock send an IPI to the other CPU to check the list.

At the least this should assert that he current cpu isn't in the mask when wait == 1.

There's little chance of me being flexible on changing the on_selected_cpus() interface. This may be better handled under arch/arm, or with a new interface, or just as you sugegst rethinking the higher-level problem so you don't get painted into this corner in the first place.

Quoted 38 lines
Quoted 23 lines
---
 xen/common/smp.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)
xen/common/smp.c +2 -3
index dcd93ad..7deb97c 100644
@@ -33,10 +33,9 @@ void on_selected_cpus(
     int wait)
 {
     unsigned int nr_cpus;
+    unsigned long flags;
 
-    ASSERT(local_irq_is_enabled());
-
-    spin_lock(&call_lock);
+    spin_lock_irqsave(&call_lock, flags);
 
     cpumask_copy(&call_data.selected, selected);
 

xen/common/smp.c void on_selected_cpus( · line 59

Quoted 8 lines
xen/common/smp.c +1 -1
@@ -54,7 +53,7 @@ void on_selected_cpus(
         cpu_relax();
 
 out:
-    spin_unlock(&call_lock);
+    spin_unlock_irqrestore(&call_lock, flags);
 }
 
 void smp_call_function_interrupt(void)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#4 lore raw

Re: [PATCH 1/2] xen: allow on_selected_cpus with interrupts disabled

Quoted 12 lines · Keir Fraser
Quoted 10 lines · Ian Campbell
Quoted 1 line · Stefano Stabellini

Allow on_selected_cpus with interrupts disabled, use it with care.

This is a deadlock waiting to happen. Can we not find a way to do cross CPU EOI without it? If we can guarantee that we only need to EOI on one CPU then does that make a specialised SGI vector more plausible?

Can the IPI call not be moved outside the lock? i.e. remove it from the list under the lock and then IPI outside?

Quoted 8 lines
Quoted 5 lines

Or could you queue the IRQ on a per-pcpu list of IRQs to EOI and then outside the lock send an IPI to the other CPU to check the list.

At the least this should assert that he current cpu isn't in the mask when wait == 1.

There's little chance of me being flexible on changing the on_selected_cpus() interface.

I hadn't noticed this was common code, but in any case I agree with you.

Quoted 3 lines

This may be better handled under arch/arm, or
with a new interface, or just as you sugegst rethinking the higher-level problem so you don't get painted into this corner in the first place.

ACK.

Ian.

#5 lore raw

Re: [PATCH 1/2] xen: allow on_selected_cpus with interrupts disabled

Quoted 6 lines · Ian Campbell
Quoted 2 lines · Keir Fraser

There's little chance of me being flexible on changing the on_selected_cpus() interface.

I hadn't noticed this was common code, but in any case I agree with you.

Which reminds me -- any chance you could Ack/Nack Julien's patch which moves the original code (i.e. without this change) to common code.

"[PATCH V2 2/2] xen/arm: implement smp_call_function" <[email protected]>

Ian.

#6 lore raw

Re: [PATCH 1/2] xen: allow on_selected_cpus with interrupts disabled

Quoted 6 lines · Ian Campbell
Quoted 5 lines · Ian Campbell
Quoted 2 lines · Keir Fraser

There's little chance of me being flexible on changing the on_selected_cpus() interface.

Quoted 7 lines
Quoted 1 line

I hadn't noticed this was common code, but in any case I agree with you.

Which reminds me -- any chance you could Ack/Nack Julien's patch which moves the original code (i.e. without this change) to common code.

"[PATCH V2 2/2] xen/arm: implement smp_call_function" <[email protected]>

Okay, I will take a look. :)

Quoted 2 lines

Ian.

#7 lore raw

Re: [PATCH 1/2] xen: allow on_selected_cpus with interrupts disabled

In reply to Ian Campbell
Quoted 9 lines · Ian Campbell
Quoted 1 line · Stefano Stabellini

Allow on_selected_cpus with interrupts disabled, use it with care.

This is a deadlock waiting to happen. Can we not find a way to do cross CPU EOI without it? If we can guarantee that we only need to EOI on one CPU then does that make a specialised SGI vector more plausible?

Can the IPI call not be moved outside the lock? i.e. remove it from the list under the lock and then IPI outside?

I think it can: after all if the physical IRQ has not beeing EOIed yet, we cannot receive a second one at this time with ot without the lock held.

#8 lore raw

Re: [PATCH 1/2] xen: allow on_selected_cpus with interrupts disabled

In reply to Stefano Stabellini
Quoted 3 lines · Stefano Stabellini

Allow on_selected_cpus with interrupts disabled, use it with care.

NACK. The potential for deadlock is obvious, unclear what 'with care' might entail.

-- Keir

Quoted 30 lines
---
 xen/common/smp.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)
xen/common/smp.c +3 -4
index dcd93ad..7deb97c 100644
@@ -33,10 +33,9 @@ void on_selected_cpus(
     int wait)
 {
     unsigned int nr_cpus;
+    unsigned long flags;
 
-    ASSERT(local_irq_is_enabled());
-
-    spin_lock(&call_lock);
+    spin_lock_irqsave(&call_lock, flags);
 
     cpumask_copy(&call_data.selected, selected);
 
@@ -54,7 +53,7 @@ void on_selected_cpus(
         cpu_relax();
 
 out:
-    spin_unlock(&call_lock);
+    spin_unlock_irqrestore(&call_lock, flags);
 }
 
 void smp_call_function_interrupt(void)
#9 lore raw

PATCH 2/2 xen/gic: EOI irqs on the right pcpu

In reply to Stefano Stabellini

We need to write the irq number to GICC_DIR on the physical cpu that previously received the interrupt, but currently we are doing it on the pcpu that received the maintenance interrupt. As a consequence if a vcpu is migrated to a different pcpu, the irq is going to be EOI'ed on the wrong pcpu.

This covers the case where dom0 vcpu0 is running on pcpu1 for example (you can test this scenario by using xl vcpu-pin).

---
 xen/arch/arm/gic.c           |   15 ++++++++++++++-
 xen/arch/arm/vgic.c          |    7 ++++++-
 xen/include/asm-arm/domain.h |    2 ++
 3 files changed, 22 insertions(+), 2 deletions(-)
xen/arch/arm/gic.c +14 -1
index 61de230..9514d1c 100644
@@ -723,6 +723,12 @@ int gicv_setup(struct domain *d)
                         gic.vbase);
 }
 
+static void gic_irq_eoi(void *info)
+{
+    int virq = *(int *)info;
+    GICC[GICC_DIR] = virq;
+}
+
 static void maintenance_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs)
 {
     int i = 0, virq;
@@ -753,8 +759,15 @@ static void maintenance_interrupt(int irq, void *dev_id, struct cpu_user_regs *r
         spin_lock_irq(&v->arch.vgic.lock);
         p = irq_to_pending(v, virq);
         if ( p->desc != NULL ) {
+            int cpu;
             p->desc->status &= ~IRQ_INPROGRESS;
-            GICC[GICC_DIR] = virq;
+            /* Assume only one pcpu needs an EOI */
+            cpu = cpumask_first(&p->eoimask);
+            cpumask_clear(&p->eoimask);
+            if ( cpu == smp_processor_id() )
+                gic_irq_eoi(&virq);
+            else
+                on_selected_cpus(cpumask_of(cpu), gic_irq_eoi, &virq, 0);
         }
         list_del_init(&p->inflight);
         spin_unlock_irq(&v->arch.vgic.lock);
xen/arch/arm/vgic.c +6 -1
index f9c1a6b..c5370d5 100644
@@ -676,9 +676,14 @@ void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int irq, int virtual)
     n->irq = irq;
     n->priority = priority;
     if (!virtual)
+    {
         n->desc = irq_to_desc(irq);
-    else
+        cpumask_clear(&n->eoimask);
+        /* Assume we received the IRQ on the current pcpu */
+        cpumask_set_cpu(smp_processor_id(), &n->eoimask);
+    } else {
         n->desc = NULL;
+    }
 
     /* the irq is enabled */
     if ( rank->ienable & (1 << (irq % 32)) )
xen/include/asm-arm/domain.h +2 -0
index cca7416..5561531 100644
@@ -3,6 +3,7 @@
 
 #include <xen/config.h>
 #include <xen/cache.h>
+#include <xen/cpumask.h>
 #include <xen/sched.h>
 #include <asm/page.h>
 #include <asm/p2m.h>
@@ -21,6 +22,7 @@ struct pending_irq
 {
     int irq;
     struct irq_desc *desc; /* only set it the irq corresponds to a physical irq */
+    cpumask_t eoimask;
     uint8_t priority;
     /* inflight is used to append instances of pending_irq to
      * vgic.inflight_irqs */
Signature · 1 line
1.7.2.5
#10 lore raw

Re: [PATCH 2/2] xen/gic: EOI irqs on the right pcpu

Quoted 37 lines · Stefano Stabellini

We need to write the irq number to GICC_DIR on the physical cpu that previously received the interrupt, but currently we are doing it on the pcpu that received the maintenance interrupt. As a consequence if a vcpu is migrated to a different pcpu, the irq is going to be EOI'ed on the wrong pcpu.

This covers the case where dom0 vcpu0 is running on pcpu1 for example (you can test this scenario by using xl vcpu-pin).

---
 xen/arch/arm/gic.c           |   15 ++++++++++++++-
 xen/arch/arm/vgic.c          |    7 ++++++-
 xen/include/asm-arm/domain.h |    2 ++
 3 files changed, 22 insertions(+), 2 deletions(-)
xen/arch/arm/gic.c +6 -0
index 61de230..9514d1c 100644
@@ -723,6 +723,12 @@ int gicv_setup(struct domain *d)
                         gic.vbase);
 }
 
+static void gic_irq_eoi(void *info)
+{
+    int virq = *(int *)info;
+    GICC[GICC_DIR] = virq;
+}
+
 static void maintenance_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs)
 {
     int i = 0, virq;
@@ -753,8 +759,15 @@ static void maintenance_interrupt(int irq, void *dev_id, struct cpu_user_regs *r
         spin_lock_irq(&v->arch.vgic.lock);
         p = irq_to_pending(v, virq);
         if ( p->desc != NULL ) {

xen/arch/arm/gic.c static void maintenance_interrupt(int irq, void *dev_id, struct cpu_user_regs *r · line 770

Quoted 10 lines
xen/arch/arm/gic.c +8 -1
+            int cpu;
             p->desc->status &= ~IRQ_INPROGRESS;
-            GICC[GICC_DIR] = virq;
+            /* Assume only one pcpu needs an EOI */
+            cpu = cpumask_first(&p->eoimask);
+            cpumask_clear(&p->eoimask);
+            if ( cpu == smp_processor_id() )
+                gic_irq_eoi(&virq);
+            else
+                on_selected_cpus(cpumask_of(cpu), gic_irq_eoi, &virq, 0);

There is a race condition here. You use a pointer to a local variable (virq) and you don't wait the completion of the function. So virq could be updated/removed before EOI.

Quoted 43 lines
xen/arch/arm/gic.c +0 -0
         }
         list_del_init(&p->inflight);
         spin_unlock_irq(&v->arch.vgic.lock);
xen/arch/arm/vgic.c +6 -1
index f9c1a6b..c5370d5 100644
@@ -676,9 +676,14 @@ void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int irq, int virtual)
     n->irq = irq;
     n->priority = priority;
     if (!virtual)
+    {
         n->desc = irq_to_desc(irq);
-    else
+        cpumask_clear(&n->eoimask);
+        /* Assume we received the IRQ on the current pcpu */
+        cpumask_set_cpu(smp_processor_id(), &n->eoimask);
+    } else {
         n->desc = NULL;
+    }
 
     /* the irq is enabled */
     if ( rank->ienable & (1 << (irq % 32)) )
xen/include/asm-arm/domain.h +2 -0
index cca7416..5561531 100644
@@ -3,6 +3,7 @@
 
 #include <xen/config.h>
 #include <xen/cache.h>
+#include <xen/cpumask.h>
 #include <xen/sched.h>
 #include <asm/page.h>
 #include <asm/p2m.h>
@@ -21,6 +22,7 @@ struct pending_irq
 {
     int irq;
     struct irq_desc *desc; /* only set it the irq corresponds to a physical irq */
+    cpumask_t eoimask;
     uint8_t priority;
     /* inflight is used to append instances of pending_irq to
      * vgic.inflight_irqs */
Signature · 1 line
Julien