Patchwork [RFC,v2,03/20] memory: merge adjacent segments of a single memory region

login
register
mail settings
Submitter Avi Kivity
Date June 27, 2011, 1:21 p.m.
Message ID <1309180927-19003-4-git-send-email-avi@redhat.com>
Download mbox | patch
Permalink /patch/102176/
State New
Headers show

Comments

Avi Kivity - June 27, 2011, 1:21 p.m.
Simple implementations of memory routers, for example the Cirrus VGA memory banks
or the 440FX PAM registers can generate adjacent memory regions which are contiguous.
Detect these and merge them; this saves kvm memory slots and shortens lookup times.

Signed-off-by: Avi Kivity <avi@redhat.com>
---
 memory.c |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)
Jan Kiszka - June 27, 2011, 1:56 p.m.
On 2011-06-27 15:21, Avi Kivity wrote:
> Simple implementations of memory routers, for example the Cirrus VGA memory banks
> or the 440FX PAM registers can generate adjacent memory regions which are contiguous.
> Detect these and merge them; this saves kvm memory slots and shortens lookup times.

That reminds me of KVM_CAP_JOIN_MEMORY_REGIONS_WORKS. Have you checked
if things still work in the absence of the feature or if we need to lift
our requirements on the host kernel (would be 2.6.30, not that problematic)?

Jan
Avi Kivity - June 27, 2011, 2:11 p.m.
On 06/27/2011 04:56 PM, Jan Kiszka wrote:
> On 2011-06-27 15:21, Avi Kivity wrote:
> >  Simple implementations of memory routers, for example the Cirrus VGA memory banks
> >  or the 440FX PAM registers can generate adjacent memory regions which are contiguous.
> >  Detect these and merge them; this saves kvm memory slots and shortens lookup times.
>
> That reminds me of KVM_CAP_JOIN_MEMORY_REGIONS_WORKS. Have you checked
> if things still work in the absence of the feature or if we need to lift
> our requirements on the host kernel (would be 2.6.30, not that problematic)?

Good catch.

We should probably disable merging if it doesn't work.  Or we should 
declare that a kernel bug should be fixed in the kernel, not userspace, 
and backport the fix to 2.6.27.whatever.

Patch

diff --git a/memory.c b/memory.c
index d962fff..f136714 100644
--- a/memory.c
+++ b/memory.c
@@ -122,6 +122,27 @@  static void flatview_destroy(FlatView *view)
     qemu_free(view->ranges);
 }
 
+/* Attempt to simplify a view by merging ajacent ranges */
+static void flatview_simplify(FlatView *view)
+{
+    unsigned i;
+    FlatRange *r1, *r2;
+
+    for (i = 0; i + 1 < view->nr; ++i) {
+        r1 = &view->ranges[i];
+        r2 = &view->ranges[i+1];
+        if (addrrange_end(r1->addr) == r2->addr.start
+            && r1->mr == r2->mr
+            && r1->offset_in_region + r1->addr.size == r2->offset_in_region
+            && r1->dirty_log_mask == r2->dirty_log_mask) {
+            r1->addr.size += r2->addr.size;
+            memmove(r2, r2 + 1, (view->nr - (i + 2)) * sizeof(*r2));
+            --view->nr;
+            --i;
+        }
+    }
+}
+
 /* Render a memory region into the global view.  Ranges in @view obscure
  * ranges in @mr.
  */
@@ -209,6 +230,7 @@  static FlatView generate_memory_topology(MemoryRegion *mr)
     flatview_init(&view);
 
     render_memory_region(&view, mr, 0, addrrange_make(0, UINT64_MAX));
+    flatview_simplify(&view);
 
     return view;
 }