Skip to content

Commit

Permalink
KVM: selftests: Split out helper to allocate guest mem via memfd
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Sean Christopherson authored and Paolo Bonzini committed Mar 8, 2022
1 parent 3d7d604 commit a4187c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
1 change: 1 addition & 0 deletions tools/testing/selftests/kvm/include/kvm_util_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
42 changes: 24 additions & 18 deletions tools/testing/selftests/kvm/lib/kvm_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a4187c9

Please sign in to comment.