KVM_CAP_GMEM_CONVERSION indicates that guest_memfd supports conversion. With this patch, as long as guest_memfd supports shared memory, it also supports conversion. With conversion support comes tracking of private/shared memory within guest_memfd, hence now all VM types support shared memory in guest_memfd. Before this patch, Coco VMs did not support shared memory because that would allow private memory to be accessible to the host. Coco VMs now support shared memory because with private/shared status tracked in guest_memfd, private memory will not be allowed to be mapped into the host. Change-Id: I057b7bd267dd84a93fdee2e95cceb88cd9dfc647 Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx> --- arch/arm64/include/asm/kvm_host.h | 5 ----- arch/x86/include/asm/kvm_host.h | 10 ---------- include/linux/kvm_host.h | 13 ------------- include/uapi/linux/kvm.h | 1 + virt/kvm/guest_memfd.c | 12 ++++-------- virt/kvm/kvm_main.c | 3 ++- 6 files changed, 7 insertions(+), 37 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 2514779f5131..7df673a71ade 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -1598,9 +1598,4 @@ static inline bool kvm_arch_supports_gmem(struct kvm *kvm) return IS_ENABLED(CONFIG_KVM_GMEM); } -static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm) -{ - return IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM); -} - #endif /* __ARM64_KVM_HOST_H__ */ diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index f72722949cae..709cc2a7ba66 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2255,18 +2255,8 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level, #ifdef CONFIG_KVM_GMEM #define kvm_arch_supports_gmem(kvm) ((kvm)->arch.supports_gmem) - -/* - * CoCo VMs with hardware support that use guest_memfd only for backing private - * memory, e.g., TDX, cannot use guest_memfd with userspace mapping enabled. - */ -#define kvm_arch_vm_supports_gmem_shared_mem(kvm) \ - (IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM) && \ - ((kvm)->arch.vm_type == KVM_X86_SW_PROTECTED_VM || \ - (kvm)->arch.vm_type == KVM_X86_DEFAULT_VM)) #else #define kvm_arch_supports_gmem(kvm) false -#define kvm_arch_vm_supports_gmem_shared_mem(kvm) false #endif #define kvm_arch_has_readonly_mem(kvm) (!(kvm)->arch.has_protected_state) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 91279e05e010..d703f291f467 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -729,19 +729,6 @@ static inline bool kvm_arch_supports_gmem(struct kvm *kvm) } #endif -/* - * Returns true if this VM supports shared mem in guest_memfd. - * - * Arch code must define kvm_arch_vm_supports_gmem_shared_mem if support for - * guest_memfd is enabled. - */ -#if !defined(kvm_arch_vm_supports_gmem_shared_mem) && !IS_ENABLED(CONFIG_KVM_GMEM) -static inline bool kvm_arch_vm_supports_gmem_shared_mem(struct kvm *kvm) -{ - return false; -} -#endif - #ifndef kvm_arch_has_readonly_mem static inline bool kvm_arch_has_readonly_mem(struct kvm *kvm) { diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 5b28e17f6f14..433e184f83ea 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -931,6 +931,7 @@ struct kvm_enable_cap { #define KVM_CAP_X86_GUEST_MODE 238 #define KVM_CAP_ARM_WRITABLE_IMP_ID_REGS 239 #define KVM_CAP_GMEM_SHARED_MEM 240 +#define KVM_CAP_GMEM_CONVERSION 241 struct kvm_irq_routing_irqchip { __u32 irqchip; diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 853e989bdcb2..8c9c9e54616b 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -1216,7 +1216,7 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) u64 flags = args->flags; u64 valid_flags = 0; - if (kvm_arch_vm_supports_gmem_shared_mem(kvm)) + if (IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM)) valid_flags |= GUEST_MEMFD_FLAG_SUPPORT_SHARED; if (flags & GUEST_MEMFD_FLAG_SUPPORT_SHARED) @@ -1286,13 +1286,9 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, offset + size > i_size_read(inode)) goto err; - if (kvm_gmem_supports_shared(inode)) { - if (!kvm_arch_vm_supports_gmem_shared_mem(kvm)) - goto err; - - if (slot->userspace_addr && - !kvm_gmem_is_same_range(kvm, slot, file, offset)) - goto err; + if (kvm_gmem_supports_shared(inode) && slot->userspace_addr && + !kvm_gmem_is_same_range(kvm, slot, file, offset)) { + goto err; } filemap_invalidate_lock(inode->i_mapping); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 66dfdafbb3b6..92054b1bbd3f 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4843,7 +4843,8 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) #endif #ifdef CONFIG_KVM_GMEM_SHARED_MEM case KVM_CAP_GMEM_SHARED_MEM: - return !kvm || kvm_arch_vm_supports_gmem_shared_mem(kvm); + case KVM_CAP_GMEM_CONVERSION: + return true; #endif default: break; -- 2.49.0.1045.g170613ef41-goog