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.

Assisted-by: Kiro
Signed-off-by: Alexander Graf <[email protected]>
---
 system/memory.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/system/memory.c b/system/memory.c
index 5fc36708ec..e3ee0416cc 100644
--- a/system/memory.c
+++ b/system/memory.c
@@ -3631,15 +3631,15 @@ 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
+     * canonical path of @owner 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


Reply via email to