Skip to contentSkip to conversation

vpci/msi: fix updating already bound MSI interrupts

PATCH In review Roger Pau Monne 3 participants 8 messages May 15, 2018 – May 16, 2018 1 message not in the archive

lore
Share & tools
lore
#5 lore raw

PATCH v2 2/3 vpci/msi: split code to bind pirq

In reply to Roger Pau Monne

And put it in a separate update function. This is required in order to improve binding of MSI PIRQs when using vPCI.

No functional change.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>

---
 xen/arch/x86/hvm/vmsi.c | 73 +++++++++++++++++++++++++----------------
 1 file changed, 45 insertions(+), 28 deletions(-)
xen/arch/x86/hvm/vmsi.c +45 -28
index 5ab7387d78..acadc23f8d 100644
@@ -663,6 +663,42 @@ void vpci_msi_arch_mask(struct vpci_msi *msi, const struct pci_dev *pdev,
     vpci_mask_pirq(pdev->domain, msi->arch.pirq + entry, mask);
 }
 
+static int vpci_msi_update(const struct pci_dev *pdev, uint32_t data,
+                           uint64_t address, unsigned int vectors,
+                           unsigned int pirq, uint32_t mask)
+{
+    unsigned int i;
+
+    ASSERT(pcidevs_locked());
+
+    for ( i = 0; i < vectors; i++ )
+    {
+        uint8_t vector = MASK_EXTR(data, MSI_DATA_VECTOR_MASK);
+        uint8_t vector_mask = 0xff >> (8 - fls(vectors) + 1);
+        struct xen_domctl_bind_pt_irq bind = {
+            .machine_irq = pirq + i,
+            .irq_type = PT_IRQ_TYPE_MSI,
+            .u.msi.gvec = (vector & ~vector_mask) |
+                          ((vector + i) & vector_mask),
+            .u.msi.gflags = msi_gflags(data, address, (mask >> i) & 1),
+        };
+        int rc = pt_irq_create_bind(pdev->domain, &bind);
+
+        if ( rc )
+        {
+            gdprintk(XENLOG_ERR,
+                     "%04x:%02x:%02x.%u: failed to bind PIRQ %u: %d\n",
+                     pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
+                     PCI_FUNC(pdev->devfn), pirq + i, rc);
+            while ( bind.machine_irq-- > pirq )
+                pt_irq_destroy_bind(pdev->domain, &bind);
+            return rc;
+        }
+    }
+
+    return 0;
+}
+
 static int vpci_msi_enable(const struct pci_dev *pdev, uint32_t data,
                            uint64_t address, unsigned int nr,
                            paddr_t table_base, uint32_t mask)
@@ -674,7 +710,7 @@ static int vpci_msi_enable(const struct pci_dev *pdev, uint32_t data,
         .table_base = table_base,
         .entry_nr = nr,
     };
-    unsigned int i, vectors = table_base ? 1 : nr;
+    unsigned vectors = table_base ? 1 : nr;
     int rc, pirq = INVALID_PIRQ;
 
     /* Get a PIRQ. */
@@ -690,36 +726,17 @@ static int vpci_msi_enable(const struct pci_dev *pdev, uint32_t data,
         return rc;
     }
 
-    for ( i = 0; i < vectors; i++ )
+    pcidevs_lock();
+    rc = vpci_msi_update(pdev, data, address, vectors, pirq, mask);
+    if ( rc )
     {
-        uint8_t vector = MASK_EXTR(data, MSI_DATA_VECTOR_MASK);
-        uint8_t vector_mask = 0xff >> (8 - fls(vectors) + 1);
-        struct xen_domctl_bind_pt_irq bind = {
-            .machine_irq = pirq + i,
-            .irq_type = PT_IRQ_TYPE_MSI,
-            .u.msi.gvec = (vector & ~vector_mask) |
-                          ((vector + i) & vector_mask),
-            .u.msi.gflags = msi_gflags(data, address, (mask >> i) & 1),
-        };
-
-        pcidevs_lock();
-        rc = pt_irq_create_bind(pdev->domain, &bind);
-        if ( rc )
-        {
-            gdprintk(XENLOG_ERR,
-                     "%04x:%02x:%02x.%u: failed to bind PIRQ %u: %d\n",
-                     pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
-                     PCI_FUNC(pdev->devfn), pirq + i, rc);
-            while ( bind.machine_irq-- > pirq )
-                pt_irq_destroy_bind(pdev->domain, &bind);
-            spin_lock(&pdev->domain->event_lock);
-            unmap_domain_pirq(pdev->domain, pirq);
-            spin_unlock(&pdev->domain->event_lock);
-            pcidevs_unlock();
-            return rc;
-        }
+        spin_lock(&pdev->domain->event_lock);
+        unmap_domain_pirq(pdev->domain, pirq);
+        spin_unlock(&pdev->domain->event_lock);
         pcidevs_unlock();
+        return rc;
     }
+    pcidevs_unlock();
 
     return pirq;
 }
Signature · 7 lines
2.17.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH v2 1/3 vpci/msi: fix unbind loop

In reply to Roger Pau Monne

The current unbind loop on failure in vpci_msi_enable is wrong and will only work correctly if the initial pirq is 0. Fix this by adding a proper bound.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>

---
 xen/arch/x86/hvm/vmsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/hvm/vmsi.c +1 -1
index 900d4f67d4..5ab7387d78 100644
@@ -710,7 +710,7 @@ static int vpci_msi_enable(const struct pci_dev *pdev, uint32_t data,
                      "%04x:%02x:%02x.%u: failed to bind PIRQ %u: %d\n",
                      pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
                      PCI_FUNC(pdev->devfn), pirq + i, rc);
-            while ( bind.machine_irq-- )
+            while ( bind.machine_irq-- > pirq )
                 pt_irq_destroy_bind(pdev->domain, &bind);
             spin_lock(&pdev->domain->event_lock);
             unmap_domain_pirq(pdev->domain, pirq);
Signature · 7 lines
2.17.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#7 lore raw

PATCH v2 3/3 vpci/msi: fix update of bound MSI interrupts

In reply to Roger Pau Monne

Current update process of already bound MSI interrupts is wrong because unmap_domain_pirq calls pci_disable_msi, which disables MSI interrupts on the device. On the other hand map_domain_pirq doesn't enable MSI, so the current update process of already enabled MSI entries is wrong because MSI control bit will be disabled by unmap_domain_pirq and not re-enabled by map_domain_pirq.

In order to fix this avoid unmapping the PIRQs and just update the binding of the PIRQ. A new arch helper to do that is introduced.

Note that MSI-X is not affected because unmap_domain_pirq only disables the MSI enable control bit for the MSI case, for MSI-X the bit is left untouched by unmap_domain_pirq.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>

---
 xen/arch/x86/hvm/vmsi.c | 23 +++++++++++++++++++++++
 xen/drivers/vpci/msi.c  |  3 +--
 xen/include/xen/vpci.h  |  2 ++
 3 files changed, 26 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/vmsi.c +23 -0
index acadc23f8d..3001d5c488 100644
@@ -699,6 +699,29 @@ static int vpci_msi_update(const struct pci_dev *pdev, uint32_t data,
     return 0;
 }
 
+int vpci_msi_arch_update(struct vpci_msi *msi, const struct pci_dev *pdev)
+{
+    int rc;
+
+    ASSERT(msi->arch.pirq != INVALID_PIRQ);
+
+    pcidevs_lock();
+    rc = vpci_msi_update(pdev, msi->data, msi->address, msi->vectors,
+                         msi->arch.pirq, msi->mask);
+    if ( rc )
+    {
+        spin_lock(&pdev->domain->event_lock);
+        unmap_domain_pirq(pdev->domain, msi->arch.pirq);
+        spin_unlock(&pdev->domain->event_lock);
+        pcidevs_unlock();
+        msi->arch.pirq = INVALID_PIRQ;
+        return rc;
+    }
+    pcidevs_unlock();
+
+    return 0;
+}
+
 static int vpci_msi_enable(const struct pci_dev *pdev, uint32_t data,
                            uint64_t address, unsigned int nr,
                            paddr_t table_base, uint32_t mask)
xen/drivers/vpci/msi.c +1 -2
index ad26c38a92..8f15ad7bf2 100644
@@ -87,8 +87,7 @@ static void update_msi(const struct pci_dev *pdev, struct vpci_msi *msi)
     if ( !msi->enabled )
         return;
 
-    vpci_msi_arch_disable(msi, pdev);
-    if ( vpci_msi_arch_enable(msi, pdev, msi->vectors) )
+    if ( vpci_msi_arch_update(msi, pdev) )
         msi->enabled = false;
 }
 
xen/include/xen/vpci.h +2 -0
index 72d2225a97..af2b8580ee 100644
@@ -159,6 +159,8 @@ int __must_check vpci_msi_arch_enable(struct vpci_msi *msi,
                                       const struct pci_dev *pdev,
                                       unsigned int vectors);
 void vpci_msi_arch_disable(struct vpci_msi *msi, const struct pci_dev *pdev);
+int __must_check vpci_msi_arch_update(struct vpci_msi *msi,
+                                      const struct pci_dev *pdev);
 void vpci_msi_arch_init(struct vpci_msi *msi);
 void vpci_msi_arch_print(const struct vpci_msi *msi);
 
Signature · 7 lines
2.17.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#0 lore raw

v2 cover vpci/msi: fix updating already bound MSI interrupts

Hello,

There's a bug in current vpci code for MSI emulation when updating an already bound interrupt. The code will disable and enable the interrupt in order to update the binding, which calls unmap_domain_pirq that disables the global MSI enable flag in the control register.

In order to fix this incorrect behavior introduce a new update helper that should be used to update the bindings of an already enabled group of MSI interrupts.

Thanks, Roger.

Roger Pau Monne (3):
vpci/msi: fix unbind loop
vpci/msi: split code to bind pirq
vpci/msi: fix update of bound MSI interrupts

 xen/arch/x86/hvm/vmsi.c | 96 +++++++++++++++++++++++++++++------------
 xen/drivers/vpci/msi.c  |  3 +-
 xen/include/xen/vpci.h  |  2 +
 3 files changed, 71 insertions(+), 30 deletions(-)
Signature · 7 lines
2.17.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] vpci/msi: fix unbind loop

In reply to Roger Pau Monne
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 15.05.18 at 16:10, <[email protected]> wrote:

The current unbind loop on failure in vpci_msi_enable is wrong and will only work correctly if the initial pirq is 0. Fix this by adding a proper bound.

Jürgen,

any thoughts on taking this one for 4.11?

Jan

Quoted 11 lines
xen/arch/x86/hvm/vmsi.c +1 -1
@@ -710,7 +710,7 @@ static int vpci_msi_enable(const struct pci_dev *pdev, 
 uint32_t data,
                      "%04x:%02x:%02x.%u: failed to bind PIRQ %u: %d\n",
                      pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
                      PCI_FUNC(pdev->devfn), pirq + i, rc);
-            while ( bind.machine_irq-- )
+            while ( bind.machine_irq-- > pirq )
                 pt_irq_destroy_bind(pdev->domain, &bind);
             spin_lock(&pdev->domain->event_lock);
Quoted 3 lines
             unmap_domain_pirq(pdev->domain, pirq);
Signature · 1 line
2.17.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by on v2 2/3
#6 lore raw

Re: [PATCH v2 2/3] vpci/msi: split code to bind pirq

In reply to Roger Pau Monne
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 15.05.18 at 16:10, <[email protected]> wrote:

And put it in a separate update function. This is required in order to improve binding of MSI PIRQs when using vPCI.

No functional change.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by on v2 3/3
#8 lore raw

Re: [PATCH v2 3/3] vpci/msi: fix update of bound MSI interrupts

In reply to Roger Pau Monne
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 15.05.18 at 16:10, <[email protected]> wrote:

Current update process of already bound MSI interrupts is wrong because unmap_domain_pirq calls pci_disable_msi, which disables MSI interrupts on the device. On the other hand map_domain_pirq doesn't enable MSI, so the current update process of already enabled MSI entries is wrong because MSI control bit will be disabled by unmap_domain_pirq and not re-enabled by map_domain_pirq.

Quoted 8 lines

In order to fix this avoid unmapping the PIRQs and just update the binding of the PIRQ. A new arch helper to do that is introduced.

Note that MSI-X is not affected because unmap_domain_pirq only disables the MSI enable control bit for the MSI case, for MSI-X the bit is left untouched by unmap_domain_pirq.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw

Re: [PATCH v2 1/3] vpci/msi: fix unbind loop

In reply to a message not in the archive
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 15.05.18 at 16:10, <[email protected]> wrote:

The current unbind loop on failure in vpci_msi_enable is wrong and will only work correctly if the initial pirq is 0. Fix this by adding a proper bound.

Quoted 8 lines
Quoted 2 lines

Jürgen,

any thoughts on taking this one for 4.11?

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel