From patchwork Tue Apr 5 15:32:17 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: 606548 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3qfXzc6mTSz9t5Y for ; Wed, 6 Apr 2016 01:37:32 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b=IgHia0NZ; dkim-atps=neutral Received: from localhost ([::1]:37837 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1anT2p-0006vQ-5T for incoming@patchwork.ozlabs.org; Tue, 05 Apr 2016 11:37:31 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54864) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1anSyB-0006qv-IW for qemu-devel@nongnu.org; Tue, 05 Apr 2016 11:32:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1anSy5-0007Dn-FA for qemu-devel@nongnu.org; Tue, 05 Apr 2016 11:32:43 -0400 Received: from mail-wm0-x22b.google.com ([2a00:1450:400c:c09::22b]:38153) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1anSy5-0007Dc-4w for qemu-devel@nongnu.org; Tue, 05 Apr 2016 11:32:37 -0400 Received: by mail-wm0-x22b.google.com with SMTP id u206so9435546wme.1 for ; Tue, 05 Apr 2016 08:32:37 -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=i61hsRjVJn1Aaxdza0nNwchsiTb9vKpQgMz6/TsM0lU=; b=IgHia0NZKojgQ1JEoaC3cgMOcC1taWLT8Pg5VWkaPX2MgvmKul0ZjHXQtcGGQbscSl p9+WVzhquFHWjUkoKPqbBHcihwWPzVTZ26/pu+gJmnznghcZ0eJQ8VIV0mQgMiNL8Y4F Bfl01OBPjJiOyan7611VH0hsSV9p3VC80wWPM= 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=i61hsRjVJn1Aaxdza0nNwchsiTb9vKpQgMz6/TsM0lU=; b=OewNepudyAmMI1nb51jL0+dtFgitFGKtlGzQkMlHRjd5fMetWIrIHuBA05idw0IXHc U5GFqHYCgLHIL+cUTwjG+JrMyOi0cmv+1hv+E6CCh4VQ4bbZ8os+eyv/wSSUWVJynHi4 wktAtb77VBizeFuu0oEVhUdE5JloraFnZn/NBi6Ppc3iLmWt2xRGMIBQXbchpwaHu6xR o31oXfpVt6eQWSQn7G2RRUJulS1lHzSbV80fh5W4q/oxYIYueENDdJBGWrIqIF0pFtaB 2njEJrz+49j3oxUPJYKliuFHOPE8aufwMteQVGyB3P36fQXhg7B0X5JNkFV1VEL8akUk BogQ== X-Gm-Message-State: AD7BkJJLBnjl7a2TOjqWjXNBHB6l5PDt+U0Yk+IXk/JXM2wTGYupZodOya9z3l7zQnx7AIEx X-Received: by 10.194.9.201 with SMTP id c9mr15179131wjb.7.1459870356323; Tue, 05 Apr 2016 08:32:36 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id ks5sm35342323wjb.13.2016.04.05.08.32.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Apr 2016 08:32:32 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 2D0B53E04E9; Tue, 5 Apr 2016 16:32:33 +0100 (BST) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: mttcg@listserver.greensocs.com, fred.konrad@greensocs.com, a.rigo@virtualopensystems.com, serge.fdrv@gmail.com, cota@braap.org Date: Tue, 5 Apr 2016 16:32:17 +0100 Message-Id: <1459870344-16773-5-git-send-email-alex.bennee@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1459870344-16773-1-git-send-email-alex.bennee@linaro.org> References: <1459870344-16773-1-git-send-email-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::22b Cc: peter.maydell@linaro.org, claudio.fontana@huawei.com, Peter Crosthwaite , jan.kiszka@siemens.com, mark.burton@greensocs.com, qemu-devel@nongnu.org, pbonzini@redhat.com, =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Andreas=20F=C3=A4rber?= , rth@twiddle.net Subject: [Qemu-devel] [RFC v2 04/11] tcg: comment on which functions have to be called with tb_lock held 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 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 --- v1(ajb): - just s-o-b v2 - clarify write lock on tb_jump_cache --- exec.c | 1 + include/exec/exec-all.h | 1 + include/qom/cpu.h | 3 +++ tcg/tcg.h | 2 ++ translate-all.c | 32 ++++++++++++++++++++++++++------ 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/exec.c b/exec.c index f46e596..17f390e 100644 --- a/exec.c +++ b/exec.c @@ -826,6 +826,7 @@ int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags, { CPUBreakpoint *bp; + /* TODO: locking (RCU?) */ bp = g_malloc(sizeof(*bp)); bp->pc = pc; diff --git a/include/exec/exec-all.h b/include/exec/exec-all.h index bbd9807..60716ae 100644 --- a/include/exec/exec-all.h +++ b/include/exec/exec-all.h @@ -386,6 +386,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 6931db9..13eeaae 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -306,7 +306,10 @@ struct CPUState { void *env_ptr; /* CPUArchState */ struct TranslationBlock *current_tb; + + /* 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 ea4ff02..a46d17c 100644 --- a/tcg/tcg.h +++ b/tcg/tcg.h @@ -609,6 +609,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); void tcg_pool_delete(TCGContext *s); @@ -617,6 +618,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 d923008..a7ff5e7 100644 --- a/translate-all.c +++ b/translate-all.c @@ -248,7 +248,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) { @@ -306,8 +308,10 @@ bool cpu_restore_state(CPUState *cpu, uintptr_t retaddr) if (tb->cflags & CF_NOCACHE) { /* one-shot translation, invalidate it immediately */ cpu->current_tb = NULL; + tb_lock(); tb_phys_invalidate(tb, -1); tb_free(tb); + tb_unlock(); } return true; } @@ -399,6 +403,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) @@ -754,8 +759,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; @@ -769,6 +778,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 @@ -874,7 +884,10 @@ static void tb_invalidate_check(target_ulong address) } } -/* verify that all the pages have correct rights for code */ +/* verify that all the pages have correct rights for code + * + * Called with tb_lock held. + */ static void tb_page_check(void) { TranslationBlock *tb; @@ -985,7 +998,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; @@ -1062,6 +1078,7 @@ static void build_page_bitmap(PageDesc *p) /* add the tb in the target page and protect it if necessary * + * Called with tb_lock held. * Called with mmap_lock held for user-mode emulation. */ static inline void tb_alloc_page(TranslationBlock *tb, @@ -1429,7 +1446,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) { @@ -1571,6 +1590,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;