Skip to contentSkip to conversation

hw/pci: prevent hotplug of devices on pcie-root-ports with wrong devfn address

PATCH Superseded Ani Sinha 3 participants 9 messages Jun 14, 2023 – Jun 15, 2023

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ani Sinha

author
#0 lore raw

PATCH 1/1 hw/pci: prevent hotplug of devices on pcie-root-ports with wrong devfn address

PCIE root ports only allow one device on slot 0/function 0. When hotplugging a device on a pcie root port, make sure that the device address passed is always 0x00 that represents slot 0 and function 0. Any other slot value and function value would be illegal on a root port.

CC: [email protected]
CC: [email protected]
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2128929

---
 hw/pci/pci.c | 9 +++++++++
 1 file changed, 9 insertions(+)

Note:
I tested this with both native and acpi hotplug enabled on pcie. The check seems to work on both.

(qemu) netdev_add socket,id=hostnet1,listen=:1234
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x2.0x5
Error: PCI: slot 2 function 5 is not valid for e1000e
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x0.0 (qemu) info network
net1: index=0,type=nic,model=e1000e,macaddr=00:11:22:33:44:03
\ hostnet1: index=0,type=socket,
(qemu) device_del net1
(qemu) info network
hostnet1: index=0,type=socket,

hw/pci/pci.c +9 -0
index bf38905b7d..bc39322dfd 100644
@@ -64,6 +64,7 @@ bool pci_available = true;
 static char *pcibus_get_dev_path(DeviceState *dev);
 static char *pcibus_get_fw_dev_path(DeviceState *dev);
 static void pcibus_reset(BusState *qbus);
+static bool pcie_has_upstream_port(PCIDevice *dev);
 
 static Property pci_props[] = {
     DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
@@ -1189,6 +1190,14 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,
                    name);
 
        return NULL;
+    } else if (dev->hotplugged &&
+               !pci_is_vf(pci_dev) &&
+               pcie_has_upstream_port(pci_dev) && devfn) {
+        /* For an upstream PCIE port, we only support one device at slot 0 */
+        error_setg(errp, "PCI: slot %d function %d is not valid for %s",
+                   PCI_SLOT(devfn), PCI_FUNC(devfn), name);
+        return NULL;
+
     }
 
     pci_dev->devfn = devfn;
Signature · 1 line
2.39.1
#1 lore raw
Quoted 41 lines · On Wed, 14 Jun 2023 18:01:50 +0530 Ani Sinha <[email protected]> wrote:

PCIE root ports only allow one device on slot 0/function 0. When hotplugging a device on a pcie root port, make sure that the device address passed is always 0x00 that represents slot 0 and function 0. Any other slot value and function value would be illegal on a root port.

CC: [email protected]
CC: [email protected]
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2128929

---
 hw/pci/pci.c | 9 +++++++++
 1 file changed, 9 insertions(+)

Note:
I tested this with both native and acpi hotplug enabled on pcie. The check seems to work on both.

(qemu) netdev_add socket,id=hostnet1,listen=:1234
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x2.0x5
Error: PCI: slot 2 function 5 is not valid for e1000e
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x0.0 (qemu) info network
net1: index=0,type=nic,model=e1000e,macaddr=00:11:22:33:44:03
\ hostnet1: index=0,type=socket,
(qemu) device_del net1
(qemu) info network
hostnet1: index=0,type=socket,

hw/pci/pci.c +1 -0
index bf38905b7d..bc39322dfd 100644
@@ -64,6 +64,7 @@ bool pci_available = true;
 static char *pcibus_get_dev_path(DeviceState *dev);
 static char *pcibus_get_fw_dev_path(DeviceState *dev);
 static void pcibus_reset(BusState *qbus);
+static bool pcie_has_upstream_port(PCIDevice *dev);
 
 static Property pci_props[] = {
     DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),

hw/pci/pci.c static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, · line 1196

Quoted 7 lines
hw/pci/pci.c +3 -0
@@ -1189,6 +1190,14 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,
                    name);
 
        return NULL;
+    } else if (dev->hotplugged &&
 
+               !pci_is_vf(pci_dev) &&
+               pcie_has_upstream_port(pci_dev) 

can you clarify what above 2 lines are here for?

you can't hotplug anything into upstream port (only downstream) and why _vf() is here also not clear.

Quoted 9 lines
hw/pci/pci.c +5 -0
 && devfn) {
+        /* For an upstream PCIE port, we only support one device at slot 0 */
+        error_setg(errp, "PCI: slot %d function %d is not valid for %s",
+                   PCI_SLOT(devfn), PCI_FUNC(devfn), name);
+        return NULL;
+
     }
 
     pci_dev->devfn = devfn;
author
#2 lore raw
Quoted 50 lines · Igor Mammedov
Quoted 43 lines · On Wed, 14 Jun 2023 18:01:50 +0530 Ani Sinha <[email protected]> wrote:

PCIE root ports only allow one device on slot 0/function 0. When hotplugging a device on a pcie root port, make sure that the device address passed is always 0x00 that represents slot 0 and function 0. Any other slot value and function value would be illegal on a root port.

CC: [email protected]
CC: [email protected]
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2128929

---
hw/pci/pci.c | 9 +++++++++

1 file changed, 9 insertions(+)

Note:
I tested this with both native and acpi hotplug enabled on pcie. The check seems to work on both.

(qemu) netdev_add socket,id=hostnet1,listen=:1234
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x2.0x5
Error: PCI: slot 2 function 5 is not valid for e1000e
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x0.0 (qemu) info network
net1: index=0,type=nic,model=e1000e,macaddr=00:11:22:33:44:03
\ hostnet1: index=0,type=socket,
(qemu) device_del net1
(qemu) info network
hostnet1: index=0,type=socket,

hw/pci/pci.c +1 -0
index bf38905b7d..bc39322dfd 100644
@@ -64,6 +64,7 @@ bool pci_available = true;
 static char *pcibus_get_dev_path(DeviceState *dev);
 static char *pcibus_get_fw_dev_path(DeviceState *dev);
 static void pcibus_reset(BusState *qbus);
+static bool pcie_has_upstream_port(PCIDevice *dev);
 
 static Property pci_props[] = {
    DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
@@ -1189,6 +1190,14 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,
                   name);
 
Quoted 8 lines

hw/pci/pci.c static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,

Quoted 5 lines
hw/pci/pci.c +3 -0
       return NULL;
+    } else if (dev->hotplugged &&
 
+               !pci_is_vf(pci_dev) &&
+               pcie_has_upstream_port(pci_dev) 

can you clarify what above 2 lines are here for?

you can't hotplug anything into upstream port (only downstream)

This code checks if the hot plugged device is being plugged into a pcie root port. For pcie root ports, only slot 0 can be used. Please see 3f1e1478db2d67098d9 . The conditional check just above this hunk therefore checks for unoccupied function 0 in slot 0 if the device is plugged into an upstream pcie root port. I think for hot plugs, only function 0 is allowed.

As for vf, I am not sure. I believe for vfs the restriction is not valid. I just kept the check based on the conditional just prior to it.

Quoted 11 lines

and why _vf() is here also not clear.

Quoted 9 lines
hw/pci/pci.c +5 -0
 && devfn) {
+        /* For an upstream PCIE port, we only support one device at slot 0 */
+        error_setg(errp, "PCI: slot %d function %d is not valid for %s",
+                   PCI_SLOT(devfn), PCI_FUNC(devfn), name);
+        return NULL;
+
    }
    pci_dev->devfn = devfn;
#3 lore raw
Quoted 53 lines · Ani Sinha
Quoted 53 lines · Igor Mammedov
Quoted 45 lines · On Wed, 14 Jun 2023 18:01:50 +0530 Ani Sinha <[email protected]> wrote:

PCIE root ports only allow one device on slot 0/function 0. When hotplugging a device on a pcie root port, make sure that the device address passed is always 0x00 that represents slot 0 and function 0. Any other slot value and function value would be illegal on a root port.

CC: [email protected]
CC: [email protected]
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2128929

---
hw/pci/pci.c | 9 +++++++++

1 file changed, 9 insertions(+)

Note:
I tested this with both native and acpi hotplug enabled on pcie. The check seems to work on both.

(qemu) netdev_add socket,id=hostnet1,listen=:1234
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x2.0x5
Error: PCI: slot 2 function 5 is not valid for e1000e
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x0.0 (qemu) info network
net1: index=0,type=nic,model=e1000e,macaddr=00:11:22:33:44:03
\ hostnet1: index=0,type=socket,
(qemu) device_del net1
(qemu) info network
hostnet1: index=0,type=socket,

hw/pci/pci.c +2 -0
index bf38905b7d..bc39322dfd 100644
@@ -64,6 +64,7 @@ bool pci_available = true;
 static char *pcibus_get_dev_path(DeviceState *dev);
 static char *pcibus_get_fw_dev_path(DeviceState *dev);
 static void pcibus_reset(BusState *qbus);
+static bool pcie_has_upstream_port(PCIDevice *dev);
 
 static Property pci_props[] = {
    DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
@@ -1189,6 +1190,14 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,
                   name);
 
       return NULL;
+    } else if (dev->hotplugged &&
Quoted 7 lines
Quoted 5 lines

hw/pci/pci.c static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, · line 1195

Quoted 2 lines
hw/pci/pci.c +2 -0
+               !pci_is_vf(pci_dev) &&
+               pcie_has_upstream_port(pci_dev) 

can you clarify what above 2 lines are here for?

you can't hotplug anything into upstream port (only downstream)

This code checks if the hot plugged device is being plugged into a pcie root port. For pcie root ports, only slot 0 can be used. Please see 3f1e1478db2d67098d9 . The conditional check just above this hunk therefore checks for unoccupied function 0 in slot 0 if the device is plugged into an upstream pcie root port. I think for hot plugs, only function 0 is allowed.

Not really. The way we hotplug a multifunction device is by first populating all functions != 0, then finally pupulating function 0 which is when the event triggers.

Quoted 14 lines

As for vf, I am not sure. I believe for vfs the restriction is not valid. I just kept the check based on the conditional just prior to it.

Quoted 11 lines

and why _vf() is here also not clear.

Quoted 9 lines
hw/pci/pci.c +5 -0
 && devfn) {
+        /* For an upstream PCIE port, we only support one device at slot 0 */
+        error_setg(errp, "PCI: slot %d function %d is not valid for %s",
+                   PCI_SLOT(devfn), PCI_FUNC(devfn), name);
+        return NULL;
+
    }
 
    pci_dev->devfn = devfn;
author
#4 lore raw
Quoted 62 lines · Michael S. Tsirkin
Quoted 57 lines · Ani Sinha
Quoted 57 lines · Igor Mammedov
Quoted 41 lines · On Wed, 14 Jun 2023 18:01:50 +0530 Ani Sinha <[email protected]> wrote:

PCIE root ports only allow one device on slot 0/function 0. When hotplugging a device on a pcie root port, make sure that the device address passed is always 0x00 that represents slot 0 and function 0. Any other slot value and function value would be illegal on a root port.

CC: [email protected]
CC: [email protected]
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2128929

---
hw/pci/pci.c | 9 +++++++++

1 file changed, 9 insertions(+)

Note:
I tested this with both native and acpi hotplug enabled on pcie. The check seems to work on both.

(qemu) netdev_add socket,id=hostnet1,listen=:1234
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x2.0x5
Error: PCI: slot 2 function 5 is not valid for e1000e
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x0.0 (qemu) info network
net1: index=0,type=nic,model=e1000e,macaddr=00:11:22:33:44:03
\ hostnet1: index=0,type=socket,
(qemu) device_del net1
(qemu) info network
hostnet1: index=0,type=socket,

hw/pci/pci.c +1 -0
index bf38905b7d..bc39322dfd 100644
@@ -64,6 +64,7 @@ bool pci_available = true;
 static char *pcibus_get_dev_path(DeviceState *dev);
 static char *pcibus_get_fw_dev_path(DeviceState *dev);
 static void pcibus_reset(BusState *qbus);
+static bool pcie_has_upstream_port(PCIDevice *dev);
 
 static Property pci_props[] = {
   DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),

hw/pci/pci.c static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, · line 1196

Quoted 7 lines
hw/pci/pci.c +3 -0
@@ -1189,6 +1190,14 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,
                  name);
 
      return NULL;
+    } else if (dev->hotplugged &&
 
+               !pci_is_vf(pci_dev) &&
+               pcie_has_upstream_port(pci_dev) 

can you clarify what above 2 lines are here for?

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

you can't hotplug anything into upstream port (only downstream)

This code checks if the hot plugged device is being plugged into a pcie root port. For pcie root ports, only slot 0 can be used. Please see 3f1e1478db2d67098d9 . The conditional check just above this hunk therefore checks for unoccupied function 0 in slot 0 if the device is plugged into an upstream pcie root port. I think for hot plugs, only function 0 is allowed.

Not really. The way we hotplug a multifunction device is by first populating all functions != 0, then finally pupulating function 0 which is when the event triggers.

Let me explain better ..

This check currently already exists:

    } else if (dev->hotplugged &&
               !pci_is_vf(pci_dev) &&
               pci_get_function_0(pci_dev)) {
        error_setg(errp, "PCI: slot %d function 0 already occupied by %s,"
                   " new func %s cannot be exposed to guest.",
                   PCI_SLOT(pci_get_function_0(pci_dev)->devfn),
                   pci_get_function_0(pci_dev)->name,
                   name);

       return NULL;
    }

This code checks for unoccupied function 0. If you look into pci_get_function_0(), it checks for slot 0 function 0 for devices that are plugged into pcie root port.

Quoted 15 lines
Quoted 15 lines

As for vf, I am not sure. I believe for vfs the restriction is not valid. I just kept the check based on the conditional just prior to it.

Quoted 11 lines

and why _vf() is here also not clear.

Quoted 9 lines
hw/pci/pci.c +5 -0
 && devfn) {
+        /* For an upstream PCIE port, we only support one device at slot 0 */
+        error_setg(errp, "PCI: slot %d function %d is not valid for %s",
+                   PCI_SLOT(devfn), PCI_FUNC(devfn), name);
+        return NULL;
+
   }
   pci_dev->devfn = devfn;
#5 lore raw
In reply to Ani Sinha
Quoted 1 line · Ani Sinha

PCIE root ports only allow one device on slot 0/function 0.

Why do you say this? PCI devices can be multifunction.

Quoted 57 lines

When hotplugging a
device on a pcie root port, make sure that the device address passed is always 0x00 that represents slot 0 and function 0. Any other slot value and function value would be illegal on a root port.

CC: [email protected]
CC: [email protected]
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2128929

---
 hw/pci/pci.c | 9 +++++++++
 1 file changed, 9 insertions(+)

Note:
I tested this with both native and acpi hotplug enabled on pcie. The check seems to work on both.

(qemu) netdev_add socket,id=hostnet1,listen=:1234
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x2.0x5
Error: PCI: slot 2 function 5 is not valid for e1000e
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x0.0 (qemu) info network
net1: index=0,type=nic,model=e1000e,macaddr=00:11:22:33:44:03
\ hostnet1: index=0,type=socket,
(qemu) device_del net1
(qemu) info network
hostnet1: index=0,type=socket,

hw/pci/pci.c +9 -0
index bf38905b7d..bc39322dfd 100644
@@ -64,6 +64,7 @@ bool pci_available = true;
 static char *pcibus_get_dev_path(DeviceState *dev);
 static char *pcibus_get_fw_dev_path(DeviceState *dev);
 static void pcibus_reset(BusState *qbus);
+static bool pcie_has_upstream_port(PCIDevice *dev);
 
 static Property pci_props[] = {
     DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
@@ -1189,6 +1190,14 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,
                    name);
 
        return NULL;
+    } else if (dev->hotplugged &&
+               !pci_is_vf(pci_dev) &&
+               pcie_has_upstream_port(pci_dev) && devfn) {
+        /* For an upstream PCIE port, we only support one device at slot 0 */
+        error_setg(errp, "PCI: slot %d function %d is not valid for %s",
+                   PCI_SLOT(devfn), PCI_FUNC(devfn), name);
+        return NULL;
+
     }
 
     pci_dev->devfn = devfn;
Signature · 1 line
2.39.1
author
#6 lore raw
Quoted 9 lines · Michael S. Tsirkin
Quoted 1 line · Ani Sinha

PCIE root ports only allow one device on slot 0/function 0.

Why do you say this? PCI devices can be multifunction.

Yeah you are right, the language needs correction. For hot plugging, I believe only function 0 is relevant. At least that is what it looks like from the code.

Quoted 59 lines
Quoted 58 lines

When hotplugging a
device on a pcie root port, make sure that the device address passed is always 0x00 that represents slot 0 and function 0. Any other slot value and function value would be illegal on a root port.

CC: [email protected]
CC: [email protected]
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2128929

---
hw/pci/pci.c | 9 +++++++++

1 file changed, 9 insertions(+)

Note:
I tested this with both native and acpi hotplug enabled on pcie. The check seems to work on both.

(qemu) netdev_add socket,id=hostnet1,listen=:1234
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x2.0x5
Error: PCI: slot 2 function 5 is not valid for e1000e
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x0.0 (qemu) info network
net1: index=0,type=nic,model=e1000e,macaddr=00:11:22:33:44:03
\ hostnet1: index=0,type=socket,
(qemu) device_del net1
(qemu) info network
hostnet1: index=0,type=socket,

hw/pci/pci.c +9 -0
index bf38905b7d..bc39322dfd 100644
@@ -64,6 +64,7 @@ bool pci_available = true;
 static char *pcibus_get_dev_path(DeviceState *dev);
 static char *pcibus_get_fw_dev_path(DeviceState *dev);
 static void pcibus_reset(BusState *qbus);
+static bool pcie_has_upstream_port(PCIDevice *dev);
 
 static Property pci_props[] = {
    DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
@@ -1189,6 +1190,14 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,
                   name);
 
       return NULL;
+    } else if (dev->hotplugged &&
+               !pci_is_vf(pci_dev) &&
+               pcie_has_upstream_port(pci_dev) && devfn) {
+        /* For an upstream PCIE port, we only support one device at slot 0 */
+        error_setg(errp, "PCI: slot %d function %d is not valid for %s",
+                   PCI_SLOT(devfn), PCI_FUNC(devfn), name);
+        return NULL;
+
    }
 
    pci_dev->devfn = devfn;
Signature · 1 line
2.39.1
#7 lore raw
Quoted 11 lines · Ani Sinha
Quoted 9 lines · Michael S. Tsirkin
Quoted 1 line · Ani Sinha

PCIE root ports only allow one device on slot 0/function 0.

Why do you say this? PCI devices can be multifunction.

Yeah you are right, the language needs correction. For hot plugging, I believe only function 0 is relevant. At least that is what it looks like from the code.

Adding function 0 is what makes it visible to guest.
But you can't block multifunction devices that would be a regression.

Quoted 59 lines
Quoted 59 lines
Quoted 58 lines

When hotplugging a
device on a pcie root port, make sure that the device address passed is always 0x00 that represents slot 0 and function 0. Any other slot value and function value would be illegal on a root port.

CC: [email protected]
CC: [email protected]
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2128929

---
hw/pci/pci.c | 9 +++++++++

1 file changed, 9 insertions(+)

Note:
I tested this with both native and acpi hotplug enabled on pcie. The check seems to work on both.

(qemu) netdev_add socket,id=hostnet1,listen=:1234
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x2.0x5
Error: PCI: slot 2 function 5 is not valid for e1000e
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x0.0 (qemu) info network
net1: index=0,type=nic,model=e1000e,macaddr=00:11:22:33:44:03
\ hostnet1: index=0,type=socket,
(qemu) device_del net1
(qemu) info network
hostnet1: index=0,type=socket,

hw/pci/pci.c +9 -0
index bf38905b7d..bc39322dfd 100644
@@ -64,6 +64,7 @@ bool pci_available = true;
 static char *pcibus_get_dev_path(DeviceState *dev);
 static char *pcibus_get_fw_dev_path(DeviceState *dev);
 static void pcibus_reset(BusState *qbus);
+static bool pcie_has_upstream_port(PCIDevice *dev);
 
 static Property pci_props[] = {
    DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
@@ -1189,6 +1190,14 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,
                   name);
 
       return NULL;
+    } else if (dev->hotplugged &&
+               !pci_is_vf(pci_dev) &&
+               pcie_has_upstream_port(pci_dev) && devfn) {
+        /* For an upstream PCIE port, we only support one device at slot 0 */
+        error_setg(errp, "PCI: slot %d function %d is not valid for %s",
+                   PCI_SLOT(devfn), PCI_FUNC(devfn), name);
+        return NULL;
+
    }
 
    pci_dev->devfn = devfn;
Signature · 1 line
2.39.1
author
#8 lore raw
Quoted 13 lines · Michael S. Tsirkin
Quoted 8 lines · Ani Sinha
Quoted 8 lines · Michael S. Tsirkin
Quoted 1 line · Ani Sinha

PCIE root ports only allow one device on slot 0/function 0.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Why do you say this? PCI devices can be multifunction.

Yeah you are right, the language needs correction. For hot plugging, I believe only function 0 is relevant. At least that is what it looks like from the code.

Adding function 0 is what makes it visible to guest.
But you can't block multifunction devices that would be a regression.

OK fair enough and I can make a small adjustment in the code.

Quoted 62 lines
Quoted 61 lines
Quoted 61 lines
Quoted 60 lines

When hotplugging a
device on a pcie root port, make sure that the device address passed is always 0x00 that represents slot 0 and function 0. Any other slot value and function value would be illegal on a root port.

CC: [email protected]
CC: [email protected]
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2128929

---
hw/pci/pci.c | 9 +++++++++

1 file changed, 9 insertions(+)

Note:
I tested this with both native and acpi hotplug enabled on pcie. The check seems to work on both.

(qemu) netdev_add socket,id=hostnet1,listen=:1234
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x2.0x5
Error: PCI: slot 2 function 5 is not valid for e1000e
(qemu) device_add e1000e,netdev=hostnet1,mac=00:11:22:33:44:03,id=net1,bus=pci.6,addr=0x0.0 (qemu) info network
net1: index=0,type=nic,model=e1000e,macaddr=00:11:22:33:44:03
\ hostnet1: index=0,type=socket,
(qemu) device_del net1
(qemu) info network
hostnet1: index=0,type=socket,

hw/pci/pci.c +9 -0
index bf38905b7d..bc39322dfd 100644
@@ -64,6 +64,7 @@ bool pci_available = true;
 static char *pcibus_get_dev_path(DeviceState *dev);
 static char *pcibus_get_fw_dev_path(DeviceState *dev);
 static void pcibus_reset(BusState *qbus);
+static bool pcie_has_upstream_port(PCIDevice *dev);
 
 static Property pci_props[] = {
   DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
@@ -1189,6 +1190,14 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,
                  name);
 
      return NULL;
+    } else if (dev->hotplugged &&
+               !pci_is_vf(pci_dev) &&
+               pcie_has_upstream_port(pci_dev) && devfn) {
+        /* For an upstream PCIE port, we only support one device at slot 0 */
+        error_setg(errp, "PCI: slot %d function %d is not valid for %s",
+                   PCI_SLOT(devfn), PCI_FUNC(devfn), name);
+        return NULL;
+
   }
 
   pci_dev->devfn = devfn;
Signature · 1 line
2.39.1