]> git.baikalelectronics.ru Git - kernel.git/commitdiff
KVM: selftests: Add VM creation helper that "returns" vCPUs
authorSean Christopherson <seanjc@google.com>
Wed, 16 Feb 2022 19:19:18 +0000 (11:19 -0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Sat, 11 Jun 2022 15:47:06 +0000 (11:47 -0400)
Add a VM creator that "returns" the created vCPUs by filling the provided
array.  This will allow converting multi-vCPU tests away from hardcoded
vCPU IDs.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
tools/testing/selftests/kvm/include/kvm_util_base.h
tools/testing/selftests/kvm/kvm_page_table_test.c
tools/testing/selftests/kvm/lib/kvm_util.c
tools/testing/selftests/kvm/lib/perf_test_util.c

index 2c7a8a91ebe28c88f2c2d791287d3fad677f23d8..c0b2158a53d559f4cd538023d6ecfee367b90b67 100644 (file)
@@ -563,10 +563,19 @@ struct kvm_vm *vm_create_default_with_vcpus(uint32_t nr_vcpus, uint64_t extra_me
                                            uint32_t vcpuids[]);
 
 /* Like vm_create_default_with_vcpus, but accepts mode and slot0 memory as a parameter */
-struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus,
-                                   uint64_t slot0_mem_pages, uint64_t extra_mem_pages,
-                                   uint32_t num_percpu_pages, void *guest_code,
-                                   uint32_t vcpuids[]);
+struct kvm_vm *__vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus,
+                                     uint64_t slot0_mem_pages, uint64_t extra_mem_pages,
+                                     uint32_t num_percpu_pages, void *guest_code,
+                                     uint32_t vcpuids[], struct kvm_vcpu *vcpus[]);
+
+static inline struct kvm_vm *vm_create_with_vcpus(uint32_t nr_vcpus,
+                                                 void *guest_code,
+                                                 struct kvm_vcpu *vcpus[])
+{
+       return __vm_create_with_vcpus(VM_MODE_DEFAULT, nr_vcpus,
+                                     DEFAULT_GUEST_PHY_PAGES, 0, 0,
+                                     guest_code, NULL, vcpus);
+}
 
 /*
  * Create a VM with a single vCPU with reasonable defaults and @extra_mem_pages
index 2c4a7563a4f8adf4416f77c92c3fcf328711e51f..e91bc7f1400d111e9d610e1283942319c4d0d3b6 100644 (file)
@@ -268,8 +268,8 @@ static struct kvm_vm *pre_init_before_test(enum vm_guest_mode mode, void *arg)
 
        /* Create a VM with enough guest pages */
        guest_num_pages = test_mem_size / guest_page_size;
-       vm = vm_create_with_vcpus(mode, nr_vcpus, DEFAULT_GUEST_PHY_PAGES,
-                                 guest_num_pages, 0, guest_code, NULL);
+       vm = __vm_create_with_vcpus(mode, nr_vcpus, DEFAULT_GUEST_PHY_PAGES,
+                                   guest_num_pages, 0, guest_code, NULL, NULL);
 
        /* Align down GPA of the testing memslot */
        if (!p->phys_offset)
index c2a99f26e9baa8fbf2dff499a49a8b2e8c2a029b..3015d490a8f6229733ed222cfd52479c715c9294 100644 (file)
@@ -296,12 +296,13 @@ struct kvm_vm *__vm_create(enum vm_guest_mode mode, uint64_t nr_pages)
  * extra_mem_pages is only used to calculate the maximum page table size,
  * no real memory allocation for non-slot0 memory in this function.
  */
-struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus,
-                                   uint64_t slot0_mem_pages, uint64_t extra_mem_pages,
-                                   uint32_t num_percpu_pages, void *guest_code,
-                                   uint32_t vcpuids[])
+struct kvm_vm *__vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus,
+                                     uint64_t slot0_mem_pages, uint64_t extra_mem_pages,
+                                     uint32_t num_percpu_pages, void *guest_code,
+                                     uint32_t vcpuids[], struct kvm_vcpu *vcpus[])
 {
        uint64_t vcpu_pages, extra_pg_pages, pages;
+       struct kvm_vcpu *vcpu;
        struct kvm_vm *vm;
        int i;
 
@@ -328,7 +329,9 @@ struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus,
        for (i = 0; i < nr_vcpus; ++i) {
                uint32_t vcpuid = vcpuids ? vcpuids[i] : i;
 
-               vm_vcpu_add(vm, vcpuid, guest_code);
+               vcpu = vm_vcpu_add(vm, vcpuid, guest_code);
+               if (vcpus)
+                       vcpus[i] = vcpu;
        }
 
        return vm;
@@ -338,8 +341,8 @@ struct kvm_vm *vm_create_default_with_vcpus(uint32_t nr_vcpus, uint64_t extra_me
                                            uint32_t num_percpu_pages, void *guest_code,
                                            uint32_t vcpuids[])
 {
-       return vm_create_with_vcpus(VM_MODE_DEFAULT, nr_vcpus, DEFAULT_GUEST_PHY_PAGES,
-                                   extra_mem_pages, num_percpu_pages, guest_code, vcpuids);
+       return __vm_create_with_vcpus(VM_MODE_DEFAULT, nr_vcpus, DEFAULT_GUEST_PHY_PAGES,
+                                     extra_mem_pages, num_percpu_pages, guest_code, vcpuids, NULL);
 }
 
 struct kvm_vm *vm_create_default(uint32_t vcpuid, uint64_t extra_mem_pages,
index f989ff91f022abf2f84f595f7fea48250bb8f0eb..d3ff05f00653d43f074ac12a4bd0b8e4c103d320 100644 (file)
@@ -147,8 +147,9 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
         * The memory is also added to memslot 0, but that's a benign side
         * effect as KVM allows aliasing HVAs in meslots.
         */
-       vm = vm_create_with_vcpus(mode, vcpus, slot0_pages, guest_num_pages, 0,
-                                 perf_test_guest_code, NULL);
+       vm = __vm_create_with_vcpus(mode, vcpus, DEFAULT_GUEST_PHY_PAGES,
+                                   slot0_pages + guest_num_pages, 0,
+                                   perf_test_guest_code, NULL, NULL);
 
        pta->vm = vm;