{"id":814883,"url":"http://patchwork.ozlabs.org/api/patches/814883/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20170918101709.30421-6-aik@ozlabs.ru/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/projects/14/?format=json","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":"<20170918101709.30421-6-aik@ozlabs.ru>","list_archive_url":null,"date":"2017-09-18T10:17:01","name":"[qemu,v3,05/13] memory: Move AddressSpaceDispatch from AddressSpace to FlatView","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"1d6870b1b02834a98132bb54f9f611f3b58f28bb","submitter":{"id":7621,"url":"http://patchwork.ozlabs.org/api/people/7621/?format=json","name":"Alexey Kardashevskiy","email":"aik@ozlabs.ru"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20170918101709.30421-6-aik@ozlabs.ru/mbox/","series":[{"id":3617,"url":"http://patchwork.ozlabs.org/api/series/3617/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=3617","date":"2017-09-18T10:16:57","name":"memory: Reduce memory use","version":3,"mbox":"http://patchwork.ozlabs.org/series/3617/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/814883/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/814883/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 3xwj2p6DSLz9s78\n\tfor <incoming@patchwork.ozlabs.org>;\n\tMon, 18 Sep 2017 20:30:58 +1000 (AEST)","from localhost ([::1]:35659 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 1dttKL-0007yn-1q\n\tfor incoming@patchwork.ozlabs.org; Mon, 18 Sep 2017 06:30:57 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:42205)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <aik@ozlabs.ru>) id 1dtt7j-00066q-Ki\n\tfor qemu-devel@nongnu.org; Mon, 18 Sep 2017 06:17:57 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <aik@ozlabs.ru>) id 1dtt7i-0004NU-6W\n\tfor qemu-devel@nongnu.org; Mon, 18 Sep 2017 06:17:55 -0400","from ozlabs.ru ([107.173.13.209]:35346)\n\tby eggs.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <aik@ozlabs.ru>) id 1dtt7h-0004NB-Q3\n\tfor qemu-devel@nongnu.org; Mon, 18 Sep 2017 06:17:54 -0400","from vpl1.ozlabs.ibm.com (localhost [IPv6:::1])\n\tby ozlabs.ru (Postfix) with ESMTP id 30F333A60051;\n\tMon, 18 Sep 2017 06:18:38 -0400 (EDT)"],"From":"Alexey Kardashevskiy <aik@ozlabs.ru>","To":"qemu-devel@nongnu.org","Date":"Mon, 18 Sep 2017 20:17:01 +1000","Message-Id":"<20170918101709.30421-6-aik@ozlabs.ru>","X-Mailer":"git-send-email 2.11.0","In-Reply-To":"<20170918101709.30421-1-aik@ozlabs.ru>","References":"<20170918101709.30421-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] [PATCH qemu v3 05/13] memory: Move\n\tAddressSpaceDispatch from AddressSpace to FlatView","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>","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":"As we are going to share FlatView's between AddressSpace's,\nand AddressSpaceDispatch is a structure to perform quick lookup\nin FlatView, this moves ASD to FlatView.\n\nAfter previosly open coded ASD rendering, we can also remove\nas->next_dispatch as the new FlatView pointer is stored\non a stack and set to an AS atomically.\n\nflatview_destroy() is executed under RCU instead of\naddress_space_dispatch_free() now.\n\nThis makes mem_begin/mem_commit to work with ASD and mem_add with FV\nas later on mem_add will be taking FV as an argument anyway.\n\nSigned-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>\n---\n include/exec/memory-internal.h | 12 +++++++-----\n include/exec/memory.h          |  2 --\n exec.c                         | 41 +++++++++++------------------------------\n memory.c                       | 29 +++++++++++++++++++++++------\n 4 files changed, 41 insertions(+), 43 deletions(-)","diff":"diff --git a/include/exec/memory-internal.h b/include/exec/memory-internal.h\nindex 9abde2f11c..6e08eda256 100644\n--- a/include/exec/memory-internal.h\n+++ b/include/exec/memory-internal.h\n@@ -22,16 +22,18 @@\n #ifndef CONFIG_USER_ONLY\n typedef struct AddressSpaceDispatch AddressSpaceDispatch;\n \n-void address_space_destroy_dispatch(AddressSpace *as);\n-\n extern const MemoryRegionOps unassigned_mem_ops;\n \n bool memory_region_access_valid(MemoryRegion *mr, hwaddr addr,\n                                 unsigned size, bool is_write);\n \n-void mem_add(AddressSpace *as, MemoryRegionSection *section);\n-void mem_begin(AddressSpace *as);\n-void mem_commit(AddressSpace *as);\n+void mem_add(AddressSpace *as, FlatView *fv, MemoryRegionSection *section);\n+AddressSpaceDispatch *mem_begin(AddressSpace *as);\n+void mem_commit(AddressSpaceDispatch *d);\n+\n+AddressSpaceDispatch *address_space_to_dispatch(AddressSpace *as);\n+AddressSpaceDispatch *flatview_to_dispatch(FlatView *fv);\n+void address_space_dispatch_free(AddressSpaceDispatch *d);\n \n #endif\n #endif\ndiff --git a/include/exec/memory.h b/include/exec/memory.h\nindex 9581f7a7db..2346f8b863 100644\n--- a/include/exec/memory.h\n+++ b/include/exec/memory.h\n@@ -316,8 +316,6 @@ struct AddressSpace {\n \n     int ioeventfd_nb;\n     struct MemoryRegionIoeventfd *ioeventfds;\n-    struct AddressSpaceDispatch *dispatch;\n-    struct AddressSpaceDispatch *next_dispatch;\n     QTAILQ_HEAD(memory_listeners_as, MemoryListener) listeners;\n     QTAILQ_ENTRY(AddressSpace) address_spaces_link;\n };\ndiff --git a/exec.c b/exec.c\nindex 3ed3718dea..6b0211bafc 100644\n--- a/exec.c\n+++ b/exec.c\n@@ -188,8 +188,6 @@ 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@@ -486,7 +484,7 @@ static MemoryRegionSection address_space_do_translate(AddressSpace *as,\n     IOMMUMemoryRegionClass *imrc;\n \n     for (;;) {\n-        AddressSpaceDispatch *d = atomic_rcu_read(&as->dispatch);\n+        AddressSpaceDispatch *d = address_space_to_dispatch(as);\n         section = address_space_translate_internal(d, addr, &addr, plen, is_mmio);\n \n         iommu_mr = memory_region_get_iommu(section->mr);\n@@ -1223,7 +1221,7 @@ hwaddr memory_region_section_get_iotlb(CPUState *cpu,\n     } else {\n         AddressSpaceDispatch *d;\n \n-        d = atomic_rcu_read(&section->address_space->dispatch);\n+        d = address_space_to_dispatch(section->address_space);\n         iotlb = section - d->map.sections;\n         iotlb += xlat;\n     }\n@@ -1348,9 +1346,9 @@ static void register_multipage(AddressSpaceDispatch *d,\n     phys_page_set(d, start_addr >> TARGET_PAGE_BITS, num_pages, section_index);\n }\n \n-void mem_add(AddressSpace *as, MemoryRegionSection *section)\n+void mem_add(AddressSpace *as, FlatView *fv, MemoryRegionSection *section)\n {\n-    AddressSpaceDispatch *d = as->next_dispatch;\n+    AddressSpaceDispatch *d = flatview_to_dispatch(fv);\n     MemoryRegionSection now = *section, remain = *section;\n     Int128 page_size = int128_make64(TARGET_PAGE_SIZE);\n \n@@ -2673,7 +2671,7 @@ static void io_mem_init(void)\n                           NULL, UINT64_MAX);\n }\n \n-void mem_begin(AddressSpace *as)\n+AddressSpaceDispatch *mem_begin(AddressSpace *as)\n {\n     AddressSpaceDispatch *d = g_new0(AddressSpaceDispatch, 1);\n     uint16_t n;\n@@ -2689,26 +2687,19 @@ void mem_begin(AddressSpace *as)\n \n     d->phys_map  = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .skip = 1 };\n     d->as = as;\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-void mem_commit(AddressSpace *as)\n+void mem_commit(AddressSpaceDispatch *d)\n {\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@@ -2724,21 +2715,11 @@ 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_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/memory.c b/memory.c\nindex d3bf0da703..7d28aec5e3 100644\n--- a/memory.c\n+++ b/memory.c\n@@ -229,6 +229,7 @@ struct FlatView {\n     FlatRange *ranges;\n     unsigned nr;\n     unsigned nr_allocated;\n+    struct AddressSpaceDispatch *dispatch;\n };\n \n typedef struct AddressSpaceOps AddressSpaceOps;\n@@ -289,6 +290,9 @@ static void flatview_destroy(FlatView *view)\n {\n     int i;\n \n+    if (view->dispatch) {\n+        address_space_dispatch_free(view->dispatch);\n+    }\n     for (i = 0; i < view->nr; i++) {\n         memory_region_unref(view->ranges[i].mr);\n     }\n@@ -304,10 +308,25 @@ 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+static FlatView *address_space_to_flatview(AddressSpace *as)\n+{\n+    return atomic_rcu_read(&as->current_map);\n+}\n+\n+AddressSpaceDispatch *flatview_to_dispatch(FlatView *fv)\n+{\n+    return fv->dispatch;\n+}\n+\n+AddressSpaceDispatch *address_space_to_dispatch(AddressSpace *as)\n+{\n+    return flatview_to_dispatch(address_space_to_flatview(as));\n+}\n+\n static bool can_merge(FlatRange *r1, FlatRange *r2)\n {\n     return int128_eq(addrrange_end(r1->addr), r2->addr.start)\n@@ -886,13 +905,13 @@ static void address_space_update_topology(AddressSpace *as)\n     FlatView *new_view = generate_memory_topology(as->root);\n     int i;\n \n-    mem_begin(as);\n+    new_view->dispatch = mem_begin(as);\n     for (i = 0; i < new_view->nr; i++) {\n         MemoryRegionSection mrs =\n             section_from_flat_range(&new_view->ranges[i], as);\n-        mem_add(as, &mrs);\n+        mem_add(as, new_view, &mrs);\n     }\n-    mem_commit(as);\n+    mem_commit(new_view->dispatch);\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@@ -2629,7 +2648,6 @@ void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)\n     QTAILQ_INIT(&as->listeners);\n     QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);\n     as->name = g_strdup(name ? name : \"anonymous\");\n-    as->dispatch = NULL;\n     memory_region_update_pending |= root->enabled;\n     memory_region_transaction_commit();\n }\n@@ -2638,7 +2656,6 @@ static void do_address_space_destroy(AddressSpace *as)\n {\n     bool do_free = as->malloced;\n \n-    address_space_destroy_dispatch(as);\n     assert(QTAILQ_EMPTY(&as->listeners));\n \n     flatview_unref(as->current_map);\n","prefixes":["qemu","v3","05/13"]}