From nobody Fri Oct 24 20:22:33 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519710670727741.4219212536436; Mon, 26 Feb 2018 21:51:10 -0800 (PST) Received: from localhost ([::1]:34979 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqYAP-0004zU-Gj for importer@patchew.org; Tue, 27 Feb 2018 00:51:09 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50989) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eqXzH-0004bR-QL for qemu-devel@nongnu.org; Tue, 27 Feb 2018 00:39:45 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eqXzD-0000RY-FH for qemu-devel@nongnu.org; Tue, 27 Feb 2018 00:39:39 -0500 Received: from out2-smtp.messagingengine.com ([66.111.4.26]:54061) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eqXzD-0000Qn-9w for qemu-devel@nongnu.org; Tue, 27 Feb 2018 00:39:35 -0500 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id C513420C4A; Tue, 27 Feb 2018 00:39:34 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute4.internal (MEProxy); Tue, 27 Feb 2018 00:39:34 -0500 Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id 8547E24575; Tue, 27 Feb 2018 00:39:34 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=braap.org; h=cc :date:from:in-reply-to:message-id:references:subject:to :x-me-sender:x-me-sender:x-sasl-enc; s=mesmtp; bh=ALovd3J5J4xWjJ 3aIBiCQw5frSUAmRrkF/RRHP1fVgA=; b=2UeVG90v39Wk1vUB7hoBBjQ+agFAjp KxBJPwrsqqOt8nIwM5/Rz47nRxQUhwHWhJxLG4ftEyieiuPGrppujkMFZbusMcZD BQOMogYOIPma1yrDYFamchizWN+ISf/Yfqra18eiPCKwB6Iq7JOm7bgZcqsqRGcj rhH0DtfuE5EQo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=ALovd3J5J4xWjJ3aIBiCQw5frSUAmRrkF/RRHP1fVgA=; b=Ij5rLNxg 36J3nu08S/ObUaxh8g3uD5cNvA2/E2oKKWJYkMg76bBdu79TecN/wt3gJwE1vWv9 X2X6TzQsnOXZ2xKE5LmzucJqiW6k1NZnL1Q7mjRg/VTBDHGpP9HnrIhiSphS/U1A l6UimmBwvqUEIqzHr8Ul8Iqrx1ePP3uguWGgiAaHFNUaFV3CwVJTJ9PsfJfbi2sP YdZ5O/XnHM12oJ8PJR4Y1cti7mdSw8HVTcKexFeYb+6V8XuBZoqOSJi/hQXwiQb0 fw0xKidcyAfvDtth2gvx689qfhv2lm1wBwu3phuSrl3ftfhh2ZZBjSjZGk2c2GKk XYr+THoYwNnx7w== X-ME-Sender: From: "Emilio G. Cota" To: qemu-devel@nongnu.org Date: Tue, 27 Feb 2018 00:39:21 -0500 Message-Id: <1519709965-29833-13-git-send-email-cota@braap.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1519709965-29833-1-git-send-email-cota@braap.org> References: <1519709965-29833-1-git-send-email-cota@braap.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.111.4.26 Subject: [Qemu-devel] [PATCH 12/16] translate-all: discard TB when tb_link_page returns an existing matching TB 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: Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Use the recently-gained QHT feature of returning the matching TB if it already exists. This allows us to get rid of the lookup we perform right after acquiring tb_lock. Suggested-by: Richard Henderson Signed-off-by: Emilio G. Cota --- accel/tcg/cpu-exec.c | 14 ++------------ accel/tcg/translate-all.c | 47 ++++++++++++++++++++++++++++++++++++++-----= ---- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/accel/tcg/cpu-exec.c b/accel/tcg/cpu-exec.c index 7c83887..8aed38c 100644 --- a/accel/tcg/cpu-exec.c +++ b/accel/tcg/cpu-exec.c @@ -243,10 +243,7 @@ void cpu_exec_step_atomic(CPUState *cpu) if (tb =3D=3D NULL) { mmap_lock(); tb_lock(); - tb =3D tb_htable_lookup(cpu, pc, cs_base, flags, cf_mask); - if (likely(tb =3D=3D NULL)) { - tb =3D tb_gen_code(cpu, pc, cs_base, flags, cflags); - } + tb =3D tb_gen_code(cpu, pc, cs_base, flags, cflags); tb_unlock(); mmap_unlock(); } @@ -396,14 +393,7 @@ static inline TranslationBlock *tb_find(CPUState *cpu, tb_lock(); acquired_tb_lock =3D true; =20 - /* There's a chance that our desired tb has been translated while - * taking the locks so we check again inside the lock. - */ - tb =3D tb_htable_lookup(cpu, pc, cs_base, flags, cf_mask); - if (likely(tb =3D=3D NULL)) { - /* if no translated code available, then translate it now */ - tb =3D tb_gen_code(cpu, pc, cs_base, flags, cf_mask); - } + tb =3D tb_gen_code(cpu, pc, cs_base, flags, cf_mask); =20 mmap_unlock(); /* We add the TB in the virtual pc hash table for the fast lookup = */ diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index 82832ef..dbe6c12 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -1503,12 +1503,16 @@ static inline void tb_page_add(PageDesc *p, Transla= tionBlock *tb, * (-1) to indicate that only one page contains the TB. * * Called with mmap_lock held for user-mode emulation. + * + * Returns @tb or an existing TB that matches @tb. */ -static void tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc, - tb_page_addr_t phys_page2) +static TranslationBlock * +tb_link_page(TranslationBlock *tb, tb_page_addr_t phys_pc, + tb_page_addr_t phys_page2) { PageDesc *p; PageDesc *p2 =3D NULL; + void *existing_tb; uint32_t h; =20 assert_memory_lock(); @@ -1516,6 +1520,11 @@ static void tb_link_page(TranslationBlock *tb, tb_pa= ge_addr_t phys_pc, /* * Add the TB to the page list. * To avoid deadlock, acquire first the lock of the lower-addressed pa= ge. + * We keep the locks held until after inserting the TB in the hash tab= le, + * so that if the insertion fails we know for sure that the TBs are st= ill + * in the page descriptors. + * Note that inserting into the hash table first isn't an option, since + * we can only insert TBs that are fully initialized. */ p =3D page_find_alloc(phys_pc >> TARGET_PAGE_BITS, 1); if (likely(phys_page2 =3D=3D -1)) { @@ -1535,21 +1544,33 @@ static void tb_link_page(TranslationBlock *tb, tb_p= age_addr_t phys_pc, tb_page_add(p2, tb, 1, phys_page2); } =20 + /* add in the hash table */ + h =3D tb_hash_func(phys_pc, tb->pc, tb->flags, tb->cflags & CF_HASH_MA= SK, + tb->trace_vcpu_dstate); + existing_tb =3D qht_insert(&tb_ctx.htable, tb, h); + + /* remove TB from the page(s) if we couldn't insert it */ + if (unlikely(existing_tb)) { + tb_page_remove(p, tb); + invalidate_page_bitmap(p); + if (p2) { + tb_page_remove(p2, tb); + invalidate_page_bitmap(p2); + } + tb =3D existing_tb; + } + if (p2) { page_unlock(p2); } page_unlock(p); =20 - /* add in the hash table */ - h =3D tb_hash_func(phys_pc, tb->pc, tb->flags, tb->cflags & CF_HASH_MA= SK, - tb->trace_vcpu_dstate); - qht_insert(&tb_ctx.htable, tb, h); - #ifdef CONFIG_USER_ONLY if (DEBUG_TB_CHECK_GATE) { tb_page_check(); } #endif + return tb; } =20 /* Called with mmap_lock held for user mode emulation. */ @@ -1558,7 +1579,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu, uint32_t flags, int cflags) { CPUArchState *env =3D cpu->env_ptr; - TranslationBlock *tb; + TranslationBlock *tb, *existing_tb; tb_page_addr_t phys_pc, phys_page2; target_ulong virt_page2; tcg_insn_unit *gen_code_buf; @@ -1706,7 +1727,15 @@ TranslationBlock *tb_gen_code(CPUState *cpu, * memory barrier is required before tb_link_page() makes the TB visib= le * through the physical hash table and physical page list. */ - tb_link_page(tb, phys_pc, phys_page2); + existing_tb =3D tb_link_page(tb, phys_pc, phys_page2); + /* if the TB already exists, discard what we just translated */ + if (unlikely(existing_tb !=3D tb)) { + uintptr_t orig_aligned =3D (uintptr_t)gen_code_buf; + + orig_aligned -=3D ROUND_UP(sizeof(*tb), qemu_icache_linesize); + atomic_set(&tcg_ctx->code_gen_ptr, orig_aligned); + return existing_tb; + } tcg_tb_insert(tb); return tb; } --=20 2.7.4