All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Zhang Chen <zhangckid@gmail.com>
Cc: qemu-devel <qemu-devel@nongnu.org>,
	 "Dr . David Alan Gilbert" <dave@treblig.org>,
	 Eric Blake <eblake@redhat.com>,
	 "Michael S . Tsirkin" <mst@redhat.com>,
	 Stefan Hajnoczi <stefanha@redhat.com>
Subject: Re: [PATCH V6 06/14] monitor: Update tracking iothread users with holder name
Date: Thu, 23 Apr 2026 14:44:32 +0200	[thread overview]
Message-ID: <87ldedongv.fsf@pond.sub.org> (raw)
In-Reply-To: <20260410150457.85190-7-zhangckid@gmail.com> (Zhang Chen's message of "Fri, 10 Apr 2026 23:04:49 +0800")

Zhang Chen <zhangckid@gmail.com> writes:

> Since the Monitor struct is not a QOM Object, we cannot use
> object_get_canonical_path(). Instead, this patch uses the monitor's
> name (or a default type-based name) as the holder identifier.
>
> Cache the AioContext in the Monitor struct to avoid repeated calls to
> iothread_get_aio_context() and ensure symmetrical ref/unref during
> monitor lifecycle.
>
> Signed-off-by: Zhang Chen <zhangckid@gmail.com>
> ---
>  monitor/monitor-internal.h |  3 +++
>  monitor/monitor.c          | 24 ++++++++++++++++++++++--
>  monitor/qmp.c              |  3 ++-
>  3 files changed, 27 insertions(+), 3 deletions(-)
>
> diff --git a/monitor/monitor-internal.h b/monitor/monitor-internal.h
> index feca111ae3..51cedf90e4 100644
> --- a/monitor/monitor-internal.h
> +++ b/monitor/monitor-internal.h
> @@ -116,6 +116,9 @@ struct Monitor {
>      guint out_watch;
>      int mux_out;
>      int reset_seen;
> +
> +    /* iothread context */
> +    AioContext *ctx;
>  };
>  
>  struct MonitorHMP {
> diff --git a/monitor/monitor.c b/monitor/monitor.c
> index 00b93ed612..b6efe776d6 100644
> --- a/monitor/monitor.c
> +++ b/monitor/monitor.c
> @@ -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(mon->ctx);
>      }
>  
>      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 = mon->ctx;
>          } else {
>              ctx = qemu_get_aio_context();
>          }
> @@ -612,6 +612,18 @@ void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
>  {
>      if (use_io_thread && !mon_iothread) {
>          monitor_iothread_init();
> +        /*
> +         * Because of current Monitor is not a QOM Object,
> +         * so using OBJECT(mon) is undefined behavior and may crash.
> +         * Try using a hard-coded future implementation of the qom path instead.
> +         * (Like the name of the "mon_iothread").
> +         * long-term solution would be making Monitor QOM, after that change
> +         * here to:
> +         * g_autofree path = object_get_canonical_path(OBJECT(mon));
> +         */
> +        g_autofree char *path = g_strdup(is_qmp ? "/monitor/qmp_mon0" :
> +                                                  "/monitor/hmp_mon0");
> +        mon->ctx = iothread_ref_and_get_aio_context(mon_iothread, path);

Oh.

PATCH 02 defines QAPI type IoThreadHolder as a union with two branches
for the two kinds of holders: one for block node holder, and one for QOM
object holder.  Both branches use a string to identify the holder.

The C code in PATCH 02 uses just a string to identify the holder.  This
silently assumes that the strings for block nodes are distinct from the
strings for QOM objects.  True as long as we only use absolute QOM
paths: these start with '/', and block nodes can't.

This patch shows me there's actually a third kind: monitor.  You use the
QOM object branch for it.  That's confusing; a monitor is not a QOM
object.  Moreover, you make yet another silent assumption: absolute QOM
paths do not start with "/monitor/".

This is too much for me.

Please use a separate IoThreadHolderKind value and IoThreadHolder branch
for each kind of holder.  The kinds I've seen so far are block-node, QOM
object, monitor.

If Daniel Berrangé's "[PATCH RFC 00/17] monitor: turn QMP and HMP into
QOM objects" gets merged, kind monitor can go away.

Use of just a string to identify the holder requires strings for
different kinds to be distinct.  The argument why they are must be
written down, simple, and likely to remain true.

But I'd prefer to use IoThreadHolder instead of string.  No assumptions
necessary then.

If this IoThreadHolder arguments make the calls ugly or overly verbose,
consider thin wrappers for each kind, i.e.

    new_ctx = iothread_get_aio_context_block(iothread, bs);

instead of

    holder_name = bdrv_get_node_name(bs);
    new_ctx = iothread_get_aio_context(iothread, holder_name);

and so forth.

Questions?

>      }
>      qemu_mutex_init(&mon->mon_lock);
>      mon->is_qmp = is_qmp;
> @@ -631,6 +643,14 @@ void monitor_data_destroy(Monitor *mon)
>      }
>      g_string_free(mon->outbuf, true);
>      qemu_mutex_destroy(&mon->mon_lock);
> +
> +    if (mon->ctx && mon_iothread) {
> +        g_autofree char *path = g_strdup(monitor_is_qmp(mon) ?
> +                                        "/monitor/qmp_mon0" :
> +                                        "/monitor/hmp_mon0");
> +        iothread_put_aio_context(mon_iothread, path);
> +        mon->ctx = NULL;
> +    }
>  }
>  
>  void monitor_cleanup(void)
> diff --git a/monitor/qmp.c b/monitor/qmp.c
> index 687019811f..8e4a775fac 100644
> --- a/monitor/qmp.c
> +++ b/monitor/qmp.c
> @@ -549,7 +549,8 @@ void monitor_init_qmp(Chardev *chr, bool pretty, 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),
> +        Monitor *mon_p = &mon->common;
> +        aio_bh_schedule_oneshot(mon_p->ctx,
>                                  monitor_qmp_setup_handlers_bh, mon);
>          /* The bottom half will add @mon to @mon_list */
>      } else {



  reply	other threads:[~2026-04-23 12:45 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-10 15:04 [PATCH V6 00/14] iothread: Support tracking and querying IOThread holders Zhang Chen
2026-04-10 15:04 ` [PATCH V6 01/14] qapi/misc: Fix missed query-iothreads items Zhang Chen
2026-04-10 15:04 ` [PATCH V6 02/14] iothread: introduce iothread_ref/unref to track attached devices Zhang Chen
2026-04-21 14:15   ` Markus Armbruster
2026-04-27 14:55     ` Zhang Chen
2026-04-10 15:04 ` [PATCH V6 03/14] iothread: tracking iothread users with holder name Zhang Chen
2026-04-10 15:04 ` [PATCH V6 04/14] blockdev: Update " Zhang Chen
2026-04-10 15:04 ` [PATCH V6 05/14] block/export: track IOThread reference in BlockExport Zhang Chen
2026-04-10 15:04 ` [PATCH V6 06/14] monitor: Update tracking iothread users with holder name Zhang Chen
2026-04-23 12:44   ` Markus Armbruster [this message]
2026-04-27 14:55     ` Zhang Chen
2026-04-28  7:26       ` Markus Armbruster
2026-04-29  5:34         ` Zhang Chen
2026-04-10 15:04 ` [PATCH V6 07/14] virtio-vq-mapping: track iothread-vq-mapping references using device path Zhang Chen
2026-04-10 15:04 ` [PATCH V6 08/14] virtio: use iothread_get/put_aio_context for thread pinning Zhang Chen
2026-04-10 15:04 ` [PATCH V6 09/14] net/colo: track IOThread references using path-based holder Zhang Chen
2026-04-10 15:04 ` [PATCH V6 10/14] virtio-balloon: Update tracking iothread users with holder name Zhang Chen
2026-04-10 15:04 ` [PATCH V6 11/14] vfio-user/proxy: " Zhang Chen
2026-04-10 15:04 ` [PATCH V6 12/14] xen-block: " Zhang Chen
2026-04-10 15:04 ` [PATCH V6 13/14] qapi: examine IOThread attachment status via query-iothreads Zhang Chen
2026-04-23 13:09   ` Markus Armbruster
2026-04-27 15:10     ` Zhang Chen
2026-04-10 15:04 ` [PATCH V6 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=87ldedongv.fsf@pond.sub.org \
    --to=armbru@redhat.com \
    --cc=dave@treblig.org \
    --cc=eblake@redhat.com \
    --cc=mst@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=zhangckid@gmail.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.