From patchwork Mon Feb 20 06:57:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhaotianrui X-Patchwork-Id: 59275 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1166078wrn; Sun, 19 Feb 2023 23:00:00 -0800 (PST) X-Google-Smtp-Source: AK7set9U0kaHySSz/dsYCtkI9TtlliRz9Y9bWEv6kVL83nLz4mJYpgoWE4PxiKU75+Yeuy+/a6++ X-Received: by 2002:a17:902:c40d:b0:19a:8284:83a2 with SMTP id k13-20020a170902c40d00b0019a828483a2mr1331057plk.10.1676876400610; Sun, 19 Feb 2023 23:00:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676876400; cv=none; d=google.com; s=arc-20160816; b=urNJM0RcwsMZUxw9NW7EqZoEmicOSivkFcKSYnXU1Cr5xbF5nVn389K2TevpHZ/XgC NmgpuRPW7WfxXhbWXVBzO+txhOf0Vj8w0QBzelofPaApBajsv4MnIk5WW6QeH+o8INtQ QZQewouXcJAVAuAm9ndRvju+z6Sb81r6YRtURKl737ocjKTD87mGJ1F8qk3ZvEubLqAP WpmVlwOxb9+G+ClAM5Cs9XCaDrQblYqwNrr5I55lGR6+YHMIL6m29z+YCW3lycTI0MiN +BjPKZC8PFbYqt9SmZ4rpET9mDUJAIUnrsCAmmOFafra220wbLBpghbunqCaO5iVY0Td qx/Q== 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=kzvYgShvQDPLq5r9MPv28xUJvcoShRB4gPJh+Hkjq+Z4MUDX1tlws26u4AM/Es3o4X iwUiSAtBtJw2LCJ62RXuqGx023hZZstXRXQF77eCeAFjwUp57v60Lq6wlK82UYgn0jso GoGM0NlcVSE8Ls2BURU/PgAyU/u/67KuNKcyDr192ZG4Vxy+9+9RxgTZSu3OMI5Fmixr qmmSNSRduUOC+h73997FpK85S0XJ5aOTzWZhva2sZNgJEDTfXDFC6AOx1zAFzyOf1brm rZBPR1JPZgIEplKNKqGql5Xy/WNGmMwgzIcN/M86OUSlBmjrcNEuVHvzF0tnb/D5tMSy o8qw== 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 k3-20020a170902ba8300b0019b009d5d60si4434985pls.134.2023.02.19.22.59.48; Sun, 19 Feb 2023 23:00:00 -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 S231179AbjBTG6M (ORCPT + 99 others); Mon, 20 Feb 2023 01:58:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230337AbjBTG5p (ORCPT ); Mon, 20 Feb 2023 01:57:45 -0500 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B3CFDF75B; Sun, 19 Feb 2023 22:57:43 -0800 (PST) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8Dxj83lGfNjqLQCAA--.56S3; Mon, 20 Feb 2023 14:57:41 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Ax+73fGfNjFvk2AA--.34690S18; Mon, 20 Feb 2023 14:57:41 +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 , Oliver Upton , maobibo@loongson.cn Subject: [PATCH v2 16/29] LoongArch: KVM: Implement update VM id function Date: Mon, 20 Feb 2023 14:57:22 +0800 Message-Id: <20230220065735.1282809-17-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230220065735.1282809-1-zhaotianrui@loongson.cn> References: <20230220065735.1282809-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8Ax+73fGfNjFvk2AA--.34690S18 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjvJXoW7Aw1kJrW5GFWrWry3CF4rZrb_yoW8tFW8pr WxCrn5Xr4rXrnxC3sIq3Wvqr1Y93yrGF13XasrAa4Yyry7t3sFkrZYk3yDAFyxXr1rAryI qF1FyF4YkF1kA37anT9S1TB71UUUUjJqnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU bx8Fc2x0x2IEx4CE42xK8VAvwI8IcIk0rVWrJVCq3wA2ocxC64kIII0Yj41l84x0c7CEw4 AK67xGY2AK021l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF 7I0E14v26r4UJVWxJr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aV CY1x0267AKxVWxJr0_GcWln4kS14v26r126r1DM2AIxVAIcxkEcVAq07x20xvEncxIr21l 57IF6xkI12xvs2x26I8E6xACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x8ErcxFaV Av8VWrMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwCY1x0262kKe7AKxVWU AVWUtwCF04k20xvY0x0EwIxGrwCF04k20xvE74AGY7Cv6cx26rWl4I8I3I0E4IkC6x0Yz7 v_Jr0_Gr1l4IxYO2xFxVAFwI0_JF0_Jw1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8G jcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2I x0cI8IcVAFwI0_tr0E3s1lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4UJVWxJr1lIxAIcVCF 04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r4UJVWxJr1lIxAIcVC2z280aV CY1x0267AKxVW8Jr0_Cr1UYxBIdaVFxhVjvjDU0xZFpf9x0zR9iSdUUUUU= 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?1758332348355528038?= X-GMAIL-MSGID: =?utf-8?q?1758332348355528038?= 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); +}