From patchwork Fri Mar 18 16:18:44 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: 8621711 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 7D035C0553 for ; Fri, 18 Mar 2016 16:21:23 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 83B412037F for ; Fri, 18 Mar 2016 16:21:22 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 861CC2026F for ; Fri, 18 Mar 2016 16:21:21 +0000 (UTC) Received: from localhost ([::1]:44797 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1agx9M-0004NA-RE for patchwork-qemu-devel@patchwork.kernel.org; Fri, 18 Mar 2016 12:21:20 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56864) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1agx79-0000R9-J2 for qemu-devel@nongnu.org; Fri, 18 Mar 2016 12:19:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1agx76-0002vr-Gi for qemu-devel@nongnu.org; Fri, 18 Mar 2016 12:19:03 -0400 Received: from mail-wm0-x229.google.com ([2a00:1450:400c:c09::229]:37472) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1agx76-0002vl-6F for qemu-devel@nongnu.org; Fri, 18 Mar 2016 12:19:00 -0400 Received: by mail-wm0-x229.google.com with SMTP id p65so44377441wmp.0 for ; Fri, 18 Mar 2016 09:19:00 -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=Xm96Ad8iXl90HVcQ5TFUHGXfxTnzU1pJxVAQPF+Mzpw=; b=O9m/sygZ7eAj19SkG22en8NiFYVfc44vH8lg4bgwnUM7GyrcZB1z3M2fWtFH/pshJL nP7j7K0pzLFIadjNXtfhYRBlGQCKhxxcqxzfmcLGcycW6gwh4MI4RdJ4Pf2qSqwis4rl Iwqf6wkcmSiq0b1Z75KuuN505JkPKbqR5MxjM= 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=Xm96Ad8iXl90HVcQ5TFUHGXfxTnzU1pJxVAQPF+Mzpw=; b=SPX3K9laFjW91sPA9pKAGvFKQPiXUNDJ35PGB02hcMQ7y7nCHjMUHaBQaRkyy98+ab y3WqbvX9u5FVWjTbg0OfL0y99NwVBzKW5JLOPE/0YaC3v80kGN8wd/Zk6XF9Oia5nhP9 3ZcLiXGb0RIzP1qrCcBs/Qb6/go5uajoDAVUhgWuRax/7L0lyIfwhXsMmmR6ihHfIslS fs3V1Heaf9oTrEeV1PoibU9FnMDRRm7jTywAFdSvKNTZJIaorlyMOk5hcg0dlewDELaJ jeqnH8JBhNbtlrhaSRrD8QtU/QrbSBxuDdHEMyja9pYcJX9uFxXF99jljxw5uSnR1FLD 8qTg== X-Gm-Message-State: AD7BkJI/aZyuGs9LHK/bPmkOKOJtMmvfAzeofkXTO6l3JJskyB25ZXPvbFyA29roKaiOsSHH X-Received: by 10.28.9.71 with SMTP id 68mr213087wmj.33.1458317939705; Fri, 18 Mar 2016 09:18:59 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id ys9sm12814117wjc.35.2016.03.18.09.18.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 18 Mar 2016 09:18:56 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id E04993E0432; Fri, 18 Mar 2016 16:18:55 +0000 (GMT) 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: Fri, 18 Mar 2016 16:18:44 +0000 Message-Id: <1458317932-1875-4-git-send-email-alex.bennee@linaro.org> X-Mailer: git-send-email 2.7.3 In-Reply-To: <1458317932-1875-1-git-send-email-alex.bennee@linaro.org> References: <1458317932-1875-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::229 Cc: Peter Crosthwaite , 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?= , Richard Henderson Subject: [Qemu-devel] [RFC v1 03/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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- exec.c | 1 + include/exec/exec-all.h | 1 + include/qom/cpu.h | 3 +++ tcg/tcg.h | 2 ++ translate-all.c | 38 ++++++++++++++++++++++++++++++-------- 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/exec.c b/exec.c index f09dd4e..4f0e5ed 100644 --- a/exec.c +++ b/exec.c @@ -825,6 +825,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 0ef6ea5..c41d680 100644 --- a/include/exec/exec-all.h +++ b/include/exec/exec-all.h @@ -372,6 +372,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 4132108..9124b6d 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -310,7 +310,10 @@ struct CPUState { void *env_ptr; /* CPUArchState */ struct TranslationBlock *current_tb; + + /* Protected by tb_lock. */ 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 aa4e123..cd7cde7 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); @@ -618,6 +619,7 @@ void tb_unlock(void); bool tb_lock_recursive(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 f68dcbc..1a02450 100644 --- a/translate-all.c +++ b/translate-all.c @@ -260,7 +260,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) { @@ -318,8 +320,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; } @@ -411,6 +415,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) @@ -766,8 +771,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; @@ -781,6 +790,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 @@ -888,7 +898,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; @@ -976,7 +989,10 @@ static inline void tb_jmp_remove(TranslationBlock *tb, int n) } } -/* invalidate one TB */ +/* invalidate one TB + * + * Called with tb_lock held. + */ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) { PageDesc *p; @@ -1067,7 +1083,7 @@ static void build_page_bitmap(PageDesc *p) } #endif -/* Called with mmap_lock held for user mode emulation. */ +/* Called with tb_lock held, and mmap_lock too for user mode emulation. */ TranslationBlock *tb_gen_code(CPUState *cpu, target_ulong pc, target_ulong cs_base, int flags, int cflags) @@ -1333,7 +1349,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) { @@ -1423,6 +1441,7 @@ static void tb_invalidate_phys_page(tb_page_addr_t addr, /* 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, @@ -1482,6 +1501,7 @@ static inline void tb_alloc_page(TranslationBlock *tb, /* add a new TB and link it to the physical page tables. phys_page2 is * (-1) to indicate that only one page contains the TB. * + * Called with tb_lock held. * Called with mmap_lock held for user-mode emulation. */ static void tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc, @@ -1528,7 +1548,8 @@ static void tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc, } /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr < - tb[1].tc_ptr. Return NULL if not found */ + * tb[1].tc_ptr. Return NULL if not found + */ static TranslationBlock *tb_find_pc(uintptr_t tc_ptr) { int m_min, m_max, m; @@ -1581,6 +1602,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;