Binbin Wu <binbin.wu@xxxxxxxxxxxxxxx> writes: > On 5/17/2025 1:42 AM, Ackerley Tng wrote: >> Ira Weiny <ira.weiny@xxxxxxxxx> writes: >> >>> Ackerley Tng wrote: >>>> Test that GUEST_MEMFD_FLAG_INIT_PRIVATE is only valid when >>>> GUEST_MEMFD_FLAG_SUPPORT_SHARED is set. >>>> >>>> Change-Id: I506e236a232047cfaee17bcaed02ee14c8d25bbb >>>> Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx> >>>> --- >>>> .../testing/selftests/kvm/guest_memfd_test.c | 36 ++++++++++++------- >>>> 1 file changed, 24 insertions(+), 12 deletions(-) >>>> >>>> diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c >>>> index 60aaba5808a5..bf2876cbd711 100644 >>>> --- a/tools/testing/selftests/kvm/guest_memfd_test.c >>>> +++ b/tools/testing/selftests/kvm/guest_memfd_test.c >>>> @@ -401,13 +401,31 @@ static void test_with_type(unsigned long vm_type, uint64_t guest_memfd_flags, >>>> kvm_vm_release(vm); >>>> } >>>> >>>> +static void test_vm_with_gmem_flag(struct kvm_vm *vm, uint64_t flag, >>>> + bool expect_valid) >>>> +{ >>>> + size_t page_size = getpagesize(); >>>> + int fd; >>>> + >>>> + fd = __vm_create_guest_memfd(vm, page_size, flag); >>>> + >>>> + if (expect_valid) { >>>> + TEST_ASSERT(fd > 0, >>>> + "guest_memfd() with flag '0x%lx' should be valid", >>>> + flag); >>>> + close(fd); >>>> + } else { >>>> + TEST_ASSERT(fd == -1 && errno == EINVAL, >>>> + "guest_memfd() with flag '0x%lx' should fail with EINVAL", >>>> + flag); >>>> + } >>>> +} >>>> + >>>> static void test_vm_type_gmem_flag_validity(unsigned long vm_type, >>>> uint64_t expected_valid_flags) >>>> { >>>> - size_t page_size = getpagesize(); >>>> struct kvm_vm *vm; >>>> uint64_t flag = 0; >>>> - int fd; >>>> >>>> if (!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(vm_type))) >>>> return; >>>> @@ -415,17 +433,11 @@ static void test_vm_type_gmem_flag_validity(unsigned long vm_type, >>>> vm = vm_create_barebones_type(vm_type); >>>> >>>> for (flag = BIT(0); flag; flag <<= 1) { >>>> - fd = __vm_create_guest_memfd(vm, page_size, flag); >>>> + test_vm_with_gmem_flag(vm, flag, flag & expected_valid_flags); >>>> >>>> - if (flag & expected_valid_flags) { >>>> - TEST_ASSERT(fd > 0, >>>> - "guest_memfd() with flag '0x%lx' should be valid", >>>> - flag); >>>> - close(fd); >>>> - } else { >>>> - TEST_ASSERT(fd == -1 && errno == EINVAL, >>>> - "guest_memfd() with flag '0x%lx' should fail with EINVAL", >>>> - flag); >>>> + if (flag == GUEST_MEMFD_FLAG_SUPPORT_SHARED) { >>>> + test_vm_with_gmem_flag( >>>> + vm, flag | GUEST_MEMFD_FLAG_INIT_PRIVATE, true); >>> I don't understand the point of this check. In 2/51 we set >>> GUEST_MEMFD_FLAG_INIT_PRIVATE when GUEST_MEMFD_FLAG_SUPPORT_SHARED is set. >>> >>> When can this check ever fail? >>> >>> Ira >> In 02/51, GUEST_MEMFD_FLAG_INIT_PRIVATE is not set by default, >> GUEST_MEMFD_FLAG_INIT_PRIVATE is set as one of the valid_flags. >> >> The intention is that GUEST_MEMFD_FLAG_INIT_PRIVATE is only valid if >> GUEST_MEMFD_FLAG_SUPPORT_SHARED is requested by userspace. >> >> In this test, the earlier part before the if block calls >> test_vm_with_gmem_flag() all valid flags, and that already tests >> GUEST_MEMFD_FLAG_SUPPORT_SHARED individually. >> >> Specifically if GUEST_MEMFD_FLAG_SUPPORT_SHARED is set, this if block >> adds a test for when both GUEST_MEMFD_FLAG_SUPPORT_SHARED and >> GUEST_MEMFD_FLAG_INIT_PRIVATE are set, and sets that expect_valid is >> true. > Maybe it's more clear to move this case out of the loop? > Will try that in the next revision. Thanks! >> >> This second test doesn't fail, it is meant to check that the kernel >> allows the pair of flags to be set. Hope that makes sense.