Skip to content

Commit

Permalink
KVM: selftests: Add a memory region subtest to validate invalid flags
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
sean-jc authored and bonzini committed Nov 14, 2023
1 parent e357778 commit 5d74316
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
9 changes: 1 addition & 8 deletions tools/testing/selftests/kvm/guest_memfd_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,13 @@ static void test_create_guest_memfd_invalid(struct kvm_vm *vm)
size);
}

for (flag = 1; flag; flag <<= 1) {
for (flag = 0; flag; flag <<= 1) {
uint64_t bit;

fd = __vm_create_guest_memfd(vm, page_size, flag);
TEST_ASSERT(fd == -1 && errno == EINVAL,
"guest_memfd() with flag '0x%lx' should fail with EINVAL",
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));
}
}
}

Expand Down
49 changes: 49 additions & 0 deletions tools/testing/selftests/kvm/set_memory_region_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,53 @@ static void test_zero_memory_regions(void)
}
#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
* tentative to add further slots should fail.
Expand Down Expand Up @@ -491,6 +538,8 @@ int main(int argc, char *argv[])
test_zero_memory_regions();
#endif

test_invalid_memory_region_flags();

test_add_max_memory_regions();

#ifdef __x86_64__
Expand Down

0 comments on commit 5d74316

Please sign in to comment.