]> git.hungrycats.org Git - linux/commitdiff
KVM: guest_memfd: Split bind() into prepare()+commit() phases
authorSean Christopherson <seanjc@google.com>
Tue, 22 Sep 2026 00:13:30 +0000 (17:13 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 6 Nov 2026 17:10:49 +0000 (12:10 -0500)
commit 83411f32b8ad96c6b05b1511d1a588daab44bd25 upstream.

Split binding a memslot to a guest_memfd instance into prepare() and
commit() phases so that KVM can separate preparing the memslot from binding
the memslot to the gmem instance, i.e. from committing the memslot.  This
will allow waiting to commit the memslot+gmem binding until the memslot is
fully prepared, which is necessary as the memslot becomes reachable when
the binding is created.

As a bonus, drop the unwind-on-failure from the commit phase (other than
nullifying the bindings), as the only reason bind() did the full unwind is
because it technically didn't own the memslot, i.e. "needed" to leave
memslot in the same state it started in.

No functional change intended (the unwinding down on bind() failure was
effectively dead code since KVM simply deletes the memslot on failure,
i.e. there was nothing that could actually observe the unwind).

Cc: stable@vger.kernel.org
Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
Link: https://patch.msgid.link/20260922001332.1121266-5-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
virt/kvm/guest_memfd.c
virt/kvm/guest_memfd.h
virt/kvm/kvm_main.c

index 55de16c96327d3ef5cbb422f46bc3b403cceba87..7fb8295f424b69780fa9ced65b6465219d1a51d5 100644 (file)
@@ -603,15 +603,14 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)
        return __kvm_gmem_create(kvm, size, flags);
 }
 
-int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
-                 unsigned int fd, uoff_t offset)
+int kvm_gmem_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *slot,
+                                  unsigned int fd, uoff_t offset)
 {
        uoff_t size = slot->npages << PAGE_SHIFT;
-       unsigned long start, end;
        struct gmem_file *f;
        struct inode *inode;
        struct file *file;
-       int r = -EINVAL;
+
 
        BUILD_BUG_ON(sizeof(gpa_t) != sizeof(offset));
        BUILD_BUG_ON(sizeof(gfn_t) != sizeof(slot->gmem.pgoff));
@@ -635,44 +634,55 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
        if (!PAGE_ALIGNED(offset) || offset + size > i_size_read(inode))
                goto err;
 
-       filemap_invalidate_lock(inode->i_mapping);
-
-       start = offset >> PAGE_SHIFT;
-       end = start + slot->npages;
-
-       if (!xa_empty(&f->bindings) &&
-           xa_find(&f->bindings, &start, end - 1, XA_PRESENT)) {
-               r = -EEXIST;
-               filemap_invalidate_unlock(inode->i_mapping);
-               goto err;
-       }
-
        /*
         * memslots of flag KVM_MEM_GUEST_MEMFD are immutable to change, so
         * kvm_gmem_bind() must occur on a new memslot.  Because the memslot
         * is not visible yet, kvm_gmem_get_pfn() is guaranteed to see the file.
         */
        WRITE_ONCE(slot->gmem.file, file);
-       slot->gmem.pgoff = start;
+       slot->gmem.pgoff = offset >> PAGE_SHIFT;
        if (kvm_gmem_supports_mmap(inode))
                slot->flags |= KVM_MEMSLOT_GMEM_ONLY;
 
-       r = xa_err(xa_store_range(&f->bindings, start, end - 1, slot, GFP_KERNEL));
-       if (r) {
-               xa_store_range(&f->bindings, start, end - 1, NULL, GFP_KERNEL);
-               slot->gmem.file = NULL;
-               slot->gmem.pgoff = 0;
-               slot->flags &= ~KVM_MEMSLOT_GMEM_ONLY;
-       }
-       filemap_invalidate_unlock(inode->i_mapping);
-
        /*
-        * Drop the reference to the file, even on success.  The file pins KVM,
-        * not the other way 'round.  Active bindings are invalidated if the
-        * file is closed before memslots are destroyed.
+        * Gift the caller a reference to the file.  The reference will be
+        * dropped after bindings are established, or if installing the new
+        * memslot ultimately fails.
         */
+       return 0;
+
 err:
        fput(file);
+       return -EINVAL;
+}
+
+int kvm_gmem_commit_memory_region(struct kvm *kvm, struct kvm_memory_slot *slot)
+{
+       struct gmem_file *f = slot->gmem.file->private_data;
+       struct inode *inode = file_inode(slot->gmem.file);
+       unsigned long start, end;
+       int r;
+
+       if (WARN_ON_ONCE(slot->gmem.file->f_op != &kvm_gmem_fops))
+               return -EIO;
+
+       filemap_invalidate_lock(inode->i_mapping);
+
+       start = slot->gmem.pgoff;
+       end = start + slot->npages;
+
+       if (!xa_empty(&f->bindings) &&
+           xa_find(&f->bindings, &start, end - 1, XA_PRESENT)) {
+               filemap_invalidate_unlock(inode->i_mapping);
+               return -EEXIST;
+       }
+
+       r = xa_err(xa_store_range(&f->bindings, start, end - 1, slot, GFP_KERNEL));
+       if (r)
+               xa_store_range(&f->bindings, start, end - 1, NULL, GFP_KERNEL);
+
+       filemap_invalidate_unlock(inode->i_mapping);
+
        return r;
 }
 
index 0f9c6f84083894b3b391d8336f97f6e3a61919cc..01bd359d27e307e63cd3d4728ada46fce313d236 100644 (file)
@@ -8,8 +8,9 @@
 int kvm_gmem_init(struct module *module);
 void kvm_gmem_exit(void);
 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);
+int kvm_gmem_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *slot,
+                                  unsigned int fd, uoff_t offset);
+int kvm_gmem_commit_memory_region(struct kvm *kvm, struct kvm_memory_slot *slot);
 void kvm_gmem_unbind(struct kvm_memory_slot *slot);
 #else
 static inline int kvm_gmem_init(struct module *module)
@@ -17,9 +18,17 @@ static inline int kvm_gmem_init(struct module *module)
        return 0;
 }
 static inline void kvm_gmem_exit(void) {};
-static inline int kvm_gmem_bind(struct kvm *kvm,
-                                        struct kvm_memory_slot *slot,
-                                        unsigned int fd, uoff_t offset)
+
+static inline int kvm_gmem_prepare_memory_region(struct kvm *kvm,
+                                                struct kvm_memory_slot *slot,
+                                                unsigned int fd, uoff_t offset)
+{
+       WARN_ON_ONCE(1);
+       return -EIO;
+}
+
+static inline int kvm_gmem_commit_memory_region(struct kvm *kvm,
+                                               struct kvm_memory_slot *slot)
 {
        WARN_ON_ONCE(1);
        return -EIO;
index 07a3511ed056e6028f928e674f4d705614d1e46c..a7c12afed34f77c6bd2ce56a0308424991ba53fb 100644 (file)
@@ -2123,7 +2123,23 @@ static int kvm_set_memory_region(struct kvm *kvm,
        new->flags = mem->flags;
        new->userspace_addr = mem->userspace_addr;
        if (change == KVM_MR_CREATE && (mem->flags & KVM_MEM_GUEST_MEMFD)) {
-               r = kvm_gmem_bind(kvm, new, mem->guest_memfd, mem->guest_memfd_offset);
+               r = kvm_gmem_prepare_memory_region(kvm, new, mem->guest_memfd,
+                                                  mem->guest_memfd_offset);
+               if (r)
+                       goto out;
+
+               r = kvm_gmem_commit_memory_region(kvm, new);
+
+               /*
+                * Drop the reference to the file, even on success.  The file
+                * pins KVM, not the other way 'round.  Active bindings are
+                * invalidated if the file is closed before memslots are
+                * destroyed.
+                */
+#ifdef CONFIG_KVM_GUEST_MEMFD
+                fput(new->gmem.file);
+#endif
+
                if (r)
                        goto out;
        }