From nobody Sun Apr 28 14:57:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1528207519329516.2960653904792; Tue, 5 Jun 2018 07:05:19 -0700 (PDT) Received: from localhost ([::1]:46985 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fQCaM-000301-6c for importer@patchew.org; Tue, 05 Jun 2018 10:05:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36170) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fQCWf-0000d0-QO for qemu-devel@nongnu.org; Tue, 05 Jun 2018 10:01:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fQCWb-0002SR-9g for qemu-devel@nongnu.org; Tue, 05 Jun 2018 10:01:27 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:37920 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 1fQCWb-0002S9-4n for qemu-devel@nongnu.org; Tue, 05 Jun 2018 10:01:25 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A13444023834 for ; Tue, 5 Jun 2018 14:01:24 +0000 (UTC) Received: from dell-r430-03.lab.eng.brq.redhat.com (dell-r430-03.lab.eng.brq.redhat.com [10.37.153.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9F0A310F1C18; Tue, 5 Jun 2018 14:01:23 +0000 (UTC) From: Igor Mammedov To: qemu-devel@nongnu.org Date: Tue, 5 Jun 2018 16:00:42 +0200 Message-Id: <1528207243-268226-2-git-send-email-imammedo@redhat.com> In-Reply-To: <1528207243-268226-1-git-send-email-imammedo@redhat.com> References: <1528207243-268226-1-git-send-email-imammedo@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 05 Jun 2018 14:01:24 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 05 Jun 2018 14:01:24 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'imammedo@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 v3 1/2] cli: Don't run early event loop if no --preconfig was specified 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: ldoktor@redhat.com, pbonzini@redhat.com, ehabkost@redhat.com, mreitz@redhat.com 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" After 047f7038f586d215 it is possible for event loop to run two times. First time whilst parsing command line options (the idea is to bring up monitor early so that management applications can tweak config before machine is initialized). And the second time is after everything is set up (this is the usual place). In both cases the event loop is called as main_loop_wait(nonblocking =3D false) which causes the event loop to block until at least one event occurred. Now, consider that somebody (i.e. libvirt) calls us with -daemonize. This operation is split in two steps. The main() calls os_daemonize() which fork()-s and then waits in read() until child notifies it via write(): /qemu.git $ ./x86_64-softmmu/qemu-system-x86_64 -S -daemonize \ -no-user-config -nodefaults -nographic main(): child: os_daemonize(): read(pipe[0]) main_loop(): main_loop_wait(false) os_setup_post(): write(pipe[1]) main_loop(): main_loop_wait(false) Here it can be clearly seen that main() does not exit until an event occurs, but at the same time nobody will touch the monitor socket until their exec("qemu-system-*") finishes. So the whole thing deadlocks. The solution is to not call main_loop_wait() unless --preconfig was specified (in which case caller knows they must connect to the socket before exec() finishes). Patch also fixes hang when -nodefaults option is used, which were causing QEMU hang in the early main_loop_wait() indefinitely by the same means (not calling main_loop_wait() unless --preconfig is present on CLI) Based on From: Michal Privoznik Subject: [PATCH] cli: Don't run early event loop if no --preconfig was sp= ecified Message-Id: Signed-off-by: Igor Mammedov --- v2: - fix iotests that involve migration, rewrite v1 so it would take into account -incoming use case --- vl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vl.c b/vl.c index c4fe255..fa44138 100644 --- a/vl.c +++ b/vl.c @@ -1956,7 +1956,7 @@ static void main_loop(void) #ifdef CONFIG_PROFILER int64_t ti; #endif - do { + while (!main_loop_should_exit()) { #ifdef CONFIG_PROFILER ti =3D profile_getclock(); #endif @@ -1964,7 +1964,7 @@ static void main_loop(void) #ifdef CONFIG_PROFILER dev_time +=3D profile_getclock() - ti; #endif - } while (!main_loop_should_exit()); + } } =20 static void version(void) --=20 2.7.4 From nobody Sun Apr 28 14:57:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1528207418952841.7868091114752; Tue, 5 Jun 2018 07:03:38 -0700 (PDT) Received: from localhost ([::1]:46976 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fQCYj-0001da-EZ for importer@patchew.org; Tue, 05 Jun 2018 10:03:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36208) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fQCWl-0000hv-0M for qemu-devel@nongnu.org; Tue, 05 Jun 2018 10:01:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fQCWf-0002VK-3c for qemu-devel@nongnu.org; Tue, 05 Jun 2018 10:01:35 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:46972 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 1fQCWe-0002Uk-V8 for qemu-devel@nongnu.org; Tue, 05 Jun 2018 10:01:29 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7809C818BAEE for ; Tue, 5 Jun 2018 14:01:28 +0000 (UTC) Received: from dell-r430-03.lab.eng.brq.redhat.com (dell-r430-03.lab.eng.brq.redhat.com [10.37.153.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id D537010F1C18; Tue, 5 Jun 2018 14:01:24 +0000 (UTC) From: Igor Mammedov To: qemu-devel@nongnu.org Date: Tue, 5 Jun 2018 16:00:43 +0200 Message-Id: <1528207243-268226-3-git-send-email-imammedo@redhat.com> In-Reply-To: <1528207243-268226-1-git-send-email-imammedo@redhat.com> References: <1528207243-268226-1-git-send-email-imammedo@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 05 Jun 2018 14:01:28 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 05 Jun 2018 14:01:28 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'imammedo@redhat.com' RCPT:'' Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v3 2/2] vl: fix use of --daemonize with --preconfig 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: ldoktor@redhat.com, pbonzini@redhat.com, ehabkost@redhat.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" When using --daemonize, the initial lead process will fork a child and then wait to be notified that setup is complete via a pipe, before it exits. When using --preconfig there is an extra call to main_loop() before the notification is done from os_setup_post(). Thus the parent process won't exit until the mgmt application connects to the monitor and tells QEMU to leave the RUN_STATE_PRECONFIG. The mgmt application won't connect to the monitor until daemonizing has completed though. This is a chicken and egg problem, leading to deadlock at startup. The only viable way to fix this is to call os_setup_post() before the early main_loop_wait() call when in RUN_STATE_PRECONFIG. This has the downside that any errors from this point onwards won't be handled well by the mgmt application, because it will think QEMU has started successfully, so not be expecting an abrupt exit. The only way to deal with that is to move as much user input validation as possible to before the main_loop() call. This is left as an exercise for future interested developers. Based on: From: Daniel P. Berrang=C3=A9 Subject: [PATCH v2 2/2] vl: fix use of --daemonize with --preconfig Message-Id: <20180604120345.12955-3-berrange@redhat.com> Signed-off-by: Igor Mammedov --- v3: - rewrite to apply on top of 1/2 --- os-posix.c | 6 ++++++ vl.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/os-posix.c b/os-posix.c index 9ce6f74..ee06a8d 100644 --- a/os-posix.c +++ b/os-posix.c @@ -309,8 +309,14 @@ void os_daemonize(void) =20 void os_setup_post(void) { + static bool os_setup_post_done =3D false; int fd =3D 0; =20 + if (os_setup_post_done) { + return; + } + os_setup_post_done =3D true; + if (daemonize) { if (chdir("/")) { error_report("not able to chdir to /: %s", strerror(errno)); diff --git a/vl.c b/vl.c index fa44138..d6fa67f 100644 --- a/vl.c +++ b/vl.c @@ -1960,6 +1960,7 @@ static void main_loop(void) #ifdef CONFIG_PROFILER ti =3D profile_getclock(); #endif + os_setup_post(); main_loop_wait(false); #ifdef CONFIG_PROFILER dev_time +=3D profile_getclock() - ti; @@ -4707,7 +4708,6 @@ int main(int argc, char **argv, char **envp) } =20 accel_setup_post(current_machine); - os_setup_post(); =20 main_loop(); =20 --=20 2.7.4