From patchwork Thu Sep 21 23:16:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 817238 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="MzxFGGs/"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3xytDG3Y0qz9tXm for ; Fri, 22 Sep 2017 09:31:39 +1000 (AEST) Received: from localhost ([::1]:55926 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dvAwT-0000QN-T0 for incoming@patchwork.ozlabs.org; Thu, 21 Sep 2017 19:31:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37402) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dvAiS-0004pc-N2 for qemu-devel@nongnu.org; Thu, 21 Sep 2017 19:17:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dvAiP-0004K0-Il for qemu-devel@nongnu.org; Thu, 21 Sep 2017 19:17:08 -0400 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]:37204) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dvAiP-0004J0-Bx for qemu-devel@nongnu.org; Thu, 21 Sep 2017 19:17:05 -0400 Received: by mail-wm0-x241.google.com with SMTP id f4so6421420wmh.4 for ; Thu, 21 Sep 2017 16:17:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=pfQ8WqrAT8mpiuFJUlYz2y/IIzIPiihRG97X6UjQMg8=; b=MzxFGGs/5pMUxkIf6Yum9aSPfWlmqainUoQioFfyOj9Tr+FeeWlYyDwBkDJN7NVsu5 dRsC3g9YcpQA2I95uEZMz0OPrqHTZ+fb6bbkZXhprzdnEcw9JmKW+BrfHvHSPVNidpZ7 cBMjr/ygWDXkXQBI5gVNbt5dld72JA4ZwWwirQJcddopirET4a/n9CY+CvKLAXvt6xta neYs2FY6JAcpTGTWwtknemBfDyuPZScZJ47FgKZlYrYByRXmIbrANtLERwDXFWmwace7 wsnVS6k7es2rYE4iNWa9IAIMxN8XoFdCsQ+Duyk/nyBhn/BphTl0w6GoCSXe1ueHG2EM tSzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=pfQ8WqrAT8mpiuFJUlYz2y/IIzIPiihRG97X6UjQMg8=; b=YYHA6L2MPQ1aGsu5uTE6D185GquDZ6AscHBLcSlTn7uUjsWkItu8QpVypNuUD5XiE+ gz+BnBLMPRj79GiO/V6LoBh5JbvKbQraWrYi0HG/tl7ujS1IndIHbDsuOuXFz/1qhmCh RF1cdbFB83pYDTVwW81+di4q92ykY1vqI4NLWLbp7YoS1FAQECgVVT72WZlbuX0jMl8V a0lF60blV2qTfYgV2V6nHdNvNyXCF90+Gh3UzgedUTMLcpErqOU4y5sgS7WXu7MjKWsF IwuAUmSYqfVT7hBkZfn4ImLlS7hujFwVRYyDs9Cz07AF1TY0ju+0O0YylD/Ump8D+zpi hsyg== X-Gm-Message-State: AHPjjUh4ERjL4DXEnMzzgE7XatwLXkbLl0dlhWV8q7SUmUkwk6c6Gzpo i+XhzjWbw6lx8uEgFT8wTe7/R0Tj X-Google-Smtp-Source: AOwi7QAHO01L7HvG3i9goxYMBGRjUSacZa7tvTcHl56uUERKQH/PYGPgjBv7orlic177aRJaVXQplg== X-Received: by 10.28.21.71 with SMTP id 68mr2316326wmv.139.1506035824103; Thu, 21 Sep 2017 16:17:04 -0700 (PDT) Received: from 640k.lan (dynamic-adsl-78-12-246-117.clienti.tiscali.it. [78.12.246.117]) by smtp.gmail.com with ESMTPSA id u186sm2596278wmd.19.2017.09.21.16.17.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 21 Sep 2017 16:17:03 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 22 Sep 2017 01:16:25 +0200 Message-Id: <1506035800-30509-18-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1506035800-30509-1-git-send-email-pbonzini@redhat.com> References: <1506035800-30509-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::241 Subject: [Qemu-devel] [PULL 17/32] memory: Share FlatView's and dispatch trees between address spaces X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alexey Kardashevskiy Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Alexey Kardashevskiy This allows sharing flat views between address spaces (AS) when the same root memory region is used when creating a new address space. This is done by walking through all ASes and caching one FlatView per a physical root MR (i.e. not aliased). This removes search for duplicates from address_space_init_shareable() as FlatViews are shared elsewhere and keeping as::ref_count correct seems an unnecessary and useless complication. This should cause no change and memory use or boot time yet. Signed-off-by: Alexey Kardashevskiy Message-Id: <20170921085110.25598-13-aik@ozlabs.ru> Signed-off-by: Paolo Bonzini --- memory.c | 56 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/memory.c b/memory.c index bb582d0..8ee3c81 100644 --- a/memory.c +++ b/memory.c @@ -47,6 +47,8 @@ static QTAILQ_HEAD(memory_listeners, MemoryListener) memory_listeners static QTAILQ_HEAD(, AddressSpace) address_spaces = QTAILQ_HEAD_INITIALIZER(address_spaces); +static GHashTable *flat_views; + typedef struct AddrRange AddrRange; /* @@ -761,6 +763,7 @@ static FlatView *generate_memory_topology(MemoryRegion *mr) flatview_add_to_dispatch(view, &mrs); } address_space_dispatch_compact(view->dispatch); + g_hash_table_replace(flat_views, mr, view); return view; } @@ -930,11 +933,47 @@ static void address_space_update_topology_pass(AddressSpace *as, } } -static void address_space_update_topology(AddressSpace *as) +static void flatviews_init(void) +{ + if (flat_views) { + return; + } + + flat_views = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, + (GDestroyNotify) flatview_unref); +} + +static void flatviews_reset(void) +{ + AddressSpace *as; + + if (flat_views) { + g_hash_table_unref(flat_views); + flat_views = NULL; + } + flatviews_init(); + + /* Render unique FVs */ + QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { + MemoryRegion *physmr = memory_region_get_flatview_root(as->root); + + if (g_hash_table_lookup(flat_views, physmr)) { + continue; + } + + generate_memory_topology(physmr); + } +} + +static void address_space_set_flatview(AddressSpace *as) { FlatView *old_view = address_space_get_flatview(as); - MemoryRegion *physmr = memory_region_get_flatview_root(old_view->root); - FlatView *new_view = generate_memory_topology(physmr); + MemoryRegion *physmr = memory_region_get_flatview_root(as->root); + FlatView *new_view = g_hash_table_lookup(flat_views, physmr); + + assert(new_view); + + flatview_ref(new_view); if (!QTAILQ_EMPTY(&as->listeners)) { address_space_update_topology_pass(as, old_view, new_view, false); @@ -970,10 +1009,12 @@ void memory_region_transaction_commit(void) --memory_region_transaction_depth; if (!memory_region_transaction_depth) { if (memory_region_update_pending) { + flatviews_reset(); + MEMORY_LISTENER_CALL_GLOBAL(begin, Forward); QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { - address_space_update_topology(as); + address_space_set_flatview(as); address_space_update_ioeventfds(as); } memory_region_update_pending = false; @@ -2696,13 +2737,6 @@ AddressSpace *address_space_init_shareable(MemoryRegion *root, const char *name) { AddressSpace *as; - QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { - if (root == as->root && as->malloced) { - as->ref_count++; - return as; - } - } - as = g_malloc0(sizeof *as); address_space_init(as, root, name); as->malloced = true;