Skip to content

Commit df0d992

Browse files
Ackerley Tngsean-jc
authored andcommitted
KVM: selftests: Add test coverage for guest_memfd without GUEST_MEMFD_FLAG_MMAP
If a VM type supports KVM_CAP_GUEST_MEMFD_MMAP, the guest_memfd test will run all test cases with GUEST_MEMFD_FLAG_MMAP set. This leaves the code path for creating a non-mmap()-able guest_memfd on a VM that supports mappable guest memfds untested. Refactor the test to run the main test suite with a given set of flags. Then, for VM types that support the mappable capability, invoke the test suite twice: once with no flags, and once with GUEST_MEMFD_FLAG_MMAP set. This ensures both creation paths are properly exercised on capable VMs. Run test_guest_memfd_flags() only once per VM type since it depends only on the set of valid/supported flags, i.e. iterating over an arbitrary set of flags is both unnecessary and wrong. Signed-off-by: Ackerley Tng <ackerleytng@google.com> [sean: use double-underscores for the inner helper] Reviewed-by: Fuad Tabba <tabba@google.com> Tested-by: Fuad Tabba <tabba@google.com> Reviewed-by: David Hildenbrand <david@redhat.com> Link: https://lore.kernel.org/r/20251003232606.4070510-9-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 21d602e commit df0d992

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

tools/testing/selftests/kvm/guest_memfd_test.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -268,18 +268,8 @@ do { \
268268
close(fd); \
269269
} while (0)
270270

271-
static void test_guest_memfd(unsigned long vm_type)
271+
static void __test_guest_memfd(struct kvm_vm *vm, uint64_t flags)
272272
{
273-
struct kvm_vm *vm;
274-
uint64_t flags;
275-
276-
vm = vm_create_barebones_type(vm_type);
277-
flags = vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_FLAGS);
278-
279-
/* This test doesn't yet support testing mmap() on private memory. */
280-
if (!(flags & GUEST_MEMFD_FLAG_INIT_SHARED))
281-
flags &= ~GUEST_MEMFD_FLAG_MMAP;
282-
283273
test_create_guest_memfd_multiple(vm);
284274
test_create_guest_memfd_invalid_sizes(vm, flags);
285275

@@ -295,9 +285,24 @@ static void test_guest_memfd(unsigned long vm_type)
295285
gmem_test(file_size, vm, flags);
296286
gmem_test(fallocate, vm, flags);
297287
gmem_test(invalid_punch_hole, vm, flags);
288+
}
289+
290+
static void test_guest_memfd(unsigned long vm_type)
291+
{
292+
struct kvm_vm *vm = vm_create_barebones_type(vm_type);
293+
uint64_t flags;
298294

299295
test_guest_memfd_flags(vm);
300296

297+
__test_guest_memfd(vm, 0);
298+
299+
flags = vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_FLAGS);
300+
301+
/* MMAP should always be supported if INIT_SHARED is supported. */
302+
if (flags & GUEST_MEMFD_FLAG_INIT_SHARED)
303+
__test_guest_memfd(vm, GUEST_MEMFD_FLAG_MMAP |
304+
GUEST_MEMFD_FLAG_INIT_SHARED);
305+
301306
kvm_vm_free(vm);
302307
}
303308

0 commit comments

Comments
 (0)