[RFC PATCH v2 090/137] memory: Accept non-device owners in memory_region_init_ram()

Alexander Graf <[email protected]>
Newsgroups org.nongnu.qemu-arm,org.nongnu.qemu-devel,org.nongnu.qemu-riscv
Message-ID <[email protected]>
memory_region_register_ram() casts the owner to DeviceState with an
asserting DEVICE() check. Historically this was fine because every
caller either passed a device or NULL, but the following patches
give board-created RAM regions their machine as owner, which is not
a device.

vmstate_register_ram() only uses the device to prefix the RAMBlock
idstr with the device's canonical path. Use object_dynamic_cast()
so non-device owners degrade to the same NULL-owner idstr they had
before, and drop the stale TODO comment.

No functional change for existing callers.

AI-used-for: code (refactoring)
Signed-off-by: Alexander Graf <[email protected]>
---
 system/memory.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/system/memory.c b/system/memory.c
index 5fc36708ec..838be57b4d 100644
--- a/system/memory.c
+++ b/system/memory.c
@@ -3631,15 +3631,16 @@ void mtree_info(bool flatview, bool dispatch_tree, bool owner, bool disabled)
 
 static void memory_region_register_ram(MemoryRegion *mr, Object *owner)
 {
-    DeviceState *owner_dev;
-
-    /* This will assert if owner is neither NULL nor a DeviceState.
-     * We only want the owner here for the purposes of defining a
-     * unique name for migration. TODO: Ideally we should implement
-     * a naming scheme for Objects which are not DeviceStates, in
-     * which case we can relax this restriction.
+    /*
+     * vmstate_register_ram() prefixes the RAMBlock idstr with the
+     * bus-class dev-path of @owner (via qdev_get_dev_path()), not
+     * the QOM canonical path, when it is a DeviceState.  Board-
+     * created RAM regions are owned by the machine, which is not a
+     * device; pass NULL there so the idstr is the bare region name,
+     * matching historical behaviour for owner==NULL callers.
      */
-    owner_dev = DEVICE(owner);
+    DeviceState *owner_dev =
+        (DeviceState *)object_dynamic_cast(owner, TYPE_DEVICE);
     vmstate_register_ram(mr, owner_dev);
 }
 
-- 
2.47.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.