Re: [RFC PATCH 11/14] KVM: guest_memfd: Add kvm_gmem_restore() for PFN mode providers
From: Frederic Griffoul
Date: Thu Oct 29 2026 - 10:32:33 EST
On Thu, Oct 29, 2026 at 05:52:40AM -0700, Ackerley Tng wrote:
> Fred Griffoul <griffoul@xxxxxxxxx> writes:
>
> > kvm_gmem_revoke() does not fit: it would
> > zap the guest's and the VMM's mappings and reclaim frames that a guest
> > may already use.
> >
>
> I'd drop this paragraph from the commit message, kvm_gmem_revoke() is
> the complete opposite and isn't a candidate at all for the goal, which
> is to notify importers that a page is now available (and they can map it
> in).
Sure, I'll drop it.
> provider --1--> guest_memfd --2--> IOMMU
>
> 2 is definitely required, but why is 1 required?
>
> Between the first .get_pfn() or .lookup_pfn_range() () and
> .put_pfn_range() or kvm_gmem_revoke(), the provider should consider the
> memory in-use by guest_memfd.
>
> When it's in-use by guest_memfd, then guest_memfd should use --2--> to
> map into IOMMU page tables as necessary.
>
> When it's not in-use by guest_memfd, why would the provider force it
> into guest_memfd?
>
> If the guest VM knows that the page is now usable (some other
> communication channel), it would touch the memory and do a .get_pfn(),
> that would be successful this time. Perhaps that's when --2--> needs to
> be performed?
The IOMMU never asks, and the CPU may not touch the page before a
device does. A few cases where only the provider knows:
- The provider gives frames back with no guest involved: a page that
arrives during post-copy, a range moved back to the VM, memory given
to an overcommitted VM.
- The guest already gave that IOVA to a device as a DMA buffer. The
device writes it without any CPU access, so there is no .get_pfn()
from a stage-2 fault, and most devices cannot fault.
- A read-only frame becomes writable. KVM finds out on the next write
fault, but a device write to a read-only IOPTE just fails.
kvm_gmem_restore() doesn't push the memory into guest_memfd: guest_memfd
only passes it on to its exports. Stage-2 and userspace mappings stay
lazy, and iommufd looks the range up again and maps it.
I'll make that clear in the kernel-doc and the commit message. Would
that work for you?
Fred