Hi Fuad! On Wed, 2025-01-29 at 17:23 +0000, Fuad Tabba wrote: > 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 (shared memory). To that end, this patch adds > the ability to check whether the VM type has that support, and > only allows mapping its memory if that's the case. > > Additionally, this behavior is gated with a new configuration > option, CONFIG_KVM_GMEM_SHARED_MEM. > > Signed-off-by: Fuad Tabba <tabba@xxxxxxxxxx> > > --- > > This patch series will allow shared memory support for software > VMs in x86. It will also introduce a similar VM type for arm64 > and allow shared memory support for that. In the future, pKVM > will also support shared memory. > --- > include/linux/kvm_host.h | 11 ++++++ > virt/kvm/Kconfig | 4 +++ > virt/kvm/guest_memfd.c | 77 ++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 92 insertions(+) > > -snip- > > diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c > index 47a9f68f7b24..86441581c9ae 100644 > --- a/virt/kvm/guest_memfd.c > +++ b/virt/kvm/guest_memfd.c > @@ -307,7 +307,84 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) > return gfn - slot->base_gfn + slot->gmem.pgoff; > } > > +#ifdef CONFIG_KVM_GMEM_SHARED_MEM > +static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf) > +{ > + struct inode *inode = file_inode(vmf->vma->vm_file); > + struct folio *folio; > + vm_fault_t ret = VM_FAULT_LOCKED; > + > + filemap_invalidate_lock_shared(inode->i_mapping); > + > + folio = kvm_gmem_get_folio(inode, vmf->pgoff); > + if (IS_ERR(folio)) { > + ret = VM_FAULT_SIGBUS; > + goto out_filemap; > + } > + > + if (folio_test_hwpoison(folio)) { > + ret = VM_FAULT_HWPOISON; > + goto out_folio; > + } > + > + if (WARN_ON_ONCE(folio_test_guestmem(folio))) { > + ret = VM_FAULT_SIGBUS; > + goto out_folio; > + } > + > + /* No support for huge pages. */ > + if (WARN_ON_ONCE(folio_nr_pages(folio) > 1)) { > + ret = VM_FAULT_SIGBUS; > + goto out_folio; > + } > + > + if (!folio_test_uptodate(folio)) { > + clear_highpage(folio_page(folio, 0)); > + folio_mark_uptodate(folio); kvm_gmem_mark_prepared() instead of direct folio_mark_uptodate() here, I think (in preparation of things like [1])? Noticed this while rebasing my direct map removal series on top of this and wondering why mmap'd folios sometimes didn't get removed (since it hooks mark_prepared()). Best, Patrick [1]: https://lore.kernel.org/kvm/20241108155056.332412-1-pbonzini@xxxxxxxxxx/