All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>
Subject: [Qemu-devel] [PULL 16/27] qdev: Fix latent bug with compat_props and onboard devices
Date: Mon, 11 Mar 2019 23:08:32 +0100	[thread overview]
Message-ID: <20190311220843.4026-17-armbru@redhat.com> (raw)
In-Reply-To: <20190311220843.4026-1-armbru@redhat.com>

Compatibility properties started life as a qdev property thing: we
supported them only for qdev properties, and implemented them with the
machinery backing command line option -global.

Recent commit fa0cb34d221 put them to use (tacitly) with memory
backend objects (subtypes of TYPE_MEMORY_BACKEND).  To make that
possible, we first moved the work of applying them from the -global
machinery into TYPE_DEVICE's .instance_post_init() method
device_post_init(), in commits ea9ce8934c5 and b66bbee39f6, then made
it available to TYPE_MEMORY_BACKEND's .instance_post_init() method
host_memory_backend_post_init() as object_apply_compat_props(), in
commit 1c3994f6d2a.

Note the code smell: we now have function name starting with object_
in hw/core/qdev.c.  It has to be there rather than in qom/, because it
calls qdev_get_machine() to find the current accelerator's and
machine's compat_props.

Turns out calling qdev_get_machine() there is problematic.  If we
qdev_create() from a machine's .instance_init() method, we call
device_post_init() and thus qdev_get_machine() before main() can
create "/machine" in QOM.  qdev_get_machine() tries to get it with
container_get(), which "helpfully" creates it as "container" object,
and returns that.  object_apply_compat_props() tries to paper over the
problem by doing nothing when the value of qdev_get_machine() isn't a
TYPE_MACHINE.  But the damage is done already: when main() later
attempts to create the real "/machine", it fails with "attempt to add
duplicate property 'machine' to object (type 'container')", and
aborts.

Since no machine .instance_init() calls qdev_create() so far, the bug
is latent.  But since I want to do that, I get to fix the bug first.

Observe that object_apply_compat_props() doesn't actually need the
MachineState, only its the compat_props member of its MachineClass and
AccelClass.  This permits a simple fix: register MachineClass and
AccelClass compat_props with the object_apply_compat_props() machinery
right after these classes get selected.

This is actually similar to how things worked before commits
ea9ce8934c5 and b66bbee39f6, except we now register much earlier.  The
old code registered them only after the machine's .instance_init()
ran, which would've broken compatibility properties for any devices
created there.

Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20190308131445.17502-2-armbru@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
---
 accel/accel.c          |  1 +
 hw/core/qdev.c         | 48 ++++++++++++++++++++++++++++++++----------
 include/hw/qdev-core.h |  2 ++
 vl.c                   |  1 +
 4 files changed, 41 insertions(+), 11 deletions(-)

diff --git a/accel/accel.c b/accel/accel.c
index 0d5b370dfd..8deb475b5d 100644
--- a/accel/accel.c
+++ b/accel/accel.c
@@ -66,6 +66,7 @@ static int accel_init_machine(AccelClass *acc, MachineState *ms)
         *(acc->allowed) = false;
         object_unref(OBJECT(accel));
     }
+    object_set_accelerator_compat_props(acc->compat_props);
     return ret;
 }
 
diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index 512ce7ca7a..4f3200d54b 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -978,25 +978,51 @@ static void device_initfn(Object *obj)
     QLIST_INIT(&dev->gpios);
 }
 
+/*
+ * Global property defaults
+ * Slot 0: accelerator's global property defaults
+ * Slot 1: machine's global property defaults
+ * Each is a GPtrArray of of GlobalProperty.
+ * Applied in order, later entries override earlier ones.
+ */
+static GPtrArray *object_compat_props[2];
+
+/*
+ * Set machine's global property defaults to @compat_props.
+ * May be called at most once.
+ */
+void object_set_machine_compat_props(GPtrArray *compat_props)
+{
+    assert(!object_compat_props[1]);
+    object_compat_props[1] = compat_props;
+}
+
+/*
+ * Set accelerator's global property defaults to @compat_props.
+ * May be called at most once.
+ */
+void object_set_accelerator_compat_props(GPtrArray *compat_props)
+{
+    assert(!object_compat_props[0]);
+    object_compat_props[0] = compat_props;
+}
+
 void object_apply_compat_props(Object *obj)
 {
-    if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
-        MachineState *m = MACHINE(qdev_get_machine());
-        MachineClass *mc = MACHINE_GET_CLASS(m);
+    int i;
 
-        if (m->accelerator) {
-            AccelClass *ac = ACCEL_GET_CLASS(m->accelerator);
-
-            if (ac->compat_props) {
-                object_apply_global_props(obj, ac->compat_props, &error_abort);
-            }
-        }
-        object_apply_global_props(obj, mc->compat_props, &error_abort);
+    for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
+        object_apply_global_props(obj, object_compat_props[i],
+                                  &error_abort);
     }
 }
 
 static void device_post_init(Object *obj)
 {
+    /*
+     * Note: ordered so that the user's global properties take
+     * precedence.
+     */
     object_apply_compat_props(obj);
     qdev_prop_set_globals(DEVICE(obj));
 }
diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
index 17f09aac72..aa8a3ea782 100644
--- a/include/hw/qdev-core.h
+++ b/include/hw/qdev-core.h
@@ -431,6 +431,8 @@ const char *qdev_fw_name(DeviceState *dev);
 
 Object *qdev_get_machine(void);
 
+void object_set_machine_compat_props(GPtrArray *compat_props);
+void object_set_accelerator_compat_props(GPtrArray *compat_props);
 void object_apply_compat_props(Object *obj);
 
 /* FIXME: make this a link<> */
diff --git a/vl.c b/vl.c
index f46f8d769a..5278beaae0 100644
--- a/vl.c
+++ b/vl.c
@@ -3953,6 +3953,7 @@ int main(int argc, char **argv, char **envp)
     configure_rtc(qemu_find_opts_singleton("rtc"));
 
     machine_class = select_machine();
+    object_set_machine_compat_props(machine_class->compat_props);
 
     set_memory_options(&ram_slots, &maxram_size, machine_class);
 
-- 
2.17.2

  parent reply	other threads:[~2019-03-11 22:09 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-11 22:08 [Qemu-devel] [PULL 00/27] Pflash and firmware configuration patches for 2019-03-11 Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 01/27] pflash: Rename pflash_t to PFlashCFI01, PFlashCFI02 Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 02/27] pflash_cfi01: Do not exit() on guest aborting "write to buffer" Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 03/27] pflash_cfi01: Log use of flawed " Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 04/27] pflash: Rename *CFI_PFLASH* to *PFLASH_CFI* Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 05/27] hw: Use PFLASH_CFI0{1, 2} and TYPE_PFLASH_CFI0{1, 2} Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 06/27] sam460ex: Don't size flash memory to match backing image Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 07/27] ppc405_boards: Delete stale, disabled DEBUG_BOARD_INIT code Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 08/27] ppc405_boards: Don't size flash memory to match backing image Markus Armbruster
2020-03-20 15:25   ` Peter Maydell
2020-03-20 16:10     ` Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 09/27] r2d: Fix flash memory size, sector size, width, device ID Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 10/27] mips_malta: Delete disabled, broken DEBUG_BOARD_INIT code Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 11/27] hw/mips/malta: Remove fl_sectors variable Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 12/27] hw/mips/malta: Restrict 'bios_size' variable scope Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 13/27] mips_malta: Clean up definition of flash memory size somewhat Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 14/27] pflash: Clean up after commit 368a354f02b, part 1 Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 15/27] pflash: Clean up after commit 368a354f02b, part 2 Markus Armbruster
2019-03-11 22:08 ` Markus Armbruster [this message]
2019-03-11 22:08 ` [Qemu-devel] [PULL 17/27] qom: Move compat_props machinery from qdev to QOM Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 18/27] vl: Fix latent bug with -global and onboard devices Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 19/27] sysbus: Fix latent bug with " Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 20/27] vl: Improve legibility of BlockdevOptions queue Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 21/27] vl: Factor configure_blockdev() out of main() Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 22/27] vl: Create block backends before setting machine properties Markus Armbruster
2019-05-16  8:29   ` Michal Privoznik
2019-05-16 11:43     ` Markus Armbruster
2019-05-16 12:44       ` Michal Privoznik
2019-06-03 17:40         ` Markus Armbruster
2019-06-04 13:29           ` Paolo Bonzini
2019-03-11 22:08 ` [Qemu-devel] [PULL 23/27] pflash_cfi01: Add pflash_cfi01_get_blk() helper Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 24/27] pc_sysfw: Remove unused PcSysFwDevice Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 25/27] pc_sysfw: Pass PCMachineState to pc_system_firmware_init() Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 26/27] pc: Support firmware configuration with -blockdev Markus Armbruster
2019-03-11 22:08 ` [Qemu-devel] [PULL 27/27] docs/interop/firmware.json: Prefer -machine to if=pflash Markus Armbruster

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=20190311220843.4026-17-armbru@redhat.com \
    --to=armbru@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /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.