From patchwork Fri Jul 15 17:58:49 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sergey Fedorov X-Patchwork-Id: 72128 Delivered-To: patches@linaro.org Received: by 10.140.29.52 with SMTP id a49csp747972qga; Fri, 15 Jul 2016 10:59:28 -0700 (PDT) X-Received: by 10.25.21.197 with SMTP id 66mr9546183lfv.99.1468605552125; Fri, 15 Jul 2016 10:59:12 -0700 (PDT) Return-Path: Received: from mail-lf0-x233.google.com (mail-lf0-x233.google.com. [2a00:1450:4010:c07::233]) by mx.google.com with ESMTPS id f73si7328047lfi.200.2016.07.15.10.59.11 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 15 Jul 2016 10:59:12 -0700 (PDT) Received-SPF: pass (google.com: domain of sergey.fedorov@linaro.org designates 2a00:1450:4010:c07::233 as permitted sender) client-ip=2a00:1450:4010:c07::233; 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::233 as permitted sender) smtp.mailfrom=sergey.fedorov@linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by mail-lf0-x233.google.com with SMTP id l69so36599151lfg.1 for ; Fri, 15 Jul 2016 10:59:11 -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=oAiy4h3ucLS8hsnmGH3yemjbylpVR7ak9gdfC8QsJiU=; b=HeJ9YXiolk1DxKYeyVQMHcNZNxeQENWa1cVUdeiOMEvz6+gXaP5DquVNNk1wHFABCX UFgLFyTv4MKa9Ob1XuKEETFPpGqpaSzD+899VhHTcSPs+405yincaGw5rYHjYCA7aAE9 ULi2VmYe3vdWuDKQFpL1xd6S+LdSMLLvA7sK4= 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=oAiy4h3ucLS8hsnmGH3yemjbylpVR7ak9gdfC8QsJiU=; b=KpJhSzepZ//sbq1EM4F6EoG38ghQL4N544O4VAJ4GZ6qL3beHTvgR/YG0ZOwQiqrtb 7DppkdNjLbFrf2/t6bhiHQB+mTRZ642xmebQtP2O3AgW0ajpVh8CfLfSZMMPaik/jY7S Q9avRErryXrPxniNlbQj2ue8sLhQPf+N+2FV8/1kSovK9nFFf4VmgbO0PT+zp/YdkrRl T/iVjvmm2VKRMaur+6vy7200LX8wZBW79EfYZqwVsVraEwjyeCBUiDBFFBnT0Rw0lOF6 g+aTMrGiYWDOLA6vGpkNbMtNlJp9PEqmHfgGFep7igWS6cyX0GKIigwkzuV0jdUNiAaW 3XNQ== X-Gm-Message-State: ALyK8tLlwtTYKdfC7dVJ6NATq8vEOOpTs7DufSlDJr3EuWKwbclEhSaiTSAQ7XwYRMSuftcLBMc= X-Received: by 10.25.19.169 with SMTP id 41mr9597358lft.24.1468605551694; Fri, 15 Jul 2016 10:59:11 -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 g193sm2113268lfb.14.2016.07.15.10.59.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 15 Jul 2016 10:59:10 -0700 (PDT) From: Sergey Fedorov To: qemu-devel@nongnu.org Cc: patches@linaro.org, Sergey Fedorov , mttcg@listserver.greensocs.com, fred.konrad@greensocs.com, a.rigo@virtualopensystems.com, cota@braap.org, bobby.prani@gmail.com, rth@twiddle.net, mark.burton@greensocs.com, pbonzini@redhat.com, jan.kiszka@siemens.com, peter.maydell@linaro.org, claudio.fontana@huawei.com, =?UTF-8?q?Alex=20Benn=C3=A9e?= , Sergey Fedorov , Peter Crosthwaite Subject: [PATCH v4 09/12] tcg: cpu-exec: remove tb_lock from the hot-path Date: Fri, 15 Jul 2016 20:58:49 +0300 Message-Id: <20160715175852.30749-10-sergey.fedorov@linaro.org> X-Mailer: git-send-email 2.9.1 In-Reply-To: <20160715175852.30749-1-sergey.fedorov@linaro.org> References: <20160715175852.30749-1-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(-) -- 2.9.1 diff --git a/cpu-exec.c b/cpu-exec.c index e16df762f50a..bbaed5bb1978 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 || atomic_read(&tb->pc) != pc || atomic_read(&tb->cs_base) != cs_base || @@ -350,14 +343,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; }