xref: /linux/tools/testing/selftests/kvm/memslot_modification_stress_test.c (revision c94cd9508b1335b949fd13ebd269313c65492df0)
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 };
83 
84 static void run_test(enum vm_guest_mode mode, void *arg)
85 {
86 	struct test_params *p = arg;
87 	struct kvm_vm *vm;
88 
89 	vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
90 				 VM_MEM_SRC_ANONYMOUS,
91 				 p->partition_vcpu_memory_access);
92 
93 	pr_info("Finished creating vCPUs\n");
94 
95 	memstress_start_vcpu_threads(nr_vcpus, vcpu_worker);
96 
97 	pr_info("Started all vCPUs\n");
98 
99 	add_remove_memslot(vm, p->delay, p->nr_iterations);
100 
101 	memstress_join_vcpu_threads(nr_vcpus);
102 	pr_info("All vCPU threads joined\n");
103 
104 	memstress_destroy_vm(vm);
105 }
106 
107 static void help(char *name)
108 {
109 	puts("");
110 	printf("usage: %s [-h] [-m mode] [-d delay_usec]\n"
111 	       "          [-b memory] [-v vcpus] [-o] [-i iterations]\n", name);
112 	guest_modes_help();
113 	printf(" -d: add a delay between each iteration of adding and\n"
114 	       "     deleting a memslot in usec.\n");
115 	printf(" -b: specify the size of the memory region which should be\n"
116 	       "     accessed by each vCPU. e.g. 10M or 3G.\n"
117 	       "     Default: 1G\n");
118 	printf(" -v: specify the number of vCPUs to run.\n");
119 	printf(" -o: Overlap guest memory accesses instead of partitioning\n"
120 	       "     them into a separate region of memory for each vCPU.\n");
121 	printf(" -i: specify the number of iterations of adding and removing\n"
122 	       "     a memslot.\n"
123 	       "     Default: %d\n", DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS);
124 	puts("");
125 	exit(0);
126 }
127 
128 int main(int argc, char *argv[])
129 {
130 	int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
131 	int opt;
132 	struct test_params p = {
133 		.delay = 0,
134 		.nr_iterations = DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS,
135 		.partition_vcpu_memory_access = true
136 	};
137 
138 	guest_modes_append_default();
139 
140 	while ((opt = getopt(argc, argv, "hm:d:b:v:oi:")) != -1) {
141 		switch (opt) {
142 		case 'm':
143 			guest_modes_cmdline(optarg);
144 			break;
145 		case 'd':
146 			p.delay = atoi_non_negative("Delay", optarg);
147 			break;
148 		case 'b':
149 			guest_percpu_mem_size = parse_size(optarg);
150 			break;
151 		case 'v':
152 			nr_vcpus = atoi_positive("Number of vCPUs", optarg);
153 			TEST_ASSERT(nr_vcpus <= max_vcpus,
154 				    "Invalid number of vcpus, must be between 1 and %d",
155 				    max_vcpus);
156 			break;
157 		case 'o':
158 			p.partition_vcpu_memory_access = false;
159 			break;
160 		case 'i':
161 			p.nr_iterations = atoi_positive("Number of iterations", optarg);
162 			break;
163 		case 'h':
164 		default:
165 			help(argv[0]);
166 			break;
167 		}
168 	}
169 
170 	for_each_guest_mode(run_test, &p);
171 
172 	return 0;
173 }
174