From nobody Sun May 5 16:23:10 2024 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 1528430411661319.9948665163698; Thu, 7 Jun 2018 21:00:11 -0700 (PDT) Received: from localhost ([::1]:32930 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fR8ZE-0007bW-IN for importer@patchew.org; Fri, 08 Jun 2018 00:00:00 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47218) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fR8Ur-0003tX-Gk for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fR8Up-0006wm-SY for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:29 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:34742 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 1fR8Up-0006wh-Mg for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:27 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3A0DA81663C4 for ; Fri, 8 Jun 2018 03:55:27 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-40.pek2.redhat.com [10.72.12.40]) by smtp.corp.redhat.com (Postfix) with ESMTP id 76D5210EE6C7; Fri, 8 Jun 2018 03:55:24 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Fri, 8 Jun 2018 11:55:05 +0800 Message-Id: <20180608035511.7439-2-peterx@redhat.com> In-Reply-To: <20180608035511.7439-1-peterx@redhat.com> References: <20180608035511.7439-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 08 Jun 2018 03:55:27 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 08 Jun 2018 03:55:27 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.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 v10 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 Reviewed-by: Markus Armbruster Signed-off-by: Peter Xu --- monitor.c | 53 +++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/monitor.c b/monitor.c index 6d0cec552e..5bc9b2dcd0 100644 --- a/monitor.c +++ b/monitor.c @@ -206,15 +206,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; @@ -223,6 +214,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. */ @@ -365,14 +370,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; @@ -410,9 +415,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 */ @@ -420,7 +425,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') @@ -433,7 +438,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) @@ -724,7 +729,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. */ @@ -744,7 +749,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); @@ -776,13 +781,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); @@ -4381,9 +4386,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); @@ -4403,9 +4408,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.1 From nobody Sun May 5 16:23:10 2024 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 1528430256816728.1385641327205; Thu, 7 Jun 2018 20:57:36 -0700 (PDT) Received: from localhost ([::1]:32918 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fR8Wu-0005au-2B for importer@patchew.org; Thu, 07 Jun 2018 23:57:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47244) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fR8Uu-0003vn-6i for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fR8Ut-0006xa-6e for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:32 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:55844 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 1fR8Ut-0006xL-0n for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:31 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8438240711D2 for ; Fri, 8 Jun 2018 03:55:30 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-40.pek2.redhat.com [10.72.12.40]) by smtp.corp.redhat.com (Postfix) with ESMTP id C502410EE6C7; Fri, 8 Jun 2018 03:55:27 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Fri, 8 Jun 2018 11:55:06 +0800 Message-Id: <20180608035511.7439-3-peterx@redhat.com> In-Reply-To: <20180608035511.7439-1-peterx@redhat.com> References: <20180608035511.7439-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Fri, 08 Jun 2018 03:55:30 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Fri, 08 Jun 2018 03:55:30 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.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 v10 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 5bc9b2dcd0..0fba3ccc20 100644 --- a/monitor.c +++ b/monitor.c @@ -212,7 +212,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 /* @@ -224,6 +223,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. */ @@ -2187,7 +2187,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) { @@ -2202,13 +2202,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 @@ -2217,24 +2221,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 @@ -2242,6 +2253,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 @@ -2255,10 +2267,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.1 From nobody Sun May 5 16:23:10 2024 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 1528430439068374.05827092659126; Thu, 7 Jun 2018 21:00:39 -0700 (PDT) Received: from localhost ([::1]:32931 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fR8Zq-00089Q-Ci for importer@patchew.org; Fri, 08 Jun 2018 00:00:38 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47280) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fR8Uy-00045W-4v for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fR8Uw-00070F-Qg for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:35 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:53988 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 1fR8Uw-000707-LD for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:34 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2EB5C4023156 for ; Fri, 8 Jun 2018 03:55:34 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-40.pek2.redhat.com [10.72.12.40]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1BE6010EE6C7; Fri, 8 Jun 2018 03:55:30 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Fri, 8 Jun 2018 11:55:07 +0800 Message-Id: <20180608035511.7439-4-peterx@redhat.com> In-Reply-To: <20180608035511.7439-1-peterx@redhat.com> References: <20180608035511.7439-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 08 Jun 2018 03:55:34 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 08 Jun 2018 03:55:34 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.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 v10 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. Reviewed-by: Stefan Hajnoczi Reviewed-by: Markus Armbruster Signed-off-by: Peter Xu --- monitor.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/monitor.c b/monitor.c index 0fba3ccc20..5c60bf08cc 100644 --- a/monitor.c +++ b/monitor.c @@ -206,7 +206,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; @@ -1311,7 +1319,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; @@ -1325,6 +1333,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.1 From nobody Sun May 5 16:23:10 2024 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 1528430613472456.5856038458011; Thu, 7 Jun 2018 21:03:33 -0700 (PDT) Received: from localhost ([::1]:32949 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fR8ce-0002qp-Kr for importer@patchew.org; Fri, 08 Jun 2018 00:03:32 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47359) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fR8V9-0004aU-B8 for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fR8V8-00073m-Ns for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:47 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:34750 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 1fR8V8-00073a-Hy for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:46 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1171181B0096 for ; Fri, 8 Jun 2018 03:55:46 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-40.pek2.redhat.com [10.72.12.40]) by smtp.corp.redhat.com (Postfix) with ESMTP id EF4C010EE6C7; Fri, 8 Jun 2018 03:55:34 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Fri, 8 Jun 2018 11:55:08 +0800 Message-Id: <20180608035511.7439-5-peterx@redhat.com> In-Reply-To: <20180608035511.7439-1-peterx@redhat.com> References: <20180608035511.7439-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 08 Jun 2018 03:55:46 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 08 Jun 2018 03:55:46 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.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 v10 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 Reviewed-by: Stefan Hajnoczi Reviewed-by: Markus Armbruster Signed-off-by: Peter Xu --- monitor.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/monitor.c b/monitor.c index 5c60bf08cc..fa9c35631f 100644 --- a/monitor.c +++ b/monitor.c @@ -266,10 +266,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 @@ -571,8 +572,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.1 From nobody Sun May 5 16:23:10 2024 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 1528430279818859.3881318297987; Thu, 7 Jun 2018 20:57:59 -0700 (PDT) Received: from localhost ([::1]:32920 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fR8XH-0005v4-3x for importer@patchew.org; Thu, 07 Jun 2018 23:57:59 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47393) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fR8VE-0004ia-Ap for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fR8VC-00074u-4t for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:52 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:34752 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 1fR8VC-00074m-02 for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:50 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 861CD80125D6 for ; Fri, 8 Jun 2018 03:55:49 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-40.pek2.redhat.com [10.72.12.40]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9F89410EE6C7; Fri, 8 Jun 2018 03:55:46 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Fri, 8 Jun 2018 11:55:09 +0800 Message-Id: <20180608035511.7439-6-peterx@redhat.com> In-Reply-To: <20180608035511.7439-1-peterx@redhat.com> References: <20180608035511.7439-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 08 Jun 2018 03:55:49 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 08 Jun 2018 03:55:49 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.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 v10 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 depend on configure_accelerator() (that's where qtest_enabled() start to take effect). After this change, we don't have such a dependency any more. We just need to make sure configure_accelerator() is called when we start to use it. Now it's only used in monitor_qapi_event_queue() and monitor_qapi_event_handler(), so we're good. Suggested-by: Markus Armbruster Signed-off-by: Peter Xu Reviewed-by: Markus Armbruster Reviewed-by: Stefan Hajnoczi --- monitor.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/monitor.c b/monitor.c index fa9c35631f..2bda0dfa12 100644 --- a/monitor.c +++ b/monitor.c @@ -281,8 +281,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 @@ -309,6 +307,19 @@ static inline bool monitor_is_hmp_non_interactive(cons= t Monitor *mon) return !monitor_is_qmp(mon) && !monitor_uses_readline(mon); } =20 +/* + * This should never be called before configure_accelerator() since + * only until then could we know whether qtest was enabled or not. + */ +static inline QEMUClockType monitor_get_clock(void) +{ + /* + * This allows us to perform tests on the monitor queues to verify + * that the rate limits are enforced. + */ + return qtest_enabled() ? QEMU_CLOCK_VIRTUAL : QEMU_CLOCK_REALTIME; +} + /** * Is the current monitor, if any, a QMP monitor? */ @@ -632,7 +643,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 @@ -640,7 +651,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); @@ -665,7 +676,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); @@ -721,10 +732,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.1 From nobody Sun May 5 16:23:10 2024 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 1528430468242440.4170073295579; Thu, 7 Jun 2018 21:01:08 -0700 (PDT) Received: from localhost ([::1]:32938 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fR8aJ-0000Gu-FX for importer@patchew.org; Fri, 08 Jun 2018 00:01:07 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47412) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fR8VG-0004kW-Bn for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fR8VF-00076F-Mx for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:54 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47358 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 1fR8VF-000767-IE for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:55:53 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1D5ACBB42D for ; Fri, 8 Jun 2018 03:55:53 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-40.pek2.redhat.com [10.72.12.40]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3A04610EE6C7; Fri, 8 Jun 2018 03:55:49 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Fri, 8 Jun 2018 11:55:10 +0800 Message-Id: <20180608035511.7439-7-peterx@redhat.com> In-Reply-To: <20180608035511.7439-1-peterx@redhat.com> References: <20180608035511.7439-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Fri, 08 Jun 2018 03:55:53 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Fri, 08 Jun 2018 03:55:53 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.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 v10 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 Reviewed-by: Stefan Hajnoczi Reviewed-by: Markus Armbruster Signed-off-by: Peter Xu --- vl.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/vl.c b/vl.c index 06031715ac..5ddea3d235 100644 --- a/vl.c +++ b/vl.c @@ -2978,6 +2978,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: "); @@ -4412,12 +4413,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.1 From nobody Sun May 5 16:23:10 2024 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 1528430620324236.52798018307658; Thu, 7 Jun 2018 21:03:40 -0700 (PDT) Received: from localhost ([::1]:32950 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fR8cl-0002w0-H1 for importer@patchew.org; Fri, 08 Jun 2018 00:03:39 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47509) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fR8VW-0004zO-Io for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:56:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fR8VR-0007Cx-ME for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:56:10 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47364 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 1fR8VR-0007Cp-Fu for qemu-devel@nongnu.org; Thu, 07 Jun 2018 23:56:05 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 04931BB42D for ; Fri, 8 Jun 2018 03:56:05 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-40.pek2.redhat.com [10.72.12.40]) by smtp.corp.redhat.com (Postfix) with ESMTP id C8D1310EE6C7; Fri, 8 Jun 2018 03:55:53 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Fri, 8 Jun 2018 11:55:11 +0800 Message-Id: <20180608035511.7439-8-peterx@redhat.com> In-Reply-To: <20180608035511.7439-1-peterx@redhat.com> References: <20180608035511.7439-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Fri, 08 Jun 2018 03:56:05 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Fri, 08 Jun 2018 03:56:05 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.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 v10 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. Reviewed-by: Stefan Hajnoczi Reviewed-by: Markus Armbruster Signed-off-by: Peter Xu --- 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 2bda0dfa12..ede4aed8b6 100644 --- a/monitor.c +++ b/monitor.c @@ -271,7 +271,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[]; @@ -2319,9 +2322,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, @@ -2356,6 +2361,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; @@ -2375,10 +2381,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); @@ -2394,6 +2402,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; @@ -2423,6 +2432,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; } @@ -2435,6 +2445,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 */ @@ -2455,6 +2466,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 */ @@ -2505,16 +2517,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; @@ -2522,20 +2539,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) @@ -2543,20 +2564,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 @@ -2565,6 +2591,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) { @@ -2573,13 +2600,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 @@ -4519,6 +4550,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.1