From patchwork Tue Feb 14 02:56:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhaotianrui X-Patchwork-Id: 56623 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2723597wrn; Mon, 13 Feb 2023 18:59:07 -0800 (PST) X-Google-Smtp-Source: AK7set/vshOis/jJrSj98OYBC2xOnve0sTzjbHKHuJcmfIw9AlfgIvSmvhxFeAQxtBTWnFT7Nhu5 X-Received: by 2002:a05:6a20:a004:b0:bc:6bda:af51 with SMTP id p4-20020a056a20a00400b000bc6bdaaf51mr889296pzj.34.1676343547057; Mon, 13 Feb 2023 18:59:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676343547; cv=none; d=google.com; s=arc-20160816; b=qLMfYoCnbyC8JX4gkCtKAAOwDTwAj1BVHl9ogu0yoWhBA46HR4WEEVxNYs4bNjvRQl ngmqv0b4K0Yo/1fNaELL1baQAySagYB/W8E1pkjciVvRDL1zUaoSPI7slJkcEjKEVvhK bcvak2noRTWbwTreX3O942LOoa1WtcLHQ7xVX0wMOA7a0+9vWWrY4w3qDEpUwyqz8s64 9UF9W8OlwMmMYHmFAk97J3OGLVtad6xdYZh8WGOh15spo76g+H31j05Psdo4Q849ITtL zJGz7tqh61t9XNtjyLXYx96B0De/hMajrOpBs4vHSPHRGx2Okin42wyz4UJ5ta7bUu25 apsg== 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=T01RqdrusQNVC58R1Swn4XXvUbmdxmprJDjhao8bCIs=; b=G+y00QNZSW8zrEuiuMNh/BuNUMScXtvALdAgSvRtVOb/yJtfkDf6dbKFhPCmcBXLgj SCOrNxmoiYPQUEUt0qrb0T6TvqYO+mC7RIP32tLYfk6NaTXyKVvUi/vaZRf7/Ywrimli HPSTS48qgOvdonmIfEkgtrWER9IPdZ7kLRKrx1oBtsvLkMJ3MVSmLngOeflRHap5tXzy WSfDhxkT1z+EoDaUouZY/RE+00IYdrDjAvn1MH3NX+N9ZOdSVI+TZaO79Wkt9d07UXkx vfSOgozTDnUmff2z4AyCCH2v2yDbF0d+81W7ZtV8FNgE7JzYw9p2WcLi1mTUZbT/ttxb 4fsg== 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 t11-20020a63954b000000b004e0d688fb84si12552671pgn.847.2023.02.13.18.58.54; Mon, 13 Feb 2023 18:59:07 -0800 (PST) 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 S231260AbjBNC5N (ORCPT + 99 others); Mon, 13 Feb 2023 21:57:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230245AbjBNC45 (ORCPT ); Mon, 13 Feb 2023 21:56:57 -0500 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 136A018A80; Mon, 13 Feb 2023 18:56:55 -0800 (PST) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8Axbdp2+OpjclcAAA--.1148S3; Tue, 14 Feb 2023 10:56:54 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Axeb1w+OpjmZwyAA--.28802S13; Tue, 14 Feb 2023 10:56:54 +0800 (CST) From: Tianrui Zhao To: Paolo Bonzini Cc: Huacai Chen , WANG Xuerui , Greg Kroah-Hartman , loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Jens Axboe , Mark Brown , Alex Deucher Subject: [PATCH v1 11/24] LoongArch: KVM: Implement update VM id function Date: Tue, 14 Feb 2023 10:56:35 +0800 Message-Id: <20230214025648.1898508-12-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230214025648.1898508-1-zhaotianrui@loongson.cn> References: <20230214025648.1898508-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8Axeb1w+OpjmZwyAA--.28802S13 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjvJXoW7Aw1kJrW5GFWrWry3CF4rZrb_yoW8tFW8pr WxCrn5Xr4rXrnxC3sIq3Wvqr1Y93yrGF13XasrAa4Yyry7t3sFkrZYk3yDAFyxXr1rAryI qF1FyF4YkF1kA37anT9S1TB71UUUUj7qnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU bxxFc2x0x2IEx4CE42xK8VAvwI8IcIk0rVWrJVCq3wA2ocxC64kIII0Yj41l84x0c7CEw4 AK67xGY2AK021l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF 7I0E14v26r4UJVWxJr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aV CY1x0267AKxVW8Jr0_Cr1UM2kKe7AKxVWUAVWUtwAS0I0E0xvYzxvE52x082IY62kv0487 Mc804VCY07AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VCjz48v1s IEY20_WwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lc7CjxVAaw2AFwI0_ JF0_Jw1l42xK82IYc2Ij64vIr41l42xK82IY6x8ErcxFaVAv8VWrMxC20s026xCaFVCjc4 AY6r1j6r4UMxCIbckI1I0E14v26r126r1DMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCj r7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6x IIjxv20xvE14v26w1j6s0DMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UMIIF0xvE 42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8Jr0_Cr1UMIIF0xvEx4A2js IEc7CjxVAFwI0_Gr1j6F4UJbIYCTnIWIevJa73UjIFyTuYvj4RKpBTUUUUU X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757773611455596320?= X-GMAIL-MSGID: =?utf-8?q?1757773611455596320?= Implement kvm check vmid and update vmid, the vmid should be checked before vcpu enter guest. Signed-off-by: Tianrui Zhao --- arch/loongarch/kvm/vmid.c | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 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 000000000..82729968e --- /dev/null +++ b/arch/loongarch/kvm/vmid.c @@ -0,0 +1,64 @@ +// 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 & context->vpid_mask)) { + /* finish round of 64 bit loop */ + if (unlikely(!vpid)) + vpid = context->vpid_mask + 1; + + /* vpid 0 reserved for root */ + ++vpid; + + /* start new vpid cycle */ + kvm_flush_tlb_all(); + } + + context->vpid_cache = vpid; + vcpu->arch.vpid[cpu] = vpid; +} + +void _kvm_check_vmid(struct kvm_vcpu *vcpu, int cpu) +{ + struct kvm_context *context; + bool migrated; + unsigned long ver, old, vpid; + + /* + * 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[cpu] & ~context->vpid_mask; + old = context->vpid_cache & ~context->vpid_mask; + if (migrated || (ver != old)) { + _kvm_update_vpid(vcpu, cpu); + trace_kvm_vpid_change(vcpu, vcpu->arch.vpid[cpu]); + } + + /* Restore GSTAT(0x50).vpid */ + vpid = (vcpu->arch.vpid[cpu] & context->vpid_mask) + << CSR_GSTAT_GID_SHIFT; + change_csr_gstat(context->vpid_mask << CSR_GSTAT_GID_SHIFT, vpid); +}