Skip to content

Commit a11d712

Browse files
Fuad Tabbabonzini
authored andcommitted
KVM: selftests: guest_memfd mmap() test when mmap is supported
Expand the guest_memfd selftests to comprehensively test host userspace mmap functionality for guest_memfd-backed memory when supported by the VM type. Introduce new test cases to verify the following: * Successful mmap operations: Ensure that MAP_SHARED mappings succeed when guest_memfd mmap is enabled. * Data integrity: Validate that data written to the mmap'd region is correctly persistent and readable. * fallocate interaction: Test that fallocate(FALLOC_FL_PUNCH_HOLE) correctly zeros out mapped pages. * Out-of-bounds access: Verify that accessing memory beyond the guest_memfd's size correctly triggers a SIGBUS signal. * Unsupported mmap: Confirm that mmap attempts fail as expected when guest_memfd mmap support is not enabled for the specific guest_memfd instance or VM type. * Flag validity: Introduce test_vm_type_gmem_flag_validity() to systematically test that only allowed guest_memfd creation flags are accepted for different VM types (e.g., GUEST_MEMFD_FLAG_MMAP for default VMs, no flags for CoCo VMs). The existing tests for guest_memfd creation (multiple instances, invalid sizes), file read/write, file size, and invalid punch hole operations are integrated into the new test_with_type() framework to allow testing across different VM types. Cc: James Houghton <[email protected]> Cc: Gavin Shan <[email protected]> Cc: Shivank Garg <[email protected]> Co-developed-by: Ackerley Tng <[email protected]> Signed-off-by: Ackerley Tng <[email protected]> Signed-off-by: Fuad Tabba <[email protected]> Co-developed-by: Sean Christopherson <[email protected]> Signed-off-by: Sean Christopherson <[email protected]> Message-ID: <[email protected]> [Fix default vm_types to use BIT() - Paolo] Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 692f6ec commit a11d712

File tree

1 file changed

+139
-22
lines changed

1 file changed

+139
-22
lines changed

tools/testing/selftests/kvm/guest_memfd_test.c

Lines changed: 139 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#include <linux/bitmap.h>
1515
#include <linux/falloc.h>
16+
#include <setjmp.h>
17+
#include <signal.h>
1618
#include <sys/mman.h>
1719
#include <sys/types.h>
1820
#include <sys/stat.h>
@@ -34,12 +36,83 @@ static void test_file_read_write(int fd)
3436
"pwrite on a guest_mem fd should fail");
3537
}
3638

37-
static void test_mmap(int fd, size_t page_size)
39+
static void test_mmap_supported(int fd, size_t page_size, size_t total_size)
40+
{
41+
const char val = 0xaa;
42+
char *mem;
43+
size_t i;
44+
int ret;
45+
46+
mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
47+
TEST_ASSERT(mem == MAP_FAILED, "Copy-on-write not allowed by guest_memfd.");
48+
49+
mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
50+
TEST_ASSERT(mem != MAP_FAILED, "mmap() for guest_memfd should succeed.");
51+
52+
memset(mem, val, total_size);
53+
for (i = 0; i < total_size; i++)
54+
TEST_ASSERT_EQ(READ_ONCE(mem[i]), val);
55+
56+
ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0,
57+
page_size);
58+
TEST_ASSERT(!ret, "fallocate the first page should succeed.");
59+
60+
for (i = 0; i < page_size; i++)
61+
TEST_ASSERT_EQ(READ_ONCE(mem[i]), 0x00);
62+
for (; i < total_size; i++)
63+
TEST_ASSERT_EQ(READ_ONCE(mem[i]), val);
64+
65+
memset(mem, val, page_size);
66+
for (i = 0; i < total_size; i++)
67+
TEST_ASSERT_EQ(READ_ONCE(mem[i]), val);
68+
69+
ret = munmap(mem, total_size);
70+
TEST_ASSERT(!ret, "munmap() should succeed.");
71+
}
72+
73+
static sigjmp_buf jmpbuf;
74+
void fault_sigbus_handler(int signum)
75+
{
76+
siglongjmp(jmpbuf, 1);
77+
}
78+
79+
static void test_fault_overflow(int fd, size_t page_size, size_t total_size)
80+
{
81+
struct sigaction sa_old, sa_new = {
82+
.sa_handler = fault_sigbus_handler,
83+
};
84+
size_t map_size = total_size * 4;
85+
const char val = 0xaa;
86+
char *mem;
87+
size_t i;
88+
int ret;
89+
90+
mem = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
91+
TEST_ASSERT(mem != MAP_FAILED, "mmap() for guest_memfd should succeed.");
92+
93+
sigaction(SIGBUS, &sa_new, &sa_old);
94+
if (sigsetjmp(jmpbuf, 1) == 0) {
95+
memset(mem, 0xaa, map_size);
96+
TEST_ASSERT(false, "memset() should have triggered SIGBUS.");
97+
}
98+
sigaction(SIGBUS, &sa_old, NULL);
99+
100+
for (i = 0; i < total_size; i++)
101+
TEST_ASSERT_EQ(READ_ONCE(mem[i]), val);
102+
103+
ret = munmap(mem, map_size);
104+
TEST_ASSERT(!ret, "munmap() should succeed.");
105+
}
106+
107+
static void test_mmap_not_supported(int fd, size_t page_size, size_t total_size)
38108
{
39109
char *mem;
40110

41111
mem = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
42112
TEST_ASSERT_EQ(mem, MAP_FAILED);
113+
114+
mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
115+
TEST_ASSERT_EQ(mem, MAP_FAILED);
43116
}
44117

45118
static void test_file_size(int fd, size_t page_size, size_t total_size)
@@ -120,26 +193,19 @@ static void test_invalid_punch_hole(int fd, size_t page_size, size_t total_size)
120193
}
121194
}
122195

123-
static void test_create_guest_memfd_invalid(struct kvm_vm *vm)
196+
static void test_create_guest_memfd_invalid_sizes(struct kvm_vm *vm,
197+
uint64_t guest_memfd_flags,
198+
size_t page_size)
124199
{
125-
size_t page_size = getpagesize();
126-
uint64_t flag;
127200
size_t size;
128201
int fd;
129202

130203
for (size = 1; size < page_size; size++) {
131-
fd = __vm_create_guest_memfd(vm, size, 0);
132-
TEST_ASSERT(fd == -1 && errno == EINVAL,
204+
fd = __vm_create_guest_memfd(vm, size, guest_memfd_flags);
205+
TEST_ASSERT(fd < 0 && errno == EINVAL,
133206
"guest_memfd() with non-page-aligned page size '0x%lx' should fail with EINVAL",
134207
size);
135208
}
136-
137-
for (flag = BIT(0); flag; flag <<= 1) {
138-
fd = __vm_create_guest_memfd(vm, page_size, flag);
139-
TEST_ASSERT(fd == -1 && errno == EINVAL,
140-
"guest_memfd() with flag '0x%lx' should fail with EINVAL",
141-
flag);
142-
}
143209
}
144210

145211
static void test_create_guest_memfd_multiple(struct kvm_vm *vm)
@@ -171,30 +237,81 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm)
171237
close(fd1);
172238
}
173239

174-
int main(int argc, char *argv[])
240+
static void test_guest_memfd_flags(struct kvm_vm *vm, uint64_t valid_flags)
175241
{
176-
size_t page_size;
177-
size_t total_size;
242+
size_t page_size = getpagesize();
243+
uint64_t flag;
178244
int fd;
179-
struct kvm_vm *vm;
180245

181-
TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD));
246+
for (flag = BIT(0); flag; flag <<= 1) {
247+
fd = __vm_create_guest_memfd(vm, page_size, flag);
248+
if (flag & valid_flags) {
249+
TEST_ASSERT(fd >= 0,
250+
"guest_memfd() with flag '0x%lx' should succeed",
251+
flag);
252+
close(fd);
253+
} else {
254+
TEST_ASSERT(fd < 0 && errno == EINVAL,
255+
"guest_memfd() with flag '0x%lx' should fail with EINVAL",
256+
flag);
257+
}
258+
}
259+
}
260+
261+
static void test_guest_memfd(unsigned long vm_type)
262+
{
263+
uint64_t flags = 0;
264+
struct kvm_vm *vm;
265+
size_t total_size;
266+
size_t page_size;
267+
int fd;
182268

183269
page_size = getpagesize();
184270
total_size = page_size * 4;
185271

186-
vm = vm_create_barebones();
272+
vm = vm_create_barebones_type(vm_type);
273+
274+
if (vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_MMAP))
275+
flags |= GUEST_MEMFD_FLAG_MMAP;
187276

188-
test_create_guest_memfd_invalid(vm);
189277
test_create_guest_memfd_multiple(vm);
278+
test_create_guest_memfd_invalid_sizes(vm, flags, page_size);
190279

191-
fd = vm_create_guest_memfd(vm, total_size, 0);
280+
fd = vm_create_guest_memfd(vm, total_size, flags);
192281

193282
test_file_read_write(fd);
194-
test_mmap(fd, page_size);
283+
284+
if (flags & GUEST_MEMFD_FLAG_MMAP) {
285+
test_mmap_supported(fd, page_size, total_size);
286+
test_fault_overflow(fd, page_size, total_size);
287+
} else {
288+
test_mmap_not_supported(fd, page_size, total_size);
289+
}
290+
195291
test_file_size(fd, page_size, total_size);
196292
test_fallocate(fd, page_size, total_size);
197293
test_invalid_punch_hole(fd, page_size, total_size);
198294

295+
test_guest_memfd_flags(vm, flags);
296+
199297
close(fd);
298+
kvm_vm_free(vm);
299+
}
300+
301+
int main(int argc, char *argv[])
302+
{
303+
unsigned long vm_types, vm_type;
304+
305+
TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD));
306+
307+
/*
308+
* Not all architectures support KVM_CAP_VM_TYPES. However, those that
309+
* support guest_memfd have that support for the default VM type.
310+
*/
311+
vm_types = kvm_check_cap(KVM_CAP_VM_TYPES);
312+
if (!vm_types)
313+
vm_types = BIT(VM_TYPE_DEFAULT);
314+
315+
for_each_set_bit(vm_type, &vm_types, BITS_PER_TYPE(vm_types))
316+
test_guest_memfd(vm_type);
200317
}

0 commit comments

Comments
 (0)