KVM: selftests: Support guest_memfd in-place conversion

With guest_memfd in-place conversion, the memory region can be set as
shared, eliminating the need for dual-backed memory.

By setting the region to shared initially, the KVM selftest framework
can populate the mmap()-ed guest_memfd memory just like any standard
memory provider.

For CoCo VMs, pages that need to be private are explicitly set to
private before executing the VM.

Signed-off-by: Lisa Wang <wyihan@google.com>
Link: https://patch.msgid.link/20261001-tdx-selftests-v15-13-7c62a5d8a992@google.com
[sean: anchor comment to its relevant place, drop unnecessary code changes]
Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
Lisa Wang 2026-10-01 19:37:40 +00:00 committed by Sean Christopherson
parent ad06cf5338
commit a6270ed4be

View File

@ -494,6 +494,7 @@ struct kvm_vm *__vm_create(struct vm_shape shape, u32 nr_runnable_vcpus,
u64 nr_pages = vm_nr_pages_required(shape.mode, nr_runnable_vcpus,
nr_extra_pages);
struct userspace_mem_region *slot0;
u64 gmem_flags = 0;
struct kvm_vm *vm;
int i, flags;
@ -509,10 +510,20 @@ struct kvm_vm *__vm_create(struct vm_shape shape, u32 nr_runnable_vcpus,
* for CoCo VMs that require GUEST_MEMFD backed private memory.
*/
flags = 0;
if (is_guest_memfd_required(shape))
if (is_guest_memfd_required(shape)) {
flags |= KVM_MEM_GUEST_MEMFD;
vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, flags);
/*
* Allow mmap() and initialize the gmem instance as SHARED if
* in-place conversions are supported, so that the memory can
* be populated/initialized like any other memory region.
*/
if (kvm_has_gmem_attributes)
gmem_flags |= GUEST_MEMFD_FLAG_INIT_SHARED |
GUEST_MEMFD_FLAG_MMAP;
}
vm_mem_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, flags, -1, 0, gmem_flags);
for (i = 0; i < NR_MEM_REGIONS; i++)
vm->memslots[i] = 0;