[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 05/14] tcg: comment on which functions have to be cal
From: |
Paolo Bonzini |
Subject: |
[Qemu-devel] [PULL 05/14] tcg: comment on which functions have to be called with tb_lock held |
Date: |
Mon, 31 Oct 2016 15:13:26 +0100 |
softmmu requires more functions to be thread-safe, because translation
blocks can be invalidated from e.g. notdirty callbacks. Probably the
same holds for user-mode emulation, it's just that no one has ever
tried to produce a coherent locking there.
This patch will guide the introduction of more tb_lock and tb_unlock
calls for system emulation.
Note that after this patch some (most) of the mentioned functions are
still called outside tb_lock/tb_unlock. The next one will rectify this.
Signed-off-by: Paolo Bonzini <address@hidden>
Signed-off-by: Alex Bennée <address@hidden>
Reviewed-by: Richard Henderson <address@hidden>
Message-Id: <address@hidden>
Signed-off-by: Paolo Bonzini <address@hidden>
---
include/exec/exec-all.h | 1 +
include/qom/cpu.h | 3 +++
tcg/tcg.h | 2 ++
translate-all.c | 28 +++++++++++++++++++++++-----
4 files changed, 29 insertions(+), 5 deletions(-)
diff --git a/include/exec/exec-all.h b/include/exec/exec-all.h
index 4d36ee3..a8c13ce 100644
--- a/include/exec/exec-all.h
+++ b/include/exec/exec-all.h
@@ -316,6 +316,7 @@ static inline void tb_set_jmp_target(TranslationBlock *tb,
#endif
+/* Called with tb_lock held. */
static inline void tb_add_jump(TranslationBlock *tb, int n,
TranslationBlock *tb_next)
{
diff --git a/include/qom/cpu.h b/include/qom/cpu.h
index 633c3fc..9f597bb 100644
--- a/include/qom/cpu.h
+++ b/include/qom/cpu.h
@@ -319,7 +319,10 @@ struct CPUState {
MemoryRegion *memory;
void *env_ptr; /* CPUArchState */
+
+ /* Writes protected by tb_lock, reads not thread-safe */
struct TranslationBlock *tb_jmp_cache[TB_JMP_CACHE_SIZE];
+
struct GDBRegisterState *gdb_regs;
int gdb_num_regs;
int gdb_num_g_regs;
diff --git a/tcg/tcg.h b/tcg/tcg.h
index b34b5fb..dc1281f 100644
--- a/tcg/tcg.h
+++ b/tcg/tcg.h
@@ -726,6 +726,7 @@ static inline bool tcg_op_buf_full(void)
/* pool based memory allocation */
+/* tb_lock must be held for tcg_malloc_internal. */
void *tcg_malloc_internal(TCGContext *s, int size);
void tcg_pool_reset(TCGContext *s);
@@ -733,6 +734,7 @@ void tb_lock(void);
void tb_unlock(void);
void tb_lock_reset(void);
+/* Called with tb_lock held. */
static inline void *tcg_malloc(int size)
{
TCGContext *s = &tcg_ctx;
diff --git a/translate-all.c b/translate-all.c
index 5aded3d..fad2646 100644
--- a/translate-all.c
+++ b/translate-all.c
@@ -308,7 +308,9 @@ static int encode_search(TranslationBlock *tb, uint8_t
*block)
return p - block;
}
-/* The cpu state corresponding to 'searched_pc' is restored. */
+/* The cpu state corresponding to 'searched_pc' is restored.
+ * Called with tb_lock held.
+ */
static int cpu_restore_state_from_tb(CPUState *cpu, TranslationBlock *tb,
uintptr_t searched_pc)
{
@@ -462,6 +464,7 @@ static void page_init(void)
}
/* If alloc=1:
+ * Called with tb_lock held for system emulation.
* Called with mmap_lock held for user-mode emulation.
*/
static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc)
@@ -826,8 +829,12 @@ bool tcg_enabled(void)
return tcg_ctx.code_gen_buffer != NULL;
}
-/* Allocate a new translation block. Flush the translation buffer if
- too many translation blocks or too much generated code. */
+/*
+ * Allocate a new translation block. Flush the translation buffer if
+ * too many translation blocks or too much generated code.
+ *
+ * Called with tb_lock held.
+ */
static TranslationBlock *tb_alloc(target_ulong pc)
{
TranslationBlock *tb;
@@ -842,6 +849,7 @@ static TranslationBlock *tb_alloc(target_ulong pc)
return tb;
}
+/* Called with tb_lock held. */
void tb_free(TranslationBlock *tb)
{
/* In practice this is mostly used for single use temporary TB
@@ -966,6 +974,10 @@ do_tb_invalidate_check(struct qht *ht, void *p, uint32_t
hash, void *userp)
}
}
+/* verify that all the pages have correct rights for code
+ *
+ * Called with tb_lock held.
+ */
static void tb_invalidate_check(target_ulong address)
{
address &= TARGET_PAGE_MASK;
@@ -1070,7 +1082,10 @@ static inline void tb_jmp_unlink(TranslationBlock *tb)
}
}
-/* invalidate one TB */
+/* invalidate one TB
+ *
+ * Called with tb_lock held.
+ */
void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
{
CPUState *cpu;
@@ -1504,7 +1519,9 @@ void tb_invalidate_phys_page_fast(tb_page_addr_t start,
int len)
}
if (!p->code_bitmap &&
++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD) {
- /* build code bitmap */
+ /* build code bitmap. FIXME: writes should be protected by
+ * tb_lock, reads by tb_lock or RCU.
+ */
build_page_bitmap(p);
}
if (p->code_bitmap) {
@@ -1645,6 +1662,7 @@ void tb_invalidate_phys_addr(AddressSpace *as, hwaddr
addr)
}
#endif /* !defined(CONFIG_USER_ONLY) */
+/* Called with tb_lock held. */
void tb_check_watchpoint(CPUState *cpu)
{
TranslationBlock *tb;
--
2.7.4
- [Qemu-devel] [PULL 00/14] MTTCG patches for 2016-10-31, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 04/14] cpu-exec: include cpu_index in CPU_LOG_EXEC messages, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 03/14] translate-all: add DEBUG_LOCKING asserts, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 06/14] linux-user/elfload: ensure mmap_lock() held while setting up, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 05/14] tcg: comment on which functions have to be called with tb_lock held,
Paolo Bonzini <=
- [Qemu-devel] [PULL 02/14] translate_all: DEBUG_FLUSH -> DEBUG_TB_FLUSH, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 01/14] cpus: make all_vcpus_paused() return bool, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 08/14] tcg: protect translation related stuff with tb_lock., Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 07/14] translate-all: Add assert_(memory|tb)_lock annotations, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 11/14] tcg: cpus rm tcg_exec_all(), Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 10/14] tcg: move tcg_exec_all and helpers above thread fn, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 12/14] cpus: re-factor out handle_icount_deadline, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 09/14] target-arm/arm-powerctl: wake up sleeping CPUs, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 14/14] tcg: move locking for tb_invalidate_phys_page_range up, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 13/14] *_run_on_cpu: introduce run_on_cpu_data type, Paolo Bonzini, 2016/10/31