KVM: selftests: Split out helper to allocate guest mem via memfd
authorSean Christopherson <seanjc@google.com>
Sat, 26 Feb 2022 00:15:44 +0000 (00:15 +0000)
committerPaolo Bonzini <pbonzini@redhat.com>
Tue, 8 Mar 2022 15:59:10 +0000 (10:59 -0500)
Extract the code for allocating guest memory via memfd out of
vm_userspace_mem_region_add() and into a new helper, kvm_memfd_alloc().
A future selftest to populate a guest with the maximum amount of guest
memory will abuse KVM's memslots to alias guest memory regions to a
single memfd-backed host region, i.e. needs to back a guest with memfd
memory without a 1:1 association between a memslot and a memfd instance.

No functional change intended.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Message-Id: <20220226001546.360188-27-seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
tools/testing/selftests/kvm/include/kvm_util_base.h
tools/testing/selftests/kvm/lib/kvm_util.c

index 573de03..92cef0f 100644 (file)
@@ -123,6 +123,7 @@ int kvm_memcmp_hva_gva(void *hva, struct kvm_vm *vm, const vm_vaddr_t gva,
                       size_t len);
 
 void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename);
+int kvm_memfd_alloc(size_t size, bool hugepages);
 
 void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent);
 
index dcb8e96..1665a22 100644 (file)
@@ -718,6 +718,27 @@ void kvm_vm_free(struct kvm_vm *vmp)
        free(vmp);
 }
 
+int kvm_memfd_alloc(size_t size, bool hugepages)
+{
+       int memfd_flags = MFD_CLOEXEC;
+       int fd, r;
+
+       if (hugepages)
+               memfd_flags |= MFD_HUGETLB;
+
+       fd = memfd_create("kvm_selftest", memfd_flags);
+       TEST_ASSERT(fd != -1, "memfd_create() failed, errno: %i (%s)",
+                   errno, strerror(errno));
+
+       r = ftruncate(fd, size);
+       TEST_ASSERT(!r, "ftruncate() failed, errno: %i (%s)", errno, strerror(errno));
+
+       r = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, size);
+       TEST_ASSERT(!r, "fallocate() failed, errno: %i (%s)", errno, strerror(errno));
+
+       return fd;
+}
+
 /*
  * Memory Compare, host virtual to guest virtual
  *
@@ -970,24 +991,9 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
                region->mmap_size += alignment;
 
        region->fd = -1;
-       if (backing_src_is_shared(src_type)) {
-               int memfd_flags = MFD_CLOEXEC;
-
-               if (src_type == VM_MEM_SRC_SHARED_HUGETLB)
-                       memfd_flags |= MFD_HUGETLB;
-
-               region->fd = memfd_create("kvm_selftest", memfd_flags);
-               TEST_ASSERT(region->fd != -1,
-                           "memfd_create failed, errno: %i", errno);
-
-               ret = ftruncate(region->fd, region->mmap_size);
-               TEST_ASSERT(ret == 0, "ftruncate failed, errno: %i", errno);
-
-               ret = fallocate(region->fd,
-                               FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0,
-                               region->mmap_size);
-               TEST_ASSERT(ret == 0, "fallocate failed, errno: %i", errno);
-       }
+       if (backing_src_is_shared(src_type))
+               region->fd = kvm_memfd_alloc(region->mmap_size,
+                                            src_type == VM_MEM_SRC_SHARED_HUGETLB);
 
        region->mmap_start = mmap(NULL, region->mmap_size,
                                  PROT_READ | PROT_WRITE,