From patchwork Wed Oct 31 12:21:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 149786 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp6723228ljp; Wed, 31 Oct 2018 05:24:34 -0700 (PDT) X-Google-Smtp-Source: AJdET5crjZfAI867euiFZe3aV8n8oG0fLEP7J+vq7KdnAP0Tr2LH0RGX+gs7oNxm9vgPJhYbZiyD X-Received: by 2002:aed:20a3:: with SMTP id 32-v6mr2408896qtb.195.1540988674642; Wed, 31 Oct 2018 05:24:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540988674; cv=none; d=google.com; s=arc-20160816; b=h+w3SkSqbF/+BkA2SPjkz1QtkemkqOslaybpvw3nihf22MIu5jTIdFDCzJNG2wBBa7 QxAhYUiqk2Xd+32SzszZAJSjOtRxQH6I6KPHpoGLc2EtNd1dzJ/suLlJJyhUrdvYd9VF 1EiVEGyF8g1tLDqkfwviY85K7jM/a+/cF5qBKkD36PH26UZerlegN23k0Vfo/cdAQqSE NIuW/Sw7pXYbqC1/7D7CqhkhHK1rys2OonVxZUgc+TU4II+iVkdChmlE/ijxnR4i6CYL c4TbKm+02QwaoxJKjFlIuPb6AHoMWuR+3+z7c2NFB6ffHwGpisip0adDokLSt3IzJj9X FpXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=M7C/qJbPBoTQzDBdhyvdybNqwgrTLXTdlhXmYkdqAcs=; b=L2tnJtdLdqs9kx7YyB+TyFYa1tQNtzi3W8HXQ9g3VNyBiy989lhS2IphWv0H7Sldc/ nUUGYAZbHNA0Ad9zoHJf/s9ziFug1AcG7kTaDm0Whgd8vH6uDxEy7e50aeOZnLxjVT/Q rugKncsyHYFXMSpnBC1nXIMHAzaO463mAWs0aSSbNMGd7M43/tq5co8U/2LmJVdXC2d/ LKaywQeiRz1eocKV58FDTjUAB4Jht6NXZ9i8ep++HuEFZhEW9TCPzPmpAghm5jNtk12o oJLZLZ3sdDvqfzIjicMhJgzFKb7CFqHsMekL1ZrOCLIAuA6JXvCpy6iqxhfiR7DN1+j9 ms+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=YWvGryWz; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id k32-v6si1460246qtc.318.2018.10.31.05.24.34 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 31 Oct 2018 05:24:34 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=YWvGryWz; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:59148 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gHpY2-00034q-1e for patch@linaro.org; Wed, 31 Oct 2018 08:24:34 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48844) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gHpVU-0000hA-9p for qemu-devel@nongnu.org; Wed, 31 Oct 2018 08:21:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gHpVS-0001fQ-0j for qemu-devel@nongnu.org; Wed, 31 Oct 2018 08:21:55 -0400 Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]:40488) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gHpVQ-00019T-I1 for qemu-devel@nongnu.org; Wed, 31 Oct 2018 08:21:52 -0400 Received: by mail-wr1-x42c.google.com with SMTP id i17-v6so16201711wre.7 for ; Wed, 31 Oct 2018 05:21:24 -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=M7C/qJbPBoTQzDBdhyvdybNqwgrTLXTdlhXmYkdqAcs=; b=YWvGryWzgM6SfqH8JrzOK9hrL8ZUOClOFX0GDo9/ANYri39IAWDGMkq1pwpGEwnnqf v9i+z+5rbcpyLUSwi+zfGi5khJ2bEl0oKCKdoI3Z7x68DuPpiemOzQNvejMP0kDVNMtk r95g3vAem/qDxUeOqL2KYA7vuF5HbhIE4s6Ak= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=M7C/qJbPBoTQzDBdhyvdybNqwgrTLXTdlhXmYkdqAcs=; b=gSeWMBO4vCXJ0hdtqr9rUMfqAR6Ls18lhA/1JR9xQbCkLe/142BonFnV3ryYR8of01 jNqLLADFk68h0M6/dYC9BCHcIWpau3xkP9z+T2YKp1OZkGdXaHvxlG20HiEiK8uu+gcy Js+S6mzs8beA52BM1yV/KVlJ9qXng69iWbsD/d2KE9HaoILKWxSadzRcjUSFLo9z0oGm I0m0lCDdVKpqKvaZFBRUwdU4ZAb1Qvh6hw6mNcLHx4jogi/zxGfOcMslenxbBKUOfVZy lGLBiKoDB+Pbh3fWYfIFZ/IB9lmZILdlLXBqPisp9s95OQdzOMHPBqVyBd0rD0olH2gj warQ== X-Gm-Message-State: AGRZ1gJG0sqTpYA+6xZdCpzeUCcajlFKGUq2wb8hiiylhO+hy3V3KaAv 2X9fAa0UTNJbWrkcdswZVUl6NtharqU= X-Received: by 2002:adf:ce06:: with SMTP id p6-v6mr2758231wrn.324.1540988483383; Wed, 31 Oct 2018 05:21:23 -0700 (PDT) Received: from cloudburst.twiddle.net.lan (79-69-241-110.dynamic.dsl.as9105.com. [79.69.241.110]) by smtp.gmail.com with ESMTPSA id v2-v6sm13450362wru.17.2018.10.31.05.21.22 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 31 Oct 2018 05:21:22 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 31 Oct 2018 12:21:12 +0000 Message-Id: <20181031122119.1669-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20181031122119.1669-1-richard.henderson@linaro.org> References: <20181031122119.1669-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::42c Subject: [Qemu-devel] [PULL 03/10] cputlb: Move cpu->pending_tlb_flush to env->tlb_c.pending_flush 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 Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Protect it with the tlb_lock instead of using atomics. The move puts it in or near the same cacheline as the lock; using the lock means we don't need a second atomic operation in order to perform the update. Which makes it cheap to also update pending_flush in tlb_flush_by_mmuidx_async_work. Tested-by: Emilio G. Cota Reviewed-by: Emilio G. Cota Signed-off-by: Richard Henderson --- include/exec/cpu-defs.h | 8 +++++++- include/qom/cpu.h | 6 ------ accel/tcg/cputlb.c | 35 +++++++++++++++++++++++------------ 3 files changed, 30 insertions(+), 19 deletions(-) -- 2.17.2 diff --git a/include/exec/cpu-defs.h b/include/exec/cpu-defs.h index 9005923b4d..659c73d2a1 100644 --- a/include/exec/cpu-defs.h +++ b/include/exec/cpu-defs.h @@ -145,8 +145,14 @@ typedef struct CPUIOTLBEntry { * Data elements that are shared between all MMU modes. */ typedef struct CPUTLBCommon { - /* lock serializes updates to tlb_table and tlb_v_table */ + /* Serialize updates to tlb_table and tlb_v_table, and others as noted. */ QemuSpin lock; + /* + * Within pending_flush, for each bit N, there exists an outstanding + * cross-cpu flush for mmu_idx N. Further cross-cpu flushes to that + * mmu_idx may be discarded. Protected by tlb_c.lock. + */ + uint16_t pending_flush; } CPUTLBCommon; /* diff --git a/include/qom/cpu.h b/include/qom/cpu.h index def0c64308..1396f53e5b 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -429,12 +429,6 @@ struct CPUState { struct hax_vcpu_state *hax_vcpu; - /* The pending_tlb_flush flag is set and cleared atomically to - * avoid potential races. The aim of the flag is to avoid - * unnecessary flushes. - */ - uint16_t pending_tlb_flush; - int hvf_fd; /* track IOMMUs whose translations we've cached in the TCG TLB */ diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c index d080769c83..abcd08a8a2 100644 --- a/accel/tcg/cputlb.c +++ b/accel/tcg/cputlb.c @@ -133,6 +133,7 @@ static void tlb_flush_nocheck(CPUState *cpu) * that do not hold the lock are performed by the same owner thread. */ qemu_spin_lock(&env->tlb_c.lock); + env->tlb_c.pending_flush = 0; memset(env->tlb_table, -1, sizeof(env->tlb_table)); memset(env->tlb_v_table, -1, sizeof(env->tlb_v_table)); qemu_spin_unlock(&env->tlb_c.lock); @@ -142,8 +143,6 @@ static void tlb_flush_nocheck(CPUState *cpu) env->vtlb_index = 0; env->tlb_flush_addr = -1; env->tlb_flush_mask = 0; - - atomic_mb_set(&cpu->pending_tlb_flush, 0); } static void tlb_flush_global_async_work(CPUState *cpu, run_on_cpu_data data) @@ -154,8 +153,15 @@ static void tlb_flush_global_async_work(CPUState *cpu, run_on_cpu_data data) void tlb_flush(CPUState *cpu) { if (cpu->created && !qemu_cpu_is_self(cpu)) { - if (atomic_mb_read(&cpu->pending_tlb_flush) != ALL_MMUIDX_BITS) { - atomic_mb_set(&cpu->pending_tlb_flush, ALL_MMUIDX_BITS); + CPUArchState *env = cpu->env_ptr; + uint16_t pending; + + qemu_spin_lock(&env->tlb_c.lock); + pending = env->tlb_c.pending_flush; + env->tlb_c.pending_flush = ALL_MMUIDX_BITS; + qemu_spin_unlock(&env->tlb_c.lock); + + if (pending != ALL_MMUIDX_BITS) { async_run_on_cpu(cpu, tlb_flush_global_async_work, RUN_ON_CPU_NULL); } @@ -189,6 +195,8 @@ static void tlb_flush_by_mmuidx_async_work(CPUState *cpu, run_on_cpu_data data) tlb_debug("start: mmu_idx:0x%04lx\n", mmu_idx_bitmask); qemu_spin_lock(&env->tlb_c.lock); + env->tlb_c.pending_flush &= ~mmu_idx_bitmask; + for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { if (test_bit(mmu_idx, &mmu_idx_bitmask)) { @@ -210,19 +218,22 @@ void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap) tlb_debug("mmu_idx: 0x%" PRIx16 "\n", idxmap); if (!qemu_cpu_is_self(cpu)) { - uint16_t pending_flushes = idxmap; - pending_flushes &= ~atomic_mb_read(&cpu->pending_tlb_flush); + CPUArchState *env = cpu->env_ptr; + uint16_t pending, to_clean; - if (pending_flushes) { - tlb_debug("reduced mmu_idx: 0x%" PRIx16 "\n", pending_flushes); + qemu_spin_lock(&env->tlb_c.lock); + pending = env->tlb_c.pending_flush; + to_clean = idxmap & ~pending; + env->tlb_c.pending_flush = pending | idxmap; + qemu_spin_unlock(&env->tlb_c.lock); - atomic_or(&cpu->pending_tlb_flush, pending_flushes); + if (to_clean) { + tlb_debug("reduced mmu_idx: 0x%" PRIx16 "\n", to_clean); async_run_on_cpu(cpu, tlb_flush_by_mmuidx_async_work, - RUN_ON_CPU_HOST_INT(pending_flushes)); + RUN_ON_CPU_HOST_INT(to_clean)); } } else { - tlb_flush_by_mmuidx_async_work(cpu, - RUN_ON_CPU_HOST_INT(idxmap)); + tlb_flush_by_mmuidx_async_work(cpu, RUN_ON_CPU_HOST_INT(idxmap)); } }