Skip to contentSkip to conversation

Memory: use memory address space for cpu-memory

PATCH In review Anthony Xu 3 participants 8 messages May 16, 2017 – May 19, 2017

lore
Share & tools
lore

Anthony Xu

author
#0 lore raw

PATCH 1/1 Memory: use memory address space for cpu-memory

If cpu-memory address space is same as memory address space, use memory address space for cpu-memory address space.

any memory region change causeaddress space to rebuild PhysPageMap, rebuilding PhysPageMap is very expensive.

removing cpu-memory address space reduces the guest boot time and memory usage.

---
 cpus.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
cpus.c +7 -2
index 740b8dc..15c7a6a 100644
@@ -1748,8 +1748,13 @@ void qemu_init_vcpu(CPUState *cpu)
         /* If the target cpu hasn't set up any address spaces itself,
          * give it the default one.
          */
-        AddressSpace *as = address_space_init_shareable(cpu->memory,
-                                                        "cpu-memory");
+        AddressSpace *as;
+        if (cpu->memory == address_space_memory.root) {
+            address_space_memory.ref_count++;
+            as = &address_space_memory;
+        } else {
+            as = address_space_init_shareable(cpu->memory, "cpu-memory");
+        }
         cpu->num_ases = 1;
         cpu_address_space_init(cpu, as, 0);
     }
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 19 lines · On Tue, 16 May 2017 15:15:23 -0700 Anthony Xu <[email protected]> wrote:

If cpu-memory address space is same as memory address space, use memory address space for cpu-memory address space.

any memory region change causeaddress space to rebuild PhysPageMap, rebuilding PhysPageMap is very expensive.

removing cpu-memory address space reduces the guest boot time and memory usage.

---
 cpus.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
cpus.c +0 -0
index 740b8dc..15c7a6a 100644

cpus.c void qemu_init_vcpu(CPUState *cpu) · line 1753

Quoted 8 lines
cpus.c +3 -2
@@ -1748,8 +1748,13 @@ void qemu_init_vcpu(CPUState *cpu)
         /* If the target cpu hasn't set up any address spaces itself,
          * give it the default one.
          */
-        AddressSpace *as = address_space_init_shareable(cpu->memory,
-                                                        "cpu-memory");
+        AddressSpace *as;
+        if (cpu->memory == address_space_memory.root) {
+            address_space_memory.ref_count++;

probably this would cause reference leak when vcpu is destroyed

Quoted 7 lines
cpus.c +4 -0
+            as = &address_space_memory;
+        } else {
+            as = address_space_init_shareable(cpu->memory, "cpu-memory");
+        }
         cpu->num_ases = 1;
         cpu_address_space_init(cpu, as, 0);
     }
#2 lore raw
Quoted 15 lines
Quoted 15 lines

If cpu-memory address space is same as memory address space, use memory address space for cpu-memory address space.

any memory region change causeaddress space to rebuild PhysPageMap, rebuilding PhysPageMap is very expensive.

removing cpu-memory address space reduces the guest boot time and memory usage.

---
 cpus.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
Quoted 14 lines

cpus.c void qemu_init_vcpu(CPUState *cpu) · line 1753

Quoted 13 lines
cpus.c +3 -2
index 740b8dc..15c7a6a 100644
@@ -1748,8 +1748,13 @@ void qemu_init_vcpu(CPUState *cpu)
         /* If the target cpu hasn't set up any address spaces itself,
          * give it the default one.
          */
-        AddressSpace *as = address_space_init_shareable(cpu->memory,
-                                                        "cpu-memory");
+        AddressSpace *as;
+        if (cpu->memory == address_space_memory.root) {
+            address_space_memory.ref_count++;

probably this would cause reference leak when vcpu is destroyed

I thought address_space_destroy is called when vcpu is unplugged, seems that's not the case, then ref_count++ is not needed.

Any other comments?

Thanks,
Anthony

#3 lore raw
Quoted 8 lines · Anthony
Quoted 6 lines

Review

Quoted 5 lines
(quoted diff) +3 -2
-        AddressSpace *as = address_space_init_shareable(cpu->memory,
-                                                        "cpu-memory");
+        AddressSpace *as;
+        if (cpu->memory == address_space_memory.root) {
+            address_space_memory.ref_count++;

probably this would cause reference leak when vcpu is destroyed

I thought address_space_destroy is called when vcpu is unplugged, seems that's not the case, then ref_count++ is not needed.

It should be called. Alternatively you could try adding a new function to mark address_space_memory as a never-destroyed AddressSpace:

include/exec/memory.h +1 -0
index 99e0f54d86..b27b288c8f 100644
@@ -290,6 +290,7 @@ struct AddressSpace {
     MemoryRegion *root;
     int ref_count;
     bool malloced;
+    bool shared;
 
     /* Accessed via RCU.  */
     struct FlatView *current_map;
memory.c +11 -1
index b727f5ec0e..190cd3d5ce 100644
@@ -2432,6 +2432,7 @@ void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
     as->ref_count = 1;
     as->root = root;
     as->malloced = false;
+    as->shared = false;
     as->current_map = g_new(FlatView, 1);
     flatview_init(as->current_map);
     as->ioeventfd_nb = 0;
@@ -2460,12 +2461,18 @@ static void do_address_space_destroy(AddressSpace *as)
     }
 }
 
+void address_space_init_static(AddressSpace *as, MemoryRegion *root, const char *name)
+{
+    address_space_init(as, root, name);
+    as->shared = true;
+}
+
 AddressSpace *address_space_init_shareable(MemoryRegion *root, const char *name)
 {
     AddressSpace *as;
 
     QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
-        if (root == as->root && as->malloced) {
+        if (root == as->root && as->shared) {
             as->ref_count++;
             return as;
         }
@@ -2474,6 +2481,7 @@ AddressSpace *address_space_init_shareable(MemoryRegion *root, const char *name)
     as = g_malloc0(sizeof *as);
     address_space_init(as, root, name);
     as->malloced = true;
+    as->shared = true;
     return as;
 }
 
@@ -2485,6 +2493,8 @@ void address_space_destroy(AddressSpace *as)
     if (as->ref_count) {
         return;
     }
+    assert(!as->shared || as->malloced);
+
     /* Flush out anything from MemoryListeners listening in on this */
     memory_region_transaction_begin();
     as->root = NULL;

then CPUs can keep using address_space_init_shareable.

Paolo

#4 lore raw
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
(quoted diff) +3 -2
-        AddressSpace *as = address_space_init_shareable(cpu->memory,
-                                                        "cpu-memory");
+        AddressSpace *as;
+        if (cpu->memory == address_space_memory.root) {
+            address_space_memory.ref_count++;
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

probably this would cause reference leak when vcpu is destroyed

I thought address_space_destroy is called when vcpu is unplugged, seems that's not the case, then ref_count++ is not needed.

It should be called. Alternatively you could try adding a new function to mark address_space_memory as a never-destroyed AddressSpace:

This patch would do it, could you please submit this patch?

address_space_destroy is not called when vcpu is unplugged, that likely causes memory leak. I will take a look when I have time.
If someone can take a look now, that'd be great.

Thanks,
Anthony

Quoted 71 lines
include/exec/memory.h +1 -0
index 99e0f54d86..b27b288c8f 100644
@@ -290,6 +290,7 @@ struct AddressSpace {
     MemoryRegion *root;
     int ref_count;
     bool malloced;
+    bool shared;
 
     /* Accessed via RCU.  */
     struct FlatView *current_map;
memory.c +1 -0
index b727f5ec0e..190cd3d5ce 100644
@@ -2432,6 +2432,7 @@ void address_space_init(AddressSpace *as,
 MemoryRegion *root, const char *name)
     as->ref_count = 1;
     as->root = root;
     as->malloced = false;
+    as->shared = false;
     as->current_map = g_new(FlatView, 1);
     flatview_init(as->current_map);
     as->ioeventfd_nb = 0;
memory.c +7 -1
@@ -2460,12 +2461,18 @@ static void
 do_address_space_destroy(AddressSpace *as)
     }
 }
 
+void address_space_init_static(AddressSpace *as, MemoryRegion *root,
 const char *name)
+{
+    address_space_init(as, root, name);
+    as->shared = true;
+}
+
 AddressSpace *address_space_init_shareable(MemoryRegion *root, const
 char *name)
 {
     AddressSpace *as;
 
     QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
-        if (root == as->root && as->malloced) {
+        if (root == as->root && as->shared) {
             as->ref_count++;
             return as;
         }
memory.c +3 -0
@@ -2474,6 +2481,7 @@ AddressSpace
 *address_space_init_shareable(MemoryRegion *root, const char *name)
     as = g_malloc0(sizeof *as);
     address_space_init(as, root, name);
     as->malloced = true;
+    as->shared = true;
     return as;
 }
@@ -2485,6 +2493,8 @@ void address_space_destroy(AddressSpace *as)
     if (as->ref_count) {
         return;
     }
+    assert(!as->shared || as->malloced);
+
     /* Flush out anything from MemoryListeners listening in on this */
     memory_region_transaction_begin();
     as->root = NULL;

then CPUs can keep using address_space_init_shareable.

Paolo

#5 lore raw
Quoted 4 lines · Anthony
Quoted 3 lines

It should be called. Alternatively you could try adding a new function to mark address_space_memory as a never-destroyed AddressSpace:

This patch would do it, could you please submit this patch?

If you have tested it (together with the change in the initialization of address_space_memory), I can do that.

Thanks,

Paolo

#6 lore raw
Quoted 9 lines · Anthony
Quoted 4 lines
Quoted 3 lines

It should be called. Alternatively you could try adding a new function to mark address_space_memory as a never-destroyed AddressSpace:

This patch would do it, could you please submit this patch?

If you have tested it (together with the change in the initialization of address_space_memory), I can do that.

Based on your patch, I added the change in the initialization of address_space_memory. It works well in my setup, cpu-memory address space doesn't show up as we expected.

Anthony

exec.c +1 -1
index 96e3ac9..746dbbc 100644
@@ -2712,7 +2712,7 @@ static void memory_map_init(void)
     system_memory = g_malloc(sizeof(*system_memory));
 
     memory_region_init(system_memory, NULL, "system", UINT64_MAX);
-    address_space_init(&address_space_memory, system_memory, "memory");
+    address_space_init_static(&address_space_memory, system_memory, "memory");
 
     system_io = g_malloc(sizeof(*system_io));
     memory_region_init_io(system_io, NULL, &unassigned_io_ops, NULL, "io",
include/exec/memory.h +11 -0
index b27b288..6f44b79 100644
@@ -1395,6 +1395,17 @@ MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
 
 /**
+ * address_space_init_static: initializes an static address space
+ *
+ * @as: an uninitialized #AddressSpace
+ * @root: a #MemoryRegion that routes addresses for the address space
+ * @name: an address space name.  The name is only used for debugging
+ *        output.
+ */
+void address_space_init_static(AddressSpace *as, MemoryRegion *root,
+       const char *name);
+
+/**
  * address_space_init_shareable: return an address space for a memory region,
  *                               creating it if it does not already exist
  *
memory.c +2 -1
index 190cd3d..6c933d8 100644
@@ -2461,7 +2461,8 @@ static void do_address_space_destroy(AddressSpace *as)
     }
 }
 
-void address_space_init_static(AddressSpace *as, MemoryRegion *root, const char *name)
+void address_space_init_static(AddressSpace *as, MemoryRegion *root,
+       const char *name)
 {
     address_space_init(as, root, name);
     as->shared = true;
#7 lore raw
Quoted 6 lines · Anthony
Quoted 6 lines · Anthony
Quoted 4 lines
Quoted 3 lines

It should be called. Alternatively you could try adding a new function to mark address_space_memory as a never-destroyed AddressSpace:

This patch would do it, could you please submit this patch?

Quoted 7 lines
Quoted 3 lines

If you have tested it (together with the change in the initialization of address_space_memory), I can do that.

Based on your patch, I added the change in the initialization of address_space_memory. It works well in my setup, cpu-memory address space doesn't show up as we expected.

Thanks.

I am also thinking of resolving aliases early, and sharing the AddressSpaceDispatch if the root memory region is the same. This hopefully will also reduce memory consumption and speed up AddressSpace updates.

Paolo

Quoted 52 lines

Anthony

exec.c +1 -1
index 96e3ac9..746dbbc 100644
@@ -2712,7 +2712,7 @@ static void memory_map_init(void)
     system_memory = g_malloc(sizeof(*system_memory));
 
     memory_region_init(system_memory, NULL, "system", UINT64_MAX);
-    address_space_init(&address_space_memory, system_memory, "memory");
+    address_space_init_static(&address_space_memory, system_memory, "memory");
 
     system_io = g_malloc(sizeof(*system_io));
     memory_region_init_io(system_io, NULL, &unassigned_io_ops, NULL, "io",
include/exec/memory.h +11 -0
index b27b288..6f44b79 100644
@@ -1395,6 +1395,17 @@ MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
 
 /**
+ * address_space_init_static: initializes an static address space
+ *
+ * @as: an uninitialized #AddressSpace
+ * @root: a #MemoryRegion that routes addresses for the address space
+ * @name: an address space name.  The name is only used for debugging
+ *        output.
+ */
+void address_space_init_static(AddressSpace *as, MemoryRegion *root,
+       const char *name);
+
+/**
  * address_space_init_shareable: return an address space for a memory region,
  *                               creating it if it does not already exist
  *
memory.c +2 -1
index 190cd3d..6c933d8 100644
@@ -2461,7 +2461,8 @@ static void do_address_space_destroy(AddressSpace *as)
     }
 }
 
-void address_space_init_static(AddressSpace *as, MemoryRegion *root, const char *name)
+void address_space_init_static(AddressSpace *as, MemoryRegion *root,
+       const char *name)
 {
     address_space_init(as, root, name);
     as->shared = true;