Skip to content

Commit 5d74316

Browse files
sean-jcbonzini
authored andcommitted
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: Sean Christopherson <seanjc@google.com> Message-Id: <20231031002049.3915752-1-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent e357778 commit 5d74316

2 files changed

Lines changed: 50 additions & 8 deletions

File tree

tools/testing/selftests/kvm/guest_memfd_test.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,13 @@ static void test_create_guest_memfd_invalid(struct kvm_vm *vm)
136136
size);
137137
}
138138

139-
for (flag = 1; flag; flag <<= 1) {
139+
for (flag = 0; flag; flag <<= 1) {
140140
uint64_t bit;
141141

142142
fd = __vm_create_guest_memfd(vm, page_size, flag);
143143
TEST_ASSERT(fd == -1 && errno == EINVAL,
144144
"guest_memfd() with flag '0x%lx' should fail with EINVAL",
145145
flag);
146-
147-
for_each_set_bit(bit, &valid_flags, 64) {
148-
fd = __vm_create_guest_memfd(vm, page_size, flag | BIT_ULL(bit));
149-
TEST_ASSERT(fd == -1 && errno == EINVAL,
150-
"guest_memfd() with flags '0x%llx' should fail with EINVAL",
151-
flag | BIT_ULL(bit));
152-
}
153146
}
154147
}
155148

tools/testing/selftests/kvm/set_memory_region_test.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,53 @@ static void test_zero_memory_regions(void)
326326
}
327327
#endif /* __x86_64__ */
328328

329+
static void test_invalid_memory_region_flags(void)
330+
{
331+
uint32_t supported_flags = KVM_MEM_LOG_DIRTY_PAGES;
332+
const uint32_t v2_only_flags = KVM_MEM_GUEST_MEMFD;
333+
struct kvm_vm *vm;
334+
int r, i;
335+
336+
#ifdef __x86_64__
337+
supported_flags |= KVM_MEM_READONLY;
338+
339+
if (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM))
340+
vm = vm_create_barebones_protected_vm();
341+
else
342+
#endif
343+
vm = vm_create_barebones();
344+
345+
if (kvm_check_cap(KVM_CAP_MEMORY_ATTRIBUTES) & KVM_MEMORY_ATTRIBUTE_PRIVATE)
346+
supported_flags |= KVM_MEM_GUEST_MEMFD;
347+
348+
for (i = 0; i < 32; i++) {
349+
if ((supported_flags & BIT(i)) && !(v2_only_flags & BIT(i)))
350+
continue;
351+
352+
r = __vm_set_user_memory_region(vm, MEM_REGION_SLOT, BIT(i),
353+
MEM_REGION_GPA, MEM_REGION_SIZE, NULL);
354+
355+
TEST_ASSERT(r && errno == EINVAL,
356+
"KVM_SET_USER_MEMORY_REGION should have failed on v2 only flag 0x%lx", BIT(i));
357+
358+
if (supported_flags & BIT(i))
359+
continue;
360+
361+
r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT, BIT(i),
362+
MEM_REGION_GPA, MEM_REGION_SIZE, NULL, 0, 0);
363+
TEST_ASSERT(r && errno == EINVAL,
364+
"KVM_SET_USER_MEMORY_REGION2 should have failed on unsupported flag 0x%lx", BIT(i));
365+
}
366+
367+
if (supported_flags & KVM_MEM_GUEST_MEMFD) {
368+
r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT,
369+
KVM_MEM_LOG_DIRTY_PAGES | KVM_MEM_GUEST_MEMFD,
370+
MEM_REGION_GPA, MEM_REGION_SIZE, NULL, 0, 0);
371+
TEST_ASSERT(r && errno == EINVAL,
372+
"KVM_SET_USER_MEMORY_REGION2 should have failed, dirty logging private memory is unsupported");
373+
}
374+
}
375+
329376
/*
330377
* Test it can be added memory slots up to KVM_CAP_NR_MEMSLOTS, then any
331378
* tentative to add further slots should fail.
@@ -491,6 +538,8 @@ int main(int argc, char *argv[])
491538
test_zero_memory_regions();
492539
#endif
493540

541+
test_invalid_memory_region_flags();
542+
494543
test_add_max_memory_regions();
495544

496545
#ifdef __x86_64__

0 commit comments

Comments
 (0)