On Mon, Aug 10, 2026, Ackerley Tng wrote:
> "David Hildenbrand (Arm)" <[email protected]> writes:
> 
> > On 8/7/26 23:52, Ackerley Tng via B4 Relay wrote:
> >> From: Ackerley Tng <[email protected]>
> >>
> >> Move the folio initialization logic from kvm_gmem_get_pfn() into
> >> __kvm_gmem_get_pfn() to also zero pages if the page is to be used in
> >> kvm_gmem_populate().
> >>
> >> With in-place conversion, the existing data in a guest_memfd page can be
> >> populated into guest memory through platform-specific ioctls.
> >>
> >> Without first zeroing the page obtained using __kvm_gmem_get_pfn(), it
> >> might contain uninitialized host memory, which would leak to the guest if
> >> the populate completes.
> >>
> >> guest_memfd pages are zeroed at most once in the page's entire lifetime
> >> with guest_memfd, and that is tracked using the uptodate flag.
> >>
> >> Zeroing the page in __kvm_gmem_get_pfn() is chosen over zeroing in
> >> kvm_gmem_get_folio() since other flows, such as a future write() syscall,
> >> can get a page, write to the page and then set page uptodate without
> >> zeroing.
> >>
> >> This aligns with the concept of zeroing before first use - the other place
> >> where zeroing happens is in kvm_gmem_fault_user_mapping().
> >>
> >> Don't mark the page uptodate again after populating, since the page would
> >> already be marked uptodate before the post_populate() call.
> >
> > The downside is that __kvm_gmem_populate() will now zero+write. I assume we
> > don't care about possible performance impacts?
> >
> 
> Would we rather zero in kvm_gmem_populate() only if !uptodate &&
> gmem_in_place_conversion && src_addr == 0? That could work too.

No.  I'm 99% certain we discussed this (multiple times?), and the consensus was
that any performance penalties due to redundant zeroing would pale in comparison
to the cost of actually assigning the page to the VM.

> Previously, without in-place conversion, populate never reads memory
> from guest_memfd so there was no danger of leaking uninitialized memory.
> >> @@ -1159,8 +1159,6 @@ static long __kvm_gmem_populate(struct kvm *kvm, 
> >> struct kvm_memory_slot *slot,
> >>    }
> >>
> >>    ret = post_populate(kvm, gfn, pfn, src_page, opaque);
> >> -  if (!ret)
> >> -          folio_mark_uptodate(folio);
> >
> > In case post-populate failed, do we want to re-zero the pages?
> 
> I believe we can't re-zero the pages. When SNP fails to populate it
> could be because SNP didn't like the CPUIDs userspace set up, and after
> the error userspace is expected to check what SNP likes, then
> retry.
> 
> IIUC zeroing will destroy the message SNP wanted to leave for userspace.
> 
> Michael should be able to explain more here :)

Not Michael, but the above is correct.  If firmware rejects a CPUID page, then
KVM copies back the expected CPUID values provided by firmware.

That said, now that we have have @may_writeback_src we _could_ re-zero the page,
i.e. only zero pages for which @may_writeback_src is %false.

And _that_ said, I vote "no".  KVM zeros the memory mostly to ensure userspace
can't read stale data, e.g. someone else's data.  I don't think we need to
guarantee that a failed populate() (or rather, whatever ioctl called into it)
will leave memory in any particular state.  It would be easier to document that
the page contents may be modified on failure.

Reply via email to