[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 18/21] exec: protect mru_block with RCU
From: |
Paolo Bonzini |
Subject: |
[Qemu-devel] [PULL 18/21] exec: protect mru_block with RCU |
Date: |
Mon, 16 Feb 2015 19:17:59 +0100 |
Hence, freeing a RAMBlock has to be switched to call_rcu.
Reviewed-by: Fam Zheng <address@hidden>
Signed-off-by: Paolo Bonzini <address@hidden>
---
exec.c | 52 +++++++++++++++++++++++++++++++++++---------------
include/exec/cpu-all.h | 2 ++
2 files changed, 39 insertions(+), 15 deletions(-)
diff --git a/exec.c b/exec.c
index 76b3b6c..5a27c42 100644
--- a/exec.c
+++ b/exec.c
@@ -811,7 +811,7 @@ static RAMBlock *qemu_get_ram_block(ram_addr_t addr)
RAMBlock *block;
/* The list is protected by the iothread lock here. */
- block = ram_list.mru_block;
+ block = atomic_rcu_read(&ram_list.mru_block);
if (block && addr - block->offset < block->max_length) {
goto found;
}
@@ -825,6 +825,22 @@ static RAMBlock *qemu_get_ram_block(ram_addr_t addr)
abort();
found:
+ /* It is safe to write mru_block outside the iothread lock. This
+ * is what happens:
+ *
+ * mru_block = xxx
+ * rcu_read_unlock()
+ * xxx removed from list
+ * rcu_read_lock()
+ * read mru_block
+ * mru_block = NULL;
+ * call_rcu(reclaim_ramblock, xxx);
+ * rcu_read_unlock()
+ *
+ * atomic_rcu_set is not needed here. The block was already published
+ * when it was placed into the list. Here we're just making an extra
+ * copy of the pointer.
+ */
ram_list.mru_block = block;
return block;
}
@@ -1526,13 +1542,31 @@ void qemu_ram_free_from_ptr(ram_addr_t addr)
QTAILQ_REMOVE(&ram_list.blocks, block, next);
ram_list.mru_block = NULL;
ram_list.version++;
- g_free(block);
+ g_free_rcu(block, rcu);
break;
}
}
qemu_mutex_unlock_ramlist();
}
+static void reclaim_ramblock(RAMBlock *block)
+{
+ if (block->flags & RAM_PREALLOC) {
+ ;
+ } else if (xen_enabled()) {
+ xen_invalidate_map_cache_entry(block->host);
+#ifndef _WIN32
+ } else if (block->fd >= 0) {
+ munmap(block->host, block->max_length);
+ close(block->fd);
+#endif
+ } else {
+ qemu_anon_ram_free(block->host, block->max_length);
+ }
+ g_free(block);
+}
+
+/* Called with the iothread lock held */
void qemu_ram_free(ram_addr_t addr)
{
RAMBlock *block;
@@ -1544,19 +1578,7 @@ void qemu_ram_free(ram_addr_t addr)
QTAILQ_REMOVE(&ram_list.blocks, block, next);
ram_list.mru_block = NULL;
ram_list.version++;
- if (block->flags & RAM_PREALLOC) {
- ;
- } else if (xen_enabled()) {
- xen_invalidate_map_cache_entry(block->host);
-#ifndef _WIN32
- } else if (block->fd >= 0) {
- munmap(block->host, block->max_length);
- close(block->fd);
-#endif
- } else {
- qemu_anon_ram_free(block->host, block->max_length);
- }
- g_free(block);
+ call_rcu(block, reclaim_ramblock, rcu);
break;
}
}
diff --git a/include/exec/cpu-all.h b/include/exec/cpu-all.h
index 2c48286..b8781d1 100644
--- a/include/exec/cpu-all.h
+++ b/include/exec/cpu-all.h
@@ -24,6 +24,7 @@
#include "exec/memory.h"
#include "qemu/thread.h"
#include "qom/cpu.h"
+#include "qemu/rcu.h"
/* some important defines:
*
@@ -268,6 +269,7 @@ CPUArchState *cpu_copy(CPUArchState *env);
typedef struct RAMBlock RAMBlock;
struct RAMBlock {
+ struct rcu_head rcu;
struct MemoryRegion *mr;
uint8_t *host;
ram_addr_t offset;
--
2.3.0
- [Qemu-devel] [PULL 12/21] docs: clarify memory region lifecycle, (continued)
- [Qemu-devel] [PULL 12/21] docs: clarify memory region lifecycle, Paolo Bonzini, 2015/02/16
- [Qemu-devel] [PULL 14/21] exec: make iotlb RCU-friendly, Paolo Bonzini, 2015/02/16
- [Qemu-devel] [PULL 15/21] exec: RCUify AddressSpaceDispatch, Paolo Bonzini, 2015/02/16
- [Qemu-devel] [PULL 09/21] memory: keep the owner of the AddressSpace alive until do_address_space_destroy, Paolo Bonzini, 2015/02/16
- [Qemu-devel] [PULL 13/21] exec: introduce cpu_reload_memory_map, Paolo Bonzini, 2015/02/16
- [Qemu-devel] [PULL 16/21] rcu: introduce RCU-enabled QLIST, Paolo Bonzini, 2015/02/16
- [Qemu-devel] [PULL 19/21] cosmetic changes preparing for the following patches, Paolo Bonzini, 2015/02/16
- [Qemu-devel] [PULL 20/21] exec: convert ram_list to QLIST, Paolo Bonzini, 2015/02/16
- [Qemu-devel] [PULL 21/21] Convert ram_list to RCU, Paolo Bonzini, 2015/02/16
- [Qemu-devel] [PULL 17/21] rcu: add g_free_rcu, Paolo Bonzini, 2015/02/16
- [Qemu-devel] [PULL 18/21] exec: protect mru_block with RCU,
Paolo Bonzini <=
- Re: [Qemu-devel] [PULL 00/21] SCSI, RCU, KVM changes for 2015-02-16, Peter Maydell, 2015/02/25