Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp6072517rwd; Mon, 19 Jun 2023 01:43:47 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6PU47QX739Pyf7wi9Vq4MxBCEpSAqb2qbzcuDg1XjRBaeNeilY08D6OuTa00zT8uYBx0xL X-Received: by 2002:a17:902:f547:b0:1b5:4f0e:eb04 with SMTP id h7-20020a170902f54700b001b54f0eeb04mr3998062plf.24.1687164226853; Mon, 19 Jun 2023 01:43:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687164226; cv=none; d=google.com; s=arc-20160816; b=iGHH/9GyYbTnktBNXMg7STn3s/uK0J5ymW2qJXLzQiSWvO2LJ9tI+CWjTesbE46MnW n8dZdweWRYvUUACXdh47XcuERPLqbtvlvtQKM9WfSvkDufWoT96k7dR8YrSMHvFdVpYa Vxw+wHdr+3pmLBjC5LD14nIQ44dMzoXA2eaCCkdbLMDEgwD1bEvMUJ1v+msrbkw4Xxtf bF74NRDqCxeMTHFBrD5jOxwTuqGfLBjY481G/iAg9Y9Ipjw/8suv4fZ8ERtHgckW7uNq a1plMWF+P6d3z6uUMJBhvfZ6AwMCXNAogZfl4uM2sjtZx7k8uzXkqgIM1hzBcdk00ZEZ a58Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=6xe1Lj1OeRF5FB+tWoype+k6gik2uKjDqX0zpeV+rbo=; b=CZInnmhniY1Tum/v415LXZywslAfrdmFcGSaez70gj9QmEYF+uA/8L9+MnoVnX/gUy MU3gEH+V6AeNK43ktOnlCUbKPyHTdF5u3loix+a9DVNJSYUN3Mixd+hxdbH3O6v8jxlt fOUtvj7rdSK/6y4V5MPrY1+56sc01yS5+nxYjXpybXseHN+uByPeuU2KnnU6ofoodNLy MIncfEcPv/s2c4k3uqZV3z5k40NvDjj5yvtq4iWd42y58m31h19oIC4193IM82sVGNhs ifr8mKLmvTuXyhnYZqHf0U1XhQ4Po8nbYugNKXx49zpV1Gg/5PMAnkKn6YFH3EMxxmT9 J4gg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f11-20020a170902ce8b00b001b392bf919bsi18783931plg.407.2023.06.19.01.43.34; Mon, 19 Jun 2023 01:43:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229640AbjFSIea (ORCPT + 99 others); Mon, 19 Jun 2023 04:34:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230344AbjFSIdP (ORCPT ); Mon, 19 Jun 2023 04:33:15 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D98BCE4; Mon, 19 Jun 2023 01:33:12 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8DxTuvEEpBkyssGAA--.13941S3; Mon, 19 Jun 2023 16:33:08 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxC8q3EpBkAgsgAA--.15169S18; Mon, 19 Jun 2023 16:33:07 +0800 (CST) From: Tianrui Zhao To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Huacai Chen , WANG Xuerui , Greg Kroah-Hartman , loongarch@lists.linux.dev, Jens Axboe , Mark Brown , Alex Deucher , Oliver Upton , maobibo@loongson.cn, Xi Ruoyao , zhaotianrui@loongson.cn, tangyouling@loongson.cn Subject: [PATCH v14 16/30] LoongArch: KVM: Implement update VM id function Date: Mon, 19 Jun 2023 16:32:41 +0800 Message-Id: <20230619083255.3841777-17-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230619083255.3841777-1-zhaotianrui@loongson.cn> References: <20230619083255.3841777-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8BxC8q3EpBkAgsgAA--.15169S18 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 Implement kvm check vmid and update vmid, the vmid should be checked before vcpu enter guest. Reviewed-by: Bibo Mao Signed-off-by: Tianrui Zhao --- arch/loongarch/kvm/vmid.c | 66 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 arch/loongarch/kvm/vmid.c diff --git a/arch/loongarch/kvm/vmid.c b/arch/loongarch/kvm/vmid.c new file mode 100644 index 000000000000..380c25c5d12b --- /dev/null +++ b/arch/loongarch/kvm/vmid.c @@ -0,0 +1,66 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2020-2023 Loongson Technology Corporation Limited + */ + +#include +#include "trace.h" + +static void _kvm_update_vpid(struct kvm_vcpu *vcpu, int cpu) +{ + struct kvm_context *context; + unsigned long vpid; + + context = per_cpu_ptr(vcpu->kvm->arch.vmcs, cpu); + vpid = context->vpid_cache + 1; + if (!(vpid & vpid_mask)) { + /* finish round of 64 bit loop */ + if (unlikely(!vpid)) + vpid = vpid_mask + 1; + + /* vpid 0 reserved for root */ + ++vpid; + + /* start new vpid cycle */ + kvm_flush_tlb_all(); + } + + context->vpid_cache = vpid; + vcpu->arch.vpid = vpid; +} + +void _kvm_check_vmid(struct kvm_vcpu *vcpu) +{ + struct kvm_context *context; + bool migrated; + unsigned long ver, old, vpid; + int cpu; + + cpu = smp_processor_id(); + /* + * Are we entering guest context on a different CPU to last time? + * If so, the vCPU's guest TLB state on this CPU may be stale. + */ + context = per_cpu_ptr(vcpu->kvm->arch.vmcs, cpu); + migrated = (vcpu->arch.last_exec_cpu != cpu); + vcpu->arch.last_exec_cpu = cpu; + + /* + * Check if our vpid is of an older version + * + * We also discard the stored vpid if we've executed on + * another CPU, as the guest mappings may have changed without + * hypervisor knowledge. + */ + ver = vcpu->arch.vpid & ~vpid_mask; + old = context->vpid_cache & ~vpid_mask; + if (migrated || (ver != old)) { + _kvm_update_vpid(vcpu, cpu); + trace_kvm_vpid_change(vcpu, vcpu->arch.vpid); + } + + /* Restore GSTAT(0x50).vpid */ + vpid = (vcpu->arch.vpid & vpid_mask) + << CSR_GSTAT_GID_SHIFT; + change_csr_gstat(vpid_mask << CSR_GSTAT_GID_SHIFT, vpid); +} -- 2.39.1