All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kurz <groug@kaod.org>
To: qemu-devel@nongnu.org
Cc: peter.maydell@linaro.org, "Thomas Huth" <thuth@redhat.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Eduardo Habkost" <ehabkost@redhat.com>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Greg Kurz" <groug@kaod.org>,
	"Paolo Bonzini" <pbonzini@redhat.com>
Subject: [PATCH 2/2] cpu/core: Fix "help" of CPU core device types
Date: Fri,  9 Apr 2021 18:03:39 +0200	[thread overview]
Message-ID: <20210409160339.500167-3-groug@kaod.org> (raw)
In-Reply-To: <20210409160339.500167-1-groug@kaod.org>

Calling qdev_get_machine() from a QOM instance_init function is
fragile because we can't be sure the machine object actually
exists. And this happens to break when passing ",help" on the
command line to get the list of properties for a CPU core
device types :

$ ./qemu-system-ppc64 -device power8_v2.0-spapr-cpu-core,help
qemu-system-ppc64: ../../hw/core/machine.c:1290:
 qdev_get_machine: Assertion `machine != NULL' failed.
Aborted (core dumped)

This used to work before QEMU 5.0, but commit 3df261b6676b
unwillingly introduced a subtle regression : the above command
line needs to create an instance but the instance_init function
of the base class calls qdev_get_machine() before
qemu_create_machine() has been called, which is a programming bug.

Use current_machine instead. It is okay to skip the setting of
nr_thread in this case since only its type is displayed.

Reported-by: Thomas Huth <thuth@redhat.com>
Fixes: 3df261b6676b ("softmmu/vl.c: Handle '-cpu help' and '-device help' before 'no default machine'")
Cc: peter.maydell@linaro.org
Signed-off-by: Greg Kurz <groug@kaod.org>
---
 hw/cpu/core.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/hw/cpu/core.c b/hw/cpu/core.c
index 92d3b2fbad62..987607515574 100644
--- a/hw/cpu/core.c
+++ b/hw/cpu/core.c
@@ -66,10 +66,16 @@ static void core_prop_set_nr_threads(Object *obj, Visitor *v, const char *name,
 
 static void cpu_core_instance_init(Object *obj)
 {
-    MachineState *ms = MACHINE(qdev_get_machine());
     CPUCore *core = CPU_CORE(obj);
 
-    core->nr_threads = ms->smp.threads;
+    /*
+     * Only '-device something-cpu-core,help' can get us there before
+     * the machine has been created. We don't care to set nr_threads
+     * in this case since it isn't used afterwards.
+     */
+    if (current_machine) {
+        core->nr_threads = current_machine->smp.threads;
+    }
 }
 
 static void cpu_core_class_init(ObjectClass *oc, void *data)
-- 
2.26.3



  parent reply	other threads:[~2021-04-09 16:11 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-09 16:03 [PATCH 0/2] cpu/core: Fix "help" of CPU core device types Greg Kurz
2021-04-09 16:03 ` [PATCH 1/2] qdev: Separate implementations of qdev_get_machine() for user and system Greg Kurz
2021-04-09 20:14   ` Eduardo Habkost
2021-04-10  6:33     ` Greg Kurz
2021-04-10  4:56   ` Thomas Huth
2021-04-10  8:59   ` Markus Armbruster
2021-04-15  8:26     ` Greg Kurz
2021-04-13 22:25   ` Eduardo Habkost
2021-04-15 10:53     ` Greg Kurz
2021-04-15 12:39   ` Philippe Mathieu-Daudé
2021-04-15 13:30     ` Greg Kurz
2021-04-15 16:45       ` Philippe Mathieu-Daudé
2021-04-15 16:56         ` Greg Kurz
2021-04-15 19:07           ` Philippe Mathieu-Daudé
2021-04-16  6:42             ` Greg Kurz
2021-04-19 15:45             ` Thomas Huth
2021-04-09 16:03 ` Greg Kurz [this message]
2021-04-09 20:04   ` [PATCH 2/2] cpu/core: Fix "help" of CPU core device types Eduardo Habkost
2021-04-10  4:53   ` Thomas Huth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210409160339.500167-3-groug@kaod.org \
    --to=groug@kaod.org \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.