From: Denis V. Lunev <[email protected]>

generate_memory_topology() assigns view->dispatch and then fills the tree
through it, because flatview_add_to_dispatch() reaches the dispatch back
through the view. Nothing can observe the half-built tree today, since
the view is not published to an address space until the render returns,
but the ordering is there to trip over the moment anything builds a tree
for a view which is already visible.

Pass the dispatch down instead of routing it through the view, and store
it into the view once it is built. Extract the build into
flatview_build_dispatch() while doing so, since that is the natural unit
to publish.

CC: Paolo Bonzini <[email protected]>
CC: Peter Xu <[email protected]>
CC: Philippe Mathieu-Daude <[email protected]>
CC: Fabiano Rosas <[email protected]>
Signed-off-by: Denis V. Lunev <[email protected]>
---
 system/memory-internal.h |  4 +++-
 system/memory.c          | 27 +++++++++++++++++++--------
 system/physmem.c         | 16 ++++++++--------
 3 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/system/memory-internal.h b/system/memory-internal.h
index b2b9b5b0c5..d06c136744 100644
--- a/system/memory-internal.h
+++ b/system/memory-internal.h
@@ -32,7 +32,9 @@ void flatview_unref(FlatView *view);
 
 extern const MemoryRegionOps unassigned_mem_ops;
 
-void flatview_add_to_dispatch(FlatView *fv, MemoryRegionSection *section);
+void flatview_build_dispatch(FlatView *fv);
+void flatview_add_to_dispatch(FlatView *fv, AddressSpaceDispatch *d,
+                              MemoryRegionSection *section);
 AddressSpaceDispatch *address_space_dispatch_new(FlatView *fv);
 void address_space_dispatch_compact(AddressSpaceDispatch *d);
 void address_space_dispatch_free(AddressSpaceDispatch *d);
diff --git a/system/memory.c b/system/memory.c
index 56c5dd6c8a..33bd97c9a3 100644
--- a/system/memory.c
+++ b/system/memory.c
@@ -745,10 +745,27 @@ static MemoryRegion 
*memory_region_get_flatview_root(MemoryRegion *mr)
     return NULL;
 }
 
+void flatview_build_dispatch(FlatView *view)
+{
+    AddressSpaceDispatch *d;
+    int i;
+
+    assert(!view->dispatch);
+
+    d = address_space_dispatch_new(view);
+    for (i = 0; i < view->nr; i++) {
+        MemoryRegionSection mrs =
+            section_from_flat_range(&view->ranges[i], view);
+        flatview_add_to_dispatch(view, d, &mrs);
+    }
+    address_space_dispatch_compact(d);
+
+    qatomic_rcu_set(&view->dispatch, d);
+}
+
 /* Render a memory topology into a list of disjoint absolute ranges. */
 static FlatView *generate_memory_topology(MemoryRegion *mr)
 {
-    int i;
     FlatView *view;
 
     view = flatview_new(mr);
@@ -760,13 +777,7 @@ static FlatView *generate_memory_topology(MemoryRegion *mr)
     }
     flatview_simplify(view);
 
-    view->dispatch = address_space_dispatch_new(view);
-    for (i = 0; i < view->nr; i++) {
-        MemoryRegionSection mrs =
-            section_from_flat_range(&view->ranges[i], view);
-        flatview_add_to_dispatch(view, &mrs);
-    }
-    address_space_dispatch_compact(view->dispatch);
+    flatview_build_dispatch(view);
     g_hash_table_replace(flat_views, mr, view);
 
     return view;
diff --git a/system/physmem.c b/system/physmem.c
index a7300bb9d9..2cfdb1e6b4 100644
--- a/system/physmem.c
+++ b/system/physmem.c
@@ -1350,9 +1350,9 @@ static void phys_sections_free(PhysPageMap *map)
     g_free(map->nodes);
 }
 
-static void register_subpage(FlatView *fv, MemoryRegionSection *section)
+static void register_subpage(FlatView *fv, AddressSpaceDispatch *d,
+                             MemoryRegionSection *section)
 {
-    AddressSpaceDispatch *d = flatview_to_dispatch(fv);
     subpage_t *subpage;
     hwaddr base = section->offset_within_address_space
         & TARGET_PAGE_MASK;
@@ -1381,10 +1381,9 @@ static void register_subpage(FlatView *fv, 
MemoryRegionSection *section)
 }
 
 
-static void register_multipage(FlatView *fv,
+static void register_multipage(FlatView *fv, AddressSpaceDispatch *d,
                                MemoryRegionSection *section)
 {
-    AddressSpaceDispatch *d = flatview_to_dispatch(fv);
     hwaddr start_addr = section->offset_within_address_space;
     uint16_t section_index = phys_section_add(&d->map, section);
     uint64_t num_pages = int128_get64(int128_rshift(section->size,
@@ -1401,7 +1400,8 @@ static void register_multipage(FlatView *fv,
  *
  * where s stands for subpage and P for page.
  */
-void flatview_add_to_dispatch(FlatView *fv, MemoryRegionSection *section)
+void flatview_add_to_dispatch(FlatView *fv, AddressSpaceDispatch *d,
+                              MemoryRegionSection *section)
 {
     MemoryRegionSection remain = *section;
     Int128 page_size = int128_make64(TARGET_PAGE_SIZE);
@@ -1413,7 +1413,7 @@ void flatview_add_to_dispatch(FlatView *fv, 
MemoryRegionSection *section)
 
         MemoryRegionSection now = remain;
         now.size = int128_min(int128_make64(left), now.size);
-        register_subpage(fv, &now);
+        register_subpage(fv, d, &now);
         if (int128_eq(remain.size, now.size)) {
             return;
         }
@@ -1426,7 +1426,7 @@ void flatview_add_to_dispatch(FlatView *fv, 
MemoryRegionSection *section)
     if (int128_ge(remain.size, page_size)) {
         MemoryRegionSection now = remain;
         now.size = int128_and(now.size, int128_neg(page_size));
-        register_multipage(fv, &now);
+        register_multipage(fv, d, &now);
         if (int128_eq(remain.size, now.size)) {
             return;
         }
@@ -1436,7 +1436,7 @@ void flatview_add_to_dispatch(FlatView *fv, 
MemoryRegionSection *section)
     }
 
     /* register last subpage */
-    register_subpage(fv, &remain);
+    register_subpage(fv, d, &remain);
 }
 
 void qemu_flush_coalesced_mmio_buffer(void)
-- 
2.53.0


Reply via email to