From patchwork Tue Apr 26 10:41:18 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: 8937591 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 D0215BF29F for ; Tue, 26 Apr 2016 10:42:16 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C66A120123 for ; Tue, 26 Apr 2016 10:42:15 +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 C338120114 for ; Tue, 26 Apr 2016 10:42:13 +0000 (UTC) Received: from localhost ([::1]:37345 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1av0RZ-0007fY-1Z for patchwork-qemu-devel@patchwork.kernel.org; Tue, 26 Apr 2016 06:42:13 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53904) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1av0RE-0007VE-1o for qemu-devel@nongnu.org; Tue, 26 Apr 2016 06:41:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1av0R9-0006JW-VP for qemu-devel@nongnu.org; Tue, 26 Apr 2016 06:41:51 -0400 Received: from mail-lf0-x229.google.com ([2a00:1450:4010:c07::229]:35206) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1av0R9-0006Hv-II for qemu-devel@nongnu.org; Tue, 26 Apr 2016 06:41:47 -0400 Received: by mail-lf0-x229.google.com with SMTP id c126so12667004lfb.2 for ; Tue, 26 Apr 2016 03:41:47 -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=uzasYAQCevpHmpcz4azWB0fUtw3/mJGV70o4VuUf8T0=; b=QujZHBHAxzj7n4f6iVwyYHgHFUYvPdcD7mMzbx76sIc+TirbKfmBVAAsqMAk+mhKWQ pPHSitCWplsZ/tNPglGQ5UwUCPaaJ4jXH20k43BtbMnT7v7h9u/J6uQaYzS+te/4shz9 mnSHVscUVDKyP6T/KhsfBReIdlBARQdQDubPo= 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=uzasYAQCevpHmpcz4azWB0fUtw3/mJGV70o4VuUf8T0=; b=ZiqXjo/E3c4NoDnJaoH4MIFUjjWvuxdnAit0cd0QPcedMrXCp4FuMstr1+3fPCca1b J6kcsj4iBPKzI7RpLgdUxP1Z9cIk3AUrG5tqt/1C45qAoPRTB+OIX1fO0LiQrFh+QIHi 3VfKZ0I+euzoUUsE6Rc3zt0RRaBteIc6FTzSbAWCDckWvv6maLEEZcqzKyjJ9dnPUpL4 CWXS1qVOtv7irA3Ogc0+ckHeETxaa+JUiWBqr49JXKf/GZA9C/B1HDjzeI78n0VXSeEK 1uMWrwomaiFQW/sqr+N5bDDvP6QPuNq8YrSayrUwP66OmPqk3ggUHHTLa1JfgSNq5G6Q 2U/w== X-Gm-Message-State: AOPr4FW0gFuz/a0jUVA8HQ1JubWpUWAnhhfYIEkz24D65piZQfNIhzwAfWd4cSCxvKbY7AQP X-Received: by 10.112.149.3 with SMTP id tw3mr838786lbb.129.1461667306817; Tue, 26 Apr 2016 03:41:46 -0700 (PDT) Received: from localhost.localdomain ([195.91.132.170]) by smtp.gmail.com with ESMTPSA id l124sm5325654lfg.40.2016.04.26.03.41.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 26 Apr 2016 03:41:46 -0700 (PDT) From: Sergey Fedorov To: qemu-devel@nongnu.org Date: Tue, 26 Apr 2016 13:41:18 +0300 Message-Id: <1461667279-5229-6-git-send-email-sergey.fedorov@linaro.org> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1461667279-5229-1-git-send-email-sergey.fedorov@linaro.org> References: <1461667279-5229-1-git-send-email-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::229 Subject: [Qemu-devel] [PATCH v5 5/6] tcg: Rework tb_invalidated_flag 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: Sergey Fedorov , Peter Crosthwaite , Paolo Bonzini , Sergey Fedorov , =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Andreas=20F=C3=A4rber?= , Richard Henderson Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" 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: Sergey Fedorov 'tb_invalidated_flag' was meant to catch two events: * some TB has been invalidated by tb_phys_invalidate(); * the whole translation buffer has been flushed by tb_flush(). Then it was checked: * in cpu_exec() to ensure that the last executed TB can be safely linked to directly call the next one; * in cpu_exec_nocache() to decide if the original TB should be provided for further possible invalidation along with the temporarily generated TB. It is always safe to patch an invalidated TB since it is not going to be used anyway. It is also safe to call tb_phys_invalidate() for an already invalidated TB. Thus, setting this flag in tb_phys_invalidate() is simply unnecessary. Moreover, it can prevent from pretty proper linking of TBs, if any arbitrary TB has been invalidated. So just don't touch it in tb_phys_invalidate(). If this flag is only used to catch whether tb_flush() has been called then rename it to 'tb_flushed'. Declare it as 'bool' and stick to using only 'true' and 'false' to set its value. Also, instead of setting it in tb_gen_code(), just after tb_flush() has been called, do it right inside of tb_flush(). In cpu_exec(), this flag is used to track if tb_flush() has been called and have made 'next_tb' (a reference to the last executed TB) invalid for linking it to directly call the next TB. tb_flush() can be called during the CPU execution loop from tb_gen_code(), during TB execution or by another thread while 'tb_lock' is released. Catch for translation buffer flush reliably by resetting this flag once before first TB lookup and each time we find it set before trying to add a direct jump. Don't touch in in tb_find_physical(). Each vCPU has its own execution loop in multithreaded mode and thus should have its own copy of the flag to be able to reset it with its own 'next_tb' and don't affect any other vCPU execution thread. So make this flag per-vCPU and move it to CPUState. In cpu_exec_nocache(), we only need to check if tb_flush() has been called from tb_gen_code() called by cpu_exec_nocache() itself. To do this reliably, preserve the old value of the flag, reset it before calling tb_gen_code(), check afterwards, and combine the saved value back to the flag. This patch is based on the patch "tcg: move tb_invalidated_flag to CPUState" from Paolo Bonzini . Signed-off-by: Sergey Fedorov Signed-off-by: Sergey Fedorov --- Changes in v4: * Rebased on top of the previous patch cpu-exec.c | 21 +++++++++++---------- include/exec/exec-all.h | 2 -- include/qom/cpu.h | 2 ++ translate-all.c | 5 +---- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/cpu-exec.c b/cpu-exec.c index 6ad5fbdf1f8e..8ba899b47a23 100644 --- a/cpu-exec.c +++ b/cpu-exec.c @@ -202,16 +202,20 @@ 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 = tcg_ctx.tb_ctx.tb_invalidated_flag ? NULL : orig_tb; + tb->orig_tb = cpu->tb_flushed ? NULL : orig_tb; + cpu->tb_flushed |= old_tb_flushed; cpu->current_tb = tb; /* execute the generated code */ trace_exec_tb_nocache(tb, tb->pc); @@ -232,8 +236,6 @@ static TranslationBlock *tb_find_physical(CPUState *cpu, unsigned int h; tb_page_addr_t phys_pc, phys_page1; - tcg_ctx.tb_ctx.tb_invalidated_flag = 0; - /* find translated block using physical mappings */ phys_pc = get_page_addr_code(env, pc); phys_page1 = phys_pc & TARGET_PAGE_MASK; @@ -446,6 +448,7 @@ 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(;;) { interrupt_request = cpu->interrupt_request; if (unlikely(interrupt_request)) { @@ -510,14 +513,12 @@ int cpu_exec(CPUState *cpu) } tb_lock(); tb = tb_find_fast(cpu); - /* Note: we do it here to avoid a gcc bug on Mac OS X when - doing it in tb_find_slow */ - if (tcg_ctx.tb_ctx.tb_invalidated_flag) { - /* as some TB could have been invalidated because - of memory exceptions while generating the code, we - must recompute the hash index here */ + if (cpu->tb_flushed) { + /* Ensure that no TB jump will be modified as the + * translation buffer has been flushed. + */ last_tb = NULL; - tcg_ctx.tb_ctx.tb_invalidated_flag = 0; + cpu->tb_flushed = false; } /* see if we can patch the calling TB. When the TB spans two pages, we cannot safely do a direct diff --git a/include/exec/exec-all.h b/include/exec/exec-all.h index 736209505a68..0ba845e12b12 100644 --- a/include/exec/exec-all.h +++ b/include/exec/exec-all.h @@ -288,8 +288,6 @@ struct TBContext { /* statistics */ int tb_flush_count; int tb_phys_invalidate_count; - - int tb_invalidated_flag; }; void tb_free(TranslationBlock *tb); diff --git a/include/qom/cpu.h b/include/qom/cpu.h index b7a10f791acc..c1ae24d1fcbb 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -238,6 +238,7 @@ struct kvm_run; * @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. @@ -289,6 +290,7 @@ struct CPUState { bool stopped; 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 0d5d9449dc6b..acce9396581e 100644 --- a/translate-all.c +++ b/translate-all.c @@ -844,6 +844,7 @@ void tb_flush(CPUState *cpu) CPU_FOREACH(cpu) { memset(cpu->tb_jmp_cache, 0, sizeof(cpu->tb_jmp_cache)); + cpu->tb_flushed = true; } memset(tcg_ctx.tb_ctx.tb_phys_hash, 0, sizeof(tcg_ctx.tb_ctx.tb_phys_hash)); @@ -990,8 +991,6 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) invalidate_page_bitmap(p); } - tcg_ctx.tb_ctx.tb_invalidated_flag = 1; - /* remove the TB from the hash list */ h = tb_jmp_cache_hash_func(tb->pc); CPU_FOREACH(cpu) { @@ -1081,8 +1080,6 @@ TranslationBlock *tb_gen_code(CPUState *cpu, /* cannot fail at this point */ tb = tb_alloc(pc); assert(tb != NULL); - /* Don't forget to invalidate previous TB info. */ - tcg_ctx.tb_ctx.tb_invalidated_flag = 1; } gen_code_buf = tcg_ctx.code_gen_ptr;