From nobody Sun Nov 2 11:43:25 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1528114077292530.6028415898121; Mon, 4 Jun 2018 05:07:57 -0700 (PDT) Received: from localhost ([::1]:39382 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fPoHA-0007NW-6J for importer@patchew.org; Mon, 04 Jun 2018 08:07:52 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46269) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fPoDI-0004Xi-K7 for qemu-devel@nongnu.org; Mon, 04 Jun 2018 08:03:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fPoDH-0001Fw-Da for qemu-devel@nongnu.org; Mon, 04 Jun 2018 08:03:52 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:33078 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 1fPoDH-0001Fg-8V for qemu-devel@nongnu.org; Mon, 04 Jun 2018 08:03:51 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D08AE808255C for ; Mon, 4 Jun 2018 12:03:50 +0000 (UTC) Received: from t460.redhat.com (unknown [10.33.36.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id D7B3163F30; Mon, 4 Jun 2018 12:03:48 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Mon, 4 Jun 2018 13:03:44 +0100 Message-Id: <20180604120345.12955-2-berrange@redhat.com> In-Reply-To: <20180604120345.12955-1-berrange@redhat.com> References: <20180604120345.12955-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Mon, 04 Jun 2018 12:03:50 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Mon, 04 Jun 2018 12:03:50 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'berrange@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 v2 1/2] vl: don't use RUN_STATE_PRECONFIG as initial state 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: Michal Privoznik , Markus Armbruster , Max Reitz , Igor Mammedov , Paolo Bonzini 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" The RUN_STATE_PRECONFIG state is not supposed to be reachable unless the --preconfig argument is given to QEMU, but when it was introduced in: commit 047f7038f586d2150f16c6d9ba9cfd0479f0f6ac Author: Igor Mammedov Date: Fri May 11 19:24:43 2018 +0200 cli: add --preconfig option ...the global 'current_run_state' variable was changed to have an initial value of RUN_STATE_PRECONFIG regardless of whether --preconfig is given. A second invokation of main_loop() was added which then toggles it back to RUN_STATE_PRELAUNCH when --preconfig is not given. This is racy because it leaves a window where QEMU is in RUN_STATE_PRECONFIG despite --preconfig not being given. This can be seen with the failure: $ echo | x86_64-softmmu/qemu-system-x86_64 -monitor stdio QEMU 2.12.50 monitor - type 'help' for more information (qemu) HMP not available in preconfig state, use QMP instead Using RUN_STATE_PRECONFIG required adding a state transition from RUN_STATE_PRECONFIG to RUN_STATE_INMIGRATE, which is undesirable as it prevented automatic detection of --preconfig & --incoming being mutually exclusive. If we use RUN_STATE_PRELAUNCH as the initial value, however, we need to allow transitions between RUN_STATE_PRECONFIG & RUN_STATE_PRELAUNCH in both directions which is also undesirable, as RUN_STATE_PRECONFIG should be a one-time-only state that can never be returned to. This change solves the confusion by introducing a further RUN_STATE_NONE which is used as the initial state value. This can transition to any of RUN_STATE_PRELAUNCH, RUN_STATE_PRECONFIG or RUN_STATE_INMIGRATE. This avoids the need to allow any undesirable state transitions. Signed-off-by: Daniel P. Berrang=C3=A9 --- qapi/run-state.json | 6 +++++- vl.c | 42 ++++++++++++++++++++++++------------------ 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/qapi/run-state.json b/qapi/run-state.json index 332e44897b..c3bd7b9b7a 100644 --- a/qapi/run-state.json +++ b/qapi/run-state.json @@ -10,6 +10,10 @@ # # An enumeration of VM run states. # +# @none: QEMU is in early startup. This state should never be visible +# when querying state from the monitor, as QEMU will have already +# transitioned to another state. (Since 3.0) +# # @debug: QEMU is running on a debugger # # @finish-migrate: guest is paused to finish the migration process @@ -54,7 +58,7 @@ # (Since 3.0) ## { 'enum': 'RunState', - 'data': [ 'debug', 'inmigrate', 'internal-error', 'io-error', 'paused', + 'data': [ 'none', 'debug', 'inmigrate', 'internal-error', 'io-error', 'p= aused', 'postmigrate', 'prelaunch', 'finish-migrate', 'restore-vm', 'running', 'save-vm', 'shutdown', 'suspended', 'watchdog', 'guest-panicked', 'colo', 'preconfig' ] } diff --git a/vl.c b/vl.c index 06031715ac..30d0e985f0 100644 --- a/vl.c +++ b/vl.c @@ -561,7 +561,7 @@ static int default_driver_check(void *opaque, QemuOpts = *opts, Error **errp) /***********************************************************/ /* QEMU state */ =20 -static RunState current_run_state =3D RUN_STATE_PRECONFIG; +static RunState current_run_state =3D RUN_STATE_NONE; =20 /* We use RUN_STATE__MAX but any invalid value will do */ static RunState vmstop_requested =3D RUN_STATE__MAX; @@ -574,12 +574,11 @@ typedef struct { =20 static const RunStateTransition runstate_transitions_def[] =3D { /* from -> to */ + { RUN_STATE_NONE, RUN_STATE_PRELAUNCH }, + { RUN_STATE_NONE, RUN_STATE_PRECONFIG }, + { RUN_STATE_NONE, RUN_STATE_INMIGRATE }, + { RUN_STATE_PRECONFIG, RUN_STATE_PRELAUNCH }, - /* Early switch to inmigrate state to allow -incoming CLI option wo= rk - * as it used to. TODO: delay actual switching to inmigrate state to - * the point after machine is built and remove this hack. - */ - { RUN_STATE_PRECONFIG, RUN_STATE_INMIGRATE }, =20 { RUN_STATE_DEBUG, RUN_STATE_RUNNING }, { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE }, @@ -618,7 +617,6 @@ static const RunStateTransition runstate_transitions_de= f[] =3D { =20 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING }, { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE }, - { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE }, =20 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING }, { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED }, @@ -1522,7 +1520,7 @@ static pid_t shutdown_pid; static int powerdown_requested; static int debug_requested; static int suspend_requested; -static bool preconfig_exit_requested =3D true; +static bool preconfig_exit_requested; static WakeupReason wakeup_reason; static NotifierList powerdown_notifiers =3D NOTIFIER_LIST_INITIALIZER(powerdown_notifiers); @@ -3572,7 +3570,12 @@ int main(int argc, char **argv, char **envp) } break; case QEMU_OPTION_preconfig: - preconfig_exit_requested =3D false; + if (!runstate_check(RUN_STATE_NONE)) { + error_report("'--preconfig' and '--incoming' options a= re " + "mutually exclusive"); + exit(EXIT_FAILURE); + } + runstate_set(RUN_STATE_PRECONFIG); break; case QEMU_OPTION_enable_kvm: olist =3D qemu_find_opts("machine"); @@ -3768,9 +3771,12 @@ int main(int argc, char **argv, char **envp) } break; case QEMU_OPTION_incoming: - if (!incoming) { - runstate_set(RUN_STATE_INMIGRATE); + if (!runstate_check(RUN_STATE_NONE)) { + error_report("'--preconfig' and '--incoming' options a= re " + "mutually exclusive"); + exit(EXIT_FAILURE); } + runstate_set(RUN_STATE_INMIGRATE); incoming =3D optarg; break; case QEMU_OPTION_only_migratable: @@ -3943,14 +3949,12 @@ int main(int argc, char **argv, char **envp) */ loc_set_none(); =20 - replay_configure(icount_opts); - - if (incoming && !preconfig_exit_requested) { - error_report("'preconfig' and 'incoming' options are " - "mutually exclusive"); - exit(EXIT_FAILURE); + if (runstate_check(RUN_STATE_NONE)) { + runstate_set(RUN_STATE_PRELAUNCH); } =20 + replay_configure(icount_opts); + machine_class =3D select_machine(); =20 set_memory_options(&ram_slots, &maxram_size, machine_class); @@ -4471,7 +4475,9 @@ int main(int argc, char **argv, char **envp) parse_numa_opts(current_machine); =20 /* do monitor/qmp handling at preconfig state if requested */ - main_loop(); + if (runstate_check(RUN_STATE_PRECONFIG)) { + main_loop(); + } =20 /* from here on runstate is RUN_STATE_PRELAUNCH */ machine_run_board_init(current_machine); --=20 2.17.0 From nobody Sun Nov 2 11:43:25 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1528113929747474.94770800563265; Mon, 4 Jun 2018 05:05:29 -0700 (PDT) Received: from localhost ([::1]:39369 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fPoEr-0005FG-2P for importer@patchew.org; Mon, 04 Jun 2018 08:05:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46282) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fPoDK-0004Y2-C6 for qemu-devel@nongnu.org; Mon, 04 Jun 2018 08:03:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fPoDJ-0001IB-HD for qemu-devel@nongnu.org; Mon, 04 Jun 2018 08:03:54 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:41552 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 1fPoDJ-0001Ha-Cg for qemu-devel@nongnu.org; Mon, 04 Jun 2018 08:03:53 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F2A53BB40A for ; Mon, 4 Jun 2018 12:03:52 +0000 (UTC) Received: from t460.redhat.com (unknown [10.33.36.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2610163F30; Mon, 4 Jun 2018 12:03:51 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Mon, 4 Jun 2018 13:03:45 +0100 Message-Id: <20180604120345.12955-3-berrange@redhat.com> In-Reply-To: <20180604120345.12955-1-berrange@redhat.com> References: <20180604120345.12955-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Mon, 04 Jun 2018 12:03:53 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Mon, 04 Jun 2018 12:03:53 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'berrange@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 v2 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: Michal Privoznik , Markus Armbruster , Max Reitz , Igor Mammedov , Paolo Bonzini 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() 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. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- vl.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/vl.c b/vl.c index 30d0e985f0..f4bba36d19 100644 --- a/vl.c +++ b/vl.c @@ -2921,6 +2921,7 @@ int main(int argc, char **argv, char **envp) Error *err =3D NULL; bool list_data_dirs =3D false; char *dir, **dirs; + bool os_setup_post_done =3D false; typedef struct BlockdevOptions_queue { BlockdevOptions *bdo; Location loc; @@ -4476,6 +4477,8 @@ int main(int argc, char **argv, char **envp) =20 /* do monitor/qmp handling at preconfig state if requested */ if (runstate_check(RUN_STATE_PRECONFIG)) { + os_setup_post(); + os_setup_post_done =3D true; main_loop(); } =20 @@ -4606,7 +4609,9 @@ int main(int argc, char **argv, char **envp) } =20 accel_setup_post(current_machine); - os_setup_post(); + if (!os_setup_post_done) { + os_setup_post(); + } =20 main_loop(); =20 --=20 2.17.0