From nobody Sat Apr 27 23:38:15 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 1524042571176300.6519923219304; Wed, 18 Apr 2018 02:09:31 -0700 (PDT) Received: from localhost ([::1]:42526 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f8j5l-0005IH-FO for importer@patchew.org; Wed, 18 Apr 2018 05:09:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56335) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f8izQ-0000Qb-DP for qemu-devel@nongnu.org; Wed, 18 Apr 2018 05:03:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f8izK-0005sE-3z for qemu-devel@nongnu.org; Wed, 18 Apr 2018 05:02:56 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:53314 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 1f8izJ-0005rG-WA for qemu-devel@nongnu.org; Wed, 18 Apr 2018 05:02:50 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 93B948DC2E for ; Wed, 18 Apr 2018 09:02:47 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-25.pek2.redhat.com [10.72.12.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id AB2DF215CDC8; Wed, 18 Apr 2018 09:02:44 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 18 Apr 2018 17:02:37 +0800 Message-Id: <20180418090239.13090-2-peterx@redhat.com> In-Reply-To: <20180418090239.13090-1-peterx@redhat.com> References: <20180418090239.13090-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Wed, 18 Apr 2018 09:02:47 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Wed, 18 Apr 2018 09:02:47 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.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 v2 1/3] 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 out buffers. 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. We split this lock until necessary. So far I don't see a reason to complicate lock usage for monitors. Since at it, arrange the Monitor struct a bit. Signed-off-by: Peter Xu --- monitor.c | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/monitor.c b/monitor.c index 39f8ee17ba..c93aa4e22b 100644 --- a/monitor.c +++ b/monitor.c @@ -202,20 +202,17 @@ typedef struct { =20 struct Monitor { CharBackend chr; + /* We can't access guest memory when holding the lock */ + QemuMutex mon_lock; int reset_seen; int flags; 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. */ + /* Read under either BQL or mon_lock, written with BQL+mon_lock. */ int mux_out; - ReadLineState *rs; MonitorQMP qmp; gchar *mon_cpu_path; @@ -366,14 +363,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 +408,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 +418,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 +431,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) @@ -728,7 +725,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. */ @@ -748,7 +745,7 @@ static void monitor_data_destroy(Monitor *mon) } readline_free(mon->rs); QDECREF(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); @@ -780,13 +777,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); @@ -4383,9 +4380,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); @@ -4405,9 +4402,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.14.3 From nobody Sat Apr 27 23:38:15 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 1524042494130370.4154395685505; Wed, 18 Apr 2018 02:08:14 -0700 (PDT) Received: from localhost ([::1]:42460 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f8j4V-0004Cw-8e for importer@patchew.org; Wed, 18 Apr 2018 05:08:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56339) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f8izQ-0000Qh-Fs for qemu-devel@nongnu.org; Wed, 18 Apr 2018 05:03:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f8izL-0005u9-QN for qemu-devel@nongnu.org; Wed, 18 Apr 2018 05:02:56 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47260 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 1f8izL-0005tm-M5 for qemu-devel@nongnu.org; Wed, 18 Apr 2018 05:02:51 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3815F4022909 for ; Wed, 18 Apr 2018 09:02:51 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-25.pek2.redhat.com [10.72.12.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3A77521568B2; Wed, 18 Apr 2018 09:02:47 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 18 Apr 2018 17:02:38 +0800 Message-Id: <20180418090239.13090-3-peterx@redhat.com> In-Reply-To: <20180418090239.13090-1-peterx@redhat.com> References: <20180418090239.13090-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 18 Apr 2018 09:02:51 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 18 Apr 2018 09:02:51 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.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 v2 2/3] monitor: take mon_lock where proper 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" Went through all the montior.h APIs to make sure existing Monitor related APIs will always take the new monitor lock when necessary. Signed-off-by: Peter Xu --- monitor.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/monitor.c b/monitor.c index c93aa4e22b..f4951cafbc 100644 --- a/monitor.c +++ b/monitor.c @@ -306,16 +306,20 @@ void monitor_read_command(Monitor *mon, int show_prom= pt) if (!mon->rs) return; =20 + qemu_mutex_lock(&mon->mon_lock); readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL); if (show_prompt) readline_show_prompt(mon->rs); + qemu_mutex_unlock(&mon->mon_lock); } =20 int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func, void *opaque) { if (mon->rs) { + qemu_mutex_lock(&mon->mon_lock); readline_start(mon->rs, "Password: ", 1, readline_func, opaque); + qemu_mutex_unlock(&mon->mon_lock); /* prompt is printed on return from the command handler */ return 0; } else { @@ -1308,8 +1312,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 */ -int monitor_set_cpu(int cpu_index) +static int monitor_set_cpu_locked(Monitor *mon, int cpu_index) { CPUState *cpu; =20 @@ -1317,15 +1320,28 @@ int monitor_set_cpu(int cpu_index) if (cpu =3D=3D NULL) { return -1; } - g_free(cur_mon->mon_cpu_path); - cur_mon->mon_cpu_path =3D object_get_canonical_path(OBJECT(cpu)); + g_free(mon->mon_cpu_path); + mon->mon_cpu_path =3D object_get_canonical_path(OBJECT(cpu)); return 0; } =20 +/* set the current CPU defined by the user */ +int monitor_set_cpu(int cpu_index) +{ + int ret; + + qemu_mutex_lock(&cur_mon->mon_lock); + ret =3D monitor_set_cpu_locked(cur_mon, cpu_index); + qemu_mutex_unlock(&cur_mon->mon_lock); + + return ret; +} + static CPUState *mon_get_cpu_sync(bool synchronize) { CPUState *cpu; =20 + qemu_mutex_lock(&cur_mon->mon_lock); if (cur_mon->mon_cpu_path) { cpu =3D (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_pat= h, TYPE_CPU, NULL); @@ -1336,11 +1352,14 @@ static CPUState *mon_get_cpu_sync(bool synchronize) } if (!cur_mon->mon_cpu_path) { if (!first_cpu) { + qemu_mutex_unlock(&cur_mon->mon_lock); return NULL; } - monitor_set_cpu(first_cpu->cpu_index); + monitor_set_cpu_locked(cur_mon, first_cpu->cpu_index); cpu =3D first_cpu; } + qemu_mutex_unlock(&cur_mon->mon_lock); + if (synchronize) { cpu_synchronize_state(cpu); } @@ -2239,6 +2258,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 @@ -2252,9 +2272,10 @@ 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); return fd; } + qemu_mutex_unlock(&mon->mon_lock); =20 error_setg(errp, "File descriptor named '%s' has not been found", fdna= me); return -1; --=20 2.14.3 From nobody Sat Apr 27 23:38:15 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 1524042348219633.7611331766973; Wed, 18 Apr 2018 02:05:48 -0700 (PDT) Received: from localhost ([::1]:42313 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f8j2B-00024h-Df for importer@patchew.org; Wed, 18 Apr 2018 05:05:47 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56344) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f8izQ-0000Qn-LI for qemu-devel@nongnu.org; Wed, 18 Apr 2018 05:03:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f8izP-0005xb-DA for qemu-devel@nongnu.org; Wed, 18 Apr 2018 05:02:56 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:53318 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 1f8izP-0005xM-7X for qemu-devel@nongnu.org; Wed, 18 Apr 2018 05:02:55 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C4D0A8DC4A for ; Wed, 18 Apr 2018 09:02:54 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-25.pek2.redhat.com [10.72.12.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id CF306215CDC8; Wed, 18 Apr 2018 09:02:51 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 18 Apr 2018 17:02:39 +0800 Message-Id: <20180418090239.13090-4-peterx@redhat.com> In-Reply-To: <20180418090239.13090-1-peterx@redhat.com> References: <20180418090239.13090-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Wed, 18 Apr 2018 09:02:54 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Wed, 18 Apr 2018 09:02:54 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.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 v2 3/3] 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. Signed-off-by: Peter Xu --- monitor.c | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/monitor.c b/monitor.c index f4951cafbc..40b5b56f66 100644 --- a/monitor.c +++ b/monitor.c @@ -254,6 +254,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; @@ -270,6 +273,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? */ @@ -2308,9 +2321,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, @@ -2345,6 +2360,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; @@ -2364,10 +2380,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); @@ -2383,6 +2401,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; @@ -2412,6 +2431,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; } @@ -2424,6 +2444,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 */ @@ -2444,6 +2465,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 */ @@ -2494,6 +2516,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 @@ -2531,29 +2554,38 @@ 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) { @@ -2562,14 +2594,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.14.3