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 "memstress.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
vcpu_worker(struct memstress_vcpu_args * vcpu_args)37 static void vcpu_worker(struct memstress_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(memstress_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
add_remove_memslot(struct kvm_vm * vm,useconds_t delay,uint64_t nr_modifications)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 memstress memslot, which is
74 * at the top of the guest physical address space.
75 */
76 gpa = memstress_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 delay;
89 uint64_t nr_iterations;
90 bool partition_vcpu_memory_access;
91 };
92
run_test(enum vm_guest_mode mode,void * arg)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 = memstress_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 memstress_start_vcpu_threads(nr_vcpus, vcpu_worker);
105
106 pr_info("Started all vCPUs\n");
107
108 add_remove_memslot(vm, p->delay, p->nr_iterations);
109
110 memstress_join_vcpu_threads(nr_vcpus);
111 pr_info("All vCPU threads joined\n");
112
113 memstress_destroy_vm(vm);
114 }
115
help(char * name)116 static void help(char *name)
117 {
118 puts("");
119 printf("usage: %s [-h] [-m mode] [-d delay_usec]\n"
120 " [-b memory] [-v vcpus] [-o] [-i iterations]\n", name);
121 guest_modes_help();
122 printf(" -d: add a delay between each iteration of adding and\n"
123 " deleting a memslot in usec.\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
main(int argc,char * argv[])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:b: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 case 'h':
173 default:
174 help(argv[0]);
175 break;
176 }
177 }
178
179 for_each_guest_mode(run_test, &p);
180
181 return 0;
182 }
183