Skip to content

Commit b73a30c

Browse files
pratiksampatsean-jc
authored andcommitted
KVM: selftests: Force GUEST_MEMFD flag for SNP VM type
Force the SEV-SNP VM type to set the KVM_MEM_GUEST_MEMFD flag for the creation of private memslots. Signed-off-by: Pratik R. Sampat <[email protected]> Link: https://lore.kernel.org/r/[email protected] [sean: add a comment, don't break non-x86] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 3bf3e0a commit b73a30c

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

tools/testing/selftests/kvm/lib/kvm_util.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,23 @@ void kvm_set_files_rlimit(uint32_t nr_vcpus)
444444

445445
}
446446

447+
static bool is_guest_memfd_required(struct vm_shape shape)
448+
{
449+
#ifdef __x86_64__
450+
return shape.type == KVM_X86_SNP_VM;
451+
#else
452+
return false;
453+
#endif
454+
}
455+
447456
struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,
448457
uint64_t nr_extra_pages)
449458
{
450459
uint64_t nr_pages = vm_nr_pages_required(shape.mode, nr_runnable_vcpus,
451460
nr_extra_pages);
452461
struct userspace_mem_region *slot0;
453462
struct kvm_vm *vm;
454-
int i;
463+
int i, flags;
455464

456465
kvm_set_files_rlimit(nr_runnable_vcpus);
457466

@@ -460,7 +469,15 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,
460469

461470
vm = ____vm_create(shape);
462471

463-
vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, 0);
472+
/*
473+
* Force GUEST_MEMFD for the primary memory region if necessary, e.g.
474+
* for CoCo VMs that require GUEST_MEMFD backed private memory.
475+
*/
476+
flags = 0;
477+
if (is_guest_memfd_required(shape))
478+
flags |= KVM_MEM_GUEST_MEMFD;
479+
480+
vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, flags);
464481
for (i = 0; i < NR_MEM_REGIONS; i++)
465482
vm->memslots[i] = 0;
466483

0 commit comments

Comments
 (0)