From nobody Sun Nov 9 11:35:40 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1550836914277508.1809428992931; Fri, 22 Feb 2019 04:01:54 -0800 (PST) Received: from localhost ([127.0.0.1]:49225 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gx9WU-0005g8-Nk for importer@patchew.org; Fri, 22 Feb 2019 07:01:46 -0500 Received: from eggs.gnu.org ([209.51.188.92]:52323) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gx9Un-0004uS-Qo for qemu-devel@nongnu.org; Fri, 22 Feb 2019 07:00:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gx9Um-000472-Qm for qemu-devel@nongnu.org; Fri, 22 Feb 2019 07:00:01 -0500 Received: from fanzine.igalia.com ([91.117.99.155]:39613) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gx9Um-0003ph-74 for qemu-devel@nongnu.org; Fri, 22 Feb 2019 07:00:00 -0500 Received: from 62-165-147-230.co.dnainternet.fi ([62.165.147.230] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1gx9UZ-0005RL-JY; Fri, 22 Feb 2019 12:59:47 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1gx9UL-0002kN-QB; Fri, 22 Feb 2019 13:59:33 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=o6fOduiHi6Wo+QbByeVCTahnlzURTUwEv4ZdqgfqpX8=; b=bqRGYr1txrnf1u0gMqdjwL9tWpQF9tGHwnRprh9sGoWRn88Q4Mffa+wwbFqXirY4vi6C6jd8F7vylm7bR3kzni9Sz8TPakn4n6yNRCIegn2ZnxW69/EiaShb7LHl4fWHyxa+Br7jDyTSl95I+b5i3SaWG10kHQgAYdfrklxIbiwi6ZnQC5CZaR8gkMAfXwRRdxE+77oqNasdD8vUVICG136iFInGCg1aOYRiwAqxauAxghK6wZ0H6Pyt8MFiJUcEIBoWTJUgOegXZ2xXiPXEI+n/kFDHRaViHq3KT6n8BHSVAP557c0BRGF0iSTlzdg8ijwEYPvr1Kndl+Oe+OGksg==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Fri, 22 Feb 2019 13:59:10 +0200 Message-Id: <0df0773f716140fec012d6778220b8a07067a5c6.1550836631.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v2 1/3] main-loop: Fix GSource leak in qio_task_thread_worker() 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 , Alberto Garcia , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" After g_source_attach() the GMainContext holds a reference to the GSource, so the caller does not need to keep it. qio_task_thread_worker() is not releasing its reference so the GSource is being leaked since a17536c594bfed94d05667b419f747b692f5fc7f. Signed-off-by: Alberto Garcia Reviewed-by: Daniel P. Berrang=C3=A9 --- io/task.c | 1 + 1 file changed, 1 insertion(+) diff --git a/io/task.c b/io/task.c index 64c4c7126a..1ae7b86488 100644 --- a/io/task.c +++ b/io/task.c @@ -136,6 +136,7 @@ static gpointer qio_task_thread_worker(gpointer opaque) qio_task_thread_result, task, NULL); g_source_attach(task->thread->completion, task->thread->context); + g_source_unref(task->thread->completion); trace_qio_task_thread_source_attach(task, task->thread->completion); =20 qemu_cond_signal(&task->thread_cond); --=20 2.11.0 From nobody Sun Nov 9 11:35:40 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1550836911566207.3445266040211; Fri, 22 Feb 2019 04:01:51 -0800 (PST) Received: from localhost ([127.0.0.1]:49227 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gx9WW-0005hk-9O for importer@patchew.org; Fri, 22 Feb 2019 07:01:48 -0500 Received: from eggs.gnu.org ([209.51.188.92]:52344) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gx9Uo-0004uT-6N for qemu-devel@nongnu.org; Fri, 22 Feb 2019 07:00:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gx9Um-00047E-Sl for qemu-devel@nongnu.org; Fri, 22 Feb 2019 07:00:02 -0500 Received: from fanzine.igalia.com ([91.117.99.155]:39612) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gx9Um-0003pf-88 for qemu-devel@nongnu.org; Fri, 22 Feb 2019 07:00:00 -0500 Received: from 62-165-147-230.co.dnainternet.fi ([62.165.147.230] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1gx9UZ-0005RI-I2; Fri, 22 Feb 2019 12:59:47 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1gx9UL-0002kP-RE; Fri, 22 Feb 2019 13:59:33 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=GFFG7FSm/2MdWLLLybJbUJ9jy/JF6cqQGQ+oPZduELY=; b=g+zisfW0wQF/fooM30cOZU4WflMtBQPLcW3fBcjpiBwBs7x3ppEL9K9DIznZBqzURIqfVFnDkzkIn6RGCZwvtLfxRJiBUl83dPvnmh4JtsSqR2VmUmQp9dT1UusffPUSh7ml0GojT457rgoI0q6xnUeOl75x0jfdosPdUg0PuPvVNEjIdJhkiAYqw3IOBYpboNQPS27zdI17bhTr/WmgP75pgZeDkRh9sn1huPuOkV7aXZEMbV77S0U0rHMf381lRAx+1M852Z44G1lDChmp0CSXCZ7hTDDpSBL+rnuLCylPyO8s0wFi2SVVZ1XoNNBBUBpVHB5W2dbMebPSOWQTOQ==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Fri, 22 Feb 2019 13:59:11 +0200 Message-Id: X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v2 2/3] main-loop: Add qemu_idle_add() 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 , Alberto Garcia , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This works like g_idle_add() but allows specifying a different GMainContext. It also returns the GSource directly instead of its ID number for convenience. qio_task_thread_worker() is modified to make use of this new function. Signed-off-by: Alberto Garcia Reviewed-by: Daniel P. Berrang=C3=A9 --- include/qemu/main-loop.h | 12 ++++++++++++ io/task.c | 10 +++------- util/main-loop.c | 9 +++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/include/qemu/main-loop.h b/include/qemu/main-loop.h index f6ba78ea73..f966c015d0 100644 --- a/include/qemu/main-loop.h +++ b/include/qemu/main-loop.h @@ -295,6 +295,18 @@ void qemu_mutex_lock_iothread_impl(const char *file, i= nt line); */ void qemu_mutex_unlock_iothread(void); =20 +/** + * qemu_idle_add: Add an idle function to a GMainContext + * + * This function is similar to GLib's g_idle_add() but it allows + * specifying a GMainContext instead of using the global one. + * + * The returned GSource is owned by the GMainContext and is deleted + * automatically after @func returns false. It can also be deleted by + * removing it from the GMainContext using g_source_destroy(). + */ +GSource *qemu_idle_add(GSourceFunc func, gpointer data, GMainContext *ctx); + /* internal interfaces */ =20 void qemu_fd_register(int fd); diff --git a/io/task.c b/io/task.c index 1ae7b86488..16754e22ba 100644 --- a/io/task.c +++ b/io/task.c @@ -19,6 +19,7 @@ */ =20 #include "qemu/osdep.h" +#include "qemu/main-loop.h" #include "io/task.h" #include "qapi/error.h" #include "qemu/thread.h" @@ -130,13 +131,8 @@ static gpointer qio_task_thread_worker(gpointer opaque) trace_qio_task_thread_exit(task); =20 qemu_mutex_lock(&task->thread_lock); - - task->thread->completion =3D g_idle_source_new(); - g_source_set_callback(task->thread->completion, - qio_task_thread_result, task, NULL); - g_source_attach(task->thread->completion, - task->thread->context); - g_source_unref(task->thread->completion); + task->thread->completion =3D qemu_idle_add(qio_task_thread_result, tas= k, + task->thread->context); trace_qio_task_thread_source_attach(task, task->thread->completion); =20 qemu_cond_signal(&task->thread_cond); diff --git a/util/main-loop.c b/util/main-loop.c index d4a521caeb..e23eda68cc 100644 --- a/util/main-loop.c +++ b/util/main-loop.c @@ -171,6 +171,15 @@ int qemu_init_main_loop(Error **errp) return 0; } =20 +GSource *qemu_idle_add(GSourceFunc func, gpointer data, GMainContext *ctx) +{ + GSource *idle =3D g_idle_source_new(); + g_source_set_callback(idle, func, data, NULL); + g_source_attach(idle, ctx); + g_source_unref(idle); + return idle; +} + static int max_priority; =20 #ifndef _WIN32 --=20 2.11.0 From nobody Sun Nov 9 11:35:40 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1550837110092818.6690315511136; Fri, 22 Feb 2019 04:05:10 -0800 (PST) Received: from localhost ([127.0.0.1]:49249 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gx9Zj-0007uS-48 for importer@patchew.org; Fri, 22 Feb 2019 07:05:07 -0500 Received: from eggs.gnu.org ([209.51.188.92]:52470) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gx9Us-0004wr-BA for qemu-devel@nongnu.org; Fri, 22 Feb 2019 07:00:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gx9Uo-0004CJ-OU for qemu-devel@nongnu.org; Fri, 22 Feb 2019 07:00:06 -0500 Received: from fanzine.igalia.com ([91.117.99.155]:39614) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gx9Uo-0003pg-8q for qemu-devel@nongnu.org; Fri, 22 Feb 2019 07:00:02 -0500 Received: from 62-165-147-230.co.dnainternet.fi ([62.165.147.230] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1gx9UZ-0005RK-HL; Fri, 22 Feb 2019 12:59:47 +0100 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1gx9UL-0002kR-SG; Fri, 22 Feb 2019 13:59:33 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=jUqLWIcQLV0VUti+8dAMGW9lzlwIezLTU66Dy5OT0AU=; b=A2JWkdeAW0Kv8wCDNOtPwR9PpZyFe6vHvbDZkfg5/ePOhMYe3BeWtlqDDjU8GPHhD+wQtsaRyKtvc7tHjbOq5piyzhmy/+sTg4WYLC6QYKnyGTQk6UbYpw9WwWG6ekjlJJE6FkW5meLFBqJZuYN/hqfEffznT9SVtb5X6UHW7hU2owuSM1Nftjzf6s7vNGrFe3h058UGaXT8iJn+jBsU7n6B/hopGfo8ExLh/oBlu1kLoIOut+aeJ9OrxTiQM09p9hOmCWCe1PCIpukT8Ct9DVzTJ/YooVCus6S94DluHYQYGQz99D6joVyR1BWXMAp3KfRyWo0EGzq14g6Cg96lKQ==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Fri, 22 Feb 2019 13:59:12 +0200 Message-Id: X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v2 3/3] char-socket: Lock tcp_chr_disconnect() and socket_reconnect_timeout() 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 , Alberto Garcia , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" There's a race condition in which the tcp_chr_read() ioc handler can close a connection that is being written to from another thread. Running iotest 136 in a loop triggers this problem and crashes QEMU. (gdb) bt #0 0x00005558b842902d in object_get_class (obj=3D0x0) at qom/object.c:860 #1 0x00005558b84f92db in qio_channel_writev_full (ioc=3D0x0, iov=3D0x7ffc= 355decf0, niov=3D1, fds=3D0x0, nfds=3D0, errp=3D0x0) at io/channel.c:76 #2 0x00005558b84e0e9e in io_channel_send_full (ioc=3D0x0, buf=3D0x5558baf= 5beb0, len=3D138, fds=3D0x0, nfds=3D0) at chardev/char-io.c:123 #3 0x00005558b84e4a69 in tcp_chr_write (chr=3D0x5558ba460380, buf=3D0x555= 8baf5beb0 "...", len=3D138) at chardev/char-socket.c:135 #4 0x00005558b84dca55 in qemu_chr_write_buffer (s=3D0x5558ba460380, buf= =3D0x5558baf5beb0 "...", len=3D138, offset=3D0x7ffc355dedd0, write_all=3Dfa= lse) at chardev/char.c:112 #5 0x00005558b84dcbc2 in qemu_chr_write (s=3D0x5558ba460380, buf=3D0x5558= baf5beb0 "...", len=3D138, write_all=3Dfalse) at chardev/char.c:147 #6 0x00005558b84dfb26 in qemu_chr_fe_write (be=3D0x5558ba476610, buf=3D0x= 5558baf5beb0 "...", len=3D138) at chardev/char-fe.c:42 #7 0x00005558b8088c86 in monitor_flush_locked (mon=3D0x5558ba476610) at m= onitor.c:406 #8 0x00005558b8088e8c in monitor_puts (mon=3D0x5558ba476610, str=3D0x5558= ba921e49 "") at monitor.c:449 #9 0x00005558b8089178 in qmp_send_response (mon=3D0x5558ba476610, rsp=3D0= x5558bb161600) at monitor.c:498 #10 0x00005558b808920c in monitor_qapi_event_emit (event=3DQAPI_EVENT_SHUT= DOWN, qdict=3D0x5558bb161600) at monitor.c:526 #11 0x00005558b8089307 in monitor_qapi_event_queue_no_reenter (event=3DQAP= I_EVENT_SHUTDOWN, qdict=3D0x5558bb161600) at monitor.c:551 #12 0x00005558b80896c0 in qapi_event_emit (event=3DQAPI_EVENT_SHUTDOWN, qd= ict=3D0x5558bb161600) at monitor.c:626 #13 0x00005558b855f23b in qapi_event_send_shutdown (guest=3Dfalse, reason= =3DSHUTDOWN_CAUSE_HOST_QMP_QUIT) at qapi/qapi-events-run-state.c:43 #14 0x00005558b81911ef in qemu_system_shutdown (cause=3DSHUTDOWN_CAUSE_HOS= T_QMP_QUIT) at vl.c:1837 #15 0x00005558b8191308 in main_loop_should_exit () at vl.c:1885 #16 0x00005558b819140d in main_loop () at vl.c:1924 #17 0x00005558b8198c84 in main (argc=3D18, argv=3D0x7ffc355df3f8, envp=3D0= x7ffc355df490) at vl.c:4665 This patch adds a lock to protect tcp_chr_disconnect() and socket_reconnect_timeout() Signed-off-by: Alberto Garcia --- chardev/char-socket.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 4fcdd8aedd..67c2eeac6d 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -172,7 +172,9 @@ static int tcp_chr_write(Chardev *chr, const uint8_t *b= uf, int len) =20 if (ret < 0 && errno !=3D EAGAIN) { if (tcp_chr_read_poll(chr) <=3D 0) { + qemu_mutex_unlock(&chr->chr_write_lock); tcp_chr_disconnect(chr); + qemu_mutex_lock(&chr->chr_write_lock); return len; } /* else let the read handler finish it properly */ } @@ -464,6 +466,13 @@ static void update_disconnected_filename(SocketChardev= *s) } } =20 +static gboolean tcp_chr_be_event_closed(gpointer opaque) +{ + Chardev *chr =3D opaque; + qemu_chr_be_event(chr, CHR_EVENT_CLOSED); + return FALSE; +} + /* NB may be called even if tcp_chr_connect has not been * reached, due to TLS or telnet initialization failure, * so can *not* assume s->state =3D=3D TCP_CHARDEV_STATE_CONNECTED @@ -471,7 +480,10 @@ static void update_disconnected_filename(SocketChardev= *s) static void tcp_chr_disconnect(Chardev *chr) { SocketChardev *s =3D SOCKET_CHARDEV(chr); - bool emit_close =3D s->state =3D=3D TCP_CHARDEV_STATE_CONNECTED; + bool emit_close; + + qemu_mutex_lock(&chr->chr_write_lock); + emit_close =3D s->state =3D=3D TCP_CHARDEV_STATE_CONNECTED; =20 tcp_chr_free_connection(chr); =20 @@ -481,11 +493,12 @@ static void tcp_chr_disconnect(Chardev *chr) } update_disconnected_filename(s); if (emit_close) { - qemu_chr_be_event(chr, CHR_EVENT_CLOSED); + qemu_idle_add(tcp_chr_be_event_closed, chr, chr->gcontext); } if (s->reconnect_time) { qemu_chr_socket_restart_timer(chr); } + qemu_mutex_unlock(&chr->chr_write_lock); } =20 static gboolean tcp_chr_read(QIOChannel *chan, GIOCondition cond, void *op= aque) @@ -1128,8 +1141,10 @@ static gboolean socket_reconnect_timeout(gpointer op= aque) Chardev *chr =3D CHARDEV(opaque); SocketChardev *s =3D SOCKET_CHARDEV(opaque); =20 + qemu_mutex_lock(&chr->chr_write_lock); g_source_unref(s->reconnect_timer); s->reconnect_timer =3D NULL; + qemu_mutex_unlock(&chr->chr_write_lock); =20 if (chr->be_open) { return false; --=20 2.11.0