1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2020, Google LLC.
4  */
5 #include <inttypes.h>
6 
7 #include "kvm_util.h"
8 #include "perf_test_util.h"
9 #include "processor.h"
10 
11 struct perf_test_args perf_test_args;
12 
13 /*
14  * Guest virtual memory offset of the testing memory slot.
15  * Must not conflict with identity mapped test code.
16  */
17 static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM;
18 
19 struct vcpu_thread {
20 	/* The index of the vCPU. */
21 	int vcpu_idx;
22 
23 	/* The pthread backing the vCPU. */
24 	pthread_t thread;
25 
26 	/* Set to true once the vCPU thread is up and running. */
27 	bool running;
28 };
29 
30 /* The vCPU threads involved in this test. */
31 static struct vcpu_thread vcpu_threads[KVM_MAX_VCPUS];
32 
33 /* The function run by each vCPU thread, as provided by the test. */
34 static void (*vcpu_thread_fn)(struct perf_test_vcpu_args *);
35 
36 /* Set to true once all vCPU threads are up and running. */
37 static bool all_vcpu_threads_running;
38 
39 static struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
40 
41 /*
42  * Continuously write to the first 8 bytes of each page in the
43  * specified region.
44  */
perf_test_guest_code(uint32_t vcpu_idx)45 void perf_test_guest_code(uint32_t vcpu_idx)
46 {
47 	struct perf_test_args *pta = &perf_test_args;
48 	struct perf_test_vcpu_args *vcpu_args = &pta->vcpu_args[vcpu_idx];
49 	uint64_t gva;
50 	uint64_t pages;
51 	int i;
52 
53 	gva = vcpu_args->gva;
54 	pages = vcpu_args->pages;
55 
56 	/* Make sure vCPU args data structure is not corrupt. */
57 	GUEST_ASSERT(vcpu_args->vcpu_idx == vcpu_idx);
58 
59 	while (true) {
60 		for (i = 0; i < pages; i++) {
61 			uint64_t addr = gva + (i * pta->guest_page_size);
62 
63 			if (i % pta->wr_fract == 0)
64 				*(uint64_t *)addr = 0x0123456789ABCDEF;
65 			else
66 				READ_ONCE(*(uint64_t *)addr);
67 		}
68 
69 		GUEST_SYNC(1);
70 	}
71 }
72 
perf_test_setup_vcpus(struct kvm_vm * vm,int nr_vcpus,struct kvm_vcpu * vcpus[],uint64_t vcpu_memory_bytes,bool partition_vcpu_memory_access)73 void perf_test_setup_vcpus(struct kvm_vm *vm, int nr_vcpus,
74 			   struct kvm_vcpu *vcpus[],
75 			   uint64_t vcpu_memory_bytes,
76 			   bool partition_vcpu_memory_access)
77 {
78 	struct perf_test_args *pta = &perf_test_args;
79 	struct perf_test_vcpu_args *vcpu_args;
80 	int i;
81 
82 	for (i = 0; i < nr_vcpus; i++) {
83 		vcpu_args = &pta->vcpu_args[i];
84 
85 		vcpu_args->vcpu = vcpus[i];
86 		vcpu_args->vcpu_idx = i;
87 
88 		if (partition_vcpu_memory_access) {
89 			vcpu_args->gva = guest_test_virt_mem +
90 					 (i * vcpu_memory_bytes);
91 			vcpu_args->pages = vcpu_memory_bytes /
92 					   pta->guest_page_size;
93 			vcpu_args->gpa = pta->gpa + (i * vcpu_memory_bytes);
94 		} else {
95 			vcpu_args->gva = guest_test_virt_mem;
96 			vcpu_args->pages = (nr_vcpus * vcpu_memory_bytes) /
97 					   pta->guest_page_size;
98 			vcpu_args->gpa = pta->gpa;
99 		}
100 
101 		vcpu_args_set(vcpus[i], 1, i);
102 
103 		pr_debug("Added VCPU %d with test mem gpa [%lx, %lx)\n",
104 			 i, vcpu_args->gpa, vcpu_args->gpa +
105 			 (vcpu_args->pages * pta->guest_page_size));
106 	}
107 }
108 
perf_test_create_vm(enum vm_guest_mode mode,int nr_vcpus,uint64_t vcpu_memory_bytes,int slots,enum vm_mem_backing_src_type backing_src,bool partition_vcpu_memory_access)109 struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int nr_vcpus,
110 				   uint64_t vcpu_memory_bytes, int slots,
111 				   enum vm_mem_backing_src_type backing_src,
112 				   bool partition_vcpu_memory_access)
113 {
114 	struct perf_test_args *pta = &perf_test_args;
115 	struct kvm_vm *vm;
116 	uint64_t guest_num_pages, slot0_pages = 0;
117 	uint64_t backing_src_pagesz = get_backing_src_pagesz(backing_src);
118 	uint64_t region_end_gfn;
119 	int i;
120 
121 	pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
122 
123 	/* By default vCPUs will write to memory. */
124 	pta->wr_fract = 1;
125 
126 	/*
127 	 * Snapshot the non-huge page size.  This is used by the guest code to
128 	 * access/dirty pages at the logging granularity.
129 	 */
130 	pta->guest_page_size = vm_guest_mode_params[mode].page_size;
131 
132 	guest_num_pages = vm_adjust_num_guest_pages(mode,
133 				(nr_vcpus * vcpu_memory_bytes) / pta->guest_page_size);
134 
135 	TEST_ASSERT(vcpu_memory_bytes % getpagesize() == 0,
136 		    "Guest memory size is not host page size aligned.");
137 	TEST_ASSERT(vcpu_memory_bytes % pta->guest_page_size == 0,
138 		    "Guest memory size is not guest page size aligned.");
139 	TEST_ASSERT(guest_num_pages % slots == 0,
140 		    "Guest memory cannot be evenly divided into %d slots.",
141 		    slots);
142 
143 	/*
144 	 * If using nested, allocate extra pages for the nested page tables and
145 	 * in-memory data structures.
146 	 */
147 	if (pta->nested)
148 		slot0_pages += perf_test_nested_pages(nr_vcpus);
149 
150 	/*
151 	 * Pass guest_num_pages to populate the page tables for test memory.
152 	 * The memory is also added to memslot 0, but that's a benign side
153 	 * effect as KVM allows aliasing HVAs in meslots.
154 	 */
155 	vm = __vm_create_with_vcpus(mode, nr_vcpus, slot0_pages + guest_num_pages,
156 				    perf_test_guest_code, vcpus);
157 
158 	pta->vm = vm;
159 
160 	/* Put the test region at the top guest physical memory. */
161 	region_end_gfn = vm->max_gfn + 1;
162 
163 #ifdef __x86_64__
164 	/*
165 	 * When running vCPUs in L2, restrict the test region to 48 bits to
166 	 * avoid needing 5-level page tables to identity map L2.
167 	 */
168 	if (pta->nested)
169 		region_end_gfn = min(region_end_gfn, (1UL << 48) / pta->guest_page_size);
170 #endif
171 	/*
172 	 * If there should be more memory in the guest test region than there
173 	 * can be pages in the guest, it will definitely cause problems.
174 	 */
175 	TEST_ASSERT(guest_num_pages < region_end_gfn,
176 		    "Requested more guest memory than address space allows.\n"
177 		    "    guest pages: %" PRIx64 " max gfn: %" PRIx64
178 		    " nr_vcpus: %d wss: %" PRIx64 "]\n",
179 		    guest_num_pages, region_end_gfn - 1, nr_vcpus, vcpu_memory_bytes);
180 
181 	pta->gpa = (region_end_gfn - guest_num_pages - 1) * pta->guest_page_size;
182 	pta->gpa = align_down(pta->gpa, backing_src_pagesz);
183 #ifdef __s390x__
184 	/* Align to 1M (segment size) */
185 	pta->gpa = align_down(pta->gpa, 1 << 20);
186 #endif
187 	pta->size = guest_num_pages * pta->guest_page_size;
188 	pr_info("guest physical test memory: [0x%lx, 0x%lx)\n",
189 		pta->gpa, pta->gpa + pta->size);
190 
191 	/* Add extra memory slots for testing */
192 	for (i = 0; i < slots; i++) {
193 		uint64_t region_pages = guest_num_pages / slots;
194 		vm_paddr_t region_start = pta->gpa + region_pages * pta->guest_page_size * i;
195 
196 		vm_userspace_mem_region_add(vm, backing_src, region_start,
197 					    PERF_TEST_MEM_SLOT_INDEX + i,
198 					    region_pages, 0);
199 	}
200 
201 	/* Do mapping for the demand paging memory slot */
202 	virt_map(vm, guest_test_virt_mem, pta->gpa, guest_num_pages);
203 
204 	perf_test_setup_vcpus(vm, nr_vcpus, vcpus, vcpu_memory_bytes,
205 			      partition_vcpu_memory_access);
206 
207 	if (pta->nested) {
208 		pr_info("Configuring vCPUs to run in L2 (nested).\n");
209 		perf_test_setup_nested(vm, nr_vcpus, vcpus);
210 	}
211 
212 	ucall_init(vm, NULL);
213 
214 	/* Export the shared variables to the guest. */
215 	sync_global_to_guest(vm, perf_test_args);
216 
217 	return vm;
218 }
219 
perf_test_destroy_vm(struct kvm_vm * vm)220 void perf_test_destroy_vm(struct kvm_vm *vm)
221 {
222 	ucall_uninit(vm);
223 	kvm_vm_free(vm);
224 }
225 
perf_test_set_wr_fract(struct kvm_vm * vm,int wr_fract)226 void perf_test_set_wr_fract(struct kvm_vm *vm, int wr_fract)
227 {
228 	perf_test_args.wr_fract = wr_fract;
229 	sync_global_to_guest(vm, perf_test_args);
230 }
231 
perf_test_nested_pages(int nr_vcpus)232 uint64_t __weak perf_test_nested_pages(int nr_vcpus)
233 {
234 	return 0;
235 }
236 
perf_test_setup_nested(struct kvm_vm * vm,int nr_vcpus,struct kvm_vcpu ** vcpus)237 void __weak perf_test_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu **vcpus)
238 {
239 	pr_info("%s() not support on this architecture, skipping.\n", __func__);
240 	exit(KSFT_SKIP);
241 }
242 
vcpu_thread_main(void * data)243 static void *vcpu_thread_main(void *data)
244 {
245 	struct vcpu_thread *vcpu = data;
246 
247 	WRITE_ONCE(vcpu->running, true);
248 
249 	/*
250 	 * Wait for all vCPU threads to be up and running before calling the test-
251 	 * provided vCPU thread function. This prevents thread creation (which
252 	 * requires taking the mmap_sem in write mode) from interfering with the
253 	 * guest faulting in its memory.
254 	 */
255 	while (!READ_ONCE(all_vcpu_threads_running))
256 		;
257 
258 	vcpu_thread_fn(&perf_test_args.vcpu_args[vcpu->vcpu_idx]);
259 
260 	return NULL;
261 }
262 
perf_test_start_vcpu_threads(int nr_vcpus,void (* vcpu_fn)(struct perf_test_vcpu_args *))263 void perf_test_start_vcpu_threads(int nr_vcpus,
264 				  void (*vcpu_fn)(struct perf_test_vcpu_args *))
265 {
266 	int i;
267 
268 	vcpu_thread_fn = vcpu_fn;
269 	WRITE_ONCE(all_vcpu_threads_running, false);
270 
271 	for (i = 0; i < nr_vcpus; i++) {
272 		struct vcpu_thread *vcpu = &vcpu_threads[i];
273 
274 		vcpu->vcpu_idx = i;
275 		WRITE_ONCE(vcpu->running, false);
276 
277 		pthread_create(&vcpu->thread, NULL, vcpu_thread_main, vcpu);
278 	}
279 
280 	for (i = 0; i < nr_vcpus; i++) {
281 		while (!READ_ONCE(vcpu_threads[i].running))
282 			;
283 	}
284 
285 	WRITE_ONCE(all_vcpu_threads_running, true);
286 }
287 
perf_test_join_vcpu_threads(int nr_vcpus)288 void perf_test_join_vcpu_threads(int nr_vcpus)
289 {
290 	int i;
291 
292 	for (i = 0; i < nr_vcpus; i++)
293 		pthread_join(vcpu_threads[i].thread, NULL);
294 }
295