Re: [PATCH v5 7/7] KVM: guest_memfd: Rework PREPARE config and hook into a more generic CONVERT
From: Ackerley Tng
Date: Thu Jul 16 2026 - 17:42:30 EST
Xiaoyao Li <xiaoyao.li@xxxxxxxxx> writes:
> On 7/15/2026 7:10 AM, Sean Christopherson wrote:
>> Rework guest_memfd's "prepare" hook into a more generic "convert" flow in
>> anticipation of supporting in-place conversion, at which point KVM will use
>> the hook for both to-private and to-shared conversions, not just to
>> "prepare" PRIVATE memory.
>>
>> Opportunistically rename kvm_gmem_prepare_folio() to kvm_gmem_make_private()
>> to better reflect its role.
>>
>
> [...]
>
>> @@ -90,8 +90,8 @@ static int kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *slot,
>> gfn = ALIGN_DOWN(gfn, nr_pages);
>> index = kvm_gmem_get_index(slot, gfn);
>>
>> - return kvm_arch_gmem_prepare(kvm, gfn, folio_file_pfn(folio, index),
>> - nr_pages, folio_order(folio));
>> + return kvm_arch_gmem_convert(kvm, gfn, folio_file_pfn(folio, index),
>> + nr_pages, folio_order(folio), true);
>> #else
>> return 0;
>> #endif
>> @@ -798,7 +798,7 @@ 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);
>> + r = kvm_gmem_make_private(kvm, slot, gfn, folio);
>
> I think this and above renaming don't make sense, because
> kvm_gmem_get_pfn() can be invoked for shared memory for non-Coco VMs
> when KVM_MEMSLOT_GMEM_ONLY is set.
>
Makes sense.
> Maybe this patch can be moved to gmem in-place series and after [1]?
>
> [1]
> https://lore.kernel.org/all/3b64e897-93a8-4f9c-88a9-f416ff44b09d@xxxxxxxxx/
>
How about doing everything else in this patch in this patch, other than
this:
diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index 173a7ed008d17..9df6927dd8f55 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -66,8 +66,8 @@ static pgoff_t kvm_gmem_get_index(struct
kvm_memory_slot *slot, gfn_t gfn)
* On successful return the guest sees a zero page so as to avoid
* leaking host data and the up-to-date flag is set.
*/
-static int kvm_gmem_make_private(struct kvm *kvm, struct kvm_memory_slot *slot,
- gfn_t gfn, struct folio *folio)
+static int kvm_gmem_prepare_folio(struct kvm *kvm, struct
kvm_memory_slot *slot,
+ gfn_t gfn, struct folio *folio)
{
#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_CONVERT
unsigned long nr_pages = folio_nr_pages(folio);
@@ -798,7 +798,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct
kvm_memory_slot *slot,
folio_mark_uptodate(folio);
}
- r = kvm_gmem_make_private(kvm, slot, gfn, folio);
+ r = kvm_gmem_prepare_folio(kvm, slot, gfn, folio);
folio_unlock(folio);
I'll move just this renaming to [1] like you suggested.
I think it's okay to continue to always call prepare_folio(), and within
the prepare_folio() function, only do conversion when the CONVERT CONFIG
is defined.
I'll also move "Opportunistically rename kvm_gmem_prepare_folio() to
kvm_gmem_make_private() to better reflect its role." from Sean's commit
message to [1].
>> folio_unlock(folio);
>>