Add reference counting and device path tracking to IOThread to monitor
which devices are currently attached. This is implemented by adding
iothread_ref() and iothread_unref() calls during the virtio-blk
AioContext initialization and cleanup paths.

The IOThread now maintains:
 - attached_cnt: A counter of devices using the iothread.
 - attached_dev: A list of QOM paths for the attached devices.

This tracking is essential for management tools to understand the
relationship between IOThreads and devices, especially during hotplug
and unplug operations.

Future work will extend this ref/unref mechanism to other iothread-capable
devices (e.g. virtio-scsi).

Signed-off-by: Zhang Chen <[email protected]>
---
 hw/block/virtio-blk.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c
index ddf0e9ee53..247d6d41f3 100644
--- a/hw/block/virtio-blk.c
+++ b/hw/block/virtio-blk.c
@@ -1462,6 +1462,9 @@ static bool virtio_blk_vq_aio_context_init(VirtIOBlock 
*s, Error **errp)
             return false;
         }
     } else if (conf->iothread) {
+        char *path = object_get_canonical_path(OBJECT(s));
+
+        iothread_ref(conf->iothread, path);
         AioContext *ctx = iothread_get_aio_context(conf->iothread);
         for (unsigned i = 0; i < conf->num_queues; i++) {
             s->vq_aio_context[i] = ctx;
@@ -1491,7 +1494,12 @@ static void 
virtio_blk_vq_aio_context_cleanup(VirtIOBlock *s)
     }
 
     if (conf->iothread) {
+        char *path = object_get_canonical_path(OBJECT(s));
+
+        iothread_unref(conf->iothread, path);
         object_unref(OBJECT(conf->iothread));
+
+        g_free(path);
     }
 
     g_free(s->vq_aio_context);
-- 
2.49.0


Reply via email to