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.
I didn't test the legacy dual-backing case before posting this RFC and
probably should have.
> - region->mmap_start = kvm_mmap(region->mmap_size, PROT_READ | PROT_WRITE,
> - vm_mem_backing_src_alias(src_type)->flag,
> - region->fd);
> + mmap_offset = flags & KVM_MEM_GUEST_MEMFD ? gmem_offset : 0;
> + region->mmap_start = __kvm_mmap(region->mmap_size, PROT_READ |
> PROT_WRITE,
> +
> vm_mem_backing_src_alias(src_type)->flag,
> + region->fd, mmap_offset);
>
> TEST_ASSERT(!is_backing_src_hugetlb(src_type) ||
> region->mmap_start == align_ptr_up(region->mmap_start,
> backing_src_pagesz),
> @@ -1117,7 +1119,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
> uint64_t gpa, uint32_t slot, uint64_t npages,
> uint32_t flags)
> {
> - vm_mem_add(vm, src_type, gpa, slot, npages, flags, -1, 0);
> + vm_mem_add(vm, src_type, gpa, slot, npages, flags, -1, 0, 0);
> }
>
> /*
> diff --git a/tools/testing/selftests/kvm/x86/private_mem_conversions_test.c
> b/tools/testing/selftests/kvm/x86/private_mem_conversions_test.c
> index 1969f4ab9b280..41f6b38f04071 100644
> --- a/tools/testing/selftests/kvm/x86/private_mem_conversions_test.c
> +++ b/tools/testing/selftests/kvm/x86/private_mem_conversions_test.c
> @@ -399,7 +399,7 @@ static void test_mem_conversions(enum
> vm_mem_backing_src_type src_type, uint32_t
> for (i = 0; i < nr_memslots; i++)
> vm_mem_add(vm, src_type, BASE_DATA_GPA + slot_size * i,
> BASE_DATA_SLOT + i, slot_size / vm->page_size,
> - KVM_MEM_GUEST_MEMFD, memfd, slot_size * i);
> + KVM_MEM_GUEST_MEMFD, memfd, slot_size * i, 0);
>
> for (i = 0; i < nr_vcpus; i++) {
> uint64_t gpa = BASE_DATA_GPA + i * per_cpu_size;
> --
> 2.51.0.858.gf9c4a03a3a-goog