Blame SOURCES/kvm-memory-Share-FlatView-s-and-dispatch-trees-between-a.patch

4a2fec
From f47eec19ea8dd0f693755cf42e4962e3ec43b06b Mon Sep 17 00:00:00 2001
4a2fec
From: David Gibson <dgibson@redhat.com>
4a2fec
Date: Thu, 16 Nov 2017 03:07:25 +0100
4a2fec
Subject: [PATCH 21/30] memory: Share FlatView's and dispatch trees between
4a2fec
 address spaces
4a2fec
4a2fec
RH-Author: David Gibson <dgibson@redhat.com>
4a2fec
Message-id: <20171116030732.8560-16-dgibson@redhat.com>
4a2fec
Patchwork-id: 77707
4a2fec
O-Subject: [PATCH 15/22] memory: Share FlatView's and dispatch trees between address spaces
4a2fec
Bugzilla: 1481593
4a2fec
RH-Acked-by: Thomas Huth <thuth@redhat.com>
4a2fec
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
RH-Acked-by: Eduardo Habkost <ehabkost@redhat.com>
4a2fec
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
4a2fec
4a2fec
From: Alexey Kardashevskiy <aik@ozlabs.ru>
4a2fec
4a2fec
This allows sharing flat views between address spaces (AS) when
4a2fec
the same root memory region is used when creating a new address space.
4a2fec
This is done by walking through all ASes and caching one FlatView per
4a2fec
a physical root MR (i.e. not aliased).
4a2fec
4a2fec
This removes search for duplicates from address_space_init_shareable() as
4a2fec
FlatViews are shared elsewhere and keeping as::ref_count correct seems
4a2fec
an unnecessary and useless complication.
4a2fec
4a2fec
This should cause no change and memory use or boot time yet.
4a2fec
4a2fec
Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
4a2fec
Message-Id: <20170921085110.25598-13-aik@ozlabs.ru>
4a2fec
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
(cherry picked from commit 967dc9b1194a9281124b2e1ce67b6c3359a2138f)
4a2fec
4a2fec
Signed-off-by: David Gibson <dgibson@redhat.com>
4a2fec
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
---
4a2fec
 memory.c | 56 +++++++++++++++++++++++++++++++++++++++++++++-----------
4a2fec
 1 file changed, 45 insertions(+), 11 deletions(-)
4a2fec
4a2fec
diff --git a/memory.c b/memory.c
4a2fec
index 1f58d29..f0c8642 100644
4a2fec
--- a/memory.c
4a2fec
+++ b/memory.c
4a2fec
@@ -47,6 +47,8 @@ static QTAILQ_HEAD(memory_listeners, MemoryListener) memory_listeners
4a2fec
 static QTAILQ_HEAD(, AddressSpace) address_spaces
4a2fec
     = QTAILQ_HEAD_INITIALIZER(address_spaces);
4a2fec
 
4a2fec
+static GHashTable *flat_views;
4a2fec
+
4a2fec
 typedef struct AddrRange AddrRange;
4a2fec
 
4a2fec
 /*
4a2fec
@@ -760,6 +762,7 @@ static FlatView *generate_memory_topology(MemoryRegion *mr)
4a2fec
         flatview_add_to_dispatch(view, &mrs);
4a2fec
     }
4a2fec
     address_space_dispatch_compact(view->dispatch);
4a2fec
+    g_hash_table_replace(flat_views, mr, view);
4a2fec
 
4a2fec
     return view;
4a2fec
 }
4a2fec
@@ -929,11 +932,47 @@ static void address_space_update_topology_pass(AddressSpace *as,
4a2fec
     }
4a2fec
 }
4a2fec
 
4a2fec
-static void address_space_update_topology(AddressSpace *as)
4a2fec
+static void flatviews_init(void)
4a2fec
+{
4a2fec
+    if (flat_views) {
4a2fec
+        return;
4a2fec
+    }
4a2fec
+
4a2fec
+    flat_views = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
4a2fec
+                                       (GDestroyNotify) flatview_unref);
4a2fec
+}
4a2fec
+
4a2fec
+static void flatviews_reset(void)
4a2fec
+{
4a2fec
+    AddressSpace *as;
4a2fec
+
4a2fec
+    if (flat_views) {
4a2fec
+        g_hash_table_unref(flat_views);
4a2fec
+        flat_views = NULL;
4a2fec
+    }
4a2fec
+    flatviews_init();
4a2fec
+
4a2fec
+    /* Render unique FVs */
4a2fec
+    QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
4a2fec
+        MemoryRegion *physmr = memory_region_get_flatview_root(as->root);
4a2fec
+
4a2fec
+        if (g_hash_table_lookup(flat_views, physmr)) {
4a2fec
+            continue;
4a2fec
+        }
4a2fec
+
4a2fec
+        generate_memory_topology(physmr);
4a2fec
+    }
4a2fec
+}
4a2fec
+
4a2fec
+static void address_space_set_flatview(AddressSpace *as)
4a2fec
 {
4a2fec
     FlatView *old_view = address_space_get_flatview(as);
4a2fec
-    MemoryRegion *physmr = memory_region_get_flatview_root(old_view->root);
4a2fec
-    FlatView *new_view = generate_memory_topology(physmr);
4a2fec
+    MemoryRegion *physmr = memory_region_get_flatview_root(as->root);
4a2fec
+    FlatView *new_view = g_hash_table_lookup(flat_views, physmr);
4a2fec
+
4a2fec
+    assert(new_view);
4a2fec
+
4a2fec
+    flatview_ref(new_view);
4a2fec
 
4a2fec
     if (!QTAILQ_EMPTY(&as->listeners)) {
4a2fec
         address_space_update_topology_pass(as, old_view, new_view, false);
4a2fec
@@ -969,10 +1008,12 @@ void memory_region_transaction_commit(void)
4a2fec
     --memory_region_transaction_depth;
4a2fec
     if (!memory_region_transaction_depth) {
4a2fec
         if (memory_region_update_pending) {
4a2fec
+            flatviews_reset();
4a2fec
+
4a2fec
             MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);
4a2fec
 
4a2fec
             QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
4a2fec
-                address_space_update_topology(as);
4a2fec
+                address_space_set_flatview(as);
4a2fec
                 address_space_update_ioeventfds(as);
4a2fec
             }
4a2fec
             memory_region_update_pending = false;
4a2fec
@@ -2695,13 +2736,6 @@ AddressSpace *address_space_init_shareable(MemoryRegion *root, const char *name)
4a2fec
 {
4a2fec
     AddressSpace *as;
4a2fec
 
4a2fec
-    QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
4a2fec
-        if (root == as->root && as->malloced) {
4a2fec
-            as->ref_count++;
4a2fec
-            return as;
4a2fec
-        }
4a2fec
-    }
4a2fec
-
4a2fec
     as = g_malloc0(sizeof *as);
4a2fec
     address_space_init(as, root, name);
4a2fec
     as->malloced = true;
4a2fec
-- 
4a2fec
1.8.3.1
4a2fec