From patchwork Mon May 6 14:25:20 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 241689 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A36A72C0545 for ; Tue, 7 May 2013 00:32:49 +1000 (EST) Received: from localhost ([::1]:34373 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UZMT9-0003hZ-PL for incoming@patchwork.ozlabs.org; Mon, 06 May 2013 10:32:47 -0400 Received: from eggs.gnu.org ([208.118.235.92]:41252) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UZMMb-0004YZ-L8 for qemu-devel@nongnu.org; Mon, 06 May 2013 10:26:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UZMMY-0005Uh-Gf for qemu-devel@nongnu.org; Mon, 06 May 2013 10:26:01 -0400 Received: from mail-ea0-x22b.google.com ([2a00:1450:4013:c01::22b]:44884) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UZMMX-0005UE-HT for qemu-devel@nongnu.org; Mon, 06 May 2013 10:25:58 -0400 Received: by mail-ea0-f171.google.com with SMTP id b10so1791888eae.2 for ; Mon, 06 May 2013 07:25:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:sender:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=9QI/U9kmrQcymTxl89uFEVUs+SoC/7qPTe3euL0FjBo=; b=Bln812KQCG4meWF3Uszr5AA+QggKCfkRG9n/cYlxH0gIiewnNcB+jj2aVT3E1bPvMZ GJw0y9hUQFuaRXFtH8BuqRDxAK0VAqldNLNbKVX4WyZY2L2KUcyVJda4Ga+d6NgWr+u9 Ql8EXMBPQ3jVgxYhaKPIUG3OugcpfNxNrZb3BJvDhCgPYzeDlNU010apEzA0/AHcFg3X 4Gdl7yTqjXmNE0xY4HCbKnxAff2BZCzqclVVkl6fma2rdVPmxDbGEzReOimGIcxIRM/z bkr4K1wvsg43aeI5GDonUvG4WaYPxIQg6XMNhO3FbPCFsqewER3tHaJiLDffSn4hijWU ma8Q== X-Received: by 10.14.100.1 with SMTP id y1mr61421409eef.9.1367850356526; Mon, 06 May 2013 07:25:56 -0700 (PDT) Received: from localhost.localdomain (93-34-176-20.ip50.fastwebnet.it. [93.34.176.20]) by mx.google.com with ESMTPSA id x41sm24918585eey.17.2013.05.06.07.25.54 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 06 May 2013 07:25:55 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 6 May 2013 16:25:20 +0200 Message-Id: <1367850321-1732-8-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1367850321-1732-1-git-send-email-pbonzini@redhat.com> References: <1367850321-1732-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:4013:c01::22b Cc: jan.kiszka@siemens.com, qemulist@gmail.com, stefanha@redhat.com Subject: [Qemu-devel] [RFC PATCH 7/8] memory: add reference counting to FlatView X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org With this change, a FlatView can be used even after a concurrent update has replaced it. Because we do not have RCU, we use a mutex to protect the small critical sections that read/write the as->current_map pointer. Accesses to the FlatView can be done outside the mutex. If a MemoryRegion will be used after the FlatView is unref-ed (or after a MemoryListener callback is returned), a reference has to be added to that MemoryRegion. For example, memory_region_find adds a reference to the MemoryRegion that it returns. Signed-off-by: Paolo Bonzini --- memory.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 67 insertions(+), 8 deletions(-) diff --git a/memory.c b/memory.c index fbb2657..f02a3cc 100644 --- a/memory.c +++ b/memory.c @@ -26,12 +26,26 @@ static unsigned memory_region_transaction_depth; static bool memory_region_update_pending; static bool global_dirty_log = false; +/* Either the flat_view_mutex or the iothread mutex can be taken around reads + * of as->current_map; the critical section is extremely short, so I'm using a + * single mutex for every AS. We could also RCU for the read-side. + * + * Both locks are taken while writing to as->current_map (with the iothread + * mutex outside). + */ +static QemuMutex flat_view_mutex; + static QTAILQ_HEAD(memory_listeners, MemoryListener) memory_listeners = QTAILQ_HEAD_INITIALIZER(memory_listeners); static QTAILQ_HEAD(, AddressSpace) address_spaces = QTAILQ_HEAD_INITIALIZER(address_spaces); +static void memory_init(void) +{ + qemu_mutex_init(&flat_view_mutex); +} + typedef struct AddrRange AddrRange; /* @@ -222,6 +236,7 @@ struct FlatRange { * order. */ struct FlatView { + unsigned ref; FlatRange *ranges; unsigned nr; unsigned nr_allocated; @@ -243,6 +258,7 @@ static bool flatrange_equal(FlatRange *a, FlatRange *b) static void flatview_init(FlatView *view) { + view->ref = 1; view->ranges = NULL; view->nr = 0; view->nr_allocated = 0; @@ -276,6 +292,18 @@ static void flatview_destroy(FlatView *view) g_free(view); } +static void flatview_ref(FlatView *view) +{ + __sync_fetch_and_add(&view->ref, 1); +} + +static void flatview_unref(FlatView *view) +{ + if (__sync_fetch_and_sub(&view->ref, 1) == 1) { + flatview_destroy(view); + } +} + static bool can_merge(FlatRange *r1, FlatRange *r2) { return int128_eq(addrrange_end(r1->addr), r2->addr.start) @@ -728,16 +756,38 @@ static void address_space_update_topology_pass(AddressSpace *as, } +static FlatView *address_space_get_flatview(AddressSpace *as) +{ + FlatView *view; + + qemu_mutex_lock(&flat_view_mutex); + view = as->current_map; + flatview_ref(view); + qemu_mutex_unlock(&flat_view_mutex); + return view; +} + static void address_space_update_topology(AddressSpace *as) { - FlatView *old_view = as->current_map; + FlatView *old_view = address_space_get_flatview(as); FlatView *new_view = generate_memory_topology(as->root); address_space_update_topology_pass(as, old_view, new_view, false); address_space_update_topology_pass(as, old_view, new_view, true); + qemu_mutex_lock(&flat_view_mutex); + flatview_unref(as->current_map); as->current_map = new_view; - flatview_destroy(old_view); + qemu_mutex_unlock(&flat_view_mutex); + + /* Note that all the old MemoryRegions are still alive up to this + * point. This relieves most MemoryListeners from the need to + * ref/unref the MemoryRegions they get---unless they use them + * outside the iothread mutex, in which case precise reference + * counting is necessary. + */ + flatview_unref(old_view); + address_space_update_ioeventfds(as); } @@ -1138,12 +1188,13 @@ void memory_region_sync_dirty_bitmap(MemoryRegion *mr) FlatRange *fr; QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { - FlatView *view = as->current_map; + FlatView *view = address_space_get_flatview(as); FOR_EACH_FLAT_RANGE(fr, view) { if (fr->mr == mr) { MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync); } } + flatview_unref(view); } } @@ -1195,7 +1246,7 @@ static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpa AddrRange tmp; MemoryRegionSection section; - view = as->current_map; + view = address_space_get_flatview(as); FOR_EACH_FLAT_RANGE(fr, view) { if (fr->mr == mr) { section = (MemoryRegionSection) { @@ -1221,6 +1272,7 @@ static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpa } } } + flatview_unref(view); } static void memory_region_update_coalesced_range(MemoryRegion *mr) @@ -1508,7 +1560,7 @@ MemoryRegionSection memory_region_find(MemoryRegion *mr, as = memory_region_to_address_space(root); range = addrrange_make(int128_make64(addr), int128_make64(size)); - view = as->current_map; + view = address_space_get_flatview(as); fr = flatview_lookup(view, range); if (!fr) { return ret; @@ -1529,6 +1581,7 @@ MemoryRegionSection memory_region_find(MemoryRegion *mr, ret.readonly = fr->readonly; memory_region_ref(ret.mr); + flatview_unref(view); return ret; } @@ -1537,10 +1590,11 @@ void address_space_sync_dirty_bitmap(AddressSpace *as) FlatView *view; FlatRange *fr; - view = as->current_map; + view = address_space_get_flatview(as); FOR_EACH_FLAT_RANGE(fr, view) { MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync); } + flatview_unref(view); } void memory_global_dirty_log_start(void) @@ -1572,7 +1626,7 @@ static void listener_add_address_space(MemoryListener *listener, } } - view = as->current_map; + view = address_space_get_flatview(as); FOR_EACH_FLAT_RANGE(fr, view) { MemoryRegionSection section = { .mr = fr->mr, @@ -1586,6 +1640,7 @@ static void listener_add_address_space(MemoryListener *listener, listener->region_add(listener, §ion); } } + flatview_unref(view); } void memory_listener_register(MemoryListener *listener, AddressSpace *filter) @@ -1619,6 +1674,10 @@ void memory_listener_unregister(MemoryListener *listener) void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name) { + if (QTAILQ_EMPTY(&address_spaces)) { + memory_init(); + } + memory_region_transaction_begin(); as->root = root; as->current_map = g_new(FlatView, 1); @@ -1640,7 +1699,7 @@ void address_space_destroy(AddressSpace *as) memory_region_transaction_commit(); QTAILQ_REMOVE(&address_spaces, as, address_spaces_link); address_space_destroy_dispatch(as); - flatview_destroy(as->current_map); + flatview_unref(as->current_map); g_free(as->name); g_free(as->current_map); g_free(as->ioeventfds);