xref: /linux/tools/testing/selftests/kvm/memslot_modification_stress_test.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
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 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/syscall.h>
12 #include <unistd.h>
13 #include <asm/unistd.h>
14 #include <time.h>
15 #include <poll.h>
16 #include <pthread.h>
17 #include <linux/bitmap.h>
18 #include <linux/bitops.h>
19 #include <linux/userfaultfd.h>
20 
21 #include "memstress.h"
22 #include "processor.h"
23 #include "test_util.h"
24 #include "guest_modes.h"
25 
26 #define DUMMY_MEMSLOT_INDEX 7
27 
28 #define DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS 10
29 
30 
31 static int nr_vcpus = 1;
32 static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;
33 
34 static void vcpu_worker(struct memstress_vcpu_args *vcpu_args)
35 {
36 	struct kvm_vcpu *vcpu = vcpu_args->vcpu;
37 	struct kvm_run *run;
38 	int ret;
39 
40 	run = vcpu->run;
41 
42 	/* Let the guest access its memory until a stop signal is received */
43 	while (!READ_ONCE(memstress_args.stop_vcpus)) {
44 		ret = _vcpu_run(vcpu);
45 		TEST_ASSERT(ret == 0, "vcpu_run failed: %d", ret);
46 
47 		if (get_ucall(vcpu, NULL) == UCALL_SYNC)
48 			continue;
49 
50 		TEST_ASSERT(false,
51 			    "Invalid guest sync status: exit_reason=%s\n",
52 			    exit_reason_str(run->exit_reason));
53 	}
54 }
55 
56 static void add_remove_memslot(struct kvm_vm *vm, useconds_t delay,
57 			       uint64_t nr_modifications)
58 {
59 	uint64_t pages = max_t(int, vm->page_size, getpagesize()) / vm->page_size;
60 	uint64_t gpa;
61 	int i;
62 
63 	/*
64 	 * Add the dummy memslot just below the memstress memslot, which is
65 	 * at the top of the guest physical address space.
66 	 */
67 	gpa = memstress_args.gpa - pages * vm->page_size;
68 
69 	for (i = 0; i < nr_modifications; i++) {
70 		usleep(delay);
71 		vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa,
72 					    DUMMY_MEMSLOT_INDEX, pages, 0);
73 
74 		vm_mem_region_delete(vm, DUMMY_MEMSLOT_INDEX);
75 	}
76 }
77 
78 struct test_params {
79 	useconds_t delay;
80 	uint64_t nr_iterations;
81 	bool partition_vcpu_memory_access;
82 	bool disable_slot_zap_quirk;
83 };
84 
85 static void run_test(enum vm_guest_mode mode, void *arg)
86 {
87 	struct test_params *p = arg;
88 	struct kvm_vm *vm;
89 
90 	vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
91 				 VM_MEM_SRC_ANONYMOUS,
92 				 p->partition_vcpu_memory_access);
93 #ifdef __x86_64__
94 	if (p->disable_slot_zap_quirk)
95 		vm_enable_cap(vm, KVM_CAP_DISABLE_QUIRKS2, KVM_X86_QUIRK_SLOT_ZAP_ALL);
96 
97 	pr_info("Memslot zap quirk %s\n", p->disable_slot_zap_quirk ?
98 		"disabled" : "enabled");
99 #endif
100 
101 	pr_info("Finished creating vCPUs\n");
102 
103 	memstress_start_vcpu_threads(nr_vcpus, vcpu_worker);
104 
105 	pr_info("Started all vCPUs\n");
106 
107 	add_remove_memslot(vm, p->delay, p->nr_iterations);
108 
109 	memstress_join_vcpu_threads(nr_vcpus);
110 	pr_info("All vCPU threads joined\n");
111 
112 	memstress_destroy_vm(vm);
113 }
114 
115 static void help(char *name)
116 {
117 	puts("");
118 	printf("usage: %s [-h] [-m mode] [-d delay_usec] [-q]\n"
119 	       "          [-b memory] [-v vcpus] [-o] [-i iterations]\n", name);
120 	guest_modes_help();
121 	printf(" -d: add a delay between each iteration of adding and\n"
122 	       "     deleting a memslot in usec.\n");
123 	printf(" -q: Disable memslot zap quirk.\n");
124 	printf(" -b: specify the size of the memory region which should be\n"
125 	       "     accessed by each vCPU. e.g. 10M or 3G.\n"
126 	       "     Default: 1G\n");
127 	printf(" -v: specify the number of vCPUs to run.\n");
128 	printf(" -o: Overlap guest memory accesses instead of partitioning\n"
129 	       "     them into a separate region of memory for each vCPU.\n");
130 	printf(" -i: specify the number of iterations of adding and removing\n"
131 	       "     a memslot.\n"
132 	       "     Default: %d\n", DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS);
133 	puts("");
134 	exit(0);
135 }
136 
137 int main(int argc, char *argv[])
138 {
139 	int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
140 	int opt;
141 	struct test_params p = {
142 		.delay = 0,
143 		.nr_iterations = DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS,
144 		.partition_vcpu_memory_access = true
145 	};
146 
147 	guest_modes_append_default();
148 
149 	while ((opt = getopt(argc, argv, "hm:d:qb:v:oi:")) != -1) {
150 		switch (opt) {
151 		case 'm':
152 			guest_modes_cmdline(optarg);
153 			break;
154 		case 'd':
155 			p.delay = atoi_non_negative("Delay", optarg);
156 			break;
157 		case 'b':
158 			guest_percpu_mem_size = parse_size(optarg);
159 			break;
160 		case 'v':
161 			nr_vcpus = atoi_positive("Number of vCPUs", optarg);
162 			TEST_ASSERT(nr_vcpus <= max_vcpus,
163 				    "Invalid number of vcpus, must be between 1 and %d",
164 				    max_vcpus);
165 			break;
166 		case 'o':
167 			p.partition_vcpu_memory_access = false;
168 			break;
169 		case 'i':
170 			p.nr_iterations = atoi_positive("Number of iterations", optarg);
171 			break;
172 #ifdef __x86_64__
173 		case 'q':
174 			p.disable_slot_zap_quirk = true;
175 
176 			TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) &
177 				     KVM_X86_QUIRK_SLOT_ZAP_ALL);
178 			break;
179 #endif
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