Re: [PATCH v13 10/26] KVM: SEV: Add KVM_SEV_SNP_LAUNCH_UPDATE command

From: Paolo Bonzini
Date: Fri Apr 19 2024 - 12:13:05 EST


On Fri, Apr 19, 2024 at 1:56 PM Paolo Bonzini <pbonzini@xxxxxxxxxx> wrote:
> > + ret = kvm_gmem_populate(kvm, params.gfn_start, u64_to_user_ptr(params.uaddr),
> > + npages, sev_gmem_post_populate, &sev_populate_args);
> > + if (ret < 0) {
> > + argp->error = sev_populate_args.fw_error;
> > + pr_debug("%s: kvm_gmem_populate failed, ret %d (fw_error %d)\n",
> > + __func__, ret, argp->error);
> > + } else if (ret < npages) {
> > + params.len = ret * PAGE_SIZE;
> > + ret = -EINTR;
>
> This probably should 1) update also gfn_start and uaddr 2) return 0
> for consistency with the planned KVM_PRE_FAULT_MEMORY ioctl (aka
> KVM_MAP_MEMORY).

To be more precise, params.len should be set to the number of bytes *left*, i.e.

params.len -= ret * PAGE_SIZE;
params.gfn_start += ret * PAGE_SIZE;
if (params.type != KVM_SEV_SNP_PAGE_TYPE_ZERO)
params.uaddr += ret * PAGE_SIZE;

Also this patch needs some other changes:

1) snp_launch_update() should have something like this:

src = params.type == KVM_SEV_SNP_PAGE_TYPE_ZERO ? NULL :
u64_to_user_ptr(params.uaddr),;

so that then...

> + vaddr = kmap_local_pfn(pfn + i);
> + ret = copy_from_user(vaddr, src + i * PAGE_SIZE, PAGE_SIZE);
> + if (ret) {
> + pr_debug("Failed to copy source page into GFN 0x%llx\n", gfn);
> + goto out_unmap;
> + }

.. the copy can be done only if src is non-NULL

2) the struct should have some more fields

> + struct kvm_sev_snp_launch_update {
> + __u64 gfn_start; /* Guest page number to load/encrypt data into. */
> + __u64 uaddr; /* Userspace address of data to be loaded/encrypted. */
> + __u32 len; /* 4k-aligned length in bytes to copy into guest memory.*/
> + __u8 type; /* The type of the guest pages being initialized. */

__u8 pad0;
__u16 flags; // must be zero
__u64 pad1[5];

with accompanying flags check in snp_launch_update().

If you think IMI can be implemented already (with a bit in flags) go
ahead and do it.

Paolo