From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34580) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bly2r-0002FA-Ci for qemu-devel@nongnu.org; Mon, 19 Sep 2016 08:51:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bly2m-0001HR-AZ for qemu-devel@nongnu.org; Mon, 19 Sep 2016 08:51:37 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51874) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bly2m-0001HH-0U for qemu-devel@nongnu.org; Mon, 19 Sep 2016 08:51:32 -0400 From: Paolo Bonzini Date: Mon, 19 Sep 2016 14:50:58 +0200 Message-Id: <1474289459-15242-16-git-send-email-pbonzini@redhat.com> In-Reply-To: <1474289459-15242-1-git-send-email-pbonzini@redhat.com> References: <1474289459-15242-1-git-send-email-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH 15/16] tcg: Make tb_flush() thread safe List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: sergey.fedorov@linaro.org, serge.fdrv@gmail.com, alex.bennee@linaro.org From: Sergey Fedorov Use async_safe_run_on_cpu() to make tb_flush() thread safe. This is possible now that code generation does not happen in the middle of execution. 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 [AJB: minor re-base fixes] Signed-off-by: Alex Benn=C3=A9e Message-Id: <1470158864-17651-13-git-send-email-alex.bennee@linaro.org> Signed-off-by: Paolo Bonzini --- cpu-exec.c | 12 ++---------- include/exec/tb-context.h | 2 +- include/qom/cpu.h | 2 -- translate-all.c | 38 ++++++++++++++++++++++++++++---------- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/cpu-exec.c b/cpu-exec.c index b240b9f..a8ff2a1 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_icou= nt) { TranslationBlock *tb; - bool old_tb_flushed; =20 /* Should never happen. We only end up here when an existing TB is too long. */ if (max_cycles > CF_COUNT_MASK) max_cycles =3D CF_COUNT_MASK; =20 - old_tb_flushed =3D cpu->tb_flushed; - cpu->tb_flushed =3D false; tb =3D tb_gen_code(cpu, orig_tb->pc, orig_tb->cs_base, orig_tb->flag= s, max_cycles | CF_NOCACHE | (ignore_icount ? CF_IGNORE_ICOUNT : 0)); - tb->orig_tb =3D cpu->tb_flushed ? NULL : orig_tb; - cpu->tb_flushed |=3D old_tb_flushed; + tb->orig_tb =3D orig_tb; /* execute the generated code */ trace_exec_tb_nocache(tb, tb->pc); cpu_tb_exec(cpu, tb); @@ -337,10 +333,7 @@ static inline TranslationBlock *tb_find(CPUState *cp= u, tb_lock(); have_tb_lock =3D true; } - /* Check if translation buffer has been flushed */ - if (cpu->tb_flushed) { - cpu->tb_flushed =3D false; - } else if (!tb->invalid) { + if (!tb->invalid) { tb_add_jump(last_tb, tb_exit, tb); } } @@ -605,7 +598,6 @@ int cpu_exec(CPUState *cpu) break; } =20 - atomic_mb_set(&cpu->tb_flushed, false); /* reset before firs= t TB lookup */ for(;;) { cpu_handle_interrupt(cpu, &last_tb); tb =3D tb_find(cpu, last_tb, tb_exit); diff --git a/include/exec/tb-context.h b/include/exec/tb-context.h index dce95d9..c7f17f2 100644 --- a/include/exec/tb-context.h +++ b/include/exec/tb-context.h @@ -38,7 +38,7 @@ struct TBContext { QemuMutex tb_lock; =20 /* statistics */ - int tb_flush_count; + unsigned tb_flush_count; int tb_phys_invalidate_count; }; =20 diff --git a/include/qom/cpu.h b/include/qom/cpu.h index 4092dd9..5dfe74a 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -253,7 +253,6 @@ struct qemu_work_item; * @crash_occurred: Indicates the OS reported a crash (panic) for this C= PU * @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. @@ -306,7 +305,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 b6663dc..ab657e7 100644 --- a/translate-all.c +++ b/translate-all.c @@ -832,12 +832,19 @@ static void page_flush_tb(void) } =20 /* 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_enabled()) { - return; + unsigned tb_flush_req =3D (unsigned) (uintptr_t) data; + + tb_lock(); + + /* If it's already been done on request of another CPU, + * just retry. + */ + if (atomic_read(&tcg_ctx.tb_ctx.tb_flush_count) !=3D tb_flush_req) { + goto done; } + #if defined(DEBUG_FLUSH) printf("qemu: flush code_size=3D%ld nb_tbs=3D%d avg_tb_size=3D%ld\n"= , (unsigned long)(tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffe= r), @@ -856,7 +863,6 @@ void tb_flush(CPUState *cpu) for (i =3D 0; i < TB_JMP_CACHE_SIZE; ++i) { atomic_set(&cpu->tb_jmp_cache[i], NULL); } - atomic_mb_set(&cpu->tb_flushed, true); } =20 tcg_ctx.tb_ctx.nb_tbs =3D 0; @@ -866,7 +872,19 @@ void tb_flush(CPUState *cpu) tcg_ctx.code_gen_ptr =3D tcg_ctx.code_gen_buffer; /* XXX: flush processor icache at this point if cache flush is expensive */ - tcg_ctx.tb_ctx.tb_flush_count++; + atomic_inc(&tcg_ctx.tb_ctx.tb_flush_count); + +done: + tb_unlock(); +} + +void tb_flush(CPUState *cpu) +{ + if (tcg_enabled()) { + uintptr_t tb_flush_req =3D (uintptr_t) + atomic_read(&tcg_ctx.tb_ctx.tb_flush_count); + async_safe_run_on_cpu(cpu, do_tb_flush, (void *) tb_flush_req); + } } =20 #ifdef DEBUG_TB_CHECK @@ -1173,9 +1191,8 @@ TranslationBlock *tb_gen_code(CPUState *cpu, buffer_overflow: /* flush must be done */ tb_flush(cpu); - /* cannot fail at this point */ - tb =3D tb_alloc(pc); - assert(tb !=3D NULL); + mmap_unlock(); + cpu_loop_exit(cpu); } =20 gen_code_buf =3D tcg_ctx.code_gen_ptr; @@ -1773,7 +1790,8 @@ void dump_exec_info(FILE *f, fprintf_function cpu_f= printf) qht_statistics_destroy(&hst); =20 cpu_fprintf(f, "\nStatistics:\n"); - cpu_fprintf(f, "TB flush count %d\n", tcg_ctx.tb_ctx.tb_flush_c= ount); + cpu_fprintf(f, "TB flush count %d\n", + atomic_read(&tcg_ctx.tb_ctx.tb_flush_count)); cpu_fprintf(f, "TB invalidate count %d\n", tcg_ctx.tb_ctx.tb_phys_invalidate_count); cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count); --=20 2.7.4