From patchwork Fri Jun 3 20:40:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 9154031 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1044660751 for ; Fri, 3 Jun 2016 20:54:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 01498282E8 for ; Fri, 3 Jun 2016 20:54:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E9E7328337; Fri, 3 Jun 2016 20:54:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 70EE8282E8 for ; Fri, 3 Jun 2016 20:54:45 +0000 (UTC) Received: from localhost ([::1]:57696 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8w7A-0004vZ-6B for patchwork-qemu-devel@patchwork.kernel.org; Fri, 03 Jun 2016 16:54:44 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37677) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8vtl-0000Eb-Vg for qemu-devel@nongnu.org; Fri, 03 Jun 2016 16:40:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b8vtg-0000d7-Ge for qemu-devel@nongnu.org; Fri, 03 Jun 2016 16:40:53 -0400 Received: from mail-wm0-x22e.google.com ([2a00:1450:400c:c09::22e]:36695) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8vtf-0000cX-TM for qemu-devel@nongnu.org; Fri, 03 Jun 2016 16:40:48 -0400 Received: by mail-wm0-x22e.google.com with SMTP id n184so11949465wmn.1 for ; Fri, 03 Jun 2016 13:40:47 -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; bh=C/y/o8Qmc9JBQOaCMVpi/qyazZlP2YBbZOp6ZV8PbCg=; b=gjMqhNAeT9aRoUeWCkFnoDwUVW8CDHTQrnQguneeAIK6IWOJZnlzFISU1rMZSfYXC6 iFyYNY51RI1thIRcmEwzwFgI1heDtHUGq1LSVwSR0uY/DP+VNntHq0VTAuGTEgiHTwJ9 50V+mWTBIXtUsjPwRb4W8LR17bQ9MCrPx1pQM= 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; bh=C/y/o8Qmc9JBQOaCMVpi/qyazZlP2YBbZOp6ZV8PbCg=; b=k2fZq+WwB3bVpuEDQyAsZqkhlVHrbuThFp4VwUKArVTLbB7vtB6fCVI5IZNafnkFwk SFrB2Dof+3SQzxrsaki7Kuj6jQgl6b+GP8WQCzQr6PWVt5jy4Yfun5/Ltyl7CUaoTSoo zao24nW2cv8IYJfHLCnPY3fj5UB6fnK00mBUFzoj27zvV7z6C8U6vCIWkid2RjK76oNj jA7lepH1BJWVI7QLFt4c9SY3yzW19R9CylsjM5rrfFP6KG8NE8v2LYSgquVVkEpumdZI JrrZJLKMkdOo3BlSD9EDloT/CPYF9qynJjMoEwYqtjpwFEnywVdT8XW+t7JtvPx/a+2/ HdFA== X-Gm-Message-State: ALyK8tIyCsvn8XaqW6qKS3V68Ov6iOVVBVlGqeH5+oujKzxdc9kGMZgJPgp03Ss5WsoMDWPt X-Received: by 10.28.88.206 with SMTP id m197mr1074727wmb.43.1464986447231; Fri, 03 Jun 2016 13:40:47 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id e1sm7388340wjv.9.2016.06.03.13.40.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Jun 2016 13:40:41 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id A07FF3E31A1; Fri, 3 Jun 2016 21:40:40 +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 Date: Fri, 3 Jun 2016 21:40:27 +0100 Message-Id: <1464986428-6739-19-git-send-email-alex.bennee@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1464986428-6739-1-git-send-email-alex.bennee@linaro.org> References: <1464986428-6739-1-git-send-email-alex.bennee@linaro.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::22e Subject: [Qemu-devel] [RFC v3 18/19] tcg: Ensure safe TB 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, rth@twiddle.net Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 --- cpu-exec.c | 7 ++++++- translate-all.c | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/cpu-exec.c b/cpu-exec.c index 5ad3865..b017643 100644 --- a/cpu-exec.c +++ b/cpu-exec.c @@ -292,6 +292,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; @@ -336,7 +341,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 95e5284..29a7946 100644 --- a/translate-all.c +++ b/translate-all.c @@ -1071,11 +1071,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 con'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; + atomic_set(&cpu->tb_jmp_cache[h], NULL); } }