KVM: guest_memfd: return locked folio from __kvm_gmem_get_pfn
authorPaolo Bonzini <pbonzini@redhat.com>
Thu, 11 Jul 2024 22:27:48 +0000 (18:27 -0400)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 26 Jul 2024 18:46:14 +0000 (14:46 -0400)
Allow testing the up-to-date flag in the caller without taking the
lock again.

Reviewed-by: Michael Roth <michael.roth@amd.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
virt/kvm/guest_memfd.c

index 76139332f2f388230ac1bbe21da475901dabb752..9271aba9b7b32e60eab16123c20c2d7b9f196521 100644 (file)
@@ -59,6 +59,7 @@ static int kvm_gmem_prepare_folio(struct inode *inode, pgoff_t index, struct fol
        return 0;
 }
 
+/* Returns a locked folio on success.  */
 static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index, bool prepare)
 {
        struct folio *folio;
@@ -551,6 +552,7 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot)
        fput(file);
 }
 
+/* Returns a locked folio on success.  */
 static struct folio *
 __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot,
                   gfn_t gfn, kvm_pfn_t *pfn, int *max_order, bool prepare)
@@ -584,7 +586,6 @@ __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot,
        if (max_order)
                *max_order = 0;
 
-       folio_unlock(folio);
        return folio;
 }
 
@@ -602,6 +603,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
        if (IS_ERR(folio))
                return PTR_ERR(folio);
 
+       folio_unlock(folio);
        return 0;
 }
 EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn);
@@ -647,6 +649,7 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long
                        break;
                }
 
+               folio_unlock(folio);
                if (!IS_ALIGNED(gfn, (1 << max_order)) ||
                    (npages - i) < (1 << max_order))
                        max_order = 0;