Unmap guest_memfd ranges before removing their offset-to-GPA bindings. Invalidate shared and private mappings while holding inode and provider exclusion, using the same common invalidation path as revocation. Do not convert device attributes when deleting a memslot. Private file attributes continue to block TDI UNLOCK after unbind; userspace must explicitly share those ranges or release the last file reference. Cc: Paolo Bonzini Cc: Sean Christopherson Cc: David Hildenbrand Cc: kvm@vger.kernel.org Cc: patches@lists.linux.dev Signed-off-by: Aneesh Kumar K.V (Arm) --- virt/kvm/guest_memfd.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 22793d3c6aff..e31234212dcf 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -1860,6 +1860,8 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot) unsigned long start = slot->gmem.pgoff; unsigned long end = start + slot->npages; struct gmem_file *f; + struct gmem_inode *gi; + enum kvm_gfn_range_filter filter; /* * Nothing to do if the underlying file was _already_ closed, as @@ -1879,8 +1881,13 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot) * drops slots_lock, so there's no need to verify the file is live. */ f = file->private_data; + gi = GMEM_I(file_inode(file)); + filter = kvm_gmem_get_all_gfns_filter(file_inode(file)); filemap_invalidate_lock(file->f_mapping); + gmem_provider_lock(gi); + __kvm_gmem_invalidate_start(f, start, end, filter); + __kvm_gmem_invalidate_end(f, start, end); xa_store_range(&f->bindings, start, end - 1, NULL, GFP_KERNEL); /* @@ -1888,6 +1895,7 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot) * cannot see this memslot. */ WRITE_ONCE(slot->gmem.file, NULL); + gmem_provider_unlock(gi); filemap_invalidate_unlock(file->f_mapping); } -- 2.43.0