On Fri, May 02, 2025 at 03:29:53PM -0700, Ackerley Tng wrote: > Fuad Tabba <tabba@xxxxxxxxxx> writes: > > > Add support for mmap() and fault() for guest_memfd backed memory > > in the host for VMs that support in-place conversion between > > shared and private. To that end, this patch adds the ability to > > check whether the VM type supports in-place conversion, and only > > allows mapping its memory if that's the case. > > > > This patch introduces the configuration option KVM_GMEM_SHARED_MEM, > > which enables support for in-place shared memory. > > > > It also introduces the KVM capability KVM_CAP_GMEM_SHARED_MEM, which > > indicates that the host can create VMs that support shared memory. > > Supporting shared memory implies that memory can be mapped when shared > > with the host. > > > > Signed-off-by: Fuad Tabba <tabba@xxxxxxxxxx> > > --- > > include/linux/kvm_host.h | 15 ++++++- > > include/uapi/linux/kvm.h | 1 + > > virt/kvm/Kconfig | 5 +++ > > virt/kvm/guest_memfd.c | 92 ++++++++++++++++++++++++++++++++++++++++ > > virt/kvm/kvm_main.c | 4 ++ > > 5 files changed, 116 insertions(+), 1 deletion(-) > > > > <snip> > > At the guest_memfd call on 2025-05-01, we discussed that if guest_memfd > is created with GUEST_MEMFD_FLAG_SUPPORT_SHARED set, then if > slot->userspace_addr != 0, we would validate that the folio > slot->userspace_addr points to matches up with the folio guest_memfd > would return for the same offset. Where will the validation be executed? In kvm_gmem_bind()? > > I can think of one way to do this validation, which is to call KVM's > hva_to_pfn() function and then call kvm_gmem_get_folio() on the fd and > offset, and then check that the PFNs are equal. > > However, that would cause the page to be allocated. Any ideas on how we > could do this validation without allocating the page? If the check is in kvm_gmem_bind() and if there's no worry about munmap() and re-mmap() of the shared memory pointed by slot->userspace_addr, maybe below? mm = kvm->mm; mmap_read_lock(mm); vma = vma_lookup(mm, vaddr); pgoff = ((slot->userspace_addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; mmap_read_unlock(mm); Then check if pgoff equals to slot->gmem.guest_memfd_offset.