diff mbox series

[V1,15/26] physmem: hoist host memory allocation

Message ID 1714406135-451286-16-git-send-email-steven.sistare@oracle.com (mailing list archive)
State New, archived
Headers show
Series Live update: cpr-exec | expand

Commit Message

Steven Sistare April 29, 2024, 3:55 p.m. UTC
Hoist host memory allocation from ram_block_add.
No functional change.

Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
---
 system/physmem.c | 80 +++++++++++++++++++++++++-------------------------------
 1 file changed, 36 insertions(+), 44 deletions(-)
diff mbox series

Patch

diff --git a/system/physmem.c b/system/physmem.c
index ffcf012..b57462d 100644
--- a/system/physmem.c
+++ b/system/physmem.c
@@ -1825,44 +1825,40 @@  static void ram_block_destroy_guest_memfd(RAMBlock *rb)
     }
 }
 
-static void ram_block_add(RAMBlock *new_block, Error **errp)
+static void *ram_block_alloc_host(RAMBlock *rb, Error **errp)
+{
+    struct MemoryRegion *mr = rb->mr;
+    uint8_t *host = NULL;
+
+    if (xen_enabled()) {
+        xen_ram_alloc(rb->offset, rb->max_length, mr, errp);
+
+    } else {
+        host = qemu_anon_ram_alloc(rb->max_length, &mr->align,
+                                   qemu_ram_is_shared(rb),
+                                   qemu_ram_is_noreserve(rb));
+        if (!host) {
+            error_setg_errno(errp, errno, "cannot set up guest memory '%s'",
+                             rb->idstr);
+        }
+    }
+
+    if (host) {
+        memory_try_enable_merging(host, rb->max_length);
+    }
+    return host;
+}
+
+static void ram_block_add(RAMBlock *new_block)
 {
-    const bool noreserve = qemu_ram_is_noreserve(new_block);
-    const bool shared = qemu_ram_is_shared(new_block);
     RAMBlock *block;
     RAMBlock *last_block = NULL;
     ram_addr_t old_ram_size, new_ram_size;
-    Error *err = NULL;
-
     old_ram_size = last_ram_page();
 
     qemu_mutex_lock_ramlist();
     new_block->offset = find_ram_offset(new_block->max_length);
 
-    if (!new_block->host) {
-        if (xen_enabled()) {
-            xen_ram_alloc(new_block->offset, new_block->max_length,
-                          new_block->mr, &err);
-            if (err) {
-                error_propagate(errp, err);
-                qemu_mutex_unlock_ramlist();
-                return;
-            }
-        } else {
-            new_block->host = qemu_anon_ram_alloc(new_block->max_length,
-                                                  &new_block->mr->align,
-                                                  shared, noreserve);
-            if (!new_block->host) {
-                error_setg_errno(errp, errno,
-                                 "cannot set up guest memory '%s'",
-                                 memory_region_name(new_block->mr));
-                qemu_mutex_unlock_ramlist();
-                return;
-            }
-            memory_try_enable_merging(new_block->host, new_block->max_length);
-        }
-    }
-
     new_ram_size = MAX(old_ram_size,
               (new_block->offset + new_block->max_length) >> TARGET_PAGE_BITS);
     if (new_ram_size > old_ram_size) {
@@ -1948,7 +1944,6 @@  RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
 {
     void *host;
     RAMBlock *new_block;
-    Error *local_err = NULL;
     int64_t file_size, file_align;
 
     /* Just support these ram flags by now. */
@@ -1999,12 +1994,7 @@  RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
     }
 
     new_block->host = host;
-    ram_block_add(new_block, &local_err);
-    if (local_err) {
-        g_free(new_block);
-        error_propagate(errp, local_err);
-        return NULL;
-    }
+    ram_block_add(new_block);
     return new_block;
 }
 
@@ -2066,7 +2056,6 @@  RAMBlock *qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t max_size,
                                   MemoryRegion *mr, Error **errp)
 {
     RAMBlock *new_block;
-    Error *local_err = NULL;
     int align;
 
     assert((ram_flags & ~(RAM_SHARED | RAM_RESIZEABLE | RAM_PREALLOC |
@@ -2084,14 +2073,17 @@  RAMBlock *qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t max_size,
     }
     new_block->resized = resized;
 
-    new_block->host = host;
-    ram_block_add(new_block, &local_err);
-    if (local_err) {
-        ram_block_destroy_guest_memfd(new_block);
-        g_free(new_block);
-        error_propagate(errp, local_err);
-        return NULL;
+    if (!host) {
+        host = ram_block_alloc_host(new_block, errp);
+        if (!host) {
+            ram_block_destroy_guest_memfd(new_block);
+            g_free(new_block);
+            return NULL;
+        }
     }
+
+    new_block->host = host;
+    ram_block_add(new_block);
     return new_block;
 }