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 4508DC282C5 for ; Thu, 27 Feb 2025 19:37:40 +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=hXMzX7d145b6AtZkRBKLZBuN8t7mUmHOlSJMWDttmc0=; b=Vqp7SXFqjEcaSw0uF+Jagq4zPP Fco4Bo5TBI0q6zSHgiycbNsIFmZVj5M2n1KMCnqnrTiz6ydkYeE4A/3R3KG6FzHQJcc0J3URheJ+9 gdUREI88yxXglAIYjx0ZpzCbkxFlSRgZoAUydUnKF4bE5QwL9A9937FN36A1twCz8ZXyGAycvy/aJ raeYUC7yVyJBl2LoXglINYKmAn+DyojuHqMshPWFPsxxjndgRsciPisQKqJjz3K9Bb0d5V1nQRAb8 zFK5Q9teUN3mmUQoNyr1OGylwQysSLdueMUxBNSsv5xlEWwgVcVt8Y6maVGsMyGXu+Y94UYLzt/Lk zxSElLyg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tnjgy-00000008Td8-3eOv; Thu, 27 Feb 2025 19:37:08 +0000 Received: from dfw.source.kernel.org ([139.178.84.217]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tnedv-00000007ftP-1VKR for linux-arm-kernel@lists.infradead.org; Thu, 27 Feb 2025 14:13:40 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 8FB6C5C0622; Thu, 27 Feb 2025 14:12:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0C563C4CEE6; Thu, 27 Feb 2025 14:13:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1740665618; bh=+WL4i4M70cixQtfVOqasrx2YZsATDRdbQJ50oP28uBg=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=X0zwJItoqRhi7lEJY4Q2GanbVK3A1fJrvOSGFGQwK/JLVHGsVjp3UkJjExYZO0N9i f9BQr9/vf/pcmxccOqna+/XerooPb64600s5kshTujVP7UWT1RbbNtx2kkoPrIUOqg AaWhRhoyCZ+fU03cinWE9fAdUfnrTOxDu0z7QIGMy6fH+Jaa80mzzGPDBnCzmKc4OM VxvEi6JRPLrtebBE0OipCxip5XjXzbG4WHSnCW3dTNYtRhHNzG0rbxmVWG1ZAegHr/ mXXq5iLD+no+l/lny1xAIO/zkxYcjYNf9K8jE38n8YoexZTFFNg19E5Jj8vuJr0Ms0 fdQe4quJQRRVA== 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 1tnedr-008gqC-Mz; Thu, 27 Feb 2025 14:13:35 +0000 Date: Thu, 27 Feb 2025 14:13:34 +0000 Message-ID: <861pvjqxv5.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: References: <20250226215520.1759218-1-tabba@google.com> <20250226215520.1759218-5-tabba@google.com> <8634fzr3m1.wl-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/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_061339_575404_E36EE98D X-CRM114-Status: GOOD ( 38.44 ) 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 Thu, 27 Feb 2025 12:47:16 +0000, Fuad Tabba wrote: > > Hi Marc, > > On Thu, 27 Feb 2025 at 04:09, Marc Zyngier wrote: > > > > 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. > > This is the hypervisor's view of created_vcpus > (hyp_vm->kvm.created_vcpus), i.e., not the host's (not > host_kvm->created_vcpus). We read it once from the host at > __pkvm_init_vm(). We should know that it wouldn't overflow the vcpus > array since it's the value of created_vcpus (nr_vcpus) we use when we > calculate the vm_size in __pkvm_init_vm(), and try to map the > corresponding amount of donated memory for the hyp_vm (all in > __pkvm_init_vm()). > > Since it's all hyp values, it should be fine, right? Ah, I got confused between the host_kvm and kvm fields. Apologies for the noise, this looks fine indeed. M. -- Without deviation from the norm, progress is not possible.