On 02/06/2026 09:55, Suzuki K Poulose wrote:
On 23/05/2026 01:17, Ackerley Tng via B4 Relay wrote:
From: Ackerley Tng <[email protected]>

All-shared guest_memfd used to be only supported for non-CoCo VMs where
preparation doesn't apply. INIT_SHARED is about to be supported for
non-CoCo VMs in a later patch in this series.

nit: s/non-CoCo/CoCo ?


In addition, KVM_SET_MEMORY_ATTRIBUTES2 is about to be supported in
guest_memfd in a later patch in this series.

This means that the kvm fault handler may now call kvm_gmem_get_pfn() on a
shared folio for a CoCo VM where preparation applies.

Add a check to make sure that preparation is only performed for private
folios.

Preparation will be undone on freeing (see kvm_gmem_free_folio()) and on
conversion to shared.

Signed-off-by: Michael Roth <[email protected]>

nit: Missing Co-Developed-by: ?

Reviewed-by: Fuad Tabba <[email protected]>
Signed-off-by: Ackerley Tng <[email protected]>
---
  virt/kvm/guest_memfd.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index 78e5435967341..adf57a3a1f5dd 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -894,6 +894,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
               int *max_order)
  {
      pgoff_t index = kvm_gmem_get_index(slot, gfn);
+    struct inode *inode;
      struct folio *folio;
      int r = 0;
@@ -901,7 +902,8 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
      if (!file)
          return -EFAULT;
-    filemap_invalidate_lock_shared(file_inode(file)->i_mapping);
+    inode = file_inode(file);
+    filemap_invalidate_lock_shared(inode->i_mapping);
      folio = __kvm_gmem_get_pfn(file, slot, index, pfn, max_order);
      if (IS_ERR(folio)) {
@@ -914,7 +916,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(inode, index))

Don't we need to make sure the entire folio is private ? Not just the page at the index ?
     if (kvm_gmem_range_is_private(, index, folio_nr_pages(folio)) ?

Or rather, we should go through the individual pages and apply the
prepare for ones that are private ?

Suzuki


Suzuki

+        r = kvm_gmem_prepare_folio(kvm, slot, gfn, folio);
      folio_unlock(folio);
@@ -924,7 +927,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
          folio_put(folio);
  out:
-    filemap_invalidate_unlock_shared(file_inode(file)->i_mapping);
+    filemap_invalidate_unlock_shared(inode->i_mapping);
      return r;
  }
  EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_gmem_get_pfn);




Reply via email to