[PATCH v1 12/13] KVM: selftests: Add KVM_MEM_USERFAULT + guest_memfd toggle tests

James Houghton
2024-12-04T19:14:28Z

Make sure KVM_MEM_USERFAULT can be toggled on and off for
KVM_MEM_GUEST_MEMFD memslots.

Signed-off-by: James Houghton <jthoughton@google.com>
---
.../selftests/kvm/set_memory_region_test.c | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)

diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c
index d233cdfb0241..57b7032d7cc3 100644
--- a/tools/testing/selftests/kvm/set_memory_region_test.c
+++ b/tools/testing/selftests/kvm/set_memory_region_test.c
@@ -556,6 +556,35 @@ static void test_add_overlapping_private_memory_regions(void)
close(memfd);
kvm_vm_free(vm);
}
+
+static void test_private_memory_region_userfault(void)
+{
+ struct kvm_vm *vm;
+ int memfd;
+
+ pr_info("Testing toggling KVM_MEM_USERFAULT on KVM_MEM_GUEST_MEMFD memory regions\n");
+
+ vm = vm_create_barebones_type(KVM_X86_SW_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 = vm_create_guest_memfd(vm, MEM_REGION_SIZE, 0);
+
+ vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_GUEST_MEMFD,
+ MEM_REGION_GPA, MEM_REGION_SIZE, 0, memfd, 0);
+
+ vm_set_user_memory_region2(vm, MEM_REGION_SLOT,
+ KVM_MEM_GUEST_MEMFD | KVM_MEM_USERFAULT,
+ MEM_REGION_GPA, MEM_REGION_SIZE, 0, memfd, 0);
+
+ 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);
+}
#endif

int main(int argc, char *argv[])
@@ -582,6 +611,7 @@ int main(int argc, char *argv[])
(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM))) {
test_add_private_memory_region();
test_add_overlapping_private_memory_regions();
+ test_private_memory_region_userfault();
} else {
pr_info("Skipping tests for KVM_MEM_GUEST_MEMFD memory regions\n");
}
--
2.47.0.338.g60cca15819-goog