Skip to contentSkip to conversation

hw/pci/pci: Fix slot check for plugged devices

PATCH In review Julia Suvorova 2 participants 3 messages Oct 6, 2020 – Oct 7, 2020

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hw/pci/pci: Fix slot check for plugged devices

If devfn is assigned automatically, 'else' clauses will never be executed. And if it does not matter for the reserved and available devfn, because we have already checked it, the check for function0 needs to be done again.

Steps to reproduce:
1. Run QEMU with:

        -M q35 \
        -device pcie-root-port,id=rp,.. \
        -device pci-device,bus=rp

2. Add a new device to the same port:
device_add pci-device,bus=rp

The last command will add the device to slot 1 instead of failing with "PCI: slot 0 function 0 already occupied..."

---
v2:
* add example to the commit message [Michael]

 hw/pci/pci.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/pci/pci.c +3 -2
index de0fae10ab..ae132b0b52 100644
@@ -1034,8 +1034,9 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,
                    PCI_SLOT(devfn), PCI_FUNC(devfn), name,
                    bus->devices[devfn]->name);
         return NULL;
-    } else if (dev->hotplugged &&
-               pci_get_function_0(pci_dev)) {
+    };
+
+    if (dev->hotplugged && pci_get_function_0(pci_dev)) {
         error_setg(errp, "PCI: slot %d function 0 already ocuppied by %s,"
                    " new func %s cannot be exposed to guest.",
                    PCI_SLOT(pci_get_function_0(pci_dev)->devfn),
Signature · 1 line
2.25.4
#1 lore raw
Quoted 29 lines · Julia Suvorova

If devfn is assigned automatically, 'else' clauses will never be executed. And if it does not matter for the reserved and available devfn, because we have already checked it, the check for function0 needs to be done again.

Steps to reproduce:
1. Run QEMU with:

        -M q35 \
        -device pcie-root-port,id=rp,.. \
        -device pci-device,bus=rp

2. Add a new device to the same port:
device_add pci-device,bus=rp

The last command will add the device to slot 1 instead of failing with "PCI: slot 0 function 0 already occupied..."

---
v2:
* add example to the commit message [Michael]

 hw/pci/pci.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/pci/pci.c +0 -0
index de0fae10ab..ae132b0b52 100644

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

Quoted 6 lines
hw/pci/pci.c +1 -2
@@ -1034,8 +1034,9 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,
                    PCI_SLOT(devfn), PCI_FUNC(devfn), name,
                    bus->devices[devfn]->name);
         return NULL;
-    } else if (dev->hotplugged &&
-               pci_get_function_0(pci_dev)) {
+    };
        ^

This semicolon seems unnecessary, can we take it out?

Thanks,
Stefano

Quoted 9 lines
hw/pci/pci.c +2 -0
+
+    if (dev->hotplugged && pci_get_function_0(pci_dev)) {
         error_setg(errp, "PCI: slot %d function 0 already ocuppied by %s,"
                    " new func %s cannot be exposed to guest.",
                    PCI_SLOT(pci_get_function_0(pci_dev)->devfn),
Signature · 1 line
2.25.4
#2 lore raw
Quoted 38 lines · Stefano Garzarella
Quoted 29 lines · Julia Suvorova

If devfn is assigned automatically, 'else' clauses will never be executed. And if it does not matter for the reserved and available devfn, because we have already checked it, the check for function0 needs to be done again.

Steps to reproduce:
1. Run QEMU with:

        -M q35 \
        -device pcie-root-port,id=rp,.. \
        -device pci-device,bus=rp

2. Add a new device to the same port:
device_add pci-device,bus=rp

The last command will add the device to slot 1 instead of failing with "PCI: slot 0 function 0 already occupied..."

---
v2:
* add example to the commit message [Michael]

 hw/pci/pci.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/pci/pci.c +0 -0
index de0fae10ab..ae132b0b52 100644

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

Quoted 6 lines
hw/pci/pci.c +1 -2
@@ -1034,8 +1034,9 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,
                    PCI_SLOT(devfn), PCI_FUNC(devfn), name,
                    bus->devices[devfn]->name);
         return NULL;
-    } else if (dev->hotplugged &&
-               pci_get_function_0(pci_dev)) {
+    };
        ^
Quoted 1 line

This semicolon seems unnecessary, can we take it out?

Oops, thanks for pointing out.