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,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 366FCC433E0 for ; Tue, 16 Feb 2021 19:04:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 09EAE64E64 for ; Tue, 16 Feb 2021 19:04:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229802AbhBPTD7 (ORCPT ); Tue, 16 Feb 2021 14:03:59 -0500 Received: from mail.kernel.org ([198.145.29.99]:49132 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229628AbhBPTD7 (ORCPT ); Tue, 16 Feb 2021 14:03:59 -0500 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 Cc: arm-mail-list , kvmarm , kvm@vger.kernel.org, kernel-team@android.com, Andre Przywara 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") Content-Type: text/plain; charset=US-ASCII 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 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.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.