KVM: selftests: Force GUEST_MEMFD flag for SNP VM type
authorPratik R. Sampat <prsampat@amd.com>
Wed, 5 Mar 2025 22:59:58 +0000 (16:59 -0600)
committerSean Christopherson <seanjc@google.com>
Fri, 2 May 2025 20:05:30 +0000 (13:05 -0700)
Force the SEV-SNP VM type to set the KVM_MEM_GUEST_MEMFD flag for the
creation of private memslots.

Signed-off-by: Pratik R. Sampat <prsampat@amd.com>
Link: https://lore.kernel.org/r/20250305230000.231025-9-prsampat@amd.com
[sean: add a comment, don't break non-x86]
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/lib/kvm_util.c

index 815bc45dd8dc68b84469abb062296399bbb4115b..50edc59cc0ca6331b179f7104eca76fdd270d486 100644 (file)
@@ -444,6 +444,15 @@ void kvm_set_files_rlimit(uint32_t nr_vcpus)
 
 }
 
+static bool is_guest_memfd_required(struct vm_shape shape)
+{
+#ifdef __x86_64__
+       return shape.type == KVM_X86_SNP_VM;
+#else
+       return false;
+#endif
+}
+
 struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,
                           uint64_t nr_extra_pages)
 {
@@ -451,7 +460,7 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,
                                                 nr_extra_pages);
        struct userspace_mem_region *slot0;
        struct kvm_vm *vm;
-       int i;
+       int i, flags;
 
        kvm_set_files_rlimit(nr_runnable_vcpus);
 
@@ -460,7 +469,15 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,
 
        vm = ____vm_create(shape);
 
-       vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, 0);
+       /*
+        * Force GUEST_MEMFD for the primary memory region if necessary, e.g.
+        * for CoCo VMs that require GUEST_MEMFD backed private memory.
+        */
+       flags = 0;
+       if (is_guest_memfd_required(shape))
+               flags |= KVM_MEM_GUEST_MEMFD;
+
+       vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, flags);
        for (i = 0; i < NR_MEM_REGIONS; i++)
                vm->memslots[i] = 0;