From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C55F2C433DB for ; Tue, 16 Feb 2021 19:05:08 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8B28B64E64 for ; Tue, 16 Feb 2021 19:05:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8B28B64E64 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Subject:To:From: Message-ID:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=2juYY7YGosMZwMobgtKT4J/sltthVZGen3xgsRNuymo=; b=Q7ZBjgVW06asbmBSf9fQalKmO 2C42EqWjagpl+AlqNYeeWuANXaZqTAjLC1ssjcOQw96QesCY0xwa7OQzB/lUMBhnur3Jy7gVOzFAP Q/L/I6RJEswOLcHSGpEd8IM+Zv8glX6/DrhI0ucFlR8PE8Vcbx6DEApUVXmRCWHXB7LnH7cOQPRdu l9eyQWMKVD2A7GNxV6whDgfCrTz6ogvw5otVuNkSMHZ1Kh+1pRp4MzLOrqz01GO/BHuIFSH9TZMa3 ZJPcPdmOt+pPk5xuPhNfpdqei9tT0Mju9mD07sIxy1VujzSC8vo4xKAPiVohJ2hI++4QQ5WJyMBhJ zIb9U8waQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1lC5d3-0003sN-22; Tue, 16 Feb 2021 19:03:21 +0000 Received: from mail.kernel.org ([198.145.29.99]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1lC5d0-0003rt-Qc for linux-arm-kernel@lists.infradead.org; Tue, 16 Feb 2021 19:03:19 +0000 Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C8A7364E28; Tue, 16 Feb 2021 19:03:17 +0000 (UTC) Received: from 78.163-31-62.static.virginmediabusiness.co.uk ([62.31.163.78] helo=why.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94) (envelope-from ) id 1lC5cx-00ETSL-MA; Tue, 16 Feb 2021 19:03:15 +0000 Date: Tue, 16 Feb 2021 19:03:15 +0000 Message-ID: <87lfbnq1ho.wl-maz@kernel.org> From: Marc Zyngier To: Haibo Xu Subject: Re: [PATCH v3 63/66] KVM: arm64: nv: Allocate VNCR page when required In-Reply-To: References: <20201210160002.1407373-1-maz@kernel.org> <20201210160002.1407373-64-maz@kernel.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/27.1 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") X-SA-Exim-Connect-IP: 62.31.163.78 X-SA-Exim-Rcpt-To: haibo.xu@linaro.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, kernel-team@android.com, andre.przywara@arm.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210216_140319_037002_B6FB043F X-CRM114-Status: GOOD ( 31.87 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andre Przywara , kernel-team@android.com, kvmarm , arm-mail-list , kvm@vger.kernel.org Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Thu, 21 Jan 2021 02:47:45 +0000, Haibo Xu wrote: > > On Fri, 11 Dec 2020 at 00:04, Marc Zyngier wrote: > > > > If running a NV guest on an ARMv8.4-NV capable system, let's > > allocate an additional page that will be used by the hypervisor > > to fulfill system register accesses. > > > > Signed-off-by: Marc Zyngier > > --- > > arch/arm64/include/asm/kvm_host.h | 3 ++- > > arch/arm64/kvm/nested.c | 8 ++++++++ > > arch/arm64/kvm/reset.c | 1 + > > 3 files changed, 11 insertions(+), 1 deletion(-) > > > > diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h > > index 78630bd5124d..dada0678c28e 100644 > > --- a/arch/arm64/include/asm/kvm_host.h > > +++ b/arch/arm64/include/asm/kvm_host.h > > @@ -523,7 +523,8 @@ struct kvm_vcpu_arch { > > */ > > static inline u64 *__ctxt_sys_reg(const struct kvm_cpu_context *ctxt, int r) > > { > > - if (unlikely(r >= __VNCR_START__ && ctxt->vncr_array)) > > + if (unlikely(cpus_have_final_cap(ARM64_HAS_ENHANCED_NESTED_VIRT) && > > + r >= __VNCR_START__ && ctxt->vncr_array)) > > return &ctxt->vncr_array[r - __VNCR_START__]; > > > > return (u64 *)&ctxt->sys_regs[r]; > > diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c > > index eef8f9873814..88147ec99755 100644 > > --- a/arch/arm64/kvm/nested.c > > +++ b/arch/arm64/kvm/nested.c > > @@ -47,6 +47,12 @@ int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu) > > if (!cpus_have_final_cap(ARM64_HAS_NESTED_VIRT)) > > return -EINVAL; > > > > + if (cpus_have_final_cap(ARM64_HAS_ENHANCED_NESTED_VIRT)) { > > + vcpu->arch.ctxt.vncr_array = (u64 *)__get_free_page(GFP_KERNEL | __GFP_ZERO); > > + if (!vcpu->arch.ctxt.vncr_array) > > + return -ENOMEM; > > + } > > + > > If KVM_ARM_VCPU_INIT was called multiple times, the above codes > would try to allocate a new page without free-ing the previous > one. Besides that, the following kvm_free_stage2_pgd() call would I assume you mean kvm_init_stage2_mmu() here. > fail in the second call with the error message "kvm_arch already > initialized?". I think a possible fix is to add a new flag to > indicate whether the NV related meta data have been initialized, and > only initialize them for the first call. Good catch. But I think we have all the data we need at this stage to avoid this issue: diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index abb0669bdd4c..baff7373863f 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -390,7 +390,20 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu) int cpu, err; struct kvm_pgtable *pgt; + /* + * If we already have our page tables in place, and that the + * MMU context is the canonical one, we have a bug somewhere, + * as this is only supposed to ever happen once per VM. + * + * Otherwise, we're building nested page tables, and that's + * probably because userspace called KVM_ARM_VCPU_INIT more + * than once on the same vcpu. Since that's actually legal, + * don't kick a fuss and leave gracefully. + */ if (mmu->pgt != NULL) { + if (&kvm->arch.mmu != mmu) + return 0; + kvm_err("kvm_arch already initialized?\n"); return -EINVAL; } diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c index 88147ec99755..3b21ea57fbce 100644 --- a/arch/arm64/kvm/nested.c +++ b/arch/arm64/kvm/nested.c @@ -48,7 +48,9 @@ int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu) return -EINVAL; if (cpus_have_final_cap(ARM64_HAS_ENHANCED_NESTED_VIRT)) { - vcpu->arch.ctxt.vncr_array = (u64 *)__get_free_page(GFP_KERNEL | __GFP_ZERO); + if (!vcpu->arch.ctxt.vncr_array) + vcpu->arch.ctxt.vncr_array = (u64 *)__get_free_page(GFP_KERNEL | __GFP_ZERO); + if (!vcpu->arch.ctxt.vncr_array) return -ENOMEM; } Thanks, M. -- Without deviation from the norm, progress is not possible. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel