• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * tools/testing/selftests/kvm/include/perf_test_util.h
4  *
5  * Copyright (C) 2020, Google LLC.
6  */
7 
8 #ifndef SELFTEST_KVM_PERF_TEST_UTIL_H
9 #define SELFTEST_KVM_PERF_TEST_UTIL_H
10 
11 #include "kvm_util.h"
12 #include "processor.h"
13 
14 #define MAX_VCPUS 512
15 
16 #define PAGE_SHIFT_4K  12
17 #define PTES_PER_4K_PT 512
18 
19 #define TEST_MEM_SLOT_INDEX		1
20 
21 /* Default guest test virtual memory offset */
22 #define DEFAULT_GUEST_TEST_MEM		0xc0000000
23 
24 #define DEFAULT_PER_VCPU_MEM_SIZE	(1 << 30) /* 1G */
25 
26 /*
27  * Guest physical memory offset of the testing memory slot.
28  * This will be set to the topmost valid physical address minus
29  * the test memory size.
30  */
31 static uint64_t guest_test_phys_mem;
32 
33 /*
34  * Guest virtual memory offset of the testing memory slot.
35  * Must not conflict with identity mapped test code.
36  */
37 static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM;
38 static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;
39 
40 /* Number of VCPUs for the test */
41 static int nr_vcpus = 1;
42 
43 struct vcpu_args {
44 	uint64_t gva;
45 	uint64_t pages;
46 
47 	/* Only used by the host userspace part of the vCPU thread */
48 	int vcpu_id;
49 };
50 
51 struct perf_test_args {
52 	struct kvm_vm *vm;
53 	uint64_t host_page_size;
54 	uint64_t guest_page_size;
55 	int wr_fract;
56 
57 	struct vcpu_args vcpu_args[MAX_VCPUS];
58 };
59 
60 static struct perf_test_args perf_test_args;
61 
62 /*
63  * Continuously write to the first 8 bytes of each page in the
64  * specified region.
65  */
guest_code(uint32_t vcpu_id)66 static void guest_code(uint32_t vcpu_id)
67 {
68 	struct vcpu_args *vcpu_args = &perf_test_args.vcpu_args[vcpu_id];
69 	uint64_t gva;
70 	uint64_t pages;
71 	int i;
72 
73 	/* Make sure vCPU args data structure is not corrupt. */
74 	GUEST_ASSERT(vcpu_args->vcpu_id == vcpu_id);
75 
76 	gva = vcpu_args->gva;
77 	pages = vcpu_args->pages;
78 
79 	while (true) {
80 		for (i = 0; i < pages; i++) {
81 			uint64_t addr = gva + (i * perf_test_args.guest_page_size);
82 
83 			if (i % perf_test_args.wr_fract == 0)
84 				*(uint64_t *)addr = 0x0123456789ABCDEF;
85 			else
86 				READ_ONCE(*(uint64_t *)addr);
87 		}
88 
89 		GUEST_SYNC(1);
90 	}
91 }
92 
create_vm(enum vm_guest_mode mode,int vcpus,uint64_t vcpu_memory_bytes)93 static struct kvm_vm *create_vm(enum vm_guest_mode mode, int vcpus,
94 				uint64_t vcpu_memory_bytes)
95 {
96 	struct kvm_vm *vm;
97 	uint64_t pages = DEFAULT_GUEST_PHY_PAGES;
98 	uint64_t guest_num_pages;
99 
100 	/* Account for a few pages per-vCPU for stacks */
101 	pages += DEFAULT_STACK_PGS * vcpus;
102 
103 	/*
104 	 * Reserve twice the ammount of memory needed to map the test region and
105 	 * the page table / stacks region, at 4k, for page tables. Do the
106 	 * calculation with 4K page size: the smallest of all archs. (e.g., 64K
107 	 * page size guest will need even less memory for page tables).
108 	 */
109 	pages += (2 * pages) / PTES_PER_4K_PT;
110 	pages += ((2 * vcpus * vcpu_memory_bytes) >> PAGE_SHIFT_4K) /
111 		 PTES_PER_4K_PT;
112 	pages = vm_adjust_num_guest_pages(mode, pages);
113 
114 	pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
115 
116 	vm = vm_create(mode, pages, O_RDWR);
117 	kvm_vm_elf_load(vm, program_invocation_name, 0, 0);
118 #ifdef __x86_64__
119 	vm_create_irqchip(vm);
120 #endif
121 
122 	perf_test_args.vm = vm;
123 	perf_test_args.guest_page_size = vm_get_page_size(vm);
124 	perf_test_args.host_page_size = getpagesize();
125 
126 	TEST_ASSERT(vcpu_memory_bytes % perf_test_args.guest_page_size == 0,
127 		    "Guest memory size is not guest page size aligned.");
128 
129 	guest_num_pages = (vcpus * vcpu_memory_bytes) /
130 			  perf_test_args.guest_page_size;
131 	guest_num_pages = vm_adjust_num_guest_pages(mode, guest_num_pages);
132 
133 	/*
134 	 * If there should be more memory in the guest test region than there
135 	 * can be pages in the guest, it will definitely cause problems.
136 	 */
137 	TEST_ASSERT(guest_num_pages < vm_get_max_gfn(vm),
138 		    "Requested more guest memory than address space allows.\n"
139 		    "    guest pages: %lx max gfn: %x vcpus: %d wss: %lx]\n",
140 		    guest_num_pages, vm_get_max_gfn(vm), vcpus,
141 		    vcpu_memory_bytes);
142 
143 	TEST_ASSERT(vcpu_memory_bytes % perf_test_args.host_page_size == 0,
144 		    "Guest memory size is not host page size aligned.");
145 
146 	guest_test_phys_mem = (vm_get_max_gfn(vm) - guest_num_pages) *
147 			      perf_test_args.guest_page_size;
148 	guest_test_phys_mem &= ~(perf_test_args.host_page_size - 1);
149 
150 #ifdef __s390x__
151 	/* Align to 1M (segment size) */
152 	guest_test_phys_mem &= ~((1 << 20) - 1);
153 #endif
154 
155 	pr_info("guest physical test memory offset: 0x%lx\n", guest_test_phys_mem);
156 
157 	/* Add an extra memory slot for testing */
158 	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
159 				    guest_test_phys_mem,
160 				    TEST_MEM_SLOT_INDEX,
161 				    guest_num_pages, 0);
162 
163 	/* Do mapping for the demand paging memory slot */
164 	virt_map(vm, guest_test_virt_mem, guest_test_phys_mem, guest_num_pages, 0);
165 
166 	ucall_init(vm, NULL);
167 
168 	return vm;
169 }
170 
add_vcpus(struct kvm_vm * vm,int vcpus,uint64_t vcpu_memory_bytes)171 static void add_vcpus(struct kvm_vm *vm, int vcpus, uint64_t vcpu_memory_bytes)
172 {
173 	vm_paddr_t vcpu_gpa;
174 	struct vcpu_args *vcpu_args;
175 	int vcpu_id;
176 
177 	for (vcpu_id = 0; vcpu_id < vcpus; vcpu_id++) {
178 		vcpu_args = &perf_test_args.vcpu_args[vcpu_id];
179 
180 		vm_vcpu_add_default(vm, vcpu_id, guest_code);
181 
182 #ifdef __x86_64__
183 		vcpu_set_cpuid(vm, vcpu_id, kvm_get_supported_cpuid());
184 #endif
185 
186 		vcpu_args->vcpu_id = vcpu_id;
187 		vcpu_args->gva = guest_test_virt_mem +
188 				 (vcpu_id * vcpu_memory_bytes);
189 		vcpu_args->pages = vcpu_memory_bytes /
190 				   perf_test_args.guest_page_size;
191 
192 		vcpu_gpa = guest_test_phys_mem + (vcpu_id * vcpu_memory_bytes);
193 		pr_debug("Added VCPU %d with test mem gpa [%lx, %lx)\n",
194 			 vcpu_id, vcpu_gpa, vcpu_gpa + vcpu_memory_bytes);
195 	}
196 }
197 
198 #endif /* SELFTEST_KVM_PERF_TEST_UTIL_H */
199