diff options
| author | Lisa Wang <wyihan@google.com> | 2026-10-01 19:37:40 +0000 |
|---|---|---|
| committer | Sean Christopherson <seanjc@google.com> | 2026-10-12 08:08:29 -0700 |
| commit | a6270ed4bec0caa25913082e71ce22254e9cd3ed (patch) | |
| tree | d6368be814e17a8b6462a529886c5bac86b48365 /tools/testing/selftests/kvm/lib | |
| parent | ad06cf5338bc5501bd989584db6dc71972bb436a (diff) | |
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>
Diffstat (limited to 'tools/testing/selftests/kvm/lib')
| -rw-r--r-- | tools/testing/selftests/kvm/lib/kvm_util.c | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 3f559f207408..0c67d964a9aa 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -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; |
