Skip to contentSkip to conversation

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

PATCH Superseded Julia Suvorova 2 participants 4 messages Sep 23, 2020 – Sep 24, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

---
 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 6 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.

This is just cosmetics right? I wouldn't describe this as a "fix" then - "simplify" would be clearer.

Quoted 22 lines
---
 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
#2 lore raw
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 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.

This is just cosmetics right? I wouldn't describe this as a "fix" then - "simplify" would be clearer.

No, this is a bug fix. For example, if you had a root port with a device on it already, and you do
'device_add new_device,bus=the_same_root_port', then it will miss the last 'if' and will be added to slot 1.

Quoted 23 lines
Quoted 22 lines
---
 hw/pci/pci.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/pci/pci.c +3 -3
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),
--

2.25.4

#3 lore raw
Quoted 10 lines · Julia Suvorova
Quoted 9 lines · Michael S. Tsirkin
Quoted 6 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.

Quoted 7 lines
Quoted 2 lines

This is just cosmetics right? I wouldn't describe this as a "fix" then - "simplify" would be clearer.

No, this is a bug fix. For example, if you had a root port with a device on it already, and you do
'device_add new_device,bus=the_same_root_port', then it will miss the last 'if' and will be added to slot 1.

OK and I think that is the only example - if devfn is not supplied and is auto-assigned. Can you add this to commit log pls?

Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
---
 hw/pci/pci.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/pci/pci.c +3 -3
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),
--

2.25.4