From patchwork Fri Jul 15 18:57:26 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: sergey.fedorov@linaro.org X-Patchwork-Id: 9232479 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7F7AE60574 for ; Fri, 15 Jul 2016 19:16:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6F7CB26B4A for ; Fri, 15 Jul 2016 19:16:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 644DB275A2; Fri, 15 Jul 2016 19:16:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 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.wl.linuxfoundation.org (Postfix) with ESMTPS id D0E8026B4A for ; Fri, 15 Jul 2016 19:16:57 +0000 (UTC) Received: from localhost ([::1]:34588 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bO8bY-000388-EG for patchwork-qemu-devel@patchwork.kernel.org; Fri, 15 Jul 2016 15:16:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36811) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bO8Qn-0006wV-6k for qemu-devel@nongnu.org; Fri, 15 Jul 2016 15:05:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bO8Qh-0005Gw-NY for qemu-devel@nongnu.org; Fri, 15 Jul 2016 15:05:48 -0400 Received: from mail-lf0-x22d.google.com ([2a00:1450:4010:c07::22d]:33679) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bO8Qh-0005Gn-B7 for qemu-devel@nongnu.org; Fri, 15 Jul 2016 15:05:43 -0400 Received: by mail-lf0-x22d.google.com with SMTP id b199so94601721lfe.0 for ; Fri, 15 Jul 2016 12:05:43 -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; bh=Utuc2ANkKleQNwFNiPvMiU9bQMeZnAsAdQYc1oMqbjI=; b=HDbWpEwcexB7/4k9c4ziKx4iiwqrAim7h18UwTqp9eC16yG0ltLd61i5XLdOVexBwQ KWIWQS/B+8MBD+cFh+H7Jd/YD/BH1+Agi+4gGl3TxO9daqDMbD4mEloZcTm8PPlqCTHJ bijnPflMxQcq/JV9J5dOVvHxNbe4L+IF5OQKE= 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; bh=Utuc2ANkKleQNwFNiPvMiU9bQMeZnAsAdQYc1oMqbjI=; b=Og50okEx+R7EeLuarGT4kYqhcp3LCFyyAQ9VSrMnfx+ViV2cAEOGPBK4cDhG0SXRqg nFYerGRZACI/2o0tYrBdi4+mqGywU4FxiggT0N02aEo0OUl1essCeTYGFSEHR1ePgvp6 3kgYhy2q+QJcTwpD/LCdQQclstVzA6r1+Jg7Ur/pJxTifk2Nf6pW5v2F8WJE/03E4YrW dC1NBWzrGlKH67qIqPggO9xFPw9K3dpsNKpoxlhbhwq0E/8jf9wSceh0v+8V7QVWu2+0 kJx3hrsMcV98R+1xGDCKJe6IJDmJ3mRwmHlkDzWnoj+A8RTEgiYCb7A08sD47pKxWQvb e7/g== X-Gm-Message-State: ALyK8tIkDOsjgWBxf91ZX+oN/8kdgwYbnjJWUuv/2cWb9uYE53dhvjiLyrX8oh0tOoett+UG X-Received: by 10.25.165.148 with SMTP id o142mr7015414lfe.166.1468609069473; Fri, 15 Jul 2016 11:57:49 -0700 (PDT) Received: from sergey-laptop.Dlink (broadband-46-188-120-37.2com.net. [46.188.120.37]) by smtp.gmail.com with ESMTPSA id 35sm2126381ljb.10.2016.07.15.11.57.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 15 Jul 2016 11:57:48 -0700 (PDT) From: Sergey Fedorov To: qemu-devel@nongnu.org Date: Fri, 15 Jul 2016 21:57:26 +0300 Message-Id: <20160715185726.10181-13-sergey.fedorov@linaro.org> X-Mailer: git-send-email 2.9.1 In-Reply-To: <20160715185726.10181-1-sergey.fedorov@linaro.org> References: <20160715185726.10181-1-sergey.fedorov@linaro.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:4010:c07::22d Subject: [Qemu-devel] [PATCH v4 12/12] tcg: Make tb_flush() thread safe 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 Devel , Peter Maydell , Sergey Fedorov , patches@linaro.org, Peter Crosthwaite , Alvise Rigo , "Emilio G. Cota" , Paolo Bonzini , Sergey Fedorov , Richard Henderson , =?UTF-8?Q?Alex_Benn=c3=a9e?= , =?UTF-8?B?S09OUkFEIEZyw6lkw6lyaWM=?= Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Sergey Fedorov Use async_safe_run_on_cpu() to make tb_flush() thread safe. It can happen that multiple threads schedule a safe work to flush the translation buffer. To keep statistics and debugging output sane, always check if the translation buffer has already been flushed. Signed-off-by: Sergey Fedorov Signed-off-by: Sergey Fedorov --- Changes in v4: - check if flush has already been done by raced CPU work Changes in v3: - 'tb_flushed' removed Changes in v2: - stale comment about unsafe tb_flush() removed --- cpu-exec.c | 14 +------------- include/qom/cpu.h | 2 -- translate-all.c | 17 +++++++++++------ 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/cpu-exec.c b/cpu-exec.c index b840e1d2dd41..0b7614ffcc9b 100644 --- a/cpu-exec.c +++ b/cpu-exec.c @@ -203,20 +203,16 @@ static void cpu_exec_nocache(CPUState *cpu, int max_cycles, TranslationBlock *orig_tb, bool ignore_icount) { TranslationBlock *tb; - bool old_tb_flushed; /* Should never happen. We only end up here when an existing TB is too long. */ if (max_cycles > CF_COUNT_MASK) max_cycles = CF_COUNT_MASK; - old_tb_flushed = cpu->tb_flushed; - cpu->tb_flushed = false; tb = tb_gen_code(cpu, orig_tb->pc, orig_tb->cs_base, orig_tb->flags, max_cycles | CF_NOCACHE | (ignore_icount ? CF_IGNORE_ICOUNT : 0)); - tb->orig_tb = cpu->tb_flushed ? NULL : orig_tb; - cpu->tb_flushed |= old_tb_flushed; + tb->orig_tb = orig_tb; /* execute the generated code */ trace_exec_tb_nocache(tb, tb->pc); cpu_tb_exec(cpu, tb); @@ -338,13 +334,6 @@ static inline TranslationBlock *tb_find_fast(CPUState *cpu, tb->flags != flags)) { tb = tb_find_slow(cpu, pc, cs_base, flags); } - if (cpu->tb_flushed) { - /* Ensure that no TB jump will be modified as the - * translation buffer has been flushed. - */ - *last_tb = NULL; - cpu->tb_flushed = false; - } #ifndef CONFIG_USER_ONLY /* We don't take care of direct jumps when address mapping changes in * system emulation. So it's not safe to make a direct jump to a TB @@ -619,7 +608,6 @@ int cpu_exec(CPUState *cpu) } last_tb = NULL; /* forget the last executed TB after exception */ - cpu->tb_flushed = false; /* reset before first TB lookup */ for(;;) { cpu_handle_interrupt(cpu, &last_tb); tb = tb_find_fast(cpu, &last_tb, tb_exit); diff --git a/include/qom/cpu.h b/include/qom/cpu.h index ab67bf2ba19f..9af4420d2bb5 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -258,7 +258,6 @@ struct qemu_work_item { * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this * CPU and return to its top level loop. - * @tb_flushed: Indicates the translation buffer has been flushed. * @singlestep_enabled: Flags for single-stepping. * @icount_extra: Instructions until next timer event. * @icount_decr: Number of cycles left, with interrupt flag in high bit. @@ -310,7 +309,6 @@ struct CPUState { bool unplug; bool crash_occurred; bool exit_request; - bool tb_flushed; uint32_t interrupt_request; int singlestep_enabled; int64_t icount_extra; diff --git a/translate-all.c b/translate-all.c index 0d47c1c0cf82..030273ee7b13 100644 --- a/translate-all.c +++ b/translate-all.c @@ -831,9 +831,11 @@ static void page_flush_tb(void) } /* flush all the translation blocks */ -/* XXX: tb_flush is currently not thread safe */ -void tb_flush(CPUState *cpu) +static void do_tb_flush(CPUState *cpu, void *data) { + if (tcg_ctx.tb_ctx.nb_tbs == 0) { + return; + } #if defined(DEBUG_FLUSH) printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n", (unsigned long)(tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer), @@ -849,7 +851,6 @@ void tb_flush(CPUState *cpu) CPU_FOREACH(cpu) { memset(cpu->tb_jmp_cache, 0, sizeof(cpu->tb_jmp_cache)); - cpu->tb_flushed = true; } qht_reset_size(&tcg_ctx.tb_ctx.htable, CODE_GEN_HTABLE_SIZE); @@ -861,6 +862,11 @@ void tb_flush(CPUState *cpu) tcg_ctx.tb_ctx.tb_flush_count++; } +void tb_flush(CPUState *cpu) +{ + async_safe_run_on_cpu(cpu, do_tb_flush, NULL); +} + #ifdef DEBUG_TB_CHECK static void @@ -1163,9 +1169,8 @@ TranslationBlock *tb_gen_code(CPUState *cpu, buffer_overflow: /* flush must be done */ tb_flush(cpu); - /* cannot fail at this point */ - tb = tb_alloc(pc); - assert(tb != NULL); + mmap_unlock(); + cpu_loop_exit(cpu); } gen_code_buf = tcg_ctx.code_gen_ptr;