From patchwork Tue May 7 14:17:02 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 242388 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 756912C00FD for ; Wed, 8 May 2013 02:02:45 +1000 (EST) Received: from localhost ([::1]:56047 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UZiv3-00044l-LW for incoming@patchwork.ozlabs.org; Tue, 07 May 2013 10:31:05 -0400 Received: from eggs.gnu.org ([208.118.235.92]:32804) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UZit5-0002yG-Q5 for qemu-devel@nongnu.org; Tue, 07 May 2013 10:29:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UZit0-0001Qo-Vn for qemu-devel@nongnu.org; Tue, 07 May 2013 10:29:03 -0400 Received: from mail-wg0-x235.google.com ([2a00:1450:400c:c00::235]:41054) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UZiit-000601-DZ for qemu-devel@nongnu.org; Tue, 07 May 2013 10:18:31 -0400 Received: by mail-wg0-f53.google.com with SMTP id y10so649600wgg.8 for ; Tue, 07 May 2013 07:18:30 -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=cq9+DH0yZW40mKgBSCyBr67SkDQTsAkJOCnWOShw+0g=; b=L3C5ku8Krp22FMZPRmNldhPLo7TlUsHrtoq6XYo8YJNuBEyABgIHvB3JZiNBChlIxb 1mr/WNDWDnWdCh/4EEbjMyhGtLE3eMP9AhW4duCZcasO5YNqSCo8pzILAvx7nVSUnump N/bO6HvmRpnamTBswapyBXaYdqDXc+FLj0BdHIxDP2K9a4S05BwFPPcblbYy4/iXKEVu +lpIsbMZjw9K08gXNu0BcI6W/FV1IveaUSBBSzmIkh9rmt5iUrhhiC1xFSFcANygKQJR MBhzoVEuOW6E6jCSia44MyuzsDw59Lww7wBFzcuCaEaqx2iGwZNPBf4j7V7A+PjEJ7EW TmQw== X-Received: by 10.180.185.135 with SMTP id fc7mr15279186wic.29.1367936310714; Tue, 07 May 2013 07:18:30 -0700 (PDT) Received: from localhost.localdomain (93-34-176-20.ip50.fastwebnet.it. [93.34.176.20]) by mx.google.com with ESMTPSA id ge7sm3335978wib.6.2013.05.07.07.18.28 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 07 May 2013 07:18:29 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 7 May 2013 16:17:02 +0200 Message-Id: <1367936238-12196-25-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1367936238-12196-1-git-send-email-pbonzini@redhat.com> References: <1367936238-12196-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:400c:c00::235 Cc: aik@ozlabs.ru, jan.kiszka@siemens.com, qemulist@gmail.com, stefanha@redhat.com, david@gibson.dropbear.id.au Subject: [Qemu-devel] [PATCH 24/40] memory: add getter/setter for owner 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 Whenever memory regions are accessed outside the BQL, they need to be preserved against hot-unplug. MemoryRegions actually do not have their own reference count; they piggyback on a QOM object, their "owner". Add two functions to retrieve and specify the owner. The setter function will affect the owner recursively on a whole tree of contained regions, but without crossing (a) aliases (b) regions that are already owned by another device. This is so that a device can create a complex tree of regions and a single call to memory_region_set_owner (perhaps even within a bus-specific function, e.g. pci_register_bar) will affect the entire tree. Signed-off-by: Paolo Bonzini --- include/exec/memory.h | 18 ++++++++++++++++++ memory.c | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 0 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index 06586f3..5c20bac 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -135,6 +135,7 @@ struct MemoryRegion { const MemoryRegionIOMMUOps *iommu_ops; void *opaque; MemoryRegion *parent; + struct Object *owner; Int128 size; hwaddr addr; void (*destructor)(MemoryRegion *mr); @@ -374,6 +375,23 @@ void memory_region_init_iommu(MemoryRegion *mr, void memory_region_destroy(MemoryRegion *mr); /** + * memory_region_owner: get a memory region's owner. + * + * @mr: the memory region being queried. + */ +struct Object *memory_region_owner(MemoryRegion *mr); + +/** + * memory_region_set_owner: set the owner for a memory region and all + * the unowned regions below it. + * + * @mr: the memory region being set. + * @owner: the object that acts as the owner + */ +void memory_region_set_owner(MemoryRegion *mr, + struct Object *owner); + +/** * memory_region_size: get a memory region's size. * * @mr: the memory region being queried. diff --git a/memory.c b/memory.c index 00e31c0..f7fddb1 100644 --- a/memory.c +++ b/memory.c @@ -789,6 +789,7 @@ void memory_region_init(MemoryRegion *mr, mr->ops = NULL; mr->iommu_ops = NULL; mr->parent = NULL; + mr->owner = NULL; mr->size = int128_make64(size); if (size == UINT64_MAX) { mr->size = int128_2_64(); @@ -1040,6 +1041,26 @@ void memory_region_destroy(MemoryRegion *mr) g_free(mr->ioeventfds); } +Object *memory_region_owner(MemoryRegion *mr) +{ + return mr->owner; +} + +void memory_region_set_owner(MemoryRegion *mr, + Object *owner) +{ + MemoryRegion *child; + + assert(mr->owner == NULL || mr->owner == owner); + mr->owner = owner; + + QTAILQ_FOREACH(child, &mr->subregions, subregions_link) { + if (child->owner == NULL || child->owner == owner) { + memory_region_set_owner(child, owner); + } + } +} + uint64_t memory_region_size(MemoryRegion *mr) { if (int128_eq(mr->size, int128_2_64())) {