From patchwork Thu Oct 27 15:10:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 79727 Delivered-To: patch@linaro.org Received: by 10.140.97.247 with SMTP id m110csp690506qge; Thu, 27 Oct 2016 08:37:32 -0700 (PDT) X-Received: by 10.31.174.147 with SMTP id x141mr6980395vke.73.1477582652835; Thu, 27 Oct 2016 08:37:32 -0700 (PDT) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id e16si3559934uaa.123.2016.10.27.08.37.32 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 27 Oct 2016 08:37:32 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:42349 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bzmkG-0000rC-2K for patch@linaro.org; Thu, 27 Oct 2016 11:37:32 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42713) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bzmKv-0004q1-Kj for qemu-devel@nongnu.org; Thu, 27 Oct 2016 11:11:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bzmKq-0007lG-Sa for qemu-devel@nongnu.org; Thu, 27 Oct 2016 11:11:21 -0400 Received: from mail-wm0-x233.google.com ([2a00:1450:400c:c09::233]:37474) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1bzmKq-0007kc-HR for qemu-devel@nongnu.org; Thu, 27 Oct 2016 11:11:16 -0400 Received: by mail-wm0-x233.google.com with SMTP id 140so29479538wmv.0 for ; Thu, 27 Oct 2016 08:11:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KxD00empIUwAx00uctGBKHSMIcrYXB3tTqVvIAE0mPU=; b=PtZN0E46LHXAw62l7SzLTEp+HYyBNm2q6K34xZFtaG54JARThsjuXgskj7+tVzZ/Cu fVHZGUJ5lVMg6CrLwSPiZw/nlNvj4smKq+dsnxZteWyCQX5ItxNAtg0HzLj5KP8gJmwZ 0kMWWro/RDDcup/Ts11uIiXaB9vjXz3qxZsF8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KxD00empIUwAx00uctGBKHSMIcrYXB3tTqVvIAE0mPU=; b=mXXzQr/JtDaCe59XHFAkdx3CJeXP38IHaZke7Knhbtue1dQrLmIEKvcNUjC30qIT/G +uyt3Tt8Otc0Vwz1i4ZWU2tdyL9YL1oRDG+WadXDfNu9ADVI1r16bUdJYamMNKuqoLrO WEVk2aOyVCwvJ5SM13/3PFbcq6teWWvpv3X89jkvfmOEYM3kijKa9Q93YV/mFpQkz13u lP94XNlFu9KOrzV4vLjec7BXE9TWM/VQYEDNVIBJOIPMULiRfcEr/4mOTuwLY6hANYl5 VEydiYV9rm2jbdXyEbKxMOFOMVql0GTAhCiIv/QKddFdsSxEzwPFRrL00EAEXFfljkUd TB3A== X-Gm-Message-State: ABUngvfCjJm7NxSjOrwnrCp8nYw5uKdhj3HJiR+WmCXBck1HMLARDyED4ysWiCzDjcnngmdD X-Received: by 10.28.178.133 with SMTP id b127mr4130225wmf.83.1477581064595; Thu, 27 Oct 2016 08:11:04 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id a5sm9006541wjv.6.2016.10.27.08.11.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Oct 2016 08:11:01 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 8663E3E02C8; Thu, 27 Oct 2016 16:10:59 +0100 (BST) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: pbonzini@redhat.com Date: Thu, 27 Oct 2016 16:10:03 +0100 Message-Id: <20161027151030.20863-7-alex.bennee@linaro.org> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20161027151030.20863-1-alex.bennee@linaro.org> References: <20161027151030.20863-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::233 Subject: [Qemu-devel] [PATCH v5 06/33] tcg: comment on which functions have to be called with tb_lock held X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mttcg@listserver.greensocs.com, peter.maydell@linaro.org, claudio.fontana@huawei.com, nikunj@linux.vnet.ibm.com, Peter Crosthwaite , jan.kiszka@siemens.com, mark.burton@greensocs.com, a.rigo@virtualopensystems.com, qemu-devel@nongnu.org, cota@braap.org, serge.fdrv@gmail.com, bobby.prani@gmail.com, rth@twiddle.net, =?UTF-8?q?Alex=20Benn=C3=A9e?= , fred.konrad@greensocs.com Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Paolo Bonzini 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 Signed-off-by: Alex Bennée Reviewed-by: Richard Henderson --- v1(ajb): - just s-o-b v2 - clarify write lock on tb_jump_cache v3 - drop RCU comment for debug stuff (separate commit now) v5 - fix merge failure that broke DEBUG_TB_CHECK --- 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(-) -- 2.10.1 diff --git a/include/exec/exec-all.h b/include/exec/exec-all.h index 84fecc8..189deb8 100644 --- a/include/exec/exec-all.h +++ b/include/exec/exec-all.h @@ -317,6 +317,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 6d481a1..44571da 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 758f654..f6da7bd 100644 --- a/translate-all.c +++ b/translate-all.c @@ -289,7 +289,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) { @@ -441,6 +443,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) @@ -805,8 +808,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; @@ -821,6 +828,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 @@ -945,6 +953,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; @@ -1049,7 +1061,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; @@ -1483,7 +1498,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) { @@ -1624,6 +1641,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;