From nobody Sat Nov 1 22:28:31 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1527573616798169.01144782310257; Mon, 28 May 2018 23:00:16 -0700 (PDT) Received: from localhost ([::1]:59053 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fNXg3-0005Si-A9 for importer@patchew.org; Tue, 29 May 2018 02:00:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58730) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fNXe7-0004EG-0G for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fNXe5-0001CA-Uc for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:11 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:45780 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fNXe5-0001Bj-Ok for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:09 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4691F4002248 for ; Tue, 29 May 2018 05:58:09 +0000 (UTC) Received: from xz-mi.nay.redhat.com (dhcp-14-151.nay.redhat.com [10.66.14.151]) by smtp.corp.redhat.com (Postfix) with ESMTP id 61DEF63F53; Tue, 29 May 2018 05:58:06 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Tue, 29 May 2018 13:57:49 +0800 Message-Id: <20180529055755.12404-2-peterx@redhat.com> In-Reply-To: <20180529055755.12404-1-peterx@redhat.com> References: <20180529055755.12404-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Tue, 29 May 2018 05:58:09 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Tue, 29 May 2018 05:58:09 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'peterx@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v9 1/7] monitor: rename out_lock to mon_lock X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The out_lock is protecting a few Monitor fields. In the future the monitor code will start to run in multiple threads. We are going to turn it into a bigger lock to protect not only the out buffer but also most of the rest. Since at it, rearrange the Monitor struct a bit. Reviewed-by: Stefan Hajnoczi Signed-off-by: Peter Xu Reviewed-by: Markus Armbruster --- monitor.c | 53 +++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/monitor.c b/monitor.c index 46814af533..14c681dc8a 100644 --- a/monitor.c +++ b/monitor.c @@ -207,15 +207,6 @@ struct Monitor { int suspend_cnt; /* Needs to be accessed atomically */ bool skip_flush; bool use_io_thr; - - /* We can't access guest memory when holding the lock */ - QemuMutex out_lock; - QString *outbuf; - guint out_watch; - - /* Read under either BQL or out_lock, written with BQL+out_lock. */ - int mux_out; - ReadLineState *rs; MonitorQMP qmp; gchar *mon_cpu_path; @@ -224,6 +215,20 @@ struct Monitor { mon_cmd_t *cmd_table; QLIST_HEAD(,mon_fd_t) fds; QTAILQ_ENTRY(Monitor) entry; + + /* + * The per-monitor lock. We can't access guest memory when holding + * the lock. + */ + QemuMutex mon_lock; + + /* + * Fields that are protected by the per-monitor lock. + */ + QString *outbuf; + guint out_watch; + /* Read under either BQL or mon_lock, written with BQL+mon_lock. */ + int mux_out; }; =20 /* Let's add monitor global variables to this struct. */ @@ -366,14 +371,14 @@ static gboolean monitor_unblocked(GIOChannel *chan, G= IOCondition cond, { Monitor *mon =3D opaque; =20 - qemu_mutex_lock(&mon->out_lock); + qemu_mutex_lock(&mon->mon_lock); mon->out_watch =3D 0; monitor_flush_locked(mon); - qemu_mutex_unlock(&mon->out_lock); + qemu_mutex_unlock(&mon->mon_lock); return FALSE; } =20 -/* Called with mon->out_lock held. */ +/* Called with mon->mon_lock held. */ static void monitor_flush_locked(Monitor *mon) { int rc; @@ -411,9 +416,9 @@ static void monitor_flush_locked(Monitor *mon) =20 void monitor_flush(Monitor *mon) { - qemu_mutex_lock(&mon->out_lock); + qemu_mutex_lock(&mon->mon_lock); monitor_flush_locked(mon); - qemu_mutex_unlock(&mon->out_lock); + qemu_mutex_unlock(&mon->mon_lock); } =20 /* flush at every end of line */ @@ -421,7 +426,7 @@ static void monitor_puts(Monitor *mon, const char *str) { char c; =20 - qemu_mutex_lock(&mon->out_lock); + qemu_mutex_lock(&mon->mon_lock); for(;;) { c =3D *str++; if (c =3D=3D '\0') @@ -434,7 +439,7 @@ static void monitor_puts(Monitor *mon, const char *str) monitor_flush_locked(mon); } } - qemu_mutex_unlock(&mon->out_lock); + qemu_mutex_unlock(&mon->mon_lock); } =20 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) @@ -725,7 +730,7 @@ static void monitor_data_init(Monitor *mon, bool skip_f= lush, bool use_io_thr) { memset(mon, 0, sizeof(Monitor)); - qemu_mutex_init(&mon->out_lock); + qemu_mutex_init(&mon->mon_lock); qemu_mutex_init(&mon->qmp.qmp_queue_lock); mon->outbuf =3D qstring_new(); /* Use *mon_cmds by default. */ @@ -745,7 +750,7 @@ static void monitor_data_destroy(Monitor *mon) } readline_free(mon->rs); qobject_unref(mon->outbuf); - qemu_mutex_destroy(&mon->out_lock); + qemu_mutex_destroy(&mon->mon_lock); qemu_mutex_destroy(&mon->qmp.qmp_queue_lock); monitor_qmp_cleanup_req_queue_locked(mon); monitor_qmp_cleanup_resp_queue_locked(mon); @@ -777,13 +782,13 @@ char *qmp_human_monitor_command(const char *command_l= ine, bool has_cpu_index, handle_hmp_command(&hmp, command_line); cur_mon =3D old_mon; =20 - qemu_mutex_lock(&hmp.out_lock); + qemu_mutex_lock(&hmp.mon_lock); if (qstring_get_length(hmp.outbuf) > 0) { output =3D g_strdup(qstring_get_str(hmp.outbuf)); } else { output =3D g_strdup(""); } - qemu_mutex_unlock(&hmp.out_lock); + qemu_mutex_unlock(&hmp.mon_lock); =20 out: monitor_data_destroy(&hmp); @@ -4377,9 +4382,9 @@ static void monitor_event(void *opaque, int event) =20 switch (event) { case CHR_EVENT_MUX_IN: - qemu_mutex_lock(&mon->out_lock); + qemu_mutex_lock(&mon->mon_lock); mon->mux_out =3D 0; - qemu_mutex_unlock(&mon->out_lock); + qemu_mutex_unlock(&mon->mon_lock); if (mon->reset_seen) { readline_restart(mon->rs); monitor_resume(mon); @@ -4399,9 +4404,9 @@ static void monitor_event(void *opaque, int event) } else { atomic_inc(&mon->suspend_cnt); } - qemu_mutex_lock(&mon->out_lock); + qemu_mutex_lock(&mon->mon_lock); mon->mux_out =3D 1; - qemu_mutex_unlock(&mon->out_lock); + qemu_mutex_unlock(&mon->mon_lock); break; =20 case CHR_EVENT_OPENED: --=20 2.17.0 From nobody Sat Nov 1 22:28:31 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1527573616810716.6652194516544; Mon, 28 May 2018 23:00:16 -0700 (PDT) Received: from localhost ([::1]:59054 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fNXg7-0005Ud-3Z for importer@patchew.org; Tue, 29 May 2018 02:00:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58744) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fNXeA-0004Fj-F7 for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fNXe9-0001EM-Dn for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:14 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:51268 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fNXe9-0001EC-7w for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:13 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BE909818BAFE for ; Tue, 29 May 2018 05:58:12 +0000 (UTC) Received: from xz-mi.nay.redhat.com (dhcp-14-151.nay.redhat.com [10.66.14.151]) by smtp.corp.redhat.com (Postfix) with ESMTP id DBAC042227; Tue, 29 May 2018 05:58:09 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Tue, 29 May 2018 13:57:50 +0800 Message-Id: <20180529055755.12404-3-peterx@redhat.com> In-Reply-To: <20180529055755.12404-1-peterx@redhat.com> References: <20180529055755.12404-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 29 May 2018 05:58:12 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 29 May 2018 05:58:12 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'peterx@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v9 2/7] monitor: protect mon->fds with mon_lock X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" mon->fds were protected by BQL. Now protect it by mon_lock so that it can even be used in monitor iothread. Reviewed-by: Stefan Hajnoczi Reviewed-by: Markus Armbruster Signed-off-by: Peter Xu --- monitor.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/monitor.c b/monitor.c index 14c681dc8a..d6c3c08932 100644 --- a/monitor.c +++ b/monitor.c @@ -213,7 +213,6 @@ struct Monitor { BlockCompletionFunc *password_completion_cb; void *password_opaque; mon_cmd_t *cmd_table; - QLIST_HEAD(,mon_fd_t) fds; QTAILQ_ENTRY(Monitor) entry; =20 /* @@ -225,6 +224,7 @@ struct Monitor { /* * Fields that are protected by the per-monitor lock. */ + QLIST_HEAD(, mon_fd_t) fds; QString *outbuf; guint out_watch; /* Read under either BQL or mon_lock, written with BQL+mon_lock. */ @@ -2189,7 +2189,7 @@ static void hmp_acl_remove(Monitor *mon, const QDict = *qdict) void qmp_getfd(const char *fdname, Error **errp) { mon_fd_t *monfd; - int fd; + int fd, tmp_fd; =20 fd =3D qemu_chr_fe_get_msgfd(&cur_mon->chr); if (fd =3D=3D -1) { @@ -2204,13 +2204,17 @@ void qmp_getfd(const char *fdname, Error **errp) return; } =20 + qemu_mutex_lock(&cur_mon->mon_lock); QLIST_FOREACH(monfd, &cur_mon->fds, next) { if (strcmp(monfd->name, fdname) !=3D 0) { continue; } =20 - close(monfd->fd); + tmp_fd =3D monfd->fd; monfd->fd =3D fd; + qemu_mutex_unlock(&cur_mon->mon_lock); + /* Make sure close() is out of critical section */ + close(tmp_fd); return; } =20 @@ -2219,24 +2223,31 @@ void qmp_getfd(const char *fdname, Error **errp) monfd->fd =3D fd; =20 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next); + qemu_mutex_unlock(&cur_mon->mon_lock); } =20 void qmp_closefd(const char *fdname, Error **errp) { mon_fd_t *monfd; + int tmp_fd; =20 + qemu_mutex_lock(&cur_mon->mon_lock); QLIST_FOREACH(monfd, &cur_mon->fds, next) { if (strcmp(monfd->name, fdname) !=3D 0) { continue; } =20 QLIST_REMOVE(monfd, next); - close(monfd->fd); + tmp_fd =3D monfd->fd; g_free(monfd->name); g_free(monfd); + qemu_mutex_unlock(&cur_mon->mon_lock); + /* Make sure close() is out of critical section */ + close(tmp_fd); return; } =20 + qemu_mutex_unlock(&cur_mon->mon_lock); error_setg(errp, QERR_FD_NOT_FOUND, fdname); } =20 @@ -2244,6 +2255,7 @@ int monitor_get_fd(Monitor *mon, const char *fdname, = Error **errp) { mon_fd_t *monfd; =20 + qemu_mutex_lock(&mon->mon_lock); QLIST_FOREACH(monfd, &mon->fds, next) { int fd; =20 @@ -2257,10 +2269,12 @@ int monitor_get_fd(Monitor *mon, const char *fdname= , Error **errp) QLIST_REMOVE(monfd, next); g_free(monfd->name); g_free(monfd); + qemu_mutex_unlock(&mon->mon_lock); =20 return fd; } =20 + qemu_mutex_unlock(&mon->mon_lock); error_setg(errp, "File descriptor named '%s' has not been found", fdna= me); return -1; } --=20 2.17.0 From nobody Sat Nov 1 22:28:31 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1527573624445167.83944314195958; Mon, 28 May 2018 23:00:24 -0700 (PDT) Received: from localhost ([::1]:59055 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fNXgC-0005a2-8A for importer@patchew.org; Tue, 29 May 2018 02:00:20 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58761) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fNXeG-0004Ki-P9 for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fNXeC-0001Gj-S6 for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:20 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:59206 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fNXeC-0001GU-O1 for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:16 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4245F7D839 for ; Tue, 29 May 2018 05:58:16 +0000 (UTC) Received: from xz-mi.nay.redhat.com (dhcp-14-151.nay.redhat.com [10.66.14.151]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5FD8642227; Tue, 29 May 2018 05:58:13 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Tue, 29 May 2018 13:57:51 +0800 Message-Id: <20180529055755.12404-4-peterx@redhat.com> In-Reply-To: <20180529055755.12404-1-peterx@redhat.com> References: <20180529055755.12404-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Tue, 29 May 2018 05:58:16 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Tue, 29 May 2018 05:58:16 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'peterx@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v9 3/7] monitor: more comments on lock-free elements X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add some explicit comments for both Readline and cpu_set/cpu_get helpers that they do not need the mon_lock protection. Signed-off-by: Peter Xu Reviewed-by: Markus Armbruster Reviewed-by: Stefan Hajnoczi --- monitor.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/monitor.c b/monitor.c index d6c3c08932..f23178951e 100644 --- a/monitor.c +++ b/monitor.c @@ -207,7 +207,15 @@ struct Monitor { int suspend_cnt; /* Needs to be accessed atomically */ bool skip_flush; bool use_io_thr; + + /* + * State used only in the thread "owning" the monitor. + * If @use_io_thr, this is mon_global.mon_iothread. + * Else, it's the main thread. + * These members can be safely accessed without locks. + */ ReadLineState *rs; + MonitorQMP qmp; gchar *mon_cpu_path; BlockCompletionFunc *password_completion_cb; @@ -1313,7 +1321,7 @@ void qmp_qmp_capabilities(bool has_enable, QMPCapabil= ityList *enable, cur_mon->qmp.commands =3D &qmp_commands; } =20 -/* set the current CPU defined by the user */ +/* Set the current CPU defined by the user. Callers must hold BQL. */ int monitor_set_cpu(int cpu_index) { CPUState *cpu; @@ -1327,6 +1335,7 @@ int monitor_set_cpu(int cpu_index) return 0; } =20 +/* Callers must hold BQL. */ static CPUState *mon_get_cpu_sync(bool synchronize) { CPUState *cpu; --=20 2.17.0 From nobody Sat Nov 1 22:28:31 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1527573807818885.4747100144385; Mon, 28 May 2018 23:03:27 -0700 (PDT) Received: from localhost ([::1]:59078 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fNXjC-00081a-VW for importer@patchew.org; Tue, 29 May 2018 02:03:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58785) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fNXeQ-0004Ta-8i for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fNXeM-0001Of-A1 for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:30 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:59216 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fNXeM-0001OO-61 for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:26 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B32B77D839 for ; Tue, 29 May 2018 05:58:25 +0000 (UTC) Received: from xz-mi.nay.redhat.com (dhcp-14-151.nay.redhat.com [10.66.14.151]) by smtp.corp.redhat.com (Postfix) with ESMTP id D933463F53; Tue, 29 May 2018 05:58:16 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Tue, 29 May 2018 13:57:52 +0800 Message-Id: <20180529055755.12404-5-peterx@redhat.com> In-Reply-To: <20180529055755.12404-1-peterx@redhat.com> References: <20180529055755.12404-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Tue, 29 May 2018 05:58:25 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Tue, 29 May 2018 05:58:25 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'peterx@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v9 4/7] monitor: fix comment for monitor_lock X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Fix typo in d622cb5879c. Meanwhile move these variables close to each other. monitor_qapi_event_state can be declared static, add that. Reported-by: Markus Armbruster Signed-off-by: Peter Xu Reviewed-by: Markus Armbruster Reviewed-by: Stefan Hajnoczi --- monitor.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/monitor.c b/monitor.c index f23178951e..2504696d76 100644 --- a/monitor.c +++ b/monitor.c @@ -267,10 +267,11 @@ typedef struct QMPRequest QMPRequest; /* QMP checker flags */ #define QMP_ACCEPT_UNKNOWNS 1 =20 -/* Protects mon_list, monitor_event_state. */ +/* Protects mon_list, monitor_qapi_event_state. */ static QemuMutex monitor_lock; - +static GHashTable *monitor_qapi_event_state; static QTAILQ_HEAD(mon_list, Monitor) mon_list; + static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets; static int mon_refcount; =20 @@ -572,8 +573,6 @@ static MonitorQAPIEventConf monitor_qapi_event_conf[QAP= I_EVENT__MAX] =3D { [QAPI_EVENT_VSERPORT_CHANGE] =3D { 1000 * SCALE_MS }, }; =20 -GHashTable *monitor_qapi_event_state; - /* * Emits the event to every monitor instance, @event is only used for trace * Called with monitor_lock held. --=20 2.17.0 From nobody Sat Nov 1 22:28:31 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1527574007062939.488446373831; Mon, 28 May 2018 23:06:47 -0700 (PDT) Received: from localhost ([::1]:59097 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fNXmH-00020Z-4c for importer@patchew.org; Tue, 29 May 2018 02:06:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58791) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fNXeQ-0004Tz-Mh for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fNXeP-0001Qg-Si for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:30 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:52728 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fNXeP-0001QW-N6 for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:29 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 389E7401EF09 for ; Tue, 29 May 2018 05:58:29 +0000 (UTC) Received: from xz-mi.nay.redhat.com (dhcp-14-151.nay.redhat.com [10.66.14.151]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5588B42227; Tue, 29 May 2018 05:58:26 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Tue, 29 May 2018 13:57:53 +0800 Message-Id: <20180529055755.12404-6-peterx@redhat.com> In-Reply-To: <20180529055755.12404-1-peterx@redhat.com> References: <20180529055755.12404-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 29 May 2018 05:58:29 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 29 May 2018 05:58:29 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'peterx@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v9 5/7] monitor: remove event_clock_type X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Instead, use a dynamic function to detect which clock we'll use. The problem is that the old code will let monitor initialization depends on qtest_enabled(). After this change, we don't have such a dependency any more. Suggested-by: Markus Armbruster Signed-off-by: Peter Xu --- monitor.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/monitor.c b/monitor.c index 2504696d76..bd9ab5597f 100644 --- a/monitor.c +++ b/monitor.c @@ -282,8 +282,6 @@ QmpCommandList qmp_commands, qmp_cap_negotiation_comman= ds; =20 Monitor *cur_mon; =20 -static QEMUClockType event_clock_type =3D QEMU_CLOCK_REALTIME; - static void monitor_command_cb(void *opaque, const char *cmdline, void *readline_opaque); =20 @@ -310,6 +308,15 @@ static inline bool monitor_is_hmp_non_interactive(cons= t Monitor *mon) return !monitor_is_qmp(mon) && !monitor_uses_readline(mon); } =20 +static inline QEMUClockType monitor_get_clock(void) +{ + if (qtest_enabled()) { + return QEMU_CLOCK_VIRTUAL; + } else { + return QEMU_CLOCK_REALTIME; + } +} + /** * Is the current monitor, if any, a QMP monitor? */ @@ -633,7 +640,7 @@ monitor_qapi_event_queue(QAPIEvent event, QDict *qdict,= Error **errp) * monitor_qapi_event_handler() in evconf->rate ns. Any * events arriving before then will be delayed until then. */ - int64_t now =3D qemu_clock_get_ns(event_clock_type); + int64_t now =3D qemu_clock_get_ns(monitor_get_clock()); =20 monitor_qapi_event_emit(event, qdict); =20 @@ -641,7 +648,7 @@ monitor_qapi_event_queue(QAPIEvent event, QDict *qdict,= Error **errp) evstate->event =3D event; evstate->data =3D qobject_ref(data); evstate->qdict =3D NULL; - evstate->timer =3D timer_new_ns(event_clock_type, + evstate->timer =3D timer_new_ns(monitor_get_clock(), monitor_qapi_event_handler, evstate); g_hash_table_add(monitor_qapi_event_state, evstate); @@ -666,7 +673,7 @@ static void monitor_qapi_event_handler(void *opaque) qemu_mutex_lock(&monitor_lock); =20 if (evstate->qdict) { - int64_t now =3D qemu_clock_get_ns(event_clock_type); + int64_t now =3D qemu_clock_get_ns(monitor_get_clock()); =20 monitor_qapi_event_emit(evstate->event, evstate->qdict); qobject_unref(evstate->qdict); @@ -722,10 +729,6 @@ static gboolean qapi_event_throttle_equal(const void *= a, const void *b) =20 static void monitor_qapi_event_init(void) { - if (qtest_enabled()) { - event_clock_type =3D QEMU_CLOCK_VIRTUAL; - } - monitor_qapi_event_state =3D g_hash_table_new(qapi_event_throttle_hash, qapi_event_throttle_equal); qmp_event_set_func_emit(monitor_qapi_event_queue); --=20 2.17.0 From nobody Sat Nov 1 22:28:31 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1527573796747588.121576655538; Mon, 28 May 2018 23:03:16 -0700 (PDT) Received: from localhost ([::1]:59077 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fNXj2-0007t6-1B for importer@patchew.org; Tue, 29 May 2018 02:03:16 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58807) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fNXeU-0004XD-21 for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fNXeT-0001SS-Am for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:34 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:27721 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fNXeT-0001SB-68 for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:33 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B1EF04022905 for ; Tue, 29 May 2018 05:58:32 +0000 (UTC) Received: from xz-mi.nay.redhat.com (dhcp-14-151.nay.redhat.com [10.66.14.151]) by smtp.corp.redhat.com (Postfix) with ESMTP id CDD6542227; Tue, 29 May 2018 05:58:29 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Tue, 29 May 2018 13:57:54 +0800 Message-Id: <20180529055755.12404-7-peterx@redhat.com> In-Reply-To: <20180529055755.12404-1-peterx@redhat.com> References: <20180529055755.12404-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 29 May 2018 05:58:32 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 29 May 2018 05:58:32 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'peterx@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v9 6/7] monitor: move init global earlier X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Before this patch, monitor fd helpers might be called even earlier than monitor_init_globals(). This can be problematic. After previous work, now monitor_init_globals() does not depend on accelerator initialization any more. Call it earlier (before CLI parsing; that's where the monitor APIs might be called) to make sure it is called before any of the monitor APIs. Suggested-by: Markus Armbruster Signed-off-by: Peter Xu Reviewed-by: Markus Armbruster Reviewed-by: Stefan Hajnoczi --- vl.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/vl.c b/vl.c index 038d7f8042..97b43b399f 100644 --- a/vl.c +++ b/vl.c @@ -3074,6 +3074,7 @@ int main(int argc, char **argv, char **envp) =20 runstate_init(); postcopy_infrastructure_init(); + monitor_init_globals(); =20 if (qcrypto_init(&err) < 0) { error_reportf_err(err, "cannot initialize crypto: "); @@ -4490,12 +4491,6 @@ int main(int argc, char **argv, char **envp) default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS); default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS); =20 - /* - * Note: qtest_enabled() (which is used in monitor_qapi_event_init()) - * depends on configure_accelerator() above. - */ - monitor_init_globals(); - if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, NULL)) { exit(1); --=20 2.17.0 From nobody Sat Nov 1 22:28:31 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1527573998554205.803120657372; Mon, 28 May 2018 23:06:38 -0700 (PDT) Received: from localhost ([::1]:59096 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fNXm9-0001tU-4O for importer@patchew.org; Tue, 29 May 2018 02:06:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58829) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fNXea-0004dA-SJ for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fNXeW-0001U5-Rs for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:40 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:52730 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fNXeW-0001Tq-MT for qemu-devel@nongnu.org; Tue, 29 May 2018 01:58:36 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 36F5A401EF08 for ; Tue, 29 May 2018 05:58:36 +0000 (UTC) Received: from xz-mi.nay.redhat.com (dhcp-14-151.nay.redhat.com [10.66.14.151]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5495B42227; Tue, 29 May 2018 05:58:33 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Tue, 29 May 2018 13:57:55 +0800 Message-Id: <20180529055755.12404-8-peterx@redhat.com> In-Reply-To: <20180529055755.12404-1-peterx@redhat.com> References: <20180529055755.12404-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 29 May 2018 05:58:36 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 29 May 2018 05:58:36 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'peterx@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v9 7/7] monitor: add lock to protect mon_fdsets X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Introduce a new global big lock for mon_fdsets. Take it where needed. The monitor_fdset_get_fd() handling is a bit tricky: now we need to call qemu_mutex_unlock() which might pollute errno, so we need to make sure the correct errno be passed up to the callers. To make things simpler, we let monitor_fdset_get_fd() return the -errno directly when error happens, then in qemu_open() we move it back into errno. Signed-off-by: Peter Xu Reviewed-by: Markus Armbruster Reviewed-by: Stefan Hajnoczi --- monitor.c | 52 +++++++++++++++++++++++++++++++++++++++++---------- stubs/fdset.c | 2 +- util/osdep.c | 3 ++- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/monitor.c b/monitor.c index bd9ab5597f..09902a58d5 100644 --- a/monitor.c +++ b/monitor.c @@ -272,7 +272,10 @@ static QemuMutex monitor_lock; static GHashTable *monitor_qapi_event_state; static QTAILQ_HEAD(mon_list, Monitor) mon_list; =20 +/* Protects mon_fdsets */ +static QemuMutex mon_fdsets_lock; static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets; + static int mon_refcount; =20 static mon_cmd_t mon_cmds[]; @@ -2317,9 +2320,11 @@ static void monitor_fdsets_cleanup(void) MonFdset *mon_fdset; MonFdset *mon_fdset_next; =20 + qemu_mutex_lock(&mon_fdsets_lock); QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) { monitor_fdset_cleanup(mon_fdset); } + qemu_mutex_unlock(&mon_fdsets_lock); } =20 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque, @@ -2354,6 +2359,7 @@ void qmp_remove_fd(int64_t fdset_id, bool has_fd, int= 64_t fd, Error **errp) MonFdsetFd *mon_fdset_fd; char fd_str[60]; =20 + qemu_mutex_lock(&mon_fdsets_lock); QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { if (mon_fdset->id !=3D fdset_id) { continue; @@ -2373,10 +2379,12 @@ void qmp_remove_fd(int64_t fdset_id, bool has_fd, i= nt64_t fd, Error **errp) goto error; } monitor_fdset_cleanup(mon_fdset); + qemu_mutex_unlock(&mon_fdsets_lock); return; } =20 error: + qemu_mutex_unlock(&mon_fdsets_lock); if (has_fd) { snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId= 64, fdset_id, fd); @@ -2392,6 +2400,7 @@ FdsetInfoList *qmp_query_fdsets(Error **errp) MonFdsetFd *mon_fdset_fd; FdsetInfoList *fdset_list =3D NULL; =20 + qemu_mutex_lock(&mon_fdsets_lock); QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { FdsetInfoList *fdset_info =3D g_malloc0(sizeof(*fdset_info)); FdsetFdInfoList *fdsetfd_list =3D NULL; @@ -2421,6 +2430,7 @@ FdsetInfoList *qmp_query_fdsets(Error **errp) fdset_info->next =3D fdset_list; fdset_list =3D fdset_info; } + qemu_mutex_unlock(&mon_fdsets_lock); =20 return fdset_list; } @@ -2433,6 +2443,7 @@ AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdse= t_id, int64_t fdset_id, MonFdsetFd *mon_fdset_fd; AddfdInfo *fdinfo; =20 + qemu_mutex_lock(&mon_fdsets_lock); if (has_fdset_id) { QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { /* Break if match found or match impossible due to ordering by= ID */ @@ -2453,6 +2464,7 @@ AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdse= t_id, int64_t fdset_id, if (fdset_id < 0) { error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id", "a non-negative value"); + qemu_mutex_unlock(&mon_fdsets_lock); return NULL; } /* Use specified fdset ID */ @@ -2503,16 +2515,21 @@ AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fd= set_id, int64_t fdset_id, fdinfo->fdset_id =3D mon_fdset->id; fdinfo->fd =3D mon_fdset_fd->fd; =20 + qemu_mutex_unlock(&mon_fdsets_lock); return fdinfo; } =20 int monitor_fdset_get_fd(int64_t fdset_id, int flags) { -#ifndef _WIN32 +#ifdef _WIN32 + return -ENOENT; +#else MonFdset *mon_fdset; MonFdsetFd *mon_fdset_fd; int mon_fd_flags; + int ret; =20 + qemu_mutex_lock(&mon_fdsets_lock); QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { if (mon_fdset->id !=3D fdset_id) { continue; @@ -2520,20 +2537,24 @@ int monitor_fdset_get_fd(int64_t fdset_id, int flag= s) QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { mon_fd_flags =3D fcntl(mon_fdset_fd->fd, F_GETFL); if (mon_fd_flags =3D=3D -1) { - return -1; + ret =3D -errno; + goto out; } =20 if ((flags & O_ACCMODE) =3D=3D (mon_fd_flags & O_ACCMODE)) { - return mon_fdset_fd->fd; + ret =3D mon_fdset_fd->fd; + goto out; } } - errno =3D EACCES; - return -1; + ret =3D -EACCES; + goto out; } -#endif + ret =3D -ENOENT; =20 - errno =3D ENOENT; - return -1; +out: + qemu_mutex_unlock(&mon_fdsets_lock); + return ret; +#endif } =20 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd) @@ -2541,20 +2562,25 @@ int monitor_fdset_dup_fd_add(int64_t fdset_id, int = dup_fd) MonFdset *mon_fdset; MonFdsetFd *mon_fdset_fd_dup; =20 + qemu_mutex_lock(&mon_fdsets_lock); QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { if (mon_fdset->id !=3D fdset_id) { continue; } QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) { if (mon_fdset_fd_dup->fd =3D=3D dup_fd) { - return -1; + goto err; } } mon_fdset_fd_dup =3D g_malloc0(sizeof(*mon_fdset_fd_dup)); mon_fdset_fd_dup->fd =3D dup_fd; QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next); + qemu_mutex_unlock(&mon_fdsets_lock); return 0; } + +err: + qemu_mutex_unlock(&mon_fdsets_lock); return -1; } =20 @@ -2563,6 +2589,7 @@ static int monitor_fdset_dup_fd_find_remove(int dup_f= d, bool remove) MonFdset *mon_fdset; MonFdsetFd *mon_fdset_fd_dup; =20 + qemu_mutex_lock(&mon_fdsets_lock); QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) { if (mon_fdset_fd_dup->fd =3D=3D dup_fd) { @@ -2571,13 +2598,17 @@ static int monitor_fdset_dup_fd_find_remove(int dup= _fd, bool remove) if (QLIST_EMPTY(&mon_fdset->dup_fds)) { monitor_fdset_cleanup(mon_fdset); } - return -1; + goto err; } else { + qemu_mutex_unlock(&mon_fdsets_lock); return mon_fdset->id; } } } } + +err: + qemu_mutex_unlock(&mon_fdsets_lock); return -1; } =20 @@ -4511,6 +4542,7 @@ void monitor_init_globals(void) monitor_qapi_event_init(); sortcmdlist(); qemu_mutex_init(&monitor_lock); + qemu_mutex_init(&mon_fdsets_lock); monitor_iothread_init(); } =20 diff --git a/stubs/fdset.c b/stubs/fdset.c index 6020cf28c8..4f3edf2ea4 100644 --- a/stubs/fdset.c +++ b/stubs/fdset.c @@ -14,7 +14,7 @@ int monitor_fdset_dup_fd_find(int dup_fd) =20 int monitor_fdset_get_fd(int64_t fdset_id, int flags) { - return -1; + return -ENOENT; } =20 void monitor_fdset_dup_fd_remove(int dupfd) diff --git a/util/osdep.c b/util/osdep.c index a73de0e1ba..ea51d500b6 100644 --- a/util/osdep.c +++ b/util/osdep.c @@ -302,7 +302,8 @@ int qemu_open(const char *name, int flags, ...) } =20 fd =3D monitor_fdset_get_fd(fdset_id, flags); - if (fd =3D=3D -1) { + if (fd < 0) { + errno =3D -fd; return -1; } =20 --=20 2.17.0