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(struct perf_test_vcpu_args *vcpu_args)
40 {
41 	struct kvm_vcpu *vcpu = vcpu_args->vcpu;
42 	struct kvm_run *run;
43 	int ret;
44 
45 	run = vcpu->run;
46 
47 	/* Let the guest access its memory until a stop signal is received */
48 	while (READ_ONCE(run_vcpus)) {
49 		ret = _vcpu_run(vcpu);
50 		TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret);
51 
52 		if (get_ucall(vcpu, NULL) == UCALL_SYNC)
53 			continue;
54 
55 		TEST_ASSERT(false,
56 			    "Invalid guest sync status: exit_reason=%s\n",
57 			    exit_reason_str(run->exit_reason));
58 	}
59 }
60 
61 struct memslot_antagonist_args {
62 	struct kvm_vm *vm;
63 	useconds_t delay;
64 	uint64_t nr_modifications;
65 };
66 
67 static void add_remove_memslot(struct kvm_vm *vm, useconds_t delay,
68 			       uint64_t nr_modifications)
69 {
70 	const uint64_t pages = 1;
71 	uint64_t gpa;
72 	int i;
73 
74 	/*
75 	 * Add the dummy memslot just below the perf_test_util memslot, which is
76 	 * at the top of the guest physical address space.
77 	 */
78 	gpa = perf_test_args.gpa - pages * vm->page_size;
79 
80 	for (i = 0; i < nr_modifications; i++) {
81 		usleep(delay);
82 		vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa,
83 					    DUMMY_MEMSLOT_INDEX, pages, 0);
84 
85 		vm_mem_region_delete(vm, DUMMY_MEMSLOT_INDEX);
86 	}
87 }
88 
89 struct test_params {
90 	useconds_t memslot_modification_delay;
91 	uint64_t nr_memslot_modifications;
92 	bool partition_vcpu_memory_access;
93 };
94 
95 static void run_test(enum vm_guest_mode mode, void *arg)
96 {
97 	struct test_params *p = arg;
98 	struct kvm_vm *vm;
99 
100 	vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
101 				 VM_MEM_SRC_ANONYMOUS,
102 				 p->partition_vcpu_memory_access);
103 
104 	pr_info("Finished creating vCPUs\n");
105 
106 	perf_test_start_vcpu_threads(nr_vcpus, vcpu_worker);
107 
108 	pr_info("Started all vCPUs\n");
109 
110 	add_remove_memslot(vm, p->memslot_modification_delay,
111 			   p->nr_memslot_modifications);
112 
113 	run_vcpus = false;
114 
115 	perf_test_join_vcpu_threads(nr_vcpus);
116 	pr_info("All vCPU threads joined\n");
117 
118 	perf_test_destroy_vm(vm);
119 }
120 
121 static void help(char *name)
122 {
123 	puts("");
124 	printf("usage: %s [-h] [-m mode] [-d delay_usec]\n"
125 	       "          [-b memory] [-v vcpus] [-o] [-i iterations]\n", name);
126 	guest_modes_help();
127 	printf(" -d: add a delay between each iteration of adding and\n"
128 	       "     deleting a memslot in usec.\n");
129 	printf(" -b: specify the size of the memory region which should be\n"
130 	       "     accessed by each vCPU. e.g. 10M or 3G.\n"
131 	       "     Default: 1G\n");
132 	printf(" -v: specify the number of vCPUs to run.\n");
133 	printf(" -o: Overlap guest memory accesses instead of partitioning\n"
134 	       "     them into a separate region of memory for each vCPU.\n");
135 	printf(" -i: specify the number of iterations of adding and removing\n"
136 	       "     a memslot.\n"
137 	       "     Default: %d\n", DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS);
138 	puts("");
139 	exit(0);
140 }
141 
142 int main(int argc, char *argv[])
143 {
144 	int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
145 	int opt;
146 	struct test_params p = {
147 		.memslot_modification_delay = 0,
148 		.nr_memslot_modifications =
149 			DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS,
150 		.partition_vcpu_memory_access = true
151 	};
152 
153 	guest_modes_append_default();
154 
155 	while ((opt = getopt(argc, argv, "hm:d:b:v:oi:")) != -1) {
156 		switch (opt) {
157 		case 'm':
158 			guest_modes_cmdline(optarg);
159 			break;
160 		case 'd':
161 			p.memslot_modification_delay = strtoul(optarg, NULL, 0);
162 			TEST_ASSERT(p.memslot_modification_delay >= 0,
163 				    "A negative delay is not supported.");
164 			break;
165 		case 'b':
166 			guest_percpu_mem_size = parse_size(optarg);
167 			break;
168 		case 'v':
169 			nr_vcpus = atoi(optarg);
170 			TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus,
171 				    "Invalid number of vcpus, must be between 1 and %d",
172 				    max_vcpus);
173 			break;
174 		case 'o':
175 			p.partition_vcpu_memory_access = false;
176 			break;
177 		case 'i':
178 			p.nr_memslot_modifications = atoi(optarg);
179 			break;
180 		case 'h':
181 		default:
182 			help(argv[0]);
183 			break;
184 		}
185 	}
186 
187 	for_each_guest_mode(run_test, &p);
188 
189 	return 0;
190 }
191