Convert pcie_sriov_pf_init() to the parented pci_new() API so each
VF becomes a child<> of its PF PCIDevice with a stable "vf[N]" name
in the composition tree instead of landing in /machine/unattached
with an unstable device[N] name.

Since object_property_add_child() now holds the sole reference to
the VF, drop the extra creator-ref object_unref() on both the
realize error path and in unparent_vfs(): object_unparent() drops
the last reference and frees the VF.

The remaining *_orphan() calls in hw/pci/pci.c are the transitional
API definitions themselves plus the public pci_vga_init() /
pci_init_nic_in_slot() helpers, which need parent threading through
their callers and are converted in the per-board patches.

Link: https://lore.kernel.org/qemu-devel/[email protected]/
AI-used-for: code (refactoring)
Signed-off-by: Alexander Graf <[email protected]>
---
 hw/pci/pcie_sriov.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/hw/pci/pcie_sriov.c b/hw/pci/pcie_sriov.c
index a69a41cdde..f96b0f45cb 100644
--- a/hw/pci/pcie_sriov.c
+++ b/hw/pci/pcie_sriov.c
@@ -26,7 +26,6 @@ static void unparent_vfs(PCIDevice *dev, uint16_t total_vfs)
     for (uint16_t i = 0; i < total_vfs; i++) {
         PCIDevice *vf = dev->exp.sriov_pf.vf[i];
         object_unparent(OBJECT(vf));
-        object_unref(OBJECT(vf));
     }
     g_free(dev->exp.sriov_pf.vf);
     dev->exp.sriov_pf.vf = NULL;
@@ -171,13 +170,13 @@ bool pcie_sriov_pf_init(PCIDevice *dev, uint16_t offset,
     dev->exp.sriov_pf.vf = g_new(PCIDevice *, total_vfs);
 
     for (uint16_t i = 0; i < total_vfs; i++) {
-        PCIDevice *vf = pci_new_orphan(devfn, vfname);
+        g_autofree char *vf_id = g_strdup_printf("vf[%u]", i);
+        PCIDevice *vf = pci_new(OBJECT(dev), vf_id, devfn, vfname);
         vf->exp.sriov_vf.pf = dev;
         vf->exp.sriov_vf.vf_number = i;
 
         if (!qdev_realize(&vf->qdev, bus, errp)) {
             object_unparent(OBJECT(vf));
-            object_unref(vf);
             unparent_vfs(dev, i);
             return false;
         }
-- 
2.47.1


Reply via email to