[PATCH v4 17/18] KVM: guest_memfd: Align the gfn as well as the pfn when "preparing" a folio

From: Sean Christopherson

Date: Thu Jul 09 2026 - 16:54:11 EST


When preparing a guest_memfd folio, align the gfn as well as the pfn, i.e.
preserve the effective gfn+pfn pair provided by the caller. If the folio
is larger than order-0, aligning the pfn but not the gfn would create a
mismatch set and unintentionally crash the guest due to assigning the pfn
at the wrong gfn.

Fixes: b85524314a3d ("KVM: guest_memfd: delay kvm_gmem_prepare_folio() until the memory is passed to the guest")
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
---
virt/kvm/guest_memfd.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index cd7a795834cb..4082cde3f3fc 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -87,8 +87,8 @@ static int kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *slot,
* checked when creating memslots.
*/
WARN_ON_ONCE(!IS_ALIGNED(slot->gmem.pgoff, nr_pages));
+ gfn = ALIGN_DOWN(gfn, nr_pages);
index = kvm_gmem_get_index(slot, gfn);
- index = ALIGN_DOWN(index, nr_pages);

return kvm_arch_gmem_prepare(kvm, gfn_to_gpa(gfn),
folio_file_pfn(folio, index), nr_pages,
--
2.55.0.795.g602f6c329a-goog