Re: [PATCH 3/3] KVM: guest_memfd: GUP source pages prior to populating guest memory
From: Yan Zhao
Date: Thu Dec 04 2025 - 22:40:59 EST
On Wed, Dec 03, 2025 at 10:26:48PM +0800, Michael Roth wrote:
> Look at your
> changelog for the change above, for instance: it has no relevance in the
> context of this series. What do I put in its place? Bug reports about
> my experimental tree? It's just not the right place to try to justify
> these changes.
The following diff is reasonable to this series(if npages is up to 2MB),
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -878,11 +878,10 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long
}
folio_unlock(folio);
- WARN_ON(!IS_ALIGNED(gfn, 1 << max_order) ||
- (npages - i) < (1 << max_order));
ret = -EINVAL;
- while (!kvm_range_has_memory_attributes(kvm, gfn, gfn + (1 << max_order),
+ while (!IS_ALIGNED(gfn, 1 << max_order) || (npages - i) < (1 << max_order) ||
+ !kvm_range_has_memory_attributes(kvm, gfn, gfn + (1 << max_order),
KVM_MEMORY_ATTRIBUTE_PRIVATE,
KVM_MEMORY_ATTRIBUTE_PRIVATE)) {
if (!max_order)
because:
1. kmalloc_array() + GUP 2MB src pages + returning -ENOMEM in "Hunk 1" is a
waste if max_order is always 0.
2. If we allow max_order > 0, then we must remove the WARN_ON().
3. When start_gfn is not 2MB aligned, just allocating 4KB src page each round is
enough (as in Sean's sketch patch).
Hunk 1: -------------------------------------------------------------------
src_npages = IS_ALIGNED((unsigned long)src, PAGE_SIZE) ? npages : npages + 1;
src_pages = kmalloc_array(src_npages, sizeof(struct page *), GFP_KERNEL);
if (!src_pages)
return -ENOMEM;
ret = get_user_pages_fast((unsigned long)src, src_npages, 0, src_pages);
if (ret < 0)
return ret;
if (ret != src_npages)
return -ENOMEM;
Hunk 2: -------------------------------------------------------------------
for (i = 0; i < npages; i += (1 << max_order)) {
...
folio = __kvm_gmem_get_pfn(file, slot, index, &pfn, &max_order);
WARN_ON(!IS_ALIGNED(gfn, 1 << max_order) ||
(npages - i) < (1 << max_order));
ret = post_populate(kvm, gfn, pfn, src ? &src_pages[i] : NULL,
src_offset, max_order, opaque);
...
}