Skip to contentSkip to conversation

iothread: Support tracking and querying IOThread holders

PATCH Superseded Zhang Chen 4 participants 44 messages Jun 24 – Jul 16

Subsystems (2)

v9 of 20 Superseded Latest is v20 →

lore
Share & tools
lore

Zhang Chen

author
#0 lore raw

v9 cover iothread: Support tracking and querying IOThread holders

### Motivation
Currently, the relationship between IOThreads and the resources using them is opaque. From a management perspective, it is difficult to determine which devices or block exports are currently associated with a specific IOThread via QMP or HMP.

This series introduces a "holder" tracking mechanism based on QOM paths, block nodes or monitor to significantly improve IOThread observability. In high-performance virtualization environments, users often pre-allocate a set of IOThreads to serve as a persistent thread pool. During device hotplug/unplug cycles, while IOThread objects remain persistent, their attachment state changes dynamically.

By tracking these "holders," administrators can use query-iothreads to monitor the real-time load distribution of the thread pool and identify which specific devices (e.g., virtio-blk, BlockExport) are attached to which thread.

### Technical Overview
* Atomic API Design: Merged iothread_get_aio_context and iothread_ref_and_get_aio_context into a single unified function. The new API enforces a 'holder' parameter, making "retrieval" and "tracking" an atomic operation to prevent reference tracking leaks.
* Subsystem Integration: Fully migrated and adapted core modules including Virtio (blk, vq-mapping, balloon, scsi), Block Layer (export, xen), Network (COLO), VFIO, and the Monitor subsystem.
* Introspection: Expanded the IOThreadInfo QAPI structure to include a 'holders' array (list of QOM paths), accessible via both query-iothreads (QMP) and info iothreads (HMP).

### V9 -> V8 Changelog
- Drop to assign unique default ID for monitor, and keep the NULL ID.
- Rewrite and split the monitor patch 7 and 8.
- Add the [PATCH 17/17]tests/unit/iothread: Update the iothread_get_aio_context to keep the API same in the unit tests.
- Refactorcode according comments.
- Update the example.
- Fix other comments issues.

### V7 -> V8 Changelog
- Drop the assumption of the Monitor QOM, add the new type IO_THREAD_HOLDER_KIND_MONITOR_NAME.
- Drop the assumption of the string '/' as QOM path.
- Introduce new patch for Monitor:
[PATCH 06/15] assign unique default ID to anonymous monitors
- Fixed Monitor ID for the IOThread.
- Refactorcode according comments.
- Update QMP example.
- Rebased patches on upstream code.
- Fixed comments issues.
- Removed redundant code.

### V6 -> V7 Changelog
- Fixed comments issues in patch 02/14 (Thanks Markus).
- Removed redundant code.
- Rebased patches on upstream code.
- Fixed code conflict with latest patches.
- Added detailed description in git log and code.
- Added comments for patch 06/14 assuming the monitor is a QOM object, Depends on Daniel Berrange's RFC patch:
[PATCH RFC 00/17] monitor: turn QMP and HMP into QOM objects

### V5 -> V6 Changelog
* API Refactoring (Breaking Change):
- Implement the 'union': 'IoThreadHolder' for combine the QOM objects and the block-node.
- Completely merged iothread_get_aio_context variants.
- The iothread_get_aio_context function now strictly requires a 'holder' argument to ensure every context retrieval is accounted for.
* Expanded Module Coverage:
- Added holder tracking for the monitor subsystem (tracking threads used by QMP/HMP).
- Added support for the virtio-scsi dataplane.
* Code Cleanup:
- Removed redundant and obsolete API declarations.
- Standardized the use of object_get_canonical_path() across all callers to provide consistent and unique holder names.
* Bug Fixes:
- Updated qapi/misc.json documentation to fix missing descriptions in query-iothreads items.
* Build System:
- Improved header inclusion logic to avoid redundant declaration errors in unit tests.
* QAPI part:
- Sorry to drop the "Acked-by: Markus Armbruster <[email protected]>", because this version have lots of changes in the:
[PATCH V6 13/14]qapi: examine IOThread attachment status via query-iothreads

Zhang Chen (17):
qapi/misc: Fix missed query-iothreads items
iothread: introduce iothread_ref/unref to track attached devices
iothread: tracking iothread users with holder name
iothread: introduce iothread_unsafe_get_aio_context() block/export: track IOThread reference in BlockExport
monitor: refactor monitor_data_init() to pass ID
monitor: support iothread ref/unref for anonymous monitors
monitor: switch to iothread_unsafe_get_aio_context()
virtio-vq-mapping: track iothread-vq-mapping references using device
path
virtio: use iothread_get/put_aio_context for thread pinning net/colo: track IOThread references using path-based holder
virtio-balloon: Update tracking iothread users with holder vfio-user/proxy: Update tracking iothread users with holder name
xen-block: Update tracking iothread users with holder name
qapi: examine IOThread attachment status via query-iothreads
iothread: simplify API by merging iothread_get_aio_context variants tests/unit/iothread: Update the iothread_get_aio_context

 block/export/export.c                   |  62 +++++++++--
 blockdev.c                              |   2 +-
 chardev/char.c                          |   2 +-
 gdbstub/system.c                        |   3 +-
 hw/block/dataplane/xen-block.c          |  19 +++-
 hw/block/virtio-blk.c                   |  22 ++--
 hw/scsi/virtio-scsi-dataplane.c         |  22 ++--
 hw/vfio-user/proxy.c                    |  15 ++-
 hw/virtio/iothread-vq-mapping.c         |  19 +++-
 hw/virtio/virtio-balloon.c              |  22 +++-
 include/block/export.h                  |   6 ++
 include/hw/virtio/iothread-vq-mapping.h |   6 +-
 include/monitor/monitor.h               |   5 +-
 include/system/iothread.h               |  17 ++-
 iothread.c                              | 136 +++++++++++++++++++++++-
 monitor/hmp-cmds.c                      |  26 +++++
 monitor/hmp.c                           |   5 +-
 monitor/monitor-internal.h              |   4 +-
 monitor/monitor.c                       |  37 +++++--
 monitor/qmp-cmds.c                      |   2 +-
 monitor/qmp.c                           |   7 +-
 net/colo-compare.c                      |  29 +++--
 qapi/misc.json                          |  82 +++++++++++++-
 stubs/monitor-internal.c                |   3 +-
 tests/unit/iothread.c                   |  16 +--
 tests/unit/iothread.h                   |   6 +-
 tests/unit/test-aio-multithread.c       |   5 +-
 tests/unit/test-bdrv-drain.c            |  18 +++-
 tests/unit/test-block-iothread.c        |  21 ++--
 29 files changed, 526 insertions(+), 93 deletions(-)
Signature · 1 line
2.49.0
author
#1 lore raw

PATCH v9 1/17 qapi/misc: Fix missed query-iothreads items

The example is incomplete: it misses members @poll-max-ns, @poll-grow, @poll-shrink, @aio-max-batch. Messed up in commit 5fc00480ab1 (monitor: add poll-* properties into query-iothreads result) and commit 1793ad0247c (iothread: add aio-max-batch parameter).

---
 qapi/misc.json | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
qapi/misc.json +10 -2
index 22b7afed9f..c71a5fe657 100644
@@ -123,11 +123,19 @@
 #     <- { "return": [
 #              {
 #                 "id":"iothread0",
-#                 "thread-id":3134
+#                 "thread-id":3134,
+#                 "poll-max-ns":32768,
+#                 "poll-grow":0,
+#                 "poll-shrink":0,
+#                 "aio-max-batch":0
 #              },
 #              {
 #                 "id":"iothread1",
-#                 "thread-id":3135
+#                 "thread-id":3135,
+#                 "poll-max-ns":32768,
+#                 "poll-grow":0,
+#                 "poll-shrink":0,
+#                 "aio-max-batch":0
 #              }
 #           ]
 #        }
Signature · 1 line
2.49.0
author
#13 lore raw

PATCH v9 4/17 iothread: introduce iothread_unsafe_get_aio_context()

In reply to Zhang Chen

The iothread_unsafe_get_aio_context() is a low-level unsafe way of getting the AioContext, recommend migrating to the new API with IOThreadHolder as much as possible.

The blockdev is the first user:
Every BlockDriverState would need to get/put the IOThread AioContext in bdrv_change_aio_context() or related functions.
However, the QEMU block layer has been moving away from having a per-BlockDriverState AioContext. It is possible to use a BlockDriverState from any AioContext (including multiple AioContexts at the same time). I'm in favor of not associating BlockDriverStates with IOThreads and instead relying on their device owners (e.g. emulated storage controllers) to be the IOThread holders.

---
 blockdev.c                | 2 +-
 include/system/iothread.h | 6 ++++++
 iothread.c                | 6 ++++++
 3 files changed, 13 insertions(+), 1 deletion(-)
blockdev.c +1 -1
index 6e86c6262f..baeab3a3e1 100644
@@ -3683,7 +3683,7 @@ void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread,
             goto out;
         }
 
-        new_context = iothread_get_aio_context(obj);
+        new_context = iothread_unsafe_get_aio_context(obj);
     } else {
         new_context = qemu_get_aio_context();
     }
include/system/iothread.h +6 -0
index b483bbfab3..064c05e78d 100644
@@ -71,6 +71,12 @@ DECLARE_INSTANCE_CHECKER(IOThread, IOTHREAD,
 char *iothread_get_id(IOThread *iothread);
 IOThread *iothread_by_id(const char *id);
 AioContext *iothread_get_aio_context(IOThread *iothread);
+/*
+ * The iothread_unsafe_get_aio_context() is a low-level unsafe way of getting
+ * the AioContext, recommend migrating to the new API with IOThreadHolder
+ * as much as possible.
+ */
+AioContext *iothread_unsafe_get_aio_context(IOThread *iothread);
 AioContext *iothread_ref_and_get_aio_context(IOThread *iothread,
                                              const IOThreadHolder *holder);
 void iothread_put_aio_context(IOThread *iothread, const IOThreadHolder *holder);
iothread.c +6 -0
index 528002c34a..a85e960e45 100644
@@ -468,6 +468,12 @@ void iothread_put_aio_context(IOThread *iothread, const IOThreadHolder *holder)
     iothread_unref(iothread, holder);
 }
 
+/* Recommend migrating to the new API with IOThreadHolder as much as possible */
+AioContext *iothread_unsafe_get_aio_context(IOThread *iothread)
+{
+    return iothread->ctx;
+}
+
 static int query_one_iothread(Object *object, void *opaque)
 {
     IOThreadInfoList ***tail = opaque;
Signature · 1 line
2.49.0
author
#16 lore raw

PATCH v9 5/17 block/export: track IOThread reference in BlockExport

In reply to Zhang Chen

Users currently lack visibility into which block exports are utilizing specific IOThreads. This patch integrates IOThread referencing into the BlockExport lifecycle.

- Add iothreads array and holder_name to BlockExport struct.
- Use iothread_ref_and_get_aio_context during export creation.
- Implement proper cleanup in blk_exp_add fail path and blk_exp_delete_bh.
- Support both single and multi-iothread export configurations.

This ensures IOThread 'holders' status correctly reflects active block exports for better debugging and resource tracking.

---
 block/export/export.c  | 63 ++++++++++++++++++++++++++++++++++++------
 include/block/export.h |  6 ++++
 2 files changed, 60 insertions(+), 9 deletions(-)
block/export/export.c +54 -9
index b733f269f3..b6c07f69b5 100644
@@ -15,7 +15,6 @@
 
 #include "block/block.h"
 #include "system/block-backend.h"
-#include "system/iothread.h"
 #include "block/export.h"
 #include "block/fuse.h"
 #include "block/nbd.h"
@@ -85,6 +84,8 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
     AioContext *ctx;
     AioContext **multithread_ctxs = NULL;
     size_t multithread_count = 0;
+    g_autofree IOThread **local_iothreads = NULL;
+    const char *holder_name = NULL;
     uint64_t perm;
     int ret;
 
@@ -139,7 +140,16 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
             goto fail;
         }
 
-        new_ctx = iothread_get_aio_context(iothread);
+        holder_name = bdrv_get_node_name(bs);
+        IOThreadHolder holder = {
+            .type = IO_THREAD_HOLDER_KIND_BLOCK_NODE,
+            .u.block_node.node_name = (char *)holder_name,
+        };
+
+        new_ctx = iothread_ref_and_get_aio_context(iothread, &holder);
+        multithread_count = 1;
+        local_iothreads = g_new0(IOThread *, 1);
+        local_iothreads[0] = iothread;
 
         /* Ignore errors with fixed-iothread=false */
         set_context_errp = fixed_iothread ? errp : NULL;
@@ -163,8 +173,15 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
             return NULL;
         }
 
+        local_iothreads = g_new0(IOThread *, multithread_count);
         multithread_ctxs = g_new(AioContext *, multithread_count);
         i = 0;
+        holder_name = bdrv_get_node_name(bs);
+        IOThreadHolder holder = {
+            .type = IO_THREAD_HOLDER_KIND_BLOCK_NODE,
+            .u.block_node.node_name = (char *)holder_name,
+        };
+
         for (strList *e = iothread_list; e; e = e->next) {
             IOThread *iothread = iothread_by_id(e->value);
 
@@ -172,7 +189,9 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
                 error_setg(errp, "iothread \"%s\" not found", e->value);
                 goto fail;
             }
-            multithread_ctxs[i++] = iothread_get_aio_context(iothread);
+            local_iothreads[i] = iothread;
+            multithread_ctxs[i++] = iothread_ref_and_get_aio_context(iothread,
+                                                                     &holder);
         }
         assert(i == multithread_count);
     }
@@ -225,12 +244,15 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
     assert(drv->instance_size >= sizeof(BlockExport));
     exp = g_malloc0(drv->instance_size);
     *exp = (BlockExport) {
-        .drv        = drv,
-        .refcount   = 1,
-        .user_owned = true,
-        .id         = g_strdup(export->id),
-        .ctx        = ctx,
-        .blk        = blk,
+        .drv                  = drv,
+        .refcount             = 1,
+        .user_owned           = true,
+        .id                   = g_strdup(export->id),
+        .ctx                  = ctx,
+        .blk                  = blk,
+        .iothreads            = g_steal_pointer(&local_iothreads),
+        .iothread_count       = multithread_count,
+        .iothread_holder_name = g_strdup(holder_name),
     };
 
     ret = drv->create(exp, export, multithread_ctxs, multithread_count, errp);
@@ -253,6 +275,18 @@ fail:
         g_free(exp->id);
         g_free(exp);
     }
+    if (local_iothreads) {
+        IOThreadHolder holder = {
+            .type = IO_THREAD_HOLDER_KIND_BLOCK_NODE,
+            .u.block_node.node_name = (char *)holder_name,
+        };
+
+        for (size_t j = 0; j < multithread_count; j++) {
+            if (local_iothreads[j]) {
+                iothread_put_aio_context(local_iothreads[j], &holder);
+            }
+        }
+    }
     g_free(multithread_ctxs);
     return NULL;
 }
@@ -269,6 +303,17 @@ static void blk_exp_delete_bh(void *opaque)
     BlockExport *exp = opaque;
 
     assert(exp->refcount == 0);
+    if (exp->iothreads) {
+        IOThreadHolder holder = {
+            .type = IO_THREAD_HOLDER_KIND_BLOCK_NODE,
+            .u.block_node.node_name = (char *)exp->iothread_holder_name,
+        };
+
+        for (size_t i = 0; i < exp->iothread_count; i++) {
+            iothread_put_aio_context(exp->iothreads[i], &holder);
+        }
+        g_free(exp->iothreads);
+    }
     QLIST_REMOVE(exp, next);
     exp->drv->delete(exp);
     blk_set_dev_ops(exp->blk, NULL, NULL);
include/block/export.h +6 -0
index ca45da928c..2bb98aae31 100644
@@ -16,6 +16,7 @@
 
 #include "qapi/qapi-types-block-export.h"
 #include "qemu/queue.h"
+#include "system/iothread.h"
 
 typedef struct BlockExport BlockExport;
 
@@ -89,6 +90,11 @@ struct BlockExport {
 
     /* List entry for block_exports */
     QLIST_ENTRY(BlockExport) next;
+
+    /* The iothreads list for block_exports */
+    IOThread **iothreads;
+    size_t iothread_count;
+    char *iothread_holder_name;
 };
 
 BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp);
Signature · 1 line
2.49.0
author
#10 lore raw

PATCH v9 3/17 iothread: tracking iothread users with holder name

In reply to Zhang Chen

Introduce iothread_get_aio_context() (In this patch named "iothread_ref_and_get_aio_context" for build, will change the name in the last patch) with a 'holder' argument and its counterpart iothread_put_aio_context().

Previously, users of an IOThread's AioContext did not explicitly record their identity, making it difficult to debug which devices or subsystems were pinning an IOThread.

This patch enhances the reference counting mechanism by:
1. Automatically incrementing the object reference count when a context is retrieved.
2. Tracking holders using iothread_ref() and iothread_unref().

In iothread_instance_finalize(), we now retrieve the source name from the GMainContext to correctly unref the initial internal holder.

---
 include/system/iothread.h |  4 ++++
 iothread.c                | 31 +++++++++++++++++++++++--------
 2 files changed, 27 insertions(+), 8 deletions(-)
include/system/iothread.h +4 -0
index b9207ad829..b483bbfab3 100644
@@ -18,6 +18,7 @@
 #include "qemu/thread.h"
 #include "qom/object.h"
 #include "system/event-loop-base.h"
+#include "qapi/qapi-types-misc.h"
 
 #define TYPE_IOTHREAD "iothread"
 
@@ -70,6 +71,9 @@ DECLARE_INSTANCE_CHECKER(IOThread, IOTHREAD,
 char *iothread_get_id(IOThread *iothread);
 IOThread *iothread_by_id(const char *id);
 AioContext *iothread_get_aio_context(IOThread *iothread);
+AioContext *iothread_ref_and_get_aio_context(IOThread *iothread,
+                                             const IOThreadHolder *holder);
+void iothread_put_aio_context(IOThread *iothread, const IOThreadHolder *holder);
 GMainContext *iothread_get_g_main_context(IOThread *iothread);
 
 /*
iothread.c +23 -8
index 3301b8d495..528002c34a 100644
@@ -55,6 +55,12 @@ static void iothread_ref(IOThread *iothread, const IOThreadHolder *holder)
     }
 
     iothread->holders = g_list_prepend(iothread->holders, h);
+
+    /*
+     * This guarantees that the IOThread and its AioContext remain alive
+     * as long as there is a holder.
+     */
+    object_ref(OBJECT(iothread));
 }
 
 static int iothread_holder_compare(gconstpointer a, gconstpointer b)
@@ -109,6 +115,8 @@ static void iothread_unref(IOThread *iothread, const IOThreadHolder *holder)
     IOThreadHolder *h = (IOThreadHolder *)link->data;
     qapi_free_IOThreadHolder(h);
     iothread->holders = g_list_delete_link(iothread->holders, link);
+
+    object_unref(OBJECT(iothread));
 }
 
 static void *iothread_run(void *opaque)
@@ -223,7 +231,7 @@ static void iothread_init_gcontext(IOThread *iothread, const char *thread_name)
     g_autofree char *name = g_strdup_printf("%s aio-context", thread_name);
 
     iothread->worker_context = g_main_context_new();
-    source = aio_get_g_source(iothread_get_aio_context(iothread));
+    source = aio_get_g_source(iothread->ctx);
     g_source_set_name(source, name);
     g_source_attach(source, iothread->worker_context);
     g_source_unref(source);
@@ -442,17 +450,24 @@ char *iothread_get_id(IOThread *iothread)
 
 AioContext *iothread_get_aio_context(IOThread *iothread)
 {
-    /* Remove in next patch for build */
-    IOThreadHolder holder = {
-        .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
-        .u.qom_object.qom_path = (char *)"tmp_path",
-    };
-    iothread_ref(iothread, &holder);
-    iothread_unref(iothread, &holder);
+    return iothread->ctx;
+}
+
+AioContext *iothread_ref_and_get_aio_context(IOThread *iothread,
+                                             const IOThreadHolder *holder)
+{
+    /* Add IOThreadHolder to the list */
+    iothread_ref(iothread, holder);
 
     return iothread->ctx;
 }
 
+void iothread_put_aio_context(IOThread *iothread, const IOThreadHolder *holder)
+{
+    /* Delete IOThreadHolder from the list */
+    iothread_unref(iothread, holder);
+}
+
 static int query_one_iothread(Object *object, void *opaque)
 {
     IOThreadInfoList ***tail = opaque;
Signature · 1 line
2.49.0
author
#29 lore raw

PATCH v9 7/17 monitor: support iothread ref/unref for anonymous monitors

In reply to Zhang Chen

Implement iothread registration and unregistration within monitor data lifecycle helpers using IOThreadHolder.

When an explicit monitor ID is missing, fall back to utilizing the chardev label as a safe identifier for iothread_ref/put operations. This enables accurate tracking for anonymous monitors while keeping 'mon->id' as NULL.

---
 monitor/monitor.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
monitor/monitor.c +22 -2
index 65749f931e..c3eec8c069 100644
@@ -612,8 +612,18 @@ void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
 {
     mon->id = id ? g_strdup(id) : NULL;
 
-    if (use_io_thread && !mon_iothread) {
-        monitor_iothread_init();
+    if (use_io_thread) {
+        if (!mon_iothread) {
+            monitor_iothread_init();
+        }
+
+        /* Setup the chr->label as the backup ID for iothread_ref/unref */
+        IOThreadHolder holder = {
+            .type = IO_THREAD_HOLDER_KIND_MONITOR_NAME,
+            .u.monitor_name.monitor_name = id ? (char *)mon->id :
+                                                (char *)mon->chr.chr->label,
+        };
+        iothread_ref_and_get_aio_context(mon_iothread, &holder);
     }
     qemu_mutex_init(&mon->mon_lock);
     mon->is_qmp = is_qmp;
@@ -624,6 +634,16 @@ void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
 
 void monitor_data_destroy(Monitor *mon)
 {
+    if (mon->use_io_thread && mon_iothread) {
+        IOThreadHolder holder = {
+            .type = IO_THREAD_HOLDER_KIND_MONITOR_NAME,
+            .u.monitor_name.monitor_name = mon->id ? (char *)mon->id :
+                                           (char *)mon->chr.chr->label,
+        };
+
+        iothread_put_aio_context(mon_iothread, &holder);
+    }
+
     g_free(mon->mon_cpu_path);
     qemu_chr_fe_deinit(&mon->chr, false);
     if (monitor_is_qmp(mon)) {
Signature · 1 line
2.49.0
author
#35 lore raw

PATCH v9 9/17 virtio-vq-mapping: track iothread-vq-mapping references using device path

In reply to Zhang Chen

Replace raw object_ref/unref calls with iothread_get/put_aio_context in iothread-vq-mapping. This allows tracking IOThread users via the device's canonical QOM path, improving lifecycle traceability for virtio-blk and virtio-scsi devices.

---
 hw/block/virtio-blk.c                   |  8 +++++++-
 hw/scsi/virtio-scsi-dataplane.c         |  9 +++++++--
 hw/virtio/iothread-vq-mapping.c         | 20 +++++++++++++++-----
 include/hw/virtio/iothread-vq-mapping.h |  6 +++++-
 4 files changed, 34 insertions(+), 9 deletions(-)
hw/block/virtio-blk.c +7 -1
index 9cb9f1fb2b..30e9fcf870 100644
@@ -1487,9 +1487,12 @@ static bool virtio_blk_vq_aio_context_init(VirtIOBlock *s, Error **errp)
     s->vq_aio_context = g_new(AioContext *, conf->num_queues);
 
     if (conf->iothread_vq_mapping_list) {
+        g_autofree char *path = object_get_canonical_path(OBJECT(vdev));
+
         if (!iothread_vq_mapping_apply(conf->iothread_vq_mapping_list,
                                        s->vq_aio_context,
                                        conf->num_queues,
+                                       path,
                                        errp)) {
             g_free(s->vq_aio_context);
             s->vq_aio_context = NULL;
@@ -1521,7 +1524,10 @@ static void virtio_blk_vq_aio_context_cleanup(VirtIOBlock *s)
     assert(!s->ioeventfd_started);
 
     if (conf->iothread_vq_mapping_list) {
-        iothread_vq_mapping_cleanup(conf->iothread_vq_mapping_list);
+        g_autofree char *path = object_get_canonical_path(
+                                OBJECT(VIRTIO_DEVICE(s)));
+
+        iothread_vq_mapping_cleanup(conf->iothread_vq_mapping_list, path);
     }
 
     if (conf->iothread) {
hw/scsi/virtio-scsi-dataplane.c +7 -2
index 95f13fb7c2..26ecefd547 100644
@@ -65,9 +65,11 @@ void virtio_scsi_dataplane_setup(VirtIOSCSI *s, Error **errp)
     s->vq_aio_context[1] = qemu_get_aio_context();
 
     if (vs->conf.iothread_vq_mapping_list) {
+        g_autofree char *path = object_get_canonical_path(OBJECT(vdev));
+
         if (!iothread_vq_mapping_apply(vs->conf.iothread_vq_mapping_list,
                     &s->vq_aio_context[VIRTIO_SCSI_VQ_NUM_FIXED],
-                    vs->conf.num_queues, errp)) {
+                    vs->conf.num_queues, path, errp)) {
             g_free(s->vq_aio_context);
             s->vq_aio_context = NULL;
             return;
@@ -94,7 +96,10 @@ void virtio_scsi_dataplane_cleanup(VirtIOSCSI *s)
     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
 
     if (vs->conf.iothread_vq_mapping_list) {
-        iothread_vq_mapping_cleanup(vs->conf.iothread_vq_mapping_list);
+        g_autofree char *path = object_get_canonical_path(
+                                OBJECT(VIRTIO_DEVICE(s)));
+
+        iothread_vq_mapping_cleanup(vs->conf.iothread_vq_mapping_list, path);
     }
 
     if (vs->conf.iothread) {
hw/virtio/iothread-vq-mapping.c +15 -5
index 55ce62986c..2cb48dd387 100644
@@ -77,6 +77,7 @@ bool iothread_vq_mapping_apply(
         IOThreadVirtQueueMappingList *list,
         AioContext **vq_aio_context,
         uint16_t num_queues,
+        const char *holder,
         Error **errp)
 {
     IOThreadVirtQueueMappingList *node;
@@ -93,10 +94,13 @@ bool iothread_vq_mapping_apply(
 
     for (node = list; node; node = node->next) {
         IOThread *iothread = iothread_by_id(node->value->iothread);
-        AioContext *ctx = iothread_get_aio_context(iothread);
+        IOThreadHolder io_holder = {
+            .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
+            .u.qom_object.qom_path = (char *)holder,
+        };
 
-        /* Released in virtio_blk_vq_aio_context_cleanup() */
-        object_ref(OBJECT(iothread));
+        AioContext *ctx = iothread_ref_and_get_aio_context(iothread,
+                                                           &io_holder);
 
         if (node->value->vqs) {
             uint16List *vq;
@@ -120,13 +124,19 @@ bool iothread_vq_mapping_apply(
     return true;
 }
 
-void iothread_vq_mapping_cleanup(IOThreadVirtQueueMappingList *list)
+void iothread_vq_mapping_cleanup(IOThreadVirtQueueMappingList *list,
+                                 const char *holder)
 {
     IOThreadVirtQueueMappingList *node;
 
     for (node = list; node; node = node->next) {
         IOThread *iothread = iothread_by_id(node->value->iothread);
-        object_unref(OBJECT(iothread));
+        IOThreadHolder io_holder = {
+            .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
+            .u.qom_object.qom_path = (char *)holder,
+        };
+
+        iothread_put_aio_context(iothread, &io_holder);
     }
 }
 
include/hw/virtio/iothread-vq-mapping.h +5 -1
index 57335c3703..0d39caddf3 100644
@@ -17,6 +17,7 @@
  * @list: The mapping of virtqueues to IOThreads.
  * @vq_aio_context: The array of AioContext pointers to fill in.
  * @num_queues: The length of @vq_aio_context.
+ * @holder: The QOM paths for attached device.
  * @errp: If an error occurs, a pointer to the area to store the error.
  *
  * Fill in the AioContext for each virtqueue in the @vq_aio_context array given
@@ -31,15 +32,18 @@ bool iothread_vq_mapping_apply(
         IOThreadVirtQueueMappingList *list,
         AioContext **vq_aio_context,
         uint16_t num_queues,
+        const char *holder,
         Error **errp);
 
 /**
  * iothread_vq_mapping_cleanup:
  * @list: The mapping of virtqueues to IOThreads.
+ * @holder: The QOM paths for attached device.
  *
  * Release IOThread object references that were acquired by
  * iothread_vq_mapping_apply().
  */
-void iothread_vq_mapping_cleanup(IOThreadVirtQueueMappingList *list);
+void iothread_vq_mapping_cleanup(IOThreadVirtQueueMappingList *list,
+                                 const char *holder);
 
 #endif /* HW_VIRTIO_IOTHREAD_VQ_MAPPING_H */
Signature · 1 line
2.49.0
author
#23 lore raw

PATCH v9 6/17 monitor: refactor monitor_data_init() to pass ID

In reply to Zhang Chen

Anonymous monitors (e.g., '-monitor stdio' or GDB stub) leave their 'id' field as NULL. To allow downstream subsystems (like iothread tracking) to identify these instances later, the initialization paths must propagate the context.

Refactor monitor_data_init() to accept the 'id' parameters, and update all internal QMP/HMP paths to pass these identifiers.

For anonymous monitors, 'mon->id' remains NULL at this stage to avoid namespace pollution, setting up infrastructure for subsequent patches.

---
 chardev/char.c             | 2 +-
 gdbstub/system.c           | 3 ++-
 include/monitor/monitor.h  | 5 +++--
 monitor/hmp.c              | 5 +++--
 monitor/monitor-internal.h | 4 +++-
 monitor/monitor.c          | 9 ++++++---
 monitor/qmp-cmds.c         | 2 +-
 monitor/qmp.c              | 5 +++--
 stubs/monitor-internal.c   | 3 ++-
 9 files changed, 24 insertions(+), 14 deletions(-)
chardev/char.c +1 -1
index ca8b37ed8d..f057247001 100644
@@ -805,7 +805,7 @@ static Chardev *qemu_chr_new_from_name(const char *label, const char *filename,
 
     if (qemu_opt_get_bool(opts, "mux", 0)) {
         assert(permit_mux_mon);
-        monitor_init_hmp(chr, true, &err);
+        monitor_init_hmp(chr, true, NULL, &err);
         if (err) {
             error_report_err(err);
             object_unparent(OBJECT(chr));
gdbstub/system.c +2 -1
index e86c5870ab..50f934fde3 100644
@@ -388,7 +388,8 @@ bool gdbserver_start(const char *device, Error **errp)
         /* Initialize a monitor terminal for gdb */
         mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
                                    NULL, NULL, &error_abort);
-        monitor_init_hmp(mon_chr, false, &error_abort);
+
+        monitor_init_hmp(mon_chr, false, NULL, &error_abort);
     } else {
         qemu_chr_fe_deinit(&gdbserver_system_state.chr, true);
         mon_chr = gdbserver_system_state.mon_chr;
include/monitor/monitor.h +3 -2
index 55649a8664..4a5eb20bea 100644
@@ -19,8 +19,9 @@ bool monitor_cur_is_qmp(void);
 
 void monitor_init_globals(void);
 void monitor_init_globals_core(void);
-void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp);
-void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp);
+void monitor_init_qmp(Chardev *chr, bool pretty, const char *id, Error **errp);
+void monitor_init_hmp(Chardev *chr, bool use_readline, const char *id,
+                      Error **errp);
 int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp);
 int monitor_init_opts(QemuOpts *opts, Error **errp);
 void monitor_cleanup(void);
monitor/hmp.c +3 -2
index cc4390486e..4ee8cb58d1 100644
@@ -1522,7 +1522,8 @@ static void monitor_readline_flush(void *opaque)
     monitor_flush(&mon->common);
 }
 
-void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
+void monitor_init_hmp(Chardev *chr, bool use_readline, const char *id,
+                      Error **errp)
 {
     MonitorHMP *mon = g_new0(MonitorHMP, 1);
 
@@ -1531,7 +1532,7 @@ void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
         return;
     }
 
-    monitor_data_init(&mon->common, false, false, false);
+    monitor_data_init(&mon->common, false, false, false, id);
 
     mon->use_readline = use_readline;
     if (mon->use_readline) {
monitor/monitor-internal.h +3 -1
index a5c4aba306..f651ba6a90 100644
@@ -108,6 +108,8 @@ struct Monitor {
     bool skip_flush;
     bool use_io_thread;
 
+    char *id;
+
     char *mon_cpu_path;
     QTAILQ_ENTRY(Monitor) entry;
 
@@ -179,7 +181,7 @@ extern QemuMutex monitor_lock;
 extern MonitorList mon_list;
 
 void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
-                       bool use_io_thread);
+                       bool use_io_thread, const char *id);
 void monitor_data_destroy(Monitor *mon);
 int monitor_can_read(void *opaque);
 void monitor_list_append(Monitor *mon);
monitor/monitor.c +6 -3
index 00b93ed612..65749f931e 100644
@@ -608,8 +608,10 @@ static void monitor_iothread_init(void)
 }
 
 void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
-                       bool use_io_thread)
+                       bool use_io_thread, const char *id)
 {
+    mon->id = id ? g_strdup(id) : NULL;
+
     if (use_io_thread && !mon_iothread) {
         monitor_iothread_init();
     }
@@ -629,6 +631,7 @@ void monitor_data_destroy(Monitor *mon)
     } else {
         readline_free(container_of(mon, MonitorHMP, common)->rs);
     }
+    g_free(mon->id);
     g_string_free(mon->outbuf, true);
     qemu_mutex_destroy(&mon->mon_lock);
 }
@@ -732,7 +735,7 @@ int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp)
 
     switch (opts->mode) {
     case MONITOR_MODE_CONTROL:
-        monitor_init_qmp(chr, opts->pretty, errp);
+        monitor_init_qmp(chr, opts->pretty, opts->id, errp);
         break;
     case MONITOR_MODE_READLINE:
         if (!allow_hmp) {
@@ -743,7 +746,7 @@ int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp)
             error_setg(errp, "'pretty' is not compatible with HMP monitors");
             return -1;
         }
-        monitor_init_hmp(chr, true, errp);
+        monitor_init_hmp(chr, true, opts->id, errp);
         break;
     default:
         g_assert_not_reached();
monitor/qmp-cmds.c +1 -1
index 0c409c27dc..e49b9cbf8b 100644
@@ -168,7 +168,7 @@ char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
     char *output = NULL;
     MonitorHMP hmp = {};
 
-    monitor_data_init(&hmp.common, false, true, false);
+    monitor_data_init(&hmp.common, false, true, false, NULL);
 
     if (has_cpu_index) {
         int ret = monitor_set_cpu(&hmp.common, cpu_index);
monitor/qmp.c +3 -2
index 687019811f..8fd71386e8 100644
@@ -513,7 +513,7 @@ static void monitor_qmp_setup_handlers_bh(void *opaque)
     monitor_list_append(&mon->common);
 }
 
-void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
+void monitor_init_qmp(Chardev *chr, bool pretty, const char *id, Error **errp)
 {
     MonitorQMP *mon = g_new0(MonitorQMP, 1);
 
@@ -525,7 +525,8 @@ void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
 
     /* Note: we run QMP monitor in I/O thread when @chr supports that */
     monitor_data_init(&mon->common, true, false,
-                      qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT));
+                      qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT),
+                      id);
 
     mon->pretty = pretty;
 
stubs/monitor-internal.c +2 -1
index 4fece49d53..325a559e62 100644
@@ -8,6 +8,7 @@ int monitor_get_fd(Monitor *mon, const char *name, Error **errp)
     return -1;
 }
 
-void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
+void monitor_init_hmp(Chardev *chr, bool use_readline, const char *id,
+                      Error **errp)
 {
 }
Signature · 1 line
2.49.0
author
#37 lore raw

PATCH v9 11/17 net/colo: track IOThread references using path-based holder

In reply to Zhang Chen

Convert colo-compare to use the iothread_ref_and_get_aio_context() and iothread_put_aio_context() APIs. This ensures that IOThread references are tracked using the COLO object's canonical QOM path as the holder ID.

This refactoring improves IOThread lifecycle traceability and aligns the code with modern QEMU iothread reference management patterns.

---
 net/colo-compare.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
net/colo-compare.c +19 -10
index 823b8aa323..4f180936e3 100644
@@ -130,6 +130,7 @@ struct CompareState {
     GHashTable *connection_track_table;
 
     IOThread *iothread;
+    AioContext *iothread_ctx;
     GMainContext *worker_context;
     QEMUTimer *packet_check_timer;
 
@@ -926,9 +927,7 @@ void colo_notify_compares_event(void *opaque, int event, Error **errp)
 
 static void colo_compare_timer_init(CompareState *s)
 {
-    AioContext *ctx = iothread_get_aio_context(s->iothread);
-
-    s->packet_check_timer = aio_timer_new(ctx, QEMU_CLOCK_HOST,
+    s->packet_check_timer = aio_timer_new(s->iothread_ctx, QEMU_CLOCK_HOST,
                                 SCALE_MS, check_old_packet_regular,
                                 s);
     timer_mod(s->packet_check_timer, qemu_clock_get_ms(QEMU_CLOCK_HOST) +
@@ -968,8 +967,15 @@ static void colo_compare_handle_event(void *opaque)
 
 static void colo_compare_iothread(CompareState *s)
 {
-    AioContext *ctx = iothread_get_aio_context(s->iothread);
-    object_ref(OBJECT(s->iothread));
+    g_autofree char *path = object_get_canonical_path(OBJECT(s));
+    IOThreadHolder io_holder = {
+        .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
+        .u.qom_object.qom_path = (char *)path,
+    };
+
+    AioContext *ctx = iothread_ref_and_get_aio_context(s->iothread, &io_holder);
+
+    s->iothread_ctx = ctx;
     s->worker_context = iothread_get_g_main_context(s->iothread);
 
     qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read,
@@ -1408,6 +1414,7 @@ static void colo_compare_finalize(Object *obj)
 {
     CompareState *s = COLO_COMPARE(obj);
     CompareState *tmp;
+    g_autofree char *path = object_get_canonical_path(OBJECT(s));
 
     qemu_mutex_lock(&colo_compare_mutex);
     QTAILQ_FOREACH(tmp, &net_compares, next) {
@@ -1434,18 +1441,20 @@ static void colo_compare_finalize(Object *obj)
     g_clear_pointer(&s->event_bh, qemu_bh_delete);
 
     if (s->iothread) {
-        AioContext *ctx = iothread_get_aio_context(s->iothread);
-
-        AIO_WAIT_WHILE(ctx, !s->out_sendco.done);
+        AIO_WAIT_WHILE(s->iothread_ctx, !s->out_sendco.done);
         if (s->notify_dev) {
-            AIO_WAIT_WHILE(ctx, !s->notify_sendco.done);
+            AIO_WAIT_WHILE(s->iothread_ctx, !s->notify_sendco.done);
         }
 
         /* Release all unhandled packets after compare thread exited */
         g_queue_foreach(&s->conn_list, colo_flush_packets, s);
         AIO_WAIT_WHILE(NULL, !s->out_sendco.done);
 
-        object_unref(OBJECT(s->iothread));
+        IOThreadHolder io_holder = {
+            .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
+            .u.qom_object.qom_path = (char *)path,
+        };
+        iothread_put_aio_context(s->iothread, &io_holder);
     }
 
     g_queue_clear(&s->conn_list);
Signature · 1 line
2.49.0
author
#40 lore raw

PATCH v9 14/17 xen-block: Update tracking iothread users with holder name

In reply to Zhang Chen

Replace raw object_ref/unref calls with iothread_get/put_aio_context.

---
 hw/block/dataplane/xen-block.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
hw/block/dataplane/xen-block.c +16 -3
index 48c2e315f3..b5bf8d359f 100644
@@ -621,9 +621,15 @@ XenBlockDataPlane *xen_block_dataplane_create(XenDevice *xendev,
     QLIST_INIT(&dataplane->freelist);
 
     if (iothread) {
+        g_autofree char *path = object_get_canonical_path(OBJECT(xendev));
+        IOThreadHolder io_holder = {
+            .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
+            .u.qom_object.qom_path = (char *)path,
+        };
+
         dataplane->iothread = iothread;
-        object_ref(OBJECT(dataplane->iothread));
-        dataplane->ctx = iothread_get_aio_context(dataplane->iothread);
+        dataplane->ctx = iothread_ref_and_get_aio_context(dataplane->iothread,
+                                                          &io_holder);
     } else {
         dataplane->ctx = qemu_get_aio_context();
     }
@@ -652,7 +658,14 @@ void xen_block_dataplane_destroy(XenBlockDataPlane *dataplane)
 
     qemu_bh_delete(dataplane->bh);
     if (dataplane->iothread) {
-        object_unref(OBJECT(dataplane->iothread));
+        g_autofree char *path = object_get_canonical_path(
+                                        OBJECT(dataplane->xendev));
+        IOThreadHolder io_holder = {
+            .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
+            .u.qom_object.qom_path = (char *)path,
+        };
+
+        iothread_put_aio_context(dataplane->iothread, &io_holder);
     }
 
     g_free(dataplane);
Signature · 1 line
2.49.0
author
#41 lore raw

PATCH v9 15/17 qapi: examine IOThread attachment status via query-iothreads

In reply to Zhang Chen

Extend the 'IOThreadInfo' structure to include attachment metrics. This allows users to monitor the associated devices by identify them by their QOM paths, block nodes or monitors.

New fields added to IOThreadInfo:
@holders: IOThreadHolder of the devices currently associated with
this iothread. Users can pre-allocate multiple iothread objects to serve as a persistent thread pool. When a device is hot-unplugged, it is detached from its iothread, but the iothread remains available, allowing future hot-plugged devices to attach to it. (Since 11.1)

These fields are also exposed via the Human Monitor Interface (HMP) command 'info iothreads' to assist with manual debugging and performance tuning.

---
 iothread.c         | 22 ++++++++++++++++++++++
 monitor/hmp-cmds.c | 26 ++++++++++++++++++++++++++
 qapi/misc.json     |  9 +++++++++
 3 files changed, 57 insertions(+)
iothread.c +22 -0
index a85e960e45..5949785b32 100644
@@ -24,6 +24,8 @@
 #include "qemu/error-report.h"
 #include "qemu/rcu.h"
 #include "qemu/main-loop.h"
+#include "qapi/clone-visitor.h"
+#include "qapi/qapi-visit-misc.h"
 
 /*
  * iothread_ref:
@@ -119,6 +121,25 @@ static void iothread_unref(IOThread *iothread, const IOThreadHolder *holder)
     object_unref(OBJECT(iothread));
 }
 
+static IOThreadHolderList *iothread_get_holders_list(IOThread *iothread)
+{
+    IOThreadHolderList *head = NULL;
+    IOThreadHolderList **prev = &head;
+    GList *l;
+
+    for (l = iothread->holders; l; l = l->next) {
+        IOThreadHolder *src = l->data;
+        IOThreadHolderList *entry = g_new0(IOThreadHolderList, 1);
+
+        entry->value = QAPI_CLONE(IOThreadHolder, src);
+
+        *prev = entry;
+        prev = &entry->next;
+    }
+
+    return head;
+}
+
 static void *iothread_run(void *opaque)
 {
     IOThread *iothread = opaque;
@@ -488,6 +509,7 @@ static int query_one_iothread(Object *object, void *opaque)
     info = g_new0(IOThreadInfo, 1);
     info->id = iothread_get_id(iothread);
     info->thread_id = iothread->thread_id;
+    info->holders = iothread_get_holders_list(iothread);
     info->poll_max_ns = iothread->poll_max_ns;
     info->poll_grow = iothread->poll_grow;
     info->poll_shrink = iothread->poll_shrink;
monitor/hmp-cmds.c +26 -0
index 443b8c785d..1b5883147b 100644
@@ -237,11 +237,37 @@ void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
     IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
     IOThreadInfoList *info;
     IOThreadInfo *value;
+    IOThreadHolderList *h;
 
     for (info = info_list; info; info = info->next) {
         value = info->value;
         monitor_printf(mon, "%s:\n", value->id);
         monitor_printf(mon, "  thread_id=%" PRId64 "\n", value->thread_id);
+        monitor_printf(mon, "  holders=");
+        if (value->holders) {
+            for (h = value->holders; h; h = h->next) {
+                IOThreadHolder *holder = h->value;
+
+                switch (holder->type) {
+                case IO_THREAD_HOLDER_KIND_BLOCK_NODE:
+                    monitor_printf(mon, "[block-node: %s]",
+                                   holder->u.block_node.node_name);
+                    break;
+                case IO_THREAD_HOLDER_KIND_QOM_OBJECT:
+                    monitor_printf(mon, "[qom-path: %s]",
+                                   holder->u.qom_object.qom_path);
+                    break;
+                case IO_THREAD_HOLDER_KIND_MONITOR_NAME:
+                    monitor_printf(mon, "[monitor-name: %s]",
+                                   holder->u.monitor_name.monitor_name);
+                    break;
+                default:
+                    monitor_printf(mon, "[unknown]");
+                    break;
+                }
+            }
+            monitor_printf(mon, "\n");
+        }
         monitor_printf(mon, "  poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
         monitor_printf(mon, "  poll-grow=%" PRId64 "\n", value->poll_grow);
         monitor_printf(mon, "  poll-shrink=%" PRId64 "\n", value->poll_shrink);
qapi/misc.json +9 -0
index d9f82f0922..6486c62ab5 100644
@@ -137,6 +137,11 @@
 #
 # @thread-id: ID of the underlying host thread
 #
+# @holders: the QOM objects, block nodes or monitors currently
+#     associated with this iothread.  When an associated component is
+#     detached or destroyed, it is removed from this list.
+#     (Since 11.1)
+#
 # @poll-max-ns: maximum polling time in ns, 0 means polling is
 #     disabled (since 2.9)
 #
@@ -159,6 +164,7 @@
 { 'struct': 'IOThreadInfo',
   'data': {'id': 'str',
            'thread-id': 'int',
+           'holders': ['IOThreadHolder'],
            'poll-max-ns': 'int',
            'poll-grow': 'int',
            'poll-shrink': 'int',
@@ -185,6 +191,8 @@
 #              {
 #                 "id":"iothread0",
 #                 "thread-id":3134,
+#                 "holders":[{"qom-path": "/machine/peripheral/blk1/virtio-backend", "type": "qom-object"},
+#                            {"monitor-name": "compat_monitor0", "type": "monitor-name"}],
 #                 "poll-max-ns":32768,
 #                 "poll-grow":0,
 #                 "poll-shrink":0,
@@ -193,6 +201,7 @@
 #              {
 #                 "id":"iothread1",
 #                 "thread-id":3135,
+#                 "holders":[{"node-name": "fmt_qcow2", "type": "block-node"}],
 #                 "poll-max-ns":32768,
 #                 "poll-grow":0,
 #                 "poll-shrink":0,
Signature · 1 line
2.49.0
author
#36 lore raw

PATCH v9 10/17 virtio: use iothread_get/put_aio_context for thread pinning

In reply to Zhang Chen

Refactor virtio-blk and virtio-scsi to use the new iothread_get/put APIs for AioContext management. This ensures IOThread references are tracked via the device's canonical QOM path.

Summary of changes:
- Lift 'path' scope to cover both vq_mapping and single iothread cases.
- Replace raw object_ref/unref with iothread_get/put_aio_context.
- Ensure consistent memory cleanup of the QOM path string.

---
 hw/block/virtio-blk.c           | 24 +++++++++++++-----------
 hw/scsi/virtio-scsi-dataplane.c | 23 +++++++++++++----------
 2 files changed, 26 insertions(+), 21 deletions(-)
hw/block/virtio-blk.c +13 -11
index 30e9fcf870..d8dc1dd136 100644
@@ -1463,6 +1463,7 @@ static bool virtio_blk_vq_aio_context_init(VirtIOBlock *s, Error **errp)
     VirtIOBlkConf *conf = &s->conf;
     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
+    g_autofree char *path = object_get_canonical_path(OBJECT(vdev));
 
     if (conf->iothread && conf->iothread_vq_mapping_list) {
         error_setg(errp,
@@ -1487,8 +1488,6 @@ static bool virtio_blk_vq_aio_context_init(VirtIOBlock *s, Error **errp)
     s->vq_aio_context = g_new(AioContext *, conf->num_queues);
 
     if (conf->iothread_vq_mapping_list) {
-        g_autofree char *path = object_get_canonical_path(OBJECT(vdev));
-
         if (!iothread_vq_mapping_apply(conf->iothread_vq_mapping_list,
                                        s->vq_aio_context,
                                        conf->num_queues,
@@ -1499,13 +1498,15 @@ static bool virtio_blk_vq_aio_context_init(VirtIOBlock *s, Error **errp)
             return false;
         }
     } else if (conf->iothread) {
-        AioContext *ctx = iothread_get_aio_context(conf->iothread);
+        IOThreadHolder io_holder = {
+            .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
+            .u.qom_object.qom_path = (char *)path,
+        };
+        AioContext *ctx = iothread_ref_and_get_aio_context(conf->iothread,
+                                                           &io_holder);
         for (unsigned i = 0; i < conf->num_queues; i++) {
             s->vq_aio_context[i] = ctx;
         }
-
-        /* Released in virtio_blk_vq_aio_context_cleanup() */
-        object_ref(OBJECT(conf->iothread));
     } else {
         AioContext *ctx = qemu_get_aio_context();
         for (unsigned i = 0; i < conf->num_queues; i++) {
@@ -1520,21 +1521,22 @@ static bool virtio_blk_vq_aio_context_init(VirtIOBlock *s, Error **errp)
 static void virtio_blk_vq_aio_context_cleanup(VirtIOBlock *s)
 {
     VirtIOBlkConf *conf = &s->conf;
+    g_autofree char *path = object_get_canonical_path(OBJECT(VIRTIO_DEVICE(s)));
 
     assert(!s->ioeventfd_started);
 
     if (conf->iothread_vq_mapping_list) {
-        g_autofree char *path = object_get_canonical_path(
-                                OBJECT(VIRTIO_DEVICE(s)));
-
         iothread_vq_mapping_cleanup(conf->iothread_vq_mapping_list, path);
     }
 
     if (conf->iothread) {
-        object_unref(OBJECT(conf->iothread));
+        IOThreadHolder io_holder = {
+            .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
+            .u.qom_object.qom_path = (char *)path,
+        };
+        iothread_put_aio_context(conf->iothread, &io_holder);
     }
 
-    g_free(s->vq_aio_context);
     s->vq_aio_context = NULL;
 }
 
hw/scsi/virtio-scsi-dataplane.c +13 -10
index 26ecefd547..c71f33b41e 100644
@@ -28,6 +28,7 @@ void virtio_scsi_dataplane_setup(VirtIOSCSI *s, Error **errp)
     VirtIODevice *vdev = VIRTIO_DEVICE(s);
     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
+    g_autofree char *path = object_get_canonical_path(OBJECT(vdev));
 
     if (vs->conf.iothread && vs->conf.iothread_vq_mapping_list) {
         error_setg(errp,
@@ -65,8 +66,6 @@ void virtio_scsi_dataplane_setup(VirtIOSCSI *s, Error **errp)
     s->vq_aio_context[1] = qemu_get_aio_context();
 
     if (vs->conf.iothread_vq_mapping_list) {
-        g_autofree char *path = object_get_canonical_path(OBJECT(vdev));
-
         if (!iothread_vq_mapping_apply(vs->conf.iothread_vq_mapping_list,
                     &s->vq_aio_context[VIRTIO_SCSI_VQ_NUM_FIXED],
                     vs->conf.num_queues, path, errp)) {
@@ -75,13 +74,15 @@ void virtio_scsi_dataplane_setup(VirtIOSCSI *s, Error **errp)
             return;
         }
     } else if (vs->conf.iothread) {
-        AioContext *ctx = iothread_get_aio_context(vs->conf.iothread);
+        IOThreadHolder io_holder = {
+            .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
+            .u.qom_object.qom_path = (char *)path,
+        };
+        AioContext *ctx = iothread_ref_and_get_aio_context(vs->conf.iothread,
+                                                           &io_holder);
         for (uint16_t i = 0; i < vs->conf.num_queues; i++) {
             s->vq_aio_context[VIRTIO_SCSI_VQ_NUM_FIXED + i] = ctx;
         }
-
-        /* Released in virtio_scsi_dataplane_cleanup() */
-        object_ref(OBJECT(vs->conf.iothread));
     } else {
         AioContext *ctx = qemu_get_aio_context();
         for (unsigned i = 0; i < vs->conf.num_queues; i++) {
@@ -94,16 +95,18 @@ void virtio_scsi_dataplane_setup(VirtIOSCSI *s, Error **errp)
 void virtio_scsi_dataplane_cleanup(VirtIOSCSI *s)
 {
     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
+    g_autofree char *path = object_get_canonical_path(OBJECT(VIRTIO_DEVICE(s)));
 
     if (vs->conf.iothread_vq_mapping_list) {
-        g_autofree char *path = object_get_canonical_path(
-                                OBJECT(VIRTIO_DEVICE(s)));
-
         iothread_vq_mapping_cleanup(vs->conf.iothread_vq_mapping_list, path);
     }
 
     if (vs->conf.iothread) {
-        object_unref(OBJECT(vs->conf.iothread));
+        IOThreadHolder io_holder = {
+            .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
+            .u.qom_object.qom_path = (char *)path,
+        };
+        iothread_put_aio_context(vs->conf.iothread, &io_holder);
     }
 
     g_free(s->vq_aio_context);
Signature · 1 line
2.49.0
author
#30 lore raw

PATCH v9 8/17 monitor: switch to iothread_unsafe_get_aio_context()

In reply to Zhang Chen

Since previous patches introduced explicit monitor tracking and reference counting via IOThreadHolder during initialization, standard global calls to iothread_get_aio_context() are no longer appropriate for raw context fetches.

Switch the remaining fallback sites in monitor_suspend(), monitor_resume(), and monitor_init_qmp() to use iothread_unsafe_get_aio_context(). This aligns with the new safe ref/unref lifecycles introduced for monitor tracking.

---
 monitor/monitor.c | 4 ++--
 monitor/qmp.c     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
monitor/monitor.c +2 -2
index c3eec8c069..98727f3d20 100644
@@ -529,7 +529,7 @@ int monitor_suspend(Monitor *mon)
          * Kick I/O thread to make sure this takes effect.  It'll be
          * evaluated again in prepare() of the watch object.
          */
-        aio_notify(iothread_get_aio_context(mon_iothread));
+        aio_notify(iothread_unsafe_get_aio_context(mon_iothread));
     }
 
     trace_monitor_suspend(mon, 1);
@@ -564,7 +564,7 @@ void monitor_resume(Monitor *mon)
         AioContext *ctx;
 
         if (mon->use_io_thread) {
-            ctx = iothread_get_aio_context(mon_iothread);
+            ctx = iothread_unsafe_get_aio_context(mon_iothread);
         } else {
             ctx = qemu_get_aio_context();
         }
monitor/qmp.c +1 -1
index 8fd71386e8..3341df19c8 100644
@@ -550,7 +550,7 @@ void monitor_init_qmp(Chardev *chr, bool pretty, const char *id, Error **errp)
          * since chardev might be running in the monitor I/O
          * thread.  Schedule a bottom half.
          */
-        aio_bh_schedule_oneshot(iothread_get_aio_context(mon_iothread),
+        aio_bh_schedule_oneshot(iothread_unsafe_get_aio_context(mon_iothread),
                                 monitor_qmp_setup_handlers_bh, mon);
         /* The bottom half will add @mon to @mon_list */
     } else {
Signature · 1 line
2.49.0
author
#39 lore raw

PATCH v9 13/17 vfio-user/proxy: Update tracking iothread users with holder name

In reply to Zhang Chen

Add object_ref/unref calls with iothread_get/put_aio_context.

---
 hw/vfio-user/proxy.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
hw/vfio-user/proxy.c +15 -1
index 0f7d8425d6..b4f749f245 100644
@@ -898,6 +898,7 @@ VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp)
     QIOChannelSocket *sioc;
     QIOChannel *ioc;
     char *sockname;
+    g_autofree char *path;
 
     if (addr->type != SOCKET_ADDRESS_TYPE_UNIX) {
         error_setg(errp, "vfio_user_connect - bad address family");
@@ -917,6 +918,11 @@ VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp)
     proxy = g_malloc0(sizeof(VFIOUserProxy));
     proxy->sockname = g_strdup_printf("unix:%s", sockname);
     proxy->ioc = ioc;
+    path = object_get_canonical_path(OBJECT(proxy->ioc));
+    IOThreadHolder io_holder = {
+        .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
+        .u.qom_object.qom_path = (char *)path,
+    };
 
     /* init defaults */
     proxy->max_xfer_size = VFIO_USER_DEF_MAX_XFER;
@@ -936,7 +942,8 @@ VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp)
         vfio_user_iothread = iothread_create("vfio-user", errp);
     }
 
-    proxy->ctx = iothread_get_aio_context(vfio_user_iothread);
+    proxy->ctx = iothread_ref_and_get_aio_context(vfio_user_iothread,
+                                                  &io_holder);
     proxy->req_bh = qemu_bh_new(vfio_user_request, proxy);
 
     QTAILQ_INIT(&proxy->outgoing);
@@ -967,6 +974,11 @@ void vfio_user_set_handler(VFIODevice *vbasedev,
 void vfio_user_disconnect(VFIOUserProxy *proxy)
 {
     VFIOUserMsg *r1, *r2;
+    g_autofree char *path = object_get_canonical_path(OBJECT(proxy->ioc));
+    IOThreadHolder io_holder = {
+        .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
+        .u.qom_object.qom_path = (char *)path,
+    };
 
     qemu_mutex_lock(&proxy->lock);
 
@@ -1021,6 +1033,8 @@ void vfio_user_disconnect(VFIOUserProxy *proxy)
     qemu_cond_destroy(&proxy->close_cv);
     qemu_mutex_destroy(&proxy->lock);
 
+    iothread_put_aio_context(vfio_user_iothread, &io_holder);
+
     QLIST_REMOVE(proxy, next);
     if (QLIST_EMPTY(&vfio_user_sockets)) {
         iothread_destroy(vfio_user_iothread);
Signature · 1 line
2.49.0
author
#42 lore raw

PATCH v9 16/17 iothread: simplify API by merging iothread_get_aio_context variants

In reply to Zhang Chen

Simplify the interface by merging iothread_ref_and_get_aio_context() into iothread_get_aio_context(). The updated function now requires a 'holder' parameter, ensuring that every retrieval of an AioContext for long-term use is automatically registered in the IOThread's holder list.

Update all callers across block, virtio, scsi, net, and monitor subsystems to match the new signature. This cleanup reduces code redundancy and improves the reliability of IOThread introspection.

---
 block/export/export.c           | 5 ++---
 hw/block/dataplane/xen-block.c  | 4 ++--
 hw/block/virtio-blk.c           | 4 ++--
 hw/scsi/virtio-scsi-dataplane.c | 4 ++--
 hw/vfio-user/proxy.c            | 3 +--
 hw/virtio/iothread-vq-mapping.c | 3 +--
 hw/virtio/virtio-balloon.c      | 2 +-
 include/system/iothread.h       | 6 +++---
 iothread.c                      | 9 ++-------
 monitor/monitor.c               | 2 +-
 net/colo-compare.c              | 2 +-
 11 files changed, 18 insertions(+), 26 deletions(-)
block/export/export.c +2 -3
index b6c07f69b5..acd061e86e 100644
@@ -146,7 +146,7 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
             .u.block_node.node_name = (char *)holder_name,
         };
 
-        new_ctx = iothread_ref_and_get_aio_context(iothread, &holder);
+        new_ctx = iothread_get_aio_context(iothread, &holder);
         multithread_count = 1;
         local_iothreads = g_new0(IOThread *, 1);
         local_iothreads[0] = iothread;
@@ -190,8 +190,7 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
                 goto fail;
             }
             local_iothreads[i] = iothread;
-            multithread_ctxs[i++] = iothread_ref_and_get_aio_context(iothread,
-                                                                     &holder);
+            multithread_ctxs[i++] = iothread_get_aio_context(iothread, &holder);
         }
         assert(i == multithread_count);
     }
hw/block/dataplane/xen-block.c +2 -2
index b5bf8d359f..46bfd62f5a 100644
@@ -628,8 +628,8 @@ XenBlockDataPlane *xen_block_dataplane_create(XenDevice *xendev,
         };
 
         dataplane->iothread = iothread;
-        dataplane->ctx = iothread_ref_and_get_aio_context(dataplane->iothread,
-                                                          &io_holder);
+        dataplane->ctx = iothread_get_aio_context(dataplane->iothread,
+                                                  &io_holder);
     } else {
         dataplane->ctx = qemu_get_aio_context();
     }
hw/block/virtio-blk.c +2 -2
index d8dc1dd136..d1d2bd7025 100644
@@ -1502,8 +1502,8 @@ static bool virtio_blk_vq_aio_context_init(VirtIOBlock *s, Error **errp)
             .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
             .u.qom_object.qom_path = (char *)path,
         };
-        AioContext *ctx = iothread_ref_and_get_aio_context(conf->iothread,
-                                                           &io_holder);
+        AioContext *ctx = iothread_get_aio_context(conf->iothread,
+                                                   &io_holder);
         for (unsigned i = 0; i < conf->num_queues; i++) {
             s->vq_aio_context[i] = ctx;
         }
hw/scsi/virtio-scsi-dataplane.c +2 -2
index c71f33b41e..76ab22610f 100644
@@ -78,8 +78,8 @@ void virtio_scsi_dataplane_setup(VirtIOSCSI *s, Error **errp)
             .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
             .u.qom_object.qom_path = (char *)path,
         };
-        AioContext *ctx = iothread_ref_and_get_aio_context(vs->conf.iothread,
-                                                           &io_holder);
+        AioContext *ctx = iothread_get_aio_context(vs->conf.iothread,
+                                                   &io_holder);
         for (uint16_t i = 0; i < vs->conf.num_queues; i++) {
             s->vq_aio_context[VIRTIO_SCSI_VQ_NUM_FIXED + i] = ctx;
         }
hw/vfio-user/proxy.c +1 -2
index b4f749f245..8ad0f4cfd3 100644
@@ -942,8 +942,7 @@ VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp)
         vfio_user_iothread = iothread_create("vfio-user", errp);
     }
 
-    proxy->ctx = iothread_ref_and_get_aio_context(vfio_user_iothread,
-                                                  &io_holder);
+    proxy->ctx = iothread_get_aio_context(vfio_user_iothread, &io_holder);
     proxy->req_bh = qemu_bh_new(vfio_user_request, proxy);
 
     QTAILQ_INIT(&proxy->outgoing);
hw/virtio/iothread-vq-mapping.c +1 -2
index 2cb48dd387..727358a483 100644
@@ -99,8 +99,7 @@ bool iothread_vq_mapping_apply(
             .u.qom_object.qom_path = (char *)holder,
         };
 
-        AioContext *ctx = iothread_ref_and_get_aio_context(iothread,
-                                                           &io_holder);
+        AioContext *ctx = iothread_get_aio_context(iothread, &io_holder);
 
         if (node->value->vqs) {
             uint16List *vq;
hw/virtio/virtio-balloon.c +1 -1
index e9d023b4ec..4405a87ed0 100644
@@ -903,7 +903,7 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
         precopy_add_notifier(&s->free_page_hint_notify);
 
         s->free_page_bh = aio_bh_new_guarded(
-                      iothread_ref_and_get_aio_context(s->iothread, &io_holder),
+                      iothread_get_aio_context(s->iothread, &io_holder),
                       virtio_ballloon_get_free_page_hints, s,
                       &dev->mem_reentrancy_guard);
     }
include/system/iothread.h +3 -3
index 064c05e78d..e5db215954 100644
@@ -70,15 +70,15 @@ DECLARE_INSTANCE_CHECKER(IOThread, IOTHREAD,
 
 char *iothread_get_id(IOThread *iothread);
 IOThread *iothread_by_id(const char *id);
-AioContext *iothread_get_aio_context(IOThread *iothread);
+
 /*
  * The iothread_unsafe_get_aio_context() is a low-level unsafe way of getting
  * the AioContext, recommend migrating to the new API with IOThreadHolder
  * as much as possible.
  */
 AioContext *iothread_unsafe_get_aio_context(IOThread *iothread);
-AioContext *iothread_ref_and_get_aio_context(IOThread *iothread,
-                                             const IOThreadHolder *holder);
+AioContext *iothread_get_aio_context(IOThread *iothread,
+                                     const IOThreadHolder *holder);
 void iothread_put_aio_context(IOThread *iothread, const IOThreadHolder *holder);
 GMainContext *iothread_get_g_main_context(IOThread *iothread);
 
iothread.c +2 -7
index 5949785b32..9bc8e359c1 100644
@@ -469,13 +469,8 @@ char *iothread_get_id(IOThread *iothread)
     return g_strdup(object_get_canonical_path_component(OBJECT(iothread)));
 }
 
-AioContext *iothread_get_aio_context(IOThread *iothread)
-{
-    return iothread->ctx;
-}
-
-AioContext *iothread_ref_and_get_aio_context(IOThread *iothread,
-                                             const IOThreadHolder *holder)
+AioContext *iothread_get_aio_context(IOThread *iothread,
+                                     const IOThreadHolder *holder)
 {
     /* Add IOThreadHolder to the list */
     iothread_ref(iothread, holder);
monitor/monitor.c +1 -1
index 98727f3d20..07f2f4dbe2 100644
@@ -623,7 +623,7 @@ void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
             .u.monitor_name.monitor_name = id ? (char *)mon->id :
                                                 (char *)mon->chr.chr->label,
         };
-        iothread_ref_and_get_aio_context(mon_iothread, &holder);
+        iothread_get_aio_context(mon_iothread, &holder);
     }
     qemu_mutex_init(&mon->mon_lock);
     mon->is_qmp = is_qmp;
net/colo-compare.c +1 -1
index 4f180936e3..820ea4d824 100644
@@ -973,7 +973,7 @@ static void colo_compare_iothread(CompareState *s)
         .u.qom_object.qom_path = (char *)path,
     };
 
-    AioContext *ctx = iothread_ref_and_get_aio_context(s->iothread, &io_holder);
+    AioContext *ctx = iothread_get_aio_context(s->iothread, &io_holder);
 
     s->iothread_ctx = ctx;
     s->worker_context = iothread_get_g_main_context(s->iothread);
Signature · 1 line
2.49.0
author
#43 lore raw

PATCH v9 17/17 tests/unit/iothread: Update the iothread_get_aio_context

In reply to Zhang Chen

Update the iothread_get_aio_context and the
iothread_put_aio_context for tests.

---
 tests/unit/iothread.c             | 16 ++++++++++------
 tests/unit/iothread.h             |  6 +++++-
 tests/unit/test-aio-multithread.c |  5 ++++-
 tests/unit/test-bdrv-drain.c      | 18 +++++++++++++-----
 tests/unit/test-block-iothread.c  | 21 ++++++++++++++-------
 5 files changed, 46 insertions(+), 20 deletions(-)
tests/unit/iothread.c +10 -6
index a363bf8f70..6cac850035 100644
@@ -30,12 +30,21 @@ struct IOThread {
     bool stopping;
 };
 
+AioContext *iothread_get_aio_context(IOThread *iothread, IOThreadHolder *holder)
+{
+    return iothread->ctx;
+}
+
+void iothread_put_aio_context(IOThread *iothread, IOThreadHolder *holder)
+{
+}
+
 static void iothread_init_gcontext(IOThread *iothread)
 {
     GSource *source;
 
     iothread->worker_context = g_main_context_new();
-    source = aio_get_g_source(iothread_get_aio_context(iothread));
+    source = aio_get_g_source(iothread->ctx);
     g_source_attach(source, iothread->worker_context);
     g_source_unref(source);
     iothread->main_loop = g_main_loop_new(iothread->worker_context, TRUE);
@@ -113,8 +122,3 @@ IOThread *iothread_new(void)
     qemu_mutex_unlock(&iothread->init_done_lock);
     return iothread;
 }
-
-AioContext *iothread_get_aio_context(IOThread *iothread)
-{
-    return iothread->ctx;
-}
tests/unit/iothread.h +5 -1
index eb4d0c77f8..42992c171d 100644
@@ -17,9 +17,13 @@
 #include "qemu/thread.h"
 
 typedef struct IOThread IOThread;
+typedef struct IOThreadHolder IOThreadHolder;
+
+AioContext *iothread_get_aio_context(IOThread *iothread,
+                                     IOThreadHolder *holder);
+void iothread_put_aio_context(IOThread *iothread, IOThreadHolder *holder);
 
 IOThread *iothread_new(void);
 void iothread_join(IOThread *iothread);
-AioContext *iothread_get_aio_context(IOThread *iothread);
 
 #endif
tests/unit/test-aio-multithread.c +4 -1
index 9179cdc6a3..92fa210ddc 100644
@@ -69,7 +69,7 @@ static void create_aio_contexts(void)
 
     for (i = 0; i < NUM_CONTEXTS; i++) {
         threads[i] = iothread_new();
-        ctx[i] = iothread_get_aio_context(threads[i]);
+        ctx[i] = iothread_get_aio_context(threads[i], NULL);
     }
 
     qemu_event_init(&done_event, false);
@@ -87,6 +87,9 @@ static void join_aio_contexts(void)
     for (i = 0; i < NUM_CONTEXTS; i++) {
         aio_context_ref(ctx[i]);
     }
+    for (i = 0; i < NUM_CONTEXTS; i++) {
+        iothread_put_aio_context(threads[i], NULL);
+    }
     for (i = 0; i < NUM_CONTEXTS; i++) {
         iothread_join(threads[i]);
     }
tests/unit/test-bdrv-drain.c +13 -5
index 43b0ba8648..05e52e89ac 100644
@@ -537,8 +537,8 @@ static void test_iothread_common(enum drain_type drain_type, int drain_thread)
 
     IOThread *a = iothread_new();
     IOThread *b = iothread_new();
-    AioContext *ctx_a = iothread_get_aio_context(a);
-    AioContext *ctx_b = iothread_get_aio_context(b);
+    AioContext *ctx_a = iothread_get_aio_context(a, NULL);
+    AioContext *ctx_b = iothread_get_aio_context(b, NULL);
 
     QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, 0);
 
@@ -612,6 +612,8 @@ static void test_iothread_common(enum drain_type drain_type, int drain_thread)
 
     bdrv_unref(bs);
     blk_unref(blk);
+    iothread_put_aio_context(a, NULL);
+    iothread_put_aio_context(b, NULL);
 
 out:
     iothread_join(a);
@@ -762,7 +764,7 @@ static void test_blockjob_common_drain_node(enum drain_type drain_type,
         AioContext *ctx;
 
         iothread = iothread_new();
-        ctx = iothread_get_aio_context(iothread);
+        ctx = iothread_get_aio_context(iothread, NULL);
         blk_set_aio_context(blk_src, ctx, &error_abort);
     }
 
@@ -892,6 +894,10 @@ static void test_blockjob_common_drain_node(enum drain_type drain_type,
     bdrv_unref(src_overlay);
     bdrv_unref(target);
 
+    if (use_iothread) {
+        iothread_put_aio_context(iothread, NULL);
+    }
+
     if (iothread) {
         iothread_join(iothread);
     }
@@ -1398,8 +1404,8 @@ static void test_set_aio_context(void)
     BlockDriverState *bs;
     IOThread *a = iothread_new();
     IOThread *b = iothread_new();
-    AioContext *ctx_a = iothread_get_aio_context(a);
-    AioContext *ctx_b = iothread_get_aio_context(b);
+    AioContext *ctx_a = iothread_get_aio_context(a, NULL);
+    AioContext *ctx_b = iothread_get_aio_context(b, NULL);
 
     bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
                               &error_abort);
@@ -1410,6 +1416,8 @@ static void test_set_aio_context(void)
     bdrv_try_change_aio_context(bs, qemu_get_aio_context(), NULL, &error_abort);
 
     bdrv_unref(bs);
+    iothread_put_aio_context(a, NULL);
+    iothread_put_aio_context(b, NULL);
     iothread_join(a);
     iothread_join(b);
 }
tests/unit/test-block-iothread.c +14 -7
index 5273ff235a..8b7ac6f980 100644
@@ -466,7 +466,7 @@ static void test_sync_op(const void *opaque)
 {
     const SyncOpTest *t = opaque;
     IOThread *iothread = iothread_new();
-    AioContext *ctx = iothread_get_aio_context(iothread);
+    AioContext *ctx = iothread_get_aio_context(iothread, NULL);
     BlockBackend *blk;
     BlockDriverState *bs;
     BdrvChild *c;
@@ -493,6 +493,7 @@ static void test_sync_op(const void *opaque)
 
     bdrv_unref(bs);
     blk_unref(blk);
+    iothread_put_aio_context(iothread, NULL);
 }
 
 typedef struct TestBlockJob {
@@ -549,7 +550,7 @@ BlockJobDriver test_job_driver = {
 static void test_attach_blockjob(void)
 {
     IOThread *iothread = iothread_new();
-    AioContext *ctx = iothread_get_aio_context(iothread);
+    AioContext *ctx = iothread_get_aio_context(iothread, NULL);
     BlockBackend *blk;
     BlockDriverState *bs;
     TestBlockJob *tjob;
@@ -595,6 +596,7 @@ static void test_attach_blockjob(void)
 
     bdrv_unref(bs);
     blk_unref(blk);
+    iothread_put_aio_context(iothread, NULL);
 }
 
 /*
@@ -612,7 +614,7 @@ static void test_attach_blockjob(void)
 static void test_propagate_basic(void)
 {
     IOThread *iothread = iothread_new();
-    AioContext *ctx = iothread_get_aio_context(iothread);
+    AioContext *ctx = iothread_get_aio_context(iothread, NULL);
     AioContext *main_ctx;
     BlockBackend *blk;
     BlockDriverState *bs_a, *bs_b, *bs_verify;
@@ -658,6 +660,7 @@ static void test_propagate_basic(void)
     bdrv_unref(bs_b);
     bdrv_unref(bs_a);
     blk_unref(blk);
+    iothread_put_aio_context(iothread, NULL);
 }
 
 /*
@@ -676,7 +679,7 @@ static void test_propagate_basic(void)
 static void test_propagate_diamond(void)
 {
     IOThread *iothread = iothread_new();
-    AioContext *ctx = iothread_get_aio_context(iothread);
+    AioContext *ctx = iothread_get_aio_context(iothread, NULL);
     AioContext *main_ctx;
     BlockBackend *blk;
     BlockDriverState *bs_a, *bs_b, *bs_c, *bs_verify;
@@ -736,12 +739,13 @@ static void test_propagate_diamond(void)
     bdrv_unref(bs_c);
     bdrv_unref(bs_b);
     bdrv_unref(bs_a);
+    iothread_put_aio_context(iothread, NULL);
 }
 
 static void test_propagate_mirror(void)
 {
     IOThread *iothread = iothread_new();
-    AioContext *ctx = iothread_get_aio_context(iothread);
+    AioContext *ctx = iothread_get_aio_context(iothread, NULL);
     AioContext *main_ctx = qemu_get_aio_context();
     BlockDriverState *src, *target, *filter;
     BlockBackend *blk;
@@ -807,12 +811,13 @@ static void test_propagate_mirror(void)
     blk_unref(blk);
     bdrv_unref(src);
     bdrv_unref(target);
+    iothread_put_aio_context(iothread, NULL);
 }
 
 static void test_attach_second_node(void)
 {
     IOThread *iothread = iothread_new();
-    AioContext *ctx = iothread_get_aio_context(iothread);
+    AioContext *ctx = iothread_get_aio_context(iothread, NULL);
     AioContext *main_ctx = qemu_get_aio_context();
     BlockBackend *blk;
     BlockDriverState *bs, *filter;
@@ -840,12 +845,13 @@ static void test_attach_second_node(void)
     bdrv_unref(filter);
     bdrv_unref(bs);
     blk_unref(blk);
+    iothread_put_aio_context(iothread, NULL);
 }
 
 static void test_attach_preserve_blk_ctx(void)
 {
     IOThread *iothread = iothread_new();
-    AioContext *ctx = iothread_get_aio_context(iothread);
+    AioContext *ctx = iothread_get_aio_context(iothread, NULL);
     BlockBackend *blk;
     BlockDriverState *bs;
 
@@ -871,6 +877,7 @@ static void test_attach_preserve_blk_ctx(void)
     blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
     bdrv_unref(bs);
     blk_unref(blk);
+    iothread_put_aio_context(iothread, NULL);
 }
 
 int main(int argc, char **argv)
Signature · 1 line
2.49.0
author
#2 lore raw

PATCH v9 2/17 iothread: introduce iothread_ref/unref to track attached devices

In reply to Zhang Chen

Currently, IOThreads do not maintain a record of which devices are associated with them. This makes it difficult to monitor the workload distribution of IOThreads, especially in complex hotplug scenarios involving multiple virtio-blk or virtio-scsi devices.

This patch introduces a reference counting and tracking mechanism within the IOThread object:

- iothread_ref(): Prepends the device's IOThreadHolder to a list.
- iothread_unref(): Searches for the IOThreadHolder using a custom string comparison (g_strcmp0), releases the associated memory upon a successful match.
- holders: A GList storing the IOThreadHolder of attached devices for runtime introspection.

A later commit will add QMP commands to let management applications query the attachment status of IOThreads.

---
 include/system/iothread.h |  5 +++
 iothread.c                | 94 +++++++++++++++++++++++++++++++++++++++
 qapi/misc.json            | 61 +++++++++++++++++++++++++
 3 files changed, 160 insertions(+)
include/system/iothread.h +5 -0
index a1ef7696cb..b9207ad829 100644
@@ -50,6 +50,11 @@ struct IOThread {
     bool stopping;              /* has iothread_stop() been called? */
     bool running;               /* should iothread_run() continue? */
     int thread_id;
+    /*
+     * The list elements are of type IOThreadHolder, which can
+     * represent either a QOM path or a block node name.
+     */
+    GList *holders;
 
     /* AioContext poll parameters */
     int64_t poll_max_ns;
iothread.c +94 -0

106 lines not on this page · raw message

qapi/misc.json +61 -0

67 lines not on this page · raw message

Signature · 1 line
2.49.0
author
#38 lore raw

PATCH v9 12/17 virtio-balloon: Update tracking iothread users with holder

In reply to Zhang Chen

Replace raw object_ref/unref calls with iothread_get/put_aio_context.

---
 hw/virtio/virtio-balloon.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)
hw/virtio/virtio-balloon.c +17 -5
index 4c5f486ba2..e9d023b4ec 100644
@@ -892,14 +892,20 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
     s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
 
     if (virtio_has_feature(s->host_features, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
+        g_autofree char *path = object_get_canonical_path(OBJECT(s));
+        IOThreadHolder io_holder = {
+            .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
+            .u.qom_object.qom_path = (char *)path,
+        };
+
         s->free_page_vq = virtio_add_queue(vdev, VIRTQUEUE_MAX_SIZE,
                                            virtio_balloon_handle_free_page_vq);
         precopy_add_notifier(&s->free_page_hint_notify);
 
-        object_ref(OBJECT(s->iothread));
-        s->free_page_bh = aio_bh_new_guarded(iothread_get_aio_context(s->iothread),
-                                             virtio_ballloon_get_free_page_hints, s,
-                                             &dev->mem_reentrancy_guard);
+        s->free_page_bh = aio_bh_new_guarded(
+                      iothread_ref_and_get_aio_context(s->iothread, &io_holder),
+                      virtio_ballloon_get_free_page_hints, s,
+                      &dev->mem_reentrancy_guard);
     }
 
     if (virtio_has_feature(s->host_features, VIRTIO_BALLOON_F_REPORTING)) {
@@ -919,9 +925,15 @@ static void virtio_balloon_device_unrealize(DeviceState *dev)
 
     qemu_unregister_resettable(OBJECT(dev));
     if (s->free_page_bh) {
+        g_autofree char *path = object_get_canonical_path(OBJECT(s));
+        IOThreadHolder io_holder = {
+            .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
+            .u.qom_object.qom_path = (char *)path,
+        };
+
         qemu_bh_delete(s->free_page_bh);
-        object_unref(OBJECT(s->iothread));
         virtio_balloon_free_page_stop(s);
+        iothread_put_aio_context(s->iothread, &io_holder);
         precopy_remove_notifier(&s->free_page_hint_notify);
     }
     balloon_stats_destroy_timer(s);
Signature · 1 line
2.49.0
Reviewed-by on v9 2/17
#3 lore raw

Re: [PATCH V9 02/17] iothread: introduce iothread_ref/unref to track attached devices

In reply to Zhang Chen
Quoted 18 lines · Zhang Chen

Currently, IOThreads do not maintain a record of which devices are associated with them. This makes it difficult to monitor the workload distribution of IOThreads, especially in complex hotplug scenarios involving multiple virtio-blk or virtio-scsi devices.

This patch introduces a reference counting and tracking mechanism within the IOThread object:

- iothread_ref(): Prepends the device's IOThreadHolder to a list.
- iothread_unref(): Searches for the IOThreadHolder using a custom string comparison (g_strcmp0), releases the associated memory upon a successful match.
- holders: A GList storing the IOThreadHolder of attached devices for runtime introspection.

A later commit will add QMP commands to let management applications query the attachment status of IOThreads.

Quoted 6 lines
---
 include/system/iothread.h |  5 +++
 iothread.c                | 94 +++++++++++++++++++++++++++++++++++++++
 qapi/misc.json            | 61 +++++++++++++++++++++++++
 3 files changed, 160 insertions(+)

Minor comments below. This patch looks good overall:

include/system/iothread.h struct IOThread { · line 55

Quoted 11 lines
include/system/iothread.h +3 -0
index a1ef7696cb..b9207ad829 100644
@@ -50,6 +50,11 @@ struct IOThread {
     bool stopping;              /* has iothread_stop() been called? */
     bool running;               /* should iothread_run() continue? */
     int thread_id;
+    /*
+     * The list elements are of type IOThreadHolder, which can
+     * represent either a QOM path or a block node name.

This comment duplicates the IOThreadHolder types and will become outdated easily. It might already be outdated (there are 3 types, not just the 2 mentioned in the comment). I suggest replacing it with something like:

/* List of iothread_ref() holders (elements are of type IOThreadHolder) */

Quoted 20 lines
include/system/iothread.h +20 -0
+static int iothread_holder_compare(gconstpointer a, gconstpointer b)
+{
+    const IOThreadHolder *holder_a = a;
+    const IOThreadHolder *holder_b = b;
+    const char *name_a, *name_b;
+
+    if (holder_a->type != holder_b->type) {
+        return -1;
+    }
+
+    switch (holder_a->type) {
+    case IO_THREAD_HOLDER_KIND_QOM_OBJECT:
+        name_a = holder_a->u.qom_object.qom_path;
+        name_b = holder_b->u.qom_object.qom_path;
+        break;
+    case IO_THREAD_HOLDER_KIND_BLOCK_NODE:
+        name_a = holder_a->u.block_node.node_name;
+        name_b = holder_b->u.block_node.node_name;
+        break;
+    case IO_THREAD_HOLDER_KIND_MONITOR_NAME:

include/system/iothread.h struct IOThread {

Quoted 10 lines
include/system/iothread.h +10 -0
+        name_a = holder_a->u.monitor_name.monitor_name;
+        name_b = holder_b->u.monitor_name.monitor_name;
+        break;
+    default:
+       /*
+        * This should not happen. If it does, name_a/b remains
+        * NULL and g_strcmp0 will handle it safely.
+        */
+        name_a = NULL;
+        name_b = NULL;

return -1 would be simpler and safer. g_strcmp0(NULL, NULL) returns 0, so callers will think they have found a match and proceed with their operation - it will likely result in undefined behavior since memory is probably corrupted if we got here. It's safer to fail the comparison so the caller does not proceed.

#14 lore raw

Re: [PATCH V9 04/17] iothread: introduce iothread_unsafe_get_aio_context()

In reply to Zhang Chen

include/system/iothread.h DECLARE_INSTANCE_CHECKER(IOThread, IOTHREAD, · line 77

Quoted 12 lines · Zhang Chen
include/system/iothread.h +4 -0
index b483bbfab3..064c05e78d 100644
@@ -71,6 +71,12 @@ DECLARE_INSTANCE_CHECKER(IOThread, IOTHREAD,
 char *iothread_get_id(IOThread *iothread);
 IOThread *iothread_by_id(const char *id);
 AioContext *iothread_get_aio_context(IOThread *iothread);
+/*
+ * The iothread_unsafe_get_aio_context() is a low-level unsafe way of getting
+ * the AioContext, recommend migrating to the new API with IOThreadHolder
+ * as much as possible.

This should describe the safety concerns and when it is okay to use this API. Something like:

Normally the IOThread's AioContext is fetched using iothread_ref_and_get_aio_context(), but there are legacy callers without a clear ref/unref lifecycle. They cannot unref (e.g. because they do not have an IOThread pointer), so provide an unsafe way to fetch the AioContext without holding a reference count. This unsafe API serves legacy callers - do not use it in new code.

Quoted 5 lines
include/system/iothread.h +2 -0
+ */
+AioContext *iothread_unsafe_get_aio_context(IOThread *iothread);
 AioContext *iothread_ref_and_get_aio_context(IOThread *iothread,
                                              const IOThreadHolder *holder);
 void iothread_put_aio_context(IOThread *iothread, const IOThreadHolder *holder);
#17 lore raw

Re: [PATCH V9 05/17] block/export: track IOThread reference in BlockExport

In reply to Zhang Chen
Quoted 9 lines · Zhang Chen

Users currently lack visibility into which block exports are utilizing specific IOThreads. This patch integrates IOThread referencing into the BlockExport lifecycle.

- Add iothreads array and holder_name to BlockExport struct.
- Use iothread_ref_and_get_aio_context during export creation.
- Implement proper cleanup in blk_exp_add fail path and blk_exp_delete_bh.
- Support both single and multi-iothread export configurations.

Quoted 8 lines

This ensures IOThread 'holders' status correctly reflects active block exports for better debugging and resource tracking.

---
 block/export/export.c  | 63 ++++++++++++++++++++++++++++++++++++------
 include/block/export.h |  6 ++++
 2 files changed, 60 insertions(+), 9 deletions(-)

If AI was used to generate this patch series, please see QEMU's AI policy:
https://www.qemu.org/docs/master/devel/code-provenance.html#use-of-ai-generated-content

This feels like AI generated code in that it is verbose and has lots of structure (e.g. commit messages that look like an AI summary of the code change), but the details are not correct (misleading comments, leaks, etc).

I will pause my review at this patch and wait for your response.

Quoted 23 lines
block/export/export.c +2 -1
index b733f269f3..b6c07f69b5 100644
@@ -15,7 +15,6 @@
 
 #include "block/block.h"
 #include "system/block-backend.h"
-#include "system/iothread.h"
 #include "block/export.h"
 #include "block/fuse.h"
 #include "block/nbd.h"
@@ -85,6 +84,8 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
     AioContext *ctx;
     AioContext **multithread_ctxs = NULL;
     size_t multithread_count = 0;
+    g_autofree IOThread **local_iothreads = NULL;
+    const char *holder_name = NULL;
     uint64_t perm;
     int ret;
 

block/export/export.c BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp) · line 145

Quoted 7 lines
block/export/export.c +3 -1
@@ -139,7 +140,16 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
             goto fail;
         }
 
-        new_ctx = iothread_get_aio_context(iothread);
+        holder_name = bdrv_get_node_name(bs);
+        IOThreadHolder holder = {
+            .type = IO_THREAD_HOLDER_KIND_BLOCK_NODE,

This is not a block node, this is a block export (a separate type of object). Reporting the export as a node is confusing because users will not know which block export id is assigned using this IOThread, they will only know the block node (which could be doing other things too).

block/export/export.c BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp) · line 146

Quoted 1 line
block/export/export.c +1 -0
+            .u.block_node.node_name = (char *)holder_name,

To remove the suspicious-looking const cast, try:

  const IOThreadHolder holder = {
      .type = ...,
      .u.block_node.node_name = holder_name,
  };
Quoted 59 lines
block/export/export.c +25 -7
+        };
+
+        new_ctx = iothread_ref_and_get_aio_context(iothread, &holder);
+        multithread_count = 1;
+        local_iothreads = g_new0(IOThread *, 1);
+        local_iothreads[0] = iothread;
 
         /* Ignore errors with fixed-iothread=false */
         set_context_errp = fixed_iothread ? errp : NULL;
@@ -163,8 +173,15 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
             return NULL;
         }
 
+        local_iothreads = g_new0(IOThread *, multithread_count);
         multithread_ctxs = g_new(AioContext *, multithread_count);
         i = 0;
+        holder_name = bdrv_get_node_name(bs);
+        IOThreadHolder holder = {
+            .type = IO_THREAD_HOLDER_KIND_BLOCK_NODE,
+            .u.block_node.node_name = (char *)holder_name,
+        };
+
         for (strList *e = iothread_list; e; e = e->next) {
             IOThread *iothread = iothread_by_id(e->value);
 
@@ -172,7 +189,9 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
                 error_setg(errp, "iothread \"%s\" not found", e->value);
                 goto fail;
             }
-            multithread_ctxs[i++] = iothread_get_aio_context(iothread);
+            local_iothreads[i] = iothread;
+            multithread_ctxs[i++] = iothread_ref_and_get_aio_context(iothread,
+                                                                     &holder);
         }
         assert(i == multithread_count);
     }
@@ -225,12 +244,15 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
     assert(drv->instance_size >= sizeof(BlockExport));
     exp = g_malloc0(drv->instance_size);
     *exp = (BlockExport) {
-        .drv        = drv,
-        .refcount   = 1,
-        .user_owned = true,
-        .id         = g_strdup(export->id),
-        .ctx        = ctx,
-        .blk        = blk,
+        .drv                  = drv,
+        .refcount             = 1,
+        .user_owned           = true,
+        .id                   = g_strdup(export->id),
+        .ctx                  = ctx,
+        .blk                  = blk,
+        .iothreads            = g_steal_pointer(&local_iothreads),
+        .iothread_count       = multithread_count,
+        .iothread_holder_name = g_strdup(holder_name),
     };
 
     ret = drv->create(exp, export, multithread_ctxs, multithread_count, errp);

block/export/export.c fail: · line 278

Quoted 4 lines
block/export/export.c +1 -0
@@ -253,6 +275,18 @@ fail:
         g_free(exp->id);
         g_free(exp);
     }
+    if (local_iothreads) {

This doesn't handle the drv->create() goto fail code path where g_steal_pointer(&local_iothreads) has moved the pointer into exp. local_iothreads will be leaked.

iothread_holder_name will also be leaked (it should be handled like g_free(exp->id) above).

Quoted 19 lines
block/export/export.c +12 -0
+        IOThreadHolder holder = {
+            .type = IO_THREAD_HOLDER_KIND_BLOCK_NODE,
+            .u.block_node.node_name = (char *)holder_name,
+        };
+
+        for (size_t j = 0; j < multithread_count; j++) {
+            if (local_iothreads[j]) {
+                iothread_put_aio_context(local_iothreads[j], &holder);
+            }
+        }
+    }
     g_free(multithread_ctxs);
     return NULL;
 }
@@ -269,6 +303,17 @@ static void blk_exp_delete_bh(void *opaque)
     BlockExport *exp = opaque;
 
     assert(exp->refcount == 0);
+    if (exp->iothreads) {

block/export/export.c static void blk_exp_delete_bh(void *opaque) · line 316

Quoted 10 lines
block/export/export.c +10 -0
+        IOThreadHolder holder = {
+            .type = IO_THREAD_HOLDER_KIND_BLOCK_NODE,
+            .u.block_node.node_name = (char *)exp->iothread_holder_name,
+        };
+
+        for (size_t i = 0; i < exp->iothread_count; i++) {
+            iothread_put_aio_context(exp->iothreads[i], &holder);
+        }
+        g_free(exp->iothreads);
+    }

iothread_holder_name is leaked.

block/export/export.c static void blk_exp_delete_bh(void *opaque) · line 318

Quoted 2 lines
block/export/export.c +0 -0
     QLIST_REMOVE(exp, next);
     exp->drv->delete(exp);

iothread_put_aio_context() must be moved after ->delete() since exports may still need to do some AioContext cleanup (e.g. vduse_blk_detach_ctx()).

Quoted 14 lines
block/export/export.c +0 -0
     blk_set_dev_ops(exp->blk, NULL, NULL);
include/block/export.h +1 -0
index ca45da928c..2bb98aae31 100644
@@ -16,6 +16,7 @@
 
 #include "qapi/qapi-types-block-export.h"
 #include "qemu/queue.h"
+#include "system/iothread.h"
 
 typedef struct BlockExport BlockExport;
 

include/block/export.h struct BlockExport { · line 94

Quoted 5 lines
include/block/export.h +2 -0
@@ -89,6 +90,11 @@ struct BlockExport {
 
     /* List entry for block_exports */
     QLIST_ENTRY(BlockExport) next;
+
+    /* The iothreads list for block_exports */

This comment is confusing: block_export is the global list of BlockExports. This iothreads[] array is not for block_exports, it's for this specific BlockExport only.

Quoted 9 lines
include/block/export.h +3 -0
+    IOThread **iothreads;
+    size_t iothread_count;
+    char *iothread_holder_name;
 };
 
 BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp);
Signature · 1 line
2.49.0
#11 lore raw

Re: [PATCH V9 03/17] iothread: tracking iothread users with holder name

In reply to Zhang Chen

include/system/iothread.h DECLARE_INSTANCE_CHECKER(IOThread, IOTHREAD, · line 76

Quoted 7 lines · Zhang Chen
include/system/iothread.h +3 -0
@@ -70,6 +71,9 @@ DECLARE_INSTANCE_CHECKER(IOThread, IOTHREAD,
 char *iothread_get_id(IOThread *iothread);
 IOThread *iothread_by_id(const char *id);
 AioContext *iothread_get_aio_context(IOThread *iothread);
+AioContext *iothread_ref_and_get_aio_context(IOThread *iothread,
+                                             const IOThreadHolder *holder);
+void iothread_put_aio_context(IOThread *iothread, const IOThreadHolder *holder);

There should be doc comments for these new APIs. One constraint that should be documented is that these APIs are not thread-safe and must be called under the Big QEMU Lock (BQL) (this is necessary both to avoid holders list race conditions and because object_unref() can invoke finalize callbacks that expect to run under the BQL).

author
#4 lore raw

Re: [PATCH V9 02/17] iothread: introduce iothread_ref/unref to track attached devices

In reply to Stefan Hajnoczi
Quoted 27 lines · Stefan Hajnoczi
Quoted 18 lines · Zhang Chen

Currently, IOThreads do not maintain a record of which devices are associated with them. This makes it difficult to monitor the workload distribution of IOThreads, especially in complex hotplug scenarios involving multiple virtio-blk or virtio-scsi devices.

This patch introduces a reference counting and tracking mechanism within the IOThread object:

- iothread_ref(): Prepends the device's IOThreadHolder to a list.
- iothread_unref(): Searches for the IOThreadHolder using a custom string comparison (g_strcmp0), releases the associated memory upon a successful match.
- holders: A GList storing the IOThreadHolder of attached devices for runtime introspection.

A later commit will add QMP commands to let management applications query the attachment status of IOThreads.

Quoted 6 lines
---
 include/system/iothread.h |  5 +++
 iothread.c                | 94 +++++++++++++++++++++++++++++++++++++++
 qapi/misc.json            | 61 +++++++++++++++++++++++++
 3 files changed, 160 insertions(+)
Quoted 3 lines

Minor comments below. This patch looks good overall:

Thanks~

Quoted 18 lines

include/system/iothread.h struct IOThread { · line 55

Quoted 12 lines
include/system/iothread.h +3 -0
index a1ef7696cb..b9207ad829 100644
@@ -50,6 +50,11 @@ struct IOThread {
     bool stopping;              /* has iothread_stop() been called? */
     bool running;               /* should iothread_run() continue? */
     int thread_id;
+    /*
+     * The list elements are of type IOThreadHolder, which can
+     * represent either a QOM path or a block node name.

This comment duplicates the IOThreadHolder types and will become outdated easily. It might already be outdated (there are 3 types, not just the 2 mentioned in the comment). I suggest replacing it with something like:

Quoted 2 lines

/* List of iothread_ref() holders (elements are of type IOThreadHolder) */

OK, Sorry, I forgot to update the comments with the patch version updates.

Quoted 28 lines
Quoted 28 lines
include/system/iothread.h +28 -0
+static int iothread_holder_compare(gconstpointer a, gconstpointer b)
+{
+    const IOThreadHolder *holder_a = a;
+    const IOThreadHolder *holder_b = b;
+    const char *name_a, *name_b;
+
+    if (holder_a->type != holder_b->type) {
+        return -1;
+    }
+
+    switch (holder_a->type) {
+    case IO_THREAD_HOLDER_KIND_QOM_OBJECT:
+        name_a = holder_a->u.qom_object.qom_path;
+        name_b = holder_b->u.qom_object.qom_path;
+        break;
+    case IO_THREAD_HOLDER_KIND_BLOCK_NODE:
+        name_a = holder_a->u.block_node.node_name;
+        name_b = holder_b->u.block_node.node_name;
+        break;
+    case IO_THREAD_HOLDER_KIND_MONITOR_NAME:
+        name_a = holder_a->u.monitor_name.monitor_name;
+        name_b = holder_b->u.monitor_name.monitor_name;
+        break;
+    default:
+       /*
+        * This should not happen. If it does, name_a/b remains
+        * NULL and g_strcmp0 will handle it safely.
+        */
Quoted 8 lines

include/system/iothread.h

Quoted 2 lines
include/system/iothread.h +2 -0
+        name_a = NULL;
+        name_b = NULL;

return -1 would be simpler and safer. g_strcmp0(NULL, NULL) returns 0, so callers will think they have found a match and proceed with their operation - it will likely result in undefined behavior since memory is probably corrupted if we got here. It's safer to fail the comparison so the caller does not proceed.

OK.

Thanks

Chen

author
#15 lore raw

Re: [PATCH V9 04/17] iothread: introduce iothread_unsafe_get_aio_context()

In reply to Stefan Hajnoczi
Quoted 18 lines · Stefan Hajnoczi

include/system/iothread.h DECLARE_INSTANCE_CHECKER(IOThread, IOTHREAD, · line 77

Quoted 12 lines · Zhang Chen
include/system/iothread.h +4 -0
index b483bbfab3..064c05e78d 100644
@@ -71,6 +71,12 @@ DECLARE_INSTANCE_CHECKER(IOThread, IOTHREAD,
 char *iothread_get_id(IOThread *iothread);
 IOThread *iothread_by_id(const char *id);
 AioContext *iothread_get_aio_context(IOThread *iothread);
+/*
+ * The iothread_unsafe_get_aio_context() is a low-level unsafe way of getting
+ * the AioContext, recommend migrating to the new API with IOThreadHolder
+ * as much as possible.

This should describe the safety concerns and when it is okay to use this API. Something like:

Quoted 6 lines

Normally the IOThread's AioContext is fetched using iothread_ref_and_get_aio_context(), but there are legacy callers without a clear ref/unref lifecycle. They cannot unref (e.g. because they do not have an IOThread pointer), so provide an unsafe way to fetch the AioContext without holding a reference count. This unsafe API serves legacy callers - do not use it in new code.

Looks better~ will change it in next version.

Thanks

Chen

Quoted 6 lines
Quoted 6 lines
include/system/iothread.h +2 -0
+ */
+AioContext *iothread_unsafe_get_aio_context(IOThread *iothread);
 AioContext *iothread_ref_and_get_aio_context(IOThread *iothread,
                                              const IOThreadHolder *holder);
 void iothread_put_aio_context(IOThread *iothread, const IOThreadHolder *holder);
author
#12 lore raw

Re: [PATCH V9 03/17] iothread: tracking iothread users with holder name

In reply to Stefan Hajnoczi
Quoted 15 lines · Stefan Hajnoczi

include/system/iothread.h DECLARE_INSTANCE_CHECKER(IOThread, IOTHREAD, · line 76

Quoted 7 lines · Zhang Chen
include/system/iothread.h +3 -0
@@ -70,6 +71,9 @@ DECLARE_INSTANCE_CHECKER(IOThread, IOTHREAD,
 char *iothread_get_id(IOThread *iothread);
 IOThread *iothread_by_id(const char *id);
 AioContext *iothread_get_aio_context(IOThread *iothread);
+AioContext *iothread_ref_and_get_aio_context(IOThread *iothread,
+                                             const IOThreadHolder *holder);
+void iothread_put_aio_context(IOThread *iothread, const IOThreadHolder *holder);

There should be doc comments for these new APIs. One constraint that should be documented is that these APIs are not thread-safe and must be called under the Big QEMU Lock (BQL) (this is necessary both to avoid holders list race conditions and because object_unref() can invoke finalize callbacks that expect to run under the BQL).

OK, I will add comments like this in next version:
+ /*

 +  * The iothread_get_aio_context() and iothread_put_aio_context() are not
 +  * thread-safe and must be called under the Big QEMU Lock (BQL).
 +  */

Thanks
Chen

author
#18 lore raw

Re: [PATCH V9 05/17] block/export: track IOThread reference in BlockExport

In reply to Stefan Hajnoczi
Quoted 24 lines · Stefan Hajnoczi
Quoted 9 lines · Zhang Chen

Users currently lack visibility into which block exports are utilizing specific IOThreads. This patch integrates IOThread referencing into the BlockExport lifecycle.

- Add iothreads array and holder_name to BlockExport struct.
- Use iothread_ref_and_get_aio_context during export creation.
- Implement proper cleanup in blk_exp_add fail path and blk_exp_delete_bh.
- Support both single and multi-iothread export configurations.

Quoted 8 lines

This ensures IOThread 'holders' status correctly reflects active block exports for better debugging and resource tracking.

---
 block/export/export.c  | 63 ++++++++++++++++++++++++++++++++++++------
 include/block/export.h |  6 ++++
 2 files changed, 60 insertions(+), 9 deletions(-)

If AI was used to generate this patch series, please see QEMU's AI policy:
https://www.qemu.org/docs/master/devel/code-provenance.html#use-of-ai-generated-content

Quoted 6 lines

This feels like AI generated code in that it is verbose and has lots of structure (e.g. commit messages that look like an AI summary of the code change), but the details are not correct (misleading comments, leaks, etc).

I will pause my review at this patch and wait for your response.

I used the AI for parts of commit messages.
Please continue to review code parts and skip the commit message parts, I will remove all AI-generated commit messages in next version.

Sorry, I just noticed the discussion:
[PATCH v2] docs/devel: relax policy on AI-generated contributions

Quoted 27 lines
Quoted 27 lines
block/export/export.c +2 -1
index b733f269f3..b6c07f69b5 100644
@@ -15,7 +15,6 @@
 
 #include "block/block.h"
 #include "system/block-backend.h"
-#include "system/iothread.h"
 #include "block/export.h"
 #include "block/fuse.h"
 #include "block/nbd.h"
@@ -85,6 +84,8 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
     AioContext *ctx;
     AioContext **multithread_ctxs = NULL;
     size_t multithread_count = 0;
+    g_autofree IOThread **local_iothreads = NULL;
+    const char *holder_name = NULL;
     uint64_t perm;
     int ret;
 
@@ -139,7 +140,16 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
             goto fail;
         }
 
Quoted 9 lines

block/export/export.c BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp) · line 145

Quoted 4 lines
block/export/export.c +3 -1
-        new_ctx = iothread_get_aio_context(iothread);
+        holder_name = bdrv_get_node_name(bs);
+        IOThreadHolder holder = {
+            .type = IO_THREAD_HOLDER_KIND_BLOCK_NODE,

This is not a block node, this is a block export (a separate type of object). Reporting the export as a node is confusing because users will not know which block export id is assigned using this IOThread, they will only know the block node (which could be doing other things too).

OK, it looks need to change the type name from:
IO_THREAD_HOLDER_KIND_BLOCK_NODE
to:
IO_THREAD_HOLDER_KIND_BLOCK_EXPORT

Quoted 9 lines

block/export/export.c BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp) · line 146

Quoted 2 lines
block/export/export.c +1 -0
+            .u.block_node.node_name = (char *)holder_name,

To remove the suspicious-looking const cast, try:

  const IOThreadHolder holder = {
      .type = ...,
      .u.block_node.node_name = holder_name,
  };

OK.

Quoted 69 lines
Quoted 60 lines
block/export/export.c +25 -7
+        };
+
+        new_ctx = iothread_ref_and_get_aio_context(iothread, &holder);
+        multithread_count = 1;
+        local_iothreads = g_new0(IOThread *, 1);
+        local_iothreads[0] = iothread;
 
         /* Ignore errors with fixed-iothread=false */
         set_context_errp = fixed_iothread ? errp : NULL;
@@ -163,8 +173,15 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
             return NULL;
         }
 
+        local_iothreads = g_new0(IOThread *, multithread_count);
         multithread_ctxs = g_new(AioContext *, multithread_count);
         i = 0;
+        holder_name = bdrv_get_node_name(bs);
+        IOThreadHolder holder = {
+            .type = IO_THREAD_HOLDER_KIND_BLOCK_NODE,
+            .u.block_node.node_name = (char *)holder_name,
+        };
+
         for (strList *e = iothread_list; e; e = e->next) {
             IOThread *iothread = iothread_by_id(e->value);
 
@@ -172,7 +189,9 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
                 error_setg(errp, "iothread \"%s\" not found", e->value);
                 goto fail;
             }
-            multithread_ctxs[i++] = iothread_get_aio_context(iothread);
+            local_iothreads[i] = iothread;
+            multithread_ctxs[i++] = iothread_ref_and_get_aio_context(iothread,
+                                                                     &holder);
         }
         assert(i == multithread_count);
     }
@@ -225,12 +244,15 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
     assert(drv->instance_size >= sizeof(BlockExport));
     exp = g_malloc0(drv->instance_size);
     *exp = (BlockExport) {
-        .drv        = drv,
-        .refcount   = 1,
-        .user_owned = true,
-        .id         = g_strdup(export->id),
-        .ctx        = ctx,
-        .blk        = blk,
+        .drv                  = drv,
+        .refcount             = 1,
+        .user_owned           = true,
+        .id                   = g_strdup(export->id),
+        .ctx                  = ctx,
+        .blk                  = blk,
+        .iothreads            = g_steal_pointer(&local_iothreads),
+        .iothread_count       = multithread_count,
+        .iothread_holder_name = g_strdup(holder_name),
     };
 
     ret = drv->create(exp, export, multithread_ctxs, multithread_count, errp);

block/export/export.c fail: · line 278

Quoted 4 lines
block/export/export.c +1 -0
@@ -253,6 +275,18 @@ fail:
         g_free(exp->id);
         g_free(exp);
     }
+    if (local_iothreads) {

This doesn't handle the drv->create() goto fail code path where g_steal_pointer(&local_iothreads) has moved the pointer into exp. local_iothreads will be leaked.

Quoted 2 lines

iothread_holder_name will also be leaked (it should be handled like g_free(exp->id) above).

OK, will fix in next version.

Quoted 18 lines
Quoted 18 lines
block/export/export.c +11 -0
+        IOThreadHolder holder = {
+            .type = IO_THREAD_HOLDER_KIND_BLOCK_NODE,
+            .u.block_node.node_name = (char *)holder_name,
+        };
+
+        for (size_t j = 0; j < multithread_count; j++) {
+            if (local_iothreads[j]) {
+                iothread_put_aio_context(local_iothreads[j], &holder);
+            }
+        }
+    }
     g_free(multithread_ctxs);
     return NULL;
 }
@@ -269,6 +303,17 @@ static void blk_exp_delete_bh(void *opaque)
     BlockExport *exp = opaque;
 
Quoted 14 lines

block/export/export.c static void blk_exp_delete_bh(void *opaque) · line 316

Quoted 12 lines
block/export/export.c +11 -0
     assert(exp->refcount == 0);
+    if (exp->iothreads) {
+        IOThreadHolder holder = {
+            .type = IO_THREAD_HOLDER_KIND_BLOCK_NODE,
+            .u.block_node.node_name = (char *)exp->iothread_holder_name,
+        };
+
+        for (size_t i = 0; i < exp->iothread_count; i++) {
+            iothread_put_aio_context(exp->iothreads[i], &holder);
+        }
+        g_free(exp->iothreads);
+    }

iothread_holder_name is leaked.

Will fix in next version.

Thanks
Chen

Quoted 41 lines

block/export/export.c static void blk_exp_delete_bh(void *opaque) · line 318

Quoted 3 lines
block/export/export.c +0 -0
     QLIST_REMOVE(exp, next);
     exp->drv->delete(exp);

iothread_put_aio_context() must be moved after ->delete() since exports may still need to do some AioContext cleanup (e.g. vduse_blk_detach_ctx()).

Quoted 14 lines
block/export/export.c +0 -0
     blk_set_dev_ops(exp->blk, NULL, NULL);
include/block/export.h +1 -0
index ca45da928c..2bb98aae31 100644
@@ -16,6 +16,7 @@
 
 #include "qapi/qapi-types-block-export.h"
 #include "qemu/queue.h"
+#include "system/iothread.h"
 
 typedef struct BlockExport BlockExport;
 

include/block/export.h struct BlockExport { · line 94

Quoted 5 lines
include/block/export.h +2 -0
@@ -89,6 +90,11 @@ struct BlockExport {
 
     /* List entry for block_exports */
     QLIST_ENTRY(BlockExport) next;
+
+    /* The iothreads list for block_exports */

This comment is confusing: block_export is the global list of BlockExports. This iothreads[] array is not for block_exports, it's for this specific BlockExport only.

Quoted 9 lines
include/block/export.h +3 -1
+    IOThread **iothreads;
+    size_t iothread_count;
+    char *iothread_holder_name;
 };
 
 BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp);
--

2.49.0

#19 lore raw

Re: [PATCH V9 05/17] block/export: track IOThread reference in BlockExport

Quoted 30 lines · Zhang Chen
Quoted 29 lines · Stefan Hajnoczi
Quoted 9 lines · Zhang Chen

Users currently lack visibility into which block exports are utilizing specific IOThreads. This patch integrates IOThread referencing into the BlockExport lifecycle.

- Add iothreads array and holder_name to BlockExport struct.
- Use iothread_ref_and_get_aio_context during export creation.
- Implement proper cleanup in blk_exp_add fail path and blk_exp_delete_bh.
- Support both single and multi-iothread export configurations.

Quoted 8 lines

This ensures IOThread 'holders' status correctly reflects active block exports for better debugging and resource tracking.

---
 block/export/export.c  | 63 ++++++++++++++++++++++++++++++++++++------
 include/block/export.h |  6 ++++
 2 files changed, 60 insertions(+), 9 deletions(-)

If AI was used to generate this patch series, please see QEMU's AI policy:
https://www.qemu.org/docs/master/devel/code-provenance.html#use-of-ai-generated-content

This feels like AI generated code in that it is verbose and has lots of structure (e.g. commit messages that look like an AI summary of the code change), but the details are not correct (misleading comments, leaks, etc).

Quoted 5 lines
Quoted 1 line

I will pause my review at this patch and wait for your response.

I used the AI for parts of commit messages.
Please continue to review code parts and skip the commit message parts, I will remove all AI-generated commit messages in next version.

Okay, thanks. There is no need to enumerate the code changes in the commit message unless there is some rationale or commentary you want to share so that people understand why you did things in a certain way.

Quoted 2 lines

Sorry, I just noticed the discussion:
[PATCH v2] docs/devel: relax policy on AI-generated contributions

The new policy will allow some use of AI generated content when the policy comes into effect. For now we still have to follow the existing policy.

Quoted 39 lines
Quoted 29 lines
Quoted 29 lines
block/export/export.c +2 -1
index b733f269f3..b6c07f69b5 100644
@@ -15,7 +15,6 @@
 
 #include "block/block.h"
 #include "system/block-backend.h"
-#include "system/iothread.h"
 #include "block/export.h"
 #include "block/fuse.h"
 #include "block/nbd.h"
@@ -85,6 +84,8 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
     AioContext *ctx;
     AioContext **multithread_ctxs = NULL;
     size_t multithread_count = 0;
+    g_autofree IOThread **local_iothreads = NULL;
+    const char *holder_name = NULL;
     uint64_t perm;
     int ret;
 
@@ -139,7 +140,16 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
             goto fail;
         }
 
Quoted 9 lines

block/export/export.c BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp) · line 145

Quoted 4 lines
block/export/export.c +3 -1
-        new_ctx = iothread_get_aio_context(iothread);
+        holder_name = bdrv_get_node_name(bs);
+        IOThreadHolder holder = {
+            .type = IO_THREAD_HOLDER_KIND_BLOCK_NODE,

This is not a block node, this is a block export (a separate type of object). Reporting the export as a node is confusing because users will not know which block export id is assigned using this IOThread, they will only know the block node (which could be doing other things too).

Quoted 4 lines

OK, it looks need to change the type name from:
IO_THREAD_HOLDER_KIND_BLOCK_NODE
to:
IO_THREAD_HOLDER_KIND_BLOCK_EXPORT

Yes.

#20 lore raw

Re: [PATCH V9 05/17] block/export: track IOThread reference in BlockExport

In reply to Zhang Chen
Quoted 30 lines · Zhang Chen
Quoted 29 lines · Stefan Hajnoczi
Quoted 9 lines · Zhang Chen

Users currently lack visibility into which block exports are utilizing specific IOThreads. This patch integrates IOThread referencing into the BlockExport lifecycle.

- Add iothreads array and holder_name to BlockExport struct.
- Use iothread_ref_and_get_aio_context during export creation.
- Implement proper cleanup in blk_exp_add fail path and blk_exp_delete_bh.
- Support both single and multi-iothread export configurations.

Quoted 8 lines

This ensures IOThread 'holders' status correctly reflects active block exports for better debugging and resource tracking.

---
 block/export/export.c  | 63 ++++++++++++++++++++++++++++++++++++------
 include/block/export.h |  6 ++++
 2 files changed, 60 insertions(+), 9 deletions(-)

If AI was used to generate this patch series, please see QEMU's AI policy:
https://www.qemu.org/docs/master/devel/code-provenance.html#use-of-ai-generated-content

This feels like AI generated code in that it is verbose and has lots of structure (e.g. commit messages that look like an AI summary of the code change), but the details are not correct (misleading comments, leaks, etc).

Quoted 8 lines
Quoted 1 line

I will pause my review at this patch and wait for your response.

I used the AI for parts of commit messages.
Please continue to review code parts and skip the commit message parts, I will remove all AI-generated commit messages in next version.

Sorry, I just noticed the discussion:
[PATCH v2] docs/devel: relax policy on AI-generated contributions

By the way, the existing policy already allows using AI for code review. I recommend doing this before sending patches.

Stefan

#5 lore raw

Re: [PATCH V9 02/17] iothread: introduce iothread_ref/unref to track attached devices

In reply to Zhang Chen

Looks like you missed my review of v8. I'm replaying my review comments.

Quoted 30 lines · Zhang Chen

Currently, IOThreads do not maintain a record of which devices are associated with them. This makes it difficult to monitor the workload distribution of IOThreads, especially in complex hotplug scenarios involving multiple virtio-blk or virtio-scsi devices.

This patch introduces a reference counting and tracking mechanism within the IOThread object:

- iothread_ref(): Prepends the device's IOThreadHolder to a list.
- iothread_unref(): Searches for the IOThreadHolder using a custom string comparison (g_strcmp0), releases the associated memory upon a successful match.
- holders: A GList storing the IOThreadHolder of attached devices for runtime introspection.

A later commit will add QMP commands to let management applications query the attachment status of IOThreads.

---
 include/system/iothread.h |  5 +++
 iothread.c                | 94 +++++++++++++++++++++++++++++++++++++++
 qapi/misc.json            | 61 +++++++++++++++++++++++++
 3 files changed, 160 insertions(+)
include/system/iothread.h +0 -0
index a1ef7696cb..b9207ad829 100644

include/system/iothread.h struct IOThread { · line 56

Quoted 7 lines
include/system/iothread.h +4 -0
@@ -50,6 +50,11 @@ struct IOThread {
     bool stopping;              /* has iothread_stop() been called? */
     bool running;               /* should iothread_run() continue? */
     int thread_id;
+    /*
+     * The list elements are of type IOThreadHolder, which can
+     * represent either a QOM path or a block node name.
+     */

You forgot monitors. Stefan spotted this as well, and I agree with the fix he suggested.

Quoted 30 lines
include/system/iothread.h +1 -0
+    GList *holders;
 
     /* AioContext poll parameters */
     int64_t poll_max_ns;
iothread.c +18 -0
index 3558535b40..3301b8d495 100644
@@ -25,6 +25,92 @@
 #include "qemu/rcu.h"
 #include "qemu/main-loop.h"
 
+/*
+ * iothread_ref:
+ * @iothread: the iothread to track
+ * @holder: the IOThreadHolder object initialized by the caller
+ *
+ * Add the @holder to the iothread's tracking list.
+ */
+static void iothread_ref(IOThread *iothread, const IOThreadHolder *holder)
+{
+    assert(holder);
+    IOThreadHolder *h = g_new0(IOThreadHolder, 1);
+
+    h->type = holder->type;
+    switch (holder->type) {
+    case IO_THREAD_HOLDER_KIND_QOM_OBJECT:
+        h->u.qom_object.qom_path = g_strdup(holder->u.qom_object.qom_path);
+        break;
+    case IO_THREAD_HOLDER_KIND_BLOCK_NODE:

iothread.c line 55

Quoted 10 lines
iothread.c +10 -0
+        h->u.block_node.node_name =
+            g_strdup(holder->u.block_node.node_name);
+        break;
+    case IO_THREAD_HOLDER_KIND_MONITOR_NAME:
+        h->u.monitor_name.monitor_name =
+            g_strdup(holder->u.monitor_name.monitor_name);
+        break;
+    default:
+        g_assert_not_reached();
+    }

This makes @h a deep copy of @holder. Try

    IOThreadHolder *h = QAPI_CLONE(IOThreadHolder, holder);

instead.

Instead of making a copy, the function could take ownership of @holder, and require @holder's members to be dynamically allocated. More efficient, but complicates the callers somewhat. Not worthwhile, I guess.

iothread.c line 67

Quoted 12 lines
iothread.c +12 -0
+
+    iothread->holders = g_list_prepend(iothread->holders, h);
+}
+
+static int iothread_holder_compare(gconstpointer a, gconstpointer b)
+{
+    const IOThreadHolder *holder_a = a;
+    const IOThreadHolder *holder_b = b;
+    const char *name_a, *name_b;
+
+    if (holder_a->type != holder_b->type) {
+        return -1;

Please use

           return holder_b->type - holder_a->type;

Why? By convention, an int-valued function fn() comparing A to B returns less than 0 when A<B, 0 when A == B, and greater than 0 when A>B. Anything else needs a prominent comment. Best to stick to the convention whenever practical.

Since A < B iff B > A, we want fn(A, B) == -fn(B, A).

Your function does not satisfy that! Harmless for now, because you only ever pass it to g_list_find_custom(), which treats all non-zero values the same. But if anyone passes it to something like qsort() in the future, they can look forward to some debugging "fun".

Quoted 12 lines
iothread.c +12 -0
+    }
+
+    switch (holder_a->type) {
+    case IO_THREAD_HOLDER_KIND_QOM_OBJECT:
+        name_a = holder_a->u.qom_object.qom_path;
+        name_b = holder_b->u.qom_object.qom_path;
+        break;
+    case IO_THREAD_HOLDER_KIND_BLOCK_NODE:
+        name_a = holder_a->u.block_node.node_name;
+        name_b = holder_b->u.block_node.node_name;
+        break;
+    case IO_THREAD_HOLDER_KIND_MONITOR_NAME:

iothread.c line 89

Quoted 10 lines
iothread.c +10 -0
+        name_a = holder_a->u.monitor_name.monitor_name;
+        name_b = holder_b->u.monitor_name.monitor_name;
+        break;
+    default:
+       /*
+        * This should not happen. If it does, name_a/b remains
+        * NULL and g_strcmp0 will handle it safely.
+        */
+        name_a = NULL;
+        name_b = NULL;

Replace this by

           g_assert_not_reached();

Then neither @name_a nor @name_b can be null, because a non-optional QAPI str is never null. Simply use strcmp().

Quoted 12 lines
iothread.c +12 -0
+    }
+
+    return g_strcmp0(name_a, name_b);
+}
+
+/*
+ * This function removes the @holder from the @iothread's tracking list.
+ * The @holder must match the one used previously in iothread_ref().
+ * It is a programming error to call this with a @holder that is not
+ * currently associated with the @iothread.
+ */
+static void iothread_unref(IOThread *iothread, const IOThreadHolder *holder)

iothread.c line 111

Quoted 10 lines
iothread.c +10 -0
+{
+    assert(holder);
+    GList *link = g_list_find_custom(iothread->holders, holder,
+                                     (GCompareFunc)iothread_holder_compare);
+
+    assert(link);
+
+    IOThreadHolder *h = (IOThreadHolder *)link->data;
+    qapi_free_IOThreadHolder(h);
+    iothread->holders = g_list_delete_link(iothread->holders, link);

Removing @link from the list before freeing link->data would be more obviously correct, wouldn't it?

Quoted 6 lines
iothread.c +2 -0
+}
+
 static void *iothread_run(void *opaque)
 {
     IOThread *iothread = opaque;

iothread.c char *iothread_get_id(IOThread *iothread) · line 451

Quoted 10 lines
iothread.c +7 -0
@@ -356,6 +442,14 @@ char *iothread_get_id(IOThread *iothread)
 
 AioContext *iothread_get_aio_context(IOThread *iothread)
 {
+    /* Remove in next patch for build */
+    IOThreadHolder holder = {
+        .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
+        .u.qom_object.qom_path = (char *)"tmp_path",
+    };
+    iothread_ref(iothread, &holder);
+    iothread_unref(iothread, &holder);

Is this really needed to make this patch work?

Quoted 9 lines
iothread.c +1 -0
+
     return iothread->ctx;
 }
 
qapi/misc.json +0 -0
index c71a5fe657..d9f82f0922 100644

qapi/misc.json line 74

Quoted 8 lines
qapi/misc.json +5 -0
@@ -67,6 +67,67 @@
 ##
 { 'command': 'query-name', 'returns': 'NameInfo', 'allow-preconfig': true }
 
+
+##
+# @IOThreadHolderBlockNode:
+#
+# @node-name: A block node.

Let's use

# @node-name: Name of the block node

to make it more similar to node-name descriptions elsewhere.

qapi/misc.json line 85

Quoted 11 lines
qapi/misc.json +11 -0
+#
+# Since: 11.1
+#
+##
+{ 'struct': 'IOThreadHolderBlockNode',
+  'data': { 'node-name': 'str' } }
+
+##
+# @IOThreadHolderQomObject:
+#
+# @qom-path: A QOM Object.

Likewise, let's use

# @qom-path: Path to the object in the QOM tree

qapi/misc.json line 96

Quoted 11 lines
qapi/misc.json +11 -0
+#
+# Since: 11.1
+#
+##
+{ 'struct': 'IOThreadHolderQomObject',
+  'data': { 'qom-path': 'str' } }
+
+##
+# @IOThreadHolderMonitor:
+#
+# @monitor-name: A HMP/QMP monitor.

Let's use

# @monitor-name: Name of the HMP/QMP monitor

qapi/misc.json line 109

Quoted 13 lines
qapi/misc.json +13 -0
+#
+# Since: 11.1
+#
+##
+{ 'struct': 'IOThreadHolderMonitor',
+  'data': { 'monitor-name': 'str' } }
+
+##
+# @IOThreadHolderKind:
+#
+# @block-node: A block node.
+# @qom-object: A QOM Object.
+# @monitor-name: A HMP/QMP monitor.

Rename to @monitor for consistency with the other two.

Remark, not a request to change anything in your patch: kind monitor will become obsolete when monitors become QOM objects. Doing that before we release this interface is desirable.

qapi/misc.json line 118

Quoted 9 lines
qapi/misc.json +9 -0
+#
+# Since: 11.1
+##
+{ 'enum': 'IOThreadHolderKind',
+  'data': [ 'block-node', 'qom-object', 'monitor-name' ] }
+
+##
+# @IOThreadHolder:
+#

Let's add a short description:

# The block node, QOM object, or monitor holding the I/O thread.

Quoted 15 lines
qapi/misc.json +12 -0
+# @type: the kind of I/O thread holder.
+#
+# Since: 11.1
+##
+{ 'union': 'IOThreadHolder',
+  'base': { 'type': 'IOThreadHolderKind' },
+  'discriminator': 'type',
+  'data': {
+    'block-node': 'IOThreadHolderBlockNode',
+    'qom-object': 'IOThreadHolderQomObject',
+    'monitor-name': 'IOThreadHolderMonitor' } }
+
 ##
 # @IOThreadInfo:
 #
#24 lore raw

Re: [PATCH V9 06/17] monitor: refactor monitor_data_init() to pass ID

In reply to Zhang Chen
Quoted 2 lines · Zhang Chen

Anonymous monitors (e.g., '-monitor stdio' or GDB stub) leave their 'id' field as NULL.

What "'id' field" do you mean here?

Quoted 9 lines

To allow downstream subsystems (like iothread tracking) to
identify these instances later, the initialization paths must propagate the context.

Refactor monitor_data_init() to accept the 'id' parameters, and update all internal QMP/HMP paths to pass these identifiers.

For anonymous monitors, 'mon->id' remains NULL at this stage to avoid namespace pollution, setting up infrastructure for subsequent patches.

What's an "anonymous monitor"?

If it's a monitor without an ID, the sentence is a tautology. What information are you trying to convey? That the new @id member may be null?

Quoted 18 lines
---
 chardev/char.c             | 2 +-
 gdbstub/system.c           | 3 ++-
 include/monitor/monitor.h  | 5 +++--
 monitor/hmp.c              | 5 +++--
 monitor/monitor-internal.h | 4 +++-
 monitor/monitor.c          | 9 ++++++---
 monitor/qmp-cmds.c         | 2 +-
 monitor/qmp.c              | 5 +++--
 stubs/monitor-internal.c   | 3 ++-
 9 files changed, 24 insertions(+), 14 deletions(-)
chardev/char.c +0 -0
index ca8b37ed8d..f057247001 100644

chardev/char.c static Chardev *qemu_chr_new_from_name(const char *label, const char *filename, · line 808

Quoted 5 lines
chardev/char.c +1 -1
@@ -805,7 +805,7 @@ static Chardev *qemu_chr_new_from_name(const char *label, const char *filename,
 
     if (qemu_opt_get_bool(opts, "mux", 0)) {
         assert(permit_mux_mon);
-        monitor_init_hmp(chr, true, &err);
+        monitor_init_hmp(chr, true, NULL, &err);

This creates an HMP monitor connected to a "multiplexed" character device ("mux=on"). This is an old, poorly documented feature.

Quoted 8 lines
chardev/char.c +0 -0
         if (err) {
             error_report_err(err);
             object_unparent(OBJECT(chr));
gdbstub/system.c +0 -0
index e86c5870ab..50f934fde3 100644

gdbstub/system.c bool gdbserver_start(const char *device, Error **errp) · line 392

Quoted 6 lines
gdbstub/system.c +2 -1
@@ -388,7 +388,8 @@ bool gdbserver_start(const char *device, Error **errp)
         /* Initialize a monitor terminal for gdb */
         mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
                                    NULL, NULL, &error_abort);
-        monitor_init_hmp(mon_chr, false, &error_abort);
+
+        monitor_init_hmp(mon_chr, false, NULL, &error_abort);

This creates a monitor connected to gdbstub's "chardev-gdb" character device. I'm not sure this is actually an HMP monitor.

Quoted 8 lines
gdbstub/system.c +0 -0
     } else {
         qemu_chr_fe_deinit(&gdbserver_system_state.chr, true);
         mon_chr = gdbserver_system_state.mon_chr;
include/monitor/monitor.h +0 -0
index 55649a8664..4a5eb20bea 100644

include/monitor/monitor.h bool monitor_cur_is_qmp(void); · line 22

Quoted 6 lines
include/monitor/monitor.h +1 -2
@@ -19,8 +19,9 @@ bool monitor_cur_is_qmp(void);
 
 void monitor_init_globals(void);
 void monitor_init_globals_core(void);
-void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp);
-void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp);
+void monitor_init_qmp(Chardev *chr, bool pretty, const char *id, Error **errp);

Break the line before Error, please.

include/monitor/monitor.h bool monitor_cur_is_qmp(void); · line 24

Quoted 2 lines
include/monitor/monitor.h +2 -0
+void monitor_init_hmp(Chardev *chr, bool use_readline, const char *id,
+                      Error **errp);

I'd add the @id parameter before the flags. Same for monitor_data_init().

Quoted 53 lines
include/monitor/monitor.h +0 -0
 int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp);
 int monitor_init_opts(QemuOpts *opts, Error **errp);
 void monitor_cleanup(void);
monitor/hmp.c +3 -2
index cc4390486e..4ee8cb58d1 100644
@@ -1522,7 +1522,8 @@ static void monitor_readline_flush(void *opaque)
     monitor_flush(&mon->common);
 }
 
-void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
+void monitor_init_hmp(Chardev *chr, bool use_readline, const char *id,
+                      Error **errp)
 {
     MonitorHMP *mon = g_new0(MonitorHMP, 1);
 
@@ -1531,7 +1532,7 @@ void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
         return;
     }
 
-    monitor_data_init(&mon->common, false, false, false);
+    monitor_data_init(&mon->common, false, false, false, id);
 
     mon->use_readline = use_readline;
     if (mon->use_readline) {
monitor/monitor-internal.h +3 -1
index a5c4aba306..f651ba6a90 100644
@@ -108,6 +108,8 @@ struct Monitor {
     bool skip_flush;
     bool use_io_thread;
 
+    char *id;
+
     char *mon_cpu_path;
     QTAILQ_ENTRY(Monitor) entry;
 
@@ -179,7 +181,7 @@ extern QemuMutex monitor_lock;
 extern MonitorList mon_list;
 
 void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
-                       bool use_io_thread);
+                       bool use_io_thread, const char *id);
 void monitor_data_destroy(Monitor *mon);
 int monitor_can_read(void *opaque);
 void monitor_list_append(Monitor *mon);
monitor/monitor.c +0 -0
index 00b93ed612..65749f931e 100644

monitor/monitor.c static void monitor_iothread_init(void) · line 613

Quoted 7 lines
monitor/monitor.c +2 -1
@@ -608,8 +608,10 @@ static void monitor_iothread_init(void)
 }
 
 void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
-                       bool use_io_thread)
+                       bool use_io_thread, const char *id)
 {
+    mon->id = id ? g_strdup(id) : NULL;

g_strdup(NULL) returns NULL. Simplify:

       mon->id = g_strdup(id);
Quoted 13 lines
monitor/monitor.c +2 -0
+
     if (use_io_thread && !mon_iothread) {
         monitor_iothread_init();
     }
@@ -629,6 +631,7 @@ void monitor_data_destroy(Monitor *mon)
     } else {
         readline_free(container_of(mon, MonitorHMP, common)->rs);
     }
+    g_free(mon->id);
     g_string_free(mon->outbuf, true);
     qemu_mutex_destroy(&mon->mon_lock);
 }

monitor/monitor.c int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp) · line 738

Quoted 5 lines
monitor/monitor.c +1 -1
@@ -732,7 +735,7 @@ int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp)
 
     switch (opts->mode) {
     case MONITOR_MODE_CONTROL:
-        monitor_init_qmp(chr, opts->pretty, errp);
+        monitor_init_qmp(chr, opts->pretty, opts->id, errp);

This creates a QMP monitor on behalf of -mon or one of the convenience options.

For -mon, opts->id comes from the command line, and may be null.

For the convenience options, opts->id is made up by the system.

monitor/monitor.c int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp) · line 749

Quoted 9 lines
monitor/monitor.c +1 -1
         break;
     case MONITOR_MODE_READLINE:
         if (!allow_hmp) {
@@ -743,7 +746,7 @@ int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp)
             error_setg(errp, "'pretty' is not compatible with HMP monitors");
             return -1;
         }
-        monitor_init_hmp(chr, true, errp);
+        monitor_init_hmp(chr, true, opts->id, errp);

Likewise, HMP.

monitor/qmp-cmds.c char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index, · line 171

Quoted 13 lines
monitor/monitor.c +0 -0
         break;
     default:
         g_assert_not_reached();
monitor/qmp-cmds.c +1 -1
index 0c409c27dc..e49b9cbf8b 100644
@@ -168,7 +168,7 @@ char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
     char *output = NULL;
     MonitorHMP hmp = {};
 
-    monitor_data_init(&hmp.common, false, true, false);
+    monitor_data_init(&hmp.common, false, true, false, NULL);

This sets up a dummy HMP monitor not connected to anything.

Quoted 31 lines
monitor/qmp-cmds.c +0 -0
     if (has_cpu_index) {
         int ret = monitor_set_cpu(&hmp.common, cpu_index);
monitor/qmp.c +3 -2
index 687019811f..8fd71386e8 100644
@@ -513,7 +513,7 @@ static void monitor_qmp_setup_handlers_bh(void *opaque)
     monitor_list_append(&mon->common);
 }
 
-void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
+void monitor_init_qmp(Chardev *chr, bool pretty, const char *id, Error **errp)
 {
     MonitorQMP *mon = g_new0(MonitorQMP, 1);
 
@@ -525,7 +525,8 @@ void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
 
     /* Note: we run QMP monitor in I/O thread when @chr supports that */
     monitor_data_init(&mon->common, true, false,
-                      qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT));
+                      qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT),
+                      id);
 
     mon->pretty = pretty;
 
stubs/monitor-internal.c +0 -0
index 4fece49d53..325a559e62 100644

stubs/monitor-internal.c int monitor_get_fd(Monitor *mon, const char *name, Error **errp) · line 14

Quoted 8 lines
stubs/monitor-internal.c +2 -1
@@ -8,6 +8,7 @@ int monitor_get_fd(Monitor *mon, const char *name, Error **errp)
     return -1;
 }
 
-void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
+void monitor_init_hmp(Chardev *chr, bool use_readline, const char *id,
+                      Error **errp)
 {
 }

Things the commit message should make clear, but doesn't:

* This patch adds member @id to struct Monitor. It is initialized, but not yet used.

* If the monitor is created on behalf of CLI option -mon, then @id is the value of -mon's parameter "id" if present, null if absent.

* If the monitor is created on behalf of CLI convenience options, then @id is some made-up string.

* If the monitor is created in any other way, then @id is null.

Please rework your commit message accordingly.

#25 lore raw

Re: [PATCH V9 06/17] monitor: refactor monitor_data_init() to pass ID

In reply to Zhang Chen
Quoted 12 lines · Zhang Chen

Anonymous monitors (e.g., '-monitor stdio' or GDB stub) leave their 'id' field as NULL. To allow downstream subsystems (like iothread tracking) to identify these instances later, the initialization paths must propagate the context.

Refactor monitor_data_init() to accept the 'id' parameters, and update all internal QMP/HMP paths to pass these identifiers.

For anonymous monitors, 'mon->id' remains NULL at this stage to avoid namespace pollution, setting up infrastructure for subsequent patches.

Quoted 11 lines
---
 chardev/char.c             | 2 +-
 gdbstub/system.c           | 3 ++-
 include/monitor/monitor.h  | 5 +++--
 monitor/hmp.c              | 5 +++--
 monitor/monitor-internal.h | 4 +++-
 monitor/monitor.c          | 9 ++++++---
 monitor/qmp-cmds.c         | 2 +-
 monitor/qmp.c              | 5 +++--
 stubs/monitor-internal.c   | 3 ++-
 9 files changed, 24 insertions(+), 14 deletions(-)

This patch will significantly conflict with my series that creates QOM objects for the monitors. They will all be guaranteed to have an "id" at that point though, which addresses the problem you're trying to solve IIUC.

https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg06427.html

Quoted 184 lines
chardev/char.c +1 -1
index ca8b37ed8d..f057247001 100644
@@ -805,7 +805,7 @@ static Chardev *qemu_chr_new_from_name(const char *label, const char *filename,
 
     if (qemu_opt_get_bool(opts, "mux", 0)) {
         assert(permit_mux_mon);
-        monitor_init_hmp(chr, true, &err);
+        monitor_init_hmp(chr, true, NULL, &err);
         if (err) {
             error_report_err(err);
             object_unparent(OBJECT(chr));
gdbstub/system.c +2 -1
index e86c5870ab..50f934fde3 100644
@@ -388,7 +388,8 @@ bool gdbserver_start(const char *device, Error **errp)
         /* Initialize a monitor terminal for gdb */
         mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
                                    NULL, NULL, &error_abort);
-        monitor_init_hmp(mon_chr, false, &error_abort);
+
+        monitor_init_hmp(mon_chr, false, NULL, &error_abort);
     } else {
         qemu_chr_fe_deinit(&gdbserver_system_state.chr, true);
         mon_chr = gdbserver_system_state.mon_chr;
include/monitor/monitor.h +3 -2
index 55649a8664..4a5eb20bea 100644
@@ -19,8 +19,9 @@ bool monitor_cur_is_qmp(void);
 
 void monitor_init_globals(void);
 void monitor_init_globals_core(void);
-void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp);
-void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp);
+void monitor_init_qmp(Chardev *chr, bool pretty, const char *id, Error **errp);
+void monitor_init_hmp(Chardev *chr, bool use_readline, const char *id,
+                      Error **errp);
 int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp);
 int monitor_init_opts(QemuOpts *opts, Error **errp);
 void monitor_cleanup(void);
monitor/hmp.c +3 -2
index cc4390486e..4ee8cb58d1 100644
@@ -1522,7 +1522,8 @@ static void monitor_readline_flush(void *opaque)
     monitor_flush(&mon->common);
 }
 
-void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
+void monitor_init_hmp(Chardev *chr, bool use_readline, const char *id,
+                      Error **errp)
 {
     MonitorHMP *mon = g_new0(MonitorHMP, 1);
 
@@ -1531,7 +1532,7 @@ void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
         return;
     }
 
-    monitor_data_init(&mon->common, false, false, false);
+    monitor_data_init(&mon->common, false, false, false, id);
 
     mon->use_readline = use_readline;
     if (mon->use_readline) {
monitor/monitor-internal.h +3 -1
index a5c4aba306..f651ba6a90 100644
@@ -108,6 +108,8 @@ struct Monitor {
     bool skip_flush;
     bool use_io_thread;
 
+    char *id;
+
     char *mon_cpu_path;
     QTAILQ_ENTRY(Monitor) entry;
 
@@ -179,7 +181,7 @@ extern QemuMutex monitor_lock;
 extern MonitorList mon_list;
 
 void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
-                       bool use_io_thread);
+                       bool use_io_thread, const char *id);
 void monitor_data_destroy(Monitor *mon);
 int monitor_can_read(void *opaque);
 void monitor_list_append(Monitor *mon);
monitor/monitor.c +6 -3
index 00b93ed612..65749f931e 100644
@@ -608,8 +608,10 @@ static void monitor_iothread_init(void)
 }
 
 void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
-                       bool use_io_thread)
+                       bool use_io_thread, const char *id)
 {
+    mon->id = id ? g_strdup(id) : NULL;
+
     if (use_io_thread && !mon_iothread) {
         monitor_iothread_init();
     }
@@ -629,6 +631,7 @@ void monitor_data_destroy(Monitor *mon)
     } else {
         readline_free(container_of(mon, MonitorHMP, common)->rs);
     }
+    g_free(mon->id);
     g_string_free(mon->outbuf, true);
     qemu_mutex_destroy(&mon->mon_lock);
 }
@@ -732,7 +735,7 @@ int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp)
 
     switch (opts->mode) {
     case MONITOR_MODE_CONTROL:
-        monitor_init_qmp(chr, opts->pretty, errp);
+        monitor_init_qmp(chr, opts->pretty, opts->id, errp);
         break;
     case MONITOR_MODE_READLINE:
         if (!allow_hmp) {
@@ -743,7 +746,7 @@ int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp)
             error_setg(errp, "'pretty' is not compatible with HMP monitors");
             return -1;
         }
-        monitor_init_hmp(chr, true, errp);
+        monitor_init_hmp(chr, true, opts->id, errp);
         break;
     default:
         g_assert_not_reached();
monitor/qmp-cmds.c +1 -1
index 0c409c27dc..e49b9cbf8b 100644
@@ -168,7 +168,7 @@ char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
     char *output = NULL;
     MonitorHMP hmp = {};
 
-    monitor_data_init(&hmp.common, false, true, false);
+    monitor_data_init(&hmp.common, false, true, false, NULL);
 
     if (has_cpu_index) {
         int ret = monitor_set_cpu(&hmp.common, cpu_index);
monitor/qmp.c +3 -2
index 687019811f..8fd71386e8 100644
@@ -513,7 +513,7 @@ static void monitor_qmp_setup_handlers_bh(void *opaque)
     monitor_list_append(&mon->common);
 }
 
-void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
+void monitor_init_qmp(Chardev *chr, bool pretty, const char *id, Error **errp)
 {
     MonitorQMP *mon = g_new0(MonitorQMP, 1);
 
@@ -525,7 +525,8 @@ void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
 
     /* Note: we run QMP monitor in I/O thread when @chr supports that */
     monitor_data_init(&mon->common, true, false,
-                      qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT));
+                      qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT),
+                      id);
 
     mon->pretty = pretty;
 
stubs/monitor-internal.c +2 -1
index 4fece49d53..325a559e62 100644
@@ -8,6 +8,7 @@ int monitor_get_fd(Monitor *mon, const char *name, Error **errp)
     return -1;
 }
 
-void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
+void monitor_init_hmp(Chardev *chr, bool use_readline, const char *id,
+                      Error **errp)
 {
 }
Signature · 1 line
2.49.0

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
author
#21 lore raw

Re: [PATCH V9 05/17] block/export: track IOThread reference in BlockExport

In reply to Stefan Hajnoczi
Quoted 38 lines · Stefan Hajnoczi
Quoted 36 lines · Zhang Chen
Quoted 24 lines · Stefan Hajnoczi
Quoted 9 lines · Zhang Chen

Users currently lack visibility into which block exports are utilizing specific IOThreads. This patch integrates IOThread referencing into the BlockExport lifecycle.

- Add iothreads array and holder_name to BlockExport struct.
- Use iothread_ref_and_get_aio_context during export creation.
- Implement proper cleanup in blk_exp_add fail path and blk_exp_delete_bh.
- Support both single and multi-iothread export configurations.

Quoted 8 lines

This ensures IOThread 'holders' status correctly reflects active block exports for better debugging and resource tracking.

---
 block/export/export.c  | 63 ++++++++++++++++++++++++++++++++++++------
 include/block/export.h |  6 ++++
 2 files changed, 60 insertions(+), 9 deletions(-)

If AI was used to generate this patch series, please see QEMU's AI policy:
https://www.qemu.org/docs/master/devel/code-provenance.html#use-of-ai-generated-content

Quoted 6 lines

This feels like AI generated code in that it is verbose and has lots of structure (e.g. commit messages that look like an AI summary of the code change), but the details are not correct (misleading comments, leaks, etc).

I will pause my review at this patch and wait for your response.

I used the AI for parts of commit messages.
Please continue to review code parts and skip the commit message parts, I will remove all AI-generated commit messages in next version.

Quoted 5 lines
Quoted 2 lines

Sorry, I just noticed the discussion:
[PATCH v2] docs/devel: relax policy on AI-generated contributions

By the way, the existing policy already allows using AI for code review. I recommend doing this before sending patches.

OK, I will use AI for basic code review before sending the next version. By the way, do you have any design comments for other patches?

Thanks
Chen

Quoted 2 lines

Stefan

author
#6 lore raw

Re: [PATCH V9 02/17] iothread: introduce iothread_ref/unref to track attached devices

In reply to Markus Armbruster
Quoted 3 lines · Markus Armbruster

Looks like you missed my review of v8. I'm replaying my review comments.

Oh, my fault, very sorry about that, focused on other patch's comments fixes. Thank you for your detailed explanation.

I will address your comments in next version. Expect the:
IOThreadHolder *h = QAPI_CLONE(IOThreadHolder, holder);

Same as your estimate, this doesn't seem worthwhile.

And the same issue in the [ PATCH V8 14/15] comments:
static IOThreadHolderList *iothread_get_holders_list(IOThread *iothread)

Quoted 2 lines

If iothread->holders was a IOThreadHolderList instead of a GList, this loop could be QAPI_CLONE(IOThreadHolderList, iothread->holders) instead.

If we change the GList *holders to the IOThreadHolderList *holders, This means that we have to reimplement the GList related API, for example the "g_list_prepend", "g_list_find_custom" and the "g_list_delete_link"

This doesn't seem worthwhile.

Thanks
Chen

Quoted 28 lines
Quoted 25 lines · Zhang Chen

Currently, IOThreads do not maintain a record of which devices are associated with them. This makes it difficult to monitor the workload distribution of IOThreads, especially in complex hotplug scenarios involving multiple virtio-blk or virtio-scsi devices.

This patch introduces a reference counting and tracking mechanism within the IOThread object:

- iothread_ref(): Prepends the device's IOThreadHolder to a list.
- iothread_unref(): Searches for the IOThreadHolder using a custom string comparison (g_strcmp0), releases the associated memory upon a successful match.
- holders: A GList storing the IOThreadHolder of attached devices for runtime introspection.

A later commit will add QMP commands to let management applications query the attachment status of IOThreads.

---
 include/system/iothread.h |  5 +++
 iothread.c                | 94 +++++++++++++++++++++++++++++++++++++++
 qapi/misc.json            | 61 +++++++++++++++++++++++++
 3 files changed, 160 insertions(+)
Quoted 15 lines

include/system/iothread.h struct IOThread { · line 56

Quoted 12 lines
include/system/iothread.h +4 -0
index a1ef7696cb..b9207ad829 100644
@@ -50,6 +50,11 @@ struct IOThread {
     bool stopping;              /* has iothread_stop() been called? */
     bool running;               /* should iothread_run() continue? */
     int thread_id;
+    /*
+     * The list elements are of type IOThreadHolder, which can
+     * represent either a QOM path or a block node name.
+     */

You forgot monitors. Stefan spotted this as well, and I agree with the fix he suggested.

Yes, my fault. will fix in the next version.

Quoted 44 lines
Quoted 31 lines
include/system/iothread.h +1 -0
+    GList *holders;
 
     /* AioContext poll parameters */
     int64_t poll_max_ns;
iothread.c +18 -0
index 3558535b40..3301b8d495 100644
@@ -25,6 +25,92 @@
 #include "qemu/rcu.h"
 #include "qemu/main-loop.h"
 
+/*
+ * iothread_ref:
+ * @iothread: the iothread to track
+ * @holder: the IOThreadHolder object initialized by the caller
+ *
+ * Add the @holder to the iothread's tracking list.
+ */
+static void iothread_ref(IOThread *iothread, const IOThreadHolder *holder)
+{
+    assert(holder);
+    IOThreadHolder *h = g_new0(IOThreadHolder, 1);
+
+    h->type = holder->type;
+    switch (holder->type) {
+    case IO_THREAD_HOLDER_KIND_QOM_OBJECT:
+        h->u.qom_object.qom_path = g_strdup(holder->u.qom_object.qom_path);
+        break;
+    case IO_THREAD_HOLDER_KIND_BLOCK_NODE:

iothread.c line 55

Quoted 10 lines
iothread.c +10 -0
+        h->u.block_node.node_name =
+            g_strdup(holder->u.block_node.node_name);
+        break;
+    case IO_THREAD_HOLDER_KIND_MONITOR_NAME:
+        h->u.monitor_name.monitor_name =
+            g_strdup(holder->u.monitor_name.monitor_name);
+        break;
+    default:
+        g_assert_not_reached();
+    }

This makes @h a deep copy of @holder. Try

Quoted 8 lines
    IOThreadHolder *h = QAPI_CLONE(IOThreadHolder, holder);

instead.

Instead of making a copy, the function could take ownership of @holder, and require @holder's members to be dynamically allocated. More efficient, but complicates the callers somewhat. Not worthwhile, I guess.

Yes, I

Quoted 211 lines

iothread.c line 67

Quoted 13 lines
iothread.c +12 -0
+
+    iothread->holders = g_list_prepend(iothread->holders, h);
+}
+
+static int iothread_holder_compare(gconstpointer a, gconstpointer b)
+{
+    const IOThreadHolder *holder_a = a;
+    const IOThreadHolder *holder_b = b;
+    const char *name_a, *name_b;
+
+    if (holder_a->type != holder_b->type) {
+        return -1;

Please use

           return holder_b->type - holder_a->type;

Why? By convention, an int-valued function fn() comparing A to B returns less than 0 when A<B, 0 when A == B, and greater than 0 when A>B. Anything else needs a prominent comment. Best to stick to the convention whenever practical.

Since A < B iff B > A, we want fn(A, B) == -fn(B, A).

Your function does not satisfy that! Harmless for now, because you only ever pass it to g_list_find_custom(), which treats all non-zero values the same. But if anyone passes it to something like qsort() in the future, they can look forward to some debugging "fun".

Quoted 12 lines
iothread.c +12 -0
+    }
+
+    switch (holder_a->type) {
+    case IO_THREAD_HOLDER_KIND_QOM_OBJECT:
+        name_a = holder_a->u.qom_object.qom_path;
+        name_b = holder_b->u.qom_object.qom_path;
+        break;
+    case IO_THREAD_HOLDER_KIND_BLOCK_NODE:
+        name_a = holder_a->u.block_node.node_name;
+        name_b = holder_b->u.block_node.node_name;
+        break;
+    case IO_THREAD_HOLDER_KIND_MONITOR_NAME:

iothread.c line 89

Quoted 10 lines
iothread.c +10 -0
+        name_a = holder_a->u.monitor_name.monitor_name;
+        name_b = holder_b->u.monitor_name.monitor_name;
+        break;
+    default:
+       /*
+        * This should not happen. If it does, name_a/b remains
+        * NULL and g_strcmp0 will handle it safely.
+        */
+        name_a = NULL;
+        name_b = NULL;

Replace this by

           g_assert_not_reached();

Then neither @name_a nor @name_b can be null, because a non-optional QAPI str is never null. Simply use strcmp().

Quoted 12 lines
iothread.c +12 -0
+    }
+
+    return g_strcmp0(name_a, name_b);
+}
+
+/*
+ * This function removes the @holder from the @iothread's tracking list.
+ * The @holder must match the one used previously in iothread_ref().
+ * It is a programming error to call this with a @holder that is not
+ * currently associated with the @iothread.
+ */
+static void iothread_unref(IOThread *iothread, const IOThreadHolder *holder)

iothread.c line 111

Quoted 10 lines
iothread.c +10 -0
+{
+    assert(holder);
+    GList *link = g_list_find_custom(iothread->holders, holder,
+                                     (GCompareFunc)iothread_holder_compare);
+
+    assert(link);
+
+    IOThreadHolder *h = (IOThreadHolder *)link->data;
+    qapi_free_IOThreadHolder(h);
+    iothread->holders = g_list_delete_link(iothread->holders, link);

Removing @link from the list before freeing link->data would be more obviously correct, wouldn't it?

Quoted 6 lines
iothread.c +2 -0
+}
+
 static void *iothread_run(void *opaque)
 {
     IOThread *iothread = opaque;

iothread.c char *iothread_get_id(IOThread *iothread) · line 451

Quoted 10 lines
iothread.c +7 -0
@@ -356,6 +442,14 @@ char *iothread_get_id(IOThread *iothread)
 
 AioContext *iothread_get_aio_context(IOThread *iothread)
 {
+    /* Remove in next patch for build */
+    IOThreadHolder holder = {
+        .type = IO_THREAD_HOLDER_KIND_QOM_OBJECT,
+        .u.qom_object.qom_path = (char *)"tmp_path",
+    };
+    iothread_ref(iothread, &holder);
+    iothread_unref(iothread, &holder);

Is this really needed to make this patch work?

Quoted 9 lines
iothread.c +1 -0
+
     return iothread->ctx;
 }
 
qapi/misc.json +0 -0
index c71a5fe657..d9f82f0922 100644

qapi/misc.json line 74

Quoted 8 lines
qapi/misc.json +5 -0
@@ -67,6 +67,67 @@
 ##
 { 'command': 'query-name', 'returns': 'NameInfo', 'allow-preconfig': true }
 
+
+##
+# @IOThreadHolderBlockNode:
+#
+# @node-name: A block node.

Let's use

# @node-name: Name of the block node

to make it more similar to node-name descriptions elsewhere.

qapi/misc.json line 85

Quoted 11 lines
qapi/misc.json +11 -0
+#
+# Since: 11.1
+#
+##
+{ 'struct': 'IOThreadHolderBlockNode',
+  'data': { 'node-name': 'str' } }
+
+##
+# @IOThreadHolderQomObject:
+#
+# @qom-path: A QOM Object.

Likewise, let's use

# @qom-path: Path to the object in the QOM tree

qapi/misc.json line 96

Quoted 11 lines
qapi/misc.json +11 -0
+#
+# Since: 11.1
+#
+##
+{ 'struct': 'IOThreadHolderQomObject',
+  'data': { 'qom-path': 'str' } }
+
+##
+# @IOThreadHolderMonitor:
+#
+# @monitor-name: A HMP/QMP monitor.

Let's use

# @monitor-name: Name of the HMP/QMP monitor

qapi/misc.json line 109

Quoted 13 lines
qapi/misc.json +13 -0
+#
+# Since: 11.1
+#
+##
+{ 'struct': 'IOThreadHolderMonitor',
+  'data': { 'monitor-name': 'str' } }
+
+##
+# @IOThreadHolderKind:
+#
+# @block-node: A block node.
+# @qom-object: A QOM Object.
+# @monitor-name: A HMP/QMP monitor.

Rename to @monitor for consistency with the other two.

Remark, not a request to change anything in your patch: kind monitor will become obsolete when monitors become QOM objects. Doing that before we release this interface is desirable.

qapi/misc.json line 118

Quoted 9 lines
qapi/misc.json +9 -0
+#
+# Since: 11.1
+##
+{ 'enum': 'IOThreadHolderKind',
+  'data': [ 'block-node', 'qom-object', 'monitor-name' ] }
+
+##
+# @IOThreadHolder:
+#

Let's add a short description:

# The block node, QOM object, or monitor holding the I/O thread.

Quoted 15 lines
qapi/misc.json +12 -0
+# @type: the kind of I/O thread holder.
+#
+# Since: 11.1
+##
+{ 'union': 'IOThreadHolder',
+  'base': { 'type': 'IOThreadHolderKind' },
+  'discriminator': 'type',
+  'data': {
+    'block-node': 'IOThreadHolderBlockNode',
+    'qom-object': 'IOThreadHolderQomObject',
+    'monitor-name': 'IOThreadHolderMonitor' } }
+
 ##
 # @IOThreadInfo:
 #
author
#26 lore raw

Re: [PATCH V9 06/17] monitor: refactor monitor_data_init() to pass ID

In reply to Daniel P. Berrangé
Quoted 26 lines · Daniel P. Berrangé
Quoted 12 lines · Zhang Chen

Anonymous monitors (e.g., '-monitor stdio' or GDB stub) leave their 'id' field as NULL. To allow downstream subsystems (like iothread tracking) to identify these instances later, the initialization paths must propagate the context.

Refactor monitor_data_init() to accept the 'id' parameters, and update all internal QMP/HMP paths to pass these identifiers.

For anonymous monitors, 'mon->id' remains NULL at this stage to avoid namespace pollution, setting up infrastructure for subsequent patches.

Quoted 11 lines
---
 chardev/char.c             | 2 +-
 gdbstub/system.c           | 3 ++-
 include/monitor/monitor.h  | 5 +++--
 monitor/hmp.c              | 5 +++--
 monitor/monitor-internal.h | 4 +++-
 monitor/monitor.c          | 9 ++++++---
 monitor/qmp-cmds.c         | 2 +-
 monitor/qmp.c              | 5 +++--
 stubs/monitor-internal.c   | 3 ++-
 9 files changed, 24 insertions(+), 14 deletions(-)
Quoted 7 lines

This patch will significantly conflict with my series that creates QOM objects for the monitors. They will all be guaranteed to have an "id" at that point though, which addresses the problem you're trying to solve IIUC.

https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg06427.html

Thank you for your updates.
As Markus's help, we already know your series for create QOM for the monitors. So this series V7 based on the assumption of your patch merged. But for the comments:
"Feels like this patch should not be merged as is."
https://lists.gnu.org/archive/html/qemu-devel/2026-05/msg04214.html

I have to fix it by myself.
I'm very happy this series based on your series, I can conveniently make the monitor as a regular QOM object like others in this series. If you think that OK, I will rebase my series on your QOM for monitor patches in next version.

Thanks
Chen

Quoted 192 lines
Quoted 181 lines
chardev/char.c +1 -1
index ca8b37ed8d..f057247001 100644
@@ -805,7 +805,7 @@ static Chardev *qemu_chr_new_from_name(const char *label, const char *filename,
 
     if (qemu_opt_get_bool(opts, "mux", 0)) {
         assert(permit_mux_mon);
-        monitor_init_hmp(chr, true, &err);
+        monitor_init_hmp(chr, true, NULL, &err);
         if (err) {
             error_report_err(err);
             object_unparent(OBJECT(chr));
gdbstub/system.c +2 -1
index e86c5870ab..50f934fde3 100644
@@ -388,7 +388,8 @@ bool gdbserver_start(const char *device, Error **errp)
         /* Initialize a monitor terminal for gdb */
         mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
                                    NULL, NULL, &error_abort);
-        monitor_init_hmp(mon_chr, false, &error_abort);
+
+        monitor_init_hmp(mon_chr, false, NULL, &error_abort);
     } else {
         qemu_chr_fe_deinit(&gdbserver_system_state.chr, true);
         mon_chr = gdbserver_system_state.mon_chr;
include/monitor/monitor.h +3 -2
index 55649a8664..4a5eb20bea 100644
@@ -19,8 +19,9 @@ bool monitor_cur_is_qmp(void);
 
 void monitor_init_globals(void);
 void monitor_init_globals_core(void);
-void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp);
-void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp);
+void monitor_init_qmp(Chardev *chr, bool pretty, const char *id, Error **errp);
+void monitor_init_hmp(Chardev *chr, bool use_readline, const char *id,
+                      Error **errp);
 int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp);
 int monitor_init_opts(QemuOpts *opts, Error **errp);
 void monitor_cleanup(void);
monitor/hmp.c +3 -2
index cc4390486e..4ee8cb58d1 100644
@@ -1522,7 +1522,8 @@ static void monitor_readline_flush(void *opaque)
     monitor_flush(&mon->common);
 }
 
-void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
+void monitor_init_hmp(Chardev *chr, bool use_readline, const char *id,
+                      Error **errp)
 {
     MonitorHMP *mon = g_new0(MonitorHMP, 1);
 
@@ -1531,7 +1532,7 @@ void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
         return;
     }
 
-    monitor_data_init(&mon->common, false, false, false);
+    monitor_data_init(&mon->common, false, false, false, id);
 
     mon->use_readline = use_readline;
     if (mon->use_readline) {
monitor/monitor-internal.h +3 -1
index a5c4aba306..f651ba6a90 100644
@@ -108,6 +108,8 @@ struct Monitor {
     bool skip_flush;
     bool use_io_thread;
 
+    char *id;
+
     char *mon_cpu_path;
     QTAILQ_ENTRY(Monitor) entry;
 
@@ -179,7 +181,7 @@ extern QemuMutex monitor_lock;
 extern MonitorList mon_list;
 
 void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
-                       bool use_io_thread);
+                       bool use_io_thread, const char *id);
 void monitor_data_destroy(Monitor *mon);
 int monitor_can_read(void *opaque);
 void monitor_list_append(Monitor *mon);
monitor/monitor.c +6 -3
index 00b93ed612..65749f931e 100644
@@ -608,8 +608,10 @@ static void monitor_iothread_init(void)
 }
 
 void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
-                       bool use_io_thread)
+                       bool use_io_thread, const char *id)
 {
+    mon->id = id ? g_strdup(id) : NULL;
+
     if (use_io_thread && !mon_iothread) {
         monitor_iothread_init();
     }
@@ -629,6 +631,7 @@ void monitor_data_destroy(Monitor *mon)
     } else {
         readline_free(container_of(mon, MonitorHMP, common)->rs);
     }
+    g_free(mon->id);
     g_string_free(mon->outbuf, true);
     qemu_mutex_destroy(&mon->mon_lock);
 }
@@ -732,7 +735,7 @@ int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp)
 
     switch (opts->mode) {
     case MONITOR_MODE_CONTROL:
-        monitor_init_qmp(chr, opts->pretty, errp);
+        monitor_init_qmp(chr, opts->pretty, opts->id, errp);
         break;
     case MONITOR_MODE_READLINE:
         if (!allow_hmp) {
@@ -743,7 +746,7 @@ int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp)
             error_setg(errp, "'pretty' is not compatible with HMP monitors");
             return -1;
         }
-        monitor_init_hmp(chr, true, errp);
+        monitor_init_hmp(chr, true, opts->id, errp);
         break;
     default:
         g_assert_not_reached();
monitor/qmp-cmds.c +1 -1
index 0c409c27dc..e49b9cbf8b 100644
@@ -168,7 +168,7 @@ char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
     char *output = NULL;
     MonitorHMP hmp = {};
 
-    monitor_data_init(&hmp.common, false, true, false);
+    monitor_data_init(&hmp.common, false, true, false, NULL);
 
     if (has_cpu_index) {
         int ret = monitor_set_cpu(&hmp.common, cpu_index);
monitor/qmp.c +3 -2
index 687019811f..8fd71386e8 100644
@@ -513,7 +513,7 @@ static void monitor_qmp_setup_handlers_bh(void *opaque)
     monitor_list_append(&mon->common);
 }
 
-void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
+void monitor_init_qmp(Chardev *chr, bool pretty, const char *id, Error **errp)
 {
     MonitorQMP *mon = g_new0(MonitorQMP, 1);
 
@@ -525,7 +525,8 @@ void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
 
     /* Note: we run QMP monitor in I/O thread when @chr supports that */
     monitor_data_init(&mon->common, true, false,
-                      qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT));
+                      qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT),
+                      id);
 
     mon->pretty = pretty;
 
stubs/monitor-internal.c +2 -2
index 4fece49d53..325a559e62 100644
@@ -8,6 +8,7 @@ int monitor_get_fd(Monitor *mon, const char *name, Error **errp)
     return -1;
 }
 
-void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
+void monitor_init_hmp(Chardev *chr, bool use_readline, const char *id,
+                      Error **errp)
 {
 }
--
Quoted 3 lines

2.49.0

With regards,
Daniel
--

|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#7 lore raw

Re: [PATCH V9 02/17] iothread: introduce iothread_ref/unref to track attached devices

In reply to Zhang Chen
Quoted 6 lines · Zhang Chen
Quoted 3 lines · Markus Armbruster

Looks like you missed my review of v8. I'm replaying my review comments.

Oh, my fault, very sorry about that, focused on other patch's comments fixes.

Happens :)

Quoted 7 lines

Thank you for your detailed explanation.

I will address your comments in next version. Expect the:
IOThreadHolder *h = QAPI_CLONE(IOThreadHolder, holder);

Same as your estimate, this doesn't seem worthwhile.

Replacing hand-written code by QAPI_CLONE() is worth a try.

I additionally described how to avoid the copy entirely instead, but advised against it.

Quoted 6 lines

And the same issue in the [ PATCH V8 14/15] comments:
static IOThreadHolderList *iothread_get_holders_list(IOThread *iothread)

Quoted 2 lines

If iothread->holders was a IOThreadHolderList instead of a GList, this loop could be QAPI_CLONE(IOThreadHolderList, iothread->holders) instead.

Quoted 6 lines

If we change the GList *holders to the IOThreadHolderList *holders, This means that we have to reimplement the GList related API, for example the "g_list_prepend", "g_list_find_custom" and the "g_list_delete_link"

This doesn't seem worthwhile.

The replacement for g_list_prepend() would be trivial:

    h->next = iothread->holders;
    iothread->holders = h;

iothread_unref() would turn into simple loop to find and delete the matching list element. iothread_get_holders_list()'s similarly simple loop around QAPI_CLONE() would turn into a single QAPI_CLONE(). Basically the same amount of code, I think.

Your solution already exists, which is a valid argument.

Mine stays within QAPI instead of mixing GList and QAPI. Also a valid argument.

Use your judgement. I'd try to avoid the mixing.

#22 lore raw

Re: [PATCH V9 05/17] block/export: track IOThread reference in BlockExport

In reply to Zhang Chen
Quoted 39 lines · Zhang Chen
Quoted 38 lines · Stefan Hajnoczi
Quoted 36 lines · Zhang Chen
Quoted 24 lines · Stefan Hajnoczi
Quoted 9 lines · Zhang Chen

Users currently lack visibility into which block exports are utilizing specific IOThreads. This patch integrates IOThread referencing into the BlockExport lifecycle.

- Add iothreads array and holder_name to BlockExport struct.
- Use iothread_ref_and_get_aio_context during export creation.
- Implement proper cleanup in blk_exp_add fail path and blk_exp_delete_bh.
- Support both single and multi-iothread export configurations.

Quoted 8 lines

This ensures IOThread 'holders' status correctly reflects active block exports for better debugging and resource tracking.

---
 block/export/export.c  | 63 ++++++++++++++++++++++++++++++++++++------
 include/block/export.h |  6 ++++
 2 files changed, 60 insertions(+), 9 deletions(-)

If AI was used to generate this patch series, please see QEMU's AI policy:
https://www.qemu.org/docs/master/devel/code-provenance.html#use-of-ai-generated-content

Quoted 6 lines

This feels like AI generated code in that it is verbose and has lots of structure (e.g. commit messages that look like an AI summary of the code change), but the details are not correct (misleading comments, leaks, etc).

I will pause my review at this patch and wait for your response.

I used the AI for parts of commit messages.
Please continue to review code parts and skip the commit message parts, I will remove all AI-generated commit messages in next version.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Sorry, I just noticed the discussion:
[PATCH v2] docs/devel: relax policy on AI-generated contributions

By the way, the existing policy already allows using AI for code review. I recommend doing this before sending patches.

OK, I will use AI for basic code review before sending the next version. By the way, do you have any design comments for other patches?

I'm working my way through the series and will send feedback one patch at a time. You're welcome to send a new revision if you like, there's no need to wait for me to finish all patches if you have been working on code changes.

Stefan

#31 lore raw

Re: [PATCH V9 08/17] monitor: switch to iothread_unsafe_get_aio_context()

In reply to Zhang Chen
Quoted 8 lines · Zhang Chen

Since previous patches introduced explicit monitor tracking and reference counting via IOThreadHolder during initialization, standard global calls to iothread_get_aio_context() are no longer appropriate for raw context fetches.

Switch the remaining fallback sites in monitor_suspend(), monitor_resume(), and monitor_init_qmp() to use iothread_unsafe_get_aio_context(). This aligns with the new safe ref/unref lifecycles introduced for monitor tracking.

Why is the unsafe API used here? monitor_data_init() now takes a reference and gets the AioContext. Can you use that AioContext here instead of fetching it with iothread_unsafe_get_aio_context()?

author
#32 lore raw

Re: [PATCH V9 08/17] monitor: switch to iothread_unsafe_get_aio_context()

Quoted 7 lines · Stefan Hajnoczi
Quoted 5 lines · Zhang Chen

Since previous patches introduced explicit monitor tracking and reference counting via IOThreadHolder during initialization, standard global calls to iothread_get_aio_context() are no longer appropriate for raw context fetches.

Quoted 7 lines
Quoted 3 lines

Switch the remaining fallback sites in monitor_suspend(), monitor_resume(), and monitor_init_qmp() to use iothread_unsafe_get_aio_context(). This aligns with the new safe ref/unref lifecycles introduced for monitor tracking.

Why is the unsafe API used here? monitor_data_init() now takes a reference and gets the AioContext. Can you use that AioContext here instead of fetching it with iothread_unsafe_get_aio_context()?

Because we have some comments from this series V8 patches:
https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg00375.html

If OK, I will back to the V8 code and follow the [PATCH V9 06/17] comments,
https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg07323.html
based on Daniel's series for monitor parts in V10.

Thanks
Chen

author
#8 lore raw

Re: [PATCH V9 02/17] iothread: introduce iothread_ref/unref to track attached devices

In reply to Markus Armbruster
Quoted 48 lines · Markus Armbruster
Quoted 6 lines · Zhang Chen
Quoted 3 lines · Markus Armbruster

Looks like you missed my review of v8. I'm replaying my review comments.

Oh, my fault, very sorry about that, focused on other patch's comments fixes.

Happens :)

Quoted 7 lines

Thank you for your detailed explanation.

I will address your comments in next version. Expect the:
IOThreadHolder *h = QAPI_CLONE(IOThreadHolder, holder);

Same as your estimate, this doesn't seem worthwhile.

Replacing hand-written code by QAPI_CLONE() is worth a try.

I additionally described how to avoid the copy entirely instead, but advised against it.

Quoted 6 lines

And the same issue in the [ PATCH V8 14/15] comments:
static IOThreadHolderList *iothread_get_holders_list(IOThread *iothread)

Quoted 2 lines

If iothread->holders was a IOThreadHolderList instead of a GList, this loop could be QAPI_CLONE(IOThreadHolderList, iothread->holders) instead.

Quoted 6 lines

If we change the GList *holders to the IOThreadHolderList *holders, This means that we have to reimplement the GList related API, for example the "g_list_prepend", "g_list_find_custom" and the "g_list_delete_link"

This doesn't seem worthwhile.

The replacement for g_list_prepend() would be trivial:

    h->next = iothread->holders;
    iothread->holders = h;

iothread_unref() would turn into simple loop to find and delete the matching list element. iothread_get_holders_list()'s similarly simple loop around QAPI_CLONE() would turn into a single QAPI_CLONE(). Basically the same amount of code, I think.

Quoted 6 lines

Your solution already exists, which is a valid argument.

Mine stays within QAPI instead of mixing GList and QAPI. Also a valid argument.

Use your judgement. I'd try to avoid the mixing.

Make sense, I will change the GList code to QAPI code in the next version.

By the way, do you think it's OK to make this series based on Daniel's V5 patches
for the monitor QOM part.
https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg06427.html

This allows us to delete some of the code and directly use the monitor as QOM.
https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg07323.html

Thanks
Chen

Quoted 1 line
#9 lore raw

Re: [PATCH V9 02/17] iothread: introduce iothread_ref/unref to track attached devices

Zhang Chen <[email protected]> writes:

[...]

Quoted 7 lines

By the way, do you think it's OK to make this series based on Daniel's V5 patches
for the monitor QOM part.
https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg06427.html

This allows us to delete some of the code and directly use the monitor as QOM.
https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg07323.html

I think that makes sense.

#33 lore raw

Re: [PATCH V9 08/17] monitor: switch to iothread_unsafe_get_aio_context()

In reply to Zhang Chen
Quoted 16 lines · Zhang Chen
Quoted 7 lines · Stefan Hajnoczi
Quoted 5 lines · Zhang Chen

Since previous patches introduced explicit monitor tracking and reference counting via IOThreadHolder during initialization, standard global calls to iothread_get_aio_context() are no longer appropriate for raw context fetches.

Quoted 7 lines
Quoted 3 lines

Switch the remaining fallback sites in monitor_suspend(), monitor_resume(), and monitor_init_qmp() to use iothread_unsafe_get_aio_context(). This aligns with the new safe ref/unref lifecycles introduced for monitor tracking.

Why is the unsafe API used here? monitor_data_init() now takes a reference and gets the AioContext. Can you use that AioContext here instead of fetching it with iothread_unsafe_get_aio_context()?

Quoted 6 lines

Because we have some comments from this series V8 patches:
https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg00375.html

If OK, I will back to the V8 code and follow the [PATCH V9 06/17] comments,
https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg07323.html
based on Daniel's series for monitor parts in V10.

Markus had no objection to using safe ref/unref APIs. I think he didn't like the additional AioContext pointer that the caller needed to manage.

Using the safe APIs is important because the risk of bugs is high with an unsafe API. If the new safe API is too inconvenient to use, then let's find a different API design.

Here is a variation of the API that requires the caller to hold just 1 thing - the ref:

IOThreadRef *iothread_ref_get(IOThread *iothread, const IOThreadHolder *holder); void iothread_ref_put(IOThreadRef *ref);
AioContext *iothread_ref_get_aio_context(IOThreadRef *ref);

The caller can call iothread_ref_get_aio_aio_context() multiple times, so they no longer need to store the AioContext pointer.

Since iothread_ref_put() takes IOThreadRef, the caller doesn't need to build a new IOThreadHolder when releasing the reference.

What do you think?

Stefan

#27 lore raw

Re: [PATCH V9 06/17] monitor: refactor monitor_data_init() to pass ID

In reply to Zhang Chen
Quoted 42 lines · Zhang Chen
Quoted 26 lines · Daniel P. Berrangé
Quoted 12 lines · Zhang Chen

Anonymous monitors (e.g., '-monitor stdio' or GDB stub) leave their 'id' field as NULL. To allow downstream subsystems (like iothread tracking) to identify these instances later, the initialization paths must propagate the context.

Refactor monitor_data_init() to accept the 'id' parameters, and update all internal QMP/HMP paths to pass these identifiers.

For anonymous monitors, 'mon->id' remains NULL at this stage to avoid namespace pollution, setting up infrastructure for subsequent patches.

Quoted 11 lines
---
 chardev/char.c             | 2 +-
 gdbstub/system.c           | 3 ++-
 include/monitor/monitor.h  | 5 +++--
 monitor/hmp.c              | 5 +++--
 monitor/monitor-internal.h | 4 +++-
 monitor/monitor.c          | 9 ++++++---
 monitor/qmp-cmds.c         | 2 +-
 monitor/qmp.c              | 5 +++--
 stubs/monitor-internal.c   | 3 ++-
 9 files changed, 24 insertions(+), 14 deletions(-)
Quoted 7 lines

This patch will significantly conflict with my series that creates QOM objects for the monitors. They will all be guaranteed to have an "id" at that point though, which addresses the problem you're trying to solve IIUC.

https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg06427.html

Thank you for your updates.
As Markus's help, we already know your series for create QOM for the monitors. So this series V7 based on the assumption of your patch merged. But for the comments:
"Feels like this patch should not be merged as is."
https://lists.gnu.org/archive/html/qemu-devel/2026-05/msg04214.html

Quoted 5 lines

I have to fix it by myself.
I'm very happy this series based on your series, I can conveniently make the monitor as a regular QOM object like others in this series. If you think that OK, I will rebase my series on your QOM for monitor patches in next version.

FYI, my monitor hotplug series is merged, so you can simply rebase your work onto current git HEAD.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
author
#28 lore raw

Re: [PATCH V9 06/17] monitor: refactor monitor_data_init() to pass ID

Quoted 44 lines · Daniel P. Berrangé
Quoted 42 lines · Zhang Chen
Quoted 26 lines · Daniel P. Berrangé
Quoted 12 lines · Zhang Chen

Anonymous monitors (e.g., '-monitor stdio' or GDB stub) leave their 'id' field as NULL. To allow downstream subsystems (like iothread tracking) to identify these instances later, the initialization paths must propagate the context.

Refactor monitor_data_init() to accept the 'id' parameters, and update all internal QMP/HMP paths to pass these identifiers.

For anonymous monitors, 'mon->id' remains NULL at this stage to avoid namespace pollution, setting up infrastructure for subsequent patches.

Quoted 11 lines
---
 chardev/char.c             | 2 +-
 gdbstub/system.c           | 3 ++-
 include/monitor/monitor.h  | 5 +++--
 monitor/hmp.c              | 5 +++--
 monitor/monitor-internal.h | 4 +++-
 monitor/monitor.c          | 9 ++++++---
 monitor/qmp-cmds.c         | 2 +-
 monitor/qmp.c              | 5 +++--
 stubs/monitor-internal.c   | 3 ++-
 9 files changed, 24 insertions(+), 14 deletions(-)
Quoted 7 lines

This patch will significantly conflict with my series that creates QOM objects for the monitors. They will all be guaranteed to have an "id" at that point though, which addresses the problem you're trying to solve IIUC.

https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg06427.html

Thank you for your updates.
As Markus's help, we already know your series for create QOM for the monitors. So this series V7 based on the assumption of your patch merged. But for the comments:
"Feels like this patch should not be merged as is."
https://lists.gnu.org/archive/html/qemu-devel/2026-05/msg04214.html

Quoted 9 lines
Quoted 5 lines

I have to fix it by myself.
I'm very happy this series based on your series, I can conveniently make the monitor as a regular QOM object like others in this series. If you think that OK, I will rebase my series on your QOM for monitor patches in next version.

FYI, my monitor hotplug series is merged, so you can simply rebase your work onto current git HEAD.

Thank you, Daniel.
I rebased my series on your latest patches, it looks the same as merged version.
[PATCH v7 00/35] monitor: turn QMP and HMP into QOM objects
https://lore.kernel.org/qemu-devel/[email protected]/

And I have released V10 and welcome everyone to review it.

Thanks
Chen

Quoted 8 lines

With regards,
Daniel
--

|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
author
#34 lore raw

Re: [PATCH V9 08/17] monitor: switch to iothread_unsafe_get_aio_context()

In reply to Stefan Hajnoczi
Quoted 39 lines · Stefan Hajnoczi
Quoted 16 lines · Zhang Chen
Quoted 7 lines · Stefan Hajnoczi
Quoted 5 lines · Zhang Chen

Since previous patches introduced explicit monitor tracking and reference counting via IOThreadHolder during initialization, standard global calls to iothread_get_aio_context() are no longer appropriate for raw context fetches.

Quoted 7 lines
Quoted 3 lines

Switch the remaining fallback sites in monitor_suspend(), monitor_resume(), and monitor_init_qmp() to use iothread_unsafe_get_aio_context(). This aligns with the new safe ref/unref lifecycles introduced for monitor tracking.

Why is the unsafe API used here? monitor_data_init() now takes a reference and gets the AioContext. Can you use that AioContext here instead of fetching it with iothread_unsafe_get_aio_context()?

Quoted 6 lines

Because we have some comments from this series V8 patches:
https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg00375.html

If OK, I will back to the V8 code and follow the [PATCH V9 06/17] comments,
https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg07323.html
based on Daniel's series for monitor parts in V10.

Markus had no objection to using safe ref/unref APIs. I think he didn't like the additional AioContext pointer that the caller needed to manage.

Using the safe APIs is important because the risk of bugs is high with an unsafe API. If the new safe API is too inconvenient to use, then let's find a different API design.

Here is a variation of the API that requires the caller to hold just 1 thing - the ref:

IOThreadRef *iothread_ref_get(IOThread *iothread, const IOThreadHolder *holder); void iothread_ref_put(IOThreadRef *ref);
AioContext *iothread_ref_get_aio_context(IOThreadRef *ref);

Quoted 8 lines

The caller can call iothread_ref_get_aio_aio_context() multiple times, so they no longer need to store the AioContext pointer.

Since iothread_ref_put() takes IOThreadRef, the caller doesn't need to build a new IOThreadHolder when releasing the reference.

What do you think?

The monitor parts based on Daniel P. Berrangé's series:
[PATCH v7 00/35] monitor: turn QMP and HMP into QOM objects
https://lore.kernel.org/qemu-devel/[email protected]/

This part has undergone significant changes; please refer to V10 for the latest implementation.
https://lists.gnu.org/archive/html/qemu-devel/2026-07/msg04823.html

Thanks
Chen

Quoted 1 line

Stefan