From patchwork Fri Sep 28 07:58:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfgang Bumiller X-Patchwork-Id: 10619097 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AC6A114BD for ; Fri, 28 Sep 2018 07:59:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A5D762AD77 for ; Fri, 28 Sep 2018 07:59:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 997452AD89; Fri, 28 Sep 2018 07:59:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 194A32AD77 for ; Fri, 28 Sep 2018 07:59:29 +0000 (UTC) Received: from localhost ([::1]:41869 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5ngN-0003QY-UE for patchwork-qemu-devel@patchwork.kernel.org; Fri, 28 Sep 2018 03:59:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50098) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5nfe-0002oE-HR for qemu-devel@nongnu.org; Fri, 28 Sep 2018 03:58:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g5nfb-0002Kc-3Q for qemu-devel@nongnu.org; Fri, 28 Sep 2018 03:58:42 -0400 Received: from proxmox-new.maurer-it.com ([212.186.127.180]:42353) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g5nfa-0002GY-R2 for qemu-devel@nongnu.org; Fri, 28 Sep 2018 03:58:39 -0400 Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 4D8F24234B; Fri, 28 Sep 2018 09:58:33 +0200 (CEST) From: Wolfgang Bumiller To: qemu-devel@nongnu.org Date: Fri, 28 Sep 2018 09:58:31 +0200 Message-Id: <20180928075832.18586-2-w.bumiller@proxmox.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180928075832.18586-1-w.bumiller@proxmox.com> References: <20180928075832.18586-1-w.bumiller@proxmox.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 212.186.127.180 Subject: [Qemu-devel] [PATCH v2 1/2] monitor: guard iothread access by mon->use_io_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: Markus Armbruster , Peter Xu , "Dr. David Alan Gilbert" Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP monitor_resume() and monitor_suspend() both want to "kick" the I/O thread if it is there, but in monitor_suspend() lacked the use_io_thread flag condition. This is required when we later only spawn the thread on first use. Signed-off-by: Wolfgang Bumiller Reviewed-by: Eric Blake Reviewed-by: Peter Xu --- No changes in this patch. monitor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monitor.c b/monitor.c index c4677b502b..d47e4259fd 100644 --- a/monitor.c +++ b/monitor.c @@ -4285,7 +4285,7 @@ int monitor_suspend(Monitor *mon) atomic_inc(&mon->suspend_cnt); - if (monitor_is_qmp(mon)) { + if (monitor_is_qmp(mon) && mon->use_io_thread) { /* * Kick I/O thread to make sure this takes effect. It'll be * evaluated again in prepare() of the watch object. From patchwork Fri Sep 28 07:58:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfgang Bumiller X-Patchwork-Id: 10619101 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EE77A14BD for ; Fri, 28 Sep 2018 08:02:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E40F52ADE0 for ; Fri, 28 Sep 2018 08:02:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D817F2AE0F; Fri, 28 Sep 2018 08:02:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 374382ADE0 for ; Fri, 28 Sep 2018 08:02:34 +0000 (UTC) Received: from localhost ([::1]:41885 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5njN-0006RT-Iv for patchwork-qemu-devel@patchwork.kernel.org; Fri, 28 Sep 2018 04:02:33 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50100) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5nfe-0002oF-IY for qemu-devel@nongnu.org; Fri, 28 Sep 2018 03:58:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g5nfb-0002Kt-8P for qemu-devel@nongnu.org; Fri, 28 Sep 2018 03:58:42 -0400 Received: from proxmox-new.maurer-it.com ([212.186.127.180]:34743) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g5nfa-0002Gv-Rk for qemu-devel@nongnu.org; Fri, 28 Sep 2018 03:58:39 -0400 Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id C6099417D7; Fri, 28 Sep 2018 09:58:33 +0200 (CEST) From: Wolfgang Bumiller To: qemu-devel@nongnu.org Date: Fri, 28 Sep 2018 09:58:32 +0200 Message-Id: <20180928075832.18586-3-w.bumiller@proxmox.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180928075832.18586-1-w.bumiller@proxmox.com> References: <20180928075832.18586-1-w.bumiller@proxmox.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 212.186.127.180 Subject: [Qemu-devel] [PATCH v2 2/2] monitor: delay monitor iothread creation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Markus Armbruster , Peter Xu , "Dr. David Alan Gilbert" Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Commit d32749deb615 moved the call to monitor_init_globals() to before os_daemonize(), making it an unsuitable place to spawn the monitor iothread as it won't be inherited over the fork() in os_daemonize(). We now spawn the thread the first time we instantiate a monitor which actually has use_io_thread == true. Therefore mon_iothread initialization is protected by monitor_lock. We still need to create the qmp_dispatcher_bh when not using iothreads, so this now still happens via monitor_init_globals(). Signed-off-by: Wolfgang Bumiller Fixes: d32749deb615 ("monitor: move init global earlier") --- Changes to v1: - move mon_iothread declaration down to monitor_lock's declaration (updating monitor_lock's coverage comment) - in monitor_data_init() assert() that mon_iothread is not NULL or not used instead of initializing it there, as its usage pattern is so that it is a initialized once before being used, or never used at all. - in monitor_iothread_init(), protect mon_iothread initialization with monitor_lock - in monitor_init(): run monitor_ithread_init() in the `use_oob` branch. Note that I currently also test for mon_iothread being NULL there, which we could leave this out as spawning new monitors isn't something that happens a lot, but I like the idea of avoiding taking a lock when not required. Otherwise, I can send a v3 with this removed. monitor.c | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/monitor.c b/monitor.c index d47e4259fd..870584a548 100644 --- a/monitor.c +++ b/monitor.c @@ -239,9 +239,6 @@ struct Monitor { int mux_out; }; -/* Shared monitor I/O thread */ -IOThread *mon_iothread; - /* Bottom half to dispatch the requests received from I/O thread */ QEMUBH *qmp_dispatcher_bh; @@ -262,10 +259,11 @@ typedef struct QMPRequest QMPRequest; /* QMP checker flags */ #define QMP_ACCEPT_UNKNOWNS 1 -/* Protects mon_list, monitor_qapi_event_state. */ +/* Protects mon_list, monitor_qapi_event_state and mon_iothread. */ static QemuMutex monitor_lock; static GHashTable *monitor_qapi_event_state; static QTAILQ_HEAD(mon_list, Monitor) mon_list; +IOThread *mon_iothread; /* Shared monitor I/O thread */ /* Protects mon_fdsets */ static QemuMutex mon_fdsets_lock; @@ -710,6 +708,7 @@ static void handle_hmp_command(Monitor *mon, const char *cmdline); static void monitor_data_init(Monitor *mon, bool skip_flush, bool use_io_thread) { + assert(!use_io_thread || mon_iothread); memset(mon, 0, sizeof(Monitor)); qemu_mutex_init(&mon->mon_lock); qemu_mutex_init(&mon->qmp.qmp_queue_lock); @@ -4453,16 +4452,11 @@ static AioContext *monitor_get_aio_context(void) static void monitor_iothread_init(void) { - mon_iothread = iothread_create("mon_iothread", &error_abort); - - /* - * The dispatcher BH must run in the main loop thread, since we - * have commands assuming that context. It would be nice to get - * rid of those assumptions. - */ - qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(), - monitor_qmp_bh_dispatcher, - NULL); + qemu_mutex_lock(&monitor_lock); + if (!mon_iothread) { + mon_iothread = iothread_create("mon_iothread", &error_abort); + } + qemu_mutex_unlock(&monitor_lock); } void monitor_init_globals(void) @@ -4472,7 +4466,15 @@ void monitor_init_globals(void) sortcmdlist(); qemu_mutex_init(&monitor_lock); qemu_mutex_init(&mon_fdsets_lock); - monitor_iothread_init(); + + /* + * The dispatcher BH must run in the main loop thread, since we + * have commands assuming that context. It would be nice to get + * rid of those assumptions. + */ + qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(), + monitor_qmp_bh_dispatcher, + NULL); } /* These functions just adapt the readline interface in a typesafe way. We @@ -4535,6 +4537,9 @@ static void monitor_qmp_setup_handlers_bh(void *opaque) monitor_list_append(mon); } +/* + * This expects to be run in the main thread. + */ void monitor_init(Chardev *chr, int flags) { Monitor *mon = g_malloc(sizeof(*mon)); @@ -4551,6 +4556,9 @@ void monitor_init(Chardev *chr, int flags) error_report("Monitor out-of-band is only supported by QMP"); exit(1); } + if (!mon_iothread) { + monitor_iothread_init(); + } } monitor_data_init(mon, false, use_oob); @@ -4607,7 +4615,9 @@ void monitor_cleanup(void) * we need to unregister from chardev below in * monitor_data_destroy(), and chardev is not thread-safe yet */ - iothread_stop(mon_iothread); + if (mon_iothread) { + iothread_stop(mon_iothread); + } /* Flush output buffers and destroy monitors */ qemu_mutex_lock(&monitor_lock); @@ -4622,9 +4632,10 @@ void monitor_cleanup(void) /* QEMUBHs needs to be deleted before destroying the I/O thread */ qemu_bh_delete(qmp_dispatcher_bh); qmp_dispatcher_bh = NULL; - - iothread_destroy(mon_iothread); - mon_iothread = NULL; + if (mon_iothread) { + iothread_destroy(mon_iothread); + mon_iothread = NULL; + } } QemuOptsList qemu_mon_opts = {