From nobody Thu Nov 6 22:56:54 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 1543929789805932.1997772256423; Tue, 4 Dec 2018 05:23:09 -0800 (PST) Received: from localhost ([::1]:56709 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gUAfM-0001ww-LM for importer@patchew.org; Tue, 04 Dec 2018 08:23:08 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54690) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gUAbd-0005k1-9f for qemu-devel@nongnu.org; Tue, 04 Dec 2018 08:19:18 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gUAbZ-0002xz-W5 for qemu-devel@nongnu.org; Tue, 04 Dec 2018 08:19:17 -0500 Received: from mx1.redhat.com ([209.132.183.28]:8752) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gUAbZ-0002wW-Mq for qemu-devel@nongnu.org; Tue, 04 Dec 2018 08:19:13 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 14B697F6A7 for ; Tue, 4 Dec 2018 13:19:13 +0000 (UTC) Received: from localhost (ovpn-112-31.ams2.redhat.com [10.36.112.31]) by smtp.corp.redhat.com (Postfix) with ESMTP id EB9925C25C; Tue, 4 Dec 2018 13:19:06 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Tue, 4 Dec 2018 17:18:02 +0400 Message-Id: <20181204131802.16885-7-marcandre.lureau@redhat.com> In-Reply-To: <20181204131802.16885-1-marcandre.lureau@redhat.com> References: <20181204131802.16885-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 04 Dec 2018 13:19:13 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 6/6] monitor: avoid potential dead-lock when cleaning up 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: Paolo Bonzini , "Dr. David Alan Gilbert" , Markus Armbruster , peterx@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" When a monitor is connected to a Spice chardev, the monitor cleanup can dead-lock: #0 0x00007f43446637fd in __lll_lock_wait () at /lib64/libpthread.so.0 #1 0x00007f434465ccf4 in pthread_mutex_lock () at /lib64/libpthread.so.0 #2 0x0000556dd79f22ba in qemu_mutex_lock_impl (mutex=3D0x556dd81c9220 , file=3D0x556dd7ae3648 "/home/elmarco/src/qq/monitor.c", line= =3D645) at /home/elmarco/src/qq/util/qemu-thread-posix.c:66 #3 0x0000556dd7431bd5 in monitor_qapi_event_queue (event=3DQAPI_EVENT_SPI= CE_DISCONNECTED, qdict=3D0x556dd9abc850, errp=3D0x7fffb7bbddd8) at /home/el= marco/src/qq/monitor.c:645 #4 0x0000556dd79d476b in qapi_event_send_spice_disconnected (server=3D0x5= 56dd98ee760, client=3D0x556ddaaa8560, errp=3D0x556dd82180d0 ) = at qapi/qapi-events-ui.c:149 #5 0x0000556dd7870fc1 in channel_event (event=3D3, info=3D0x556ddad1b590)= at /home/elmarco/src/qq/ui/spice-core.c:235 #6 0x00007f434560a6bb in reds_handle_channel_event (reds=3D, event=3D3, info=3D0x556ddad1b590) at reds.c:316 #7 0x00007f43455f393b in main_dispatcher_self_handle_channel_event (info= =3D0x556ddad1b590, event=3D3, self=3D0x556dd9a7d8c0) at main-dispatcher.c:1= 97 #8 0x00007f43455f393b in main_dispatcher_channel_event (self=3D0x556dd9a7= d8c0, event=3Devent@entry=3D3, info=3D0x556ddad1b590) at main-dispatcher.c:= 197 #9 0x00007f4345612833 in red_stream_push_channel_event (s=3Ds@entry=3D0x5= 56ddae2ef40, event=3Devent@entry=3D3) at red-stream.c:414 #10 0x00007f434561286b in red_stream_free (s=3D0x556ddae2ef40) at red-stre= am.c:388 #11 0x00007f43455f9ddc in red_channel_client_finalize (object=3D0x556dd9bb= 21a0) at red-channel-client.c:347 #12 0x00007f434b5f9fb9 in g_object_unref () at /lib64/libgobject-2.0.so.0 #13 0x00007f43455fc212 in red_channel_client_push (rcc=3D0x556dd9bb21a0) a= t red-channel-client.c:1341 #14 0x0000556dd76081ba in spice_port_set_fe_open (chr=3D0x556dd9925e20, fe= _open=3D0) at /home/elmarco/src/qq/chardev/spice.c:241 #15 0x0000556dd796d74a in qemu_chr_fe_set_open (be=3D0x556dd9a37c00, fe_op= en=3D0) at /home/elmarco/src/qq/chardev/char-fe.c:340 #16 0x0000556dd796d4d9 in qemu_chr_fe_set_handlers (b=3D0x556dd9a37c00, fd= _can_read=3D0x0, fd_read=3D0x0, fd_event=3D0x0, be_change=3D0x0, opaque=3D0= x0, context=3D0x0, set_open=3Dtrue) at /home/elmarco/src/qq/chardev/char-fe= .c:280 #17 0x0000556dd796d359 in qemu_chr_fe_deinit (b=3D0x556dd9a37c00, del=3Dfa= lse) at /home/elmarco/src/qq/chardev/char-fe.c:233 #18 0x0000556dd7432240 in monitor_data_destroy (mon=3D0x556dd9a37c00) at /= home/elmarco/src/qq/monitor.c:786 #19 0x0000556dd743b968 in monitor_cleanup () at /home/elmarco/src/qq/monit= or.c:4683 #20 0x0000556dd75ce776 in main (argc=3D3, argv=3D0x7fffb7bbe458, envp=3D0x= 7fffb7bbe478) at /home/elmarco/src/qq/vl.c:4660 Because spice code tries to emit a "disconnected" signal on the monitors. Fix this dead-lock by releasing the monitor lock for flush/destroy. monitor_lock protects mon_list, monitor_qapi_event_state and monitor_destroyed. monitor_flush() and monitor_data_destroy() don't access any of those variables. monitor_cleanup()'s loop is safe because it uses QTAILQ_FOREACH_SAFE(), and no further monitor can be added after calling monitor_cleanup() thanks to monitor_destroyed check in monitor_list_append(). Signed-off-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Markus Armbruster --- monitor.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/monitor.c b/monitor.c index f0256bdec5..936c040b2d 100644 --- a/monitor.c +++ b/monitor.c @@ -4652,8 +4652,11 @@ void monitor_cleanup(void) monitor_destroyed =3D true; QTAILQ_FOREACH_SAFE(mon, &mon_list, entry, next) { QTAILQ_REMOVE(&mon_list, mon, entry); + /* Permit QAPI event emission from character frontend release */ + qemu_mutex_unlock(&monitor_lock); monitor_flush(mon); monitor_data_destroy(mon); + qemu_mutex_lock(&monitor_lock); g_free(mon); } qemu_mutex_unlock(&monitor_lock); --=20 2.20.0.rc1