1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * KVM memslot modification stress test
4  * Adapted from demand_paging_test.c
5  *
6  * Copyright (C) 2018, Red Hat, Inc.
7  * Copyright (C) 2020, Google, Inc.
8  */
9 
10 #define _GNU_SOURCE /* for program_invocation_name */
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <sys/syscall.h>
15 #include <unistd.h>
16 #include <asm/unistd.h>
17 #include <time.h>
18 #include <poll.h>
19 #include <pthread.h>
20 #include <linux/bitmap.h>
21 #include <linux/bitops.h>
22 #include <linux/userfaultfd.h>
23 
24 #include "perf_test_util.h"
25 #include "processor.h"
26 #include "test_util.h"
27 #include "guest_modes.h"
28 
29 #define DUMMY_MEMSLOT_INDEX 7
30 
31 #define DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS 10
32 
33 
34 static int nr_vcpus = 1;
35 static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;
36 
37 static bool run_vcpus = true;
38 
39 static void *vcpu_worker(void *data)
40 {
41 	int ret;
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;
46 	struct kvm_run *run;
47 
48 	run = vcpu_state(vm, vcpu_id);
49 
50 	/* Let the guest access its memory until a stop signal is received */
51 	while (READ_ONCE(run_vcpus)) {
52 		ret = _vcpu_run(vm, vcpu_id);
53 		TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret);
54 
55 		if (get_ucall(vm, vcpu_id, NULL) == UCALL_SYNC)
56 			continue;
57 
58 		TEST_ASSERT(false,
59 			    "Invalid guest sync status: exit_reason=%s\n",
60 			    exit_reason_str(run->exit_reason));
61 	}
62 
63 	return NULL;
64 }
65 
66 struct memslot_antagonist_args {
67 	struct kvm_vm *vm;
68 	useconds_t delay;
69 	uint64_t nr_modifications;
70 };
71 
72 static void add_remove_memslot(struct kvm_vm *vm, useconds_t delay,
73 			       uint64_t nr_modifications)
74 {
75 	const uint64_t pages = 1;
76 	uint64_t gpa;
77 	int i;
78 
79 	/*
80 	 * Add the dummy memslot just below the perf_test_util memslot, which is
81 	 * at the top of the guest physical address space.
82 	 */
83 	gpa = guest_test_phys_mem - pages * vm_get_page_size(vm);
84 
85 	for (i = 0; i < nr_modifications; i++) {
86 		usleep(delay);
87 		vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa,
88 					    DUMMY_MEMSLOT_INDEX, pages, 0);
89 
90 		vm_mem_region_delete(vm, DUMMY_MEMSLOT_INDEX);
91 	}
92 }
93 
94 struct test_params {
95 	useconds_t memslot_modification_delay;
96 	uint64_t nr_memslot_modifications;
97 	bool partition_vcpu_memory_access;
98 };
99 
100 static void run_test(enum vm_guest_mode mode, void *arg)
101 {
102 	struct test_params *p = arg;
103 	pthread_t *vcpu_threads;
104 	struct kvm_vm *vm;
105 	int vcpu_id;
106 
107 	vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
108 				 VM_MEM_SRC_ANONYMOUS);
109 
110 	perf_test_args.wr_fract = 1;
111 
112 	vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
113 	TEST_ASSERT(vcpu_threads, "Memory allocation failed");
114 
115 	perf_test_setup_vcpus(vm, nr_vcpus, guest_percpu_mem_size,
116 			      p->partition_vcpu_memory_access);
117 
118 	/* Export the shared variables to the guest */
119 	sync_global_to_guest(vm, perf_test_args);
120 
121 	pr_info("Finished creating vCPUs\n");
122 
123 	for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++)
124 		pthread_create(&vcpu_threads[vcpu_id], NULL, vcpu_worker,
125 			       &perf_test_args.vcpu_args[vcpu_id]);
126 
127 	pr_info("Started all vCPUs\n");
128 
129 	add_remove_memslot(vm, p->memslot_modification_delay,
130 			   p->nr_memslot_modifications);
131 
132 	run_vcpus = false;
133 
134 	/* Wait for the vcpu threads to quit */
135 	for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++)
136 		pthread_join(vcpu_threads[vcpu_id], NULL);
137 
138 	pr_info("All vCPU threads joined\n");
139 
140 	ucall_uninit(vm);
141 	kvm_vm_free(vm);
142 
143 	free(vcpu_threads);
144 }
145 
146 static void help(char *name)
147 {
148 	puts("");
149 	printf("usage: %s [-h] [-m mode] [-d delay_usec]\n"
150 	       "          [-b memory] [-v vcpus] [-o] [-i iterations]\n", name);
151 	guest_modes_help();
152 	printf(" -d: add a delay between each iteration of adding and\n"
153 	       "     deleting a memslot in usec.\n");
154 	printf(" -b: specify the size of the memory region which should be\n"
155 	       "     accessed by each vCPU. e.g. 10M or 3G.\n"
156 	       "     Default: 1G\n");
157 	printf(" -v: specify the number of vCPUs to run.\n");
158 	printf(" -o: Overlap guest memory accesses instead of partitioning\n"
159 	       "     them into a separate region of memory for each vCPU.\n");
160 	printf(" -i: specify the number of iterations of adding and removing\n"
161 	       "     a memslot.\n"
162 	       "     Default: %d\n", DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS);
163 	puts("");
164 	exit(0);
165 }
166 
167 int main(int argc, char *argv[])
168 {
169 	int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
170 	int opt;
171 	struct test_params p = {
172 		.memslot_modification_delay = 0,
173 		.nr_memslot_modifications =
174 			DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS,
175 		.partition_vcpu_memory_access = true
176 	};
177 
178 	guest_modes_append_default();
179 
180 	while ((opt = getopt(argc, argv, "hm:d:b:v:oi:")) != -1) {
181 		switch (opt) {
182 		case 'm':
183 			guest_modes_cmdline(optarg);
184 			break;
185 		case 'd':
186 			p.memslot_modification_delay = strtoul(optarg, NULL, 0);
187 			TEST_ASSERT(p.memslot_modification_delay >= 0,
188 				    "A negative delay is not supported.");
189 			break;
190 		case 'b':
191 			guest_percpu_mem_size = parse_size(optarg);
192 			break;
193 		case 'v':
194 			nr_vcpus = atoi(optarg);
195 			TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus,
196 				    "Invalid number of vcpus, must be between 1 and %d",
197 				    max_vcpus);
198 			break;
199 		case 'o':
200 			p.partition_vcpu_memory_access = false;
201 			break;
202 		case 'i':
203 			p.nr_memslot_modifications = atoi(optarg);
204 			break;
205 		case 'h':
206 		default:
207 			help(argv[0]);
208 			break;
209 		}
210 	}
211 
212 	for_each_guest_mode(run_test, &p);
213 
214 	return 0;
215 }
216