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