Re: [PATCH v8 08/30] KVM: selftests: TDX: Update load_td_memory_region() for VM memory backed by guest memfd

From: Reinette Chatre
Date: Wed Aug 13 2025 - 10:47:18 EST


Hi Binbin,

On 8/13/25 2:23 AM, Binbin Wu wrote:
>
>
> On 8/12/2025 4:31 AM, Sean Christopherson wrote:
>> On Thu, Aug 07, 2025, Sagi Shahar wrote:
> [...]
>>> +
>>>   /*
>>>    * 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".
>
> Is it still possible for "region->region.guest_memfd == -1" case?
> KVM_TDX_INIT_MEM_REGION can only work with guest memfd, right?
>
This is still used and supports test "KVM: selftests: TDX: Test
LOG_DIRTY_PAGES flag to a non-GUEST_MEMFD memslot" found in patch #30 that
was created to support the issue encountered when QEMU attaches an emulated
VGA device to a TD. More details available in the fix:
fbb4adadea55 ("KVM: x86: Make cpu_dirty_log_size a per-VM value")

Reinette