From nobody Wed Oct 29 11:29:44 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 1525839588637673.1512882153273; Tue, 8 May 2018 21:19:48 -0700 (PDT) Received: from localhost ([::1]:54398 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGGZm-0007lZ-3b for importer@patchew.org; Wed, 09 May 2018 00:19:38 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34813) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGGY3-0006wk-VD for qemu-devel@nongnu.org; Wed, 09 May 2018 00:17:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fGGY2-0002hN-T4 for qemu-devel@nongnu.org; Wed, 09 May 2018 00:17:51 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:58396 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 1fGGY2-0002gw-NA for qemu-devel@nongnu.org; Wed, 09 May 2018 00:17: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 5488BE4480 for ; Wed, 9 May 2018 04:17:50 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-74.pek2.redhat.com [10.72.12.74]) by smtp.corp.redhat.com (Postfix) with ESMTP id A11F510F1BEA; Wed, 9 May 2018 04:17:47 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 9 May 2018 12:17:31 +0800 Message-Id: <20180509041734.14135-2-peterx@redhat.com> In-Reply-To: <20180509041734.14135-1-peterx@redhat.com> References: <20180509041734.14135-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]); Wed, 09 May 2018 04:17:50 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 09 May 2018 04:17:50 +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 v5 1/4] 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 was only protecting a few Monitor fields. In the future the monitor code will start to run in multiple threads. We turn it into a bigger lock to protect not only the out buffer but also all the rest. For now we share the lock. We can split the lock when needed. Since at it, arrange the Monitor struct a bit. Reviewed-by: Stefan Hajnoczi Signed-off-by: Peter Xu --- 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 Wed Oct 29 11:29:44 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 1525839678646123.98120221247655; Tue, 8 May 2018 21:21:18 -0700 (PDT) Received: from localhost ([::1]:54410 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGGbN-0001Lm-Uz for importer@patchew.org; Wed, 09 May 2018 00:21:17 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34848) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGGY7-0006y9-2b for qemu-devel@nongnu.org; Wed, 09 May 2018 00:17:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fGGY6-0002ly-3Q for qemu-devel@nongnu.org; Wed, 09 May 2018 00:17:55 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:48538 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 1fGGY5-0002lH-Uk for qemu-devel@nongnu.org; Wed, 09 May 2018 00:17:54 -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 9357F40200B2 for ; Wed, 9 May 2018 04:17:53 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-74.pek2.redhat.com [10.72.12.74]) by smtp.corp.redhat.com (Postfix) with ESMTP id E075C10F1BEA; Wed, 9 May 2018 04:17:50 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 9 May 2018 12:17:32 +0800 Message-Id: <20180509041734.14135-3-peterx@redhat.com> In-Reply-To: <20180509041734.14135-1-peterx@redhat.com> References: <20180509041734.14135-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]); Wed, 09 May 2018 04:17:53 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 09 May 2018 04:17: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 v5 2/4] 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. Signed-off-by: Peter Xu Reviewed-by: Markus Armbruster Reviewed-by: Stefan Hajnoczi --- 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 Wed Oct 29 11:29:44 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 1525839597444608.9928466504831; Tue, 8 May 2018 21:19:57 -0700 (PDT) Received: from localhost ([::1]:54399 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGGa0-00088A-Ul for importer@patchew.org; Wed, 09 May 2018 00:19:52 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34870) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGGYE-00073e-BE for qemu-devel@nongnu.org; Wed, 09 May 2018 00:18:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fGGYA-0002sJ-BZ for qemu-devel@nongnu.org; Wed, 09 May 2018 00:18:02 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:49838 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 1fGGYA-0002s9-72 for qemu-devel@nongnu.org; Wed, 09 May 2018 00:17:58 -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 D6CFC7CBBA for ; Wed, 9 May 2018 04:17:57 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-74.pek2.redhat.com [10.72.12.74]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2DA9B10F1BEA; Wed, 9 May 2018 04:17:53 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 9 May 2018 12:17:33 +0800 Message-Id: <20180509041734.14135-4-peterx@redhat.com> In-Reply-To: <20180509041734.14135-1-peterx@redhat.com> References: <20180509041734.14135-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.2]); Wed, 09 May 2018 04:17:57 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Wed, 09 May 2018 04:17:57 +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 v5 3/4] monitor: more comments on lock-free fleids/funcs 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 comment 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: Stefan Hajnoczi --- monitor.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/monitor.c b/monitor.c index d6c3c08932..ae5bca9d7c 100644 --- a/monitor.c +++ b/monitor.c @@ -207,7 +207,7 @@ struct Monitor { int suspend_cnt; /* Needs to be accessed atomically */ bool skip_flush; bool use_io_thr; - ReadLineState *rs; + ReadLineState *rs; /* Only used in parser, so no lock needed. */ MonitorQMP qmp; gchar *mon_cpu_path; BlockCompletionFunc *password_completion_cb; @@ -1313,7 +1313,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. BQL needed. */ int monitor_set_cpu(int cpu_index) { CPUState *cpu; @@ -1327,6 +1327,7 @@ int monitor_set_cpu(int cpu_index) return 0; } =20 +/* BQL neeeded. */ static CPUState *mon_get_cpu_sync(bool synchronize) { CPUState *cpu; --=20 2.17.0 From nobody Wed Oct 29 11:29:44 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 1525839778300984.737544390607; Tue, 8 May 2018 21:22:58 -0700 (PDT) Received: from localhost ([::1]:54416 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGGcz-0002Bk-EX for importer@patchew.org; Wed, 09 May 2018 00:22:57 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34881) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGGYK-000768-NH for qemu-devel@nongnu.org; Wed, 09 May 2018 00:18:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fGGYJ-00035v-Jy for qemu-devel@nongnu.org; Wed, 09 May 2018 00:18:08 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:43642 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 1fGGYJ-00035E-Dq for qemu-devel@nongnu.org; Wed, 09 May 2018 00:18:07 -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 1B80B406802F for ; Wed, 9 May 2018 04:18:07 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-74.pek2.redhat.com [10.72.12.74]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6DC6410F1BEA; Wed, 9 May 2018 04:17:58 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 9 May 2018 12:17:34 +0800 Message-Id: <20180509041734.14135-5-peterx@redhat.com> In-Reply-To: <20180509041734.14135-1-peterx@redhat.com> References: <20180509041734.14135-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.5]); Wed, 09 May 2018 04:18:07 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Wed, 09 May 2018 04:18:07 +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 v5 4/4] 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" Similar to previous patch, but introduce a new global big lock for mon_fdsets. Take it where needed. Reviewed-by: Stefan Hajnoczi Signed-off-by: Peter Xu --- monitor.c | 64 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/monitor.c b/monitor.c index ae5bca9d7c..d72b695156 100644 --- a/monitor.c +++ b/monitor.c @@ -262,6 +262,9 @@ typedef struct QMPRequest QMPRequest; /* Protects mon_list, monitor_event_state. */ static QemuMutex monitor_lock; =20 +/* Protects mon_fdsets */ +static QemuMutex mon_fdsets_lock; + static QTAILQ_HEAD(mon_list, Monitor) mon_list; static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets; static int mon_refcount; @@ -278,6 +281,16 @@ static QEMUClockType event_clock_type =3D QEMU_CLOCK_R= EALTIME; static void monitor_command_cb(void *opaque, const char *cmdline, void *readline_opaque); =20 +/* + * This lock can be used very early, even during param parsing. + * Meanwhile it can also be used even at the end of main. Let's keep + * it initialized for the whole lifecycle of QEMU. + */ +static void __attribute__((constructor)) mon_fdsets_lock_init(void) +{ + qemu_mutex_init(&mon_fdsets_lock); +} + /** * Is @mon a QMP monitor? */ @@ -2307,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, @@ -2344,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; @@ -2363,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); @@ -2382,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; @@ -2411,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; } @@ -2423,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 */ @@ -2443,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 */ @@ -2493,6 +2515,7 @@ AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdse= t_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 @@ -2502,7 +2525,9 @@ int monitor_fdset_get_fd(int64_t fdset_id, int flags) MonFdset *mon_fdset; MonFdsetFd *mon_fdset_fd; int mon_fd_flags; + int ret =3D -1; =20 + qemu_mutex_lock(&mon_fdsets_lock); QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { if (mon_fdset->id !=3D fdset_id) { continue; @@ -2510,49 +2535,62 @@ 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; + 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; + break; } -#endif - +out: + qemu_mutex_unlock(&mon_fdsets_lock); + return ret; +#else errno =3D ENOENT; return -1; +#endif } =20 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd) { MonFdset *mon_fdset; MonFdsetFd *mon_fdset_fd_dup; + int ret =3D -1; =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; + ret =3D -1; + goto out; } } 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); - return 0; + ret =3D 0; + break; } - return -1; + +out: + qemu_mutex_unlock(&mon_fdsets_lock); + return ret; } =20 static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove) { MonFdset *mon_fdset; MonFdsetFd *mon_fdset_fd_dup; + int ret =3D -1; =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) { @@ -2561,14 +2599,18 @@ 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; + ret =3D -1; + goto out; } else { - return mon_fdset->id; + ret =3D mon_fdset->id; + goto out; } } } } - return -1; +out: + qemu_mutex_unlock(&mon_fdsets_lock); + return ret; } =20 int monitor_fdset_dup_fd_find(int dup_fd) --=20 2.17.0