get:
Show a patch.

patch:
Update a patch.

put:
Update a patch.

GET /api/patches/810923/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 810923,
    "url": "http://patchwork.ozlabs.org/api/patches/810923/?format=api",
    "web_url": "http://patchwork.ozlabs.org/project/qemu-devel/patch/20170907092010.3605-4-aik@ozlabs.ru/",
    "project": {
        "id": 14,
        "url": "http://patchwork.ozlabs.org/api/projects/14/?format=api",
        "name": "QEMU Development",
        "link_name": "qemu-devel",
        "list_id": "qemu-devel.nongnu.org",
        "list_email": "qemu-devel@nongnu.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": "",
        "list_archive_url": "",
        "list_archive_url_format": "",
        "commit_url_format": ""
    },
    "msgid": "<20170907092010.3605-4-aik@ozlabs.ru>",
    "list_archive_url": null,
    "date": "2017-09-07T09:20:09",
    "name": "[RFC,qemu,3/4] memory: Share flat views and dispatch trees between address spaces",
    "commit_ref": null,
    "pull_url": null,
    "state": "new",
    "archived": false,
    "hash": "1d3fb65dc916abdc558785a5acb5ab1bb5833186",
    "submitter": {
        "id": 7621,
        "url": "http://patchwork.ozlabs.org/api/people/7621/?format=api",
        "name": "Alexey Kardashevskiy",
        "email": "aik@ozlabs.ru"
    },
    "delegate": null,
    "mbox": "http://patchwork.ozlabs.org/project/qemu-devel/patch/20170907092010.3605-4-aik@ozlabs.ru/mbox/",
    "series": [
        {
            "id": 1955,
            "url": "http://patchwork.ozlabs.org/api/series/1955/?format=api",
            "web_url": "http://patchwork.ozlabs.org/project/qemu-devel/list/?series=1955",
            "date": "2017-09-07T09:20:10",
            "name": "memory: Reduce memory use",
            "version": 1,
            "mbox": "http://patchwork.ozlabs.org/series/1955/mbox/"
        }
    ],
    "comments": "http://patchwork.ozlabs.org/api/patches/810923/comments/",
    "check": "pending",
    "checks": "http://patchwork.ozlabs.org/api/patches/810923/checks/",
    "tags": {},
    "related": [],
    "headers": {
        "Return-Path": "<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>",
        "X-Original-To": "incoming@patchwork.ozlabs.org",
        "Delivered-To": "patchwork-incoming@bilbo.ozlabs.org",
        "Authentication-Results": "ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)",
        "Received": [
            "from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xnw2q4C6pz9sNV\n\tfor <incoming@patchwork.ozlabs.org>;\n\tThu,  7 Sep 2017 19:22:27 +1000 (AEST)",
            "from localhost ([::1]:39514 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1dpt0y-0001wf-H2\n\tfor incoming@patchwork.ozlabs.org; Thu, 07 Sep 2017 05:22:24 -0400",
            "from eggs.gnu.org ([2001:4830:134:3::10]:57255)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <aik@ozlabs.ru>) id 1dpszb-0001H1-6g\n\tfor qemu-devel@nongnu.org; Thu, 07 Sep 2017 05:21:05 -0400",
            "from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <aik@ozlabs.ru>) id 1dpszY-0007jF-Nu\n\tfor qemu-devel@nongnu.org; Thu, 07 Sep 2017 05:20:59 -0400",
            "from ozlabs.ru ([107.173.13.209]:51600)\n\tby eggs.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <aik@ozlabs.ru>) id 1dpszY-0007j2-95\n\tfor qemu-devel@nongnu.org; Thu, 07 Sep 2017 05:20:56 -0400",
            "from vpl1.ozlabs.ibm.com (localhost [IPv6:::1])\n\tby ozlabs.ru (Postfix) with ESMTP id CE94D3A604D3;\n\tThu,  7 Sep 2017 05:21:35 -0400 (EDT)"
        ],
        "From": "Alexey Kardashevskiy <aik@ozlabs.ru>",
        "To": "qemu-devel@nongnu.org",
        "Date": "Thu,  7 Sep 2017 19:20:09 +1000",
        "Message-Id": "<20170907092010.3605-4-aik@ozlabs.ru>",
        "X-Mailer": "git-send-email 2.11.0",
        "In-Reply-To": "<20170907092010.3605-1-aik@ozlabs.ru>",
        "References": "<20170907092010.3605-1-aik@ozlabs.ru>",
        "X-detected-operating-system": "by eggs.gnu.org: GNU/Linux 3.x [fuzzy]",
        "X-Received-From": "107.173.13.209",
        "Subject": "[Qemu-devel] [RFC PATCH qemu 3/4] memory: Share flat views and\n\tdispatch trees between address spaces",
        "X-BeenThere": "qemu-devel@nongnu.org",
        "X-Mailman-Version": "2.1.21",
        "Precedence": "list",
        "List-Id": "<qemu-devel.nongnu.org>",
        "List-Unsubscribe": "<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>",
        "List-Archive": "<http://lists.nongnu.org/archive/html/qemu-devel/>",
        "List-Post": "<mailto:qemu-devel@nongnu.org>",
        "List-Help": "<mailto:qemu-devel-request@nongnu.org?subject=help>",
        "List-Subscribe": "<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>",
        "Cc": "Alexey Kardashevskiy <aik@ozlabs.ru>, Paolo Bonzini <pbonzini@redhat.com>,\n\tStefan Hajnoczi <stefanha@gmail.com>,\n\tPeter Maydell <peter.maydell@linaro.org>,\n\tDavid Gibson <david@gibson.dropbear.id.au>",
        "Errors-To": "qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org",
        "Sender": "\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"
    },
    "content": "This allows sharing flat views between address spaces when the same root\nmemory region is used when creating a new address space.\n\nThis adds a global list of flat views and a list of attached address\nspaces per a flat view. Each address space references a flat view.\n\nThis hard codes the dispatch tree building instead of doing so via\na memory listener.\n\nSigned-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>\n---\n\nThis was suggested by Paolo in\nhttps://lists.gnu.org/archive/html/qemu-devel/2017-08/msg05011.html\n\nI am not putting \"Suggested-by\" as I want to make sure that this is doing\nwhat was actually suggested.\n---\n include/exec/memory-internal.h |   6 +-\n include/exec/memory.h          |   9 +-\n exec.c                         |  58 ++--------\n hw/pci/pci.c                   |   3 +-\n memory.c                       | 253 +++++++++++++++++++++++++++--------------\n 5 files changed, 187 insertions(+), 142 deletions(-)",
    "diff": "diff --git a/include/exec/memory-internal.h b/include/exec/memory-internal.h\nindex fb467acdba..8516e0b48f 100644\n--- a/include/exec/memory-internal.h\n+++ b/include/exec/memory-internal.h\n@@ -22,9 +22,11 @@\n #ifndef CONFIG_USER_ONLY\n typedef struct AddressSpaceDispatch AddressSpaceDispatch;\n \n-void address_space_init_dispatch(AddressSpace *as);\n void address_space_unregister(AddressSpace *as);\n-void address_space_destroy_dispatch(AddressSpace *as);\n+void address_space_dispatch_free(AddressSpaceDispatch *d);\n+AddressSpaceDispatch *mem_begin(void);\n+void mem_commit(AddressSpaceDispatch *d);\n+void mem_add(AddressSpaceDispatch *d, MemoryRegionSection *section);\n \n extern const MemoryRegionOps unassigned_mem_ops;\n \ndiff --git a/include/exec/memory.h b/include/exec/memory.h\nindex 83e82e90d5..41ab165302 100644\n--- a/include/exec/memory.h\n+++ b/include/exec/memory.h\n@@ -27,6 +27,7 @@\n #include \"qemu/rcu.h\"\n #include \"hw/qdev-core.h\"\n \n+typedef struct AddressSpaceDispatch AddressSpaceDispatch;\n #define RAM_ADDR_INVALID (~(ram_addr_t)0)\n \n #define MAX_PHYS_ADDR_SPACE_BITS 62\n@@ -312,6 +313,7 @@ struct MemoryListener {\n };\n \n AddressSpaceDispatch *address_space_to_dispatch(AddressSpace *as);\n+MemoryRegion *address_space_root(AddressSpace *as);\n \n /**\n  * AddressSpace: describes a mapping of addresses to #MemoryRegion objects\n@@ -320,20 +322,17 @@ struct AddressSpace {\n     /* All fields are private. */\n     struct rcu_head rcu;\n     char *name;\n-    MemoryRegion *root;\n-    int ref_count;\n-    bool malloced;\n \n     /* Accessed via RCU.  */\n     struct FlatView *current_map;\n \n     int ioeventfd_nb;\n     struct MemoryRegionIoeventfd *ioeventfds;\n-    struct AddressSpaceDispatch *dispatch;\n-    struct AddressSpaceDispatch *next_dispatch;\n+\n     MemoryListener dispatch_listener;\n     QTAILQ_HEAD(memory_listeners_as, MemoryListener) listeners;\n     QTAILQ_ENTRY(AddressSpace) address_spaces_link;\n+    QTAILQ_ENTRY(AddressSpace) flat_view_link;\n };\n \n /**\ndiff --git a/exec.c b/exec.c\nindex 66f01f5fce..51243f57f4 100644\n--- a/exec.c\n+++ b/exec.c\n@@ -188,15 +188,12 @@ typedef struct PhysPageMap {\n } PhysPageMap;\n \n struct AddressSpaceDispatch {\n-    struct rcu_head rcu;\n-\n     MemoryRegionSection *mru_section;\n     /* This is a multi-level map on the physical address space.\n      * The bottom level has pointers to MemoryRegionSections.\n      */\n     PhysPageEntry phys_map;\n     PhysPageMap map;\n-    AddressSpace *as;\n };\n \n typedef struct AddressSpaceDispatch AddressSpaceDispatch;\n@@ -240,11 +237,6 @@ struct DirtyBitmapSnapshot {\n     unsigned long dirty[];\n };\n \n-AddressSpaceDispatch *address_space_to_dispatch(AddressSpace *as)\n-{\n-    return atomic_rcu_read(&as->dispatch);\n-}\n-\n #endif\n \n #if !defined(CONFIG_USER_ONLY)\n@@ -1354,10 +1346,8 @@ static void register_multipage(AddressSpaceDispatch *d,\n     phys_page_set(d, start_addr >> TARGET_PAGE_BITS, num_pages, section_index);\n }\n \n-static void mem_add(MemoryListener *listener, MemoryRegionSection *section)\n+void mem_add(AddressSpaceDispatch *d, MemoryRegionSection *section)\n {\n-    AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);\n-    AddressSpaceDispatch *d = as->next_dispatch;\n     MemoryRegionSection now = *section, remain = *section;\n     Int128 page_size = int128_make64(TARGET_PAGE_SIZE);\n \n@@ -2680,9 +2670,8 @@ static void io_mem_init(void)\n                           NULL, UINT64_MAX);\n }\n \n-static void mem_begin(MemoryListener *listener)\n+AddressSpaceDispatch *mem_begin(void)\n {\n-    AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);\n     AddressSpaceDispatch *d = g_new0(AddressSpaceDispatch, 1);\n     uint16_t n;\n \n@@ -2696,27 +2685,19 @@ static void mem_begin(MemoryListener *listener)\n     assert(n == PHYS_SECTION_WATCH);\n \n     d->phys_map  = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .skip = 1 };\n-    as->next_dispatch = d;\n+\n+    return d;\n }\n \n-static void address_space_dispatch_free(AddressSpaceDispatch *d)\n+void address_space_dispatch_free(AddressSpaceDispatch *d)\n {\n     phys_sections_free(&d->map);\n     g_free(d);\n }\n \n-static void mem_commit(MemoryListener *listener)\n+void mem_commit(AddressSpaceDispatch *d)\n {\n-    AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);\n-    AddressSpaceDispatch *cur = as->dispatch;\n-    AddressSpaceDispatch *next = as->next_dispatch;\n-\n-    phys_page_compact_all(next, next->map.nodes_nb);\n-\n-    atomic_rcu_set(&as->dispatch, next);\n-    if (cur) {\n-        call_rcu(cur, address_space_dispatch_free, rcu);\n-    }\n+    phys_page_compact_all(d, d->map.nodes_nb);\n }\n \n static void tcg_commit(MemoryListener *listener)\n@@ -2732,39 +2713,16 @@ static void tcg_commit(MemoryListener *listener)\n      * We reload the dispatch pointer now because cpu_reloading_memory_map()\n      * may have split the RCU critical section.\n      */\n-    d = atomic_rcu_read(&cpuas->as->dispatch);\n+    d = address_space_to_dispatch(cpuas->as);\n     atomic_rcu_set(&cpuas->memory_dispatch, d);\n     tlb_flush(cpuas->cpu);\n }\n \n-void address_space_init_dispatch(AddressSpace *as)\n-{\n-    as->dispatch = NULL;\n-    as->dispatch_listener = (MemoryListener) {\n-        .begin = mem_begin,\n-        .commit = mem_commit,\n-        .region_add = mem_add,\n-        .region_nop = mem_add,\n-        .priority = 0,\n-    };\n-    memory_listener_register(&as->dispatch_listener, as);\n-}\n-\n void address_space_unregister(AddressSpace *as)\n {\n     memory_listener_unregister(&as->dispatch_listener);\n }\n \n-void address_space_destroy_dispatch(AddressSpace *as)\n-{\n-    AddressSpaceDispatch *d = as->dispatch;\n-\n-    atomic_rcu_set(&as->dispatch, NULL);\n-    if (d) {\n-        call_rcu(d, address_space_dispatch_free, rcu);\n-    }\n-}\n-\n static void memory_map_init(void)\n {\n     system_memory = g_malloc(sizeof(*system_memory));\ndiff --git a/hw/pci/pci.c b/hw/pci/pci.c\nindex 258fbe51e2..86b9e419fd 100644\n--- a/hw/pci/pci.c\n+++ b/hw/pci/pci.c\n@@ -88,7 +88,8 @@ static void pci_init_bus_master(PCIDevice *pci_dev)\n \n     memory_region_init_alias(&pci_dev->bus_master_enable_region,\n                              OBJECT(pci_dev), \"bus master\",\n-                             dma_as->root, 0, memory_region_size(dma_as->root));\n+                             address_space_root(dma_as), 0,\n+                             memory_region_size(address_space_root(dma_as)));\n     memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);\n     memory_region_add_subregion(&pci_dev->bus_master_container_region, 0,\n                                 &pci_dev->bus_master_enable_region);\ndiff --git a/memory.c b/memory.c\nindex c6904a7deb..385a507511 100644\n--- a/memory.c\n+++ b/memory.c\n@@ -47,6 +47,9 @@ static QTAILQ_HEAD(memory_listeners, MemoryListener) memory_listeners\n static QTAILQ_HEAD(, AddressSpace) address_spaces\n     = QTAILQ_HEAD_INITIALIZER(address_spaces);\n \n+static QTAILQ_HEAD(FlatViewList, FlatView) flat_views\n+    = QTAILQ_HEAD_INITIALIZER(flat_views);\n+\n typedef struct AddrRange AddrRange;\n \n /*\n@@ -230,6 +233,11 @@ struct FlatView {\n     FlatRange *ranges;\n     unsigned nr;\n     unsigned nr_allocated;\n+    MemoryRegion *root;\n+    struct AddressSpaceDispatch *dispatch;\n+\n+    QTAILQ_ENTRY(FlatView) flat_views_link;\n+    QTAILQ_HEAD(address_spaces, AddressSpace) address_spaces;\n };\n \n typedef struct AddressSpaceOps AddressSpaceOps;\n@@ -259,12 +267,19 @@ static bool flatrange_equal(FlatRange *a, FlatRange *b)\n         && a->readonly == b->readonly;\n }\n \n-static void flatview_init(FlatView *view)\n+static void flatview_ref(FlatView *view);\n+\n+static FlatView *flatview_alloc(MemoryRegion *mr_root)\n {\n+    FlatView *view;\n+\n+    view = g_new0(FlatView, 1);\n     view->ref = 1;\n-    view->ranges = NULL;\n-    view->nr = 0;\n-    view->nr_allocated = 0;\n+    view->root = mr_root;\n+    memory_region_ref(mr_root);\n+    QTAILQ_INIT(&view->address_spaces);\n+\n+    return view;\n }\n \n /* Insert a range into a given position.  Caller is responsible for maintaining\n@@ -292,6 +307,10 @@ static void flatview_destroy(FlatView *view)\n         memory_region_unref(view->ranges[i].mr);\n     }\n     g_free(view->ranges);\n+    if (view->dispatch) {\n+        address_space_dispatch_free(view->dispatch);\n+    }\n+    memory_region_unref(view->root);\n     g_free(view);\n }\n \n@@ -303,7 +322,7 @@ static void flatview_ref(FlatView *view)\n static void flatview_unref(FlatView *view)\n {\n     if (atomic_fetch_dec(&view->ref) == 1) {\n-        flatview_destroy(view);\n+        call_rcu(view, flatview_destroy, rcu);\n     }\n }\n \n@@ -608,7 +627,7 @@ static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)\n         mr = mr->container;\n     }\n     QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {\n-        if (mr == as->root) {\n+        if (mr == as->current_map->root) {\n             return as;\n         }\n     }\n@@ -702,23 +721,6 @@ static void render_memory_region(FlatView *view,\n     }\n }\n \n-/* Render a memory topology into a list of disjoint absolute ranges. */\n-static FlatView *generate_memory_topology(MemoryRegion *mr)\n-{\n-    FlatView *view;\n-\n-    view = g_new(FlatView, 1);\n-    flatview_init(view);\n-\n-    if (mr) {\n-        render_memory_region(view, mr, int128_zero(),\n-                             addrrange_make(int128_zero(), int128_2_64()), false);\n-    }\n-    flatview_simplify(view);\n-\n-    return view;\n-}\n-\n static void address_space_add_del_ioeventfds(AddressSpace *as,\n                                              MemoryRegionIoeventfd *fds_new,\n                                              unsigned fds_new_nb,\n@@ -769,12 +771,10 @@ static void address_space_add_del_ioeventfds(AddressSpace *as,\n     }\n }\n \n-static FlatView *address_space_get_flatview(AddressSpace *as)\n+static FlatView *flatview_get(FlatView *view)\n {\n-    FlatView *view;\n-\n     rcu_read_lock();\n-    view = atomic_rcu_read(&as->current_map);\n+    view = atomic_rcu_read(&view);\n     flatview_ref(view);\n     rcu_read_unlock();\n     return view;\n@@ -789,7 +789,7 @@ static void address_space_update_ioeventfds(AddressSpace *as)\n     AddrRange tmp;\n     unsigned i;\n \n-    view = address_space_get_flatview(as);\n+    view = flatview_get(as->current_map);\n     FOR_EACH_FLAT_RANGE(fr, view) {\n         for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {\n             tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,\n@@ -881,28 +881,89 @@ static void address_space_update_topology_pass(AddressSpace *as,\n     }\n }\n \n-\n-static void address_space_update_topology(AddressSpace *as)\n+static FlatView *address_space_update_flatview(FlatView *view)\n {\n-    FlatView *old_view = address_space_get_flatview(as);\n-    FlatView *new_view = generate_memory_topology(as->root);\n+    AddressSpace *as, *asnext;\n+    FlatView *old_view = flatview_get(view);\n+    MemoryRegion *root = old_view->root;\n+    FlatView *new_view = flatview_alloc(root);\n+    unsigned iold, inew;\n+    FlatRange *frold, *frnew;\n \n-    address_space_update_topology_pass(as, old_view, new_view, false);\n-    address_space_update_topology_pass(as, old_view, new_view, true);\n+    if (root) {\n+        render_memory_region(new_view, root, int128_zero(),\n+                             addrrange_make(int128_zero(), int128_2_64()),\n+                             false);\n+        flatview_simplify(new_view);\n+    }\n \n-    /* Writes are protected by the BQL.  */\n-    atomic_rcu_set(&as->current_map, new_view);\n-    call_rcu(old_view, flatview_unref, rcu);\n+    new_view->dispatch = mem_begin();\n \n-    /* Note that all the old MemoryRegions are still alive up to this\n-     * point.  This relieves most MemoryListeners from the need to\n-     * ref/unref the MemoryRegions they get---unless they use them\n-     * outside the iothread mutex, in which case precise reference\n-     * counting is necessary.\n+    /*\n+     * FIXME: this is cut-n-paste from address_space_update_topology_pass,\n+     * simplify it\n      */\n+    iold = inew = 0;\n+    while (iold < old_view->nr || inew < new_view->nr) {\n+        if (iold < old_view->nr) {\n+            frold = &old_view->ranges[iold];\n+        } else {\n+            frold = NULL;\n+        }\n+        if (inew < new_view->nr) {\n+            frnew = &new_view->ranges[inew];\n+        } else {\n+            frnew = NULL;\n+        }\n+\n+        if (frold\n+            && (!frnew\n+                || int128_lt(frold->addr.start, frnew->addr.start)\n+                || (int128_eq(frold->addr.start, frnew->addr.start)\n+                    && !flatrange_equal(frold, frnew)))) {\n+            ++iold;\n+        } else if (frold && frnew && flatrange_equal(frold, frnew)) {\n+            /* In both and unchanged (except logging may have changed) */\n+            MemoryRegionSection mrs = section_from_flat_range(frnew,\n+                    new_view->dispatch);\n+\n+            mem_add(new_view->dispatch, &mrs);\n+\n+            ++iold;\n+            ++inew;\n+        } else {\n+            /* In new */\n+            MemoryRegionSection mrs = section_from_flat_range(frnew,\n+                    new_view->dispatch);\n+\n+            mem_add(new_view->dispatch, &mrs);\n+\n+            ++inew;\n+        }\n+    }\n+\n+    mem_commit(new_view->dispatch);\n+\n+    QTAILQ_FOREACH(as, &old_view->address_spaces, flat_view_link) {\n+        address_space_update_topology_pass(as, old_view, new_view, false);\n+        address_space_update_topology_pass(as, old_view, new_view, true);\n+    }\n+\n+    QTAILQ_FOREACH_SAFE(as, &old_view->address_spaces, flat_view_link, asnext) {\n+        QTAILQ_REMOVE(&old_view->address_spaces, as, flat_view_link);\n+        flatview_unref(old_view);\n+\n+        atomic_rcu_set(&as->current_map, new_view);\n+\n+        flatview_ref(new_view);\n+        QTAILQ_INSERT_TAIL(&new_view->address_spaces, as, flat_view_link);\n+\n+        address_space_update_ioeventfds(as);\n+    }\n+\n     flatview_unref(old_view);\n \n-    address_space_update_ioeventfds(as);\n+    return new_view;\n }\n \n void memory_region_transaction_begin(void)\n@@ -921,11 +982,31 @@ void memory_region_transaction_commit(void)\n     --memory_region_transaction_depth;\n     if (!memory_region_transaction_depth) {\n         if (memory_region_update_pending) {\n+            FlatView *view, *vnext;\n+            struct FlatViewList fwstmp = QTAILQ_HEAD_INITIALIZER(fwstmp);\n+\n             MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);\n \n-            QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {\n-                address_space_update_topology(as);\n+            QTAILQ_FOREACH_SAFE(view, &flat_views, flat_views_link, vnext) {\n+                FlatView *old_view, *new_view;\n+\n+                old_view = flatview_get(view);\n+                new_view = address_space_update_flatview(old_view);\n+\n+                QTAILQ_REMOVE(&flat_views, old_view, flat_views_link);\n+                flatview_unref(old_view);\n+                flatview_unref(old_view);\n+\n+                QTAILQ_INSERT_HEAD(&fwstmp, new_view, flat_views_link);\n+\n+                flatview_unref(new_view);\n             }\n+\n+            QTAILQ_FOREACH_SAFE(view, &fwstmp, flat_views_link, vnext) {\n+                QTAILQ_REMOVE(&fwstmp, view, flat_views_link);\n+                QTAILQ_INSERT_HEAD(&flat_views, view, flat_views_link);\n+            }\n+\n             memory_region_update_pending = false;\n             MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);\n         } else if (ioeventfd_update_pending) {\n@@ -1834,7 +1915,7 @@ void memory_region_sync_dirty_bitmap(MemoryRegion *mr)\n             continue;\n         }\n         as = listener->address_space;\n-        view = address_space_get_flatview(as);\n+        view = flatview_get(as->current_map);\n         FOR_EACH_FLAT_RANGE(fr, view) {\n             if (fr->mr == mr) {\n                 MemoryRegionSection mrs = section_from_flat_range(fr,\n@@ -1938,7 +2019,7 @@ static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpa\n     AddrRange tmp;\n     MemoryRegionSection section;\n \n-    view = address_space_get_flatview(as);\n+    view = flatview_get(as->current_map);\n     FOR_EACH_FLAT_RANGE(fr, view) {\n         if (fr->mr == mr) {\n             section = (MemoryRegionSection) {\n@@ -2350,7 +2431,7 @@ void memory_global_dirty_log_sync(void)\n             continue;\n         }\n         as = listener->address_space;\n-        view = address_space_get_flatview(as);\n+        view = flatview_get(as->current_map);\n         FOR_EACH_FLAT_RANGE(fr, view) {\n             if (fr->dirty_log_mask) {\n                 MemoryRegionSection mrs = section_from_flat_range(fr,\n@@ -2436,7 +2517,7 @@ static void listener_add_address_space(MemoryListener *listener,\n         }\n     }\n \n-    view = address_space_get_flatview(as);\n+    view = flatview_get(as->current_map);\n     FOR_EACH_FLAT_RANGE(fr, view) {\n         MemoryRegionSection section = {\n             .mr = fr->mr,\n@@ -2615,67 +2696,72 @@ void memory_region_invalidate_mmio_ptr(MemoryRegion *mr, hwaddr offset,\n \n void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)\n {\n-    memory_region_ref(root);\n-    memory_region_transaction_begin();\n-    as->ref_count = 1;\n-    as->root = root;\n-    as->malloced = false;\n-    as->current_map = g_new(FlatView, 1);\n-    flatview_init(as->current_map);\n+    FlatView *view;\n+\n+    as->current_map = NULL;\n     as->ioeventfd_nb = 0;\n     as->ioeventfds = NULL;\n     QTAILQ_INIT(&as->listeners);\n     QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);\n     as->name = g_strdup(name ? name : \"anonymous\");\n-    address_space_init_dispatch(as);\n-    memory_region_update_pending |= root->enabled;\n-    memory_region_transaction_commit();\n+\n+    QTAILQ_FOREACH(view, &flat_views, flat_views_link) {\n+        assert(root);\n+        if (view->root == root) {\n+            as->current_map = flatview_get(view);\n+            break;\n+        }\n+    }\n+\n+    if (!as->current_map) {\n+        as->current_map = flatview_alloc(root);\n+\n+        QTAILQ_INSERT_TAIL(&flat_views, as->current_map, flat_views_link);\n+    }\n+\n+    QTAILQ_INSERT_TAIL(&as->current_map->address_spaces, as, flat_view_link);\n+}\n+\n+MemoryRegion *address_space_root(AddressSpace *as)\n+{\n+    return as->current_map->root;\n+}\n+\n+AddressSpaceDispatch *address_space_to_dispatch(AddressSpace *as)\n+{\n+    return atomic_rcu_read(&as->current_map)->dispatch;\n }\n \n static void do_address_space_destroy(AddressSpace *as)\n {\n-    bool do_free = as->malloced;\n+    FlatView *view = flatview_get(as->current_map);\n \n-    address_space_destroy_dispatch(as);\n     assert(QTAILQ_EMPTY(&as->listeners));\n \n-    flatview_unref(as->current_map);\n+    QTAILQ_REMOVE(&view->address_spaces, as, flat_view_link);\n+    flatview_unref(view);\n+\n+    flatview_unref(view);\n+\n     g_free(as->name);\n     g_free(as->ioeventfds);\n-    memory_region_unref(as->root);\n-    if (do_free) {\n-        g_free(as);\n-    }\n+    g_free(as);\n }\n \n AddressSpace *address_space_init_shareable(MemoryRegion *root, const char *name)\n {\n     AddressSpace *as;\n \n-    QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {\n-        if (root == as->root && as->malloced) {\n-            as->ref_count++;\n-            return as;\n-        }\n-    }\n-\n     as = g_malloc0(sizeof *as);\n     address_space_init(as, root, name);\n-    as->malloced = true;\n+\n     return as;\n }\n \n void address_space_destroy(AddressSpace *as)\n {\n-    MemoryRegion *root = as->root;\n-\n-    as->ref_count--;\n-    if (as->ref_count) {\n-        return;\n-    }\n     /* Flush out anything from MemoryListeners listening in on this */\n     memory_region_transaction_begin();\n-    as->root = NULL;\n     memory_region_transaction_commit();\n     QTAILQ_REMOVE(&address_spaces, as, address_spaces_link);\n     address_space_unregister(as);\n@@ -2684,7 +2770,6 @@ void address_space_destroy(AddressSpace *as)\n      * entries that the guest should never use.  Wait for the old\n      * values to expire before freeing the data.\n      */\n-    as->root = root;\n     call_rcu(as, do_address_space_destroy, rcu);\n }\n \n@@ -2816,7 +2901,7 @@ static void mtree_print_mr(fprintf_function mon_printf, void *f,\n static void mtree_print_flatview(fprintf_function p, void *f,\n                                  AddressSpace *as)\n {\n-    FlatView *view = address_space_get_flatview(as);\n+    FlatView *view = flatview_get(as->current_map);\n     FlatRange *range = &view->ranges[0];\n     MemoryRegion *mr;\n     int n = view->nr;\n@@ -2873,7 +2958,7 @@ void mtree_info(fprintf_function mon_printf, void *f, bool flatview)\n \n     QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {\n         mon_printf(f, \"address-space: %s\\n\", as->name);\n-        mtree_print_mr(mon_printf, f, as->root, 1, 0, &ml_head);\n+        mtree_print_mr(mon_printf, f, as->current_map->root, 1, 0, &ml_head);\n         mon_printf(f, \"\\n\");\n     }\n \n",
    "prefixes": [
        "RFC",
        "qemu",
        "3/4"
    ]
}