From patchwork Wed Apr 19 22:16:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 85601 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp706202vqo; Wed, 19 Apr 2023 15:36:44 -0700 (PDT) X-Google-Smtp-Source: AKy350bFfpnPxj4O/8RLETQVgmkNFv5BDcWs3t38+fvuaRtz4Dq6AZcI3PNfVrZXtFM6xom7P96N X-Received: by 2002:a17:90b:3848:b0:247:6ec8:7e9b with SMTP id nl8-20020a17090b384800b002476ec87e9bmr4113919pjb.44.1681943804149; Wed, 19 Apr 2023 15:36:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681943804; cv=none; d=google.com; s=arc-20160816; b=HV3i8CF21uTLY2eO5nPzuX7UoIT5c7H4jKUQnZtir/zjdXVw5ncmgGNnxB7AAYCy7G ogT/8OgUqNiU1OVAY5Rk3G5FxOO5n5RRswC1ZEXW7m/H2K8jYWy5UbXfKQMtw1t/ExxA zStIoxSNSovcx3rI357X5o8MwePenCiHxf5iDu6G38ZTOpE2w4Ra8Gjp9IGUQjDDHAej Q/mz2C2PQXkrVaioC0TDfmoxUS16iQj0UperQEq3dVTDJZDAIo6jddDSdOYiO5Xg7hPT gNEga359gXHCDEUkCV3lpJPtGaltVrTFnUInPX6XIU6dVZH3haEZfTzET3bs52R7e7Q5 w4xg== 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 :dkim-signature; bh=IHi27AH8EgCkCQBErjcLInO9Ogd99Uht/0Y2I/rPxr0=; b=sJQo87qVYAGkygrcj+OwI+OqN19ixIk1+ehdpUevyLYjL3gmfbDz3cbeVYvSZUE6VF fuWnTbuDTPGG0E9/+8828SrnyVcvCQv09sPIfMOo2JG8pxMDIf44w3GRHV7HUcJcWZ6c CDGcrwfidaZmOIK+QPT0PeljDXCC8yZSJmnFRSH1H/zXUb+kbMIs+KtAUJV29aM8Jo6L ZaMKdBTLpwCD9cIHa7NVgCw7Vvt52BqC3Dt6CHQ4WqXuQsgbPYdrF8bKG8XgqSzMYmrl 9NhoKpLS4UOu6w8Hxn8oKFw2z6773oeChuy5yarCt0AwPOKk3O/QAlbEcIMNSIud8BTz 4+Pg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=YSIfA3fB; 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 oj17-20020a17090b4d9100b002372f2c4d9dsi2704731pjb.44.2023.04.19.15.35.58; Wed, 19 Apr 2023 15:36:44 -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; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=YSIfA3fB; 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 S230331AbjDSWT0 (ORCPT + 99 others); Wed, 19 Apr 2023 18:19:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232345AbjDSWSe (ORCPT ); Wed, 19 Apr 2023 18:18:34 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66551902E for ; Wed, 19 Apr 2023 15:18:15 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1a5197f00e9so4659295ad.1 for ; Wed, 19 Apr 2023 15:18:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942687; x=1684534687; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IHi27AH8EgCkCQBErjcLInO9Ogd99Uht/0Y2I/rPxr0=; b=YSIfA3fBHehSI9sfNw8kNY3SjYw3MKpDND587n51K3eoVf8RJTCG1iSaqqG9yfNjCx jwqPpoi/ITD0shW0NUnRyZli8JT8h1t3/2gNbp76WuPUO0CZ5s8zSvoyiHQFyjfIMwOE 1ZldJnGMaXcDrIoP6ErfdR2UHM3fNFztr08TXf0xVGRFo5tk0moUvzoYm/r0hkRFoy0K cWLNwCIHBfTQ0bCQR3c38U1nAB1slt1q42rqYoamEUYp5QQ79+wtILJqFzbQlYA06O6P OEHwUmVOCGHeSnw6bqeEZSZ4KjU2xfgwWJGo24N5J96VrnqV0nME7zdNFrLcXHLPuPgD RkSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942687; x=1684534687; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IHi27AH8EgCkCQBErjcLInO9Ogd99Uht/0Y2I/rPxr0=; b=ZA0WmcfAqPamOM1suxHG5HX6IcC1S/0mHQhKxefzeqeXaEfMU5vYkXcsQdVE65BJwm izhqU7++LrplXjNNZPcksdaI80LF8ZYDyf9jNU+u6GI/wqC5pRGoYkHGTTmtA/JZ94EO uUiEDfVc14em15xSnxj2F/sP7nq5xN5SkynJbflCkb45008eoOoOMN05rocEiCfqH+D3 55eWAREkE5H8Zu2ollobz4qOXdHTU72vlZMKl9NgbTlJ3F8UCWtq9ysJJo+j2yAZiwU8 Y15uU3pT5ig15S0jmKVxpD5tT4SKVKAt6ih8tb61JGAOEGaHRiRRQovoT73sSX4138rh rnnA== X-Gm-Message-State: AAQBX9cdWxnn/kzURviMSp0SZfM88FDIG1kqBLTInsK6PxCRwTHKQV2m 18Qt4jPEORgSQXDsbStgnp7WU+4uLvGpw4akZ68= X-Received: by 2002:a17:902:ec8b:b0:1a9:23b7:9182 with SMTP id x11-20020a170902ec8b00b001a923b79182mr2674160plg.27.1681942686991; Wed, 19 Apr 2023 15:18:06 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id jn11-20020a170903050b00b00196807b5189sm11619190plb.292.2023.04.19.15.18.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:06 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Suzuki K Poulose , Will Deacon , Marc Zyngier , Sean Christopherson , linux-coco@lists.linux.dev, Dylan Reid , abrestic@rivosinc.com, Samuel Ortiz , Christoph Hellwig , Conor Dooley , Greg Kroah-Hartman , Guo Ren , Heiko Stuebner , Jiri Slaby , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, Mayuresh Chitale , Palmer Dabbelt , Paolo Bonzini , Paul Walmsley , Rajnesh Kanwal , Uladzislau Rezki Subject: [RFC 17/48] RISC-V : KVM: Skip vmid/hgatp management for TVMs Date: Wed, 19 Apr 2023 15:16:45 -0700 Message-Id: <20230419221716.3603068-18-atishp@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230419221716.3603068-1-atishp@rivosinc.com> References: <20230419221716.3603068-1-atishp@rivosinc.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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?1763645906340171310?= X-GMAIL-MSGID: =?utf-8?q?1763645906340171310?= The TSM manages the vmid for the guests running in CoVE. The host doesn't need to update vmid at all. As a result, the host doesn't need to update the hgatp as well. Return early for vmid/hgatp management functions for confidential guests. Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_host.h | 2 +- arch/riscv/kvm/mmu.c | 4 ++++ arch/riscv/kvm/vcpu.c | 2 +- arch/riscv/kvm/vmid.c | 17 ++++++++++++----- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h index ca2ebe3..047e046 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -325,7 +325,7 @@ unsigned long kvm_riscv_gstage_pgd_size(void); void __init kvm_riscv_gstage_vmid_detect(void); unsigned long kvm_riscv_gstage_vmid_bits(void); int kvm_riscv_gstage_vmid_init(struct kvm *kvm); -bool kvm_riscv_gstage_vmid_ver_changed(struct kvm_vmid *vmid); +bool kvm_riscv_gstage_vmid_ver_changed(struct kvm *kvm); void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu); int kvm_riscv_setup_default_irq_routing(struct kvm *kvm, u32 lines); diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index 1d5e4ed..4b0f09e 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -778,6 +778,10 @@ void kvm_riscv_gstage_update_hgatp(struct kvm_vcpu *vcpu) unsigned long hgatp = gstage_mode; struct kvm_arch *k = &vcpu->kvm->arch; + /* COVE VCPU hgatp is managed by TSM. */ + if (is_cove_vcpu(vcpu)) + return; + hgatp |= (READ_ONCE(k->vmid.vmid) << HGATP_VMID_SHIFT) & HGATP_VMID; hgatp |= (k->pgd_phys >> PAGE_SHIFT) & HGATP_PPN; diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 3b600c6..8cf462c 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -1288,7 +1288,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) kvm_riscv_update_hvip(vcpu); if (ret <= 0 || - kvm_riscv_gstage_vmid_ver_changed(&vcpu->kvm->arch.vmid) || + kvm_riscv_gstage_vmid_ver_changed(vcpu->kvm) || kvm_request_pending(vcpu) || xfer_to_guest_mode_work_pending()) { vcpu->mode = OUTSIDE_GUEST_MODE; diff --git a/arch/riscv/kvm/vmid.c b/arch/riscv/kvm/vmid.c index ddc9871..dc03601 100644 --- a/arch/riscv/kvm/vmid.c +++ b/arch/riscv/kvm/vmid.c @@ -14,6 +14,7 @@ #include #include #include +#include static unsigned long vmid_version = 1; static unsigned long vmid_next; @@ -54,12 +55,13 @@ int kvm_riscv_gstage_vmid_init(struct kvm *kvm) return 0; } -bool kvm_riscv_gstage_vmid_ver_changed(struct kvm_vmid *vmid) +bool kvm_riscv_gstage_vmid_ver_changed(struct kvm *kvm) { - if (!vmid_bits) + /* VMID version can't be changed by the host for TVMs */ + if (!vmid_bits || is_cove_vm(kvm)) return false; - return unlikely(READ_ONCE(vmid->vmid_version) != + return unlikely(READ_ONCE(kvm->arch.vmid.vmid_version) != READ_ONCE(vmid_version)); } @@ -72,9 +74,14 @@ void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu) { unsigned long i; struct kvm_vcpu *v; + struct kvm *kvm = vcpu->kvm; struct kvm_vmid *vmid = &vcpu->kvm->arch.vmid; - if (!kvm_riscv_gstage_vmid_ver_changed(vmid)) + /* No VMID management for TVMs by the host */ + if (is_cove_vcpu(vcpu)) + return; + + if (!kvm_riscv_gstage_vmid_ver_changed(kvm)) return; spin_lock(&vmid_lock); @@ -83,7 +90,7 @@ void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu) * We need to re-check the vmid_version here to ensure that if * another vcpu already allocated a valid vmid for this vm. */ - if (!kvm_riscv_gstage_vmid_ver_changed(vmid)) { + if (!kvm_riscv_gstage_vmid_ver_changed(kvm)) { spin_unlock(&vmid_lock); return; }