From patchwork Tue Jul 5 16:18:11 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 644957 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rkX9c0Vh9z9rxm for ; Wed, 6 Jul 2016 04:15:16 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b=XQOok2jg; dkim-atps=neutral Received: from localhost ([::1]:56984 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bKUsL-0007vD-HF for incoming@patchwork.ozlabs.org; Tue, 05 Jul 2016 14:15:13 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56577) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bKT3D-00078L-Ek for qemu-devel@nongnu.org; Tue, 05 Jul 2016 12:18:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bKT3B-0001wb-RW for qemu-devel@nongnu.org; Tue, 05 Jul 2016 12:18:19 -0400 Received: from mail-wm0-x22a.google.com ([2a00:1450:400c:c09::22a]:33406) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bKT3B-0001wO-Hl for qemu-devel@nongnu.org; Tue, 05 Jul 2016 12:18:17 -0400 Received: by mail-wm0-x22a.google.com with SMTP id r190so27846447wmr.0 for ; Tue, 05 Jul 2016 09:18:17 -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=4wMBqlRPDVkymGzAJKbJrkKMptGIED4HyHnKqv4x1yQ=; b=XQOok2jgiE4/4OPaspOx8h/A1mPEUMdvIEWb8AbNQHAvRinSuaGVVfqrCdVIOpYCmN RhvoqyRdSxuA07Av3KCNK6p3jZbt0VkqlYZxCE2w7Y9cae9hBKqIfGvdRqUudt5ruMfg LxJeSaW3Lr0F00ewUTvvTHrUoExByalmcd5yM= 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=4wMBqlRPDVkymGzAJKbJrkKMptGIED4HyHnKqv4x1yQ=; b=IYuYorKqJGK27asSjoRKNWXzFyVc14bAjvTKUWxArb7sy0wW7bO5xJhnzr+6fcWuiH A4V/QSB2CvfVebLdGQZlZONyWE1tAABWBW1OvY83zEnNTLl+htexFQbzbziBn4D3f7mC NNOYe8AIFlkRBFiL4dbH+VhrPFvP9N4+bQPbiOitELf6byqFoO0CHgkanHih6fzVxHOM MHtyhYSQ9M2Z8ZyW9jvta6TKAAIbGo209Lc+Z2mLuLXKmGuZ0MOuGz/umqh9EiOs6d7c GrB/Apu6MXZBB5Z40lrbn0ISZYTglss3iNrYY0m5Y65oGOxGDvETJxFdU+ul+ROJw9kP AoVg== X-Gm-Message-State: ALyK8tIUTpblb5OebbDXwZ9lKao9egQAV2Erod1ukn4NI/O6SDxAyU+hPhAQzAwu4zNkzB0F X-Received: by 10.28.145.17 with SMTP id t17mr17669015wmd.14.1467735496856; Tue, 05 Jul 2016 09:18:16 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id h8sm1201221wjg.9.2016.07.05.09.18.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Jul 2016 09:18:13 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 78CD63E0473; Tue, 5 Jul 2016 17:18:17 +0100 (BST) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: mttcg@listserver.greensocs.com, qemu-devel@nongnu.org, fred.konrad@greensocs.com, a.rigo@virtualopensystems.com, serge.fdrv@gmail.com, cota@braap.org, bobby.prani@gmail.com, rth@twiddle.net Date: Tue, 5 Jul 2016 17:18:11 +0100 Message-Id: <1467735496-16256-2-git-send-email-alex.bennee@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1467735496-16256-1-git-send-email-alex.bennee@linaro.org> References: <1467735496-16256-1-git-send-email-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::22a Subject: [Qemu-devel] [PATCH v2 1/6] tcg: Ensure safe tb_jmp_cache lookup out of 'tb_lock' 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: peter.maydell@linaro.org, Sergey Fedorov , Peter Crosthwaite , claudio.fontana@huawei.com, mark.burton@greensocs.com, jan.kiszka@siemens.com, pbonzini@redhat.com, =?UTF-8?q?Alex=20Benn=C3=A9e?= Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Sergey Fedorov First, ensure atomicity of CPU's 'tb_jmp_cache' access by: * using atomic_read() to look up a TB when not holding 'tb_lock'; * using atomic_write() to remove a TB from each CPU's local cache on TB invalidation. Second, add some memory barriers to ensure we don't put the TB being invalidated back to CPU's 'tb_jmp_cache'. If we fail to look up a TB in CPU's local cache because it is being invalidated by some other thread then it must not be found in the shared TB hash table. Otherwise we'd put it back to CPU's local cache. Note that this patch does *not* make CPU's TLB invalidation safe if it is done from some other thread while the CPU is in its execution loop. Signed-off-by: Sergey Fedorov Signed-off-by: Sergey Fedorov [AJB: fixed missing atomic set, tweak title] Signed-off-by: Alex Bennée Reviewed-by: Richard Henderson Reviewed-by: Emilio G. Cota --- v1 (AJB): - tweak title - fixed missing set of tb_jmp_cache v2 - fix spelling s/con't/can't/ - add atomic_read while clearing tb_jmp_cache - add r-b tags --- cpu-exec.c | 9 +++++++-- translate-all.c | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/cpu-exec.c b/cpu-exec.c index b840e1d..10ce1cb 100644 --- a/cpu-exec.c +++ b/cpu-exec.c @@ -285,6 +285,11 @@ static TranslationBlock *tb_find_slow(CPUState *cpu, { TranslationBlock *tb; + /* Ensure that we won't find a TB in the shared hash table + * if it is being invalidated by some other thread. + * Otherwise we'd put it back to CPU's local cache. + * Pairs with smp_wmb() in tb_phys_invalidate(). */ + smp_rmb(); tb = tb_find_physical(cpu, pc, cs_base, flags); if (tb) { goto found; @@ -315,7 +320,7 @@ static TranslationBlock *tb_find_slow(CPUState *cpu, found: /* we add the TB in the virtual pc hash table */ - cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)] = tb; + atomic_set(&cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)], tb); return tb; } @@ -333,7 +338,7 @@ static inline TranslationBlock *tb_find_fast(CPUState *cpu, is executed. */ cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags); tb_lock(); - tb = cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)]; + 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)) { tb = tb_find_slow(cpu, pc, cs_base, flags); diff --git a/translate-all.c b/translate-all.c index eaa95e4..96efe48 100644 --- a/translate-all.c +++ b/translate-all.c @@ -1004,11 +1004,16 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) invalidate_page_bitmap(p); } + /* Ensure that we won't find the TB in the shared hash table + * if we can't see it in CPU's local cache. + * Pairs with smp_rmb() in tb_find_slow(). */ + smp_wmb(); + /* remove the TB from the hash list */ h = tb_jmp_cache_hash_func(tb->pc); CPU_FOREACH(cpu) { - if (cpu->tb_jmp_cache[h] == tb) { - cpu->tb_jmp_cache[h] = NULL; + if (atomic_read(&cpu->tb_jmp_cache[h]) == tb) { + atomic_set(&cpu->tb_jmp_cache[h], NULL); } }