{"id":814873,"url":"http://patchwork.ozlabs.org/api/patches/814873/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20170918101709.30421-12-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-12-aik@ozlabs.ru>","list_archive_url":null,"date":"2017-09-18T10:17:07","name":"[qemu,v3,11/13] memory: Share FlatView's and dispatch trees between address spaces","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"665f376cc834f11b5f4f13091439750f444df513","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-12-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/814873/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/814873/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 3xwhtG4ZYCz9s78\n\tfor <incoming@patchwork.ozlabs.org>;\n\tMon, 18 Sep 2017 20:23:34 +1000 (AEST)","from localhost ([::1]:35610 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 1dttDA-00028K-MC\n\tfor incoming@patchwork.ozlabs.org; Mon, 18 Sep 2017 06:23:32 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:42055)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <aik@ozlabs.ru>) id 1dtt7L-0005ly-SK\n\tfor qemu-devel@nongnu.org; Mon, 18 Sep 2017 06:17:32 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <aik@ozlabs.ru>) id 1dtt7K-0004HC-KL\n\tfor qemu-devel@nongnu.org; Mon, 18 Sep 2017 06:17:31 -0400","from ozlabs.ru ([107.173.13.209]:35254)\n\tby eggs.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <aik@ozlabs.ru>) id 1dtt7K-0004Dh-CV\n\tfor qemu-devel@nongnu.org; Mon, 18 Sep 2017 06:17:30 -0400","from vpl1.ozlabs.ibm.com (localhost [IPv6:::1])\n\tby ozlabs.ru (Postfix) with ESMTP id 2A3C93A60055;\n\tMon, 18 Sep 2017 06:18:44 -0400 (EDT)"],"From":"Alexey Kardashevskiy <aik@ozlabs.ru>","To":"qemu-devel@nongnu.org","Date":"Mon, 18 Sep 2017 20:17:07 +1000","Message-Id":"<20170918101709.30421-12-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 11/13] memory: Share FlatView's 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>","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\nSigned-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>\n---\nChanges:\nv3:\n* got rid of global and per-AS lists of FlatView objects\n---\n\n\nI could also make all empty FlatViews (view->nr==0 but they are still\nenabled) resolve to an empty FlatView but not sure it is worth it as\nin real life all devices and therefore FlatViews are expected not to be\nempty.\n\n\n---\n memory.c | 92 ++++++++++++++++++++++++++++++++++++++++------------------------\n 1 file changed, 58 insertions(+), 34 deletions(-)","diff":"diff --git a/memory.c b/memory.c\nindex e18ef53cc2..6fca8bf932 100644\n--- a/memory.c\n+++ b/memory.c\n@@ -917,36 +917,66 @@ static void address_space_update_topology_pass(AddressSpace *as,\n     }\n }\n \n-static void address_space_update_topology(AddressSpace *as)\n+static gboolean flatview_unref_g_hash(gpointer key, gpointer value,\n+                                      gpointer user_data)\n+{\n+    flatview_unref((FlatView *) value);\n+    return true;\n+}\n+\n+static void flatview_update_topology(void)\n {\n-    FlatView *old_view = address_space_get_flatview(as);\n-    MemoryRegion *physmr = memory_region_unalias_entire(old_view->root);\n-    FlatView *new_view = generate_memory_topology(physmr);\n     int i;\n+    AddressSpace *as;\n+    FlatView *old_view, *new_view;\n+    GHashTable *views = g_hash_table_new(g_direct_hash, g_direct_equal);\n+    MemoryRegion *physmr;\n+    gpointer key;\n \n-    new_view->dispatch = address_space_dispatch_new(new_view);\n-    for (i = 0; i < new_view->nr; i++) {\n-        MemoryRegionSection mrs =\n-            section_from_flat_range(&new_view->ranges[i], new_view);\n-        flatview_add_to_dispatch(new_view, &mrs);\n+    /* Render unique FVs */\n+    QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {\n+        old_view = as->current_map;\n+        physmr = memory_region_unalias_entire(old_view->root);\n+\n+        key = !physmr->enabled ? 0 : physmr;\n+        new_view = (FlatView *) g_hash_table_lookup(views, key);\n+        if (new_view) {\n+            continue;\n+        }\n+\n+        new_view = generate_memory_topology(physmr);\n+\n+        new_view->dispatch = address_space_dispatch_new(new_view);\n+        for (i = 0; i < new_view->nr; i++) {\n+            MemoryRegionSection mrs =\n+                section_from_flat_range(&new_view->ranges[i], new_view);\n+            flatview_add_to_dispatch(new_view, &mrs);\n+        }\n+        address_space_dispatch_compact(new_view->dispatch);\n+\n+        g_hash_table_insert(views, key, new_view);\n+    }\n+\n+    /* Replace FVs in ASes */\n+    QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {\n+        old_view = as->current_map;\n+        physmr = memory_region_unalias_entire(old_view->root);\n+\n+        key = !physmr->enabled ? 0 : physmr;\n+        new_view = (FlatView *) g_hash_table_lookup(views, key);\n+        assert(new_view);\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+\n+        flatview_ref(new_view);\n+        atomic_rcu_set(&as->current_map, new_view);\n+        flatview_unref(old_view);\n     }\n-    address_space_dispatch_compact(new_view->dispatch);\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-    /* Writes are protected by the BQL.  */\n-    atomic_rcu_set(&as->current_map, new_view);\n-    call_rcu(old_view, flatview_unref, rcu);\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-    flatview_unref(old_view);\n-\n-    address_space_update_ioeventfds(as);\n+\n+    /* Unref FVs from temporary table */\n+    g_hash_table_foreach_remove(views, flatview_unref_g_hash, 0);\n+    g_hash_table_unref(views);\n }\n \n void memory_region_transaction_begin(void)\n@@ -966,9 +996,10 @@ void memory_region_transaction_commit(void)\n     if (!memory_region_transaction_depth) {\n         if (memory_region_update_pending) {\n             MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);\n+            flatview_update_topology();\n \n             QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {\n-                address_space_update_topology(as);\n+                address_space_update_ioeventfds(as);\n             }\n             memory_region_update_pending = false;\n             MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);\n@@ -2690,13 +2721,6 @@ 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","prefixes":["qemu","v3","11/13"]}