Skip to contentSkip to conversation

vpci/msix: check for BARs enabled in vpci_make_msix_hole

PATCH Superseded Stewart Hildebrand 3 participants 5 messages Feb 24

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 vpci/msix: check for BARs enabled in vpci_make_msix_hole

A hotplugged PCI device may be added uninitialized. In particular, memory decoding might be disabled and the BARs might be zeroed. In this case, the BARs will not be mapped in p2m. However, vpci_make_msix_hole() unconditionally attempts to punch holes in p2m, leading to init_msix() failing:

(XEN) d0v0 0000:01:00.0: existing mapping (mfn: 1c1880 type: 0) at 0 clobbers MSIX MMIO area (XEN) d0 0000:01:00.0: init legacy cap 17 fail rc=-17, mask it

vpci_make_msix_hole() should only attempt to punch holes if the BARs containing the MSI-X/PBA tables are mapped in p2m. Introduce a helper for checking if a BAR is enabled, and add a check for the situation inside vpci_make_msix_hole().

Move the vpci_make_msix_hole() call within modify_decoding() to after setting ->enabled.

Fixes ee2eb6849d50 ("vpci: Refactor REGISTER_VPCI_INIT")

---
Pipeline: https://gitlab.com/xen-project/people/stewarthildebrand/xen/-/pipelines/2344925375

v1->v2:
* new title, was ("vpci/msix: conditionally invoke vpci_make_msix_hole")
* move BAR enabled check to inside vpci_make_msix_hole()
* introduce vmsix_table_bar_valid() helper
* move vpci_make_msix_hole() call within modify_decoding() to after setting ->enabled
* split typo fixup to separate patch

v1: https://lore.kernel.org/xen-devel/[email protected]/T/#t

---
 xen/drivers/vpci/header.c | 26 +++++++++++++-------------
 xen/drivers/vpci/msix.c   |  4 ++++
 xen/include/xen/vpci.h    |  6 ++++++
 3 files changed, 23 insertions(+), 13 deletions(-)
xen/drivers/vpci/header.c +13 -13
index 739a5f610e91..6a28e07a625b 100644
@@ -122,19 +122,6 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
     bool map = cmd & PCI_COMMAND_MEMORY;
     unsigned int i;
 
-    /*
-     * Make sure there are no mappings in the MSIX MMIO areas, so that accesses
-     * can be trapped (and emulated) by Xen when the memory decoding bit is
-     * enabled.
-     *
-     * FIXME: punching holes after the p2m has been set up might be racy for
-     * DomU usage, needs to be revisited.
-     */
-#ifdef CONFIG_HAS_PCI_MSI
-    if ( map && !rom_only && vpci_make_msix_hole(pdev) )
-        return;
-#endif
-
     for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
     {
         struct vpci_bar *bar = &header->bars[i];
@@ -164,6 +151,19 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
             bar->enabled = map;
     }
 
+    /*
+     * Make sure there are no mappings in the MSIX MMIO areas, so that accesses
+     * can be trapped (and emulated) by Xen when the memory decoding bit is
+     * enabled.
+     *
+     * FIXME: punching holes after the p2m has been set up might be racy for
+     * DomU usage, needs to be revisited.
+     */
+#ifdef CONFIG_HAS_PCI_MSI
+    if ( map && !rom_only && vpci_make_msix_hole(pdev) )
+        return;
+#endif
+
     if ( !rom_only )
     {
         pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
xen/drivers/vpci/msix.c +4 -0
index 516282205a53..142cfbae59d5 100644
@@ -598,6 +598,10 @@ int vpci_make_msix_hole(const struct pci_dev *pdev)
     if ( !pdev->vpci->msix )
         return 0;
 
+    if ( !vmsix_table_bar_valid(pdev->vpci, VPCI_MSIX_TABLE) &&
+         !vmsix_table_bar_valid(pdev->vpci, VPCI_MSIX_PBA) )
+        return 0;
+
     /* Make sure there's a hole for the MSIX table/PBA in the p2m. */
     for ( i = 0; i < ARRAY_SIZE(pdev->vpci->msix->tables); i++ )
     {
xen/include/xen/vpci.h +6 -0
index d6310104ea17..8ce730791080 100644
@@ -267,6 +267,12 @@ static inline paddr_t vmsix_table_addr(const struct vpci *vpci, unsigned int nr)
            (vpci->msix->tables[nr] & ~PCI_MSIX_BIRMASK);
 }
 
+static inline bool vmsix_table_bar_valid(const struct vpci *vpci,
+                                         unsigned int nr)
+{
+    return vpci->header.bars[vpci->msix->tables[nr] & PCI_MSIX_BIRMASK].enabled;
+}
+
 /*
  * Note regarding the size calculation of the PBA: the spec mentions "The last
  * QWORD will not necessarily be fully populated", so it implies that the PBA

base-commit: 5eb84d6c992cf4e81936872c441b649057947442

Signature · 1 line
2.53.0
#1 lore raw
Quoted 86 lines · Stewart Hildebrand

A hotplugged PCI device may be added uninitialized. In particular, memory decoding might be disabled and the BARs might be zeroed. In this case, the BARs will not be mapped in p2m. However, vpci_make_msix_hole() unconditionally attempts to punch holes in p2m, leading to init_msix() failing:

(XEN) d0v0 0000:01:00.0: existing mapping (mfn: 1c1880 type: 0) at 0 clobbers MSIX MMIO area (XEN) d0 0000:01:00.0: init legacy cap 17 fail rc=-17, mask it

vpci_make_msix_hole() should only attempt to punch holes if the BARs containing the MSI-X/PBA tables are mapped in p2m. Introduce a helper for checking if a BAR is enabled, and add a check for the situation inside vpci_make_msix_hole().

Move the vpci_make_msix_hole() call within modify_decoding() to after setting ->enabled.

Fixes ee2eb6849d50 ("vpci: Refactor REGISTER_VPCI_INIT")

---
Pipeline: https://gitlab.com/xen-project/people/stewarthildebrand/xen/-/pipelines/2344925375

v1->v2:
* new title, was ("vpci/msix: conditionally invoke vpci_make_msix_hole")
* move BAR enabled check to inside vpci_make_msix_hole()
* introduce vmsix_table_bar_valid() helper
* move vpci_make_msix_hole() call within modify_decoding() to after setting ->enabled
* split typo fixup to separate patch

v1: https://lore.kernel.org/xen-devel/[email protected]/T/#t

---
 xen/drivers/vpci/header.c | 26 +++++++++++++-------------
 xen/drivers/vpci/msix.c   |  4 ++++
 xen/include/xen/vpci.h    |  6 ++++++
 3 files changed, 23 insertions(+), 13 deletions(-)
xen/drivers/vpci/header.c +13 -13
index 739a5f610e91..6a28e07a625b 100644
@@ -122,19 +122,6 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
     bool map = cmd & PCI_COMMAND_MEMORY;
     unsigned int i;
 
-    /*
-     * Make sure there are no mappings in the MSIX MMIO areas, so that accesses
-     * can be trapped (and emulated) by Xen when the memory decoding bit is
-     * enabled.
-     *
-     * FIXME: punching holes after the p2m has been set up might be racy for
-     * DomU usage, needs to be revisited.
-     */
-#ifdef CONFIG_HAS_PCI_MSI
-    if ( map && !rom_only && vpci_make_msix_hole(pdev) )
-        return;
-#endif
-
     for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
     {
         struct vpci_bar *bar = &header->bars[i];
@@ -164,6 +151,19 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
             bar->enabled = map;
     }
 
+    /*
+     * Make sure there are no mappings in the MSIX MMIO areas, so that accesses
+     * can be trapped (and emulated) by Xen when the memory decoding bit is
+     * enabled.
+     *
+     * FIXME: punching holes after the p2m has been set up might be racy for
+     * DomU usage, needs to be revisited.
+     */
+#ifdef CONFIG_HAS_PCI_MSI
+    if ( map && !rom_only && vpci_make_msix_hole(pdev) )
+        return;
+#endif
+
     if ( !rom_only )
     {
         pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
xen/drivers/vpci/msix.c +0 -0
index 516282205a53..142cfbae59d5 100644

xen/drivers/vpci/msix.c int vpci_make_msix_hole(const struct pci_dev *pdev) · line 603

Quoted 6 lines
xen/drivers/vpci/msix.c +3 -0
@@ -598,6 +598,10 @@ int vpci_make_msix_hole(const struct pci_dev *pdev)
     if ( !pdev->vpci->msix )
         return 0;
 
+    if ( !vmsix_table_bar_valid(pdev->vpci, VPCI_MSIX_TABLE) &&
+         !vmsix_table_bar_valid(pdev->vpci, VPCI_MSIX_PBA) )
+        return 0;

What if one is enabled and the other isn't? Doesn't the check need to move ...

xen/drivers/vpci/msix.c int vpci_make_msix_hole(const struct pci_dev *pdev) · line 607

Quoted 3 lines
xen/drivers/vpci/msix.c +0 -0
     /* Make sure there's a hole for the MSIX table/PBA in the p2m. */
     for ( i = 0; i < ARRAY_SIZE(pdev->vpci->msix->tables); i++ )
     {

... into the loop, and then apparently also need mirroring in the hwdom-only loop further down?

Jan

#2 lore raw
Quoted 96 lines · Jan Beulich
Quoted 86 lines · Stewart Hildebrand

A hotplugged PCI device may be added uninitialized. In particular, memory decoding might be disabled and the BARs might be zeroed. In this case, the BARs will not be mapped in p2m. However, vpci_make_msix_hole() unconditionally attempts to punch holes in p2m, leading to init_msix() failing:

(XEN) d0v0 0000:01:00.0: existing mapping (mfn: 1c1880 type: 0) at 0 clobbers MSIX MMIO area (XEN) d0 0000:01:00.0: init legacy cap 17 fail rc=-17, mask it

vpci_make_msix_hole() should only attempt to punch holes if the BARs containing the MSI-X/PBA tables are mapped in p2m. Introduce a helper for checking if a BAR is enabled, and add a check for the situation inside vpci_make_msix_hole().

Move the vpci_make_msix_hole() call within modify_decoding() to after setting ->enabled.

Fixes ee2eb6849d50 ("vpci: Refactor REGISTER_VPCI_INIT")

---
Pipeline: https://gitlab.com/xen-project/people/stewarthildebrand/xen/-/pipelines/2344925375

v1->v2:
* new title, was ("vpci/msix: conditionally invoke vpci_make_msix_hole")
* move BAR enabled check to inside vpci_make_msix_hole()
* introduce vmsix_table_bar_valid() helper
* move vpci_make_msix_hole() call within modify_decoding() to after setting ->enabled
* split typo fixup to separate patch

v1: https://lore.kernel.org/xen-devel/[email protected]/T/#t

---
 xen/drivers/vpci/header.c | 26 +++++++++++++-------------
 xen/drivers/vpci/msix.c   |  4 ++++
 xen/include/xen/vpci.h    |  6 ++++++
 3 files changed, 23 insertions(+), 13 deletions(-)
xen/drivers/vpci/header.c +13 -13
index 739a5f610e91..6a28e07a625b 100644
@@ -122,19 +122,6 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
     bool map = cmd & PCI_COMMAND_MEMORY;
     unsigned int i;
 
-    /*
-     * Make sure there are no mappings in the MSIX MMIO areas, so that accesses
-     * can be trapped (and emulated) by Xen when the memory decoding bit is
-     * enabled.
-     *
-     * FIXME: punching holes after the p2m has been set up might be racy for
-     * DomU usage, needs to be revisited.
-     */
-#ifdef CONFIG_HAS_PCI_MSI
-    if ( map && !rom_only && vpci_make_msix_hole(pdev) )
-        return;
-#endif
-
     for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
     {
         struct vpci_bar *bar = &header->bars[i];
@@ -164,6 +151,19 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
             bar->enabled = map;
     }
 
+    /*
+     * Make sure there are no mappings in the MSIX MMIO areas, so that accesses
+     * can be trapped (and emulated) by Xen when the memory decoding bit is
+     * enabled.
+     *
+     * FIXME: punching holes after the p2m has been set up might be racy for
+     * DomU usage, needs to be revisited.
+     */
+#ifdef CONFIG_HAS_PCI_MSI
+    if ( map && !rom_only && vpci_make_msix_hole(pdev) )
+        return;
+#endif
+
     if ( !rom_only )
     {
         pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
xen/drivers/vpci/msix.c +0 -0
index 516282205a53..142cfbae59d5 100644

xen/drivers/vpci/msix.c int vpci_make_msix_hole(const struct pci_dev *pdev) · line 603

Quoted 6 lines
xen/drivers/vpci/msix.c +3 -0
@@ -598,6 +598,10 @@ int vpci_make_msix_hole(const struct pci_dev *pdev)
     if ( !pdev->vpci->msix )
         return 0;
 
+    if ( !vmsix_table_bar_valid(pdev->vpci, VPCI_MSIX_TABLE) &&
+         !vmsix_table_bar_valid(pdev->vpci, VPCI_MSIX_PBA) )
+        return 0;

What if one is enabled and the other isn't? Doesn't the check need to move ...

Quoted 5 lines

xen/drivers/vpci/msix.c int vpci_make_msix_hole(const struct pci_dev *pdev) · line 607

Quoted 3 lines
xen/drivers/vpci/msix.c +0 -0
     /* Make sure there's a hole for the MSIX table/PBA in the p2m. */
     for ( i = 0; i < ARRAY_SIZE(pdev->vpci->msix->tables); i++ )
     {

... into the loop,

Yes, will do.

Quoted 2 lines

and then apparently also need mirroring in the hwdom-only loop further down?

I don't think that's necessary, the logic there is idempotent, and it's not dealing with p2m.

Thanks for taking a look.

#3 lore raw
In reply to Stewart Hildebrand
Quoted 9 lines · Stewart Hildebrand

A hotplugged PCI device may be added uninitialized. In particular, memory decoding might be disabled and the BARs might be zeroed. In this case, the BARs will not be mapped in p2m. However, vpci_make_msix_hole() unconditionally attempts to punch holes in p2m, leading to init_msix() failing:

(XEN) d0v0 0000:01:00.0: existing mapping (mfn: 1c1880 type: 0) at 0 clobbers MSIX MMIO area (XEN) d0 0000:01:00.0: init legacy cap 17 fail rc=-17, mask it

Quoted 7 lines

vpci_make_msix_hole() should only attempt to punch holes if the BARs containing the MSI-X/PBA tables are mapped in p2m. Introduce a helper for checking if a BAR is enabled, and add a check for the situation inside vpci_make_msix_hole().

Move the vpci_make_msix_hole() call within modify_decoding() to after setting ->enabled.

I would maybe make it clear that the movement of the vpci_make_msix_hole() call in modify_decoding() is due to the extra requirement added in this patch that ->enabled must be set before calling the function.

"As a result of the newly introduced checks in vpci_make_msix_hole(), move the ..."

Or something along the lines.

Quoted 70 lines
Fixes ee2eb6849d50 ("vpci: Refactor REGISTER_VPCI_INIT")

---
Pipeline: https://gitlab.com/xen-project/people/stewarthildebrand/xen/-/pipelines/2344925375

v1->v2:
* new title, was ("vpci/msix: conditionally invoke vpci_make_msix_hole")
* move BAR enabled check to inside vpci_make_msix_hole()
* introduce vmsix_table_bar_valid() helper
* move vpci_make_msix_hole() call within modify_decoding() to after setting ->enabled
* split typo fixup to separate patch

v1: https://lore.kernel.org/xen-devel/[email protected]/T/#t

---
 xen/drivers/vpci/header.c | 26 +++++++++++++-------------
 xen/drivers/vpci/msix.c   |  4 ++++
 xen/include/xen/vpci.h    |  6 ++++++
 3 files changed, 23 insertions(+), 13 deletions(-)
xen/drivers/vpci/header.c +13 -13
index 739a5f610e91..6a28e07a625b 100644
@@ -122,19 +122,6 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
     bool map = cmd & PCI_COMMAND_MEMORY;
     unsigned int i;
 
-    /*
-     * Make sure there are no mappings in the MSIX MMIO areas, so that accesses
-     * can be trapped (and emulated) by Xen when the memory decoding bit is
-     * enabled.
-     *
-     * FIXME: punching holes after the p2m has been set up might be racy for
-     * DomU usage, needs to be revisited.
-     */
-#ifdef CONFIG_HAS_PCI_MSI
-    if ( map && !rom_only && vpci_make_msix_hole(pdev) )
-        return;
-#endif
-
     for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
     {
         struct vpci_bar *bar = &header->bars[i];
@@ -164,6 +151,19 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
             bar->enabled = map;
     }
 
+    /*
+     * Make sure there are no mappings in the MSIX MMIO areas, so that accesses
+     * can be trapped (and emulated) by Xen when the memory decoding bit is
+     * enabled.
+     *
+     * FIXME: punching holes after the p2m has been set up might be racy for
+     * DomU usage, needs to be revisited.
+     */
+#ifdef CONFIG_HAS_PCI_MSI
+    if ( map && !rom_only && vpci_make_msix_hole(pdev) )
+        return;
+#endif
+
     if ( !rom_only )
     {
         pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
xen/drivers/vpci/msix.c +0 -0
index 516282205a53..142cfbae59d5 100644

xen/drivers/vpci/msix.c int vpci_make_msix_hole(const struct pci_dev *pdev) · line 603

Quoted 6 lines
xen/drivers/vpci/msix.c +3 -0
@@ -598,6 +598,10 @@ int vpci_make_msix_hole(const struct pci_dev *pdev)
     if ( !pdev->vpci->msix )
         return 0;
 
+    if ( !vmsix_table_bar_valid(pdev->vpci, VPCI_MSIX_TABLE) &&
+         !vmsix_table_bar_valid(pdev->vpci, VPCI_MSIX_PBA) )
+        return 0;

As Jan pointed out, this needs to be moved inside the loop.

Thanks, Roger.

#4 lore raw
Quoted 23 lines · Roger Pau Monné
Quoted 9 lines · Stewart Hildebrand

A hotplugged PCI device may be added uninitialized. In particular, memory decoding might be disabled and the BARs might be zeroed. In this case, the BARs will not be mapped in p2m. However, vpci_make_msix_hole() unconditionally attempts to punch holes in p2m, leading to init_msix() failing:

(XEN) d0v0 0000:01:00.0: existing mapping (mfn: 1c1880 type: 0) at 0 clobbers MSIX MMIO area (XEN) d0 0000:01:00.0: init legacy cap 17 fail rc=-17, mask it

Quoted 7 lines

vpci_make_msix_hole() should only attempt to punch holes if the BARs containing the MSI-X/PBA tables are mapped in p2m. Introduce a helper for checking if a BAR is enabled, and add a check for the situation inside vpci_make_msix_hole().

Move the vpci_make_msix_hole() call within modify_decoding() to after setting ->enabled.

I would maybe make it clear that the movement of the vpci_make_msix_hole() call in modify_decoding() is due to the extra requirement added in this patch that ->enabled must be set before calling the function.

Quoted 4 lines

"As a result of the newly introduced checks in vpci_make_msix_hole(), move the ..."

Or something along the lines.

Will do

Quoted 73 lines
Quoted 73 lines
Fixes ee2eb6849d50 ("vpci: Refactor REGISTER_VPCI_INIT")

---
Pipeline: https://gitlab.com/xen-project/people/stewarthildebrand/xen/-/pipelines/2344925375

v1->v2:
* new title, was ("vpci/msix: conditionally invoke vpci_make_msix_hole")
* move BAR enabled check to inside vpci_make_msix_hole()
* introduce vmsix_table_bar_valid() helper
* move vpci_make_msix_hole() call within modify_decoding() to after setting ->enabled
* split typo fixup to separate patch

v1: https://lore.kernel.org/xen-devel/[email protected]/T/#t

---
 xen/drivers/vpci/header.c | 26 +++++++++++++-------------
 xen/drivers/vpci/msix.c   |  4 ++++
 xen/include/xen/vpci.h    |  6 ++++++
 3 files changed, 23 insertions(+), 13 deletions(-)
xen/drivers/vpci/header.c +13 -13
index 739a5f610e91..6a28e07a625b 100644
@@ -122,19 +122,6 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
     bool map = cmd & PCI_COMMAND_MEMORY;
     unsigned int i;
 
-    /*
-     * Make sure there are no mappings in the MSIX MMIO areas, so that accesses
-     * can be trapped (and emulated) by Xen when the memory decoding bit is
-     * enabled.
-     *
-     * FIXME: punching holes after the p2m has been set up might be racy for
-     * DomU usage, needs to be revisited.
-     */
-#ifdef CONFIG_HAS_PCI_MSI
-    if ( map && !rom_only && vpci_make_msix_hole(pdev) )
-        return;
-#endif
-
     for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
     {
         struct vpci_bar *bar = &header->bars[i];
@@ -164,6 +151,19 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd,
             bar->enabled = map;
     }
 
+    /*
+     * Make sure there are no mappings in the MSIX MMIO areas, so that accesses
+     * can be trapped (and emulated) by Xen when the memory decoding bit is
+     * enabled.
+     *
+     * FIXME: punching holes after the p2m has been set up might be racy for
+     * DomU usage, needs to be revisited.
+     */
+#ifdef CONFIG_HAS_PCI_MSI
+    if ( map && !rom_only && vpci_make_msix_hole(pdev) )
+        return;
+#endif
+
     if ( !rom_only )
     {
         pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
xen/drivers/vpci/msix.c +0 -0
index 516282205a53..142cfbae59d5 100644
@@ -598,6 +598,10 @@ int vpci_make_msix_hole(const struct pci_dev *pdev)
     if ( !pdev->vpci->msix )
         return 0;
 
Quoted 5 lines

xen/drivers/vpci/msix.c int vpci_make_msix_hole(const struct pci_dev *pdev) · line 603

Quoted 3 lines
xen/drivers/vpci/msix.c +3 -0
+    if ( !vmsix_table_bar_valid(pdev->vpci, VPCI_MSIX_TABLE) &&
+         !vmsix_table_bar_valid(pdev->vpci, VPCI_MSIX_PBA) )
+        return 0;

As Jan pointed out, this needs to be moved inside the loop.

Will do.

Thanks!