From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55235) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZkYn1-0007wP-Sx for qemu-devel@nongnu.org; Fri, 09 Oct 2015 10:37:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZkYmw-0003Rl-F9 for qemu-devel@nongnu.org; Fri, 09 Oct 2015 10:36:55 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51036) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZkYmw-0003RO-9U for qemu-devel@nongnu.org; Fri, 09 Oct 2015 10:36:50 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id CFB728CF5E for ; Fri, 9 Oct 2015 14:36:49 +0000 (UTC) From: Markus Armbruster Date: Fri, 9 Oct 2015 16:36:38 +0200 Message-Id: <1444401407-7849-4-git-send-email-armbru@redhat.com> In-Reply-To: <1444401407-7849-1-git-send-email-armbru@redhat.com> References: <1444401407-7849-1-git-send-email-armbru@redhat.com> Subject: [Qemu-devel] [PULL v3 03/12] memory: allow destroying a non-empty MemoryRegion List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Paolo Bonzini From: Paolo Bonzini This is legal; the MemoryRegion will simply unreference all the existing subregions and possibly bring them down with it as well. However, it requires a bit of care to avoid an infinite loop. Finalizing a memory region cannot trigger an address space update, but memory_region_del_subregion errs on the side of caution and might trigger a spurious update: avoid that by resetting mr->enabled first. Signed-off-by: Paolo Bonzini Signed-off-by: Markus Armbruster Message-Id: <1443689999-12182-2-git-send-email-armbru@redhat.com> --- memory.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/memory.c b/memory.c index 1b03d22..2eb1597 100644 --- a/memory.c +++ b/memory.c @@ -1304,7 +1304,22 @@ static void memory_region_finalize(Object *obj) { MemoryRegion *mr = MEMORY_REGION(obj); - assert(QTAILQ_EMPTY(&mr->subregions)); + assert(!mr->container); + + /* We know the region is not visible in any address space (it + * does not have a container and cannot be a root either because + * it has no references, so we can blindly clear mr->enabled. + * memory_region_set_enabled instead could trigger a transaction + * and cause an infinite loop. + */ + mr->enabled = false; + memory_region_transaction_begin(); + while (!QTAILQ_EMPTY(&mr->subregions)) { + MemoryRegion *subregion = QTAILQ_FIRST(&mr->subregions); + memory_region_del_subregion(mr, subregion); + } + memory_region_transaction_commit(); + mr->destructor(mr); memory_region_clear_coalescing(mr); g_free((char *)mr->name); -- 2.4.3