From nobody Sat Apr 11 20:09:21 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1775394769; cv=none; d=zohomail.com; s=zohoarc; b=K/2oQpJw41A6cOKJSsVWE5UWWy71dsnaVL/rfPYstUZBSM2t0aoQEv3nBNnS3wRqzwDNEN7GE7i7ytQfAys5l4mvE8eGkLUklhw0fsLx3BCx8eDVFVwwObqG34eqF8LyJBDuv0o84IXUhnsHLtPtRGxlCmzzxW3nYccZYB2v+R0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1775394769; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=s6++0i+ZMdkn6BK52kLbQPFksozIQJyOVB03SKOv2Ro=; b=EVTb1oDQV+ptFgkXRXoTxnSNTsOwHVKvrlzlD/UFn0fzwF7wMWHW0BGAKwB+AV6oG7tiDa5psRNLAGsifdZICr7XxYAYmEiFk96mS5JkwmN85EDMmYkyxLRQyeKx53U64OMFOFC9w9rZxlwgBb1FomDy5MLOzlrg1FyqwdsVLKI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1775394769319557.8912813771013; Sun, 5 Apr 2026 06:12:49 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1w9NGV-0006Wd-JW; Sun, 05 Apr 2026 09:11:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1w9NGP-0006UN-HA for qemu-devel@nongnu.org; Sun, 05 Apr 2026 09:11:41 -0400 Received: from sea.source.kernel.org ([2600:3c0a:e001:78e:0:1991:8:25]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1w9NGN-0006d4-IC for qemu-devel@nongnu.org; Sun, 05 Apr 2026 09:11:41 -0400 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sea.source.kernel.org (Postfix) with ESMTP id EED2C43890; Sun, 5 Apr 2026 13:11:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5FF9EC116C6; Sun, 5 Apr 2026 13:11:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775394695; bh=Iiu1cUQv91hAV1L5+tULf8zTWFqn+vLV9HQtttoGHvU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ku0Ams00pFmGdgZVctYWmz9Q/l3vI6XbsiRILxxw+7ElA7OSDpE8vUjwjg0kRsnI5 FoxQRY0ccxBhy+npGxKh71tZ+zlj147bOandVZT03/uZh+6cBsLp7Yv5nxnOk7W3UK Uz6IREIEaBpFMtP7Cm9N9Bju4WD/dIfNiN9iSE5Ms31XjDLYV02TKPGSYQ5+C6aGOQ fJaw+MTZRBOvNvCUfn9z1ZFWgaxZQ4RI815NC0ljFLolIwYkG9rEas1rRICZovwYov drM1ImFycFiiD3hsP5PTmkOvYsE7pibxuIDPrFzSDUNu8Rzeuaw3TEC5r1GoxnOsNv wLsuG4mXOmKTQ== From: Christian Brauner Date: Sun, 05 Apr 2026 15:11:17 +0200 Subject: [PATCH v2 1/5] monitor: store monitor id and dynamic flag in Monitor struct MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260405-work-qmp-monitor-hotplug-v2-1-ad5bedd2917a@kernel.org> References: <20260405-work-qmp-monitor-hotplug-v2-0-ad5bedd2917a@kernel.org> In-Reply-To: <20260405-work-qmp-monitor-hotplug-v2-0-ad5bedd2917a@kernel.org> To: qemu-devel@nongnu.org Cc: Markus Armbruster , Eric Blake , Fabiano Rosas , Laurent Vivier , Paolo Bonzini , Thomas Huth , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Christian Brauner X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=7269; i=brauner@kernel.org; h=from:subject:message-id; bh=Iiu1cUQv91hAV1L5+tULf8zTWFqn+vLV9HQtttoGHvU=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWReim/8zL3z2R8b9p+Hlr/3+z9N+oTOYjfHvn2/JGYeV /km53VzRkcpC4MYF4OsmCKLQ7tJuNxynorNRpkaMHNYmUCGMHBxCsBELicwMiwKFjK/1sn8SqRB JHzCnd9M7j+lNATi5hpp+Auo71KomsDwz+K4Q6Hr2rvFG1S/cygpf1r8Yb+32tzM3C/zTm4I2Hl biBkA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2600:3c0a:e001:78e:0:1991:8:25; envelope-from=brauner@kernel.org; helo=sea.source.kernel.org X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @kernel.org) X-ZM-MESSAGEID: 1775394772557154100 Add 'id', 'dynamic', and 'dead' fields to struct Monitor. The id field stores the monitor identifier from MonitorOptions which was previously parsed but discarded. The dynamic flag marks monitors created at runtime via the upcoming monitor-add command, and the dead flag will be used for deferred destruction during monitor-remove. Extend monitor_init_qmp() to accept id and dynamic parameters so these are set before the monitor is added to mon_list. For iothread monitors, move monitor_list_append() from the setup BH to the caller so monitor_find_by_id() can detect duplicates immediately. Without this, two concurrent monitor-add calls could both pass the duplicate check before either BH runs. This means the monitor is now visible in mon_list before its chardev handlers are set up, which was not the case before. This is safe because the request queue is still empty (no chardev handlers means no monitor_qmp_read(), so the dispatcher finds nothing to dispatch) and event broadcast is handled below. This requires initializing mon->commands =3D &qmp_cap_negotiation_commands before monitor_list_append(). Without it, commands is NULL (from g_new0) and monitor_qapi_event_emit() would not skip the monitor during event broadcast -- its check is specifically for the qmp_cap_negotiation_commands pointer, so a NULL falls through to qmp_send_response() on an uninitialized monitor. CHR_EVENT_OPENED sets commands to the same value again later. Add monitor_find_by_id() to look up monitors by identifier. The lookup takes monitor_lock to serialize with the I/O thread BH that modifies mon_list, but releases it before returning. The caller must hold the BQL to ensure the returned pointer remains valid since only BQL holders can destroy monitors. Free the id string in monitor_data_destroy(). Signed-off-by: Christian Brauner (Amutable) --- include/monitor/monitor.h | 3 ++- monitor/monitor-internal.h | 5 +++++ monitor/monitor.c | 21 ++++++++++++++++++++- monitor/qmp.c | 11 ++++++++--- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h index 296690e1f1..7a2bb603e4 100644 --- a/include/monitor/monitor.h +++ b/include/monitor/monitor.h @@ -19,7 +19,8 @@ bool monitor_cur_is_qmp(void); =20 void monitor_init_globals(void); void monitor_init_globals_core(void); -void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp); +void monitor_init_qmp(Chardev *chr, bool pretty, const char *id, + bool dynamic, Error **errp); void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp); int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp); int monitor_init_opts(QemuOpts *opts, Error **errp); diff --git a/monitor/monitor-internal.h b/monitor/monitor-internal.h index feca111ae3..8f5fe7c111 100644 --- a/monitor/monitor-internal.h +++ b/monitor/monitor-internal.h @@ -98,7 +98,10 @@ struct Monitor { bool is_qmp; bool skip_flush; bool use_io_thread; + bool dynamic; /* true if created via monitor-add */ + bool dead; /* true if monitor-remove called, awaiting= drain */ =20 + char *id; /* Monitor identifier (NULL for unnamed CL= I monitors) */ char *mon_cpu_path; QTAILQ_ENTRY(Monitor) entry; =20 @@ -181,6 +184,8 @@ void monitor_data_destroy_qmp(MonitorQMP *mon); void coroutine_fn monitor_qmp_dispatcher_co(void *data); void qmp_dispatcher_co_wake(void); =20 +Monitor *monitor_find_by_id(const char *id); + int get_monitor_def(Monitor *mon, int64_t *pval, const char *name); void handle_hmp_command(MonitorHMP *mon, const char *cmdline); int hmp_compare_cmd(const char *name, const char *list); diff --git a/monitor/monitor.c b/monitor/monitor.c index 00b93ed612..7144255e12 100644 --- a/monitor/monitor.c +++ b/monitor/monitor.c @@ -622,6 +622,7 @@ void monitor_data_init(Monitor *mon, bool is_qmp, bool = skip_flush, =20 void monitor_data_destroy(Monitor *mon) { + g_free(mon->id); g_free(mon->mon_cpu_path); qemu_chr_fe_deinit(&mon->chr, false); if (monitor_is_qmp(mon)) { @@ -633,6 +634,24 @@ void monitor_data_destroy(Monitor *mon) qemu_mutex_destroy(&mon->mon_lock); } =20 +/* + * Look up a monitor by its id. The monitor_lock is released before + * returning, so the caller must hold the BQL to ensure the returned + * pointer remains valid (only BQL holders can destroy monitors). + */ +Monitor *monitor_find_by_id(const char *id) +{ + Monitor *mon; + + QEMU_LOCK_GUARD(&monitor_lock); + QTAILQ_FOREACH(mon, &mon_list, entry) { + if (mon->id && strcmp(mon->id, id) =3D=3D 0) { + return mon; + } + } + return NULL; +} + void monitor_cleanup(void) { /* @@ -732,7 +751,7 @@ int monitor_init(MonitorOptions *opts, bool allow_hmp, = Error **errp) =20 switch (opts->mode) { case MONITOR_MODE_CONTROL: - monitor_init_qmp(chr, opts->pretty, errp); + monitor_init_qmp(chr, opts->pretty, opts->id, false, errp); break; case MONITOR_MODE_READLINE: if (!allow_hmp) { diff --git a/monitor/qmp.c b/monitor/qmp.c index 687019811f..afbe2283d6 100644 --- a/monitor/qmp.c +++ b/monitor/qmp.c @@ -510,10 +510,10 @@ static void monitor_qmp_setup_handlers_bh(void *opaqu= e) qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read, monitor_qmp_read, monitor_qmp_event, NULL, &mon->common, context, true); - monitor_list_append(&mon->common); } =20 -void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp) +void monitor_init_qmp(Chardev *chr, bool pretty, const char *id, + bool dynamic, Error **errp) { MonitorQMP *mon =3D g_new0(MonitorQMP, 1); =20 @@ -527,12 +527,16 @@ void monitor_init_qmp(Chardev *chr, bool pretty, Erro= r **errp) monitor_data_init(&mon->common, true, false, qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT= )); =20 + mon->common.id =3D g_strdup(id); + mon->common.dynamic =3D dynamic; mon->pretty =3D pretty; =20 qemu_mutex_init(&mon->qmp_queue_lock); mon->qmp_requests =3D g_queue_new(); =20 json_message_parser_init(&mon->parser, handle_qmp_command, mon, NULL); + /* Prevent event broadcast to an uninitialized monitor. */ + mon->commands =3D &qmp_cap_negotiation_commands; if (mon->common.use_io_thread) { /* * Make sure the old iowatch is gone. It's possible when @@ -551,7 +555,8 @@ void monitor_init_qmp(Chardev *chr, bool pretty, Error = **errp) */ aio_bh_schedule_oneshot(iothread_get_aio_context(mon_iothread), monitor_qmp_setup_handlers_bh, mon); - /* The bottom half will add @mon to @mon_list */ + /* Synchronous insert for immediate duplicate detection. */ + monitor_list_append(&mon->common); } else { qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read, monitor_qmp_read, monitor_qmp_event, --=20 2.47.3