Skip to contentSkip to conversation

x86/ioapic: fix edge triggered interrupt migration

RFC PATCH In review Roger Pau Monne 2 participants 20 messages Apr 21, 2022 – Jul 5, 2022

lore
Share & tools
lore
#0 lore raw

cover x86/ioapic: fix edge triggered interrupt migration

Hello,

Following series attempts to solve the issue with IO-APIC edge triggered interrupts seeing an inconsistent RTE or IRTE when injected while being migrated.

It's currently RFC because some patches have post commit message notes, and because I'm not sure if patch 1 is really needed. I originally had the idea of suggesting to only backport patch 1 in order to fix the issue in older releases, while leaving the more complex (and thus error prone) IOMMU changes in unstable. Note however that patch 1 is just a workaround to prevent interrupts seeing inconsistent entries while being updated, masking the entry just makes the warning go away, but the interrupt will be lost.

Thanks, Roger.

Roger Pau Monne (6):
x86/ioapic: set disable hook for masking edge interrupts x86/ioapic: add a raw field to RTE struct x86/ioapic: RTE modifications must use ioapic_write_entry x86/iommu: pass full IO-APIC RTE for remapping table update amd/iommu: atomically update remapping entries when possible x86/ioapic: mask entry while updating

 xen/arch/x86/include/asm/io_apic.h       |  72 ++++++-------
 xen/arch/x86/include/asm/iommu.h         |   3 +-
 xen/arch/x86/io_apic.c                   |  81 ++++++++++-----
 xen/drivers/passthrough/amd/iommu.h      |   2 +-
 xen/drivers/passthrough/amd/iommu_intr.c | 115 +++++++--------------
 xen/drivers/passthrough/vtd/extern.h     |   2 +-
 xen/drivers/passthrough/vtd/intremap.c   | 125 +++++++++++------------
 xen/drivers/passthrough/x86/iommu.c      |   4 +-
 xen/include/xen/iommu.h                  |   3 +-
 9 files changed, 195 insertions(+), 212 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

RFC PATCH 1/6 x86/ioapic: set disable hook for masking edge interrupts

Allow disabling (masking) IO-APIC pins set to edge trigger mode. This is required in order to safely migrate such interrupts between CPUs, as the write to update the IO-APIC RTE (or the IRTE) is not done atomically, so there's a window where there's a mismatch between the destination CPU and the vector:

(XEN) CPU1: No irq handler for vector b5 (IRQ -11, LAPIC) (XEN) IRQ10 a=0002[0002,0008] v=bd[b5] t=IO-APIC-edge s=00000030

The main risk with masking edge triggered interrupts is losing them, but getting them injected while in the process of updating the RTE is equally harmful as the interrupts gets lost anyway.

---
 xen/arch/x86/io_apic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/io_apic.c +1 -1
index c086f40f63..2e5964640b 100644
@@ -1782,7 +1782,7 @@ static hw_irq_controller ioapic_edge_type = {
     .startup 	= startup_edge_ioapic_irq,
     .shutdown 	= irq_shutdown_none,
     .enable 	= unmask_IO_APIC_irq,
-    .disable 	= irq_disable_none,
+    .disable 	= mask_IO_APIC_irq,
     .ack 		= ack_edge_ioapic_irq,
     .set_affinity 	= set_ioapic_affinity_irq,
 };
Signature · 1 line
2.35.1
#2 lore raw

Re: [PATCH RFC 1/6] x86/ioapic: set disable hook for masking edge interrupts

Quoted 4 lines · Roger Pau Monne

Allow disabling (masking) IO-APIC pins set to edge trigger mode. This is required in order to safely migrate such interrupts between CPUs, as the write to update the IO-APIC RTE (or the IRTE) is not done atomically,

For IRTE on VT-d we use cmpxchg16b if available (i.e. virtually always).

Quoted 5 lines

so there's a window where there's a mismatch between the destination CPU and the vector:

(XEN) CPU1: No irq handler for vector b5 (IRQ -11, LAPIC) (XEN) IRQ10 a=0002[0002,0008] v=bd[b5] t=IO-APIC-edge s=00000030

I think this needs some further explanation, as we generally move edge IRQs only when an un-acked interrupt is in flight (and hence no further one can arrive).

Jan

Quoted 22 lines

The main risk with masking edge triggered interrupts is losing them, but getting them injected while in the process of updating the RTE is equally harmful as the interrupts gets lost anyway.

---
 xen/arch/x86/io_apic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/io_apic.c +1 -1
index c086f40f63..2e5964640b 100644
@@ -1782,7 +1782,7 @@ static hw_irq_controller ioapic_edge_type = {
     .startup 	= startup_edge_ioapic_irq,
     .shutdown 	= irq_shutdown_none,
     .enable 	= unmask_IO_APIC_irq,
-    .disable 	= irq_disable_none,
+    .disable 	= mask_IO_APIC_irq,
     .ack 		= ack_edge_ioapic_irq,
     .set_affinity 	= set_ioapic_affinity_irq,
 };
#3 lore raw

Re: [PATCH RFC 1/6] x86/ioapic: set disable hook for masking edge interrupts

Quoted 11 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne

Allow disabling (masking) IO-APIC pins set to edge trigger mode. This is required in order to safely migrate such interrupts between CPUs, as the write to update the IO-APIC RTE (or the IRTE) is not done atomically,

For IRTE on VT-d we use cmpxchg16b if available (i.e. virtually always).

Quoted 3 lines

so there's a window where there's a mismatch between the destination CPU and the vector:

Quoted 6 lines
Quoted 2 lines

(XEN) CPU1: No irq handler for vector b5 (IRQ -11, LAPIC) (XEN) IRQ10 a=0002[0002,0008] v=bd[b5] t=IO-APIC-edge s=00000030

I think this needs some further explanation, as we generally move edge IRQs only when an un-acked interrupt is in flight (and hence no further one can arrive).

A further one can arrive as soon as you modify either the vector or the destination fields of the IO-APIC RTE, as then the non-EOI'ed lapic vector is no longer there (because you have moved to a different destination or vector).

This is the issue with updating the IO-APIC RTE using two separate
writes: even when using interrupt remapping the IRTE cannot be atomically updated and there's a window where the interrupt is not masked, but the destination and vector fields are not in sync, because they reside in different parts of the RTE (destination is in the high 32bits, vector in the low 32bits part of the RTE).

Thanks, Roger.

#4 lore raw

Re: [PATCH RFC 1/6] x86/ioapic: set disable hook for masking edge interrupts

Quoted 15 lines · Roger Pau Monné
Quoted 14 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne

Allow disabling (masking) IO-APIC pins set to edge trigger mode. This is required in order to safely migrate such interrupts between CPUs, as the write to update the IO-APIC RTE (or the IRTE) is not done atomically,

For IRTE on VT-d we use cmpxchg16b if available (i.e. virtually always).

Quoted 5 lines

so there's a window where there's a mismatch between the destination CPU and the vector:

(XEN) CPU1: No irq handler for vector b5 (IRQ -11, LAPIC) (XEN) IRQ10 a=0002[0002,0008] v=bd[b5] t=IO-APIC-edge s=00000030

Quoted 8 lines
Quoted 3 lines

I think this needs some further explanation, as we generally move edge IRQs only when an un-acked interrupt is in flight (and hence no further one can arrive).

A further one can arrive as soon as you modify either the vector or the destination fields of the IO-APIC RTE, as then the non-EOI'ed lapic vector is no longer there (because you have moved to a different destination or vector).

Right - this is what I'm asking you to spell out in the description.

Jan

Quoted 9 lines

This is the issue with updating the IO-APIC RTE using two separate
writes: even when using interrupt remapping the IRTE cannot be atomically updated and there's a window where the interrupt is not masked, but the destination and vector fields are not in sync, because they reside in different parts of the RTE (destination is in the high 32bits, vector in the low 32bits part of the RTE).

Thanks, Roger.

#5 lore raw

RFC PATCH 2/6 x86/ioapic: add a raw field to RTE struct

In reply to Roger Pau Monne

No functional change intended.

---
 xen/arch/x86/include/asm/io_apic.h | 57 ++++++++++++++++--------------
 1 file changed, 30 insertions(+), 27 deletions(-)
xen/arch/x86/include/asm/io_apic.h +30 -27
index ef0878b09e..a558bb063c 100644
@@ -89,35 +89,38 @@ enum ioapic_irq_destination_types {
 };
 
 struct IO_APIC_route_entry {
-    unsigned int vector:8;
-    unsigned int delivery_mode:3; /*
-                                   * 000: FIXED
-                                   * 001: lowest prio
-                                   * 111: ExtINT
-                                   */
-    unsigned int dest_mode:1;     /* 0: physical, 1: logical */
-    unsigned int delivery_status:1;
-    unsigned int polarity:1;      /* 0: low, 1: high */
-    unsigned int irr:1;
-    unsigned int trigger:1;       /* 0: edge, 1: level */
-    unsigned int mask:1;          /* 0: enabled, 1: disabled */
-    unsigned int __reserved_2:15;
-
     union {
         struct {
-            unsigned int __reserved_1:24;
-            unsigned int physical_dest:4;
-            unsigned int __reserved_2:4;
-        } physical;
-
-        struct {
-            unsigned int __reserved_1:24;
-            unsigned int logical_dest:8;
-        } logical;
-
-        /* used when Interrupt Remapping with EIM is enabled */
-        unsigned int dest32;
-    } dest;
+            unsigned int vector:8;
+            unsigned int delivery_mode:3; /*
+                                           * 000: FIXED
+                                           * 001: lowest prio
+                                           * 111: ExtINT
+                                           */
+            unsigned int dest_mode:1;     /* 0: physical, 1: logical */
+            unsigned int delivery_status:1;
+            unsigned int polarity:1;      /* 0: low, 1: high */
+            unsigned int irr:1;
+            unsigned int trigger:1;       /* 0: edge, 1: level */
+            unsigned int mask:1;          /* 0: enabled, 1: disabled */
+            unsigned int __reserved_2:15;
+
+            union {
+                struct {
+                    unsigned int __reserved_1:24;
+                    unsigned int physical_dest:4;
+                    unsigned int __reserved_2:4;
+                } physical;
+
+                struct {
+                    unsigned int __reserved_1:24;
+                    unsigned int logical_dest:8;
+                } logical;
+                unsigned int dest32;
+            } dest;
+        };
+        uint64_t raw;
+    };
 };
 
 /*
Signature · 1 line
2.35.1
#6 lore raw

Re: [PATCH RFC 2/6] x86/ioapic: add a raw field to RTE struct

Quoted 3 lines · Roger Pau Monne

No functional change intended.

Once seeing the purpose (in a later patch, I suppose) I certainly don't mind. We do have a couple of literal initializers, though (see e.g. the top of ioapic_guest_write()). Do those still compile fine (warning free) even with old gcc?

Jan

Quoted 68 lines
xen/arch/x86/include/asm/io_apic.h +30 -27
@@ -89,35 +89,38 @@ enum ioapic_irq_destination_types {
 };
 
 struct IO_APIC_route_entry {
-    unsigned int vector:8;
-    unsigned int delivery_mode:3; /*
-                                   * 000: FIXED
-                                   * 001: lowest prio
-                                   * 111: ExtINT
-                                   */
-    unsigned int dest_mode:1;     /* 0: physical, 1: logical */
-    unsigned int delivery_status:1;
-    unsigned int polarity:1;      /* 0: low, 1: high */
-    unsigned int irr:1;
-    unsigned int trigger:1;       /* 0: edge, 1: level */
-    unsigned int mask:1;          /* 0: enabled, 1: disabled */
-    unsigned int __reserved_2:15;
-
     union {
         struct {
-            unsigned int __reserved_1:24;
-            unsigned int physical_dest:4;
-            unsigned int __reserved_2:4;
-        } physical;
-
-        struct {
-            unsigned int __reserved_1:24;
-            unsigned int logical_dest:8;
-        } logical;
-
-        /* used when Interrupt Remapping with EIM is enabled */
-        unsigned int dest32;
-    } dest;
+            unsigned int vector:8;
+            unsigned int delivery_mode:3; /*
+                                           * 000: FIXED
+                                           * 001: lowest prio
+                                           * 111: ExtINT
+                                           */
+            unsigned int dest_mode:1;     /* 0: physical, 1: logical */
+            unsigned int delivery_status:1;
+            unsigned int polarity:1;      /* 0: low, 1: high */
+            unsigned int irr:1;
+            unsigned int trigger:1;       /* 0: edge, 1: level */
+            unsigned int mask:1;          /* 0: enabled, 1: disabled */
+            unsigned int __reserved_2:15;
+
+            union {
+                struct {
+                    unsigned int __reserved_1:24;
+                    unsigned int physical_dest:4;
+                    unsigned int __reserved_2:4;
+                } physical;
+
+                struct {
+                    unsigned int __reserved_1:24;
+                    unsigned int logical_dest:8;
+                } logical;
+                unsigned int dest32;
+            } dest;
+        };
+        uint64_t raw;
+    };
 };
 
 /*
#7 lore raw

Re: [PATCH RFC 2/6] x86/ioapic: add a raw field to RTE struct

Quoted 9 lines · Jan Beulich
Quoted 3 lines · Roger Pau Monne

No functional change intended.

Once seeing the purpose (in a later patch, I suppose) I certainly don't mind. We do have a couple of literal initializers, though (see e.g. the top of ioapic_guest_write()). Do those still compile fine (warning free) even with old gcc?

Will likely need to test it with the gitlab CI.

Thanks, Roger.

#8 lore raw

RFC PATCH 3/6 x86/ioapic: RTE modifications must use ioapic_write_entry

In reply to Roger Pau Monne

Do not allow to write to RTE registers using io_apic_write and instead require changes to RTE to be performed using ioapic_write_entry.

This is in preparation for passing the full contents of the RTE to the IOMMU interrupt remapping handlers, so remapping entries for IO-APIC RTEs can be updated atomically when possible.

---
 xen/arch/x86/include/asm/io_apic.h | 15 ++----------
 xen/arch/x86/io_apic.c             | 37 +++++++++++++++---------------
 2 files changed, 20 insertions(+), 32 deletions(-)
xen/arch/x86/include/asm/io_apic.h +2 -13
index a558bb063c..c26261aecb 100644
@@ -161,22 +161,11 @@ static inline void __io_apic_write(unsigned int apic, unsigned int reg, unsigned
 
 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
 {
-    if ( ioapic_reg_remapped(reg) )
-        return iommu_update_ire_from_apic(apic, reg, value);
+    /* RTE writes must use ioapic_write_entry. */
+    BUG_ON(reg >= 0x10);
     __io_apic_write(apic, reg, value);
 }
 
-/*
- * Re-write a value: to be used for read-modify-write
- * cycles where the read already set up the index register.
- */
-static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
-{
-    if ( ioapic_reg_remapped(reg) )
-        return iommu_update_ire_from_apic(apic, reg, value);
-    *(IO_APIC_BASE(apic) + 4) = value;
-}
-
 /* 1 if "noapic" boot option passed */
 extern bool skip_ioapic_setup;
 extern bool ioapic_ack_new;
xen/arch/x86/io_apic.c +18 -19
index 2e5964640b..3a5e3b7872 100644
@@ -269,15 +269,15 @@ void __ioapic_write_entry(
 {
     union entry_union eu = { .entry = e };
 
-    if ( raw )
+    if ( raw || !iommu_intremap )
     {
         __io_apic_write(apic, 0x11 + 2 * pin, eu.w2);
         __io_apic_write(apic, 0x10 + 2 * pin, eu.w1);
     }
     else
     {
-        io_apic_write(apic, 0x11 + 2 * pin, eu.w2);
-        io_apic_write(apic, 0x10 + 2 * pin, eu.w1);
+        iommu_update_ire_from_apic(apic, 0x11 + 2 * pin, eu.w2);
+        iommu_update_ire_from_apic(apic, 0x10 + 2 * pin, eu.w1);
     }
 }
 
@@ -433,16 +433,17 @@ static void modify_IO_APIC_irq(unsigned int irq, unsigned int enable,
                                unsigned int disable)
 {
     struct irq_pin_list *entry = irq_2_pin + irq;
-    unsigned int pin, reg;
 
     for (;;) {
-        pin = entry->pin;
+        unsigned int pin = entry->pin;
+        struct IO_APIC_route_entry rte;
+
         if (pin == -1)
             break;
-        reg = io_apic_read(entry->apic, 0x10 + pin*2);
-        reg &= ~disable;
-        reg |= enable;
-        io_apic_modify(entry->apic, 0x10 + pin*2, reg);
+        rte = __ioapic_read_entry(entry->apic, pin, false);
+        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;
@@ -584,16 +585,16 @@ set_ioapic_affinity_irq(struct irq_desc *desc, const cpumask_t *mask)
             dest = SET_APIC_LOGICAL_ID(dest);
         entry = irq_2_pin + irq;
         for (;;) {
-            unsigned int data;
+            struct IO_APIC_route_entry rte;
+
             pin = entry->pin;
             if (pin == -1)
                 break;
 
-            io_apic_write(entry->apic, 0x10 + 1 + pin*2, dest);
-            data = io_apic_read(entry->apic, 0x10 + pin*2);
-            data &= ~IO_APIC_REDIR_VECTOR_MASK;
-            data |= MASK_INSR(desc->arch.vector, IO_APIC_REDIR_VECTOR_MASK);
-            io_apic_modify(entry->apic, 0x10 + pin*2, data);
+            rte = __ioapic_read_entry(entry->apic, pin, false);
+            rte.dest.dest32 = dest;
+            rte.vector = desc->arch.vector;
+            __ioapic_write_entry(entry->apic, pin, false, rte);
 
             if (!entry->next)
                 break;
@@ -2129,10 +2130,8 @@ void ioapic_resume(void)
             reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
             __io_apic_write(apic, 0, reg_00.raw);
         }
-        for (i = 0; i < nr_ioapic_entries[apic]; i++, entry++) {
-            __io_apic_write(apic, 0x11+2*i, *(((int *)entry)+1));
-            __io_apic_write(apic, 0x10+2*i, *(((int *)entry)+0));
-        }
+        for (i = 0; i < nr_ioapic_entries[apic]; i++, entry++)
+            __ioapic_write_entry(apic, i, true, *entry);
     }
     spin_unlock_irqrestore(&ioapic_lock, flags);
 }
Signature · 1 line
2.35.1
#9 lore raw

Re: [PATCH RFC 3/6] x86/ioapic: RTE modifications must use ioapic_write_entry

Quoted 2 lines · Roger Pau Monne

Do not allow to write to RTE registers using io_apic_write and instead require changes to RTE to be performed using ioapic_write_entry.

Hmm, this doubles the number of MMIO access in affected code fragments.

Quoted 13 lines
xen/arch/x86/include/asm/io_apic.h +2 -2
@@ -161,22 +161,11 @@ static inline void __io_apic_write(unsigned int apic, unsigned int reg, unsigned
 
 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
 {
-    if ( ioapic_reg_remapped(reg) )
-        return iommu_update_ire_from_apic(apic, reg, value);
+    /* RTE writes must use ioapic_write_entry. */
+    BUG_ON(reg >= 0x10);
     __io_apic_write(apic, reg, value);
 }
 

xen/arch/x86/include/asm/io_apic.h static inline void __io_apic_write(unsigned int apic, unsigned int reg, unsigned · line 178

Quoted 10 lines
xen/arch/x86/include/asm/io_apic.h +0 -10
-/*
- * Re-write a value: to be used for read-modify-write
- * cycles where the read already set up the index register.
- */
-static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
-{
-    if ( ioapic_reg_remapped(reg) )
-        return iommu_update_ire_from_apic(apic, reg, value);
-    *(IO_APIC_BASE(apic) + 4) = value;
-}

While the last caller goes away, I don't think this strictly needs to be dropped (but could just gain a BUG_ON() like you do a few lines up)?

Jan

#10 lore raw

Re: [PATCH RFC 3/6] x86/ioapic: RTE modifications must use ioapic_write_entry

Quoted 5 lines · Jan Beulich
Quoted 2 lines · Roger Pau Monne

Do not allow to write to RTE registers using io_apic_write and instead require changes to RTE to be performed using ioapic_write_entry.

Hmm, this doubles the number of MMIO access in affected code fragments.

But it does allow to simplify the IOMMU side quite a lot by no longer having to update the IRTE using two different calls. I'm quite sure it saves quite some accesses to the IOMMU RTE in the following patches.

Quoted 13 lines
Quoted 13 lines
xen/arch/x86/include/asm/io_apic.h +2 -2
@@ -161,22 +161,11 @@ static inline void __io_apic_write(unsigned int apic, unsigned int reg, unsigned
 
 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
 {
-    if ( ioapic_reg_remapped(reg) )
-        return iommu_update_ire_from_apic(apic, reg, value);
+    /* RTE writes must use ioapic_write_entry. */
+    BUG_ON(reg >= 0x10);
     __io_apic_write(apic, reg, value);
 }
 
Quoted 13 lines

xen/arch/x86/include/asm/io_apic.h static inline void __io_apic_write(unsigned int apic, unsigned int reg, unsigned · line 178

Quoted 10 lines
xen/arch/x86/include/asm/io_apic.h +0 -10
-/*
- * Re-write a value: to be used for read-modify-write
- * cycles where the read already set up the index register.
- */
-static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
-{
-    if ( ioapic_reg_remapped(reg) )
-        return iommu_update_ire_from_apic(apic, reg, value);
-    *(IO_APIC_BASE(apic) + 4) = value;
-}

While the last caller goes away, I don't think this strictly needs to be dropped (but could just gain a BUG_ON() like you do a few lines up)?

Hm, could do, but it won't be suitable to be used to modify RTEs anymore, and given that was it's only usage I didn't see much value for leaving it around.

Thanks, Roger.

#11 lore raw

Re: [PATCH RFC 3/6] x86/ioapic: RTE modifications must use ioapic_write_entry

Quoted 11 lines · Roger Pau Monné
Quoted 5 lines · Jan Beulich
Quoted 2 lines · Roger Pau Monne

Do not allow to write to RTE registers using io_apic_write and instead require changes to RTE to be performed using ioapic_write_entry.

Hmm, this doubles the number of MMIO access in affected code fragments.

But it does allow to simplify the IOMMU side quite a lot by no longer having to update the IRTE using two different calls. I'm quite sure it saves quite some accesses to the IOMMU RTE in the following patches.

Right. You may want to mention upsides and downsides (and the ultimate balance) in the description.

Quoted 27 lines
Quoted 13 lines
Quoted 13 lines
xen/arch/x86/include/asm/io_apic.h +2 -2
@@ -161,22 +161,11 @@ static inline void __io_apic_write(unsigned int apic, unsigned int reg, unsigned
 
 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
 {
-    if ( ioapic_reg_remapped(reg) )
-        return iommu_update_ire_from_apic(apic, reg, value);
+    /* RTE writes must use ioapic_write_entry. */
+    BUG_ON(reg >= 0x10);
     __io_apic_write(apic, reg, value);
 }
 
Quoted 13 lines

xen/arch/x86/include/asm/io_apic.h static inline void __io_apic_write(unsigned int apic, unsigned int reg, unsigned · line 178

Quoted 10 lines
xen/arch/x86/include/asm/io_apic.h +0 -10
-/*
- * Re-write a value: to be used for read-modify-write
- * cycles where the read already set up the index register.
- */
-static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
-{
-    if ( ioapic_reg_remapped(reg) )
-        return iommu_update_ire_from_apic(apic, reg, value);
-    *(IO_APIC_BASE(apic) + 4) = value;
-}

While the last caller goes away, I don't think this strictly needs to be dropped (but could just gain a BUG_ON() like you do a few lines up)?

Quoted 3 lines

Hm, could do, but it won't be suitable to be used to modify RTEs anymore, and given that was it's only usage I didn't see much value for leaving it around.

I could see room for use of it elsewhere, e.g. setup_ioapic_ids_from_mpc(), io_apic_get_unique_id() (albeit read and write may be a little far apart in both of them) or ioapic_resume(). Otoh one may argue its benefit is marginal, so with some extra justification I could also see the function go away at this occasion.

Jan

#12 lore raw

RFC PATCH 4/6 x86/iommu: pass full IO-APIC RTE for remapping table update

In reply to Roger Pau Monne

So that the remapping entry can be updated atomically when possible.

Doing such update atomically will avoid Xen having to mask the IO-APIC pin prior to performing any interrupt movements (ie: changing the destination and vector fields), as the interrupt remapping entry is always consistent.

This also simplifies some of the logic on both VT-d and AMD-Vi implementations, as having the full RTE available instead of half of it avoids to possibly read and update the missing other half from hardware.

While there remove the explicit zeroing of new_ire fields in ioapic_rte_to_remap_entry and initialize the variable at definition so all fields are zeroed. Note fields could be also initialized with final values at definition, but I found that likely too much to be done at this time.

---
There's some functional differences between VT-d and AMD-Vi that we might also want to address: the logic on failure from setting up the remap entry on VT-d unmasks the previous RTE, while on AMD-Vi the pin is left masked.

Note that certain combination of changes to the RTE are impossible to handle atomically. For example changing the vector and/or destination fields together with the triggering mode is impossible to be performed atomically (as the destination and vector is set in the IRTE, but the triggering mode is set in the RTE). Xen doesn't attempt to perform such changes in a single update to the RTE anyway, so it's fine.

Since the IOMMU code now also uses __ioapic_write_entry to update the RTE it might be helpful to introduce an explicit raw version of the function, as IOMMU always wants to write the provided raw value to the RTE.

---
 xen/arch/x86/include/asm/iommu.h         |   3 +-
 xen/arch/x86/io_apic.c                   |   5 +-
 xen/drivers/passthrough/amd/iommu.h      |   2 +-
 xen/drivers/passthrough/amd/iommu_intr.c |  84 ++-------------
 xen/drivers/passthrough/vtd/extern.h     |   2 +-
 xen/drivers/passthrough/vtd/intremap.c   | 125 +++++++++++------------
 xen/drivers/passthrough/x86/iommu.c      |   4 +-
 xen/include/xen/iommu.h                  |   3 +-
 8 files changed, 80 insertions(+), 148 deletions(-)
xen/arch/x86/include/asm/iommu.h +2 -1
index 9ccf4f8bdd..1d62b42e17 100644
@@ -97,7 +97,8 @@ struct iommu_init_ops {
 
 extern const struct iommu_init_ops *iommu_init_ops;
 
-void iommu_update_ire_from_apic(unsigned int apic, unsigned int reg, unsigned int value);
+void iommu_update_ire_from_apic(unsigned int apic, unsigned int reg,
+                                uint64_t rte);
 unsigned int iommu_read_apic_from_ire(unsigned int apic, unsigned int reg);
 int iommu_setup_hpet_msi(struct msi_desc *);
 
xen/arch/x86/io_apic.c +1 -4
index 3a5e3b7872..f61e56f3d1 100644
@@ -275,10 +275,7 @@ void __ioapic_write_entry(
         __io_apic_write(apic, 0x10 + 2 * pin, eu.w1);
     }
     else
-    {
-        iommu_update_ire_from_apic(apic, 0x11 + 2 * pin, eu.w2);
-        iommu_update_ire_from_apic(apic, 0x10 + 2 * pin, eu.w1);
-    }
+        iommu_update_ire_from_apic(apic, pin, e.raw);
 }
 
 static void ioapic_write_entry(
xen/drivers/passthrough/amd/iommu.h +1 -1
index 64e4bbf33c..35df504406 100644
@@ -298,7 +298,7 @@ int cf_check amd_iommu_free_intremap_table(
 unsigned int amd_iommu_intremap_table_order(
     const void *irt, const struct amd_iommu *iommu);
 void cf_check amd_iommu_ioapic_update_ire(
-    unsigned int apic, unsigned int reg, unsigned int value);
+    unsigned int apic, unsigned int pin, uint64_t raw);
 unsigned int cf_check amd_iommu_read_ioapic_from_ire(
     unsigned int apic, unsigned int reg);
 int cf_check amd_iommu_msi_msg_update_ire(
xen/drivers/passthrough/amd/iommu_intr.c +9 -75

150 lines not on this page · raw message

xen/drivers/passthrough/vtd/extern.h +1 -1
index 39602d1f88..032a7c3b42 100644
@@ -92,7 +92,7 @@ int cf_check intel_iommu_get_reserved_device_memory(
 unsigned int cf_check io_apic_read_remap_rte(
     unsigned int apic, unsigned int reg);
 void cf_check io_apic_write_remap_rte(
-    unsigned int apic, unsigned int reg, unsigned int value);
+    unsigned int apic, unsigned int ioapic_pin, uint64_t raw);
 
 struct msi_desc;
 struct msi_msg;
xen/drivers/passthrough/vtd/intremap.c +62 -63

155 lines not on this page · raw message

xen/drivers/passthrough/x86/iommu.c +2 -2
index d5bf4d3241..e0ad38a09d 100644
@@ -133,9 +133,9 @@ int iommu_enable_x2apic(void)
 }
 
 void iommu_update_ire_from_apic(
-    unsigned int apic, unsigned int reg, unsigned int value)
+    unsigned int apic, unsigned int idx, uint64_t rte)
 {
-    iommu_vcall(&iommu_ops, update_ire_from_apic, apic, reg, value);
+    iommu_vcall(&iommu_ops, update_ire_from_apic, apic, idx, rte);
 }
 
 unsigned int iommu_read_apic_from_ire(unsigned int apic, unsigned int reg)
xen/include/xen/iommu.h +2 -1
index 3a83981464..b619cd872b 100644
@@ -262,7 +262,8 @@ struct iommu_ops {
     int (*enable_x2apic)(void);
     void (*disable_x2apic)(void);
 
-    void (*update_ire_from_apic)(unsigned int apic, unsigned int reg, unsigned int value);
+    void (*update_ire_from_apic)(unsigned int apic, unsigned int idx,
+                                 uint64_t rte);
     unsigned int (*read_apic_from_ire)(unsigned int apic, unsigned int reg);
 
     int (*setup_hpet_msi)(struct msi_desc *);
Signature · 1 line
2.35.1
#13 lore raw

RFC PATCH 5/6 amd/iommu: atomically update remapping entries when possible

In reply to Roger Pau Monne

Doing so matches existing VT-d behavior, and does prevent having to disable the remapping entry or mask the IO-APIC pin prior to being updated, as the remap entry content is always consistent.

---
 xen/drivers/passthrough/amd/iommu_intr.c | 31 +++++++++++++++++++++---
 1 file changed, 28 insertions(+), 3 deletions(-)
xen/drivers/passthrough/amd/iommu_intr.c +28 -3
index feed1d1447..b24e703c75 100644
@@ -39,6 +39,7 @@ union irte32 {
 };
 
 union irte128 {
+    __uint128_t raw128;
     uint64_t raw[2];
     struct {
         bool remap_en:1;
@@ -222,6 +223,21 @@ static void update_intremap_entry(const struct amd_iommu *iommu,
             },
         };
 
+        if ( cpu_has_cx16 )
+        {
+            union irte128 old_irte = *entry.ptr128;
+            __uint128_t ret = cmpxchg16b(entry.ptr128, &old_irte, &irte);
+
+            /*
+             * In the above, we use cmpxchg16 to atomically update the 128-bit
+             * IRTE, and the hardware cannot update the IRTE behind us, so
+             * the return value of cmpxchg16 should be the same as old_ire.
+             * This ASSERT validate it.
+             */
+            ASSERT(ret == old_irte.raw128);
+            return;
+        }
+
         ASSERT(!entry.ptr128->full.remap_en);
         entry.ptr128->raw[1] = irte.raw[1];
         /*
@@ -299,7 +315,8 @@ static int update_intremap_entry_from_ioapic(
     entry = get_intremap_entry(iommu, req_id, offset);
 
     /* The RemapEn fields match for all formats. */
-    while ( iommu->enabled && entry.ptr32->flds.remap_en )
+    while ( iommu->enabled && entry.ptr32->flds.remap_en &&
+            !cpu_has_cx16 && iommu->ctrl.ga_en )
     {
         entry.ptr32->flds.remap_en = false;
         spin_unlock(lock);
@@ -366,8 +383,11 @@ void cf_check amd_iommu_ioapic_update_ire(
         fresh = true;
     }
 
-    /* mask the interrupt while we change the intremap table */
-    if ( !saved_mask )
+    /*
+     * Mask the interrupt while we change the intremap table if it can't be
+     * done atomically.
+     */
+    if ( !saved_mask && !cpu_has_cx16 && iommu->ctrl.ga_en )
     {
         old_rte.mask = 1;
         __ioapic_write_entry(apic, pin, true, old_rte);
@@ -383,6 +403,11 @@ void cf_check amd_iommu_ioapic_update_ire(
         /* Keep the entry masked. */
         printk(XENLOG_ERR "Remapping IO-APIC %#x pin %u failed (%d)\n",
                IO_APIC_ID(apic), pin, rc);
+        if ( !saved_mask && (cpu_has_cx16 || !iommu->ctrl.ga_en) )
+        {
+            old_rte.mask = 1;
+            __ioapic_write_entry(apic, pin, true, old_rte);
+        }
         return;
     }
 
Signature · 1 line
2.35.1
#14 lore raw

Re: [PATCH RFC 5/6] amd/iommu: atomically update remapping entries when possible

Quoted 15 lines · Roger Pau Monne
xen/drivers/passthrough/amd/iommu_intr.c +5 -2
@@ -366,8 +383,11 @@ void cf_check amd_iommu_ioapic_update_ire(
         fresh = true;
     }
 
-    /* mask the interrupt while we change the intremap table */
-    if ( !saved_mask )
+    /*
+     * Mask the interrupt while we change the intremap table if it can't be
+     * done atomically.
+     */
+    if ( !saved_mask && !cpu_has_cx16 && iommu->ctrl.ga_en )
     {
         old_rte.mask = 1;
         __ioapic_write_entry(apic, pin, true, old_rte);

xen/drivers/passthrough/amd/iommu_intr.c void cf_check amd_iommu_ioapic_update_ire( · line 412

Quoted 10 lines
xen/drivers/passthrough/amd/iommu_intr.c +5 -0
@@ -383,6 +403,11 @@ void cf_check amd_iommu_ioapic_update_ire(
         /* Keep the entry masked. */
         printk(XENLOG_ERR "Remapping IO-APIC %#x pin %u failed (%d)\n",
                IO_APIC_ID(apic), pin, rc);
+        if ( !saved_mask && (cpu_has_cx16 || !iommu->ctrl.ga_en) )
+        {
+            old_rte.mask = 1;
+            __ioapic_write_entry(apic, pin, true, old_rte);
+        }
         return;
     }

Iirc you have a question about this (wrt differing VT-d behavior) elsewhere. I'm not convinced of this masking. I could see it as a measure to prevent damage if an update was done partially, but I don't see such being possible here. Hence to me it would look more logical if the entry was simply left as is, leaving it to the caller to correctly deal with the failure. But of course that would first require telling the caller about the failure ...

Jan

#15 lore raw

RFC PATCH 6/6 x86/ioapic: mask entry while updating

In reply to Roger Pau Monne

When writing an IO-APIC RTE entry make sure incoming interrupts never see a partially updated entry, by masking the pin while doing the update when necessary. Add some logic to attempt to limit the number of writes.

With the masking now handled by __ioapic_write_entry itself when necessary, we can drop the setting of the disable hook for IO-APIC edge triggered interrupts.

---
Note this includes a revert of the first patch in the series.

---
 xen/arch/x86/io_apic.c | 45 +++++++++++++++++++++++++++++++++++++-----
 1 file changed, 40 insertions(+), 5 deletions(-)
xen/arch/x86/io_apic.c +40 -5
index f61e56f3d1..1860af7353 100644
@@ -267,12 +267,47 @@ void __ioapic_write_entry(
     unsigned int apic, unsigned int pin, bool raw,
     struct IO_APIC_route_entry e)
 {
-    union entry_union eu = { .entry = e };
-
     if ( raw || !iommu_intremap )
     {
-        __io_apic_write(apic, 0x11 + 2 * pin, eu.w2);
-        __io_apic_write(apic, 0x10 + 2 * pin, eu.w1);
+        union entry_union eu = { .entry = e };
+        union entry_union curr = {
+            .entry = __ioapic_read_entry(apic, pin, true),
+        };
+        bool masked = true;
+
+        if ( curr.entry.mask )
+        {
+            /*
+             * If pin is currently masked we can update the high part first
+             * without worrying about the RTE being in an inconsistent state.
+             */
+            if ( curr.w2 != eu.w2 )
+                __io_apic_write(apic, 0x11 + 2 * pin, eu.w2);
+            if ( curr.w1 != eu.w1 )
+                __io_apic_write(apic, 0x10 + 2 * pin, eu.w1);
+            return;
+        }
+
+        if ( curr.w1 != eu.w1 && curr.w2 != eu.w2 && !eu.entry.mask )
+        {
+            /*
+             * If updating both halves mask the entry while updating so
+             * interrupts are not injected with an inconsistent RTE.
+             */
+            eu.entry.mask = 1;
+            masked = false;
+        }
+
+        if ( curr.w1 != eu.w1 )
+            __io_apic_write(apic, 0x10 + 2 * pin, eu.w1);
+        if ( curr.w2 != eu.w2 )
+            __io_apic_write(apic, 0x11 + 2 * pin, eu.w2);
+
+        if ( !masked )
+        {
+            eu.entry.mask = 0;
+            __io_apic_write(apic, 0x10 + 2 * pin, eu.w1);
+        }
     }
     else
         iommu_update_ire_from_apic(apic, pin, e.raw);
@@ -1780,7 +1815,7 @@ static hw_irq_controller ioapic_edge_type = {
     .startup 	= startup_edge_ioapic_irq,
     .shutdown 	= irq_shutdown_none,
     .enable 	= unmask_IO_APIC_irq,
-    .disable 	= mask_IO_APIC_irq,
+    .disable 	= irq_disable_none,
     .ack 		= ack_edge_ioapic_irq,
     .set_affinity 	= set_ioapic_affinity_irq,
 };
Signature · 1 line
2.35.1
#16 lore raw

Re: [PATCH RFC 6/6] x86/ioapic: mask entry while updating

Quoted 41 lines · Roger Pau Monne
xen/arch/x86/io_apic.c +29 -4
@@ -267,12 +267,47 @@ void __ioapic_write_entry(
     unsigned int apic, unsigned int pin, bool raw,
     struct IO_APIC_route_entry e)
 {
-    union entry_union eu = { .entry = e };
-
     if ( raw || !iommu_intremap )
     {
-        __io_apic_write(apic, 0x11 + 2 * pin, eu.w2);
-        __io_apic_write(apic, 0x10 + 2 * pin, eu.w1);
+        union entry_union eu = { .entry = e };
+        union entry_union curr = {
+            .entry = __ioapic_read_entry(apic, pin, true),
+        };
+        bool masked = true;
+
+        if ( curr.entry.mask )
+        {
+            /*
+             * If pin is currently masked we can update the high part first
+             * without worrying about the RTE being in an inconsistent state.
+             */
+            if ( curr.w2 != eu.w2 )
+                __io_apic_write(apic, 0x11 + 2 * pin, eu.w2);
+            if ( curr.w1 != eu.w1 )
+                __io_apic_write(apic, 0x10 + 2 * pin, eu.w1);
+            return;
+        }
+
+        if ( curr.w1 != eu.w1 && curr.w2 != eu.w2 && !eu.entry.mask )
+        {
+            /*
+             * If updating both halves mask the entry while updating so
+             * interrupts are not injected with an inconsistent RTE.
+             */
+            eu.entry.mask = 1;
+            masked = false;
+        }
+

xen/arch/x86/io_apic.c void __ioapic_write_entry( · line 310

Quoted 10 lines
xen/arch/x86/io_apic.c +10 -0
+        if ( curr.w1 != eu.w1 )
+            __io_apic_write(apic, 0x10 + 2 * pin, eu.w1);
+        if ( curr.w2 != eu.w2 )
+            __io_apic_write(apic, 0x11 + 2 * pin, eu.w2);
+
+        if ( !masked )
+        {
+            eu.entry.mask = 0;
+            __io_apic_write(apic, 0x10 + 2 * pin, eu.w1);
+        }

For the write avoidance don't you want to hide differences in the r/o delivery_status field, e.g. by setting curr's to eu's after having read curr?

Jan

#17 lore raw
In reply to Roger Pau Monne
Quoted 6 lines · Roger Pau Monne

Hello,

Following series attempts to solve the issue with IO-APIC edge triggered interrupts seeing an inconsistent RTE or IRTE when injected while being migrated.

Quoted 8 lines

It's currently RFC because some patches have post commit message notes, and because I'm not sure if patch 1 is really needed. I originally had the idea of suggesting to only backport patch 1 in order to fix the issue in older releases, while leaving the more complex (and thus error prone) IOMMU changes in unstable. Note however that patch 1 is just a workaround to prevent interrupts seeing inconsistent entries while being updated, masking the entry just makes the warning go away, but the interrupt will be lost.

Ping?

Thanks, Roger.

#18 lore raw
Quoted 16 lines · Roger Pau Monné
Quoted 6 lines · Roger Pau Monne

Hello,

Following series attempts to solve the issue with IO-APIC edge triggered interrupts seeing an inconsistent RTE or IRTE when injected while being migrated.

Quoted 8 lines

It's currently RFC because some patches have post commit message notes, and because I'm not sure if patch 1 is really needed. I originally had the idea of suggesting to only backport patch 1 in order to fix the issue in older releases, while leaving the more complex (and thus error prone) IOMMU changes in unstable. Note however that patch 1 is just a workaround to prevent interrupts seeing inconsistent entries while being updated, masking the entry just makes the warning go away, but the interrupt will be lost.

Quoted 1 line

Ping?

Sorry, the usual thing with RFCs: They take lower priority than other work items. This series is certainly the first of the several pending RFC series which I mean to get to, but it's hard to predict when this would be.

Jan

#19 lore raw
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Roger Pau Monné
Quoted 14 lines · Roger Pau Monne

Hello,

Following series attempts to solve the issue with IO-APIC edge triggered interrupts seeing an inconsistent RTE or IRTE when injected while being migrated.

It's currently RFC because some patches have post commit message notes, and because I'm not sure if patch 1 is really needed. I originally had the idea of suggesting to only backport patch 1 in order to fix the issue in older releases, while leaving the more complex (and thus error prone) IOMMU changes in unstable. Note however that patch 1 is just a workaround to prevent interrupts seeing inconsistent entries while being updated, masking the entry just makes the warning go away, but the interrupt will be lost.

Quoted 6 lines
Quoted 1 line

Ping?

Sorry, the usual thing with RFCs: They take lower priority than other work items. This series is certainly the first of the several pending RFC series which I mean to get to, but it's hard to predict when this would be.

No problem, I don't think it's a super urgent issue: we have always handled interrupts this way and so far got no noticeable issues (apart from the log messages reported on the console).

There's a non-trivial amount of IOMMU code changes, so maybe the IOMMU maintainers could take a stab at those as a start?

Thanks, Roger.