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