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 	vcpu_args_set(vm, vcpu_id, 1, vcpu_id);
49 	run = vcpu_state(vm, vcpu_id);
50 
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);
55 
56 		if (get_ucall(vm, vcpu_id, NULL) == UCALL_SYNC)
57 			continue;
58 
59 		TEST_ASSERT(false,
60 			    "Invalid guest sync status: exit_reason=%s\n",
61 			    exit_reason_str(run->exit_reason));
62 	}
63 
64 	return NULL;
65 }
66 
67 struct memslot_antagonist_args {
68 	struct kvm_vm *vm;
69 	useconds_t delay;
70 	uint64_t nr_modifications;
71 };
72 
73 static void add_remove_memslot(struct kvm_vm *vm, useconds_t delay,
74 			      uint64_t nr_modifications, uint64_t gpa)
75 {
76 	int i;
77 
78 	for (i = 0; i < nr_modifications; i++) {
79 		usleep(delay);
80 		vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa,
81 					    DUMMY_MEMSLOT_INDEX, 1, 0);
82 
83 		vm_mem_region_delete(vm, DUMMY_MEMSLOT_INDEX);
84 	}
85 }
86 
87 struct test_params {
88 	useconds_t memslot_modification_delay;
89 	uint64_t nr_memslot_modifications;
90 	bool partition_vcpu_memory_access;
91 };
92 
93 static void run_test(enum vm_guest_mode mode, void *arg)
94 {
95 	struct test_params *p = arg;
96 	pthread_t *vcpu_threads;
97 	struct kvm_vm *vm;
98 	int vcpu_id;
99 
100 	vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size,
101 				 VM_MEM_SRC_ANONYMOUS);
102 
103 	perf_test_args.wr_fract = 1;
104 
105 	vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
106 	TEST_ASSERT(vcpu_threads, "Memory allocation failed");
107 
108 	perf_test_setup_vcpus(vm, nr_vcpus, guest_percpu_mem_size,
109 			      p->partition_vcpu_memory_access);
110 
111 	/* Export the shared variables to the guest */
112 	sync_global_to_guest(vm, perf_test_args);
113 
114 	pr_info("Finished creating vCPUs\n");
115 
116 	for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++)
117 		pthread_create(&vcpu_threads[vcpu_id], NULL, vcpu_worker,
118 			       &perf_test_args.vcpu_args[vcpu_id]);
119 
120 	pr_info("Started all vCPUs\n");
121 
122 	add_remove_memslot(vm, p->memslot_modification_delay,
123 			   p->nr_memslot_modifications,
124 			   guest_test_phys_mem +
125 			   (guest_percpu_mem_size * nr_vcpus) +
126 			   perf_test_args.host_page_size +
127 			   perf_test_args.guest_page_size);
128 
129 	run_vcpus = false;
130 
131 	/* Wait for the vcpu threads to quit */
132 	for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++)
133 		pthread_join(vcpu_threads[vcpu_id], NULL);
134 
135 	pr_info("All vCPU threads joined\n");
136 
137 	ucall_uninit(vm);
138 	kvm_vm_free(vm);
139 
140 	free(vcpu_threads);
141 }
142 
143 static void help(char *name)
144 {
145 	puts("");
146 	printf("usage: %s [-h] [-m mode] [-d delay_usec]\n"
147 	       "          [-b memory] [-v vcpus] [-o] [-i iterations]\n", name);
148 	guest_modes_help();
149 	printf(" -d: add a delay between each iteration of adding and\n"
150 	       "     deleting a memslot in usec.\n");
151 	printf(" -b: specify the size of the memory region which should be\n"
152 	       "     accessed by each vCPU. e.g. 10M or 3G.\n"
153 	       "     Default: 1G\n");
154 	printf(" -v: specify the number of vCPUs to run.\n");
155 	printf(" -o: Overlap guest memory accesses instead of partitioning\n"
156 	       "     them into a separate region of memory for each vCPU.\n");
157 	printf(" -i: specify the number of iterations of adding and removing\n"
158 	       "     a memslot.\n"
159 	       "     Default: %d\n", DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS);
160 	puts("");
161 	exit(0);
162 }
163 
164 int main(int argc, char *argv[])
165 {
166 	int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
167 	int opt;
168 	struct test_params p = {
169 		.memslot_modification_delay = 0,
170 		.nr_memslot_modifications =
171 			DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS,
172 		.partition_vcpu_memory_access = true
173 	};
174 
175 	guest_modes_append_default();
176 
177 	while ((opt = getopt(argc, argv, "hm:d:b:v:oi:")) != -1) {
178 		switch (opt) {
179 		case 'm':
180 			guest_modes_cmdline(optarg);
181 			break;
182 		case 'd':
183 			p.memslot_modification_delay = strtoul(optarg, NULL, 0);
184 			TEST_ASSERT(p.memslot_modification_delay >= 0,
185 				    "A negative delay is not supported.");
186 			break;
187 		case 'b':
188 			guest_percpu_mem_size = parse_size(optarg);
189 			break;
190 		case 'v':
191 			nr_vcpus = atoi(optarg);
192 			TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus,
193 				    "Invalid number of vcpus, must be between 1 and %d",
194 				    max_vcpus);
195 			break;
196 		case 'o':
197 			p.partition_vcpu_memory_access = false;
198 			break;
199 		case 'i':
200 			p.nr_memslot_modifications = atoi(optarg);
201 			break;
202 		case 'h':
203 		default:
204 			help(argv[0]);
205 			break;
206 		}
207 	}
208 
209 	for_each_guest_mode(run_test, &p);
210 
211 	return 0;
212 }
213