From: Suzuki K Poulose The upper half of a Realm's IPA space aliases its lower, protected half. Restrict memslots to the lower half so a memslot cannot be created in the shared alias. Keep the existing post-creation move/delete restriction scoped to pKVM. Protected VMs, including Realms, continue to reject dirty logging and read-only memslots. Signed-off-by: Suzuki K Poulose Signed-off-by: Steven Price --- Changes since v4: - Remove the Realm guest_memfd memslot change restriction; private Realm faults now validate guest_memfd backing and attributes. - Split out private-memory support and drop the unused early memslot validation hook. Changes since v3: - Explain the arm64-wide in-place conversion effect and keep the general IPA limit comment. Changes since v1: - Preserve private mappings if KVM invalidates a Realm guest_memfd slot before the prepare hook rejects its deletion. --- arch/arm64/kvm/mmu.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index c2cb231ea65b..79d9eeee9f32 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -2860,11 +2860,13 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, enum kvm_mr_change change) { hva_t hva, reg_end; + gfn_t ipa_limit; int ret = 0; if (kvm_vm_is_protected(kvm)) { /* Cannot modify memslots once a pVM has run. */ - if (pkvm_hyp_vm_is_created(kvm) && + if (kvm->arch.vm_flavor == VM_PROTECTED_PKVM && + pkvm_hyp_vm_is_created(kvm) && (change == KVM_MR_DELETE || change == KVM_MR_MOVE)) { return -EPERM; } @@ -2881,9 +2883,14 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, /* * Prevent userspace from creating a memory region outside of the IPA - * space addressable by the KVM guest IPA space. + * space addressable by the KVM guest IPA space. Realm shared IPAs alias + * the corresponding private IPAs, so Realm memslots must reside entirely + * in the lower half of that space. */ - if ((new->base_gfn + new->npages) > (kvm_phys_size(&kvm->arch.mmu) >> PAGE_SHIFT)) + ipa_limit = kvm_phys_size(&kvm->arch.mmu) >> PAGE_SHIFT; + if (kvm_vm_is_realm(kvm)) + ipa_limit >>= 1; + if (new->base_gfn + new->npages > ipa_limit) return -EFAULT; /* guest_memfd is incompatible with MTE. */ -- 2.43.0