Re: [RFC PATCH v1 16/37] KVM: selftests: Add support for mmap() on guest_memfd in core library
From: Sean Christopherson <seanjc@google.com>
Date: 2025-10-24 18:19:01
Also in:
cgroups, kvm, linux-doc, linux-fsdevel, linux-kselftest, linux-mm, lkml
Subsystem:
kernel selftest framework, kernel virtual machine (kvm), the rest · Maintainers:
Shuah Khan, Paolo Bonzini, Linus Torvalds
On Fri, Oct 24, 2025, Ackerley Tng wrote:
Ackerley Tng [off-list ref] writes:quoted
From: Sean Christopherson <seanjc@google.com> 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 <seanjc@google.com> [For guest_memfds, mmap() using gmem_offset instead of 0 all the time.] Signed-off-by: Ackerley Tng <redacted> --- 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. */