From patchwork Tue Jul 12 20:13:44 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sergey Fedorov X-Patchwork-Id: 71879 Delivered-To: patches@linaro.org Received: by 10.140.29.52 with SMTP id a49csp653237qga; Tue, 12 Jul 2016 13:14:38 -0700 (PDT) X-Received: by 10.25.207.15 with SMTP id f15mr2178312lfg.108.1468354468635; Tue, 12 Jul 2016 13:14:28 -0700 (PDT) Return-Path: Received: from mail-lf0-x230.google.com (mail-lf0-x230.google.com. [2a00:1450:4010:c07::230]) by mx.google.com with ESMTPS id f69si1240705lji.20.2016.07.12.13.14.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Jul 2016 13:14:28 -0700 (PDT) Received-SPF: pass (google.com: domain of sergey.fedorov@linaro.org designates 2a00:1450:4010:c07::230 as permitted sender) client-ip=2a00:1450:4010:c07::230; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org; spf=pass (google.com: domain of sergey.fedorov@linaro.org designates 2a00:1450:4010:c07::230 as permitted sender) smtp.mailfrom=sergey.fedorov@linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by mail-lf0-x230.google.com with SMTP id q132so22842528lfe.3 for ; Tue, 12 Jul 2016 13:14:28 -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=4O9CcvLaE682HUZLAocadk59+e3g8Xn/5cU6tymwdbA=; b=Ry4dCbYWE1g0Hmzw0NAuiNuBNhv7P6sZw9WR4hfJkIasv/C4ufOZEwq+pG4Nr+Xa63 dhVr6H4iZC40mRD1j8POjaQPizE56lgF4GR4z3N4qsPNDUjx09Glx/xazDB/NFTWAhQV Y1lePesh8pag7UKLSDi/ffIJQuV1K/lxQfSes= 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=4O9CcvLaE682HUZLAocadk59+e3g8Xn/5cU6tymwdbA=; b=Z5jDTgCnc/gxONp5nYbUNy2y/2b/Gw4VEYtMrGZhOrVOr/4PJgl9UyeMKSR+z105li ZRpIaIWdMjaQtCokdONvtLRNddWkWiYtyqW97U3BuIIcLkCbVl7T66TAlrcd7uCQPFd0 z10Hk2GuTTG7wzi+9RCBf7btcvVCjky8khZRWr7cw3C1niLcTe8eyfdhl7znjtK0QBwR 49hK/B+SaP8kbn1RblNjDu5Mu1tnAitJdCC+tafb0dPPtuSXgug+qHH2S97eokzj3SqX fHwWIS3XBaY5rLaLlfcfq0Sgcz+7hVLYxCcg2+Ug2vAe3CX6Hq+OB/Mo9e5MbWj7hnU5 fGjw== X-Gm-Message-State: ALyK8tInFyEwstbXsmT0k0YrDe3xEhszoiRfVzxme6sW9Zan3x2E4Jlntqny5ZwzwUJa4Ur/sTc= X-Received: by 10.25.76.137 with SMTP id z131mr2202830lfa.49.1468354468283; Tue, 12 Jul 2016 13:14:28 -0700 (PDT) Return-Path: Received: from sergey-laptop.Dlink (broadband-46-188-120-37.2com.net. [46.188.120.37]) by smtp.gmail.com with ESMTPSA id g40sm2474029ljg.22.2016.07.12.13.14.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 12 Jul 2016 13:14:27 -0700 (PDT) From: Sergey Fedorov To: qemu-devel@nongnu.org, mttcg@listserver.greensocs.com, fred.konrad@greensocs.com, a.rigo@virtualopensystems.com, serge.fdrv@gmail.com, cota@braap.org, bobby.prani@gmail.com, rth@twiddle.net Cc: patches@linaro.org, =?UTF-8?q?Alex=20Benn=C3=A9e?= , mark.burton@greensocs.com, pbonzini@redhat.com, jan.kiszka@siemens.com, peter.maydell@linaro.org, claudio.fontana@huawei.com, Sergey Fedorov , Peter Crosthwaite Subject: [PATCH v3 09/11] tcg: cpu-exec: remove tb_lock from the hot-path Date: Tue, 12 Jul 2016 23:13:44 +0300 Message-Id: <1468354426-837-10-git-send-email-sergey.fedorov@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1468354426-837-1-git-send-email-sergey.fedorov@linaro.org> References: <1468354426-837-1-git-send-email-sergey.fedorov@linaro.org> MIME-Version: 1.0 From: Alex Bennée Lock contention in the hot path of moving between existing patched TranslationBlocks is the main drag in multithreaded performance. This patch pushes the tb_lock() usage down to the two places that really need it: - code generation (tb_gen_code) - jump patching (tb_add_jump) The rest of the code doesn't really need to hold a lock as it is either using per-CPU structures, atomically updated or designed to be used in concurrent read situations (qht_lookup). To keep things simple I removed the #ifdef CONFIG_USER_ONLY stuff as the locks become NOPs anyway until the MTTCG work is completed. Signed-off-by: Alex Bennée Reviewed-by: Richard Henderson Reviewed-by: Sergey Fedorov Signed-off-by: Sergey Fedorov --- v2 (hot path) - Add r-b tags v1 (hot path, split from base-patches series) - revert name tweaking - drop test jmp_list_next outside lock - mention lock NOPs in comments v3 (base-patches) - fix merge conflicts with Sergey's patch --- cpu-exec.c | 48 +++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) -- 1.9.1 diff --git a/cpu-exec.c b/cpu-exec.c index 07dc50c56e8d..4eabd534aba0 100644 --- a/cpu-exec.c +++ b/cpu-exec.c @@ -286,35 +286,29 @@ static TranslationBlock *tb_find_slow(CPUState *cpu, TranslationBlock *tb; tb = tb_find_physical(cpu, pc, cs_base, flags); - if (tb) { - goto found; - } + if (!tb) { -#ifdef CONFIG_USER_ONLY - /* mmap_lock is needed by tb_gen_code, and mmap_lock must be - * taken outside tb_lock. Since we're momentarily dropping - * tb_lock, there's a chance that our desired tb has been - * translated. - */ - tb_unlock(); - mmap_lock(); - tb_lock(); - tb = tb_find_physical(cpu, pc, cs_base, flags); - if (tb) { - mmap_unlock(); - goto found; - } -#endif + /* mmap_lock is needed by tb_gen_code, and mmap_lock must be + * taken outside tb_lock. As system emulation is currently + * single threaded the locks are NOPs. + */ + mmap_lock(); + tb_lock(); - /* if no translated code available, then translate it now */ - tb = tb_gen_code(cpu, pc, cs_base, flags, 0); + /* There's a chance that our desired tb has been translated while + * taking the locks so we check again inside the lock. + */ + tb = tb_find_physical(cpu, pc, cs_base, flags); + if (!tb) { + /* if no translated code available, then translate it now */ + tb = tb_gen_code(cpu, pc, cs_base, flags, 0); + } -#ifdef CONFIG_USER_ONLY - mmap_unlock(); -#endif + tb_unlock(); + mmap_unlock(); + } -found: - /* we add the TB in the virtual pc hash table */ + /* We add the TB in the virtual pc hash table for the fast lookup */ atomic_set(&cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)], tb); return tb; } @@ -332,7 +326,6 @@ static inline TranslationBlock *tb_find_fast(CPUState *cpu, always be the same before a given translated block is executed. */ cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags); - tb_lock(); tb = atomic_read(&cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)]); if (unlikely(!tb || tb->pc != pc || tb->cs_base != cs_base || tb->flags != flags)) { @@ -349,14 +342,15 @@ static inline TranslationBlock *tb_find_fast(CPUState *cpu, #endif /* See if we can patch the calling TB. */ if (last_tb && !qemu_loglevel_mask(CPU_LOG_TB_NOCHAIN)) { + tb_lock(); /* Check if translation buffer has been flushed */ if (cpu->tb_flushed) { cpu->tb_flushed = false; } else if (!tb_is_invalid(tb)) { tb_add_jump(last_tb, tb_exit, tb); } + tb_unlock(); } - tb_unlock(); return tb; }