On Fri, Oct 24, 2025, Ackerley Tng wrote:
> Ackerley Tng <[email protected]> writes:
>
> > From: Sean Christopherson <[email protected]>
> >
> > Accept gmem_flags in vm_mem_add() to be able to create a guest_memfd within
> > vm_mem_add().
> >
> > When vm_mem_add() is used to set up a guest_memfd for a memslot, set up the
> > provided (or created) gmem_fd as the fd for the user memory region. This
> > makes it available to be mmap()-ed from just like fds from other memory
> > sources. mmap() from guest_memfd using the provided gmem_flags and
> > gmem_offset.
> >
> > Add a kvm_slot_to_fd() helper to provide convenient access to the file
> > descriptor of a memslot.
> >
> > Update existing callers of vm_mem_add() to pass 0 for gmem_flags to
> > preserve existing behavior.
> >
> > Signed-off-by: Sean Christopherson <[email protected]>
> > [For guest_memfds, mmap() using gmem_offset instead of 0 all the time.]
> > Signed-off-by: Ackerley Tng <[email protected]>
> > ---
> > tools/testing/selftests/kvm/include/kvm_util.h | 7 ++++++-
> > tools/testing/selftests/kvm/lib/kvm_util.c | 18 ++++++++++--------
> > .../kvm/x86/private_mem_conversions_test.c | 2 +-
> > 3 files changed, 17 insertions(+), 10 deletions(-)
> >
> >
> > [...snip...]
> >
> > @@ -1050,13 +1049,16 @@ void vm_mem_add(struct kvm_vm *vm, enum
> > vm_mem_backing_src_type src_type,
> > }
> >
> > region->fd = -1;
> > - if (backing_src_is_shared(src_type))
> > + if (flags & KVM_MEM_GUEST_MEMFD && gmem_flags & GUEST_MEMFD_FLAG_MMAP)
> > + region->fd = kvm_dup(gmem_fd);
> > + else if (backing_src_is_shared(src_type))
> > region->fd = kvm_memfd_alloc(region->mmap_size,
> > src_type ==
> > VM_MEM_SRC_SHARED_HUGETLB);
> >
>
> Doing this makes it hard to test the legacy dual-backing case.
>
> It actually broke x86/private_mem_conversions_test for the legacy
> dual-backing case because there's no way to mmap or provide a
> userspace_address from the memory provider that is not guest_memfd, as
> determined by src_type.
Yes there is. This patch is a giant nop. The only thing that the core library
doesn't support is mmap() on guest_memfd *and* the other src_type, and IMO that
is big "don't care", because KVM doesn't even support that combination:
if (kvm_gmem_supports_mmap(inode))
slot->flags |= KVM_MEMSLOT_GMEM_ONLY;
I mean, we _could_ test that KVM ignores the hva for mapping, but that's a
different and unique test entirely.
I did break x86/private_mem_conversions_test (I could have sworn I tested,
*sigh*),
but the bug is in:
KVM: selftests: Provide function to look up guest_memfd details from gpa
not here. And it's a trivial /facepalm-style fix:
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c
b/tools/testing/selftests/kvm/lib/kvm_util.c
index ee5b63f7cb50..23a8676fee6d 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -1680,7 +1680,7 @@ int kvm_gpa_to_guest_memfd(struct kvm_vm *vm, vm_paddr_t
gpa, off_t *fd_offset,
gpa_offset = gpa - region->region.guest_phys_addr;
*fd_offset = region->region.guest_memfd_offset + gpa_offset;
*nr_bytes = region->region.memory_size - gpa_offset;
- return region->fd;
+ return region->region.guest_memfd;
}
/* Create an interrupt controller chip for the specified VM. */