Commit 2feabb85 authored by Chao Peng's avatar Chao Peng Committed by Paolo Bonzini

KVM: selftests: Expand set_memory_region_test to validate guest_memfd()

Expand set_memory_region_test to exercise various positive and negative
testcases for private memory.

 - Non-guest_memfd() file descriptor for private memory
 - guest_memfd() from different VM
 - Overlapping bindings
 - Unaligned bindings
Signed-off-by: default avatarChao Peng <chao.p.peng@linux.intel.com>
Co-developed-by: default avatarAckerley Tng <ackerleytng@google.com>
Signed-off-by: default avatarAckerley Tng <ackerleytng@google.com>
[sean: trim the testcases to remove duplicate coverage]
Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
Message-Id: <20231027182217.3615211-34-seanjc@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent e6f4f345
......@@ -819,6 +819,18 @@ static inline struct kvm_vm *vm_create_barebones(void)
return ____vm_create(VM_SHAPE_DEFAULT);
}
#ifdef __x86_64__
static inline struct kvm_vm *vm_create_barebones_protected_vm(void)
{
const struct vm_shape shape = {
.mode = VM_MODE_DEFAULT,
.type = KVM_X86_SW_PROTECTED_VM,
};
return ____vm_create(shape);
}
#endif
static inline struct kvm_vm *vm_create(uint32_t nr_runnable_vcpus)
{
return __vm_create(VM_SHAPE_DEFAULT, nr_runnable_vcpus, 0);
......
......@@ -385,13 +385,105 @@ static void test_add_max_memory_regions(void)
kvm_vm_free(vm);
}
#ifdef __x86_64__
static void test_invalid_guest_memfd(struct kvm_vm *vm, int memfd,
size_t offset, const char *msg)
{
int r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_GUEST_MEMFD,
MEM_REGION_GPA, MEM_REGION_SIZE,
0, memfd, offset);
TEST_ASSERT(r == -1 && errno == EINVAL, "%s", msg);
}
static void test_add_private_memory_region(void)
{
struct kvm_vm *vm, *vm2;
int memfd, i;
pr_info("Testing ADD of KVM_MEM_GUEST_MEMFD memory regions\n");
vm = vm_create_barebones_protected_vm();
test_invalid_guest_memfd(vm, vm->kvm_fd, 0, "KVM fd should fail");
test_invalid_guest_memfd(vm, vm->fd, 0, "VM's fd should fail");
memfd = kvm_memfd_alloc(MEM_REGION_SIZE, false);
test_invalid_guest_memfd(vm, memfd, 0, "Regular memfd() should fail");
close(memfd);
vm2 = vm_create_barebones_protected_vm();
memfd = vm_create_guest_memfd(vm2, MEM_REGION_SIZE, 0);
test_invalid_guest_memfd(vm, memfd, 0, "Other VM's guest_memfd() should fail");
vm_set_user_memory_region2(vm2, MEM_REGION_SLOT, KVM_MEM_GUEST_MEMFD,
MEM_REGION_GPA, MEM_REGION_SIZE, 0, memfd, 0);
close(memfd);
kvm_vm_free(vm2);
memfd = vm_create_guest_memfd(vm, MEM_REGION_SIZE, 0);
for (i = 1; i < PAGE_SIZE; i++)
test_invalid_guest_memfd(vm, memfd, i, "Unaligned offset should fail");
vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_GUEST_MEMFD,
MEM_REGION_GPA, MEM_REGION_SIZE, 0, memfd, 0);
close(memfd);
kvm_vm_free(vm);
}
static void test_add_overlapping_private_memory_regions(void)
{
struct kvm_vm *vm;
int memfd;
int r;
pr_info("Testing ADD of overlapping KVM_MEM_GUEST_MEMFD memory regions\n");
vm = vm_create_barebones_protected_vm();
memfd = vm_create_guest_memfd(vm, MEM_REGION_SIZE * 4, 0);
vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_GUEST_MEMFD,
MEM_REGION_GPA, MEM_REGION_SIZE * 2, 0, memfd, 0);
vm_set_user_memory_region2(vm, MEM_REGION_SLOT + 1, KVM_MEM_GUEST_MEMFD,
MEM_REGION_GPA * 2, MEM_REGION_SIZE * 2,
0, memfd, MEM_REGION_SIZE * 2);
/*
* Delete the first memslot, and then attempt to recreate it except
* with a "bad" offset that results in overlap in the guest_memfd().
*/
vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_GUEST_MEMFD,
MEM_REGION_GPA, 0, NULL, -1, 0);
/* Overlap the front half of the other slot. */
r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_GUEST_MEMFD,
MEM_REGION_GPA * 2 - MEM_REGION_SIZE,
MEM_REGION_SIZE * 2,
0, memfd, 0);
TEST_ASSERT(r == -1 && errno == EEXIST, "%s",
"Overlapping guest_memfd() bindings should fail with EEXIST");
/* And now the back half of the other slot. */
r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_GUEST_MEMFD,
MEM_REGION_GPA * 2 + MEM_REGION_SIZE,
MEM_REGION_SIZE * 2,
0, memfd, 0);
TEST_ASSERT(r == -1 && errno == EEXIST, "%s",
"Overlapping guest_memfd() bindings should fail with EEXIST");
close(memfd);
kvm_vm_free(vm);
}
#endif
int main(int argc, char *argv[])
{
#ifdef __x86_64__
int i, loops;
#endif
#ifdef __x86_64__
/*
* FIXME: the zero-memslot test fails on aarch64 and s390x because
* KVM_RUN fails with ENOEXEC or EFAULT.
......@@ -402,6 +494,14 @@ int main(int argc, char *argv[])
test_add_max_memory_regions();
#ifdef __x86_64__
if (kvm_has_cap(KVM_CAP_GUEST_MEMFD) &&
(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM))) {
test_add_private_memory_region();
test_add_overlapping_private_memory_regions();
} else {
pr_info("Skipping tests for KVM_MEM_GUEST_MEMFD memory regions\n");
}
if (argc > 1)
loops = atoi_positive("Number of iterations", argv[1]);
else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment