Re: [PATCH v2 1/3] KVM: arm64: Check PGD alignment when creating a pVM
From: Vincent Donnefort
Date: Wed Dec 02 2026 - 15:16:55 EST
On Thu, Nov 05, 2026 at 07:56:04AM +0000, Fuad Tabba wrote:
> From: Quentin Perret <qperret@xxxxxxxxxx>
>
> Martijn reported a hypervisor crash when providing pKVM with an
> undersized PGD allocation. Indeed, although the size of the PGD
> allocation at EL2 is not under host control, a smaller host-side
> allocation can lead to providing pKVM with a misaligned PGD, which will
> cause the guest stage-2 init to fail in a bad way. Specifically,
> guest_s2_zalloc_pages_exact() expects a successful allocation from
> hyp_alloc_pages(), which can only happen if the pool has been pre-filled
> with a physically aligned high-order page.
>
> In order to guarantee allocation success in this path, check the
> host-provided PGD alignment early on.
>
> Fixes: 60dfe093ec13 ("KVM: arm64: Instantiate guest stage-2 page-tables at EL2")
> Reported-by: Martijn Bogaard <martijnbogaard@xxxxxxxxxx>
> Signed-off-by: Quentin Perret <qperret@xxxxxxxxxx>
> [Fuad: Returned -EINVAL and moved the check ahead of hyp_alloc(), with
> the other input checks in __pkvm_init_vm(). Cast pgd_hva for its __kern
> type, updated the comment, and pointed Fixes: at the commit that
> initialises the guest's pool from the PGD.]
> Signed-off-by: Fuad Tabba <fuad.tabba@xxxxxxxxx>
> ---
> arch/arm64/kvm/hyp/nvhe/mem_protect.c | 2 +-
> arch/arm64/kvm/hyp/nvhe/pkvm.c | 8 ++++++--
> 2 files changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
> index d4c332a918035..5b624b1ab32e8 100644
> --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
> +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
> @@ -186,7 +186,7 @@ static void *guest_s2_zalloc_pages_exact(size_t size)
> {
> void *addr = hyp_alloc_pages(¤t_vm->pool, get_order(size));
>
> - WARN_ON(size != (PAGE_SIZE << get_order(size)));
> + WARN_ON(!addr || size != (PAGE_SIZE << get_order(size)));
Instead of adding to the WARN_ON, perhaps we could
if (!addr)
return NULL;
> hyp_split_page(hyp_virt_to_page(addr));
>
> return addr;
> diff --git a/arch/arm64/kvm/hyp/nvhe/pkvm.c b/arch/arm64/kvm/hyp/nvhe/pkvm.c
> index fc0475f0877d1..03e530e15b4a2 100644
> --- a/arch/arm64/kvm/hyp/nvhe/pkvm.c
> +++ b/arch/arm64/kvm/hyp/nvhe/pkvm.c
> @@ -892,8 +892,8 @@ void teardown_selftest_vm(void)
> *
> * host_kvm: A pointer to the host's struct kvm.
> * pgd_hva: The host va of the area being donated for the stage-2 PGD for
> - * the VM. Must be page aligned. Its size is implied by the VM's
> - * VTCR.
> + * the VM. Must be aligned to its size, which is implied by the
> + * VM's VTCR.
> *
> * Return 0 success, negative error code on failure.
> */
> @@ -924,6 +924,10 @@ int __pkvm_init_vm(struct kvm *host_kvm, void __kern *pgd_hva)
>
> vm_size = pkvm_get_hyp_vm_size(nr_vcpus);
> pgd_size = kvm_pgtable_stage2_pgd_size(host_mmu.arch.mmu.vtcr);
> + if (!IS_ALIGNED((unsigned long)pgd_hva, pgd_size)) {
> + ret = -EINVAL;
> + goto err_unpin_kvm;
> + }
With this, map_donated_memory_noclear() is really just calling
__pkvm_host_donate_hyp(). Could we remove it?
Also, I wonder if it isn't better to first have
pgd = kern_hyp_va(pgd_hva);
if (!IS_ALIGNED(pgd....))
to avoid casting a __kern annotated variable?
>
> hyp_vm = hyp_alloc(vm_size);
> if (!hyp_vm) {
> --
> 2.47.3
>
--
Vincent