From patchwork Thu Jul 20 06:27:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhaotianrui X-Patchwork-Id: 123042 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2938259vqt; Thu, 20 Jul 2023 00:14:48 -0700 (PDT) X-Google-Smtp-Source: APBJJlE64XiQ8csMw+4kN09qI2tMj1JUNXx416edEONtoHaQP1g+8J12Js0tOjq64aFY3kRKl9qE X-Received: by 2002:a17:90a:e2d3:b0:263:e121:5440 with SMTP id fr19-20020a17090ae2d300b00263e1215440mr6461371pjb.1.1689837288418; Thu, 20 Jul 2023 00:14:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689837288; cv=none; d=google.com; s=arc-20160816; b=TEvQIm8pjizG3xyC3y1vYgcWjnGRvTprxotm+TXjhCXHTEgwxvLa8J2fPE07GGVWOJ NjUCxYgOwD2Z6LmzT498hCot/v1aQNhhNfvv6dti027QLJmyJt02hR88uCwn3h4QVYVa fQYx4B/FUC+t4UdPoMjaOS8I/cwkotzz4pRotGH6tIn/67kyDDlBR88HJYPAG7eDsZpn M1csVcD3PV7sqkqzGT82CSohAqMuLHUishoxH9vxf0nOwNtnPLFxRiHdQQRkV7twD/PV 7VTmsdtng9YY0K47GEupVPP55x6TmRRRXsQS5EmQYEi0d5C9xE3ZPrk6WWF2qSAgBsPE C/Og== 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=hU+f/x0fwhReIFqO1n31+biX2P4RkRF++u2k5xzX9jc=; fh=vBXozhnPXwSB3DJhjnKWz3f6luaN/KxHDpM6/QeZg4o=; b=cQxG9x8nCenu9OGvjagMqikVdTa9bJfFff2ydhuVGJ0ZY1E4O+1ddX+lc8on0/GZhy Og+NsvnlbIJmKGnBTLPguTsIt7dAKIrIemJj7z5ZZm0JodnMquLFNlJg7TXgSo3fVN60 WAAY419hTHS50sG0nXrZVDT16McvO9EG16+C8zHw4Ug8AWHhyWxBjnyb8Mj7ut3ROF+C QK/DbL4EBcLfde01dujxJuL1fgHMbFaZsDR96pnmElNFJYmR//2OvS3x4AZHlhuljdYs NbSQacU+6or78iCxgXdq3bQF+VNmAy8+1iHnlpTxlz4U1MP2Fz5ypQtEMsRZ+LMsxY1n ubcQ== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s2-20020a63e802000000b005348b4f79f1si243408pgh.345.2023.07.20.00.14.35; Thu, 20 Jul 2023 00:14:48 -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 S230443AbjGTG3Q (ORCPT + 99 others); Thu, 20 Jul 2023 02:29:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230257AbjGTG2j (ORCPT ); Thu, 20 Jul 2023 02:28:39 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0DB2B2686; Wed, 19 Jul 2023 23:28:26 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8Dxg_AD1LhknY0HAA--.19528S3; Thu, 20 Jul 2023 14:28:19 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8CxvM7907hkHU81AA--.16059S18; Thu, 20 Jul 2023 14:28:19 +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 v17 16/30] LoongArch: KVM: Implement update VM id function Date: Thu, 20 Jul 2023 14:27:59 +0800 Message-Id: <20230720062813.4126751-17-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230720062813.4126751-1-zhaotianrui@loongson.cn> References: <20230720062813.4126751-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8CxvM7907hkHU81AA--.16059S18 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,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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771922824093875983 X-GMAIL-MSGID: 1771922824093875983 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..fc25ddc3b74a --- /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); +}