Subject: Re: [PATCH v17 24/24] KVM: selftests: Add guest_memfd testcase to fault-in on !mmap()'d memory

From: Sean Christopherson <[email-masked]>
Date: Fri, 17 Apr 2026 09:47:04 -0700

On Mon, Mar 30, 2026, Zenghui Yu wrote:
> On 7/30/25 6:54 AM, Sean Christopherson wrote:
> > Add a guest_memfd testcase to verify that a vCPU can fault-in guest_memfd
> > memory that supports mmap(), but that is not currently mapped into host
> > userspace and/or has a userspace address (in the memslot) that points at
> > something other than the target guest_memfd range.  Mapping guest_memfd
> > memory into the guest is supposed to operate completely independently from
> > any userspace mappings.
> > +	vm = __vm_create_shape_with_one_vcpu(VM_SHAPE_DEFAULT, &vcpu, 1, guest_code);
> > +
> > +	TEST_ASSERT(vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_MMAP),
> > +		    "Default VM type should always support guest_memfd mmap()");
> > +
> > +	size = vm->page_size;
> > +	fd = vm_create_guest_memfd(vm, size, GUEST_MEMFD_FLAG_MMAP);
> 
> This test fails on arm64 when vm->page_size is 4k and host's page_size
> is 64k:
> 
> [root@localhost kvm]# ./guest_memfd_test
> Random seed: 0x6b8b4567
> __vm_create: mode='PA-bits:40,  VA-bits:48,  4K pages' type='0', pages='660'
> ==== Test Assertion Failure ====
>   include/kvm_util.h:683: fd >= 0
>   pid=889 tid=889 errno=22 - Invalid argument
>      1	0x000000000040262b: vm_create_guest_memfd at kvm_util.h:683
>      2	 (inlined by) test_guest_memfd_guest at guest_memfd_test.c:450
>      3	 (inlined by) main at guest_memfd_test.c:491
>      4	0x00007fff8f56af3b: ?? ??:0
>      5	0x00007fff8f56b007: ?? ??:0
>      6	0x0000000000402a2f: _start at ??:?
>   KVM_CREATE_GUEST_MEMFD failed, rc: -1 errno: 22 (Invalid argument)

Does this fix things for you?  If so, I'll send a proper patch.

diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c
index ec7644aae999..c682bda722f9 100644
--- a/tools/testing/selftests/kvm/guest_memfd_test.c
+++ b/tools/testing/selftests/kvm/guest_memfd_test.c
@@ -510,9 +510,15 @@ static void test_guest_memfd_guest(void)
                    "Default VM type should support INIT_SHARED, supported flags = 0x%x",
                    vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_FLAGS));
 
+       /*
+        * Use the guest's page size for all accesses, e.g. to avoid having to
+        * map multiple pages, but create the guest_memfd instance with the max
+        * of the host or guest page size, as KVM requires gmem files to be
+        * aligned to the host page size.
+        */
        size = vm->page_size;
-       fd = vm_create_guest_memfd(vm, size, GUEST_MEMFD_FLAG_MMAP |
-                                            GUEST_MEMFD_FLAG_INIT_SHARED);
+       fd = vm_create_guest_memfd(vm, max(size, page_size), GUEST_MEMFD_FLAG_MMAP |
+                                                            GUEST_MEMFD_FLAG_INIT_SHARED);
        vm_set_user_memory_region2(vm, slot, KVM_MEM_GUEST_MEMFD, gpa, size, NULL, fd, 0);
 
        mem = kvm_mmap(size, PROT_READ | PROT_WRITE, MAP_SHARED, fd);