Check the guest_memfd backing file when deciding whether an arm64 memslot has mappable memory rather than checking whether the slot is gmem only. A public helper function is added to be able to reuse the static kvm_gmem_supports_mmap() function. Signed-off-by: Steven Price --- Changes since v1: - New patch in v2. Check the guest_memfd backing file rather than the memslot flag when deciding whether arm64 can map the backing memory. --- arch/arm64/kvm/mmu.c | 7 ++++--- include/linux/kvm_host.h | 1 + virt/kvm/guest_memfd.c | 6 ++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 1db10d8d6db7..a146f6cde3d0 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -2839,10 +2839,11 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, return -EFAULT; /* - * Only support guest_memfd backed memslots with mappable memory, since - * there aren't any CoCo VMs that support only private memory on arm64. + * Protected VMs currently require guest_memfd backed memslots with + * mappable memory to support shared mappings. */ - if (kvm_slot_has_gmem(new) && !kvm_memslot_is_gmem_only(new)) + if (kvm_arch_has_private_mem(kvm) && kvm_slot_has_gmem(new) && + !kvm_gmem_slot_supports_mmap(new)) return -EINVAL; /* guest_memfd is incompatible with MTE. */ diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index e7fc94e2910a..f65728a713ec 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2614,6 +2614,7 @@ static inline bool kvm_is_private_gfn(struct kvm *kvm, gfn_t gfn) #ifdef CONFIG_KVM_GUEST_MEMFD bool kvm_gmem_is_private_gfn(struct kvm *kvm, gfn_t gfn); +bool kvm_gmem_slot_supports_mmap(const struct kvm_memory_slot *slot); bool kvm_arch_supports_gmem_init_shared(struct kvm *kvm); static inline u64 kvm_gmem_get_supported_flags(struct kvm *kvm) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 8c97a69b16dc..3dd8d227bebb 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -442,6 +442,12 @@ static bool kvm_gmem_supports_mmap(struct inode *inode) return GMEM_I(inode)->flags & GUEST_MEMFD_FLAG_MMAP; } +bool kvm_gmem_slot_supports_mmap(const struct kvm_memory_slot *slot) +{ + /* The guest_memfd file is pinned while preparing a new memslot. */ + return kvm_gmem_supports_mmap(file_inode(slot->gmem.file)); +} + static vm_fault_t kvm_gmem_fault_user_mapping(struct vm_fault *vmf) { struct inode *inode = file_inode(vmf->vma->vm_file); -- 2.43.0