From nobody Wed Oct 22 04:13:48 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 Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519794545332411.58158398788896; Tue, 27 Feb 2018 21:09:05 -0800 (PST) Received: from localhost ([::1]:42235 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtz3-0000Xb-7r for importer@patchew.org; Wed, 28 Feb 2018 00:08:53 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33510) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtx7-0007uv-Eb for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:06:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqtx4-0000bs-Aj for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:06:53 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:49506 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 1eqtx4-0000bU-6T for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:06:50 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0EDD4402291E for ; Wed, 28 Feb 2018 05:06:47 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-95.pek2.redhat.com [10.72.12.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id C1EC42024CA6; Wed, 28 Feb 2018 05:06:43 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 28 Feb 2018 13:06:20 +0800 Message-Id: <20180228050633.7410-2-peterx@redhat.com> In-Reply-To: <20180228050633.7410-1-peterx@redhat.com> References: <20180228050633.7410-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 28 Feb 2018 05:06:47 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 28 Feb 2018 05:06:47 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 01/14] chardev: fix leak in tcp_chr_telnet_init_io() 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: Juan Quintela , Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini 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" Need to free TCPChardevTelnetInit when session established. Since at it, switch to use G_SOURCE_* macros. Signed-off-by: Peter Xu Reviewed-by: Daniel P. Berrang=C3=A9 --- chardev/char-socket.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index bdd6cff5f6..43a2cc2c1c 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -591,19 +591,23 @@ static gboolean tcp_chr_telnet_init_io(QIOChannel *io= c, ret =3D 0; } else { tcp_chr_disconnect(init->chr); - return FALSE; + goto end; } } init->buflen -=3D ret; =20 if (init->buflen =3D=3D 0) { tcp_chr_connect(init->chr); - return FALSE; + goto end; } =20 memmove(init->buf, init->buf + ret, init->buflen); =20 - return TRUE; + return G_SOURCE_CONTINUE; + +end: + g_free(init); + return G_SOURCE_REMOVE; } =20 static void tcp_chr_telnet_init(Chardev *chr) --=20 2.14.3 From nobody Wed Oct 22 04:13:48 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 Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519794542907210.9613967601714; Tue, 27 Feb 2018 21:09:02 -0800 (PST) Received: from localhost ([::1]:42234 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtz0-0000WH-Co for importer@patchew.org; Wed, 28 Feb 2018 00:08:50 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33511) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtx7-0007uw-Ec for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:06:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqtx5-0000c6-Ci for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:06:53 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:49508 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 1eqtx5-0000c0-8H for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:06:51 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E4881402290A for ; Wed, 28 Feb 2018 05:06:50 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-95.pek2.redhat.com [10.72.12.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 96EE72024CA6; Wed, 28 Feb 2018 05:06:47 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 28 Feb 2018 13:06:21 +0800 Message-Id: <20180228050633.7410-3-peterx@redhat.com> In-Reply-To: <20180228050633.7410-1-peterx@redhat.com> References: <20180228050633.7410-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 28 Feb 2018 05:06:50 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 28 Feb 2018 05:06:50 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 02/14] qio: rename qio_task_thread_result 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: Juan Quintela , Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini 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" It is strange that it was called gio_task_thread_result. Rename it to follow the naming rule of the file. Signed-off-by: Peter Xu Reviewed-by: Daniel P. Berrang=C3=A9 --- io/task.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/io/task.c b/io/task.c index 3ce556017c..1a0a1c7185 100644 --- a/io/task.c +++ b/io/task.c @@ -80,7 +80,7 @@ struct QIOTaskThreadData { }; =20 =20 -static gboolean gio_task_thread_result(gpointer opaque) +static gboolean qio_task_thread_result(gpointer opaque) { struct QIOTaskThreadData *data =3D opaque; =20 @@ -110,7 +110,7 @@ static gpointer qio_task_thread_worker(gpointer opaque) * the worker results */ trace_qio_task_thread_exit(data->task); - g_idle_add(gio_task_thread_result, data); + g_idle_add(qio_task_thread_result, data); return NULL; } =20 --=20 2.14.3 From nobody Wed Oct 22 04:13:48 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519794712221290.1549972072771; Tue, 27 Feb 2018 21:11:52 -0800 (PST) Received: from localhost ([::1]:42253 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1equ1u-00038i-U3 for importer@patchew.org; Wed, 28 Feb 2018 00:11:50 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33538) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtxA-0007vg-A6 for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:06:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqtx9-0000dQ-9H for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:06:56 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47000 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 1eqtx9-0000dE-3Q for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:06:55 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BDAE287ABA for ; Wed, 28 Feb 2018 05:06:54 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-95.pek2.redhat.com [10.72.12.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7B5032024CA6; Wed, 28 Feb 2018 05:06:51 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 28 Feb 2018 13:06:22 +0800 Message-Id: <20180228050633.7410-4-peterx@redhat.com> In-Reply-To: <20180228050633.7410-1-peterx@redhat.com> References: <20180228050633.7410-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 28 Feb 2018 05:06:54 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 28 Feb 2018 05:06:54 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 03/14] qio: introduce qio_channel_add_watch_full() 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: Juan Quintela , Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini 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" It's a more powerful version of qio_channel_add_watch(), which supports non-default gcontext. It's stripped from the old one, then we have g_source_get_id() to fetch the tag ID to keep the old interface. Note that the new API will return a gsource, meanwhile keep a reference of it so that callers need to unref them explicitly. Signed-off-by: Peter Xu --- include/io/channel.h | 31 ++++++++++++++++++++++++++++--- io/channel.c | 24 +++++++++++++++++++----- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/include/io/channel.h b/include/io/channel.h index 3995e243a3..36af5e58ae 100644 --- a/include/io/channel.h +++ b/include/io/channel.h @@ -620,20 +620,45 @@ GSource *qio_channel_create_watch(QIOChannel *ioc, GIOCondition condition); =20 /** - * qio_channel_add_watch: + * qio_channel_add_watch_full: * @ioc: the channel object * @condition: the I/O condition to monitor * @func: callback to invoke when the source becomes ready * @user_data: opaque data to pass to @func * @notify: callback to free @user_data + * @context: gcontext to bind the source to * - * Create a new main loop source that is used to watch + * Create a new source that is used to watch * for the I/O condition @condition. The callback @func * will be registered against the source, to be invoked * when the source becomes ready. The optional @user_data * will be passed to @func when it is invoked. The @notify * callback will be used to free @user_data when the - * watch is deleted + * watch is deleted. The source will be bound to @context if + * provided, or main context if it is NULL. + * + * Note: if a valid source is returned, we need to explicitly unref + * the source to destroy it. + * + * Returns: the source pointer + */ +GSource *qio_channel_add_watch_full(QIOChannel *ioc, + GIOCondition condition, + QIOChannelFunc func, + gpointer user_data, + GDestroyNotify notify, + GMainContext *context); + +/** + * qio_channel_add_watch: + * @ioc: the channel object + * @condition: the I/O condition to monitor + * @func: callback to invoke when the source becomes ready + * @user_data: opaque data to pass to @func + * @notify: callback to free @user_data + * + * Wrapper of qio_channel_add_watch_full(), but it'll only bind the + * source object to default main context. * * The returned source ID can be used with g_source_remove() * to remove and free the source when no longer required. diff --git a/io/channel.c b/io/channel.c index ec4b86de7c..3e734cc9a5 100644 --- a/io/channel.c +++ b/io/channel.c @@ -299,6 +299,22 @@ void qio_channel_set_aio_fd_handler(QIOChannel *ioc, klass->io_set_aio_fd_handler(ioc, ctx, io_read, io_write, opaque); } =20 +GSource *qio_channel_add_watch_full(QIOChannel *ioc, + GIOCondition condition, + QIOChannelFunc func, + gpointer user_data, + GDestroyNotify notify, + GMainContext *context) +{ + GSource *source; + + source =3D qio_channel_create_watch(ioc, condition); + g_source_set_callback(source, (GSourceFunc)func, user_data, notify); + g_source_attach(source, context); + + return source; +} + guint qio_channel_add_watch(QIOChannel *ioc, GIOCondition condition, QIOChannelFunc func, @@ -308,11 +324,9 @@ guint qio_channel_add_watch(QIOChannel *ioc, GSource *source; guint id; =20 - source =3D qio_channel_create_watch(ioc, condition); - - g_source_set_callback(source, (GSourceFunc)func, user_data, notify); - - id =3D g_source_attach(source, NULL); + source =3D qio_channel_add_watch_full(ioc, condition, func, + user_data, notify, NULL); + id =3D g_source_get_id(source); g_source_unref(source); =20 return id; --=20 2.14.3 From nobody Wed Oct 22 04:13:48 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 Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519794872291652.9647345318471; Tue, 27 Feb 2018 21:14:32 -0800 (PST) Received: from localhost ([::1]:42265 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1equ4M-0005Mc-6r for importer@patchew.org; Wed, 28 Feb 2018 00:14:22 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33556) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtxG-00080A-IB for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqtxD-0000eM-DB for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:02 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47002 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 1eqtxD-0000eB-7f for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:06:59 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D9CB987ABA for ; Wed, 28 Feb 2018 05:06:58 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-95.pek2.redhat.com [10.72.12.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 53D0E2026E04; Wed, 28 Feb 2018 05:06:55 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 28 Feb 2018 13:06:23 +0800 Message-Id: <20180228050633.7410-5-peterx@redhat.com> In-Reply-To: <20180228050633.7410-1-peterx@redhat.com> References: <20180228050633.7410-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 28 Feb 2018 05:06:58 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 28 Feb 2018 05:06:58 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 04/14] migration: let incoming side use thread context 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: Laurent Vivier , Juan Quintela , Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini 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 old incoming migration is running in main thread and default gcontext. With the new qio_channel_add_watch_full() we can now let it run in the thread's own gcontext (if there is one). Currently this patch does nothing alone. But when any of the incoming migration is run in another iothread (e.g., the upcoming migrate-recover command), this patch will bind the incoming logic to the iothread instead of the main thread (which may already get page faulted and hanged). RDMA is not considered for now since it's not even using the QIO APIs at all. CC: Juan Quintela CC: Dr. David Alan Gilbert CC: Laurent Vivier Signed-off-by: Peter Xu --- migration/exec.c | 11 ++++++----- migration/fd.c | 11 ++++++----- migration/socket.c | 12 +++++++----- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/migration/exec.c b/migration/exec.c index 0bc5a427dd..f401fc005e 100644 --- a/migration/exec.c +++ b/migration/exec.c @@ -55,6 +55,7 @@ void exec_start_incoming_migration(const char *command, E= rror **errp) { QIOChannel *ioc; const char *argv[] =3D { "/bin/sh", "-c", command, NULL }; + GSource *source; =20 trace_migration_exec_incoming(command); ioc =3D QIO_CHANNEL(qio_channel_command_new_spawn(argv, @@ -65,9 +66,9 @@ void exec_start_incoming_migration(const char *command, E= rror **errp) } =20 qio_channel_set_name(ioc, "migration-exec-incoming"); - qio_channel_add_watch(ioc, - G_IO_IN, - exec_accept_incoming_migration, - NULL, - NULL); + source =3D qio_channel_add_watch_full(ioc, G_IO_IN, + exec_accept_incoming_migration, + NULL, NULL, + g_main_context_get_thread_default(= )); + g_source_unref(source); } diff --git a/migration/fd.c b/migration/fd.c index cd06182d1e..9c593eb3ff 100644 --- a/migration/fd.c +++ b/migration/fd.c @@ -55,6 +55,7 @@ void fd_start_incoming_migration(const char *infd, Error = **errp) { QIOChannel *ioc; int fd; + GSource *source; =20 fd =3D strtol(infd, NULL, 0); trace_migration_fd_incoming(fd); @@ -66,9 +67,9 @@ void fd_start_incoming_migration(const char *infd, Error = **errp) } =20 qio_channel_set_name(QIO_CHANNEL(ioc), "migration-fd-incoming"); - qio_channel_add_watch(ioc, - G_IO_IN, - fd_accept_incoming_migration, - NULL, - NULL); + source =3D qio_channel_add_watch_full(ioc, G_IO_IN, + fd_accept_incoming_migration, + NULL, NULL, + g_main_context_get_thread_default(= )); + g_source_unref(source); } diff --git a/migration/socket.c b/migration/socket.c index e090097077..82c330083c 100644 --- a/migration/socket.c +++ b/migration/socket.c @@ -164,6 +164,7 @@ static void socket_start_incoming_migration(SocketAddre= ss *saddr, Error **errp) { QIOChannelSocket *listen_ioc =3D qio_channel_socket_new(); + GSource *source; =20 qio_channel_set_name(QIO_CHANNEL(listen_ioc), "migration-socket-listener"); @@ -173,11 +174,12 @@ static void socket_start_incoming_migration(SocketAdd= ress *saddr, return; } =20 - qio_channel_add_watch(QIO_CHANNEL(listen_ioc), - G_IO_IN, - socket_accept_incoming_migration, - listen_ioc, - (GDestroyNotify)object_unref); + source =3D qio_channel_add_watch_full(QIO_CHANNEL(listen_ioc), G_IO_IN, + socket_accept_incoming_migration, + listen_ioc, + (GDestroyNotify)object_unref, + g_main_context_get_thread_default(= )); + g_source_unref(source); } =20 void tcp_start_incoming_migration(const char *host_port, Error **errp) --=20 2.14.3 From nobody Wed Oct 22 04:13:48 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519794985201957.3182550633472; Tue, 27 Feb 2018 21:16:25 -0800 (PST) Received: from localhost ([::1]:42280 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1equ6K-00078s-3d for importer@patchew.org; Wed, 28 Feb 2018 00:16:24 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33568) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtxI-00081P-Ba for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:05 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqtxH-0000hf-6a for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:04 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:54546 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 1eqtxH-0000hV-1p for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:03 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B1FF140FB650 for ; Wed, 28 Feb 2018 05:07:02 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-95.pek2.redhat.com [10.72.12.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6F5F92024CA6; Wed, 28 Feb 2018 05:06:59 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 28 Feb 2018 13:06:24 +0800 Message-Id: <20180228050633.7410-6-peterx@redhat.com> In-Reply-To: <20180228050633.7410-1-peterx@redhat.com> References: <20180228050633.7410-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Wed, 28 Feb 2018 05:07:02 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Wed, 28 Feb 2018 05:07:02 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 05/14] qio: refactor net listener source operations 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: Juan Quintela , Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini 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" Three functions are abstracted from the old code: - qio_net_listener_source_add(): create one source for listener - qio_net_listener_sources_clear(): unset existing net lister sources - qio_net_listener_sources_update(): setup all sources for listener Use them where possible. Signed-off-by: Peter Xu --- io/net-listener.c | 82 +++++++++++++++++++++++++++------------------------= ---- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/io/net-listener.c b/io/net-listener.c index de38dfae99..3e9ac51b0e 100644 --- a/io/net-listener.c +++ b/io/net-listener.c @@ -106,6 +106,39 @@ int qio_net_listener_open_sync(QIONetListener *listene= r, } } =20 +static guint qio_net_listener_source_add(QIONetListener *listener, + QIOChannelSocket *sioc) +{ + return qio_channel_add_watch(QIO_CHANNEL(sioc), G_IO_IN, + qio_net_listener_channel_func, + listener, (GDestroyNotify)object_unref); +} + +static void qio_net_listener_sources_clear(QIONetListener *listener) +{ + size_t i; + + for (i =3D 0; i < listener->nsioc; i++) { + if (listener->io_tag[i]) { + g_source_remove(listener->io_tag[i]); + listener->io_tag[i] =3D 0; + } + } +} + +static void qio_net_listener_sources_update(QIONetListener *listener) +{ + size_t i; + + if (listener->io_func !=3D NULL) { + for (i =3D 0; i < listener->nsioc; i++) { + assert(listener->io_tag[i] =3D=3D 0); + object_ref(OBJECT(listener)); + listener->io_tag[i] =3D qio_net_listener_source_add( + listener, listener->sioc[i]); + } + } +} =20 void qio_net_listener_add(QIONetListener *listener, QIOChannelSocket *sioc) @@ -127,10 +160,8 @@ void qio_net_listener_add(QIONetListener *listener, =20 if (listener->io_func !=3D NULL) { object_ref(OBJECT(listener)); - listener->io_tag[listener->nsioc] =3D qio_channel_add_watch( - QIO_CHANNEL(listener->sioc[listener->nsioc]), G_IO_IN, - qio_net_listener_channel_func, - listener, (GDestroyNotify)object_unref); + listener->io_tag[listener->nsioc] =3D qio_net_listener_source_add( + listener, listener->sioc[listener->nsioc]); } =20 listener->nsioc++; @@ -142,8 +173,6 @@ void qio_net_listener_set_client_func(QIONetListener *l= istener, gpointer data, GDestroyNotify notify) { - size_t i; - if (listener->io_notify) { listener->io_notify(listener->io_data); } @@ -151,22 +180,8 @@ void qio_net_listener_set_client_func(QIONetListener *= listener, listener->io_data =3D data; listener->io_notify =3D notify; =20 - for (i =3D 0; i < listener->nsioc; i++) { - if (listener->io_tag[i]) { - g_source_remove(listener->io_tag[i]); - listener->io_tag[i] =3D 0; - } - } - - if (listener->io_func !=3D NULL) { - for (i =3D 0; i < listener->nsioc; i++) { - object_ref(OBJECT(listener)); - listener->io_tag[i] =3D qio_channel_add_watch( - QIO_CHANNEL(listener->sioc[i]), G_IO_IN, - qio_net_listener_channel_func, - listener, (GDestroyNotify)object_unref); - } - } + qio_net_listener_sources_clear(listener); + qio_net_listener_sources_update(listener); } =20 =20 @@ -210,12 +225,7 @@ QIOChannelSocket *qio_net_listener_wait_client(QIONetL= istener *listener) }; size_t i; =20 - for (i =3D 0; i < listener->nsioc; i++) { - if (listener->io_tag[i]) { - g_source_remove(listener->io_tag[i]); - listener->io_tag[i] =3D 0; - } - } + qio_net_listener_sources_clear(listener); =20 sources =3D g_new0(GSource *, listener->nsioc); for (i =3D 0; i < listener->nsioc; i++) { @@ -238,15 +248,7 @@ QIOChannelSocket *qio_net_listener_wait_client(QIONetL= istener *listener) g_main_loop_unref(loop); g_main_context_unref(ctxt); =20 - if (listener->io_func !=3D NULL) { - for (i =3D 0; i < listener->nsioc; i++) { - object_ref(OBJECT(listener)); - listener->io_tag[i] =3D qio_channel_add_watch( - QIO_CHANNEL(listener->sioc[i]), G_IO_IN, - qio_net_listener_channel_func, - listener, (GDestroyNotify)object_unref); - } - } + qio_net_listener_sources_update(listener); =20 return data.sioc; } @@ -259,11 +261,9 @@ void qio_net_listener_disconnect(QIONetListener *liste= ner) return; } =20 + qio_net_listener_sources_clear(listener); + for (i =3D 0; i < listener->nsioc; i++) { - if (listener->io_tag[i]) { - g_source_remove(listener->io_tag[i]); - listener->io_tag[i] =3D 0; - } qio_channel_close(QIO_CHANNEL(listener->sioc[i]), NULL); } listener->connected =3D false; --=20 2.14.3 From nobody Wed Oct 22 04:13:48 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519794556725934.2971040703516; Tue, 27 Feb 2018 21:09:16 -0800 (PST) Received: from localhost ([::1]:42236 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtzP-0000vk-F3 for importer@patchew.org; Wed, 28 Feb 2018 00:09:15 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33580) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtxM-00084u-7F for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:11 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqtxL-0000jA-1U for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:08 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:54548 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 1eqtxK-0000ia-SS for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:06 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8485A40FB647 for ; Wed, 28 Feb 2018 05:07:06 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-95.pek2.redhat.com [10.72.12.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 47A1E2024CA6; Wed, 28 Feb 2018 05:07:03 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 28 Feb 2018 13:06:25 +0800 Message-Id: <20180228050633.7410-7-peterx@redhat.com> In-Reply-To: <20180228050633.7410-1-peterx@redhat.com> References: <20180228050633.7410-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Wed, 28 Feb 2018 05:07:06 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Wed, 28 Feb 2018 05:07:06 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 06/14] qio: store gsources for net listeners 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: Juan Quintela , Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini 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" Originally we were storing the GSources tag IDs. That'll be not enough if we are going to support non-default gcontext for QIO code. Switch to GSources without changing anything real. Now we still always pass in NULL, which means the default gcontext. Signed-off-by: Peter Xu --- include/io/net-listener.h | 21 ++++++++++++++-- io/net-listener.c | 62 +++++++++++++++++++++++++++++--------------= ---- 2 files changed, 58 insertions(+), 25 deletions(-) diff --git a/include/io/net-listener.h b/include/io/net-listener.h index 56d6da7a76..566be283b3 100644 --- a/include/io/net-listener.h +++ b/include/io/net-listener.h @@ -53,7 +53,7 @@ struct QIONetListener { =20 char *name; QIOChannelSocket **sioc; - gulong *io_tag; + GSource **io_source; size_t nsioc; =20 bool connected; @@ -120,17 +120,34 @@ void qio_net_listener_add(QIONetListener *listener, QIOChannelSocket *sioc); =20 /** - * qio_net_listener_set_client_func: + * qio_net_listener_set_client_func_full: * @listener: the network listener object * @func: the callback function * @data: opaque data to pass to @func * @notify: callback to free @data + * @context: the context that the sources will be bound to * * Register @func to be invoked whenever a new client * connects to the listener. @func will be invoked * passing in the QIOChannelSocket instance for the * client. */ +void qio_net_listener_set_client_func_full(QIONetListener *listener, + QIONetListenerClientFunc func, + gpointer data, + GDestroyNotify notify, + GMainContext *context); + +/** + * qio_net_listener_set_client_func: + * @listener: the network listener object + * @func: the callback function + * @data: opaque data to pass to @func + * @notify: callback to free @data + * + * Wrapper of qio_net_listener_set_client_func_full(), only that the + * sources will always be bound to default main context. + */ void qio_net_listener_set_client_func(QIONetListener *listener, QIONetListenerClientFunc func, gpointer data, diff --git a/io/net-listener.c b/io/net-listener.c index 3e9ac51b0e..7f07a81fed 100644 --- a/io/net-listener.c +++ b/io/net-listener.c @@ -106,12 +106,15 @@ int qio_net_listener_open_sync(QIONetListener *listen= er, } } =20 -static guint qio_net_listener_source_add(QIONetListener *listener, - QIOChannelSocket *sioc) +static GSource *qio_net_listener_source_add(QIONetListener *listener, + QIOChannelSocket *sioc, + GMainContext *context) { - return qio_channel_add_watch(QIO_CHANNEL(sioc), G_IO_IN, - qio_net_listener_channel_func, - listener, (GDestroyNotify)object_unref); + return qio_channel_add_watch_full(QIO_CHANNEL(sioc), G_IO_IN, + qio_net_listener_channel_func, + listener, + (GDestroyNotify)object_unref, + context); } =20 static void qio_net_listener_sources_clear(QIONetListener *listener) @@ -119,23 +122,25 @@ static void qio_net_listener_sources_clear(QIONetList= ener *listener) size_t i; =20 for (i =3D 0; i < listener->nsioc; i++) { - if (listener->io_tag[i]) { - g_source_remove(listener->io_tag[i]); - listener->io_tag[i] =3D 0; + if (listener->io_source[i]) { + g_source_destroy(listener->io_source[i]); + g_source_unref(listener->io_source[i]); + listener->io_source[i] =3D NULL; } } } =20 -static void qio_net_listener_sources_update(QIONetListener *listener) +static void qio_net_listener_sources_update(QIONetListener *listener, + GMainContext *context) { size_t i; =20 if (listener->io_func !=3D NULL) { for (i =3D 0; i < listener->nsioc; i++) { - assert(listener->io_tag[i] =3D=3D 0); + assert(listener->io_source[i] =3D=3D NULL); object_ref(OBJECT(listener)); - listener->io_tag[i] =3D qio_net_listener_source_add( - listener, listener->sioc[i]); + listener->io_source[i] =3D qio_net_listener_source_add( + listener, listener->sioc[i], context); } } } @@ -151,27 +156,30 @@ void qio_net_listener_add(QIONetListener *listener, =20 listener->sioc =3D g_renew(QIOChannelSocket *, listener->sioc, listener->nsioc + 1); - listener->io_tag =3D g_renew(gulong, listener->io_tag, listener->nsioc= + 1); + listener->io_source =3D g_renew(typeof(listener->io_source[0]), + listener->io_source, + listener->nsioc + 1); listener->sioc[listener->nsioc] =3D sioc; - listener->io_tag[listener->nsioc] =3D 0; + listener->io_source[listener->nsioc] =3D NULL; =20 object_ref(OBJECT(sioc)); listener->connected =3D true; =20 if (listener->io_func !=3D NULL) { object_ref(OBJECT(listener)); - listener->io_tag[listener->nsioc] =3D qio_net_listener_source_add( - listener, listener->sioc[listener->nsioc]); + listener->io_source[listener->nsioc] =3D qio_net_listener_source_a= dd( + listener, listener->sioc[listener->nsioc], NULL); } =20 listener->nsioc++; } =20 =20 -void qio_net_listener_set_client_func(QIONetListener *listener, - QIONetListenerClientFunc func, - gpointer data, - GDestroyNotify notify) +void qio_net_listener_set_client_func_full(QIONetListener *listener, + QIONetListenerClientFunc func, + gpointer data, + GDestroyNotify notify, + GMainContext *context) { if (listener->io_notify) { listener->io_notify(listener->io_data); @@ -181,9 +189,17 @@ void qio_net_listener_set_client_func(QIONetListener *= listener, listener->io_notify =3D notify; =20 qio_net_listener_sources_clear(listener); - qio_net_listener_sources_update(listener); + qio_net_listener_sources_update(listener, context); } =20 +void qio_net_listener_set_client_func(QIONetListener *listener, + QIONetListenerClientFunc func, + gpointer data, + GDestroyNotify notify) +{ + qio_net_listener_set_client_func_full(listener, func, data, + notify, NULL); +} =20 struct QIONetListenerClientWaitData { QIOChannelSocket *sioc; @@ -248,7 +264,7 @@ QIOChannelSocket *qio_net_listener_wait_client(QIONetLi= stener *listener) g_main_loop_unref(loop); g_main_context_unref(ctxt); =20 - qio_net_listener_sources_update(listener); + qio_net_listener_sources_update(listener, NULL); =20 return data.sioc; } @@ -285,7 +301,7 @@ static void qio_net_listener_finalize(Object *obj) for (i =3D 0; i < listener->nsioc; i++) { object_unref(OBJECT(listener->sioc[i])); } - g_free(listener->io_tag); + g_free(listener->io_source); g_free(listener->sioc); g_free(listener->name); } --=20 2.14.3 From nobody Wed Oct 22 04:13:49 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519794734534749.1894252303505; Tue, 27 Feb 2018 21:12:14 -0800 (PST) Received: from localhost ([::1]:42254 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1equ2H-0003Re-CH for importer@patchew.org; Wed, 28 Feb 2018 00:12:13 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33592) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtxP-00087s-R6 for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqtxO-0000kQ-TA for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:11 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47008 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 1eqtxO-0000kH-N1 for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:10 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5AE02EAEA0 for ; Wed, 28 Feb 2018 05:07:10 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-95.pek2.redhat.com [10.72.12.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1AA862024CA6; Wed, 28 Feb 2018 05:07:06 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 28 Feb 2018 13:06:26 +0800 Message-Id: <20180228050633.7410-8-peterx@redhat.com> In-Reply-To: <20180228050633.7410-1-peterx@redhat.com> References: <20180228050633.7410-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 28 Feb 2018 05:07:10 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 28 Feb 2018 05:07:10 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 07/14] qio/chardev: update net listener gcontext 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: Juan Quintela , Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini 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" TCP chardevs can be using QIO network listeners working in the background when in listening mode. However the network listeners are always running in main context. This can race with chardevs that are running in non-main contexts. To solve this: firstly introduce qio_net_listener_set_context() to allow caller to set gcontext for network listeners. Then call it in tcp_chr_update_read_handler(), with the newly cached gcontext. It's fairly straightforward after we have introduced some net listener helper functions - basically we unregister the GSources and add them back with the correct context. Signed-off-by: Peter Xu --- chardev/char-socket.c | 9 +++++++++ include/io/net-listener.h | 12 ++++++++++++ io/net-listener.c | 7 +++++++ 3 files changed, 28 insertions(+) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 43a2cc2c1c..8f0935cd15 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -559,6 +559,15 @@ static void tcp_chr_update_read_handler(Chardev *chr) { SocketChardev *s =3D SOCKET_CHARDEV(chr); =20 + if (s->listener) { + /* + * It's possible that chardev context is changed in + * qemu_chr_be_update_read_handlers(). Reset it for QIO net + * listener if there is. + */ + qio_net_listener_set_context(s->listener, chr->gcontext); + } + if (!s->connected) { return; } diff --git a/include/io/net-listener.h b/include/io/net-listener.h index 566be283b3..39dede9d6f 100644 --- a/include/io/net-listener.h +++ b/include/io/net-listener.h @@ -106,6 +106,18 @@ int qio_net_listener_open_sync(QIONetListener *listene= r, SocketAddress *addr, Error **errp); =20 +/** + * qio_net_listener_set_context: + * @listener: the net listener object + * @context: the context that we'd like to bind the sources to + * + * This helper does not do anything but moves existing net listener + * sources from the old one to the new one. It can be seen as a + * no-operation if there is no listening source at all. + */ +void qio_net_listener_set_context(QIONetListener *listener, + GMainContext *context); + /** * qio_net_listener_add: * @listener: the network listener object diff --git a/io/net-listener.c b/io/net-listener.c index 7f07a81fed..7ffad72f55 100644 --- a/io/net-listener.c +++ b/io/net-listener.c @@ -145,6 +145,13 @@ static void qio_net_listener_sources_update(QIONetList= ener *listener, } } =20 +void qio_net_listener_set_context(QIONetListener *listener, + GMainContext *context) +{ + qio_net_listener_sources_clear(listener); + qio_net_listener_sources_update(listener, context); +} + void qio_net_listener_add(QIONetListener *listener, QIOChannelSocket *sioc) { --=20 2.14.3 From nobody Wed Oct 22 04:13:49 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 Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519794872990874.5377290169274; Tue, 27 Feb 2018 21:14:32 -0800 (PST) Received: from localhost ([::1]:42266 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1equ4R-0005Pv-6w for importer@patchew.org; Wed, 28 Feb 2018 00:14:27 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33608) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtxT-0008BX-Nb for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqtxS-0000lV-Mf for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:15 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47012 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 1eqtxS-0000lK-Hz for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:14 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2E81E87ABA for ; Wed, 28 Feb 2018 05:07:14 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-95.pek2.redhat.com [10.72.12.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id E4C8E2024CA6; Wed, 28 Feb 2018 05:07:10 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 28 Feb 2018 13:06:27 +0800 Message-Id: <20180228050633.7410-9-peterx@redhat.com> In-Reply-To: <20180228050633.7410-1-peterx@redhat.com> References: <20180228050633.7410-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 28 Feb 2018 05:07:14 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 28 Feb 2018 05:07:14 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 08/14] chardev: allow telnet gsource to switch gcontext 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: Juan Quintela , Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini 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" It was originally created by qio_channel_add_watch() so it's always assigning the task to main context. Now we use the new API called qio_channel_add_watch_full() so that we get the GSource handle rather than the tag ID. Meanwhile, caching the gsource in SocketChardev.telnet_source so that we can also do dynamic context switch when update read handlers. Signed-off-by: Peter Xu --- chardev/char-socket.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 8f0935cd15..a16d894c40 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -59,6 +59,7 @@ typedef struct { bool is_listen; bool is_telnet; bool is_tn3270; + GSource *telnet_source; =20 GSource *reconnect_timer; int64_t reconnect_time; @@ -69,6 +70,7 @@ typedef struct { OBJECT_CHECK(SocketChardev, (obj), TYPE_CHARDEV_SOCKET) =20 static gboolean socket_reconnect_timeout(gpointer opaque); +static void tcp_chr_telnet_init(Chardev *chr); =20 static void tcp_chr_reconn_timer_cancel(SocketChardev *s) { @@ -555,6 +557,15 @@ static void tcp_chr_connect(void *opaque) qemu_chr_be_event(chr, CHR_EVENT_OPENED); } =20 +static void tcp_chr_telnet_destroy(SocketChardev *s) +{ + if (s->telnet_source) { + g_source_destroy(s->telnet_source); + g_source_unref(s->telnet_source); + s->telnet_source =3D NULL; + } +} + static void tcp_chr_update_read_handler(Chardev *chr) { SocketChardev *s =3D SOCKET_CHARDEV(chr); @@ -568,6 +579,11 @@ static void tcp_chr_update_read_handler(Chardev *chr) qio_net_listener_set_context(s->listener, chr->gcontext); } =20 + if (s->telnet_source) { + tcp_chr_telnet_destroy(s); + tcp_chr_telnet_init(CHARDEV(s)); + } + if (!s->connected) { return; } @@ -592,6 +608,7 @@ static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc, gpointer user_data) { TCPChardevTelnetInit *init =3D user_data; + SocketChardev *s =3D SOCKET_CHARDEV(init->chr); ssize_t ret; =20 ret =3D qio_channel_write(ioc, init->buf, init->buflen, NULL); @@ -616,6 +633,8 @@ static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc, =20 end: g_free(init); + g_source_unref(s->telnet_source); + s->telnet_source =3D NULL; return G_SOURCE_REMOVE; } =20 @@ -655,10 +674,10 @@ static void tcp_chr_telnet_init(Chardev *chr) =20 #undef IACSET =20 - qio_channel_add_watch( - s->ioc, G_IO_OUT, - tcp_chr_telnet_init_io, - init, NULL); + s->telnet_source =3D qio_channel_add_watch_full(s->ioc, G_IO_OUT, + tcp_chr_telnet_init_io, + init, NULL, + chr->gcontext); } =20 =20 @@ -831,6 +850,7 @@ static void char_socket_finalize(Object *obj) tcp_chr_free_connection(chr); tcp_chr_reconn_timer_cancel(s); qapi_free_SocketAddress(s->addr); + tcp_chr_telnet_destroy(s); if (s->listener) { qio_net_listener_set_client_func(s->listener, NULL, NULL, NULL); object_unref(OBJECT(s->listener)); --=20 2.14.3 From nobody Wed Oct 22 04:13:49 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519794884459184.64134210226507; Tue, 27 Feb 2018 21:14:44 -0800 (PST) Received: from localhost ([::1]:42267 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1equ4h-0005fH-58 for importer@patchew.org; Wed, 28 Feb 2018 00:14:43 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33629) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtxZ-0008F8-Mk for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:23 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqtxW-0000md-GE for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:21 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:49528 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 1eqtxW-0000mV-B9 for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:18 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 02B09402290A for ; Wed, 28 Feb 2018 05:07:18 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-95.pek2.redhat.com [10.72.12.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id B78782024CA6; Wed, 28 Feb 2018 05:07:14 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 28 Feb 2018 13:06:28 +0800 Message-Id: <20180228050633.7410-10-peterx@redhat.com> In-Reply-To: <20180228050633.7410-1-peterx@redhat.com> References: <20180228050633.7410-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 28 Feb 2018 05:07:18 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 28 Feb 2018 05:07:18 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 09/14] qio: basic non-default context support for thread 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: Juan Quintela , Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini 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" qio_task_run_in_thread() allows main thread to run blocking operations in the background. However it has an assumption on that it's always working with the default context. This patch tries to allow the QIO task framework to run with non-default gcontext. Currently no functional change so far, so the QIOTasks are still always running on main context. Signed-off-by: Peter Xu --- include/io/task.h | 6 ++++-- io/channel-socket.c | 9 ++++++--- io/dns-resolver.c | 3 ++- io/task.c | 28 ++++++++++++++++++++++++++-- tests/test-io-task.c | 2 ++ 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/include/io/task.h b/include/io/task.h index 6021f51336..9dbe3758d7 100644 --- a/include/io/task.h +++ b/include/io/task.h @@ -227,15 +227,17 @@ QIOTask *qio_task_new(Object *source, * @worker: the function to invoke in a thread * @opaque: opaque data to pass to @worker * @destroy: function to free @opaque + * @context: the context to run the complete hook * * Run a task in a background thread. When @worker * returns it will call qio_task_complete() in - * the main event thread context. + * the event thread context that provided. */ void qio_task_run_in_thread(QIOTask *task, QIOTaskWorker worker, gpointer opaque, - GDestroyNotify destroy); + GDestroyNotify destroy, + GMainContext *context); =20 /** * qio_task_complete: diff --git a/io/channel-socket.c b/io/channel-socket.c index 563e297357..4224ce323a 100644 --- a/io/channel-socket.c +++ b/io/channel-socket.c @@ -187,7 +187,8 @@ void qio_channel_socket_connect_async(QIOChannelSocket = *ioc, qio_task_run_in_thread(task, qio_channel_socket_connect_worker, addrCopy, - (GDestroyNotify)qapi_free_SocketAddress); + (GDestroyNotify)qapi_free_SocketAddress, + NULL); } =20 =20 @@ -245,7 +246,8 @@ void qio_channel_socket_listen_async(QIOChannelSocket *= ioc, qio_task_run_in_thread(task, qio_channel_socket_listen_worker, addrCopy, - (GDestroyNotify)qapi_free_SocketAddress); + (GDestroyNotify)qapi_free_SocketAddress, + NULL); } =20 =20 @@ -321,7 +323,8 @@ void qio_channel_socket_dgram_async(QIOChannelSocket *i= oc, qio_task_run_in_thread(task, qio_channel_socket_dgram_worker, data, - qio_channel_socket_dgram_worker_free); + qio_channel_socket_dgram_worker_free, + NULL); } =20 =20 diff --git a/io/dns-resolver.c b/io/dns-resolver.c index c072d121c3..75c2ca9c4a 100644 --- a/io/dns-resolver.c +++ b/io/dns-resolver.c @@ -233,7 +233,8 @@ void qio_dns_resolver_lookup_async(QIODNSResolver *reso= lver, qio_task_run_in_thread(task, qio_dns_resolver_lookup_worker, data, - qio_dns_resolver_lookup_data_free); + qio_dns_resolver_lookup_data_free, + NULL); } =20 =20 diff --git a/io/task.c b/io/task.c index 1a0a1c7185..204c0be286 100644 --- a/io/task.c +++ b/io/task.c @@ -32,6 +32,10 @@ struct QIOTask { Error *err; gpointer result; GDestroyNotify destroyResult; + + /* Threaded QIO task specific fields */ + GSource *idle_source; /* The idle task to run complete routine */ + GMainContext *context; /* The context that idle task will run with */ }; =20 =20 @@ -49,6 +53,7 @@ QIOTask *qio_task_new(Object *source, task->func =3D func; task->opaque =3D opaque; task->destroy =3D destroy; + task->idle_source =3D NULL; =20 trace_qio_task_new(task, source, func, opaque); =20 @@ -66,6 +71,12 @@ static void qio_task_free(QIOTask *task) if (task->err) { error_free(task->err); } + if (task->idle_source) { + g_source_unref(task->idle_source); + } + if (task->context) { + g_main_context_unref(task->context); + } object_unref(task->source); =20 g_free(task); @@ -100,6 +111,8 @@ static gboolean qio_task_thread_result(gpointer opaque) static gpointer qio_task_thread_worker(gpointer opaque) { struct QIOTaskThreadData *data =3D opaque; + QIOTask *task =3D data->task; + GSource *idle; =20 trace_qio_task_thread_run(data->task); data->worker(data->task, data->opaque); @@ -110,7 +123,12 @@ static gpointer qio_task_thread_worker(gpointer opaque) * the worker results */ trace_qio_task_thread_exit(data->task); - g_idle_add(qio_task_thread_result, data); + + idle =3D g_idle_source_new(); + g_source_set_callback(idle, qio_task_thread_result, data, NULL); + g_source_attach(idle, task->context); + task->idle_source =3D idle; + return NULL; } =20 @@ -118,11 +136,17 @@ static gpointer qio_task_thread_worker(gpointer opaqu= e) void qio_task_run_in_thread(QIOTask *task, QIOTaskWorker worker, gpointer opaque, - GDestroyNotify destroy) + GDestroyNotify destroy, + GMainContext *context) { struct QIOTaskThreadData *data =3D g_new0(struct QIOTaskThreadData, 1); QemuThread thread; =20 + if (context) { + g_main_context_ref(context); + task->context =3D context; + } + data->task =3D task; data->worker =3D worker; data->opaque =3D opaque; diff --git a/tests/test-io-task.c b/tests/test-io-task.c index 141aa2c55d..bac1bb4e7a 100644 --- a/tests/test-io-task.c +++ b/tests/test-io-task.c @@ -187,6 +187,7 @@ static void test_task_thread_complete(void) qio_task_run_in_thread(task, test_task_thread_worker, &data, + NULL, NULL); =20 g_main_loop_run(data.loop); @@ -228,6 +229,7 @@ static void test_task_thread_failure(void) qio_task_run_in_thread(task, test_task_thread_worker, &data, + NULL, NULL); =20 g_main_loop_run(data.loop); --=20 2.14.3 From nobody Wed Oct 22 04:13:49 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519795136629285.0506525304188; Tue, 27 Feb 2018 21:18:56 -0800 (PST) Received: from localhost ([::1]:42292 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1equ8l-0000Sy-Fm for importer@patchew.org; Wed, 28 Feb 2018 00:18:55 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33650) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtxb-0008G2-4H for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:24 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqtxa-0000nl-9X for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:23 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47018 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 1eqtxa-0000nf-5U for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:22 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D399887ABA for ; Wed, 28 Feb 2018 05:07:21 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-95.pek2.redhat.com [10.72.12.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 915702024CA6; Wed, 28 Feb 2018 05:07:18 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 28 Feb 2018 13:06:29 +0800 Message-Id: <20180228050633.7410-11-peterx@redhat.com> In-Reply-To: <20180228050633.7410-1-peterx@redhat.com> References: <20180228050633.7410-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 28 Feb 2018 05:07:21 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 28 Feb 2018 05:07:21 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 10/14] qio: refcount QIOTask 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: Juan Quintela , Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini 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" It will be used in multiple threads in follow-up patches. Let it start to have refcounts. Signed-off-by: Peter Xu --- include/io/task.h | 3 +++ io/task.c | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/io/task.h b/include/io/task.h index 9dbe3758d7..c6acd6489c 100644 --- a/include/io/task.h +++ b/include/io/task.h @@ -322,4 +322,7 @@ gpointer qio_task_get_result_pointer(QIOTask *task); */ Object *qio_task_get_source(QIOTask *task); =20 +void qio_task_ref(QIOTask *task); +void qio_task_unref(QIOTask *task); + #endif /* QIO_TASK_H */ diff --git a/io/task.c b/io/task.c index 204c0be286..00d3a5096a 100644 --- a/io/task.c +++ b/io/task.c @@ -32,6 +32,7 @@ struct QIOTask { Error *err; gpointer result; GDestroyNotify destroyResult; + uint32_t refcount; =20 /* Threaded QIO task specific fields */ GSource *idle_source; /* The idle task to run complete routine */ @@ -57,6 +58,8 @@ QIOTask *qio_task_new(Object *source, =20 trace_qio_task_new(task, source, func, opaque); =20 + qio_task_ref(task); + return task; } =20 @@ -165,7 +168,7 @@ void qio_task_complete(QIOTask *task) { task->func(task, task->opaque); trace_qio_task_complete(task); - qio_task_free(task); + qio_task_unref(task); } =20 =20 @@ -208,3 +211,21 @@ Object *qio_task_get_source(QIOTask *task) { return task->source; } + +void qio_task_ref(QIOTask *task) +{ + if (!task) { + return; + } + atomic_inc(&task->refcount); +} + +void qio_task_unref(QIOTask *task) +{ + if (!task) { + return; + } + if (atomic_fetch_dec(&task->refcount) =3D=3D 1) { + qio_task_free(task); + } +} --=20 2.14.3 From nobody Wed Oct 22 04:13:49 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519794576966889.2715522891746; Tue, 27 Feb 2018 21:09:36 -0800 (PST) Received: from localhost ([::1]:42238 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtzj-0001CV-N9 for importer@patchew.org; Wed, 28 Feb 2018 00:09:35 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33674) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtxf-0008Hu-8V for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:30 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqtxe-0000ox-5f for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:27 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47020 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 1eqtxe-0000oq-0B for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:26 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AA5C387ABA for ; Wed, 28 Feb 2018 05:07:25 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-95.pek2.redhat.com [10.72.12.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 696AE2026E04; Wed, 28 Feb 2018 05:07:22 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 28 Feb 2018 13:06:30 +0800 Message-Id: <20180228050633.7410-12-peterx@redhat.com> In-Reply-To: <20180228050633.7410-1-peterx@redhat.com> References: <20180228050633.7410-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 28 Feb 2018 05:07:25 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 28 Feb 2018 05:07:25 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 11/14] qio/chardev: return QIOTask when connect async 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: Juan Quintela , Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini 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" Let qio_channel_socket_connect_async() return the created QIOTask object for the async connection. In tcp chardev, cache that in SocketChardev for further use. With the QIOTask refcount, this is pretty safe. Since at it, generalize out tcp_chr_socket_connect_async() since the logic is used in both initial phase and reconnect timeout. Signed-off-by: Peter Xu --- chardev/char-socket.c | 33 ++++++++++++++++++++++----------- include/io/channel-socket.h | 14 +++++++++----- io/channel-socket.c | 12 +++++++----- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index a16d894c40..9d51b8da07 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -64,6 +64,7 @@ typedef struct { GSource *reconnect_timer; int64_t reconnect_time; bool connect_err_reported; + QIOTask *thread_task; } SocketChardev; =20 #define SOCKET_CHARDEV(obj) \ @@ -879,14 +880,32 @@ static void qemu_chr_socket_connected(QIOTask *task, = void *opaque) tcp_chr_new_client(chr, sioc); =20 cleanup: + assert(s->thread_task =3D=3D task); + qio_task_unref(task); + s->thread_task =3D NULL; object_unref(OBJECT(sioc)); } =20 +static void tcp_chr_socket_connect_async(SocketChardev *s) +{ + QIOChannelSocket *sioc =3D qio_channel_socket_new(); + Chardev *chr =3D CHARDEV(s); + QIOTask *task; + + assert(s->thread_task =3D=3D NULL); + + tcp_chr_set_client_ioc_name(chr, sioc); + task =3D qio_channel_socket_connect_async(sioc, s->addr, + qemu_chr_socket_connected, + chr, NULL); + qio_task_ref(task); + s->thread_task =3D task; +} + static gboolean socket_reconnect_timeout(gpointer opaque) { Chardev *chr =3D CHARDEV(opaque); SocketChardev *s =3D SOCKET_CHARDEV(opaque); - QIOChannelSocket *sioc; =20 g_source_unref(s->reconnect_timer); s->reconnect_timer =3D NULL; @@ -895,11 +914,7 @@ static gboolean socket_reconnect_timeout(gpointer opaq= ue) return false; } =20 - sioc =3D qio_channel_socket_new(); - tcp_chr_set_client_ioc_name(chr, sioc); - qio_channel_socket_connect_async(sioc, s->addr, - qemu_chr_socket_connected, - chr, NULL); + tcp_chr_socket_connect_async(s); =20 return false; } @@ -979,11 +994,7 @@ static void qmp_chardev_open_socket(Chardev *chr, } =20 if (s->reconnect_time) { - sioc =3D qio_channel_socket_new(); - tcp_chr_set_client_ioc_name(chr, sioc); - qio_channel_socket_connect_async(sioc, s->addr, - qemu_chr_socket_connected, - chr, NULL); + tcp_chr_socket_connect_async(s); } else { if (s->is_listen) { char *name; diff --git a/include/io/channel-socket.h b/include/io/channel-socket.h index 53801f6042..5cfa9e2b7c 100644 --- a/include/io/channel-socket.h +++ b/include/io/channel-socket.h @@ -108,12 +108,16 @@ int qio_channel_socket_connect_sync(QIOChannelSocket = *ioc, * will be invoked on completion or failure. The @addr * parameter will be copied, so may be freed as soon * as this function returns without waiting for completion. + * + * Returns the IOTask created. NOTE: if the caller is going to use + * the returned QIOTask, the caller is responsible to reference the + * task and unref it when it's not needed any more. */ -void qio_channel_socket_connect_async(QIOChannelSocket *ioc, - SocketAddress *addr, - QIOTaskFunc callback, - gpointer opaque, - GDestroyNotify destroy); +QIOTask *qio_channel_socket_connect_async(QIOChannelSocket *ioc, + SocketAddress *addr, + QIOTaskFunc callback, + gpointer opaque, + GDestroyNotify destroy); =20 =20 /** diff --git a/io/channel-socket.c b/io/channel-socket.c index 4224ce323a..f420502290 100644 --- a/io/channel-socket.c +++ b/io/channel-socket.c @@ -169,11 +169,11 @@ static void qio_channel_socket_connect_worker(QIOTask= *task, } =20 =20 -void qio_channel_socket_connect_async(QIOChannelSocket *ioc, - SocketAddress *addr, - QIOTaskFunc callback, - gpointer opaque, - GDestroyNotify destroy) +QIOTask *qio_channel_socket_connect_async(QIOChannelSocket *ioc, + SocketAddress *addr, + QIOTaskFunc callback, + gpointer opaque, + GDestroyNotify destroy) { QIOTask *task =3D qio_task_new( OBJECT(ioc), callback, opaque, destroy); @@ -189,6 +189,8 @@ void qio_channel_socket_connect_async(QIOChannelSocket = *ioc, addrCopy, (GDestroyNotify)qapi_free_SocketAddress, NULL); + + return task; } =20 =20 --=20 2.14.3 From nobody Wed Oct 22 04:13:49 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 Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519795231052483.58099226944466; Tue, 27 Feb 2018 21:20:31 -0800 (PST) Received: from localhost ([::1]:42304 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1equA8-0001R1-9y for importer@patchew.org; Wed, 28 Feb 2018 00:20:20 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33697) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtxi-0008LV-Vc for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:32 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqtxh-0000rT-Un for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:30 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:49542 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 1eqtxh-0000qO-QI for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:29 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7D3D1402290A for ; Wed, 28 Feb 2018 05:07:29 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-95.pek2.redhat.com [10.72.12.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 407562024CA6; Wed, 28 Feb 2018 05:07:25 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 28 Feb 2018 13:06:31 +0800 Message-Id: <20180228050633.7410-13-peterx@redhat.com> In-Reply-To: <20180228050633.7410-1-peterx@redhat.com> References: <20180228050633.7410-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 28 Feb 2018 05:07:29 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 28 Feb 2018 05:07:29 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 12/14] qio: move QIOTaskThreadData into QIOTask 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: Juan Quintela , Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini 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 major reason to do this is that, after the upper level can cache the QIOTask, it should also be able to further manage the QIOTask. And, it can't if it does not have the information in QIOTaskThreadData. So let's just merge this struct with QIOTask. Actually by doing this, it'll simplify the code a bit too. This will be needed in the next patch, when we want to rebuild the completion GSource when the GMainContext changed. Signed-off-by: Peter Xu --- io/task.c | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/io/task.c b/io/task.c index 00d3a5096a..080f9560ea 100644 --- a/io/task.c +++ b/io/task.c @@ -24,6 +24,13 @@ #include "qemu/thread.h" #include "trace.h" =20 +struct QIOTaskThreadData { + QIOTaskWorker worker; + gpointer opaque; + GDestroyNotify destroy; +}; +typedef struct QIOTaskThreadData QIOTaskThreadData; + struct QIOTask { Object *source; QIOTaskFunc func; @@ -37,6 +44,7 @@ struct QIOTask { /* Threaded QIO task specific fields */ GSource *idle_source; /* The idle task to run complete routine */ GMainContext *context; /* The context that idle task will run with */ + QIOTaskThreadData thread_data; }; =20 =20 @@ -86,26 +94,25 @@ static void qio_task_free(QIOTask *task) } =20 =20 -struct QIOTaskThreadData { - QIOTask *task; - QIOTaskWorker worker; - gpointer opaque; - GDestroyNotify destroy; -}; - - static gboolean qio_task_thread_result(gpointer opaque) { - struct QIOTaskThreadData *data =3D opaque; + QIOTask *task =3D opaque; + QIOTaskThreadData *data =3D &task->thread_data; =20 - trace_qio_task_thread_result(data->task); - qio_task_complete(data->task); + /* + * Take one more refcount since qio_task_complete() may otherwise + * release the last refcount and free, then "data" may be invalid. + */ + qio_task_ref(task); + + trace_qio_task_thread_result(task); + qio_task_complete(task); =20 if (data->destroy) { data->destroy(data->opaque); } =20 - g_free(data); + qio_task_unref(task); =20 return FALSE; } @@ -113,19 +120,19 @@ static gboolean qio_task_thread_result(gpointer opaqu= e) =20 static gpointer qio_task_thread_worker(gpointer opaque) { - struct QIOTaskThreadData *data =3D opaque; - QIOTask *task =3D data->task; + QIOTask *task =3D opaque; + QIOTaskThreadData *data =3D &task->thread_data; GSource *idle; =20 - trace_qio_task_thread_run(data->task); - data->worker(data->task, data->opaque); + trace_qio_task_thread_run(task); + data->worker(task, data->opaque); =20 /* We're running in the background thread, and must only * ever report the task results in the main event loop * thread. So we schedule an idle callback to report * the worker results */ - trace_qio_task_thread_exit(data->task); + trace_qio_task_thread_exit(task); =20 idle =3D g_idle_source_new(); g_source_set_callback(idle, qio_task_thread_result, data, NULL); @@ -142,15 +149,14 @@ void qio_task_run_in_thread(QIOTask *task, GDestroyNotify destroy, GMainContext *context) { - struct QIOTaskThreadData *data =3D g_new0(struct QIOTaskThreadData, 1); QemuThread thread; + QIOTaskThreadData *data =3D &task->thread_data; =20 if (context) { g_main_context_ref(context); task->context =3D context; } =20 - data->task =3D task; data->worker =3D worker; data->opaque =3D opaque; data->destroy =3D destroy; @@ -159,7 +165,7 @@ void qio_task_run_in_thread(QIOTask *task, qemu_thread_create(&thread, "io-task-worker", qio_task_thread_worker, - data, + task, QEMU_THREAD_DETACHED); } =20 --=20 2.14.3 From nobody Wed Oct 22 04:13:49 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519794988712935.7964837282634; Tue, 27 Feb 2018 21:16:28 -0800 (PST) Received: from localhost ([::1]:42281 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1equ6N-0007D9-Gj for importer@patchew.org; Wed, 28 Feb 2018 00:16:27 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33722) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtxn-0008Pw-8N for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:36 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqtxl-0000sk-Sv for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:35 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47022 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 1eqtxl-0000sa-Ks for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:33 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 51B8A87ABA for ; Wed, 28 Feb 2018 05:07:33 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-95.pek2.redhat.com [10.72.12.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 13C952026E04; Wed, 28 Feb 2018 05:07:29 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 28 Feb 2018 13:06:32 +0800 Message-Id: <20180228050633.7410-14-peterx@redhat.com> In-Reply-To: <20180228050633.7410-1-peterx@redhat.com> References: <20180228050633.7410-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 28 Feb 2018 05:07:33 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 28 Feb 2018 05:07:33 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 13/14] qio: allow threaded qiotask to switch contexts 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: Juan Quintela , Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini 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" This is the part of work to allow the QIOTask to use a different gcontext rather than the default main gcontext, by providing qio_task_context_set() API. We have done some work before on doing similar things to add non-default gcontext support. The general idea is that we delete the old GSource from the main context, then re-add a new one to the new context when context changed to a non-default one. However this trick won't work easily for threaded QIOTasks since we can't easily stop a real thread and re-setup the whole thing from the very beginning. But luckily, we don't need to do anything to the thread. We just need to keep an eye on the GSource that completes the QIOTask, which is assigned to gcontext after the sync operation finished. So when we setup a non-default GMainContext for a threaded QIO task, we may face two cases: - the thread is still running the sync task: then we don't need to do anything, only to update QIOTask.context to the new context - the thread has finished the sync task and queued an idle task to main thread: then we destroy that old idle task, and re-create it on the new GMainContext. Note that along the way when we modify either idle GSource or the context, we need to take the mutex before hand, since the thread may be modifying them at the same time. Finally, call qio_task_context_set() in the tcp chardev update read handler hook if QIOTask is running. Signed-off-by: Peter Xu --- chardev/char-socket.c | 4 +++ include/io/task.h | 1 + io/task.c | 70 ++++++++++++++++++++++++++++++++++++++++++-----= ---- 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 9d51b8da07..164a64ff34 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -585,6 +585,10 @@ static void tcp_chr_update_read_handler(Chardev *chr) tcp_chr_telnet_init(CHARDEV(s)); } =20 + if (s->thread_task) { + qio_task_context_set(s->thread_task, chr->gcontext); + } + if (!s->connected) { return; } diff --git a/include/io/task.h b/include/io/task.h index c6acd6489c..87e0152d8a 100644 --- a/include/io/task.h +++ b/include/io/task.h @@ -324,5 +324,6 @@ Object *qio_task_get_source(QIOTask *task); =20 void qio_task_ref(QIOTask *task); void qio_task_unref(QIOTask *task); +void qio_task_context_set(QIOTask *task, GMainContext *context); =20 #endif /* QIO_TASK_H */ diff --git a/io/task.c b/io/task.c index 080f9560ea..59bc439bdf 100644 --- a/io/task.c +++ b/io/task.c @@ -42,6 +42,9 @@ struct QIOTask { uint32_t refcount; =20 /* Threaded QIO task specific fields */ + bool has_thread; + QemuThread thread; + QemuMutex mutex; /* Protects threaded QIO task fields */ GSource *idle_source; /* The idle task to run complete routine */ GMainContext *context; /* The context that idle task will run with */ QIOTaskThreadData thread_data; @@ -57,6 +60,8 @@ QIOTask *qio_task_new(Object *source, =20 task =3D g_new0(QIOTask, 1); =20 + qemu_mutex_init(&task->mutex); + task->source =3D source; object_ref(source); task->func =3D func; @@ -88,7 +93,16 @@ static void qio_task_free(QIOTask *task) if (task->context) { g_main_context_unref(task->context); } + /* + * Make sure the thread quitted before we destroy the mutex, + * otherwise the thread might still be using it. + */ + if (task->has_thread) { + qemu_thread_join(&task->thread); + } + object_unref(task->source); + qemu_mutex_destroy(&task->mutex); =20 g_free(task); } @@ -117,12 +131,28 @@ static gboolean qio_task_thread_result(gpointer opaqu= e) return FALSE; } =20 +/* Must be with QIOTask.mutex held. */ +static void qio_task_thread_create_complete_job(QIOTask *task) +{ + GSource *idle; + + /* Remove the old if there is */ + if (task->idle_source) { + g_source_destroy(task->idle_source); + g_source_unref(task->idle_source); + } + + idle =3D g_idle_source_new(); + g_source_set_callback(idle, qio_task_thread_result, task, NULL); + g_source_attach(idle, task->context); + + task->idle_source =3D idle; +} =20 static gpointer qio_task_thread_worker(gpointer opaque) { QIOTask *task =3D opaque; QIOTaskThreadData *data =3D &task->thread_data; - GSource *idle; =20 trace_qio_task_thread_run(task); data->worker(task, data->opaque); @@ -134,10 +164,9 @@ static gpointer qio_task_thread_worker(gpointer opaque) */ trace_qio_task_thread_exit(task); =20 - idle =3D g_idle_source_new(); - g_source_set_callback(idle, qio_task_thread_result, data, NULL); - g_source_attach(idle, task->context); - task->idle_source =3D idle; + qemu_mutex_lock(&task->mutex); + qio_task_thread_create_complete_job(task); + qemu_mutex_unlock(&task->mutex); =20 return NULL; } @@ -149,24 +178,21 @@ void qio_task_run_in_thread(QIOTask *task, GDestroyNotify destroy, GMainContext *context) { - QemuThread thread; QIOTaskThreadData *data =3D &task->thread_data; =20 - if (context) { - g_main_context_ref(context); - task->context =3D context; - } + qio_task_context_set(task, context); =20 data->worker =3D worker; data->opaque =3D opaque; data->destroy =3D destroy; =20 trace_qio_task_thread_start(task, worker, opaque); - qemu_thread_create(&thread, + qemu_thread_create(&task->thread, "io-task-worker", qio_task_thread_worker, task, - QEMU_THREAD_DETACHED); + QEMU_THREAD_JOINABLE); + task->has_thread =3D true; } =20 =20 @@ -235,3 +261,23 @@ void qio_task_unref(QIOTask *task) qio_task_free(task); } } + +void qio_task_context_set(QIOTask *task, GMainContext *context) +{ + qemu_mutex_lock(&task->mutex); + if (task->context) { + g_main_context_unref(task->context); + } + if (context) { + g_main_context_ref(task->context); + task->context =3D context; + } + if (task->idle_source) { + /* + * We have had an idle job on the old context. Firstly delete + * the old one, then create a new one on the new context. + */ + qio_task_thread_create_complete_job(task); + } + qemu_mutex_unlock(&task->mutex); +} --=20 2.14.3 From nobody Wed Oct 22 04:13:49 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519794755250737.6255446570358; Tue, 27 Feb 2018 21:12:35 -0800 (PST) Received: from localhost ([::1]:42255 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1equ2b-0003i2-QV for importer@patchew.org; Wed, 28 Feb 2018 00:12:33 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33757) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqtxs-0008V8-So for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqtxp-0000tt-MJ for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:40 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:37846 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 1eqtxp-0000tb-G7 for qemu-devel@nongnu.org; Wed, 28 Feb 2018 00:07:37 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 271E340744C6 for ; Wed, 28 Feb 2018 05:07:37 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-95.pek2.redhat.com [10.72.12.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id DBBBE2024CA6; Wed, 28 Feb 2018 05:07:33 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 28 Feb 2018 13:06:33 +0800 Message-Id: <20180228050633.7410-15-peterx@redhat.com> In-Reply-To: <20180228050633.7410-1-peterx@redhat.com> References: <20180228050633.7410-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Wed, 28 Feb 2018 05:07:37 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Wed, 28 Feb 2018 05:07:37 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 14/14] qio/chardev: specify gcontext for TLS handshake 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: Juan Quintela , Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini 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" We allow the TLS code to be run with non-default gcontext by providing a new qio_channel_tls_handshake_full() API. With the new API, we can re-setup the TLS handshake GSource by calling it again with the correct gcontext. Any call to the function will clean up existing GSource tasks, and re-setup using the new gcontext. Signed-off-by: Peter Xu --- chardev/char-socket.c | 30 +++++++++++++--- include/io/channel-tls.h | 22 +++++++++++- io/channel-tls.c | 91 ++++++++++++++++++++++++++++++++++++++++----= ---- 3 files changed, 123 insertions(+), 20 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 164a64ff34..406d33c04f 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -72,6 +72,9 @@ typedef struct { =20 static gboolean socket_reconnect_timeout(gpointer opaque); static void tcp_chr_telnet_init(Chardev *chr); +static void tcp_chr_tls_handshake_setup(Chardev *chr, + QIOChannelTLS *tioc, + GMainContext *context); =20 static void tcp_chr_reconn_timer_cancel(SocketChardev *s) { @@ -570,6 +573,7 @@ static void tcp_chr_telnet_destroy(SocketChardev *s) static void tcp_chr_update_read_handler(Chardev *chr) { SocketChardev *s =3D SOCKET_CHARDEV(chr); + QIOChannelTLS *tioc; =20 if (s->listener) { /* @@ -589,6 +593,17 @@ static void tcp_chr_update_read_handler(Chardev *chr) qio_task_context_set(s->thread_task, chr->gcontext); } =20 + tioc =3D (QIOChannelTLS *)object_dynamic_cast(OBJECT(s->ioc), + TYPE_QIO_CHANNEL_TLS); + if (tioc) { + /* + * TLS session enabled; reconfigure things up. Note that, if + * there is existing handshake task, it'll be cleaned up first + * in QIO code. + */ + tcp_chr_tls_handshake_setup(chr, tioc, chr->gcontext); + } + if (!s->connected) { return; } @@ -704,6 +719,16 @@ static void tcp_chr_tls_handshake(QIOTask *task, } } =20 +static void tcp_chr_tls_handshake_setup(Chardev *chr, + QIOChannelTLS *tioc, + GMainContext *context) +{ + qio_channel_tls_handshake_full(tioc, + tcp_chr_tls_handshake, + chr, + NULL, + context); +} =20 static void tcp_chr_tls_init(Chardev *chr) { @@ -736,10 +761,7 @@ static void tcp_chr_tls_init(Chardev *chr) object_unref(OBJECT(s->ioc)); s->ioc =3D QIO_CHANNEL(tioc); =20 - qio_channel_tls_handshake(tioc, - tcp_chr_tls_handshake, - chr, - NULL); + tcp_chr_tls_handshake_setup(chr, tioc, NULL); } =20 =20 diff --git a/include/io/channel-tls.h b/include/io/channel-tls.h index d157eb10e8..98b7cd1e51 100644 --- a/include/io/channel-tls.h +++ b/include/io/channel-tls.h @@ -48,6 +48,9 @@ struct QIOChannelTLS { QIOChannel parent; QIOChannel *master; QCryptoTLSSession *session; + GMainContext *context; + GSource *tls_source; + QIOTask *task; }; =20 /** @@ -111,11 +114,12 @@ qio_channel_tls_new_client(QIOChannel *master, Error **errp); =20 /** - * qio_channel_tls_handshake: + * qio_channel_tls_handshake_full: * @ioc: the TLS channel object * @func: the callback to invoke when completed * @opaque: opaque data to pass to @func * @destroy: optional callback to free @opaque + * @context: the context that will run the handshake task * * Perform the TLS session handshake. This method * will return immediately and the handshake will @@ -123,6 +127,22 @@ qio_channel_tls_new_client(QIOChannel *master, * loop is running. When the handshake is complete, * or fails, the @func callback will be invoked. */ +void qio_channel_tls_handshake_full(QIOChannelTLS *ioc, + QIOTaskFunc func, + gpointer opaque, + GDestroyNotify destroy, + GMainContext *context); + +/** + * qio_channel_tls_handshake: + * @ioc: the TLS channel object + * @func: the callback to invoke when completed + * @opaque: opaque data to pass to @func + * @destroy: optional callback to free @opaque + * + * Wrapper of qio_channel_tls_handshake_full(), only that we are + * running the handshake always on default main context. + */ void qio_channel_tls_handshake(QIOChannelTLS *ioc, QIOTaskFunc func, gpointer opaque, diff --git a/io/channel-tls.c b/io/channel-tls.c index 6182702dab..b173680526 100644 --- a/io/channel-tls.c +++ b/io/channel-tls.c @@ -145,8 +145,12 @@ static gboolean qio_channel_tls_handshake_io(QIOChanne= l *ioc, GIOCondition condition, gpointer user_data); =20 -static void qio_channel_tls_handshake_task(QIOChannelTLS *ioc, - QIOTask *task) +/* + * Returns NULL if handshake completed, or a GSource pointer of the + * pending handshake task to be executed. + */ +static GSource *qio_channel_tls_handshake_task(QIOChannelTLS *ioc, + QIOTask *task) { Error *err =3D NULL; QCryptoTLSSessionHandshakeStatus status; @@ -155,7 +159,7 @@ static void qio_channel_tls_handshake_task(QIOChannelTL= S *ioc, trace_qio_channel_tls_handshake_fail(ioc); qio_task_set_error(task, err); qio_task_complete(task); - return; + return NULL; } =20 status =3D qcrypto_tls_session_get_handshake_status(ioc->session); @@ -169,6 +173,7 @@ static void qio_channel_tls_handshake_task(QIOChannelTL= S *ioc, trace_qio_channel_tls_credentials_allow(ioc); } qio_task_complete(task); + return NULL; } else { GIOCondition condition; if (status =3D=3D QCRYPTO_TLS_HANDSHAKE_SENDING) { @@ -178,14 +183,36 @@ static void qio_channel_tls_handshake_task(QIOChannel= TLS *ioc, } =20 trace_qio_channel_tls_handshake_pending(ioc, status); - qio_channel_add_watch(ioc->master, - condition, - qio_channel_tls_handshake_io, - task, - NULL); + return qio_channel_add_watch_full(ioc->master, + condition, + qio_channel_tls_handshake_io, + task, + NULL, + ioc->context); + } +} + +static void qio_channel_tls_context_set(QIOChannelTLS *ioc, + GMainContext *context) +{ + if (ioc->context) { + g_main_context_unref(ioc->context); + ioc->context =3D NULL; + } + if (context) { + g_main_context_ref(context); + ioc->context =3D context; } } =20 +static void qio_channel_tls_source_destroy(QIOChannelTLS *ioc) +{ + if (ioc->tls_source) { + g_source_destroy(ioc->tls_source); + g_source_unref(ioc->tls_source); + ioc->tls_source =3D NULL; + } +} =20 static gboolean qio_channel_tls_handshake_io(QIOChannel *ioc, GIOCondition condition, @@ -194,27 +221,59 @@ static gboolean qio_channel_tls_handshake_io(QIOChann= el *ioc, QIOTask *task =3D user_data; QIOChannelTLS *tioc =3D QIO_CHANNEL_TLS( qio_task_get_source(task)); + GSource *source; =20 - qio_channel_tls_handshake_task( - tioc, task); + source =3D qio_channel_tls_handshake_task(tioc, task); + /* Release existing GSource and cache the new one */ + g_source_unref(tioc->tls_source); + tioc->tls_source =3D source; =20 return FALSE; } =20 -void qio_channel_tls_handshake(QIOChannelTLS *ioc, - QIOTaskFunc func, - gpointer opaque, - GDestroyNotify destroy) +static void qio_channel_tls_cleanup(QIOChannelTLS *ioc) +{ + if (ioc->task) { + qio_task_unref(ioc->task); + ioc->task =3D NULL; + } + + qio_channel_tls_source_destroy(ioc); + qio_channel_tls_context_set(ioc, NULL); +} + +void qio_channel_tls_handshake_full(QIOChannelTLS *ioc, + QIOTaskFunc func, + gpointer opaque, + GDestroyNotify destroy, + GMainContext *context) { QIOTask *task; + GSource *source; + + /* Drop existing tasks if there is */ + qio_channel_tls_cleanup(ioc); =20 task =3D qio_task_new(OBJECT(ioc), func, opaque, destroy); + qio_task_ref(ioc->task); + ioc->task =3D task; =20 trace_qio_channel_tls_handshake_start(ioc); - qio_channel_tls_handshake_task(ioc, task); + + assert(ioc->tls_source =3D=3D NULL); + qio_channel_tls_context_set(ioc, context); + source =3D qio_channel_tls_handshake_task(ioc, task); + ioc->tls_source =3D source; } =20 +void qio_channel_tls_handshake(QIOChannelTLS *ioc, + QIOTaskFunc func, + gpointer opaque, + GDestroyNotify destroy) +{ + qio_channel_tls_handshake_full(ioc, func, opaque, destroy, NULL); +} =20 static void qio_channel_tls_init(Object *obj G_GNUC_UNUSED) { @@ -225,6 +284,8 @@ static void qio_channel_tls_finalize(Object *obj) { QIOChannelTLS *ioc =3D QIO_CHANNEL_TLS(obj); =20 + qio_channel_tls_source_destroy(ioc); + qio_channel_tls_context_set(ioc, NULL); object_unref(OBJECT(ioc->master)); qcrypto_tls_session_free(ioc->session); } --=20 2.14.3