[PATCH v6 6/9] KVM: guest_memfd: Only "prepare" folios for private pages

From: Sean Christopherson

Date: Thu Jul 23 2026 - 17:11:55 EST


From: Ackerley Tng <ackerleytng@xxxxxxxxxx>

When getting a guest_memfd pfn, prepare the folio, i.e. convert its pages
to private, if and only if the page is actually private. The misnamed
prepare() hook exists specifically to allow x86's SNP to assign pages to
the owning VM in the RMP when mapping private memory into a guest.

Guarding the call will allow renaming the prepare() hook to better reflect
its role, without creating a semantic mess, and will become a hard
requirement once in-place conversion is supported, i.e. when CoCo VMs
support SHARED guest_memfd pages.

For all intents, no functional change intended (the sole arch hook is a nop
for SHARED memory).

Suggested-by: Michael Roth <michael.roth@xxxxxxx>
Reviewed-by: Fuad Tabba <tabba@xxxxxxxxxx>
[sean: rewrite changelog to fit the context]
Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx>
---
virt/kvm/guest_memfd.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index e9d61410e5fb..f64cd87ec8b0 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -814,7 +814,8 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
folio_mark_uptodate(folio);
}

- r = kvm_gmem_prepare_folio(kvm, slot, gfn, folio);
+ if (kvm_gmem_is_private_mem(file_inode(file), index))
+ r = kvm_gmem_prepare_folio(kvm, slot, gfn, folio);

folio_unlock(folio);

--
2.55.0.229.g6434b31f56-goog