From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35722) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b6Eg9-00036o-TS for qemu-devel@nongnu.org; Fri, 27 May 2016 06:07:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b6Eg3-0007Wf-Hn for qemu-devel@nongnu.org; Fri, 27 May 2016 06:07:41 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45334) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b6Eg3-0007WY-9N for qemu-devel@nongnu.org; Fri, 27 May 2016 06:07:35 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 015EAC04B327 for ; Fri, 27 May 2016 10:07:35 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-66.ams2.redhat.com [10.36.112.66]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u4RA6isn030403 for ; Fri, 27 May 2016 06:07:34 -0400 From: Paolo Bonzini Date: Fri, 27 May 2016 12:06:43 +0200 Message-Id: <1464343604-517-31-git-send-email-pbonzini@redhat.com> In-Reply-To: <1464343604-517-1-git-send-email-pbonzini@redhat.com> References: <1464343604-517-1-git-send-email-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 30/31] memory: split memory_region_from_host from qemu_ram_addr_from_host List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Move the old qemu_ram_addr_from_host to memory_region_from_host and make it return an offset within the region. For qemu_ram_addr_from_host return the ram_addr_t directly, similar to what it was before commit 1b5ec23 ("memory: return MemoryRegion from qemu_ram_addr_from_host= ", 2013-07-04). Reviewed-by: Marc-Andr=C3=A9 Lureau Signed-off-by: Paolo Bonzini --- cputlb.c | 3 ++- exec.c | 10 +++++----- hw/virtio/vhost-user.c | 16 +++++++--------- include/exec/cpu-common.h | 2 +- include/exec/memory.h | 20 ++++++++++++++++++++ memory.c | 14 ++++++++++++-- target-i386/kvm.c | 6 ++++-- 7 files changed, 51 insertions(+), 20 deletions(-) diff --git a/cputlb.c b/cputlb.c index 1ff6354..23c9b91 100644 --- a/cputlb.c +++ b/cputlb.c @@ -246,7 +246,8 @@ static inline ram_addr_t qemu_ram_addr_from_host_nofa= il(void *ptr) { ram_addr_t ram_addr; =20 - if (qemu_ram_addr_from_host(ptr, &ram_addr) =3D=3D NULL) { + ram_addr =3D qemu_ram_addr_from_host(ptr); + if (ram_addr =3D=3D RAM_ADDR_INVALID) { fprintf(stderr, "Bad ram pointer %p\n", ptr); abort(); } diff --git a/exec.c b/exec.c index 65bad53..7f62835 100644 --- a/exec.c +++ b/exec.c @@ -1964,18 +1964,17 @@ RAMBlock *qemu_ram_block_by_name(const char *name= ) =20 /* Some of the softmmu routines need to translate from a host pointer (typically a TLB entry) back to a ram offset. */ -MemoryRegion *qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr) +ram_addr_t qemu_ram_addr_from_host(void *ptr) { RAMBlock *block; ram_addr_t offset; =20 block =3D qemu_ram_block_from_host(ptr, false, &offset); - *ram_addr =3D block->offset + offset; if (!block) { - return NULL; + return RAM_ADDR_INVALID; } =20 - return block->mr; + return block->offset + offset; } =20 /* Called within RCU critical section. */ @@ -2975,8 +2974,9 @@ void address_space_unmap(AddressSpace *as, void *bu= ffer, hwaddr len, MemoryRegion *mr; ram_addr_t addr1; =20 - mr =3D qemu_ram_addr_from_host(buffer, &addr1); + mr =3D memory_region_from_host(buffer, &addr1); assert(mr !=3D NULL); + addr1 +=3D memory_region_get_ram_addr(mr); if (is_write) { invalidate_and_set_dirty(mr, addr1, access_len); } diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 41908c0..495e09f 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -17,7 +17,6 @@ #include "sysemu/kvm.h" #include "qemu/error-report.h" #include "qemu/sockets.h" -#include "exec/ram_addr.h" #include "migration/migration.h" =20 #include @@ -247,19 +246,18 @@ static int vhost_user_set_mem_table(struct vhost_de= v *dev, =20 for (i =3D 0; i < dev->mem->nregions; ++i) { struct vhost_memory_region *reg =3D dev->mem->regions + i; - ram_addr_t ram_addr; + ram_addr_t offset; MemoryRegion *mr; =20 assert((uintptr_t)reg->userspace_addr =3D=3D reg->userspace_addr= ); - mr =3D qemu_ram_addr_from_host((void *)(uintptr_t)reg->userspace= _addr, - &ram_addr); + mr =3D memory_region_from_host((void *)(uintptr_t)reg->userspace= _addr, + &offset); fd =3D memory_region_get_fd(mr); if (fd > 0) { msg.payload.memory.regions[fd_num].userspace_addr =3D reg->u= serspace_addr; msg.payload.memory.regions[fd_num].memory_size =3D reg->mem= ory_size; msg.payload.memory.regions[fd_num].guest_phys_addr =3D reg->= guest_phys_addr; - msg.payload.memory.regions[fd_num].mmap_offset =3D reg->user= space_addr - - (uintptr_t) memory_region_get_ram_ptr(mr); + msg.payload.memory.regions[fd_num].mmap_offset =3D offset; assert(fd_num < VHOST_MEMORY_MAX_NREGIONS); fds[fd_num++] =3D fd; } @@ -617,14 +615,14 @@ static bool vhost_user_can_merge(struct vhost_dev *= dev, uint64_t start1, uint64_t size1, uint64_t start2, uint64_t size2) { - ram_addr_t ram_addr; + ram_addr_t offset; int mfd, rfd; MemoryRegion *mr; =20 - mr =3D qemu_ram_addr_from_host((void *)(uintptr_t)start1, &ram_addr)= ; + mr =3D memory_region_from_host((void *)(uintptr_t)start1, &offset); mfd =3D memory_region_get_fd(mr); =20 - mr =3D qemu_ram_addr_from_host((void *)(uintptr_t)start2, &ram_addr)= ; + mr =3D memory_region_from_host((void *)(uintptr_t)start2, &offset); rfd =3D memory_region_get_fd(mr); =20 return mfd =3D=3D rfd; diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h index fc609ad..aaee995 100644 --- a/include/exec/cpu-common.h +++ b/include/exec/cpu-common.h @@ -57,7 +57,7 @@ typedef uint32_t CPUReadMemoryFunc(void *opaque, hwaddr= addr); =20 void qemu_ram_remap(ram_addr_t addr, ram_addr_t length); /* This should not be used by devices. */ -MemoryRegion *qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr); +ram_addr_t qemu_ram_addr_from_host(void *ptr); RAMBlock *qemu_ram_block_by_name(const char *name); RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset, ram_addr_t *offset); diff --git a/include/exec/memory.h b/include/exec/memory.h index 1678494..71a27ab 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -32,6 +32,8 @@ #include "qom/object.h" #include "qemu/rcu.h" =20 +#define RAM_ADDR_INVALID (~(ram_addr_t)0) + #define MAX_PHYS_ADDR_SPACE_BITS 62 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS= ) - 1) =20 @@ -678,6 +680,24 @@ int memory_region_get_fd(MemoryRegion *mr); void memory_region_set_fd(MemoryRegion *mr, int fd); =20 /** + * memory_region_from_host: Convert a pointer into a RAM memory region + * and an offset within it. + * + * Given a host pointer inside a RAM memory region (created with + * memory_region_init_ram() or memory_region_init_ram_ptr()), return + * the MemoryRegion and the offset within it. + * + * Use with care; by the time this function returns, the returned pointe= r is + * not protected by RCU anymore. If the caller is not within an RCU cri= tical + * section and does not hold the iothread lock, it must have other means= of + * protecting the pointer, such as a reference to the region that includ= es + * the incoming ram_addr_t. + * + * @mr: the memory region being queried. + */ +MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset); + +/** * memory_region_get_ram_ptr: Get a pointer into a RAM memory region. * * Returns a host pointer to a RAM memory region (created with diff --git a/memory.c b/memory.c index d6a4a68..f8085ea 100644 --- a/memory.c +++ b/memory.c @@ -33,8 +33,6 @@ =20 //#define DEBUG_UNASSIGNED =20 -#define RAM_ADDR_INVALID (~(ram_addr_t)0) - static unsigned memory_region_transaction_depth; static bool memory_region_update_pending; static bool ioeventfd_update_pending; @@ -1665,6 +1663,18 @@ void *memory_region_get_ram_ptr(MemoryRegion *mr) return ptr + offset; } =20 +MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset) +{ + RAMBlock *block; + + block =3D qemu_ram_block_from_host(ptr, false, offset); + if (!block) { + return NULL; + } + + return block->mr; +} + ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr) { return mr->ram_block ? mr->ram_block->offset : RAM_ADDR_INVALID; diff --git a/target-i386/kvm.c b/target-i386/kvm.c index 7b3667a..abf50e6 100644 --- a/target-i386/kvm.c +++ b/target-i386/kvm.c @@ -411,7 +411,8 @@ int kvm_arch_on_sigbus_vcpu(CPUState *c, int code, vo= id *addr) =20 if ((env->mcg_cap & MCG_SER_P) && addr && (code =3D=3D BUS_MCEERR_AR || code =3D=3D BUS_MCEERR_AO)) { - if (qemu_ram_addr_from_host(addr, &ram_addr) =3D=3D NULL || + ram_addr =3D qemu_ram_addr_from_host(addr); + if (ram_addr =3D=3D RAM_ADDR_INVALID || !kvm_physical_memory_addr_from_host(c->kvm_state, addr, &pad= dr)) { fprintf(stderr, "Hardware memory error for memory used by " "QEMU itself instead of guest system!\n"); @@ -445,7 +446,8 @@ int kvm_arch_on_sigbus(int code, void *addr) hwaddr paddr; =20 /* Hope we are lucky for AO MCE */ - if (qemu_ram_addr_from_host(addr, &ram_addr) =3D=3D NULL || + ram_addr =3D qemu_ram_addr_from_host(addr); + if (ram_addr =3D=3D RAM_ADDR_INVALID || !kvm_physical_memory_addr_from_host(first_cpu->kvm_state, addr, &paddr)) { fprintf(stderr, "Hardware memory error for memory used by " --=20 2.5.5