Commit d0d87226 authored by Paolo Bonzini's avatar Paolo Bonzini

KVM: guest_memfd: return folio from __kvm_gmem_get_pfn()

Right now this is simply more consistent and avoids use of pfn_to_page()
and put_page().  It will be put to more use in upcoming patches, to
ensure that the up-to-date flag is set at the very end of both the
kvm_gmem_get_pfn() and kvm_gmem_populate() flows.
Reviewed-by: default avatarMichael Roth <michael.roth@amd.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 5932ca41
...@@ -541,34 +541,34 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot) ...@@ -541,34 +541,34 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot)
fput(file); fput(file);
} }
static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, static struct folio *
gfn_t gfn, kvm_pfn_t *pfn, int *max_order, bool prepare) __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot,
gfn_t gfn, kvm_pfn_t *pfn, int *max_order, bool prepare)
{ {
pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff;
struct kvm_gmem *gmem = file->private_data; struct kvm_gmem *gmem = file->private_data;
struct folio *folio; struct folio *folio;
struct page *page; struct page *page;
int r;
if (file != slot->gmem.file) { if (file != slot->gmem.file) {
WARN_ON_ONCE(slot->gmem.file); WARN_ON_ONCE(slot->gmem.file);
return -EFAULT; return ERR_PTR(-EFAULT);
} }
gmem = file->private_data; gmem = file->private_data;
if (xa_load(&gmem->bindings, index) != slot) { if (xa_load(&gmem->bindings, index) != slot) {
WARN_ON_ONCE(xa_load(&gmem->bindings, index)); WARN_ON_ONCE(xa_load(&gmem->bindings, index));
return -EIO; return ERR_PTR(-EIO);
} }
folio = kvm_gmem_get_folio(file_inode(file), index, prepare); folio = kvm_gmem_get_folio(file_inode(file), index, prepare);
if (IS_ERR(folio)) if (IS_ERR(folio))
return PTR_ERR(folio); return folio;
if (folio_test_hwpoison(folio)) { if (folio_test_hwpoison(folio)) {
folio_unlock(folio); folio_unlock(folio);
folio_put(folio); folio_put(folio);
return -EHWPOISON; return ERR_PTR(-EHWPOISON);
} }
page = folio_file_page(folio, index); page = folio_file_page(folio, index);
...@@ -577,25 +577,25 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, ...@@ -577,25 +577,25 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot,
if (max_order) if (max_order)
*max_order = 0; *max_order = 0;
r = 0;
folio_unlock(folio); folio_unlock(folio);
return folio;
return r;
} }
int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
gfn_t gfn, kvm_pfn_t *pfn, int *max_order) gfn_t gfn, kvm_pfn_t *pfn, int *max_order)
{ {
struct file *file = kvm_gmem_get_file(slot); struct file *file = kvm_gmem_get_file(slot);
int r; struct folio *folio;
if (!file) if (!file)
return -EFAULT; return -EFAULT;
r = __kvm_gmem_get_pfn(file, slot, gfn, pfn, max_order, true); folio = __kvm_gmem_get_pfn(file, slot, gfn, pfn, max_order, true);
fput(file); fput(file);
return r; if (IS_ERR(folio))
return PTR_ERR(folio);
return 0;
} }
EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn); EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn);
...@@ -625,6 +625,7 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long ...@@ -625,6 +625,7 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long
npages = min_t(ulong, slot->npages - (start_gfn - slot->base_gfn), npages); npages = min_t(ulong, slot->npages - (start_gfn - slot->base_gfn), npages);
for (i = 0; i < npages; i += (1 << max_order)) { for (i = 0; i < npages; i += (1 << max_order)) {
struct folio *folio;
gfn_t gfn = start_gfn + i; gfn_t gfn = start_gfn + i;
kvm_pfn_t pfn; kvm_pfn_t pfn;
...@@ -633,9 +634,11 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long ...@@ -633,9 +634,11 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long
break; break;
} }
ret = __kvm_gmem_get_pfn(file, slot, gfn, &pfn, &max_order, false); folio = __kvm_gmem_get_pfn(file, slot, gfn, &pfn, &max_order, false);
if (ret) if (IS_ERR(folio)) {
ret = PTR_ERR(folio);
break; break;
}
if (!IS_ALIGNED(gfn, (1 << max_order)) || if (!IS_ALIGNED(gfn, (1 << max_order)) ||
(npages - i) < (1 << max_order)) (npages - i) < (1 << max_order))
...@@ -644,7 +647,7 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long ...@@ -644,7 +647,7 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long
p = src ? src + i * PAGE_SIZE : NULL; p = src ? src + i * PAGE_SIZE : NULL;
ret = post_populate(kvm, gfn, pfn, p, max_order, opaque); ret = post_populate(kvm, gfn, pfn, p, max_order, opaque);
put_page(pfn_to_page(pfn)); folio_put(folio);
if (ret) if (ret)
break; break;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment