Re: [PATCH 4/6] KVM: selftests: Add test coverage for guest_memfd without GUEST_MEMFD_FLAG_MMAP

Fuad Tabba
2025-09-29T09:25:00Z

On Fri, 26 Sept 2025 at 17:31, Sean Christopherson <seanjc@google.com> wrote:
>
> From: Ackerley Tng <ackerleytng@google.com>
>
> 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.
>
> test_guest_memfd_flags() tests valid flags, hence it can be run just once
> per VM type, and valid flag identification can be moved into the test
> function.
>
> Signed-off-by: Ackerley Tng <ackerleytng@google.com>
> [sean: use double-underscores for the inner helper]
> Signed-off-by: Sean Christopherson <seanjc@google.com>

Reviewed-by: Fuad Tabba <tabba@google.com>
Tested-by: Fuad Tabba <tabba@google.com>

Cheers,
/fuad

> ---
> .../testing/selftests/kvm/guest_memfd_test.c | 30 ++++++++++++-------
> 1 file changed, 19 insertions(+), 11 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c
> index 60c6dec63490..5a50a28ce1fa 100644
> --- a/tools/testing/selftests/kvm/guest_memfd_test.c
> +++ b/tools/testing/selftests/kvm/guest_memfd_test.c
> @@ -239,11 +239,16 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm)
> close(fd1);
> }
>
> -static void test_guest_memfd_flags(struct kvm_vm *vm, uint64_t valid_flags)
> +static void test_guest_memfd_flags(struct kvm_vm *vm)
> {
> + uint64_t valid_flags = 0;
> uint64_t flag;
> int fd;
>
> + if (vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_MMAP))
> + valid_flags |= GUEST_MEMFD_FLAG_MMAP |
> + GUEST_MEMFD_FLAG_DEFAULT_SHARED;
> +
> for (flag = BIT(0); flag; flag <<= 1) {
> fd = __vm_create_guest_memfd(vm, page_size, flag);
> if (flag & valid_flags) {
> @@ -267,16 +272,8 @@ do { \
> close(fd); \
> } while (0)
>
> -static void test_guest_memfd(unsigned long vm_type)
> +static void __test_guest_memfd(struct kvm_vm *vm, uint64_t flags)
> {
> - uint64_t flags = 0;
> - struct kvm_vm *vm;
> -
> - vm = vm_create_barebones_type(vm_type);
> -
> - if (vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_MMAP))
> - flags |= GUEST_MEMFD_FLAG_MMAP | GUEST_MEMFD_FLAG_DEFAULT_SHARED;
> -
> test_create_guest_memfd_multiple(vm);
> test_create_guest_memfd_invalid_sizes(vm, flags);
>
> @@ -292,8 +289,19 @@ static void test_guest_memfd(unsigned long vm_type)
> gmem_test(file_size, vm, flags);
> gmem_test(fallocate, vm, flags);
> gmem_test(invalid_punch_hole, vm, flags);
> +}
>
> - test_guest_memfd_flags(vm, flags);
> +static void test_guest_memfd(unsigned long vm_type)
> +{
> + struct kvm_vm *vm = vm_create_barebones_type(vm_type);
> +
> + test_guest_memfd_flags(vm);
> +
> + __test_guest_memfd(vm, 0);
> +
> + if (vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_MMAP))
> + __test_guest_memfd(vm, GUEST_MEMFD_FLAG_MMAP |
> + GUEST_MEMFD_FLAG_DEFAULT_SHARED);
>
> kvm_vm_free(vm);
> }
> --
> 2.51.0.536.g15c5d4f767-goog
>