The virtio-mem system-reset shim, the vhost-user-gpu backend, and
its socket chardev live exactly as long as their enclosing virtio
device.  Parent them under it so they show up in the composition
tree instead of floating with only a refcount.

The unrealize/finalize/error paths switch from object_unref() to
object_unparent(), and vhost_user_gpu_instance_finalize() drops its
now-redundant unref: the vhost-backend is a child<> of the GPU and
is released automatically when the GPU is finalized.

Assisted-by: Kiro
Signed-off-by: Alexander Graf <[email protected]>
---
 hw/display/vhost-user-gpu.c | 12 ++++++------
 hw/virtio/virtio-mem.c      |  5 +++--
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/hw/display/vhost-user-gpu.c b/hw/display/vhost-user-gpu.c
index 57360898ca..c2c150bdcf 100644
--- a/hw/display/vhost-user-gpu.c
+++ b/hw/display/vhost-user-gpu.c
@@ -439,7 +439,8 @@ vhost_user_gpu_do_set_socket(VhostUserGPU *g, Error **errp)
         return false;
     }
 
-    chr = CHARDEV(object_new(TYPE_CHARDEV_SOCKET));
+    chr = CHARDEV(object_new_child(OBJECT(g), "vhost-chardev",
+                                   TYPE_CHARDEV_SOCKET));
     if (!chr || qemu_chr_add_client(chr, sv[0]) == -1) {
         error_setg(errp, "Failed to make socket chardev");
         goto err;
@@ -462,7 +463,7 @@ err:
     close(sv[0]);
     close(sv[1]);
     if (chr) {
-        object_unref(OBJECT(chr));
+        object_unparent(OBJECT(chr));
     }
     return false;
 }
@@ -584,7 +585,8 @@ vhost_user_gpu_instance_init(Object *obj)
 {
     VhostUserGPU *g = VHOST_USER_GPU(obj);
 
-    g->vhost = VHOST_USER_BACKEND(object_new(TYPE_VHOST_USER_BACKEND));
+    g->vhost = VHOST_USER_BACKEND(object_new_child(obj, "vhost-backend",
+                                                   TYPE_VHOST_USER_BACKEND));
     object_property_add_alias(obj, "chardev",
                               OBJECT(g->vhost), "chardev");
 }
@@ -592,9 +594,7 @@ vhost_user_gpu_instance_init(Object *obj)
 static void
 vhost_user_gpu_instance_finalize(Object *obj)
 {
-    VhostUserGPU *g = VHOST_USER_GPU(obj);
-
-    object_unref(OBJECT(g->vhost));
+    /* g->vhost is a child<> of obj and released with it */
 }
 
 static void
diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c
index 35e03ed759..c45cbdbe32 100644
--- a/hw/virtio/virtio-mem.c
+++ b/hw/virtio/virtio-mem.c
@@ -1011,7 +1011,8 @@ static void virtio_mem_device_realize(DeviceState *dev, 
Error **errp)
      * we need a dedicated reset object that only gets called during
      * qemu_devices_reset().
      */
-    obj = object_new(TYPE_VIRTIO_MEM_SYSTEM_RESET);
+    obj = object_new_child(OBJECT(vmem), "system-reset",
+                           TYPE_VIRTIO_MEM_SYSTEM_RESET);
     vmem->system_reset = VIRTIO_MEM_SYSTEM_RESET(obj);
     vmem->system_reset->vmem = vmem;
     qemu_register_resettable(obj);
@@ -1023,7 +1024,7 @@ static void virtio_mem_device_unrealize(DeviceState *dev)
     VirtIOMEM *vmem = VIRTIO_MEM(dev);
 
     qemu_unregister_resettable(OBJECT(vmem->system_reset));
-    object_unref(OBJECT(vmem->system_reset));
+    object_unparent(OBJECT(vmem->system_reset));
 
     if (vmem->early_migration) {
         vmstate_unregister(VMSTATE_IF(vmem), &vmstate_virtio_mem_device_early,
-- 
2.47.1


Reply via email to