All of lore.kernel.org
 help / color / mirror / Atom feed
From: Zhang Chen <zhangckid@gmail.com>
To: qemu-devel <qemu-devel@nongnu.org>,
	"Dr . David Alan Gilbert" <dave@treblig.org>,
	Eric Blake <eblake@redhat.com>,
	Markus Armbruster <armbru@redhat.com>,
	"Michael S . Tsirkin" <mst@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>
Cc: Zhang Chen <zhangckid@gmail.com>
Subject: [PATCH V7 03/14] iothread: tracking iothread users with holder name
Date: Mon, 11 May 2026 22:04:05 +0800	[thread overview]
Message-ID: <20260511140416.28271-4-zhangckid@gmail.com> (raw)
In-Reply-To: <20260511140416.28271-1-zhangckid@gmail.com>

Introduce iothread_get_aio_context() 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 by name 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.

Signed-off-by: Zhang Chen <zhangckid@gmail.com>
---
 include/system/iothread.h |  3 +++
 iothread.c                | 33 +++++++++++++++++++++++++++++----
 2 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/include/system/iothread.h b/include/system/iothread.h
index 2871b06edc..313ef61124 100644
--- a/include/system/iothread.h
+++ b/include/system/iothread.h
@@ -70,6 +70,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 char *holder);
+void iothread_put_aio_context(IOThread *iothread, const char *holder);
 GMainContext *iothread_get_g_main_context(IOThread *iothread);
 
 /*
diff --git a/iothread.c b/iothread.c
index b805e4f97d..5d6f46d286 100644
--- a/iothread.c
+++ b/iothread.c
@@ -44,6 +44,12 @@ static void iothread_ref(IOThread *iothread, const char *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)
@@ -83,6 +89,8 @@ static void iothread_unref(IOThread *iothread, const char *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)
@@ -200,7 +208,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);
@@ -419,13 +427,30 @@ char *iothread_get_id(IOThread *iothread)
 
 AioContext *iothread_get_aio_context(IOThread *iothread)
 {
-    /* Remove in next patch for build */
-    iothread_ref(iothread, "tmp");
-    iothread_unref(iothread, "tmp");
+    return iothread->ctx;
+}
+
+AioContext *iothread_ref_and_get_aio_context(IOThread *iothread,
+                                             const char *holder)
+{
+    /*
+     * In some cases, iothread user need the ctx to clearup other resource.
+     * When holder is empty, back to the legacy way.
+     */
+    if (holder) {
+        /* Add holder device path to the list */
+        iothread_ref(iothread, holder);
+    }
 
     return iothread->ctx;
 }
 
+void iothread_put_aio_context(IOThread *iothread, const char *holder)
+{
+    /* Delete holder device path from the list */
+    iothread_unref(iothread, holder);
+}
+
 static int query_one_iothread(Object *object, void *opaque)
 {
     IOThreadInfoList ***tail = opaque;
-- 
2.49.0



  parent reply	other threads:[~2026-05-11 14:06 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-11 14:04 [PATCH V7 00/14] iothread: Support tracking and querying IOThread holders Zhang Chen
2026-05-11 14:04 ` [PATCH V7 01/14] qapi/misc: Fix missed query-iothreads items Zhang Chen
2026-05-11 14:04 ` [PATCH V7 02/14] iothread: introduce iothread_ref/unref to track attached devices Zhang Chen
2026-05-11 18:14   ` Stefan Hajnoczi
2026-05-11 19:15     ` Zhang Chen
2026-05-18 11:58     ` Markus Armbruster
2026-05-18 13:28       ` Zhang Chen
2026-05-18 11:44   ` Markus Armbruster
2026-05-11 14:04 ` Zhang Chen [this message]
2026-05-11 18:54   ` [PATCH V7 03/14] iothread: tracking iothread users with holder name Stefan Hajnoczi
2026-05-11 19:06     ` Zhang Chen
2026-05-11 14:04 ` [PATCH V7 04/14] blockdev: Update " Zhang Chen
2026-05-11 19:05   ` Stefan Hajnoczi
2026-05-11 19:25     ` Zhang Chen
2026-05-11 14:04 ` [PATCH V7 05/14] block/export: track IOThread reference in BlockExport Zhang Chen
2026-05-11 14:04 ` [PATCH V7 06/14] monitor: Update tracking iothread users with holder name Zhang Chen
2026-05-18 11:57   ` Markus Armbruster
2026-05-18 13:47     ` Zhang Chen
2026-05-11 14:04 ` [PATCH V7 07/14] virtio-vq-mapping: track iothread-vq-mapping references using device path Zhang Chen
2026-05-11 14:04 ` [PATCH V7 08/14] virtio: use iothread_get/put_aio_context for thread pinning Zhang Chen
2026-05-11 14:04 ` [PATCH V7 09/14] net/colo: track IOThread references using path-based holder Zhang Chen
2026-05-11 14:04 ` [PATCH V7 10/14] virtio-balloon: Update tracking iothread users with holder name Zhang Chen
2026-05-11 14:04 ` [PATCH V7 11/14] vfio-user/proxy: " Zhang Chen
2026-05-11 14:04 ` [PATCH V7 12/14] xen-block: " Zhang Chen
2026-05-11 14:04 ` [PATCH V7 13/14] qapi: examine IOThread attachment status via query-iothreads Zhang Chen
2026-05-11 14:04 ` [PATCH V7 14/14] iothread: simplify API by merging iothread_get_aio_context variants Zhang Chen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260511140416.28271-4-zhangckid@gmail.com \
    --to=zhangckid@gmail.com \
    --cc=armbru@redhat.com \
    --cc=dave@treblig.org \
    --cc=eblake@redhat.com \
    --cc=mst@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.