Skip to content

Commit a5accd8

Browse files
Ackerley Tngsean-jc
authored andcommitted
KVM: selftests: Add tests - invalid inputs for KVM_CREATE_GUEST_MEMFD
Test that invalid inputs for KVM_CREATE_GUEST_MEMFD, such as non-page-aligned page size and invalid flags, are rejected by the KVM_CREATE_GUEST_MEMFD with EINVAL Signed-off-by: Ackerley Tng <ackerleytng@google.com> Link: https://lore.kernel.org/r/20230825001729.1952858-1-ackerleytng@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 4876a35 commit a5accd8

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

tools/testing/selftests/kvm/guest_memfd_test.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define _GNU_SOURCE
99
#include "test_util.h"
1010
#include "kvm_util_base.h"
11+
#include <linux/bitmap.h>
1112
#include <linux/falloc.h>
1213
#include <sys/mman.h>
1314
#include <sys/types.h>
@@ -90,6 +91,52 @@ static void test_fallocate(int fd, size_t page_size, size_t total_size)
9091
TEST_ASSERT(!ret, "fallocate to restore punched hole should succeed");
9192
}
9293

94+
static void test_create_guest_memfd_invalid(struct kvm_vm *vm)
95+
{
96+
uint64_t valid_flags = 0;
97+
size_t page_size = getpagesize();
98+
uint64_t flag;
99+
size_t size;
100+
int fd;
101+
102+
for (size = 1; size < page_size; size++) {
103+
fd = __vm_create_guest_memfd(vm, size, 0);
104+
TEST_ASSERT(fd == -1 && errno == EINVAL,
105+
"guest_memfd() with non-page-aligned page size '0x%lx' should fail with EINVAL",
106+
size);
107+
}
108+
109+
if (thp_configured()) {
110+
for (size = page_size * 2; size < get_trans_hugepagesz(); size += page_size) {
111+
fd = __vm_create_guest_memfd(vm, size, KVM_GUEST_MEMFD_ALLOW_HUGEPAGE);
112+
TEST_ASSERT(fd == -1 && errno == EINVAL,
113+
"guest_memfd() with non-hugepage-aligned page size '0x%lx' should fail with EINVAL",
114+
size);
115+
}
116+
117+
valid_flags = KVM_GUEST_MEMFD_ALLOW_HUGEPAGE;
118+
}
119+
120+
for (flag = 1; flag; flag <<= 1) {
121+
uint64_t bit;
122+
123+
if (flag & valid_flags)
124+
continue;
125+
126+
fd = __vm_create_guest_memfd(vm, page_size, flag);
127+
TEST_ASSERT(fd == -1 && errno == EINVAL,
128+
"guest_memfd() with flag '0x%lx' should fail with EINVAL",
129+
flag);
130+
131+
for_each_set_bit(bit, &valid_flags, 64) {
132+
fd = __vm_create_guest_memfd(vm, page_size, flag | BIT_ULL(bit));
133+
TEST_ASSERT(fd == -1 && errno == EINVAL,
134+
"guest_memfd() with flags '0x%llx' should fail with EINVAL",
135+
flag | BIT_ULL(bit));
136+
}
137+
}
138+
}
139+
93140

94141
int main(int argc, char *argv[])
95142
{
@@ -103,6 +150,8 @@ int main(int argc, char *argv[])
103150

104151
vm = vm_create_barebones();
105152

153+
test_create_guest_memfd_invalid(vm);
154+
106155
fd = vm_create_guest_memfd(vm, total_size, 0);
107156

108157
test_file_read_write(fd);

tools/testing/selftests/kvm/include/kvm_util_base.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,15 +474,22 @@ static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name)
474474
}
475475

476476
void vm_create_irqchip(struct kvm_vm *vm);
477-
static inline int vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
477+
478+
static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
478479
uint64_t flags)
479480
{
480481
struct kvm_create_guest_memfd gmem = {
481482
.size = size,
482483
.flags = flags,
483484
};
484485

485-
int fd = __vm_ioctl(vm, KVM_CREATE_GUEST_MEMFD, &gmem);
486+
return __vm_ioctl(vm, KVM_CREATE_GUEST_MEMFD, &gmem);
487+
}
488+
489+
static inline int vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
490+
uint64_t flags)
491+
{
492+
int fd = __vm_create_guest_memfd(vm, size, flags);
486493

487494
TEST_ASSERT(fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_GUEST_MEMFD, fd));
488495
return fd;

0 commit comments

Comments
 (0)