On Tue, Dec 28, 2021 at 09:48:08PM +0000, Sean Christopherson wrote: > On Fri, Dec 24, 2021, Chao Peng wrote: > > On Thu, Dec 23, 2021 at 06:02:33PM +0000, Sean Christopherson wrote: > > > On Thu, Dec 23, 2021, Chao Peng wrote: > > > > > > In other words, there needs to be a 1:1 gfn:file+offset mapping. Since userspace > > > likely wants to allocate a single file for guest private memory and map it into > > > multiple discontiguous slots, e.g. to skip the PCI hole, the best idea off the top > > > of my head would be to register the notifier on a per-slot basis, not a per-VM > > > basis. It would require a 'struct kvm *' in 'struct kvm_memory_slot', but that's > > > not a huge deal. > > > > > > That way, KVM's notifier callback already knows the memslot and can compute overlap > > > between the memslot and the range by reversing the math done by kvm_memfd_get_pfn(). > > > Then, armed with the gfn and slot, invalidation is just a matter of constructing > > > a struct kvm_gfn_range and invoking kvm_unmap_gfn_range(). > > > > KVM is easy but the kernel bits would be difficulty, it has to maintain > > fd+offset to memslot mapping because one fd can have multiple memslots, > > it need decide which memslot needs to be notified. > > No, the kernel side maintains an opaque pointer like it does today, But the opaque pointer will now become memslot, isn't it? That said, kernel side should maintain a list of opaque pointer (memslot) instead of one for each fd (inode) since a fd to memslot mapping is 1:M now. >KVM handles > reverse engineering the memslot to get the offset and whatever else it needs. > notify_fallocate() and other callbacks are unchanged, though they probably can > drop the inode. > > E.g. likely with bad math and handwaving on the overlap detection: > > int kvm_private_fd_fallocate_range(void *owner, pgoff_t start, pgoff_t end) > { > struct kvm_memory_slot *slot = owner; > struct kvm_gfn_range gfn_range = { > .slot = slot, > .start = (start - slot->private_offset) >> PAGE_SHIFT, > .end = (end - slot->private_offset) >> PAGE_SHIFT, > .may_block = true, > }; > > if (!has_overlap(slot, start, end)) > return 0; > > gfn_range.end = min(gfn_range.end, slot->base_gfn + slot->npages); > > kvm_unmap_gfn_range(slot->kvm, &gfn_range); > return 0; > } I understand this KVM side handling, but again one fd can have multiple memslots. How shmem decides to notify which memslot from a list of memslots when it invokes the notify_fallocate()? Or just notify all the possible memslots then let KVM to check? Thanks, Chao