When releasing the last file associated with an inode, invalidate its mappings and notify the provider that no private ranges remain. Hold the provider lock across both operations so device unlock waits for cleanup. Skip notification if provider attachment failed. Signed-off-by: Aneesh Kumar K.V (Arm) --- virt/kvm/guest_memfd.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index d3ae01aa6fe6..bdee42ff6b26 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -87,7 +87,6 @@ static bool gmem_provider_is_device(struct gmem_inode *gi) return gi->provider_ops && gi->provider_ops->device_memory; } - static bool kvm_gmem_is_pfn_mode(struct gmem_inode *gi) { return gi->flags & GUEST_MEMFD_FLAG_PFN_MODE; @@ -904,12 +903,28 @@ static long kvm_gmem_fallocate(struct file *file, int mode, loff_t offset, return ret; } +/* No file users remain, so private attributes cannot create new mappings. */ +static void gmem_provider_release_views(struct inode *inode) +{ + struct gmem_inode *gi = GMEM_I(inode); + + /* File release also runs after a failed provider attachment. */ + if (!gi->provider_ops) + return; + + if (gi->provider_ops->update_private_ranges && + list_is_singular(&gi->gmem_file_list)) + gi->provider_ops->update_private_ranges(gi->provider, false); +} + static int kvm_gmem_release(struct inode *inode, struct file *file) { struct gmem_file *f = file->private_data; + struct gmem_inode *gi = GMEM_I(inode); struct kvm_memory_slot *slot; struct kvm *kvm = f->kvm; unsigned long index; + enum kvm_gfn_range_filter filter = kvm_gmem_get_all_gfns_filter(inode); /* * Prevent concurrent attempts to *unbind* a memslot. This is the last @@ -922,6 +937,8 @@ static int kvm_gmem_release(struct inode *inode, struct file *file) filemap_invalidate_lock(inode->i_mapping); + gmem_provider_lock_updates(gi); + /* * Note! synchronize_srcu() is _not_ needed after nullifying memslot * bindings as slot->gmem.file cannot be set back to a non-null value @@ -975,9 +992,10 @@ static int kvm_gmem_release(struct inode *inode, struct file *file) * Zap all SPTEs pointed at by this file. Do not free the backing * memory, as its lifetime is associated with the inode, not the file. */ - __kvm_gmem_invalidate_start(f, 0, -1ul, - kvm_gmem_get_all_gfns_filter(inode)); + __kvm_gmem_invalidate_start(f, 0, -1ul, filter); __kvm_gmem_invalidate_end(f, 0, -1ul); + gmem_provider_release_views(inode); + gmem_provider_unlock_updates(gi); list_del(&f->entry); -- 2.43.0