1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020, Google LLC.
8 #include "perf_test_util.h"
11 struct perf_test_args perf_test_args;
14 * Guest virtual memory offset of the testing memory slot.
15 * Must not conflict with identity mapped test code.
17 static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM;
20 /* The id of the vCPU. */
23 /* The pthread backing the vCPU. */
26 /* Set to true once the vCPU thread is up and running. */
30 /* The vCPU threads involved in this test. */
31 static struct vcpu_thread vcpu_threads[KVM_MAX_VCPUS];
33 /* The function run by each vCPU thread, as provided by the test. */
34 static void (*vcpu_thread_fn)(struct perf_test_vcpu_args *);
36 /* Set to true once all vCPU threads are up and running. */
37 static bool all_vcpu_threads_running;
40 * Continuously write to the first 8 bytes of each page in the
43 void perf_test_guest_code(uint32_t vcpu_id)
45 struct perf_test_args *pta = &perf_test_args;
46 struct perf_test_vcpu_args *vcpu_args = &pta->vcpu_args[vcpu_id];
51 /* Make sure vCPU args data structure is not corrupt. */
52 GUEST_ASSERT(vcpu_args->vcpu_id == vcpu_id);
55 pages = vcpu_args->pages;
58 for (i = 0; i < pages; i++) {
59 uint64_t addr = gva + (i * pta->guest_page_size);
61 if (i % pta->wr_fract == 0)
62 *(uint64_t *)addr = 0x0123456789ABCDEF;
64 READ_ONCE(*(uint64_t *)addr);
71 void perf_test_setup_vcpus(struct kvm_vm *vm, int vcpus,
72 uint64_t vcpu_memory_bytes,
73 bool partition_vcpu_memory_access)
75 struct perf_test_args *pta = &perf_test_args;
76 struct perf_test_vcpu_args *vcpu_args;
79 for (vcpu_id = 0; vcpu_id < vcpus; vcpu_id++) {
80 vcpu_args = &pta->vcpu_args[vcpu_id];
82 vcpu_args->vcpu_id = vcpu_id;
83 if (partition_vcpu_memory_access) {
84 vcpu_args->gva = guest_test_virt_mem +
85 (vcpu_id * vcpu_memory_bytes);
86 vcpu_args->pages = vcpu_memory_bytes /
88 vcpu_args->gpa = pta->gpa + (vcpu_id * vcpu_memory_bytes);
90 vcpu_args->gva = guest_test_virt_mem;
91 vcpu_args->pages = (vcpus * vcpu_memory_bytes) /
93 vcpu_args->gpa = pta->gpa;
96 vcpu_args_set(vm, vcpu_id, 1, vcpu_id);
98 pr_debug("Added VCPU %d with test mem gpa [%lx, %lx)\n",
99 vcpu_id, vcpu_args->gpa, vcpu_args->gpa +
100 (vcpu_args->pages * pta->guest_page_size));
104 struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
105 uint64_t vcpu_memory_bytes, int slots,
106 enum vm_mem_backing_src_type backing_src,
107 bool partition_vcpu_memory_access)
109 struct perf_test_args *pta = &perf_test_args;
111 uint64_t guest_num_pages, slot0_pages = DEFAULT_GUEST_PHY_PAGES;
112 uint64_t backing_src_pagesz = get_backing_src_pagesz(backing_src);
113 uint64_t region_end_gfn;
116 pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
118 /* By default vCPUs will write to memory. */
122 * Snapshot the non-huge page size. This is used by the guest code to
123 * access/dirty pages at the logging granularity.
125 pta->guest_page_size = vm_guest_mode_params[mode].page_size;
127 guest_num_pages = vm_adjust_num_guest_pages(mode,
128 (vcpus * vcpu_memory_bytes) / pta->guest_page_size);
130 TEST_ASSERT(vcpu_memory_bytes % getpagesize() == 0,
131 "Guest memory size is not host page size aligned.");
132 TEST_ASSERT(vcpu_memory_bytes % pta->guest_page_size == 0,
133 "Guest memory size is not guest page size aligned.");
134 TEST_ASSERT(guest_num_pages % slots == 0,
135 "Guest memory cannot be evenly divided into %d slots.",
139 * If using nested, allocate extra pages for the nested page tables and
140 * in-memory data structures.
143 slot0_pages += perf_test_nested_pages(vcpus);
146 * Pass guest_num_pages to populate the page tables for test memory.
147 * The memory is also added to memslot 0, but that's a benign side
148 * effect as KVM allows aliasing HVAs in meslots.
150 vm = vm_create_with_vcpus(mode, vcpus, slot0_pages, guest_num_pages, 0,
151 perf_test_guest_code, NULL);
155 /* Put the test region at the top guest physical memory. */
156 region_end_gfn = vm_get_max_gfn(vm) + 1;
160 * When running vCPUs in L2, restrict the test region to 48 bits to
161 * avoid needing 5-level page tables to identity map L2.
164 region_end_gfn = min(region_end_gfn, (1UL << 48) / pta->guest_page_size);
167 * If there should be more memory in the guest test region than there
168 * can be pages in the guest, it will definitely cause problems.
170 TEST_ASSERT(guest_num_pages < region_end_gfn,
171 "Requested more guest memory than address space allows.\n"
172 " guest pages: %" PRIx64 " max gfn: %" PRIx64
173 " vcpus: %d wss: %" PRIx64 "]\n",
174 guest_num_pages, region_end_gfn - 1, vcpus,
177 pta->gpa = (region_end_gfn - guest_num_pages) * pta->guest_page_size;
178 pta->gpa = align_down(pta->gpa, backing_src_pagesz);
180 /* Align to 1M (segment size) */
181 pta->gpa = align_down(pta->gpa, 1 << 20);
183 pta->size = guest_num_pages * pta->guest_page_size;
184 pr_info("guest physical test memory: [0x%lx, 0x%lx)\n",
185 pta->gpa, pta->gpa + pta->size);
187 /* Add extra memory slots for testing */
188 for (i = 0; i < slots; i++) {
189 uint64_t region_pages = guest_num_pages / slots;
190 vm_paddr_t region_start = pta->gpa + region_pages * pta->guest_page_size * i;
192 vm_userspace_mem_region_add(vm, backing_src, region_start,
193 PERF_TEST_MEM_SLOT_INDEX + i,
197 /* Do mapping for the demand paging memory slot */
198 virt_map(vm, guest_test_virt_mem, pta->gpa, guest_num_pages);
200 perf_test_setup_vcpus(vm, vcpus, vcpu_memory_bytes, partition_vcpu_memory_access);
203 pr_info("Configuring vCPUs to run in L2 (nested).\n");
204 perf_test_setup_nested(vm, vcpus);
207 ucall_init(vm, NULL);
209 /* Export the shared variables to the guest. */
210 sync_global_to_guest(vm, perf_test_args);
215 void perf_test_destroy_vm(struct kvm_vm *vm)
221 void perf_test_set_wr_fract(struct kvm_vm *vm, int wr_fract)
223 perf_test_args.wr_fract = wr_fract;
224 sync_global_to_guest(vm, perf_test_args);
227 uint64_t __weak perf_test_nested_pages(int nr_vcpus)
232 void __weak perf_test_setup_nested(struct kvm_vm *vm, int nr_vcpus)
234 pr_info("%s() not support on this architecture, skipping.\n", __func__);
238 static void *vcpu_thread_main(void *data)
240 struct vcpu_thread *vcpu = data;
242 WRITE_ONCE(vcpu->running, true);
245 * Wait for all vCPU threads to be up and running before calling the test-
246 * provided vCPU thread function. This prevents thread creation (which
247 * requires taking the mmap_sem in write mode) from interfering with the
248 * guest faulting in its memory.
250 while (!READ_ONCE(all_vcpu_threads_running))
253 vcpu_thread_fn(&perf_test_args.vcpu_args[vcpu->vcpu_id]);
258 void perf_test_start_vcpu_threads(int vcpus, void (*vcpu_fn)(struct perf_test_vcpu_args *))
262 vcpu_thread_fn = vcpu_fn;
263 WRITE_ONCE(all_vcpu_threads_running, false);
265 for (vcpu_id = 0; vcpu_id < vcpus; vcpu_id++) {
266 struct vcpu_thread *vcpu = &vcpu_threads[vcpu_id];
268 vcpu->vcpu_id = vcpu_id;
269 WRITE_ONCE(vcpu->running, false);
271 pthread_create(&vcpu->thread, NULL, vcpu_thread_main, vcpu);
274 for (vcpu_id = 0; vcpu_id < vcpus; vcpu_id++) {
275 while (!READ_ONCE(vcpu_threads[vcpu_id].running))
279 WRITE_ONCE(all_vcpu_threads_running, true);
282 void perf_test_join_vcpu_threads(int vcpus)
286 for (vcpu_id = 0; vcpu_id < vcpus; vcpu_id++)
287 pthread_join(vcpu_threads[vcpu_id].thread, NULL);