exec: always call qemu_get_ram_ptr within rcu_read_lock

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 <pbonzini@redhat.com>
This commit is contained in:
Paolo Bonzini 2015-12-16 10:30:47 +01:00
parent c8ee0a445a
commit 49b24afcb1
3 changed files with 22 additions and 23 deletions

22
exec.c
View File

@ -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 * or address_space_rw instead. For local memory (e.g. video ram) that the
* device owns, use memory_region_get_ram_ptr. * device owns, use memory_region_get_ram_ptr.
* *
* By the time this function returns, the returned pointer is not protected * Called within RCU critical section.
* 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.
*/ */
void *qemu_get_ram_ptr(ram_addr_t addr) void *qemu_get_ram_ptr(ram_addr_t addr)
{ {
RAMBlock *block; RAMBlock *block = qemu_get_ram_block(addr);
void *ptr;
rcu_read_lock();
block = qemu_get_ram_block(addr);
if (xen_enabled() && block->host == NULL) { if (xen_enabled() && block->host == NULL) {
/* We need to check if the requested address is in the RAM /* 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. * In that case just map until the end of the page.
*/ */
if (block->offset == 0) { if (block->offset == 0) {
ptr = xen_map_cache(addr, 0, 0); return xen_map_cache(addr, 0, 0);
goto unlock;
} }
block->host = xen_map_cache(block->offset, block->max_length, 1); block->host = xen_map_cache(block->offset, block->max_length, 1);
} }
ptr = ramblock_ptr(block, addr - block->offset); return ramblock_ptr(block, addr - block->offset);
unlock:
rcu_read_unlock();
return ptr;
} }
/* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr /* 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; return block->mr;
} }
/* Called within RCU critical section. */
static void notdirty_mem_write(void *opaque, hwaddr ram_addr, static void notdirty_mem_write(void *opaque, hwaddr ram_addr,
uint64_t val, unsigned size) uint64_t val, unsigned size)
{ {

View File

@ -656,8 +656,13 @@ int memory_region_get_fd(MemoryRegion *mr);
* memory_region_get_ram_ptr: Get a pointer into a RAM memory region. * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
* *
* Returns a host pointer to a RAM memory region (created with * Returns a host pointer to a RAM memory region (created with
* memory_region_init_ram() or memory_region_init_ram_ptr()). Use with * memory_region_init_ram() or memory_region_init_ram_ptr()).
* care. *
* 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. * @mr: the memory region being queried.
*/ */

View File

@ -1577,13 +1577,19 @@ int memory_region_get_fd(MemoryRegion *mr)
void *memory_region_get_ram_ptr(MemoryRegion *mr) void *memory_region_get_ram_ptr(MemoryRegion *mr)
{ {
if (mr->alias) { void *ptr;
return memory_region_get_ram_ptr(mr->alias) + mr->alias_offset; uint64_t offset = 0;
rcu_read_lock();
while (mr->alias) {
offset += mr->alias_offset;
mr = mr->alias;
} }
assert(mr->ram_addr != RAM_ADDR_INVALID); 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) void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, Error **errp)