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 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D9E0AC19F32 for ; Thu, 27 Feb 2025 12:48:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:MIME-Version: References:In-Reply-To:Subject:Cc:To:From:Message-ID:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=D9gQ8QQ+j0YJJPNk7G/EpsFa3BPvNvpsT4xXMpuUqzc=; b=f6276PkZwbeNMqUSDMFzTO1sSw CbRkk4hgrP8vwb2oDyla3/kl4Uwm5jYQ5NGYOSyaieR0uSptJR5Ig/8fcglRib9rW16nZTYPESqRZ uGCN4SuPpRm1bz1RSFuFJC91t1IeiQAzV6k7UjR4vSlet+9uEbD4hZVe50noUVyc1FEuL19sqW3TK FYuxSQSFF+VfnqN1QRN3mz/XEFnSYI1K7HF4n6DKLmSbeOd/IrPPIOrzt6Ek2j9FUp8CzIuMpjBlu a+5H6YKo8SP55fxbsEapj4Zyk/rmDNKhehVihFS7WwMvp4BgoHrc0b/vtUhseWY6IKt6lxp5HFa1Z AXBznz/Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tndJ1-00000007QFp-1aPx; Thu, 27 Feb 2025 12:47:59 +0000 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tnchn-00000007Ijy-2SIs for linux-arm-kernel@lists.infradead.org; Thu, 27 Feb 2025 12:09:32 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id EC5F75C04F0; Thu, 27 Feb 2025 12:08:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6A103C4CEDD; Thu, 27 Feb 2025 12:09:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1740658170; bh=G7OYmP83uCOxk0GNDtJTIP3KHR+x4Nusplzuy5PFMAA=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=crqBnHTM8g+VTtjkj+5N0k/zYdWsqQFODvA7dUA6hOXngQEn5Yl+uCIHncQVqEAaj URk5nSY+uffDGcCcRrZGKF15QQym/Zo3xtpQXiYqIQzkMoYRMo5phYolE1U2IC9NYl Y4xt6HIz/XoAuYvxvEYzWL9tUzIc21+VjyeKD6QxxElo60hSVtfMSbWwe8eF4i6Hv/ xDy5uE9HoURygUznlZYBCfW4j/bNIfNVYUV8mCIvw/tDR6t7AdbUi6knucGm2Hr8Fc nx2j7TRz34CaxSU+HNqZiVjWMApwgha7ryoqUUMTmOUTiQAoysxfvk0UAyj0b3ONmo CC1SQvKiUxYew== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1tnchi-008eDD-UQ; Thu, 27 Feb 2025 12:09:27 +0000 Date: Thu, 27 Feb 2025 12:09:26 +0000 Message-ID: <8634fzr3m1.wl-maz@kernel.org> From: Marc Zyngier To: Fuad Tabba Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, oliver.upton@linux.dev, mark.rutland@arm.com, will@kernel.org, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, broonie@kernel.org, qperret@google.com, vdonnefort@google.com Subject: Re: [PATCH v2 4/4] KVM: arm64: Create each pKVM hyp vcpu after its corresponding host vcpu In-Reply-To: <20250226215520.1759218-5-tabba@google.com> References: <20250226215520.1759218-1-tabba@google.com> <20250226215520.1759218-5-tabba@google.com> 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/29.4 (aarch64-unknown-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: 185.219.108.64 X-SA-Exim-Rcpt-To: tabba@google.com, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, oliver.upton@linux.dev, mark.rutland@arm.com, will@kernel.org, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, broonie@kernel.org, qperret@google.com, vdonnefort@google.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-20250227_040931_699799_5AE1371A X-CRM114-Status: GOOD ( 28.03 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Wed, 26 Feb 2025 21:55:20 +0000, Fuad Tabba wrote: > > Instead of creating and initializing _all_ hyp vcpus in pKVM when > the first host vcpu runs for the first time, initialize _each_ > hyp vcpu in conjunction with its corresponding host vcpu. > > Some of the host vcpu state (e.g., system registers and traps > values) is not initialized until the first time the host vcpu is > run. Therefore, initializing a hyp vcpu before its corresponding > host vcpu has run for the first time might not view the complete > host state of these vcpus. > > Additionally, this behavior is inline with non-protected modes. > > Signed-off-by: Fuad Tabba > --- > arch/arm64/include/asm/kvm_host.h | 2 + > arch/arm64/include/asm/kvm_pkvm.h | 1 + > arch/arm64/kvm/arm.c | 4 ++ > arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 6 --- > arch/arm64/kvm/hyp/nvhe/pkvm.c | 54 +++++++++++++++----------- > arch/arm64/kvm/pkvm.c | 28 ++++++------- > 6 files changed, 53 insertions(+), 42 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h > index 3a7ec98ef123..4a0e522f6001 100644 > --- a/arch/arm64/include/asm/kvm_host.h > +++ b/arch/arm64/include/asm/kvm_host.h > @@ -869,6 +869,8 @@ struct kvm_vcpu_arch { > #define VCPU_INITIALIZED __vcpu_single_flag(cflags, BIT(0)) > /* SVE config completed */ > #define VCPU_SVE_FINALIZED __vcpu_single_flag(cflags, BIT(1)) > +/* pKVM VCPU setup completed */ > +#define VCPU_PKVM_FINALIZED __vcpu_single_flag(cflags, BIT(2)) > > /* Exception pending */ > #define PENDING_EXCEPTION __vcpu_single_flag(iflags, BIT(0)) > diff --git a/arch/arm64/include/asm/kvm_pkvm.h b/arch/arm64/include/asm/kvm_pkvm.h > index eb65f12e81d9..abd693ce5b93 100644 > --- a/arch/arm64/include/asm/kvm_pkvm.h > +++ b/arch/arm64/include/asm/kvm_pkvm.h > @@ -19,6 +19,7 @@ > int pkvm_init_host_vm(struct kvm *kvm); > int pkvm_create_hyp_vm(struct kvm *kvm); > void pkvm_destroy_hyp_vm(struct kvm *kvm); > +int pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu); > > /* > * This functions as an allow-list of protected VM capabilities. > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index b8e55a441282..cd7a808eeb64 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -833,6 +833,10 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) > ret = pkvm_create_hyp_vm(kvm); > if (ret) > return ret; > + > + ret = pkvm_create_hyp_vcpu(vcpu); > + if (ret) > + return ret; > } > > mutex_lock(&kvm->arch.config_lock); > diff --git a/arch/arm64/kvm/hyp/include/nvhe/pkvm.h b/arch/arm64/kvm/hyp/include/nvhe/pkvm.h > index e42bf68c8848..ce31d3b73603 100644 > --- a/arch/arm64/kvm/hyp/include/nvhe/pkvm.h > +++ b/arch/arm64/kvm/hyp/include/nvhe/pkvm.h > @@ -43,12 +43,6 @@ struct pkvm_hyp_vm { > struct hyp_pool pool; > hyp_spinlock_t lock; > > - /* > - * The number of vcpus initialized and ready to run. > - * Modifying this is protected by 'vm_table_lock'. > - */ > - unsigned int nr_vcpus; > - > /* Array of the hyp vCPU structures for this VM. */ > struct pkvm_hyp_vcpu *vcpus[]; > }; > diff --git a/arch/arm64/kvm/hyp/nvhe/pkvm.c b/arch/arm64/kvm/hyp/nvhe/pkvm.c > index 6efb9bf56180..4ef3748dc660 100644 > --- a/arch/arm64/kvm/hyp/nvhe/pkvm.c > +++ b/arch/arm64/kvm/hyp/nvhe/pkvm.c > @@ -245,10 +245,12 @@ struct pkvm_hyp_vcpu *pkvm_load_hyp_vcpu(pkvm_handle_t handle, > > hyp_spin_lock(&vm_table_lock); > hyp_vm = get_vm_by_handle(handle); > - if (!hyp_vm || hyp_vm->nr_vcpus <= vcpu_idx) > + if (!hyp_vm || hyp_vm->kvm.created_vcpus <= vcpu_idx) Why is it reasonable to trust kvm.created_vcpus here? It looks like something that is under direct control of the host, and that could be arbitrarily changed to allow dereferencing the vcpus[] array past its actual boundaries. What guarantees that it is stable at load-time? I have similar misgivings about the init and teardown paths. Thanks, M. -- Without deviation from the norm, progress is not possible.