Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4808221iob; Mon, 9 May 2022 02:02:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx5dcDLlVclI9HkB3usbwjUQf8tWs9oE46G6NNBgeByx7x8gFjkPkDt97cW8dvR5ovTaM8J X-Received: by 2002:a65:6e47:0:b0:3c6:7d47:ddc8 with SMTP id be7-20020a656e47000000b003c67d47ddc8mr7782577pgb.157.1652086959862; Mon, 09 May 2022 02:02:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652086959; cv=none; d=google.com; s=arc-20160816; b=tZRdu5BgzBzUid0L7H6gjSKz5p1OigYHw5DdX8S0fHIRLWpmVRCkCiL4kTcbQVgsMj wt+PigbgoUzd/R+miE5HQQ0bowSuoUuY+IHP5HN6uLdrNH7y/XV46gVEB+C23QnTkElo AOHi8DFmWya6aTg95x/x3IA65VGDyRbxM95sUqD5n9vxcJF1ycbqR64z6BdNbkochKsO OU8Cu8PQTmgTzRLRc6dI7ZaMAymoAmmeRGuwXNe1clV4cRzmmS3baEEoZcKPimPWE7rR e4Ilb0UGS8lMBiHuOAentQ3zFOa99awGYzMtF9/NQgQ0cA9xhVAtId1XaYiXXEcmXwkC rEvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=WaBl9pAWGWFlLK9nV+267TWCL6dZ06A/NoNmIhWgBMM=; b=RNsAZTBs9G6sB/jhBQ2B17pk9yAPiMfqgYaH83livzQKOleDBdp3UHY10WFOr66SHH 8WgqGDUY9vfJ592oujTlRKivS+zDCMVbUgpaqqp5TGkiDWhEviF8MBTGBmuzbl9xOWD0 PiiUuXUG6Z5tyEE2DLXXaYuFXowICu2moXr6OwdXKB13BmmJ30Yjb46Jk4yujNHz7v11 pzBjIZ7MkGpw0tI7DDOyA7O6nr6jc2r9vHg2gC3qH49HCloQ6OXmsdIWESzE+SBPXnrl NubwO/Mxp63ONzDAjRbcn1XFJOsAtMs11cIczygvf09rBVACH9tYp6AgxuemKcLFIeAT 0WrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@atishpatra.org header.s=google header.b=Su5V0nbX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id p2-20020a170902f08200b0015d19866cf7si10001725pla.345.2022.05.09.02.02.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 May 2022 02:02:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@atishpatra.org header.s=google header.b=Su5V0nbX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9F4A41EFA1C; Mon, 9 May 2022 01:50:28 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1389745AbiEFH5Y (ORCPT + 99 others); Fri, 6 May 2022 03:57:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1389750AbiEFH5V (ORCPT ); Fri, 6 May 2022 03:57:21 -0400 Received: from mail-yb1-xb34.google.com (mail-yb1-xb34.google.com [IPv6:2607:f8b0:4864:20::b34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D56F067D1D for ; Fri, 6 May 2022 00:53:35 -0700 (PDT) Received: by mail-yb1-xb34.google.com with SMTP id g28so11510172ybj.10 for ; Fri, 06 May 2022 00:53:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=atishpatra.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=WaBl9pAWGWFlLK9nV+267TWCL6dZ06A/NoNmIhWgBMM=; b=Su5V0nbXIO7rEtZnbMRp6hT4/ABpQJrp77PFoY9EtWOQEiTO3IVqGoF+Aa1fmBbLE8 sV5G5fS9H0cy10W9/iTLIJ7MbO4JOEUT53M5bbO0n4IqbE0l1jEOckYtTcTSDQ2jQi5p DpGRkL6dF5eI0pWynFjtyuja8/CSpi9bcOwko= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=WaBl9pAWGWFlLK9nV+267TWCL6dZ06A/NoNmIhWgBMM=; b=kMvU5qcPAKGT24TnCqf9TLuHZ37l5SPz8xEtIHUluXbOASg+F69uFcXzZQexyiG2II VX3vpy78icZXFKJzvuGZWrIquZFXw6yctFvh/0hnPQOuKrQNoGGzx63qVW78CRoH41jv CAfkhkNw0aBPNZDZXzmmxbuQWRdQG2EbTN1sOrF3AUOQA385Op+ldEcGh0xuaXQ35zx9 9DAcvUQEjtqCO0lB7w2v5T5Ae1ZtLKeqM2miQ2+pDFk8Tpd5YSIAuPnECP0CamI0zMEy C8TA7ZdcCy5Q+yom12Pwu5OcDAWzz2IJ+sGuFxgFG81NSTj1KmBFyPAbVu0kwy+Tt3rE moeg== X-Gm-Message-State: AOAM5329FrNv1M+CA6oznft9VStwc3IYHd9KfzdzqV6AEoM79+ohRsOC 4V7RUjkcFVBqG3RNeYCRtLTL+0ikAF7+6aAAIgbb X-Received: by 2002:a5b:803:0:b0:633:749f:9acd with SMTP id x3-20020a5b0803000000b00633749f9acdmr1437126ybp.236.1651823615080; Fri, 06 May 2022 00:53:35 -0700 (PDT) MIME-Version: 1.0 References: <20220420112450.155624-1-apatel@ventanamicro.com> <20220420112450.155624-8-apatel@ventanamicro.com> In-Reply-To: <20220420112450.155624-8-apatel@ventanamicro.com> From: Atish Patra Date: Fri, 6 May 2022 00:53:24 -0700 Message-ID: Subject: Re: [PATCH v2 7/7] RISC-V: KVM: Cleanup stale TLB entries when host CPU changes To: Anup Patel Cc: Paolo Bonzini , Palmer Dabbelt , Paul Walmsley , Alistair Francis , Anup Patel , KVM General , kvm-riscv@lists.infradead.org, linux-riscv , "linux-kernel@vger.kernel.org List" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 20, 2022 at 4:25 AM Anup Patel wrote: > > On RISC-V platforms with hardware VMID support, we share same > VMID for all VCPUs of a particular Guest/VM. This means we might > have stale G-stage TLB entries on the current Host CPU due to > some other VCPU of the same Guest which ran previously on the > current Host CPU. > > To cleanup stale TLB entries, we simply flush all G-stage TLB > entries by VMID whenever underlying Host CPU changes for a VCPU. > > Signed-off-by: Anup Patel > --- > arch/riscv/include/asm/kvm_host.h | 5 +++++ > arch/riscv/kvm/tlb.c | 23 +++++++++++++++++++++++ > arch/riscv/kvm/vcpu.c | 11 +++++++++++ > 3 files changed, 39 insertions(+) > > diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h > index a40e88a9481c..94349a5ffd34 100644 > --- a/arch/riscv/include/asm/kvm_host.h > +++ b/arch/riscv/include/asm/kvm_host.h > @@ -166,6 +166,9 @@ struct kvm_vcpu_arch { > /* VCPU ran at least once */ > bool ran_atleast_once; > > + /* Last Host CPU on which Guest VCPU exited */ > + int last_exit_cpu; > + > /* ISA feature bits (similar to MISA) */ > unsigned long isa; > > @@ -256,6 +259,8 @@ void kvm_riscv_local_hfence_vvma_gva(unsigned long vmid, > unsigned long order); > void kvm_riscv_local_hfence_vvma_all(unsigned long vmid); > > +void kvm_riscv_local_tlb_sanitize(struct kvm_vcpu *vcpu); > + > void kvm_riscv_fence_i_process(struct kvm_vcpu *vcpu); > void kvm_riscv_hfence_gvma_vmid_all_process(struct kvm_vcpu *vcpu); > void kvm_riscv_hfence_vvma_all_process(struct kvm_vcpu *vcpu); > diff --git a/arch/riscv/kvm/tlb.c b/arch/riscv/kvm/tlb.c > index c0f86d09c41d..1a76d0b1907d 100644 > --- a/arch/riscv/kvm/tlb.c > +++ b/arch/riscv/kvm/tlb.c > @@ -215,6 +215,29 @@ void kvm_riscv_local_hfence_vvma_all(unsigned long vmid) > csr_write(CSR_HGATP, hgatp); > } > > +void kvm_riscv_local_tlb_sanitize(struct kvm_vcpu *vcpu) > +{ > + unsigned long vmid; > + > + if (!kvm_riscv_gstage_vmid_bits() || > + vcpu->arch.last_exit_cpu == vcpu->cpu) > + return; > + > + /* > + * On RISC-V platforms with hardware VMID support, we share same > + * VMID for all VCPUs of a particular Guest/VM. This means we might > + * have stale G-stage TLB entries on the current Host CPU due to > + * some other VCPU of the same Guest which ran previously on the > + * current Host CPU. > + * > + * To cleanup stale TLB entries, we simply flush all G-stage TLB > + * entries by VMID whenever underlying Host CPU changes for a VCPU. > + */ > + > + vmid = READ_ONCE(vcpu->kvm->arch.vmid.vmid); > + kvm_riscv_local_hfence_gvma_vmid_all(vmid); > +} > + > void kvm_riscv_fence_i_process(struct kvm_vcpu *vcpu) > { > local_flush_icache_all(); > diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c > index 9cd8f6e91c98..a86710fcd2e0 100644 > --- a/arch/riscv/kvm/vcpu.c > +++ b/arch/riscv/kvm/vcpu.c > @@ -67,6 +67,8 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) > if (loaded) > kvm_arch_vcpu_put(vcpu); > > + vcpu->arch.last_exit_cpu = -1; > + > memcpy(csr, reset_csr, sizeof(*csr)); > > memcpy(cntx, reset_cntx, sizeof(*cntx)); > @@ -735,6 +737,7 @@ static void noinstr kvm_riscv_vcpu_enter_exit(struct kvm_vcpu *vcpu) > { > guest_state_enter_irqoff(); > __kvm_riscv_switch_to(&vcpu->arch); > + vcpu->arch.last_exit_cpu = vcpu->cpu; > guest_state_exit_irqoff(); > } > > @@ -829,6 +832,14 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) > continue; > } > > + /* > + * Cleanup stale TLB enteries > + * > + * Note: This should be done after G-stage VMID has been > + * updated using kvm_riscv_gstage_vmid_ver_changed() > + */ > + kvm_riscv_local_tlb_sanitize(vcpu); > + > guest_timing_enter_irqoff(); > > kvm_riscv_vcpu_enter_exit(vcpu); > -- > 2.25.1 > Reviewed-by: Atish Patra -- Regards, Atish