Blame SOURCES/kvm-memory-Move-AddressSpaceDispatch-from-AddressSpace-t.patch

4a2fec
From 30acf078fced0fac523d4773fe06983d4cfce64d Mon Sep 17 00:00:00 2001
4a2fec
From: David Gibson <dgibson@redhat.com>
4a2fec
Date: Thu, 16 Nov 2017 03:07:17 +0100
4a2fec
Subject: [PATCH 13/30] memory: Move AddressSpaceDispatch from AddressSpace to
4a2fec
 FlatView
4a2fec
4a2fec
RH-Author: David Gibson <dgibson@redhat.com>
4a2fec
Message-id: <20171116030732.8560-8-dgibson@redhat.com>
4a2fec
Patchwork-id: 77697
4a2fec
O-Subject: [PATCH 07/22] memory: Move AddressSpaceDispatch from AddressSpace to FlatView
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
As we are going to share FlatView's between AddressSpace's,
4a2fec
and AddressSpaceDispatch is a structure to perform quick lookup
4a2fec
in FlatView, this moves ASD to FlatView.
4a2fec
4a2fec
After previosly open coded ASD rendering, we can also remove
4a2fec
as->next_dispatch as the new FlatView pointer is stored
4a2fec
on a stack and set to an AS atomically.
4a2fec
4a2fec
flatview_destroy() is executed under RCU instead of
4a2fec
address_space_dispatch_free() now.
4a2fec
4a2fec
This makes mem_begin/mem_commit to work with ASD and mem_add with FV
4a2fec
as later on mem_add will be taking FV as an argument anyway.
4a2fec
4a2fec
This should cause no behavioural change.
4a2fec
4a2fec
Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
4a2fec
Message-Id: <20170921085110.25598-5-aik@ozlabs.ru>
4a2fec
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
(cherry picked from commit 66a6df1dc6d5b28cc3e65db0d71683fbdddc6b62)
4a2fec
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
4a2fec
Conflicts:
4a2fec
	exec.c
4a2fec
4a2fec
Conflicts because we applied d5e5fafd "exec: add page_mask for
4a2fec
flatview_do_translate" out of order downstream.
4a2fec
4a2fec
Signed-off-by: David Gibson <dgibson@redhat.com>
4a2fec
---
4a2fec
 exec.c                         | 41 +++++++++++------------------------------
4a2fec
 include/exec/memory-internal.h | 12 +++++++-----
4a2fec
 include/exec/memory.h          |  2 --
4a2fec
 memory.c                       | 31 ++++++++++++++++++++++++-------
4a2fec
 4 files changed, 42 insertions(+), 44 deletions(-)
4a2fec
4a2fec
diff --git a/exec.c b/exec.c
4a2fec
index 4fed7b7..3c0b4f8 100644
4a2fec
--- a/exec.c
4a2fec
+++ b/exec.c
4a2fec
@@ -188,8 +188,6 @@ typedef struct PhysPageMap {
4a2fec
 } PhysPageMap;
4a2fec
 
4a2fec
 struct AddressSpaceDispatch {
4a2fec
-    struct rcu_head rcu;
4a2fec
-
4a2fec
     MemoryRegionSection *mru_section;
4a2fec
     /* This is a multi-level map on the physical address space.
4a2fec
      * The bottom level has pointers to MemoryRegionSections.
4a2fec
@@ -493,7 +491,7 @@ static MemoryRegionSection address_space_do_translate(AddressSpace *as,
4a2fec
     }
4a2fec
 
4a2fec
     for (;;) {
4a2fec
-        AddressSpaceDispatch *d = atomic_rcu_read(&as->dispatch);
4a2fec
+        AddressSpaceDispatch *d = address_space_to_dispatch(as);
4a2fec
         section = address_space_translate_internal(d, addr, &addr, &plen, is_mmio);
4a2fec
 
4a2fec
         iommu_mr = memory_region_get_iommu(section->mr);
4a2fec
@@ -1233,7 +1231,7 @@ hwaddr memory_region_section_get_iotlb(CPUState *cpu,
4a2fec
     } else {
4a2fec
         AddressSpaceDispatch *d;
4a2fec
 
4a2fec
-        d = atomic_rcu_read(&section->address_space->dispatch);
4a2fec
+        d = address_space_to_dispatch(section->address_space);
4a2fec
         iotlb = section - d->map.sections;
4a2fec
         iotlb += xlat;
4a2fec
     }
4a2fec
@@ -1358,9 +1356,9 @@ static void register_multipage(AddressSpaceDispatch *d,
4a2fec
     phys_page_set(d, start_addr >> TARGET_PAGE_BITS, num_pages, section_index);
4a2fec
 }
4a2fec
 
4a2fec
-void mem_add(AddressSpace *as, MemoryRegionSection *section)
4a2fec
+void mem_add(AddressSpace *as, FlatView *fv, MemoryRegionSection *section)
4a2fec
 {
4a2fec
-    AddressSpaceDispatch *d = as->next_dispatch;
4a2fec
+    AddressSpaceDispatch *d = flatview_to_dispatch(fv);
4a2fec
     MemoryRegionSection now = *section, remain = *section;
4a2fec
     Int128 page_size = int128_make64(TARGET_PAGE_SIZE);
4a2fec
 
4a2fec
@@ -2683,7 +2681,7 @@ static void io_mem_init(void)
4a2fec
                           NULL, UINT64_MAX);
4a2fec
 }
4a2fec
 
4a2fec
-void mem_begin(AddressSpace *as)
4a2fec
+AddressSpaceDispatch *mem_begin(AddressSpace *as)
4a2fec
 {
4a2fec
     AddressSpaceDispatch *d = g_new0(AddressSpaceDispatch, 1);
4a2fec
     uint16_t n;
4a2fec
@@ -2699,26 +2697,19 @@ void mem_begin(AddressSpace *as)
4a2fec
 
4a2fec
     d->phys_map  = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .skip = 1 };
4a2fec
     d->as = as;
4a2fec
-    as->next_dispatch = d;
4a2fec
+
4a2fec
+    return d;
4a2fec
 }
4a2fec
 
4a2fec
-static void address_space_dispatch_free(AddressSpaceDispatch *d)
4a2fec
+void address_space_dispatch_free(AddressSpaceDispatch *d)
4a2fec
 {
4a2fec
     phys_sections_free(&d->map);
4a2fec
     g_free(d);
4a2fec
 }
4a2fec
 
4a2fec
-void mem_commit(AddressSpace *as)
4a2fec
+void mem_commit(AddressSpaceDispatch *d)
4a2fec
 {
4a2fec
-    AddressSpaceDispatch *cur = as->dispatch;
4a2fec
-    AddressSpaceDispatch *next = as->next_dispatch;
4a2fec
-
4a2fec
-    phys_page_compact_all(next, next->map.nodes_nb);
4a2fec
-
4a2fec
-    atomic_rcu_set(&as->dispatch, next);
4a2fec
-    if (cur) {
4a2fec
-        call_rcu(cur, address_space_dispatch_free, rcu);
4a2fec
-    }
4a2fec
+    phys_page_compact_all(d, d->map.nodes_nb);
4a2fec
 }
4a2fec
 
4a2fec
 static void tcg_commit(MemoryListener *listener)
4a2fec
@@ -2734,21 +2725,11 @@ static void tcg_commit(MemoryListener *listener)
4a2fec
      * We reload the dispatch pointer now because cpu_reloading_memory_map()
4a2fec
      * may have split the RCU critical section.
4a2fec
      */
4a2fec
-    d = atomic_rcu_read(&cpuas->as->dispatch);
4a2fec
+    d = address_space_to_dispatch(cpuas->as);
4a2fec
     atomic_rcu_set(&cpuas->memory_dispatch, d);
4a2fec
     tlb_flush(cpuas->cpu);
4a2fec
 }
4a2fec
 
4a2fec
-void address_space_destroy_dispatch(AddressSpace *as)
4a2fec
-{
4a2fec
-    AddressSpaceDispatch *d = as->dispatch;
4a2fec
-
4a2fec
-    atomic_rcu_set(&as->dispatch, NULL);
4a2fec
-    if (d) {
4a2fec
-        call_rcu(d, address_space_dispatch_free, rcu);
4a2fec
-    }
4a2fec
-}
4a2fec
-
4a2fec
 static void memory_map_init(void)
4a2fec
 {
4a2fec
     system_memory = g_malloc(sizeof(*system_memory));
4a2fec
diff --git a/include/exec/memory-internal.h b/include/exec/memory-internal.h
4a2fec
index 9abde2f..6e08eda 100644
4a2fec
--- a/include/exec/memory-internal.h
4a2fec
+++ b/include/exec/memory-internal.h
4a2fec
@@ -22,16 +22,18 @@
4a2fec
 #ifndef CONFIG_USER_ONLY
4a2fec
 typedef struct AddressSpaceDispatch AddressSpaceDispatch;
4a2fec
 
4a2fec
-void address_space_destroy_dispatch(AddressSpace *as);
4a2fec
-
4a2fec
 extern const MemoryRegionOps unassigned_mem_ops;
4a2fec
 
4a2fec
 bool memory_region_access_valid(MemoryRegion *mr, hwaddr addr,
4a2fec
                                 unsigned size, bool is_write);
4a2fec
 
4a2fec
-void mem_add(AddressSpace *as, MemoryRegionSection *section);
4a2fec
-void mem_begin(AddressSpace *as);
4a2fec
-void mem_commit(AddressSpace *as);
4a2fec
+void mem_add(AddressSpace *as, FlatView *fv, MemoryRegionSection *section);
4a2fec
+AddressSpaceDispatch *mem_begin(AddressSpace *as);
4a2fec
+void mem_commit(AddressSpaceDispatch *d);
4a2fec
+
4a2fec
+AddressSpaceDispatch *address_space_to_dispatch(AddressSpace *as);
4a2fec
+AddressSpaceDispatch *flatview_to_dispatch(FlatView *fv);
4a2fec
+void address_space_dispatch_free(AddressSpaceDispatch *d);
4a2fec
 
4a2fec
 #endif
4a2fec
 #endif
4a2fec
diff --git a/include/exec/memory.h b/include/exec/memory.h
4a2fec
index 9ee0f2e..8f26d63 100644
4a2fec
--- a/include/exec/memory.h
4a2fec
+++ b/include/exec/memory.h
4a2fec
@@ -326,8 +326,6 @@ struct AddressSpace {
4a2fec
 
4a2fec
     int ioeventfd_nb;
4a2fec
     struct MemoryRegionIoeventfd *ioeventfds;
4a2fec
-    struct AddressSpaceDispatch *dispatch;
4a2fec
-    struct AddressSpaceDispatch *next_dispatch;
4a2fec
     QTAILQ_HEAD(memory_listeners_as, MemoryListener) listeners;
4a2fec
     QTAILQ_ENTRY(AddressSpace) address_spaces_link;
4a2fec
 };
4a2fec
diff --git a/memory.c b/memory.c
4a2fec
index f51e499..41e2e67 100644
4a2fec
--- a/memory.c
4a2fec
+++ b/memory.c
4a2fec
@@ -229,6 +229,7 @@ struct FlatView {
4a2fec
     FlatRange *ranges;
4a2fec
     unsigned nr;
4a2fec
     unsigned nr_allocated;
4a2fec
+    struct AddressSpaceDispatch *dispatch;
4a2fec
 };
4a2fec
 
4a2fec
 typedef struct AddressSpaceOps AddressSpaceOps;
4a2fec
@@ -289,6 +290,9 @@ static void flatview_destroy(FlatView *view)
4a2fec
 {
4a2fec
     int i;
4a2fec
 
4a2fec
+    if (view->dispatch) {
4a2fec
+        address_space_dispatch_free(view->dispatch);
4a2fec
+    }
4a2fec
     for (i = 0; i < view->nr; i++) {
4a2fec
         memory_region_unref(view->ranges[i].mr);
4a2fec
     }
4a2fec
@@ -304,10 +308,25 @@ static bool flatview_ref(FlatView *view)
4a2fec
 static void flatview_unref(FlatView *view)
4a2fec
 {
4a2fec
     if (atomic_fetch_dec(&view->ref) == 1) {
4a2fec
-        flatview_destroy(view);
4a2fec
+        call_rcu(view, flatview_destroy, rcu);
4a2fec
     }
4a2fec
 }
4a2fec
 
4a2fec
+static FlatView *address_space_to_flatview(AddressSpace *as)
4a2fec
+{
4a2fec
+    return atomic_rcu_read(&as->current_map);
4a2fec
+}
4a2fec
+
4a2fec
+AddressSpaceDispatch *flatview_to_dispatch(FlatView *fv)
4a2fec
+{
4a2fec
+    return fv->dispatch;
4a2fec
+}
4a2fec
+
4a2fec
+AddressSpaceDispatch *address_space_to_dispatch(AddressSpace *as)
4a2fec
+{
4a2fec
+    return flatview_to_dispatch(address_space_to_flatview(as));
4a2fec
+}
4a2fec
+
4a2fec
 static bool can_merge(FlatRange *r1, FlatRange *r2)
4a2fec
 {
4a2fec
     return int128_eq(addrrange_end(r1->addr), r2->addr.start)
4a2fec
@@ -890,13 +909,13 @@ static void address_space_update_topology(AddressSpace *as)
4a2fec
     FlatView *new_view = generate_memory_topology(as->root);
4a2fec
     int i;
4a2fec
 
4a2fec
-    mem_begin(as);
4a2fec
+    new_view->dispatch = mem_begin(as);
4a2fec
     for (i = 0; i < new_view->nr; i++) {
4a2fec
         MemoryRegionSection mrs =
4a2fec
             section_from_flat_range(&new_view->ranges[i], as);
4a2fec
-        mem_add(as, &mrs);
4a2fec
+        mem_add(as, new_view, &mrs);
4a2fec
     }
4a2fec
-    mem_commit(as);
4a2fec
+    mem_commit(new_view->dispatch);
4a2fec
 
4a2fec
     if (!QTAILQ_EMPTY(&as->listeners)) {
4a2fec
         address_space_update_topology_pass(as, old_view, new_view, false);
4a2fec
@@ -905,7 +924,7 @@ static void address_space_update_topology(AddressSpace *as)
4a2fec
 
4a2fec
     /* Writes are protected by the BQL.  */
4a2fec
     atomic_rcu_set(&as->current_map, new_view);
4a2fec
-    call_rcu(old_view, flatview_unref, rcu);
4a2fec
+    flatview_unref(old_view);
4a2fec
 
4a2fec
     /* Note that all the old MemoryRegions are still alive up to this
4a2fec
      * point.  This relieves most MemoryListeners from the need to
4a2fec
@@ -2635,7 +2654,6 @@ void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
4a2fec
     QTAILQ_INIT(&as->listeners);
4a2fec
     QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
4a2fec
     as->name = g_strdup(name ? name : "anonymous");
4a2fec
-    as->dispatch = NULL;
4a2fec
     memory_region_update_pending |= root->enabled;
4a2fec
     memory_region_transaction_commit();
4a2fec
 }
4a2fec
@@ -2644,7 +2662,6 @@ static void do_address_space_destroy(AddressSpace *as)
4a2fec
 {
4a2fec
     bool do_free = as->malloced;
4a2fec
 
4a2fec
-    address_space_destroy_dispatch(as);
4a2fec
     assert(QTAILQ_EMPTY(&as->listeners));
4a2fec
 
4a2fec
     flatview_unref(as->current_map);
4a2fec
-- 
4a2fec
1.8.3.1
4a2fec