On Thu, Aug 07, 2025, Sagi Shahar wrote:
> From: Ackerley Tng <[email protected]>
> 
> If guest memory is backed by restricted memfd
> 
> + UPM is being used, hence encrypted memory region has to be
>   registered
> + Can avoid making a copy of guest memory before getting TDX to
>   initialize the memory region
> 
> Signed-off-by: Ackerley Tng <[email protected]>
> Signed-off-by: Sagi Shahar <[email protected]>
> ---
>  .../selftests/kvm/lib/x86/tdx/tdx_util.c      | 38 +++++++++++++++----
>  1 file changed, 30 insertions(+), 8 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c 
> b/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c
> index bb074af4a476..e2bf9766dc03 100644
> --- a/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c
> +++ b/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c
> @@ -324,6 +324,21 @@ static void tdx_td_finalize_mr(struct kvm_vm *vm)
>       tdx_ioctl(vm->fd, KVM_TDX_FINALIZE_VM, 0, NULL);
>  }
>  
> +/*
> + * Other ioctls
> + */
> +
> +/*
> + * Register a memory region that may contain encrypted data in KVM.
> + */

Drop these comments.

> +static void register_encrypted_memory_region(struct kvm_vm *vm,
> +                                          struct userspace_mem_region 
> *region)

This is a comically bad helper.  Any person that is at all familiar with KVM's
CoCo support, or that simply reads KVM's documentation, will expect this to
invoke KVM_MEMORY_ENCRYPT_REG_REGION.  And this is obviously doing much more 
than
"registering" an encrypted region.  Not to mention this helper doesn't need to
exist; it has _one_ caller, and the code is quite self-explanatory.

> +{
> +     vm_set_memory_attributes(vm, region->region.guest_phys_addr,
> +                              region->region.memory_size,
> +                              KVM_MEMORY_ATTRIBUTE_PRIVATE);
> +}
> +
>  /*
>   * TD creation/setup/finalization
>   */
> @@ -459,28 +474,35 @@ static void load_td_memory_region(struct kvm_vm *vm,
>       if (!sparsebit_any_set(pages))
>               return;
>  
> +     if (region->region.guest_memfd != -1)
> +             register_encrypted_memory_region(vm, region);
> +
>       sparsebit_for_each_set_range(pages, i, j) {
>               const uint64_t size_to_load = (j - i + 1) * vm->page_size;
>               const uint64_t offset =
>                       (i - lowest_page_in_region) * vm->page_size;
>               const uint64_t hva = hva_base + offset;
>               const uint64_t gpa = gpa_base + offset;
> -             void *source_addr;
> +             void *source_addr = (void *)hva;
>  
>               /*
>                * KVM_TDX_INIT_MEM_REGION ioctl cannot encrypt memory in place.
>                * Make a copy if there's only one backing memory source.
>                */
> -             source_addr = mmap(NULL, size_to_load, PROT_READ | PROT_WRITE,
> -                                MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
> -             TEST_ASSERT(source_addr,
> -                         "Could not allocate memory for loading memory 
> region");
> -
> -             memcpy(source_addr, (void *)hva, size_to_load);
> +             if (region->region.guest_memfd == -1) {

Oh, here's the "if".

> +                     source_addr = mmap(NULL, size_to_load, PROT_READ | 
> PROT_WRITE,
> +                                        MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
> +                     TEST_ASSERT(source_addr,
> +                                 "Could not allocate memory for loading 
> memory region");
> +
> +                     memcpy(source_addr, (void *)hva, size_to_load);
> +                     memset((void *)hva, 0, size_to_load);
> +             }
>  
>               tdx_init_mem_region(vm, source_addr, gpa, size_to_load);
>  
> -             munmap(source_addr, size_to_load);
> +             if (region->region.guest_memfd == -1)
> +                     munmap(source_addr, size_to_load);
>       }
>  }
>  
> -- 
> 2.51.0.rc0.155.g4a0f42376b-goog
> 

Reply via email to