1 // SPDX-License-Identifier: GPL-2.0
3 * KVM memslot modification stress test
4 * Adapted from demand_paging_test.c
6 * Copyright (C) 2018, Red Hat, Inc.
7 * Copyright (C) 2020, Google, Inc.
10 #define _GNU_SOURCE /* for program_invocation_name */
14 #include <sys/syscall.h>
16 #include <asm/unistd.h>
20 #include <linux/bitmap.h>
21 #include <linux/bitops.h>
22 #include <linux/userfaultfd.h>
24 #include "perf_test_util.h"
25 #include "processor.h"
26 #include "test_util.h"
27 #include "guest_modes.h"
29 #define DUMMY_MEMSLOT_INDEX 7
31 #define DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS 10
34 static int nr_vcpus = 1;
35 static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;
37 static bool run_vcpus = true;
39 static void *vcpu_worker(void *data)
42 struct perf_test_vcpu_args *vcpu_args =
43 (struct perf_test_vcpu_args *)data;
44 int vcpu_id = vcpu_args->vcpu_id;
45 struct kvm_vm *vm = perf_test_args.vm;
48 vcpu_args_set(vm, vcpu_id, 1, vcpu_id);
49 run = vcpu_state(vm, vcpu_id);
51 /* Let the guest access its memory until a stop signal is received */
52 while (READ_ONCE(run_vcpus)) {
53 ret = _vcpu_run(vm, vcpu_id);
54 TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret);
56 if (get_ucall(vm, vcpu_id, NULL) == UCALL_SYNC)
60 "Invalid guest sync status: exit_reason=%s\n",
61 exit_reason_str(run->exit_reason));
67 struct memslot_antagonist_args {
70 uint64_t nr_modifications;
73 static void add_remove_memslot(struct kvm_vm *vm, useconds_t delay,
74 uint64_t nr_modifications)
76 const uint64_t pages = 1;
81 * Add the dummy memslot just below the perf_test_util memslot, which is
82 * at the top of the guest physical address space.
84 gpa = guest_test_phys_mem - pages * vm_get_page_size(vm);
86 for (i = 0; i < nr_modifications; i++) {
88 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa,
89 DUMMY_MEMSLOT_INDEX, pages, 0);
91 vm_mem_region_delete(vm, DUMMY_MEMSLOT_INDEX);
96 useconds_t memslot_modification_delay;
97 uint64_t nr_memslot_modifications;
98 bool partition_vcpu_memory_access;
101 static void run_test(enum vm_guest_mode mode, void *arg)
103 struct test_params *p = arg;
104 pthread_t *vcpu_threads;
108 vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size,
109 VM_MEM_SRC_ANONYMOUS);
111 perf_test_args.wr_fract = 1;
113 vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
114 TEST_ASSERT(vcpu_threads, "Memory allocation failed");
116 perf_test_setup_vcpus(vm, nr_vcpus, guest_percpu_mem_size,
117 p->partition_vcpu_memory_access);
119 /* Export the shared variables to the guest */
120 sync_global_to_guest(vm, perf_test_args);
122 pr_info("Finished creating vCPUs\n");
124 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++)
125 pthread_create(&vcpu_threads[vcpu_id], NULL, vcpu_worker,
126 &perf_test_args.vcpu_args[vcpu_id]);
128 pr_info("Started all vCPUs\n");
130 add_remove_memslot(vm, p->memslot_modification_delay,
131 p->nr_memslot_modifications);
135 /* Wait for the vcpu threads to quit */
136 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++)
137 pthread_join(vcpu_threads[vcpu_id], NULL);
139 pr_info("All vCPU threads joined\n");
147 static void help(char *name)
150 printf("usage: %s [-h] [-m mode] [-d delay_usec]\n"
151 " [-b memory] [-v vcpus] [-o] [-i iterations]\n", name);
153 printf(" -d: add a delay between each iteration of adding and\n"
154 " deleting a memslot in usec.\n");
155 printf(" -b: specify the size of the memory region which should be\n"
156 " accessed by each vCPU. e.g. 10M or 3G.\n"
158 printf(" -v: specify the number of vCPUs to run.\n");
159 printf(" -o: Overlap guest memory accesses instead of partitioning\n"
160 " them into a separate region of memory for each vCPU.\n");
161 printf(" -i: specify the number of iterations of adding and removing\n"
163 " Default: %d\n", DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS);
168 int main(int argc, char *argv[])
170 int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
172 struct test_params p = {
173 .memslot_modification_delay = 0,
174 .nr_memslot_modifications =
175 DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS,
176 .partition_vcpu_memory_access = true
179 guest_modes_append_default();
181 while ((opt = getopt(argc, argv, "hm:d:b:v:oi:")) != -1) {
184 guest_modes_cmdline(optarg);
187 p.memslot_modification_delay = strtoul(optarg, NULL, 0);
188 TEST_ASSERT(p.memslot_modification_delay >= 0,
189 "A negative delay is not supported.");
192 guest_percpu_mem_size = parse_size(optarg);
195 nr_vcpus = atoi(optarg);
196 TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus,
197 "Invalid number of vcpus, must be between 1 and %d",
201 p.partition_vcpu_memory_access = false;
204 p.nr_memslot_modifications = atoi(optarg);
213 for_each_guest_mode(run_test, &p);