Skip to content

Commit 21d602e

Browse files
committed
KVM: selftests: Create a new guest_memfd for each testcase
Refactor the guest_memfd selftest to improve test isolation by creating a a new guest_memfd for each testcase. Currently, the test reuses a single guest_memfd instance for all testcases, and thus creates dependencies between tests, e.g. not truncating folios from the guest_memfd instance at the end of a test could lead to unexpected results (see the PUNCH_HOLE purging that needs to done by in-flight the NUMA testcases[1]). Invoke each test via a macro wrapper to create and close a guest_memfd to cut down on the boilerplate copy+paste needed to create a test. Link: https://lore.kernel.org/all/20250827175247.83322-10-shivankg@amd.com Reported-by: Ackerley Tng <ackerleytng@google.com> Reviewed-by: Fuad Tabba <tabba@google.com> Tested-by: Fuad Tabba <tabba@google.com> Reviewed-by: David Hildenbrand <david@redhat.com> Reviewed-by: Ackerley Tng <ackerleytng@google.com> Tested-by: Ackerley Tng <ackerleytng@google.com> Link: https://lore.kernel.org/r/20251003232606.4070510-8-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 3a6c085 commit 21d602e

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

tools/testing/selftests/kvm/guest_memfd_test.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
static size_t page_size;
2828

29-
static void test_file_read_write(int fd)
29+
static void test_file_read_write(int fd, size_t total_size)
3030
{
3131
char buf[64];
3232

@@ -260,14 +260,18 @@ static void test_guest_memfd_flags(struct kvm_vm *vm)
260260
}
261261
}
262262

263+
#define gmem_test(__test, __vm, __flags) \
264+
do { \
265+
int fd = vm_create_guest_memfd(__vm, page_size * 4, __flags); \
266+
\
267+
test_##__test(fd, page_size * 4); \
268+
close(fd); \
269+
} while (0)
270+
263271
static void test_guest_memfd(unsigned long vm_type)
264272
{
265273
struct kvm_vm *vm;
266-
size_t total_size;
267274
uint64_t flags;
268-
int fd;
269-
270-
total_size = page_size * 4;
271275

272276
vm = vm_create_barebones_type(vm_type);
273277
flags = vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_FLAGS);
@@ -279,24 +283,21 @@ static void test_guest_memfd(unsigned long vm_type)
279283
test_create_guest_memfd_multiple(vm);
280284
test_create_guest_memfd_invalid_sizes(vm, flags);
281285

282-
fd = vm_create_guest_memfd(vm, total_size, flags);
283-
284-
test_file_read_write(fd);
286+
gmem_test(file_read_write, vm, flags);
285287

286288
if (flags & GUEST_MEMFD_FLAG_MMAP) {
287-
test_mmap_supported(fd, total_size);
288-
test_fault_overflow(fd, total_size);
289+
gmem_test(mmap_supported, vm, flags);
290+
gmem_test(fault_overflow, vm, flags);
289291
} else {
290-
test_mmap_not_supported(fd, total_size);
292+
gmem_test(mmap_not_supported, vm, flags);
291293
}
292294

293-
test_file_size(fd, total_size);
294-
test_fallocate(fd, total_size);
295-
test_invalid_punch_hole(fd, total_size);
295+
gmem_test(file_size, vm, flags);
296+
gmem_test(fallocate, vm, flags);
297+
gmem_test(invalid_punch_hole, vm, flags);
296298

297299
test_guest_memfd_flags(vm);
298300

299-
close(fd);
300301
kvm_vm_free(vm);
301302
}
302303

0 commit comments

Comments
 (0)