Received: from eggs.gnu.org ([208.118.235.92]:43030) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UZKrQ-0000O9-7P for qemu-devel@nongnu.org; Mon, 06 May 2013 08:49:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UZKrN-0007rP-Pg for qemu-devel@nongnu.org; Mon, 06 May 2013 08:49:44 -0400 Received: from mail-ve0-x22e.google.com ([2607:f8b0:400c:c01::22e]:62779) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UZKrN-0007rJ-Ke for qemu-devel@nongnu.org; Mon, 06 May 2013 08:49:41 -0400 Received: by mail-ve0-f174.google.com with SMTP id pb11so3083829veb.19 for ; Mon, 06 May 2013 05:49:41 -0700 (PDT) From: Liu Ping Fan Date: Mon, 6 May 2013 20:48:59 +0800 Message-Id: <1367844540-9024-5-git-send-email-qemulist@gmail.com> In-Reply-To: <1367844540-9024-1-git-send-email-qemulist@gmail.com> References: <1367844540-9024-1-git-send-email-qemulist@gmail.com> Subject: [Qemu-devel] [PATCH v3 4/5] hostmem: hostmem listener pin RAM-Device by refcnt List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Peter Maydell , Anthony Liguori , Jan Kiszka , Vasilis Liaskovitis , Stefan Hajnoczi , Paolo Bonzini From: Liu Ping Fan With ref()/unref() interface of MemoryRegion, we can pin RAM-Device when using its memory, and release it when done. Signed-off-by: Liu Ping Fan --- hw/virtio/dataplane/hostmem.c | 24 +++++++++++++++++++----- hw/virtio/dataplane/vring.c | 8 ++++---- include/hw/virtio/dataplane/hostmem.h | 4 +++- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/hw/virtio/dataplane/hostmem.c b/hw/virtio/dataplane/hostmem.c index f31a703..420c9be 100644 --- a/hw/virtio/dataplane/hostmem.c +++ b/hw/virtio/dataplane/hostmem.c @@ -42,18 +42,23 @@ static void hostmem_ref(HostMem *hostmem) static void hostmem_unref(HostMem *hostmem) { - int t; + int i, t; + HostMemRegion *hmr; t = __sync_sub_and_fetch(&hostmem->ref, 1); assert(t >= 0); if (!t) { + for (i = 0; i < hostmem->num_current_regions; i++) { + hmr = &hostmem->current_regions[i]; + memory_region_unref(hmr->mr); + } g_free(hostmem->current_regions); g_free(hostmem); } } static void *address_space_mem_lookup(AddrSpaceMem *as_mem, hwaddr phys, - hwaddr len, bool is_write) + hwaddr len, MemoryRegion **mr, bool is_write) { HostMemRegion *region; void *host_addr = NULL; @@ -65,6 +70,9 @@ static void *address_space_mem_lookup(AddrSpaceMem *as_mem, hwaddr phys, hostmem_ref(hostmem); qemu_mutex_unlock(&as_mem->cur_lock); + if (mr) { + *mr = NULL; + } region = bsearch(&phys, hostmem->current_regions, hostmem->num_current_regions, sizeof(hostmem->current_regions[0]), @@ -79,7 +87,10 @@ static void *address_space_mem_lookup(AddrSpaceMem *as_mem, hwaddr phys, if (len <= region->size - offset_within_region) { host_addr = region->host_addr + offset_within_region; } - + if (mr) { + *mr = region->mr; + memory_region_ref(*mr); + } out: hostmem_unref(hostmem); return host_addr; @@ -88,9 +99,10 @@ out: /** * Map guest physical address to host pointer */ -void *hostmem_lookup(hwaddr phys, hwaddr len, bool is_write) +void *hostmem_lookup(hwaddr phys, hwaddr len, MemoryRegion **mr, + bool is_write) { - return address_space_mem_lookup(system_mem, phys, len, is_write); + return address_space_mem_lookup(system_mem, phys, len, mr, is_write); } static void hostmem_listener_begin(MemoryListener *listener) @@ -134,6 +146,7 @@ static void hostmem_append_new_region(HostMem *hostmem, hostmem->current_regions[num] = (HostMemRegion){ .host_addr = ram_ptr + section->offset_within_region, .guest_addr = section->offset_within_address_space, + .mr = section->mr, .size = section->size, .readonly = section->readonly, }; @@ -155,6 +168,7 @@ static void hostmem_listener_append_region(MemoryListener *listener, return; } + memory_region_ref(section->mr); hostmem_append_new_region(as_mem->next_hostmem, section); } diff --git a/hw/virtio/dataplane/vring.c b/hw/virtio/dataplane/vring.c index 4d6d735..e3c3afb 100644 --- a/hw/virtio/dataplane/vring.c +++ b/hw/virtio/dataplane/vring.c @@ -27,7 +27,7 @@ bool vring_setup(Vring *vring, VirtIODevice *vdev, int n) vring->broken = false; - vring_ptr = hostmem_lookup(vring_addr, vring_size, true); + vring_ptr = hostmem_lookup(vring_addr, vring_size, NULL, true); if (!vring_ptr) { error_report("Failed to map vring " "addr %#" HWADDR_PRIx " size %" HWADDR_PRIu, @@ -137,7 +137,7 @@ static int get_indirect(Vring *vring, /* Translate indirect descriptor */ desc_ptr = hostmem_lookup(indirect->addr + found * sizeof(desc), - sizeof(desc), false); + sizeof(desc), NULL, false); if (!desc_ptr) { error_report("Failed to map indirect descriptor " "addr %#" PRIx64 " len %zu", @@ -169,7 +169,7 @@ static int get_indirect(Vring *vring, return -ENOBUFS; } - iov->iov_base = hostmem_lookup(desc.addr, desc.len, + iov->iov_base = hostmem_lookup(desc.addr, desc.len, NULL, desc.flags & VRING_DESC_F_WRITE); if (!iov->iov_base) { error_report("Failed to map indirect descriptor" @@ -297,7 +297,7 @@ int vring_pop(VirtIODevice *vdev, Vring *vring, } /* TODO handle non-contiguous memory across region boundaries */ - iov->iov_base = hostmem_lookup(desc.addr, desc.len, + iov->iov_base = hostmem_lookup(desc.addr, desc.len, NULL, desc.flags & VRING_DESC_F_WRITE); if (!iov->iov_base) { error_report("Failed to map vring desc addr %#" PRIx64 " len %u", diff --git a/include/hw/virtio/dataplane/hostmem.h b/include/hw/virtio/dataplane/hostmem.h index d04ce55..094102b 100644 --- a/include/hw/virtio/dataplane/hostmem.h +++ b/include/hw/virtio/dataplane/hostmem.h @@ -21,6 +21,7 @@ typedef struct { void *host_addr; hwaddr guest_addr; + MemoryRegion *mr; uint64_t size; bool readonly; } HostMemRegion; @@ -52,7 +53,8 @@ typedef struct { * can be done with other mechanisms like bdrv_drain_all() that quiesce * in-flight I/O. */ -void *hostmem_lookup(hwaddr phys, hwaddr len, bool is_write); +void *hostmem_lookup(hwaddr phys, hwaddr len, MemoryRegion **mr, + bool is_write); void hostmem_init(void); #else -- 1.7.4.4