From patchwork Thu Dec 17 17:46:28 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 558531 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id DE2B21402BF for ; Fri, 18 Dec 2015 05:08:41 +1100 (AEDT) Received: from localhost ([::1]:55655 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a9cyl-0006Rn-ID for incoming@patchwork.ozlabs.org; Thu, 17 Dec 2015 13:08:39 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51353) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a9ceV-0001qr-OM for qemu-devel@nongnu.org; Thu, 17 Dec 2015 12:47:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a9ceU-0006yI-Gi for qemu-devel@nongnu.org; Thu, 17 Dec 2015 12:47:43 -0500 Received: from mx1.redhat.com ([209.132.183.28]:43679) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a9ceU-0006yC-9h for qemu-devel@nongnu.org; Thu, 17 Dec 2015 12:47:42 -0500 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 E3121694 for ; Thu, 17 Dec 2015 17:47:41 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-23.ams2.redhat.com [10.36.112.23]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tBHHkfcY014618 for ; Thu, 17 Dec 2015 12:47:41 -0500 From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 17 Dec 2015 18:46:28 +0100 Message-Id: <1450374401-31352-33-git-send-email-pbonzini@redhat.com> In-Reply-To: <1450374401-31352-1-git-send-email-pbonzini@redhat.com> References: <1450374401-31352-1-git-send-email-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 32/45] exec: always call qemu_get_ram_ptr within rcu_read_lock 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 Simplify the code and document the assumption. The only caller that is not within rcu_read_lock is memory_region_get_ram_ptr. Signed-off-by: Paolo Bonzini --- exec.c | 22 +++++----------------- include/exec/memory.h | 9 +++++++-- memory.c | 14 ++++++++++---- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/exec.c b/exec.c index dd11ef9..af95438 100644 --- a/exec.c +++ b/exec.c @@ -1786,19 +1786,11 @@ void *qemu_get_ram_block_host_ptr(ram_addr_t addr) * or address_space_rw instead. For local memory (e.g. video ram) that the * device owns, use memory_region_get_ram_ptr. * - * By the time this function returns, the returned pointer is not protected - * by RCU anymore. If the caller is not within an RCU critical 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 includes the incoming - * ram_addr_t. + * Called within RCU critical section. */ void *qemu_get_ram_ptr(ram_addr_t addr) { - RAMBlock *block; - void *ptr; - - rcu_read_lock(); - block = qemu_get_ram_block(addr); + RAMBlock *block = qemu_get_ram_block(addr); if (xen_enabled() && block->host == NULL) { /* We need to check if the requested address is in the RAM @@ -1806,17 +1798,12 @@ void *qemu_get_ram_ptr(ram_addr_t addr) * In that case just map until the end of the page. */ if (block->offset == 0) { - ptr = xen_map_cache(addr, 0, 0); - goto unlock; + return xen_map_cache(addr, 0, 0); } block->host = xen_map_cache(block->offset, block->max_length, 1); } - ptr = ramblock_ptr(block, addr - block->offset); - -unlock: - rcu_read_unlock(); - return ptr; + return ramblock_ptr(block, addr - block->offset); } /* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr @@ -1954,6 +1941,7 @@ MemoryRegion *qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr) return block->mr; } +/* Called within RCU critical section. */ static void notdirty_mem_write(void *opaque, hwaddr ram_addr, uint64_t val, unsigned size) { diff --git a/include/exec/memory.h b/include/exec/memory.h index 0f07159..9bbd247 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -656,8 +656,13 @@ int memory_region_get_fd(MemoryRegion *mr); * memory_region_get_ram_ptr: Get a pointer into a RAM memory region. * * Returns a host pointer to a RAM memory region (created with - * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with - * care. + * memory_region_init_ram() or memory_region_init_ram_ptr()). + * + * Use with care; by the time this function returns, the returned pointer is + * not protected by RCU anymore. If the caller is not within an RCU critical + * 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 includes + * the incoming ram_addr_t. * * @mr: the memory region being queried. */ diff --git a/memory.c b/memory.c index 1c1c192..f666c77 100644 --- a/memory.c +++ b/memory.c @@ -1577,13 +1577,19 @@ int memory_region_get_fd(MemoryRegion *mr) void *memory_region_get_ram_ptr(MemoryRegion *mr) { - if (mr->alias) { - return memory_region_get_ram_ptr(mr->alias) + mr->alias_offset; - } + void *ptr; + uint64_t offset = 0; + rcu_read_lock(); + while (mr->alias) { + offset += mr->alias_offset; + mr = mr->alias; + } assert(mr->ram_addr != RAM_ADDR_INVALID); + ptr = qemu_get_ram_ptr(mr->ram_addr & TARGET_PAGE_MASK); + rcu_read_unlock(); - return qemu_get_ram_ptr(mr->ram_addr & TARGET_PAGE_MASK); + return ptr + offset; } void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, Error **errp)