Skip to contentSkip to conversation

Implement memory_region_new_* functions

PATCH Superseded BALATON Zoltan 4 participants 15 messages Dec 23, 2025 – Jan 25

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#1 lore raw

PATCH 1/6 memory: Add internal memory_region_set_ops helper function

In reply to BALATON Zoltan

This is a common operation used at multiple places, add a helper function for it.

---
 system/memory.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
system/memory.c +12 -9
index 8b84661ae3..a003095632 100644
@@ -1559,6 +1559,15 @@ MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
     }
 }
 
+static void memory_region_set_ops(MemoryRegion *mr,
+                                  const MemoryRegionOps *ops,
+                                  void *opaque)
+{
+    mr->ops = ops ?: &unassigned_mem_ops;
+    mr->opaque = opaque;
+    mr->terminates = true;
+}
+
 void memory_region_init_io(MemoryRegion *mr,
                            Object *owner,
                            const MemoryRegionOps *ops,
@@ -1567,9 +1576,7 @@ void memory_region_init_io(MemoryRegion *mr,
                            uint64_t size)
 {
     memory_region_init(mr, owner, name, size);
-    mr->ops = ops ? ops : &unassigned_mem_ops;
-    mr->opaque = opaque;
-    mr->terminates = true;
+    memory_region_set_ops(mr, ops, opaque);
 }
 
 bool memory_region_init_ram_nomigrate(MemoryRegion *mr,
@@ -1710,10 +1717,8 @@ void memory_region_init_ram_device_ptr(MemoryRegion *mr,
 {
     memory_region_init(mr, owner, name, size);
     mr->ram = true;
-    mr->terminates = true;
     mr->ram_device = true;
-    mr->ops = &ram_device_mem_ops;
-    mr->opaque = mr;
+    memory_region_set_ops(mr, &ram_device_mem_ops, mr);
     mr->destructor = memory_region_destructor_ram;
 
     /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL.  */
@@ -1759,9 +1764,7 @@ bool memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
     Error *err = NULL;
     assert(ops);
     memory_region_init(mr, owner, name, size);
-    mr->ops = ops;
-    mr->opaque = opaque;
-    mr->terminates = true;
+    memory_region_set_ops(mr, ops, opaque);
     mr->rom_device = true;
     mr->destructor = memory_region_destructor_ram;
     mr->ram_block = qemu_ram_alloc(size, 0, mr, &err);
Signature · 1 line
2.41.3
#8 lore raw

PATCH 5/6 memory: Add internal memory_region_register_ram function

In reply to BALATON Zoltan

Factor out common operation from memory_region_init_{ram,rom} functions to register the region for migration. This avoids duplicating the long comment in several functions.

---
 system/memory.c | 75 +++++++++++++------------------------------------
 1 file changed, 20 insertions(+), 55 deletions(-)
system/memory.c +20 -55
index bff9677f7a..74244be130 100644
@@ -3678,17 +3678,10 @@ void mtree_info(bool flatview, bool dispatch_tree, bool owner, bool disabled)
     }
 }
 
-bool memory_region_init_ram(MemoryRegion *mr,
-                            Object *owner,
-                            const char *name,
-                            uint64_t size,
-                            Error **errp)
+static void memory_region_register_ram(MemoryRegion *mr, Object *owner)
 {
     DeviceState *owner_dev;
 
-    if (!memory_region_init_ram_nomigrate(mr, owner, name, size, errp)) {
-        return false;
-    }
     /* 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
@@ -3697,80 +3690,52 @@ bool memory_region_init_ram(MemoryRegion *mr,
      */
     owner_dev = DEVICE(owner);
     vmstate_register_ram(mr, owner_dev);
+}
 
+bool memory_region_init_ram(MemoryRegion *mr, Object *owner,
+                            const char *name, uint64_t size,
+                            Error **errp)
+{
+    if (!memory_region_init_ram_nomigrate(mr, owner, name, size, errp)) {
+        return false;
+    }
+    memory_region_register_ram(mr, owner);
     return true;
 }
 
-bool memory_region_init_ram_guest_memfd(MemoryRegion *mr,
-                                        Object *owner,
-                                        const char *name,
-                                        uint64_t size,
+bool memory_region_init_ram_guest_memfd(MemoryRegion *mr, Object *owner,
+                                        const char *name, uint64_t size,
                                         Error **errp)
 {
-    DeviceState *owner_dev;
-
     if (!memory_region_init_ram_flags_nomigrate(mr, owner, name, size,
                                                 RAM_GUEST_MEMFD, errp)) {
         return false;
     }
-    /* 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.
-     */
-    owner_dev = DEVICE(owner);
-    vmstate_register_ram(mr, owner_dev);
-
+    memory_region_register_ram(mr, owner);
     return true;
 }
 
-bool memory_region_init_rom(MemoryRegion *mr,
-                            Object *owner,
-                            const char *name,
-                            uint64_t size,
+bool memory_region_init_rom(MemoryRegion *mr, Object *owner,
+                            const char *name, uint64_t size,
                             Error **errp)
 {
-    DeviceState *owner_dev;
-
     if (!memory_region_init_rom_nomigrate(mr, owner, name, size, errp)) {
         return false;
     }
-    /* 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.
-     */
-    owner_dev = DEVICE(owner);
-    vmstate_register_ram(mr, owner_dev);
-
+    memory_region_register_ram(mr, owner);
     return true;
 }
 
-bool memory_region_init_rom_device(MemoryRegion *mr,
-                                   Object *owner,
-                                   const MemoryRegionOps *ops,
-                                   void *opaque,
-                                   const char *name,
-                                   uint64_t size,
+bool memory_region_init_rom_device(MemoryRegion *mr, Object *owner,
+                                   const MemoryRegionOps *ops, void *opaque,
+                                   const char *name, uint64_t size,
                                    Error **errp)
 {
-    DeviceState *owner_dev;
-
     if (!memory_region_init_rom_device_nomigrate(mr, owner, ops, opaque,
                                                  name, size, errp)) {
         return false;
     }
-    /* 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.
-     */
-    owner_dev = DEVICE(owner);
-    vmstate_register_ram(mr, owner_dev);
-
+    memory_region_register_ram(mr, owner);
     return true;
 }
 
Signature · 1 line
2.41.3
#2 lore raw

PATCH 2/6 memory: Factor out common ram region initialization

In reply to BALATON Zoltan

Introduce internal memory_region_do_init_ram() function to remove duplicated code from different memory_region_init_*ram functions.

---
 system/memory.c | 147 +++++++++++++++++-------------------------------
 1 file changed, 53 insertions(+), 94 deletions(-)
system/memory.c +53 -94
index a003095632..87c8e78662 100644
@@ -1579,29 +1579,12 @@ void memory_region_init_io(MemoryRegion *mr,
     memory_region_set_ops(mr, ops, opaque);
 }
 
-bool memory_region_init_ram_nomigrate(MemoryRegion *mr,
-                                      Object *owner,
-                                      const char *name,
-                                      uint64_t size,
-                                      Error **errp)
-{
-    return memory_region_init_ram_flags_nomigrate(mr, owner, name,
-                                                  size, 0, errp);
-}
-
-bool memory_region_init_ram_flags_nomigrate(MemoryRegion *mr,
-                                            Object *owner,
-                                            const char *name,
-                                            uint64_t size,
-                                            uint32_t ram_flags,
-                                            Error **errp)
+static bool memory_region_do_init_ram(MemoryRegion *mr,
+                                      Error *err, Error **errp)
 {
-    Error *err = NULL;
-    memory_region_init(mr, owner, name, size);
     mr->ram = true;
     mr->terminates = true;
     mr->destructor = memory_region_destructor_ram;
-    mr->ram_block = qemu_ram_alloc(size, ram_flags, mr, &err);
     if (err) {
         mr->size = int128_zero();
         object_unparent(OBJECT(mr));
@@ -1611,6 +1594,25 @@ bool memory_region_init_ram_flags_nomigrate(MemoryRegion *mr,
     return true;
 }
 
+bool memory_region_init_ram_flags_nomigrate(MemoryRegion *mr, Object *owner,
+                                            const char *name, uint64_t size,
+                                            uint32_t ram_flags, Error **errp)
+{
+    Error *err = NULL;
+
+    memory_region_init(mr, owner, name, size);
+    mr->ram_block = qemu_ram_alloc(size, ram_flags, mr, &err);
+    return memory_region_do_init_ram(mr, err, errp);
+}
+
+bool memory_region_init_ram_nomigrate(MemoryRegion *mr, Object *owner,
+                                      const char *name, uint64_t size,
+                                      Error **errp)
+{
+    return memory_region_init_ram_flags_nomigrate(mr, owner, name, size, 0,
+                                                  errp);
+}
+
 bool memory_region_init_resizeable_ram(MemoryRegion *mr,
                                        Object *owner,
                                        const char *name,
@@ -1622,108 +1624,66 @@ bool memory_region_init_resizeable_ram(MemoryRegion *mr,
                                        Error **errp)
 {
     Error *err = NULL;
+
     memory_region_init(mr, owner, name, size);
-    mr->ram = true;
-    mr->terminates = true;
-    mr->destructor = memory_region_destructor_ram;
-    mr->ram_block = qemu_ram_alloc_resizeable(size, max_size, resized,
-                                              mr, &err);
-    if (err) {
-        mr->size = int128_zero();
-        object_unparent(OBJECT(mr));
-        error_propagate(errp, err);
-        return false;
-    }
-    return true;
+    mr->ram_block = qemu_ram_alloc_resizeable(size, max_size, resized, mr,
+                                              &err);
+    return memory_region_do_init_ram(mr, err, errp);
 }
 
 #if defined(CONFIG_POSIX) && !defined(EMSCRIPTEN)
-bool memory_region_init_ram_from_file(MemoryRegion *mr,
-                                      Object *owner,
-                                      const char *name,
-                                      uint64_t size,
-                                      uint64_t align,
-                                      uint32_t ram_flags,
-                                      const char *path,
-                                      ram_addr_t offset,
+bool memory_region_init_ram_from_file(MemoryRegion *mr, Object *owner,
+                                      const char *name, uint64_t size,
+                                      uint64_t align, uint32_t ram_flags,
+                                      const char *path, ram_addr_t offset,
                                       Error **errp)
 {
     Error *err = NULL;
+
     memory_region_init(mr, owner, name, size);
-    mr->ram = true;
     mr->readonly = !!(ram_flags & RAM_READONLY);
-    mr->terminates = true;
-    mr->destructor = memory_region_destructor_ram;
     mr->align = align;
-    mr->ram_block = qemu_ram_alloc_from_file(size, mr, ram_flags, path,
-                                             offset, &err);
-    if (err) {
-        mr->size = int128_zero();
-        object_unparent(OBJECT(mr));
-        error_propagate(errp, err);
-        return false;
-    }
-    return true;
+    mr->ram_block = qemu_ram_alloc_from_file(size, mr, ram_flags, path, offset,
+                                             &err);
+    return memory_region_do_init_ram(mr, err, errp);
 }
 
-bool memory_region_init_ram_from_fd(MemoryRegion *mr,
-                                    Object *owner,
-                                    const char *name,
-                                    uint64_t size,
-                                    uint32_t ram_flags,
-                                    int fd,
-                                    ram_addr_t offset,
-                                    Error **errp)
+bool memory_region_init_ram_from_fd(MemoryRegion *mr, Object *owner,
+                                    const char *name, uint64_t size,
+                                    uint32_t ram_flags, int fd,
+                                    ram_addr_t offset, Error **errp)
 {
     Error *err = NULL;
+
     memory_region_init(mr, owner, name, size);
-    mr->ram = true;
     mr->readonly = !!(ram_flags & RAM_READONLY);
-    mr->terminates = true;
-    mr->destructor = memory_region_destructor_ram;
     mr->ram_block = qemu_ram_alloc_from_fd(size, size, NULL, mr, ram_flags, fd,
                                            offset, false, &err);
-    if (err) {
-        mr->size = int128_zero();
-        object_unparent(OBJECT(mr));
-        error_propagate(errp, err);
-        return false;
-    }
-    return true;
+    return memory_region_do_init_ram(mr, err, errp);
 }
 #endif
 
-void memory_region_init_ram_ptr(MemoryRegion *mr,
-                                Object *owner,
-                                const char *name,
-                                uint64_t size,
-                                void *ptr)
+void memory_region_init_ram_ptr(MemoryRegion *mr, Object *owner,
+                                const char *name, uint64_t size, void *ptr)
 {
     memory_region_init(mr, owner, name, size);
-    mr->ram = true;
-    mr->terminates = true;
-    mr->destructor = memory_region_destructor_ram;
-
     /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL.  */
     assert(ptr != NULL);
     mr->ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_abort);
+    memory_region_do_init_ram(mr, NULL, NULL);
 }
 
-void memory_region_init_ram_device_ptr(MemoryRegion *mr,
-                                       Object *owner,
-                                       const char *name,
-                                       uint64_t size,
+void memory_region_init_ram_device_ptr(MemoryRegion *mr, Object *owner,
+                                       const char *name, uint64_t size,
                                        void *ptr)
 {
     memory_region_init(mr, owner, name, size);
-    mr->ram = true;
-    mr->ram_device = true;
     memory_region_set_ops(mr, &ram_device_mem_ops, mr);
-    mr->destructor = memory_region_destructor_ram;
-
     /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL.  */
     assert(ptr != NULL);
     mr->ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_abort);
+    memory_region_do_init_ram(mr, NULL, NULL);
+    mr->ram_device = true;
 }
 
 void memory_region_init_alias(MemoryRegion *mr,
@@ -1762,19 +1722,18 @@ bool memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
                                              Error **errp)
 {
     Error *err = NULL;
+    bool ret;
+
     assert(ops);
     memory_region_init(mr, owner, name, size);
     memory_region_set_ops(mr, ops, opaque);
-    mr->rom_device = true;
-    mr->destructor = memory_region_destructor_ram;
     mr->ram_block = qemu_ram_alloc(size, 0, mr, &err);
-    if (err) {
-        mr->size = int128_zero();
-        object_unparent(OBJECT(mr));
-        error_propagate(errp, err);
-        return false;
+    ret = memory_region_do_init_ram(mr, err, errp);
+    if (ret) {
+        mr->ram = false;
+        mr->rom_device = true;
     }
-    return true;
+    return ret;
 }
 
 void memory_region_init_iommu(void *_iommu_mr,
Signature · 1 line
2.41.3
#5 lore raw

PATCH 3/6 memory: Factor out more common ram region initialization

In reply to BALATON Zoltan

Introduce internal memory_region_do_init_ram_ptr() function to remove duplicated code from different memory_region_init_ram_*ptr functions.

---
 system/memory.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
system/memory.c +10 -7
index 87c8e78662..3d538fb9af 100644
@@ -1663,26 +1663,29 @@ bool memory_region_init_ram_from_fd(MemoryRegion *mr, Object *owner,
 }
 #endif
 
-void memory_region_init_ram_ptr(MemoryRegion *mr, Object *owner,
-                                const char *name, uint64_t size, void *ptr)
+static void memory_region_do_init_ram_ptr(MemoryRegion *mr, uint64_t size,
+                                          void *ptr)
 {
-    memory_region_init(mr, owner, name, size);
     /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL.  */
     assert(ptr != NULL);
     mr->ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_abort);
     memory_region_do_init_ram(mr, NULL, NULL);
 }
 
+void memory_region_init_ram_ptr(MemoryRegion *mr, Object *owner,
+                                const char *name, uint64_t size, void *ptr)
+{
+    memory_region_init(mr, owner, name, size);
+    memory_region_do_init_ram_ptr(mr, size, ptr);
+}
+
 void memory_region_init_ram_device_ptr(MemoryRegion *mr, Object *owner,
                                        const char *name, uint64_t size,
                                        void *ptr)
 {
     memory_region_init(mr, owner, name, size);
     memory_region_set_ops(mr, &ram_device_mem_ops, mr);
-    /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL.  */
-    assert(ptr != NULL);
-    mr->ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_abort);
-    memory_region_do_init_ram(mr, NULL, NULL);
+    memory_region_do_init_ram_ptr(mr, size, ptr);
     mr->ram_device = true;
 }
 
Signature · 1 line
2.41.3
#0 lore raw

cover Implement memory_region_new_* functions

Our documentation says that memory regions are automatically freed when the owner dies and the reference counting to do this is also implemented. However this relies on the QOM free funtion that can only be set by creating objects with object_new but memory API only provides constructors that call object_initialize which clears the free function that prevents QOM to manage the memory region lifetime. Implement corresponding memory_region_new_* functions that do the same as the memory_region_init_* functions but create the memory region with object_new so the lifetime can be automatically managed by QOM as documented.

BALATON Zoltan (6):
memory: Add internal memory_region_set_ops helper function
memory: Factor out common ram region initialization
memory: Factor out more common ram region initialization
memory: Shorten memory_region_init_rom_nomigrate
memory: Add internal memory_region_register_ram function
memory: Add memory_region_new* functions

 include/system/memory.h | 360 ++++++++++++++++++++++++++++++
 system/memory.c         | 484 +++++++++++++++++++++++++---------------
 2 files changed, 668 insertions(+), 176 deletions(-)
Signature · 1 line
2.41.3
#10 lore raw

PATCH 6/6 memory: Add memory_region_new* functions

These are counterparts of similar memory_region_init* functions but differ in that these allocate the memory region with object_new so the memory region will be managed by QOM and freed with the owner. This behaviour was already documented and the reference tracking is implemented but it could not be used without these functions because memory_region_init* functions call object_initialize that resets the free function of the object so it would not be freed.

---
 include/system/memory.h | 360 ++++++++++++++++++++++++++++++++++++++++
 system/memory.c         | 212 +++++++++++++++++++++++
 2 files changed, 572 insertions(+)
include/system/memory.h +360 -0
index 3bd5ffa5e0..5bdaa52355 100644
@@ -1763,6 +1763,366 @@ bool memory_region_init_rom_device(MemoryRegion *mr,
                                    uint64_t size,
                                    Error **errp);
 
+/**
+ * memory_region_new: Allocate and initialize a memory region
+ *
+ * This is like memory_region_init() but allocates the #MemoryRegion and
+ * attaches it to the owner to free it when the owner is freed.
+ *
+ * @owner: the object that tracks the region's reference count
+ * @name: used for debugging; not visible to the user or ABI
+ * @size: size of the region; any subregions beyond this size will be clipped
+ *
+ * Return: Pointer to the allocated #MemoryRegion.
+ */
+MemoryRegion *memory_region_new(Object *owner,
+                                const char *name, uint64_t size);
+
+/**
+ * memory_region_new_io: Allocate and initialize an I/O memory region.
+ *
+ * This is like memory_region_init_io() but allocates the #MemoryRegion and
+ * attaches it to the owner to free it when the owner is freed.
+ *
+ * @owner: the object that tracks the region's reference count
+ * @ops: a structure containing read and write callbacks to be used when
+ *       I/O is performed on the region.
+ * @opaque: passed to the read and write callbacks of the @ops structure.
+ * @name: used for debugging; not visible to the user or ABI
+ * @size: size of the region.
+ *
+ * Return: Pointer to the allocated #MemoryRegion.
+ */
+MemoryRegion *memory_region_new_io(Object *owner,
+                                   const MemoryRegionOps *ops, void *opaque,
+                                   const char *name, uint64_t size);
+
+/**
+ * memory_region_new_ram_nomigrate:  Allocate and initialize RAM memory region.
+ *
+ * This is like memory_region_init_ram_nomigrate() but allocates the
+ * #MemoryRegion and attaches is to the owner to free when the owner is freed.
+ *
+ * @owner: the object that tracks the region's reference count
+ * @name: Region name, becomes part of RAMBlock name used in migration stream
+ *        must be unique within any device
+ * @size: size of the region.
+ * @errp: pointer to Error*, to store an error if it happens.
+ *
+ * Note that this function does not do anything to cause the data in the
+ * RAM memory region to be migrated; that is the responsibility of the caller.
+ *
+ * Return: Pointer to the allocated #MemoryRegion or NULL on error.
+ */
+MemoryRegion *memory_region_new_ram_nomigrate(Object *owner,
+                                              const char *name, uint64_t size,
+                                              Error **errp);
+
+/**
+ * memory_region_new_ram_flags_nomigrate:  Allocata and initialize RAM memory
+ *                                         region with flags.
+ *
+ * This is like memory_region_init_ram_flags_nomigrate() but allocates the
+ * #MemoryRegion and attaches is to the owner to free when the owner is freed.
+ *
+ * @owner: the object that tracks the region's reference count
+ * @name: Region name, becomes part of RAMBlock name used in migration stream
+ *        must be unique within any device
+ * @size: size of the region.
+ * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_NORESERVE,
+ *             RAM_GUEST_MEMFD.
+ * @errp: pointer to Error*, to store an error if it happens.
+ *
+ * Note that this function does not do anything to cause the data in the
+ * RAM memory region to be migrated; that is the responsibility of the caller.
+ *
+ * Return: Pointer to the allocated #MemoryRegion or NULL on error.
+ */
+MemoryRegion *memory_region_new_ram_flags_nomigrate(Object *owner,
+                                                    const char *name,
+                                                    uint64_t size,
+                                                    uint32_t ram_flags,
+                                                    Error **errp);
+
+/**
+ * memory_region_new_resizeable_ram:  Allocate and initialize memory region
+ *                                    with resizable RAM.
+ *
+ * This is like memory_region_init_resizable_ram() but allocates the
+ * #MemoryRegion and attaches is to the owner to free when the owner is freed.
+ *
+ * @owner: the object that tracks the region's reference count
+ * @name: Region name, becomes part of RAMBlock name used in migration stream
+ *        must be unique within any device
+ * @size: used size of the region.
+ * @max_size: max size of the region.
+ * @resized: callback to notify owner about used size change.
+ * @errp: pointer to Error*, to store an error if it happens.
+ *
+ * Note that this function does not do anything to cause the data in the
+ * RAM memory region to be migrated; that is the responsibility of the caller.
+ *
+ * Return: Pointer to the allocated #MemoryRegion or NULL on error.
+ */
+MemoryRegion *memory_region_new_resizeable_ram(Object *owner,
+                                               const char *name,
+                                               uint64_t size,
+                                               uint64_t max_size,
+                                               void (*resized)(const char*,
+                                                               uint64_t length,
+                                                               void *host),
+                                               Error **errp);
+
+/**
+ * memory_region_new_ram_from_file:  Allocate and initialize RAM memory region
+ *                                   with a mmap-ed backend.
+ *
+ * This is like memory_region_init_ram_from_file() but allocates the
+ * #MemoryRegion and attaches is to the owner to free when the owner is freed.
+ *
+ * @owner: the object that tracks the region's reference count
+ * @name: Region name, becomes part of RAMBlock name used in migration stream
+ *        must be unique within any device
+ * @size: size of the region.
+ * @align: alignment of the region base address; if 0, the default alignment
+ *         (getpagesize()) will be used.
+ * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
+ *             RAM_NORESERVE, RAM_PROTECTED, RAM_NAMED_FILE, RAM_READONLY,
+ *             RAM_READONLY_FD, RAM_GUEST_MEMFD
+ * @path: the path in which to allocate the RAM.
+ * @offset: offset within the file referenced by path
+ * @errp: pointer to Error*, to store an error if it happens.
+ *
+ * Note that this function does not do anything to cause the data in the
+ * RAM memory region to be migrated; that is the responsibility of the caller.
+ *
+ * Return: Pointer to the allocated #MemoryRegion or NULL on error.
+ */
+MemoryRegion *memory_region_new_ram_from_file(Object *owner,
+                                              const char *name,
+                                              uint64_t size,
+                                              uint64_t align,
+                                              uint32_t ram_flags,
+                                              const char *path,
+                                              ram_addr_t offset,
+                                              Error **errp);
+
+/**
+ * memory_region_new_ram_from_fd:  Allocate and initialize RAM memory region
+ *                                 with a mmap-ed backend.
+ *
+ * This is like memory_region_init_ram_from_fd() but allocates the
+ * #MemoryRegion and attaches is to the owner to free when the owner is freed.
+ *
+ * @owner: the object that tracks the region's reference count
+ * @name: the name of the region.
+ * @size: size of the region.
+ * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
+ *             RAM_NORESERVE, RAM_PROTECTED, RAM_NAMED_FILE, RAM_READONLY,
+ *             RAM_READONLY_FD, RAM_GUEST_MEMFD
+ * @fd: the fd to mmap.
+ * @offset: offset within the file referenced by fd
+ * @errp: pointer to Error*, to store an error if it happens.
+ *
+ * Note that this function does not do anything to cause the data in the
+ * RAM memory region to be migrated; that is the responsibility of the caller.
+ *
+ * Return: Pointer to the allocated #MemoryRegion or NULL on error.
+ */
+MemoryRegion *memory_region_new_ram_from_fd(Object *owner,
+                                            const char *name, uint64_t size,
+                                            uint32_t ram_flags, int fd,
+                                            ram_addr_t offset, Error **errp);
+
+/**
+ * memory_region_new_ram_ptr:  Allocate and initialize RAM memory region from a
+ *                             user-provided pointer.
+ *
+ * This is like memory_region_init_ram_ptr() but allocates the
+ * #MemoryRegion and attaches is to the owner to free when the owner is freed.
+ *
+ * @owner: the object that tracks the region's reference count
+ * @name: Region name, becomes part of RAMBlock name used in migration stream
+ *        must be unique within any device
+ * @size: size of the region.
+ * @ptr: memory to be mapped; must contain at least @size bytes.
+ *
+ * Note that this function does not do anything to cause the data in the
+ * RAM memory region to be migrated; that is the responsibility of the caller.
+ *
+ * Return: Pointer to the allocated #MemoryRegion
+ */
+MemoryRegion *memory_region_new_ram_ptr(Object *owner, const char *name,
+                                        uint64_t size, void *ptr);
+
+/**
+ * memory_region_new_ram_device_ptr:  Allocate and initialize RAM device memory
+ *                                    region from a user-provided pointer.
+ *
+ * This is like memory_region_init_ram_device_ptr() but allocates the
+ * #MemoryRegion and attaches is to the owner to free when the owner is freed.
+ *
+ * @owner: the object that tracks the region's reference count
+ * @name: the name of the region.
+ * @size: size of the region.
+ * @ptr: memory to be mapped; must contain at least @size bytes.
+ *
+ * Note that this function does not do anything to cause the data in the
+ * RAM memory region to be migrated; that is the responsibility of the caller.
+ * (For RAM device memory regions, migrating the contents rarely makes sense.)
+ *
+ * Return: Pointer to the allocated #MemoryRegion
+ */
+MemoryRegion *memory_region_new_ram_device_ptr(Object *owner, const char *name,
+                                               uint64_t size, void *ptr);
+
+/**
+ * memory_region_new_alias:  Allocate and initialize a memory region that
+ *                           aliases all or a part of another memory region.
+ *
+ * This is like memory_region_init_alias() but allocates the
+ * #MemoryRegion and attaches is to the owner to free when the owner is freed.
+ *
+ * @owner: the object that tracks the region's reference count
+ * @name: used for debugging; not visible to the user or ABI
+ * @orig: the region to be referenced; @mr will be equivalent to
+ *        @orig between @offset and @offset + @size - 1.
+ * @offset: start of the section in @orig to be referenced.
+ * @size: size of the region.
+ *
+ * Return: Pointer to the allocated #MemoryRegion
+ */
+MemoryRegion *memory_region_new_alias(Object *owner,
+                                      const char *name, MemoryRegion *orig,
+                                      hwaddr offset, uint64_t size);
+
+/**
+ * memory_region_new_rom_nomigrate:  Allocate and initialize a ROM memory
+ *                                   region.
+ *
+ * This is like memory_region_init_rom_nomigrate() but allocates the
+ * #MemoryRegion and attaches is to the owner to free when the owner is freed.
+ *
+ * @owner: the object that tracks the region's reference count
+ * @name: Region name, becomes part of RAMBlock name used in migration stream
+ *        must be unique within any device
+ * @size: size of the region.
+ * @errp: pointer to Error*, to store an error if it happens.
+ *
+ * Return: Pointer to the allocated #MemoryRegion or NULL on error.
+ */
+MemoryRegion *memory_region_new_rom_nomigrate(Object *owner,
+                                              const char *name, uint64_t size,
+                                              Error **errp);
+
+/**
+ * memory_region_new_rom_device_nomigrate:  Allocate and initialize a ROM
+ *                                          device memory region.
+ *
+ * This is like memory_region_init_rom_device_nomigrate() but allocates the
+ * #MemoryRegion and attaches is to the owner to free when the owner is freed.
+ *
+ * @owner: the object that tracks the region's reference count
+ * @ops: callbacks for write access handling (must not be NULL).
+ * @opaque: passed to the read and write callbacks of the @ops structure.
+ * @name: Region name, becomes part of RAMBlock name used in migration stream
+ *        must be unique within any device
+ * @size: size of the region.
+ * @errp: pointer to Error*, to store an error if it happens.
+ *
+ * Note that this function does not do anything to cause the data in the
+ * RAM side of the memory region to be migrated; that is the responsibility
+ * of the caller.
+ *
+ * Return: Pointer to the allocated #MemoryRegion or NULL on error.
+ */
+MemoryRegion *memory_region_new_rom_device_nomigrate(Object *owner,
+                                                     const MemoryRegionOps *ops,
+                                                     void *opaque,
+                                                     const char *name,
+                                                     uint64_t size,
+                                                     Error **errp);
+/**
+ * memory_region_new_ram - Allocate and initialize RAM memory region.
+ *
+ * This is like memory_region_init_ram() but allocates the
+ * #MemoryRegion and attaches is to the owner to free when the owner is freed.
+ *
+ * @owner: the object that tracks the region's reference count (must be
+ *         TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
+ * @name: name of the memory region
+ * @size: size of the region in bytes
+ * @errp: pointer to Error*, to store an error if it happens.
+ *
+ * TODO: Currently we restrict @owner to being either NULL (for
+ * global RAM regions with no owner) or devices, so that we can
+ * give the RAM block a unique name for migration purposes.
+ * We should lift this restriction and allow arbitrary Objects.
+ * If you pass a non-NULL non-device @owner then we will assert.
+ *
+ * Return: Pointer to the allocated #MemoryRegion or NULL on error.
+ */
+MemoryRegion *memory_region_new_ram(Object *owner,
+                                    const char *name, uint64_t size,
+                                    Error **errp);
+
+MemoryRegion *memory_region_new_ram_guest_memfd(Object *owner,
+                                                const char *name,
+                                                uint64_t size,
+                                                Error **errp);
+
+/**
+ * memory_region_new_rom: Allocate and initialize a ROM memory region.
+ *
+ * This is like memory_region_init_rom() but allocates the
+ * #MemoryRegion and attaches is to the owner to free when the owner is freed.
+ *
+ * TODO: Currently we restrict @owner to being either NULL (for
+ * global RAM regions with no owner) or devices, so that we can
+ * give the RAM block a unique name for migration purposes.
+ * We should lift this restriction and allow arbitrary Objects.
+ * If you pass a non-NULL non-device @owner then we will assert.
+ *
+ * @owner: the object that tracks the region's reference count
+ * @name: Region name, becomes part of RAMBlock name used in migration stream
+ *        must be unique within any device
+ * @size: size of the region.
+ * @errp: pointer to Error*, to store an error if it happens.
+ *
+ * Return: Pointer to the allocated #MemoryRegion or NULL on error.
+ */
+MemoryRegion *memory_region_new_rom(Object *owner,
+                                    const char *name, uint64_t size,
+                                    Error **errp);
+
+/**
+ * memory_region_new_rom_device:  Allocate and initialize a ROM device memory
+ *                                region.
+ *
+ * This is like memory_region_init_rom_device() but allocates the
+ * #MemoryRegion and attaches is to the owner to free when the owner is freed.
+ *
+ * TODO: Currently we restrict @owner to being either NULL (for
+ * global RAM regions with no owner) or devices, so that we can
+ * give the RAM block a unique name for migration purposes.
+ * We should lift this restriction and allow arbitrary Objects.
+ * If you pass a non-NULL non-device @owner then we will assert.
+ *
+ * @owner: the object that tracks the region's reference count
+ * @ops: callbacks for write access handling (must not be NULL).
+ * @opaque: passed to the read and write callbacks of the @ops structure.
+ * @name: Region name, becomes part of RAMBlock name used in migration stream
+ *        must be unique within any device
+ * @size: size of the region.
+ * @errp: pointer to Error*, to store an error if it happens.
+ *
+ * Return: Pointer to the allocated #MemoryRegion or NULL on error.
+ */
+MemoryRegion *memory_region_new_rom_device(Object *owner,
+                                           const MemoryRegionOps *ops,
+                                           void *opaque,
+                                           const char *name, uint64_t size,
+                                           Error **errp);
 
 /**
  * memory_region_owner: get a memory region's owner.
system/memory.c +212 -0

302 lines not on this page · raw message

Signature · 1 line
2.41.3
#7 lore raw

PATCH 4/6 memory: Shorten memory_region_init_rom_nomigrate

In reply to BALATON Zoltan

Use the shortcut memory_region_init_ram_nomigrate as we have no flags to pass and remove some extra line breaks to make the function occupy less lines. Also shorten some other function prototypes.

---
 system/memory.c | 30 ++++++++++--------------------
 1 file changed, 10 insertions(+), 20 deletions(-)
system/memory.c +10 -20
index 3d538fb9af..bff9677f7a 100644
@@ -1568,12 +1568,9 @@ static void memory_region_set_ops(MemoryRegion *mr,
     mr->terminates = true;
 }
 
-void memory_region_init_io(MemoryRegion *mr,
-                           Object *owner,
-                           const MemoryRegionOps *ops,
-                           void *opaque,
-                           const char *name,
-                           uint64_t size)
+void memory_region_init_io(MemoryRegion *mr, Object *owner,
+                           const MemoryRegionOps *ops, void *opaque,
+                           const char *name, uint64_t size)
 {
     memory_region_init(mr, owner, name, size);
     memory_region_set_ops(mr, ops, opaque);
@@ -1689,30 +1686,23 @@ void memory_region_init_ram_device_ptr(MemoryRegion *mr, Object *owner,
     mr->ram_device = true;
 }
 
-void memory_region_init_alias(MemoryRegion *mr,
-                              Object *owner,
-                              const char *name,
-                              MemoryRegion *orig,
-                              hwaddr offset,
-                              uint64_t size)
+void memory_region_init_alias(MemoryRegion *mr, Object *owner,
+                              const char *name, MemoryRegion *orig,
+                              hwaddr offset, uint64_t size)
 {
     memory_region_init(mr, owner, name, size);
     mr->alias = orig;
     mr->alias_offset = offset;
 }
 
-bool memory_region_init_rom_nomigrate(MemoryRegion *mr,
-                                      Object *owner,
-                                      const char *name,
-                                      uint64_t size,
+bool memory_region_init_rom_nomigrate(MemoryRegion *mr, Object *owner,
+                                      const char *name, uint64_t size,
                                       Error **errp)
 {
-    if (!memory_region_init_ram_flags_nomigrate(mr, owner, name,
-                                                size, 0, errp)) {
-         return false;
+    if (!memory_region_init_ram_nomigrate(mr, owner, name, size, errp)) {
+        return false;
     }
     mr->readonly = true;
-
     return true;
 }
 
Signature · 1 line
2.41.3
#11 lore raw
In reply to BALATON Zoltan
Quoted 10 lines · BALATON Zoltan

Our documentation says that memory regions are automatically freed when the owner dies and the reference counting to do this is also implemented. However this relies on the QOM free funtion that can only be set by creating objects with object_new but memory API only provides constructors that call object_initialize which clears the free function that prevents QOM to manage the memory region lifetime. Implement corresponding memory_region_new_* functions that do the same as the memory_region_init_* functions but create the memory region with object_new so the lifetime can be automatically managed by QOM as documented.

The documentation explains the existing functions so the discrepancy between them you see should be fixed by updating them, not adding new ones.

Regards,
Akihiko Odaki

#12 lore raw
Quoted 12 lines · Akihiko Odaki
Quoted 10 lines · BALATON Zoltan

Our documentation says that memory regions are automatically freed when the owner dies and the reference counting to do this is also implemented. However this relies on the QOM free funtion that can only be set by creating objects with object_new but memory API only provides constructors that call object_initialize which clears the free function that prevents QOM to manage the memory region lifetime. Implement corresponding memory_region_new_* functions that do the same as the memory_region_init_* functions but create the memory region with object_new so the lifetime can be automatically managed by QOM as documented.

Quoted 1 line

The documentation explains the existing functions so the discrepancy between them you see should be fixed by updating them, not adding new ones.

Do you mean replacing memory_region_init_* with these memory_region_new_* functions? The memory_region_init_* is still useful for embedded memory regions that are managed by some other way which is also mentioned in the documentation as an alternative so I think both of them are useful for different cases. If you mean we need to update docs to refer to memory_region_new instead of memory_region_init at some places then I think you're right, the docs may also need to be updated or clarified.

Regards,
BALATON Zoltan

#3 lore raw

Re: [PATCH 2/6] memory: Factor out common ram region initialization

In reply to BALATON Zoltan
Quoted 15 lines · BALATON Zoltan
system/memory.c +0 -15
-bool memory_region_init_ram_nomigrate(MemoryRegion *mr,
-                                      Object *owner,
-                                      const char *name,
-                                      uint64_t size,
-                                      Error **errp)
-{
-    return memory_region_init_ram_flags_nomigrate(mr, owner, name,
-                                                  size, 0, errp);
-}
-
-bool memory_region_init_ram_flags_nomigrate(MemoryRegion *mr,
-                                            Object *owner,
-                                            const char *name,
-                                            uint64_t size,
-                                            uint32_t ram_flags,

system/memory.c void memory_region_init_io(MemoryRegion *mr, · line 1604

Quoted 10 lines
system/memory.c +2 -4
-                                            Error **errp)
+static bool memory_region_do_init_ram(MemoryRegion *mr,
+                                      Error *err, Error **errp)
 {
-    Error *err = NULL;
-    memory_region_init(mr, owner, name, size);
     mr->ram = true;
     mr->terminates = true;
     mr->destructor = memory_region_destructor_ram;
-    mr->ram_block = qemu_ram_alloc(size, ram_flags, mr, &err);

If this is moved out, the err below will never be set. Maybe this helper then doesn't need errp at all.

Quoted 6 lines
     if (err) {
         mr->size = int128_zero();
         object_unparent(OBJECT(mr));
system/memory.c +0 -0
@@ -1611,6 +1594,25 @@ bool memory_region_init_ram_flags_nomigrate(MemoryRegion *mr,
     return true;
 }
Signature · 1 line
Peter Xu
#13 lore raw
In reply to BALATON Zoltan
Quoted 17 lines · BALATON Zoltan
Quoted 12 lines · Akihiko Odaki
Quoted 10 lines · BALATON Zoltan

Our documentation says that memory regions are automatically freed when the owner dies and the reference counting to do this is also implemented. However this relies on the QOM free funtion that can only be set by creating objects with object_new but memory API only provides constructors that call object_initialize which clears the free function that prevents QOM to manage the memory region lifetime. Implement corresponding memory_region_new_* functions that do the same as the memory_region_init_* functions but create the memory region with object_new so the lifetime can be automatically managed by QOM as documented.

Quoted 3 lines

The documentation explains the existing functions so the discrepancy between them you see should be fixed by updating them, not adding new ones.

Quoted 7 lines

Do you mean replacing memory_region_init_* with these memory_region_new_* functions? The memory_region_init_* is still useful for embedded memory regions that are managed by some other way which is also mentioned in the documentation as an alternative so I think both of them are useful for different cases. If you mean we need to update docs to refer to memory_region_new instead of memory_region_init at some places then I think you're right, the docs may also need to be updated or clarified.

To me, it's less convincing to add new APIs without a solid user.

IMHO we can go either (1) leave patch 6 for later, making this series a cleanup first, or, (2) add users for every new functions introduced, so at least we know why each of the new functions are introduced and necessary.

Thanks,

Signature · 1 line
Peter Xu
#14 lore raw
In reply to BALATON Zoltan
Quoted 13 lines · BALATON Zoltan
Quoted 12 lines · Akihiko Odaki
Quoted 10 lines · BALATON Zoltan

Our documentation says that memory regions are automatically freed when the owner dies and the reference counting to do this is also implemented. However this relies on the QOM free funtion that can only be set by creating objects with object_new but memory API only provides constructors that call object_initialize which clears the free function that prevents QOM to manage the memory region lifetime. Implement corresponding memory_region_new_* functions that do the same as the memory_region_init_* functions but create the memory region with object_new so the lifetime can be automatically managed by QOM as documented.

Quoted 3 lines
Quoted 1 line

The documentation explains the existing functions so the discrepancy between them you see should be fixed by updating them, not adding new ones.

Do you mean replacing memory_region_init_* with these memory_region_new_* functions? The memory_region_init_* is still useful for embedded memory regions that are managed by some other way which is also mentioned in the documentation as an alternative so I think both of them are useful for different cases. If you mean we need to update docs to refer to memory_region_new instead of memory_region_init at some places then I think you're right, the docs may also need to be updated or clarified.

I'd like to see a correspondence between the stated problem and the solution. If the intention is to solve the mismatched documentation and implementation, I think there are only two possible options:

1) Update the documentation and/or the implementation.
2) Delete them.

But this patch series does neither of them. Replacing memory_region_init_* with memory_region_new_* does solve as it will do 2) in the process. An alternative solution that implements 1) or 2) is necessary if it's not ideal.

Regards,
Akihiko Odaki

Reviewed-by on v1 3/6
#6 lore raw

Re: [PATCH 3/6] memory: Factor out more common ram region initialization

In reply to BALATON Zoltan
Quoted 7 lines · BALATON Zoltan

Introduce internal memory_region_do_init_ram_ptr() function to remove duplicated code from different memory_region_init_ram_*ptr functions.

---
  system/memory.c | 17 ++++++++++-------
  1 file changed, 10 insertions(+), 7 deletions(-)
Reviewed-by on v1 5/6
#9 lore raw

Re: [PATCH 5/6] memory: Add internal memory_region_register_ram function

In reply to BALATON Zoltan
Quoted 8 lines · BALATON Zoltan

Factor out common operation from memory_region_init_{ram,rom} functions to register the region for migration. This avoids duplicating the long comment in several functions.

---
  system/memory.c | 75 +++++++++++++------------------------------------
  1 file changed, 20 insertions(+), 55 deletions(-)
#4 lore raw

Re: [PATCH 2/6] memory: Factor out common ram region initialization

In reply to Peter Xu
Quoted 21 lines · Peter Xu
Quoted 20 lines · BALATON Zoltan
system/memory.c +2 -17
-bool memory_region_init_ram_nomigrate(MemoryRegion *mr,
-                                      Object *owner,
-                                      const char *name,
-                                      uint64_t size,
-                                      Error **errp)
-{
-    return memory_region_init_ram_flags_nomigrate(mr, owner, name,
-                                                  size, 0, errp);
-}
-
-bool memory_region_init_ram_flags_nomigrate(MemoryRegion *mr,
-                                            Object *owner,
-                                            const char *name,
-                                            uint64_t size,
-                                            uint32_t ram_flags,
-                                            Error **errp)
+static bool memory_region_do_init_ram(MemoryRegion *mr,
+                                      Error *err, Error **errp)
 {
-    Error *err = NULL;
Quoted 8 lines

system/memory.c void memory_region_init_io(MemoryRegion *mr, · line 1604

Quoted 5 lines
system/memory.c +0 -2
-    memory_region_init(mr, owner, name, size);
     mr->ram = true;
     mr->terminates = true;
     mr->destructor = memory_region_destructor_ram;
-    mr->ram_block = qemu_ram_alloc(size, ram_flags, mr, &err);

If this is moved out, the err below will never be set. Maybe this helper then doesn't need errp at all.

err is an input parameter to this function as it consolidates common error handling so it does not have to be repeated at every caller.

Regards,
BALATON Zoltan

Quoted 8 lines
Quoted 6 lines
     if (err) {
         mr->size = int128_zero();
         object_unparent(OBJECT(mr));
system/memory.c +0 -0
@@ -1611,6 +1594,25 @@ bool memory_region_init_ram_flags_nomigrate(MemoryRegion *mr,
     return true;
 }