Skip to contentSkip to conversation

virtio: Fix crash when sriov-pf is set for non-PCI-Express device

PATCH In review Kevin Wolf 2 participants 2 messages Dec 4, 2025

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH 1/1 virtio: Fix crash when sriov-pf is set for non-PCI-Express device

Setting the sriov-pf property on devices that aren't PCI Express causes an assertion failure:

    $ qemu-system-x86_64 \
        -blockdev null-co,node-name=null \
        -blockdev null-co,node-name=null2 \
        -device virtio-blk,drive=null,id=pf \
        -device virtio-blk,sriov-pf=pf,drive=null2
    qemu-system-x86_64: ../hw/pci/pcie.c:1062: void pcie_add_capability(PCIDevice *, uint16_t, uint8_t, uint16_t, uint16_t): Assertion `offset >= PCI_CONFIG_SPACE_SIZE' failed.

This is because proxy->last_pcie_cap_offset is only initialised to a non-zero value in virtio_pci_realize() if it's a PCI Express device, and then virtio_pci_device_plugged() still tries to use it.

To fix this, just skip the SR-IOV code for !pci_is_express(). Then the next thing pci_qdev_realize() does is call pcie_sriov_register_device(), which returns the appropriate error.

Fixes d0c280d3fac6 ('pcie_sriov: Make a PCI device with user-created VF ARI-capable')
---
 hw/virtio/virtio-pci.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
hw/virtio/virtio-pci.c +11 -9
index 99cb30fe595..c9a162728cd 100644
@@ -2183,15 +2183,17 @@ static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
                          PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
     }
 
-    if (pci_is_vf(&proxy->pci_dev)) {
-        pcie_ari_init(&proxy->pci_dev, proxy->last_pcie_cap_offset);
-        proxy->last_pcie_cap_offset += PCI_ARI_SIZEOF;
-    } else {
-        res = pcie_sriov_pf_init_from_user_created_vfs(
-            &proxy->pci_dev, proxy->last_pcie_cap_offset, errp);
-        if (res > 0) {
-            proxy->last_pcie_cap_offset += res;
-            virtio_add_feature(&vdev->host_features, VIRTIO_F_SR_IOV);
+    if (pci_is_express(&proxy->pci_dev)) {
+        if (pci_is_vf(&proxy->pci_dev)) {
+            pcie_ari_init(&proxy->pci_dev, proxy->last_pcie_cap_offset);
+            proxy->last_pcie_cap_offset += PCI_ARI_SIZEOF;
+        } else {
+            res = pcie_sriov_pf_init_from_user_created_vfs(
+                &proxy->pci_dev, proxy->last_pcie_cap_offset, errp);
+            if (res > 0) {
+                proxy->last_pcie_cap_offset += res;
+                virtio_add_feature(&vdev->host_features, VIRTIO_F_SR_IOV);
+            }
         }
     }
 }
Signature · 1 line
2.52.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · Kevin Wolf

Setting the sriov-pf property on devices that aren't PCI Express causes an assertion failure:

    $ qemu-system-x86_64 \
        -blockdev null-co,node-name=null \
        -blockdev null-co,node-name=null2 \
        -device virtio-blk,drive=null,id=pf \
        -device virtio-blk,sriov-pf=pf,drive=null2
    qemu-system-x86_64: ../hw/pci/pcie.c:1062: void pcie_add_capability(PCIDevice *, uint16_t, uint8_t, uint16_t, uint16_t): Assertion `offset >= PCI_CONFIG_SPACE_SIZE' failed.

This is because proxy->last_pcie_cap_offset is only initialised to a non-zero value in virtio_pci_realize() if it's a PCI Express device, and then virtio_pci_device_plugged() still tries to use it.

To fix this, just skip the SR-IOV code for !pci_is_express(). Then the next thing pci_qdev_realize() does is call pcie_sriov_register_device(), which returns the appropriate error.

Quoted 6 lines
Fixes d0c280d3fac6 ('pcie_sriov: Make a PCI device with user-created VF ARI-capable')
---
 hw/virtio/virtio-pci.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)