Received: by 2002:a05:7412:3210:b0:e2:908c:2ebd with SMTP id eu16csp91040rdb; Thu, 31 Aug 2023 04:10:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHLHndTlVMSR+2Yd3ZE3cVxAtSalyNcvaIpMogPZhAXSpBOJFaoWGfq/3z3jEEY3UriQ9Vv X-Received: by 2002:a17:907:9719:b0:99d:f5dd:6b3 with SMTP id jg25-20020a170907971900b0099df5dd06b3mr4066674ejc.76.1693480235440; Thu, 31 Aug 2023 04:10:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693480235; cv=none; d=google.com; s=arc-20160816; b=SGLlEMD8Wdi+vXavoEwtUDDfIGxNpDm6+96HOUTo5xTciBghmoFo1NtxL0r4MbUnPA 77d0Bwb7RWP3Zo7jrmmtBleJBWR6YCFYsY9Hj/FgkUXUJ3qLfdaqTKPQ5rhaLJkBNRdh 0qAm1KY5ZR9juud2GE5nr4mEvpZZnKRzT09iCJ4uUCnn0irSQerGyGjepBhz1Wg8PFN7 UivZ1K+4nBmHUxxuMK87y6qP4O5PNEXHf/FG67kfLoLPKAB+5eNUEvLE0D+0U9Z2LjM2 0GeThW+NmCxqmvC9uHdi1I1avIg1MMkaX01QnhmlYEDOYvgV3GWIWNBGvpZdjMQBO6WM 3yzw== 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=xpQJlKgpjoWeE0Xs5GyLpOIS18vq3JGVQj4s3mOIUlA=; fh=ejoaVAUUbBXsA/6jM/beuI9xCYtV2osdR9SpI7hjY84=; b=gf6QdxQPUXT4DeZXgHfCwk7LPFBByJN93i9UiWLG03AWL5kBhO7nyXimRD+tv3lEB3 ox602BM8nb6rTRqH85yHmIRbu7wqum9P1Bi7YMvAs6c8MmA8pYpB+WkPR5LIRD9lhp0S ld4sMJNTOCEymACCiRTO3iJLV4ZqO2I5/srqLCuKg/JPwfpI/EKtXxw7QhyYXVo09ZNA +yDy+DGRitLytP1ktCSVRtoyerrdaZSLSAop6mvmZMSp7boOeh6ZLFNx3pWkb+wVCaP7 zAORvWueVGb5EBncfNa+TPV6HueQl3gPcfxYS0MwjMOB+/XWhdLwYA+VbY8gT6o3aoSD GYIQ== 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 oz9-20020a170906cd0900b009a190ce8512si788082ejb.67.2023.08.31.04.10.05; Thu, 31 Aug 2023 04:10:35 -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 S231206AbjHaIbo (ORCPT + 99 others); Thu, 31 Aug 2023 04:31:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241522AbjHaIbC (ORCPT ); Thu, 31 Aug 2023 04:31:02 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 255D8E57; Thu, 31 Aug 2023 01:30:39 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8BxIvCpT_BkfF4dAA--.59322S3; Thu, 31 Aug 2023 16:30:33 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Ax3c6gT_BkOPVnAA--.55892S18; Thu, 31 Aug 2023 16:30:33 +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 Subject: [PATCH v20 16/30] LoongArch: KVM: Implement update VM id function Date: Thu, 31 Aug 2023 16:30:06 +0800 Message-Id: <20230831083020.2187109-17-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230831083020.2187109-1-zhaotianrui@loongson.cn> References: <20230831083020.2187109-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8Ax3c6gT_BkOPVnAA--.55892S18 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, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 0000000000..fc25ddc3b7 --- /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->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); + vcpu->cpu = cpu; + } + + /* 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.27.0