1c33e05d9SDavid Matlack // SPDX-License-Identifier: GPL-2.0
2c33e05d9SDavid Matlack /*
3c33e05d9SDavid Matlack  * access_tracking_perf_test
4c33e05d9SDavid Matlack  *
5c33e05d9SDavid Matlack  * Copyright (C) 2021, Google, Inc.
6c33e05d9SDavid Matlack  *
7c33e05d9SDavid Matlack  * This test measures the performance effects of KVM's access tracking.
8c33e05d9SDavid Matlack  * Access tracking is driven by the MMU notifiers test_young, clear_young, and
9c33e05d9SDavid Matlack  * clear_flush_young. These notifiers do not have a direct userspace API,
10c33e05d9SDavid Matlack  * however the clear_young notifier can be triggered by marking a pages as idle
11c33e05d9SDavid Matlack  * in /sys/kernel/mm/page_idle/bitmap. This test leverages that mechanism to
12c33e05d9SDavid Matlack  * enable access tracking on guest memory.
13c33e05d9SDavid Matlack  *
14c33e05d9SDavid Matlack  * To measure performance this test runs a VM with a configurable number of
15c33e05d9SDavid Matlack  * vCPUs that each touch every page in disjoint regions of memory. Performance
16c33e05d9SDavid Matlack  * is measured in the time it takes all vCPUs to finish touching their
17c33e05d9SDavid Matlack  * predefined region.
18c33e05d9SDavid Matlack  *
19c33e05d9SDavid Matlack  * Note that a deterministic correctness test of access tracking is not possible
20c33e05d9SDavid Matlack  * by using page_idle as it exists today. This is for a few reasons:
21c33e05d9SDavid Matlack  *
22c33e05d9SDavid Matlack  * 1. page_idle only issues clear_young notifiers, which lack a TLB flush. This
23c33e05d9SDavid Matlack  *    means subsequent guest accesses are not guaranteed to see page table
24c33e05d9SDavid Matlack  *    updates made by KVM until some time in the future.
25c33e05d9SDavid Matlack  *
26c33e05d9SDavid Matlack  * 2. page_idle only operates on LRU pages. Newly allocated pages are not
27c33e05d9SDavid Matlack  *    immediately allocated to LRU lists. Instead they are held in a "pagevec",
28c33e05d9SDavid Matlack  *    which is drained to LRU lists some time in the future. There is no
29c33e05d9SDavid Matlack  *    userspace API to force this drain to occur.
30c33e05d9SDavid Matlack  *
31c33e05d9SDavid Matlack  * These limitations are worked around in this test by using a large enough
32c33e05d9SDavid Matlack  * region of memory for each vCPU such that the number of translations cached in
33c33e05d9SDavid Matlack  * the TLB and the number of pages held in pagevecs are a small fraction of the
346336a810SEmanuele Giuseppe Esposito  * overall workload. And if either of those conditions are not true (for example
356336a810SEmanuele Giuseppe Esposito  * in nesting, where TLB size is unlimited) this test will print a warning
366336a810SEmanuele Giuseppe Esposito  * rather than silently passing.
37c33e05d9SDavid Matlack  */
38c33e05d9SDavid Matlack #include <inttypes.h>
39c33e05d9SDavid Matlack #include <limits.h>
40c33e05d9SDavid Matlack #include <pthread.h>
41c33e05d9SDavid Matlack #include <sys/mman.h>
42c33e05d9SDavid Matlack #include <sys/types.h>
43c33e05d9SDavid Matlack #include <sys/stat.h>
44c33e05d9SDavid Matlack 
45c33e05d9SDavid Matlack #include "kvm_util.h"
46c33e05d9SDavid Matlack #include "test_util.h"
479fda6753SDavid Matlack #include "memstress.h"
48c33e05d9SDavid Matlack #include "guest_modes.h"
49*8fcee042SSean Christopherson #include "processor.h"
50c33e05d9SDavid Matlack 
51c33e05d9SDavid Matlack /* Global variable used to synchronize all of the vCPU threads. */
5236c5ad73SDavid Matlack static int iteration;
53c33e05d9SDavid Matlack 
54c33e05d9SDavid Matlack /* Defines what vCPU threads should do during a given iteration. */
55c33e05d9SDavid Matlack static enum {
56c33e05d9SDavid Matlack 	/* Run the vCPU to access all its memory. */
57c33e05d9SDavid Matlack 	ITERATION_ACCESS_MEMORY,
58c33e05d9SDavid Matlack 	/* Mark the vCPU's memory idle in page_idle. */
59c33e05d9SDavid Matlack 	ITERATION_MARK_IDLE,
60c33e05d9SDavid Matlack } iteration_work;
61c33e05d9SDavid Matlack 
62c33e05d9SDavid Matlack /* The iteration that was last completed by each vCPU. */
63c33e05d9SDavid Matlack static int vcpu_last_completed_iteration[KVM_MAX_VCPUS];
64c33e05d9SDavid Matlack 
65c33e05d9SDavid Matlack /* Whether to overlap the regions of memory vCPUs access. */
66c33e05d9SDavid Matlack static bool overlap_memory_access;
67c33e05d9SDavid Matlack 
68c33e05d9SDavid Matlack struct test_params {
69c33e05d9SDavid Matlack 	/* The backing source for the region of memory. */
70c33e05d9SDavid Matlack 	enum vm_mem_backing_src_type backing_src;
71c33e05d9SDavid Matlack 
72c33e05d9SDavid Matlack 	/* The amount of memory to allocate for each vCPU. */
73c33e05d9SDavid Matlack 	uint64_t vcpu_memory_bytes;
74c33e05d9SDavid Matlack 
75c33e05d9SDavid Matlack 	/* The number of vCPUs to create in the VM. */
76c33e05d9SDavid Matlack 	int nr_vcpus;
77c33e05d9SDavid Matlack };
78c33e05d9SDavid Matlack 
pread_uint64(int fd,const char * filename,uint64_t index)79df84cef5SSean Christopherson static uint64_t pread_uint64(int fd, const char *filename, uint64_t index)
80c33e05d9SDavid Matlack {
81c33e05d9SDavid Matlack 	uint64_t value;
82c33e05d9SDavid Matlack 	off_t offset = index * sizeof(value);
83c33e05d9SDavid Matlack 
84c33e05d9SDavid Matlack 	TEST_ASSERT(pread(fd, &value, sizeof(value), offset) == sizeof(value),
85c33e05d9SDavid Matlack 		    "pread from %s offset 0x%" PRIx64 " failed!",
86c33e05d9SDavid Matlack 		    filename, offset);
87c33e05d9SDavid Matlack 
88c33e05d9SDavid Matlack 	return value;
89c33e05d9SDavid Matlack 
90c33e05d9SDavid Matlack }
91c33e05d9SDavid Matlack 
92c33e05d9SDavid Matlack #define PAGEMAP_PRESENT (1ULL << 63)
93c33e05d9SDavid Matlack #define PAGEMAP_PFN_MASK ((1ULL << 55) - 1)
94c33e05d9SDavid Matlack 
lookup_pfn(int pagemap_fd,struct kvm_vm * vm,uint64_t gva)95c33e05d9SDavid Matlack static uint64_t lookup_pfn(int pagemap_fd, struct kvm_vm *vm, uint64_t gva)
96c33e05d9SDavid Matlack {
97c33e05d9SDavid Matlack 	uint64_t hva = (uint64_t) addr_gva2hva(vm, gva);
98c33e05d9SDavid Matlack 	uint64_t entry;
99c33e05d9SDavid Matlack 	uint64_t pfn;
100c33e05d9SDavid Matlack 
101c33e05d9SDavid Matlack 	entry = pread_uint64(pagemap_fd, "pagemap", hva / getpagesize());
102c33e05d9SDavid Matlack 	if (!(entry & PAGEMAP_PRESENT))
103c33e05d9SDavid Matlack 		return 0;
104c33e05d9SDavid Matlack 
105c33e05d9SDavid Matlack 	pfn = entry & PAGEMAP_PFN_MASK;
106c33e05d9SDavid Matlack 	__TEST_REQUIRE(pfn, "Looking up PFNs requires CAP_SYS_ADMIN");
107c33e05d9SDavid Matlack 
108c33e05d9SDavid Matlack 	return pfn;
1097ed397d1SSean Christopherson }
110c33e05d9SDavid Matlack 
is_page_idle(int page_idle_fd,uint64_t pfn)111c33e05d9SDavid Matlack static bool is_page_idle(int page_idle_fd, uint64_t pfn)
112c33e05d9SDavid Matlack {
113c33e05d9SDavid Matlack 	uint64_t bits = pread_uint64(page_idle_fd, "page_idle", pfn / 64);
114c33e05d9SDavid Matlack 
115c33e05d9SDavid Matlack 	return !!((bits >> (pfn % 64)) & 1);
116c33e05d9SDavid Matlack }
117c33e05d9SDavid Matlack 
mark_page_idle(int page_idle_fd,uint64_t pfn)118c33e05d9SDavid Matlack static void mark_page_idle(int page_idle_fd, uint64_t pfn)
119c33e05d9SDavid Matlack {
120c33e05d9SDavid Matlack 	uint64_t bits = 1ULL << (pfn % 64);
121c33e05d9SDavid Matlack 
122c33e05d9SDavid Matlack 	TEST_ASSERT(pwrite(page_idle_fd, &bits, 8, 8 * (pfn / 64)) == 8,
123c33e05d9SDavid Matlack 		    "Set page_idle bits for PFN 0x%" PRIx64, pfn);
124c33e05d9SDavid Matlack }
125c33e05d9SDavid Matlack 
mark_vcpu_memory_idle(struct kvm_vm * vm,struct memstress_vcpu_args * vcpu_args)126c33e05d9SDavid Matlack static void mark_vcpu_memory_idle(struct kvm_vm *vm,
127c33e05d9SDavid Matlack 				  struct memstress_vcpu_args *vcpu_args)
128c33e05d9SDavid Matlack {
129df84cef5SSean Christopherson 	int vcpu_idx = vcpu_args->vcpu_idx;
1307812d80cSDavid Matlack 	uint64_t base_gva = vcpu_args->gva;
131c33e05d9SDavid Matlack 	uint64_t pages = vcpu_args->pages;
132df84cef5SSean Christopherson 	uint64_t page;
133df84cef5SSean Christopherson 	uint64_t still_idle = 0;
134df84cef5SSean Christopherson 	uint64_t no_pfn = 0;
135c33e05d9SDavid Matlack 	int page_idle_fd;
136c33e05d9SDavid Matlack 	int pagemap_fd;
137c33e05d9SDavid Matlack 
138c33e05d9SDavid Matlack 	/* If vCPUs are using an overlapping region, let vCPU 0 mark it idle. */
139c33e05d9SDavid Matlack 	if (overlap_memory_access && vcpu_idx)
140c33e05d9SDavid Matlack 		return;
141c33e05d9SDavid Matlack 
142df84cef5SSean Christopherson 	page_idle_fd = open("/sys/kernel/mm/page_idle/bitmap", O_RDWR);
143c33e05d9SDavid Matlack 	TEST_ASSERT(page_idle_fd > 0, "Failed to open page_idle.");
144c33e05d9SDavid Matlack 
145c33e05d9SDavid Matlack 	pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
146c33e05d9SDavid Matlack 	TEST_ASSERT(pagemap_fd > 0, "Failed to open pagemap.");
147c33e05d9SDavid Matlack 
148c33e05d9SDavid Matlack 	for (page = 0; page < pages; page++) {
149c33e05d9SDavid Matlack 		uint64_t gva = base_gva + page * memstress_args.guest_page_size;
150c33e05d9SDavid Matlack 		uint64_t pfn = lookup_pfn(pagemap_fd, vm, gva);
151c33e05d9SDavid Matlack 
1527812d80cSDavid Matlack 		if (!pfn) {
153c33e05d9SDavid Matlack 			no_pfn++;
154c33e05d9SDavid Matlack 			continue;
155c33e05d9SDavid Matlack 		}
156c33e05d9SDavid Matlack 
157c33e05d9SDavid Matlack 		if (is_page_idle(page_idle_fd, pfn)) {
158c33e05d9SDavid Matlack 			still_idle++;
159c33e05d9SDavid Matlack 			continue;
160c33e05d9SDavid Matlack 		}
161c33e05d9SDavid Matlack 
162c33e05d9SDavid Matlack 		mark_page_idle(page_idle_fd, pfn);
163c33e05d9SDavid Matlack 	}
164c33e05d9SDavid Matlack 
165c33e05d9SDavid Matlack 	/*
166c33e05d9SDavid Matlack 	 * Assumption: Less than 1% of pages are going to be swapped out from
167c33e05d9SDavid Matlack 	 * under us during this test.
168c33e05d9SDavid Matlack 	 */
169c33e05d9SDavid Matlack 	TEST_ASSERT(no_pfn < pages / 100,
170c33e05d9SDavid Matlack 		    "vCPU %d: No PFN for %" PRIu64 " out of %" PRIu64 " pages.",
171c33e05d9SDavid Matlack 		    vcpu_idx, no_pfn, pages);
172c33e05d9SDavid Matlack 
173c33e05d9SDavid Matlack 	/*
174df84cef5SSean Christopherson 	 * Check that at least 90% of memory has been marked idle (the rest
175c33e05d9SDavid Matlack 	 * might not be marked idle because the pages have not yet made it to an
176c33e05d9SDavid Matlack 	 * LRU list or the translations are still cached in the TLB). 90% is
1776336a810SEmanuele Giuseppe Esposito 	 * arbitrary; high enough that we ensure most memory access went through
1786336a810SEmanuele Giuseppe Esposito 	 * access tracking but low enough as to not make the test too brittle
1796336a810SEmanuele Giuseppe Esposito 	 * over time and across architectures.
180c33e05d9SDavid Matlack 	 *
181c33e05d9SDavid Matlack 	 * When running the guest as a nested VM, "warn" instead of asserting
182c33e05d9SDavid Matlack 	 * as the TLB size is effectively unlimited and the KVM doesn't
1836336a810SEmanuele Giuseppe Esposito 	 * explicitly flush the TLB when aging SPTEs.  As a result, more pages
184*8fcee042SSean Christopherson 	 * are cached and the guest won't see the "idle" bit cleared.
185*8fcee042SSean Christopherson 	 */
186*8fcee042SSean Christopherson 	if (still_idle >= pages / 10) {
187*8fcee042SSean Christopherson #ifdef __x86_64__
188c33e05d9SDavid Matlack 		TEST_ASSERT(this_cpu_has(X86_FEATURE_HYPERVISOR),
189*8fcee042SSean Christopherson 			    "vCPU%d: Too many pages still idle (%lu out of %lu)",
190*8fcee042SSean Christopherson 			    vcpu_idx, still_idle, pages);
191*8fcee042SSean Christopherson #endif
192*8fcee042SSean Christopherson 		printf("WARNING: vCPU%d: Too many pages still idle (%lu out of %lu), "
193*8fcee042SSean Christopherson 		       "this will affect performance results.\n",
194*8fcee042SSean Christopherson 		       vcpu_idx, still_idle, pages);
195a33004e8SSean Christopherson 	}
196a33004e8SSean Christopherson 
197df84cef5SSean Christopherson 	close(page_idle_fd);
198*8fcee042SSean Christopherson 	close(pagemap_fd);
199c33e05d9SDavid Matlack }
200c33e05d9SDavid Matlack 
assert_ucall(struct kvm_vcpu * vcpu,uint64_t expected_ucall)201c33e05d9SDavid Matlack static void assert_ucall(struct kvm_vcpu *vcpu, uint64_t expected_ucall)
202c33e05d9SDavid Matlack {
203c33e05d9SDavid Matlack 	struct ucall uc;
204df84cef5SSean Christopherson 	uint64_t actual_ucall = get_ucall(vcpu, &uc);
205c33e05d9SDavid Matlack 
206c33e05d9SDavid Matlack 	TEST_ASSERT(expected_ucall == actual_ucall,
207768e9a61SSean Christopherson 		    "Guest exited unexpectedly (expected ucall %" PRIu64
208c33e05d9SDavid Matlack 		    ", got %" PRIu64 ")",
209c33e05d9SDavid Matlack 		    expected_ucall, actual_ucall);
210c33e05d9SDavid Matlack }
211c33e05d9SDavid Matlack 
spin_wait_for_next_iteration(int * current_iteration)212c33e05d9SDavid Matlack static bool spin_wait_for_next_iteration(int *current_iteration)
213c33e05d9SDavid Matlack {
214c33e05d9SDavid Matlack 	int last_iteration = *current_iteration;
215c33e05d9SDavid Matlack 
216c33e05d9SDavid Matlack 	do {
217c33e05d9SDavid Matlack 		if (READ_ONCE(memstress_args.stop_vcpus))
218c33e05d9SDavid Matlack 			return false;
219c33e05d9SDavid Matlack 
220c33e05d9SDavid Matlack 		*current_iteration = READ_ONCE(iteration);
221c33e05d9SDavid Matlack 	} while (last_iteration == *current_iteration);
222c33e05d9SDavid Matlack 
223c33e05d9SDavid Matlack 	return true;
224c33e05d9SDavid Matlack }
225c33e05d9SDavid Matlack 
vcpu_thread_main(struct memstress_vcpu_args * vcpu_args)226c33e05d9SDavid Matlack static void vcpu_thread_main(struct memstress_vcpu_args *vcpu_args)
227c33e05d9SDavid Matlack {
228c33e05d9SDavid Matlack 	struct kvm_vcpu *vcpu = vcpu_args->vcpu;
2297812d80cSDavid Matlack 	struct kvm_vm *vm = memstress_args.vm;
230c33e05d9SDavid Matlack 	int vcpu_idx = vcpu_args->vcpu_idx;
231df84cef5SSean Christopherson 	int current_iteration = 0;
2327812d80cSDavid Matlack 
233df84cef5SSean Christopherson 	while (spin_wait_for_next_iteration(&current_iteration)) {
23436c5ad73SDavid Matlack 		switch (READ_ONCE(iteration_work)) {
235c33e05d9SDavid Matlack 		case ITERATION_ACCESS_MEMORY:
236c33e05d9SDavid Matlack 			vcpu_run(vcpu);
237c33e05d9SDavid Matlack 			assert_ucall(vcpu, UCALL_SYNC);
238c33e05d9SDavid Matlack 			break;
239768e9a61SSean Christopherson 		case ITERATION_MARK_IDLE:
240df84cef5SSean Christopherson 			mark_vcpu_memory_idle(vm, vcpu_args);
241c33e05d9SDavid Matlack 			break;
242c33e05d9SDavid Matlack 		};
243df84cef5SSean Christopherson 
244c33e05d9SDavid Matlack 		vcpu_last_completed_iteration[vcpu_idx] = current_iteration;
245c33e05d9SDavid Matlack 	}
246c33e05d9SDavid Matlack }
247df84cef5SSean Christopherson 
spin_wait_for_vcpu(int vcpu_idx,int target_iteration)248c33e05d9SDavid Matlack static void spin_wait_for_vcpu(int vcpu_idx, int target_iteration)
249c33e05d9SDavid Matlack {
250c33e05d9SDavid Matlack 	while (READ_ONCE(vcpu_last_completed_iteration[vcpu_idx]) !=
251df84cef5SSean Christopherson 	       target_iteration) {
252c33e05d9SDavid Matlack 		continue;
253df84cef5SSean Christopherson 	}
254c33e05d9SDavid Matlack }
255c33e05d9SDavid Matlack 
256c33e05d9SDavid Matlack /* The type of memory accesses to perform in the VM. */
257c33e05d9SDavid Matlack enum access_type {
258c33e05d9SDavid Matlack 	ACCESS_READ,
259c33e05d9SDavid Matlack 	ACCESS_WRITE,
260c33e05d9SDavid Matlack };
261c33e05d9SDavid Matlack 
run_iteration(struct kvm_vm * vm,int nr_vcpus,const char * description)262c33e05d9SDavid Matlack static void run_iteration(struct kvm_vm *vm, int nr_vcpus, const char *description)
263c33e05d9SDavid Matlack {
264c33e05d9SDavid Matlack 	struct timespec ts_start;
265df84cef5SSean Christopherson 	struct timespec ts_elapsed;
266c33e05d9SDavid Matlack 	int next_iteration, i;
267c33e05d9SDavid Matlack 
268c33e05d9SDavid Matlack 	/* Kick off the vCPUs by incrementing iteration. */
269df84cef5SSean Christopherson 	next_iteration = ++iteration;
270c33e05d9SDavid Matlack 
271c33e05d9SDavid Matlack 	clock_gettime(CLOCK_MONOTONIC, &ts_start);
272c33e05d9SDavid Matlack 
273c33e05d9SDavid Matlack 	/* Wait for all vCPUs to finish the iteration. */
274c33e05d9SDavid Matlack 	for (i = 0; i < nr_vcpus; i++)
275c33e05d9SDavid Matlack 		spin_wait_for_vcpu(i, next_iteration);
276c33e05d9SDavid Matlack 
277df84cef5SSean Christopherson 	ts_elapsed = timespec_elapsed(ts_start);
278df84cef5SSean Christopherson 	pr_info("%-30s: %ld.%09lds\n",
279c33e05d9SDavid Matlack 		description, ts_elapsed.tv_sec, ts_elapsed.tv_nsec);
280c33e05d9SDavid Matlack }
281c33e05d9SDavid Matlack 
access_memory(struct kvm_vm * vm,int nr_vcpus,enum access_type access,const char * description)282c33e05d9SDavid Matlack static void access_memory(struct kvm_vm *vm, int nr_vcpus,
283c33e05d9SDavid Matlack 			  enum access_type access, const char *description)
284c33e05d9SDavid Matlack {
285df84cef5SSean Christopherson 	memstress_set_write_percent(vm, (access == ACCESS_READ) ? 0 : 100);
286df84cef5SSean Christopherson 	iteration_work = ITERATION_ACCESS_MEMORY;
287c33e05d9SDavid Matlack 	run_iteration(vm, nr_vcpus, description);
2887812d80cSDavid Matlack }
289c33e05d9SDavid Matlack 
mark_memory_idle(struct kvm_vm * vm,int nr_vcpus)290df84cef5SSean Christopherson static void mark_memory_idle(struct kvm_vm *vm, int nr_vcpus)
291c33e05d9SDavid Matlack {
292c33e05d9SDavid Matlack 	/*
293df84cef5SSean Christopherson 	 * Even though this parallelizes the work across vCPUs, this is still a
294c33e05d9SDavid Matlack 	 * very slow operation because page_idle forces the test to mark one pfn
295c33e05d9SDavid Matlack 	 * at a time and the clear_young notifier serializes on the KVM MMU
296c33e05d9SDavid Matlack 	 * lock.
297c33e05d9SDavid Matlack 	 */
298c33e05d9SDavid Matlack 	pr_debug("Marking VM memory idle (slow)...\n");
299c33e05d9SDavid Matlack 	iteration_work = ITERATION_MARK_IDLE;
300c33e05d9SDavid Matlack 	run_iteration(vm, nr_vcpus, "Mark memory idle");
301c33e05d9SDavid Matlack }
302c33e05d9SDavid Matlack 
run_test(enum vm_guest_mode mode,void * arg)303df84cef5SSean Christopherson static void run_test(enum vm_guest_mode mode, void *arg)
304c33e05d9SDavid Matlack {
305c33e05d9SDavid Matlack 	struct test_params *params = arg;
306c33e05d9SDavid Matlack 	struct kvm_vm *vm;
307c33e05d9SDavid Matlack 	int nr_vcpus = params->nr_vcpus;
308c33e05d9SDavid Matlack 
309c33e05d9SDavid Matlack 	vm = memstress_create_vm(mode, nr_vcpus, params->vcpu_memory_bytes, 1,
310df84cef5SSean Christopherson 				 params->backing_src, !overlap_memory_access);
311c33e05d9SDavid Matlack 
3127812d80cSDavid Matlack 	memstress_start_vcpu_threads(nr_vcpus, vcpu_thread_main);
313cf1d5930SSean Christopherson 
314c33e05d9SDavid Matlack 	pr_info("\n");
3157812d80cSDavid Matlack 	access_memory(vm, nr_vcpus, ACCESS_WRITE, "Populating memory");
316c33e05d9SDavid Matlack 
317c33e05d9SDavid Matlack 	/* As a control, read and write to the populated memory first. */
318df84cef5SSean Christopherson 	access_memory(vm, nr_vcpus, ACCESS_WRITE, "Writing to populated memory");
319c33e05d9SDavid Matlack 	access_memory(vm, nr_vcpus, ACCESS_READ, "Reading from populated memory");
320c33e05d9SDavid Matlack 
321df84cef5SSean Christopherson 	/* Repeat on memory that has been marked as idle. */
322df84cef5SSean Christopherson 	mark_memory_idle(vm, nr_vcpus);
323c33e05d9SDavid Matlack 	access_memory(vm, nr_vcpus, ACCESS_WRITE, "Writing to idle memory");
324c33e05d9SDavid Matlack 	mark_memory_idle(vm, nr_vcpus);
325df84cef5SSean Christopherson 	access_memory(vm, nr_vcpus, ACCESS_READ, "Reading from idle memory");
326df84cef5SSean Christopherson 
327df84cef5SSean Christopherson 	memstress_join_vcpu_threads(nr_vcpus);
328df84cef5SSean Christopherson 	memstress_destroy_vm(vm);
329c33e05d9SDavid Matlack }
33081bcb261SDavid Matlack 
help(char * name)33181bcb261SDavid Matlack static void help(char *name)
33281bcb261SDavid Matlack {
3337812d80cSDavid Matlack 	puts("");
3347812d80cSDavid Matlack 	printf("usage: %s [-h] [-m mode] [-b vcpu_bytes] [-v vcpus] [-o]  [-s mem_type]\n",
335c33e05d9SDavid Matlack 	       name);
336c33e05d9SDavid Matlack 	puts("");
337c33e05d9SDavid Matlack 	printf(" -h: Display this help message.");
338c33e05d9SDavid Matlack 	guest_modes_help();
339c33e05d9SDavid Matlack 	printf(" -b: specify the size of the memory region which should be\n"
340c33e05d9SDavid Matlack 	       "     dirtied by each vCPU. e.g. 10M or 3G.\n"
341c33e05d9SDavid Matlack 	       "     (default: 1G)\n");
342c33e05d9SDavid Matlack 	printf(" -v: specify the number of vCPUs to run.\n");
343c33e05d9SDavid Matlack 	printf(" -o: Overlap guest memory accesses instead of partitioning\n"
344c33e05d9SDavid Matlack 	       "     them into a separate region of memory for each vCPU.\n");
345c33e05d9SDavid Matlack 	backing_src_help("-s");
346c33e05d9SDavid Matlack 	puts("");
347c33e05d9SDavid Matlack 	exit(0);
348c33e05d9SDavid Matlack }
349c33e05d9SDavid Matlack 
main(int argc,char * argv[])350c33e05d9SDavid Matlack int main(int argc, char *argv[])
3519f2fc555SDavid Matlack {
352c33e05d9SDavid Matlack 	struct test_params params = {
353c33e05d9SDavid Matlack 		.backing_src = DEFAULT_VM_MEM_SRC,
354c33e05d9SDavid Matlack 		.vcpu_memory_bytes = DEFAULT_PER_VCPU_MEM_SIZE,
355c33e05d9SDavid Matlack 		.nr_vcpus = 1,
356c33e05d9SDavid Matlack 	};
357c33e05d9SDavid Matlack 	int page_idle_fd;
358c33e05d9SDavid Matlack 	int opt;
3599f2fc555SDavid Matlack 
360c33e05d9SDavid Matlack 	guest_modes_append_default();
361df84cef5SSean Christopherson 
362c33e05d9SDavid Matlack 	while ((opt = getopt(argc, argv, "hm:b:v:os:")) != -1) {
363c33e05d9SDavid Matlack 		switch (opt) {
364c33e05d9SDavid Matlack 		case 'm':
365c33e05d9SDavid Matlack 			guest_modes_cmdline(optarg);
366c33e05d9SDavid Matlack 			break;
367c33e05d9SDavid Matlack 		case 'b':
368c33e05d9SDavid Matlack 			params.vcpu_memory_bytes = parse_size(optarg);
369c33e05d9SDavid Matlack 			break;
370c33e05d9SDavid Matlack 		case 'v':
371c33e05d9SDavid Matlack 			params.nr_vcpus = atoi_positive("Number of vCPUs", optarg);
372c33e05d9SDavid Matlack 			break;
373c33e05d9SDavid Matlack 		case 'o':
374c33e05d9SDavid Matlack 			overlap_memory_access = true;
375c33e05d9SDavid Matlack 			break;
376c33e05d9SDavid Matlack 		case 's':
3770001725dSVipin Sharma 			params.backing_src = parse_backing_src_type(optarg);
378c33e05d9SDavid Matlack 			break;
379c33e05d9SDavid Matlack 		case 'h':
380c33e05d9SDavid Matlack 		default:
381c33e05d9SDavid Matlack 			help(argv[0]);
382c33e05d9SDavid Matlack 			break;
383c33e05d9SDavid Matlack 		}
384c33e05d9SDavid Matlack 	}
385c33e05d9SDavid Matlack 
386c33e05d9SDavid Matlack 	page_idle_fd = open("/sys/kernel/mm/page_idle/bitmap", O_RDWR);
387c33e05d9SDavid Matlack 	__TEST_REQUIRE(page_idle_fd >= 0,
388c33e05d9SDavid Matlack 		       "CONFIG_IDLE_PAGE_TRACKING is not enabled");
389c33e05d9SDavid Matlack 	close(page_idle_fd);
390c33e05d9SDavid Matlack 
391c33e05d9SDavid Matlack 	for_each_guest_mode(run_test, &params);
392c33e05d9SDavid Matlack 
3937ed397d1SSean Christopherson 	return 0;
3947ed397d1SSean Christopherson }
395c33e05d9SDavid Matlack