Commit 5d743164 authored by Sean Christopherson's avatar Sean Christopherson Committed by Paolo Bonzini

KVM: selftests: Add a memory region subtest to validate invalid flags

Add a subtest to set_memory_region_test to verify that KVM rejects invalid
flags and combinations with -EINVAL.  KVM might or might not fail with
EINVAL anyways, but we can at least try.
Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
Message-Id: <20231031002049.3915752-1-seanjc@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent e3577788
...@@ -136,20 +136,13 @@ static void test_create_guest_memfd_invalid(struct kvm_vm *vm) ...@@ -136,20 +136,13 @@ static void test_create_guest_memfd_invalid(struct kvm_vm *vm)
size); size);
} }
for (flag = 1; flag; flag <<= 1) { for (flag = 0; flag; flag <<= 1) {
uint64_t bit; uint64_t bit;
fd = __vm_create_guest_memfd(vm, page_size, flag); fd = __vm_create_guest_memfd(vm, page_size, flag);
TEST_ASSERT(fd == -1 && errno == EINVAL, TEST_ASSERT(fd == -1 && errno == EINVAL,
"guest_memfd() with flag '0x%lx' should fail with EINVAL", "guest_memfd() with flag '0x%lx' should fail with EINVAL",
flag); flag);
for_each_set_bit(bit, &valid_flags, 64) {
fd = __vm_create_guest_memfd(vm, page_size, flag | BIT_ULL(bit));
TEST_ASSERT(fd == -1 && errno == EINVAL,
"guest_memfd() with flags '0x%llx' should fail with EINVAL",
flag | BIT_ULL(bit));
}
} }
} }
......
...@@ -326,6 +326,53 @@ static void test_zero_memory_regions(void) ...@@ -326,6 +326,53 @@ static void test_zero_memory_regions(void)
} }
#endif /* __x86_64__ */ #endif /* __x86_64__ */
static void test_invalid_memory_region_flags(void)
{
uint32_t supported_flags = KVM_MEM_LOG_DIRTY_PAGES;
const uint32_t v2_only_flags = KVM_MEM_GUEST_MEMFD;
struct kvm_vm *vm;
int r, i;
#ifdef __x86_64__
supported_flags |= KVM_MEM_READONLY;
if (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM))
vm = vm_create_barebones_protected_vm();
else
#endif
vm = vm_create_barebones();
if (kvm_check_cap(KVM_CAP_MEMORY_ATTRIBUTES) & KVM_MEMORY_ATTRIBUTE_PRIVATE)
supported_flags |= KVM_MEM_GUEST_MEMFD;
for (i = 0; i < 32; i++) {
if ((supported_flags & BIT(i)) && !(v2_only_flags & BIT(i)))
continue;
r = __vm_set_user_memory_region(vm, MEM_REGION_SLOT, BIT(i),
MEM_REGION_GPA, MEM_REGION_SIZE, NULL);
TEST_ASSERT(r && errno == EINVAL,
"KVM_SET_USER_MEMORY_REGION should have failed on v2 only flag 0x%lx", BIT(i));
if (supported_flags & BIT(i))
continue;
r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT, BIT(i),
MEM_REGION_GPA, MEM_REGION_SIZE, NULL, 0, 0);
TEST_ASSERT(r && errno == EINVAL,
"KVM_SET_USER_MEMORY_REGION2 should have failed on unsupported flag 0x%lx", BIT(i));
}
if (supported_flags & KVM_MEM_GUEST_MEMFD) {
r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT,
KVM_MEM_LOG_DIRTY_PAGES | KVM_MEM_GUEST_MEMFD,
MEM_REGION_GPA, MEM_REGION_SIZE, NULL, 0, 0);
TEST_ASSERT(r && errno == EINVAL,
"KVM_SET_USER_MEMORY_REGION2 should have failed, dirty logging private memory is unsupported");
}
}
/* /*
* Test it can be added memory slots up to KVM_CAP_NR_MEMSLOTS, then any * Test it can be added memory slots up to KVM_CAP_NR_MEMSLOTS, then any
* tentative to add further slots should fail. * tentative to add further slots should fail.
...@@ -491,6 +538,8 @@ int main(int argc, char *argv[]) ...@@ -491,6 +538,8 @@ int main(int argc, char *argv[])
test_zero_memory_regions(); test_zero_memory_regions();
#endif #endif
test_invalid_memory_region_flags();
test_add_max_memory_regions(); test_add_max_memory_regions();
#ifdef __x86_64__ #ifdef __x86_64__
......
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