[RFC PATCH v1 6/6] KVM: Invalidate guest_memfd mappings before removing memslot bindings

From: Aneesh Kumar K.V (Arm)

Date: Sat Oct 10 2026 - 03:28:57 EST


Unmap guest_memfd ranges before deleting their memslot bindings. Remove
private device mappings before committing deletion, and invalidate
remaining mappings while holding the inode invalidate lock.

Cc: Paolo Bonzini <pbonzini@xxxxxxxxxx>
Cc: Sean Christopherson <seanjc@xxxxxxxxxx>
Cc: David Hildenbrand <david@xxxxxxxxxx>
Cc: kvm@xxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
Assisted-by: Codex
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@xxxxxxxxxx>
---
virt/kvm/guest_memfd.c | 70 ++++++++++++++++++++++++++++++++++++++++--
virt/kvm/guest_memfd.h | 6 ++++
virt/kvm/kvm_main.c | 4 +++
3 files changed, 77 insertions(+), 3 deletions(-)

diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index f493b452fae8..f58270d0510d 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -1330,6 +1330,58 @@ static int kvm_gmem_device_convert(struct inode *inode, pgoff_t start,
return ret;
}

+/**
+ * kvm_gmem_prepare_memslot_delete() - remove guest_memfd mappings before deletion
+ * @kvm: VM whose memslot is being removed
+ * @slot: original guest_memfd memslot being removed
+ *
+ * The caller has replaced the active slot with an entry marked
+ * KVM_MEMSLOT_INVALID, preventing new guest faults from creating mappings.
+ * Remove private device mappings, then invalidate the remaining guest_memfd
+ * mappings, including shared mappings.
+ *
+ * Perform this cleanup while the guest_memfd binding still provides the
+ * file-offset-to-GPA translation needed to locate the guest mappings.
+ * The device provider remains attached until guest_memfd close.
+ *
+ * Caller holds slots_lock.
+ * Return: 0 on success or a negative error code.
+ */
+int kvm_gmem_prepare_memslot_delete(const struct kvm_memory_slot *slot)
+{
+ struct file *file = READ_ONCE(slot->gmem.file);
+ pgoff_t start = slot->gmem.pgoff;
+ pgoff_t end = start + slot->npages;
+ struct inode *inode;
+ int ret;
+
+ /* slots_lock keeps the bindings alive until file release can finish. */
+ file = get_file_active(&file);
+ if (!file) {
+ /* Let .release drain the bindings before deletion can discard them. */
+ if (READ_ONCE(slot->gmem.file))
+ return -EBUSY;
+ return 0;
+ }
+ inode = file_inode(file);
+ if (GMEM_I(inode)->device_ops) {
+ ret = kvm_gmem_device_convert(inode, start, slot->npages, NULL, NULL);
+ if (ret)
+ goto out;
+ }
+
+ filemap_invalidate_lock(inode->i_mapping);
+ __kvm_gmem_invalidate_start(file->private_data, start, end,
+ kvm_gmem_get_all_gfns_filter(inode));
+ __kvm_gmem_invalidate_end(file->private_data, start, end);
+ filemap_invalidate_unlock(inode->i_mapping);
+ ret = 0;
+out:
+ fput(file);
+ return ret;
+}
+
+
int kvm_gmem_device_map(struct kvm *kvm, u64 gpa, u64 size, u64 pa, u64 vdev_id)
{
struct guest_memfd_device_request req = { gpa, pa, vdev_id };
@@ -1620,11 +1672,19 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
return r;
}

-static void __kvm_gmem_unbind(struct kvm_memory_slot *slot, struct gmem_file *f)
+static void __kvm_gmem_unbind(struct kvm_memory_slot *slot, struct file *file)
{
+ struct gmem_file *f = file->private_data;
unsigned long start = slot->gmem.pgoff;
unsigned long end = start + slot->npages;

+ /*
+ * Unmap this slot's guest mappings before removing it from f->bindings.
+ * The invalidation helper walks f->bindings to find the slots to unmap.
+ */
+ __kvm_gmem_invalidate_start(f, start, end,
+ kvm_gmem_get_all_gfns_filter(file_inode(file)));
+ __kvm_gmem_invalidate_end(f, start, end);
xa_store_range(&f->bindings, start, end - 1, NULL, GFP_KERNEL);

/*
@@ -1656,12 +1716,16 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot)
* until the caller drops slots_lock.
*/
if (!file) {
- __kvm_gmem_unbind(slot, slot->gmem.file->private_data);
+ struct file *closing_file = slot->gmem.file;
+
+ filemap_invalidate_lock(closing_file->f_mapping);
+ __kvm_gmem_unbind(slot, closing_file);
+ filemap_invalidate_unlock(closing_file->f_mapping);
return;
}

filemap_invalidate_lock(file->f_mapping);
- __kvm_gmem_unbind(slot, file->private_data);
+ __kvm_gmem_unbind(slot, file);
filemap_invalidate_unlock(file->f_mapping);
}

diff --git a/virt/kvm/guest_memfd.h b/virt/kvm/guest_memfd.h
index 0f9c6f840838..6fad6adfdadc 100644
--- a/virt/kvm/guest_memfd.h
+++ b/virt/kvm/guest_memfd.h
@@ -11,6 +11,7 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args);
int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
unsigned int fd, uoff_t offset);
void kvm_gmem_unbind(struct kvm_memory_slot *slot);
+int kvm_gmem_prepare_memslot_delete(const struct kvm_memory_slot *slot);
#else
static inline int kvm_gmem_init(struct module *module)
{
@@ -29,6 +30,11 @@ static inline void kvm_gmem_unbind(struct kvm_memory_slot *slot)
{
WARN_ON_ONCE(1);
}
+
+static inline int kvm_gmem_prepare_memslot_delete(const struct kvm_memory_slot *slot)
+{
+ return 0;
+}
#endif /* CONFIG_KVM_GUEST_MEMFD */

#endif /* __KVM_GUEST_MEMFD_H__ */
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index f3f46a8563b3..58da57b8a268 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1696,6 +1696,10 @@ static int kvm_prepare_memory_region(struct kvm *kvm,
}

r = kvm_arch_prepare_memory_region(kvm, old, new, change);
+ if (!r && change == KVM_MR_DELETE) {
+ if (old->flags & KVM_MEM_GUEST_MEMFD)
+ r = kvm_gmem_prepare_memslot_delete(old);
+ }

/* Free the bitmap on failure if it was allocated above. */
if (r && new && new->dirty_bitmap && (!old || !old->dirty_bitmap))
--
2.43.0