From: Suzuki K Poulose Once the Realm is created, prevent changes to guest_memfd backed memslots, which can provide private memory. Validate these changes before KVM invalidates the old memslot, so a rejected deletion leaves its stage-2 mappings intact. Realm stage-2 faults will also validate the private memory backing in a later commit. Require Realm memslots to lie in the lower half of the IPA space, as the upper, shared half aliases the lower, protected half. Define kvm_arch_has_private_mem() so generic KVM recognizes Realm VMs as supporting private memory. Defining the macro also makes gmem_in_place_conversion true for every arm64 VM, so guest_memfd memslots are marked gmem-only even for non-Realm VMs. A well-formed KVM_SET_MEMORY_ATTRIBUTES2 request on a non-Realm guest_memfd now reaches the VM check and returns -EINVAL instead of -ENOTTY. Additionally, similar to pKVM, do not support dirty logging or read-only memslots. Signed-off-by: Suzuki K Poulose Signed-off-by: Steven Price --- 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/include/asm/kvm_host.h | 2 ++ arch/arm64/kvm/mmu.c | 37 ++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 3ddeddfacd06..55673d08e3eb 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -1607,6 +1607,8 @@ struct kvm *kvm_arch_alloc_vm(void); #define kvm_vm_is_realm(kvm) ((kvm)->arch.vm_flavor == VM_REALM) +#define kvm_arch_has_private_mem(kvm) kvm_vm_is_realm(kvm) + #define kvm_has_mte(kvm) \ (system_supports_mte() && \ test_bit(KVM_ARCH_FLAG_MTE_ENABLED, &(kvm)->arch.flags)) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 800333a345cd..9af5fe6d87f0 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -2861,17 +2861,32 @@ int kvm_arch_validate_memory_region(struct kvm *kvm, { lockdep_assert_held(&kvm->slots_lock); - if (kvm_vm_is_protected(kvm)) { + if (kvm_vm_is_protected(kvm) && new && + (new->flags & (KVM_MEM_LOG_DIRTY_PAGES | KVM_MEM_READONLY))) + return -EPERM; + + switch (kvm->arch.vm_flavor) { + case VM_PROTECTED_PKVM: /* Cannot modify memslots once a pVM has run. */ if (pkvm_hyp_vm_is_created(kvm) && - (change == KVM_MR_DELETE || change == KVM_MR_MOVE)) { + (change == KVM_MR_DELETE || change == KVM_MR_MOVE)) return -EPERM; - } + break; + case VM_REALM: + /* + * Once the Realm is created, we cannot modify any slots that + * could be providing private memory. i.e., guest_memfd backed + * slots. + * TODO: Handle trusted device private memory slots + */ + if (!kvm_realm_is_created(kvm)) + break; - if (new && - new->flags & (KVM_MEM_LOG_DIRTY_PAGES | KVM_MEM_READONLY)) { + if (kvm_slot_has_gmem(old) || kvm_slot_has_gmem(new)) return -EPERM; - } + break; + default: + break; } return 0; @@ -2883,6 +2898,7 @@ 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 (change != KVM_MR_CREATE && change != KVM_MR_MOVE && @@ -2891,9 +2907,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; /* -- 2.43.0