summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/kvm/demand_paging_test.c
blob: ab302e1f52308fa2990a7220cc8920d701ef89d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// SPDX-License-Identifier: GPL-2.0
/*
 * KVM demand paging test
 * Adapted from dirty_log_test.c
 *
 * Copyright (C) 2018, Red Hat, Inc.
 * Copyright (C) 2019, Google, Inc.
 */

#define _GNU_SOURCE /* for program_invocation_name */

#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <asm/unistd.h>
#include <time.h>
#include <poll.h>
#include <pthread.h>
#include <linux/bitmap.h>
#include <linux/bitops.h>
#include <linux/userfaultfd.h>

#include "test_util.h"
#include "kvm_util.h"
#include "processor.h"

#ifdef __NR_userfaultfd
#define VCPU_ID				1

/* The memory slot index demand page */
#define TEST_MEM_SLOT_INDEX		1

/* Default guest test virtual memory offset */
#define DEFAULT_GUEST_TEST_MEM		0xc0000000

/*
 * Guest/Host shared variables. Ensure addr_gva2hva() and/or
 * sync_global_to/from_guest() are used when accessing from
 * the host. READ/WRITE_ONCE() should also be used with anything
 * that may change.
 */
static uint64_t host_page_size;
static uint64_t guest_page_size;
static uint64_t guest_num_pages;

static char *guest_data_prototype;

/*
 * Guest physical memory offset of the testing memory slot.
 * This will be set to the topmost valid physical address minus
 * the test memory size.
 */
static uint64_t guest_test_phys_mem;

/*
 * Guest virtual memory offset of the testing memory slot.
 * Must not conflict with identity mapped test code.
 */
static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM;

/*
 * Continuously write to the first 8 bytes of each page in the demand paging
 * memory region.
 */
static void guest_code(void)
{
	int i;

	for (i = 0; i < guest_num_pages; i++) {
		uint64_t addr = guest_test_virt_mem;

		addr += i * guest_page_size;
		addr &= ~(host_page_size - 1);
		*(uint64_t *)addr = 0x0123456789ABCDEF;
	}

	GUEST_SYNC(1);
}

/* Points to the test VM memory region on which we are doing demand paging */
static void *host_test_mem;
static uint64_t host_num_pages;

static void *vcpu_worker(void *data)
{
	int ret;
	struct kvm_vm *vm = data;
	struct kvm_run *run;

	run = vcpu_state(vm, VCPU_ID);

	/* Let the guest access its memory */
	ret = _vcpu_run(vm, VCPU_ID);
	TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret);
	if (get_ucall(vm, VCPU_ID, NULL) != UCALL_SYNC) {
		TEST_ASSERT(false,
			    "Invalid guest sync status: exit_reason=%s\n",
			    exit_reason_str(run->exit_reason));
	}

	return NULL;
}

static struct kvm_vm *create_vm(enum vm_guest_mode mode, uint32_t vcpuid,
				uint64_t extra_mem_pages, void *guest_code)
{
	struct kvm_vm *vm;
	uint64_t extra_pg_pages = extra_mem_pages / 512 * 2;

	vm = _vm_create(mode, DEFAULT_GUEST_PHY_PAGES + extra_pg_pages, O_RDWR);
	kvm_vm_elf_load(vm, program_invocation_name, 0, 0);
#ifdef __x86_64__
	vm_create_irqchip(vm);
#endif
	vm_vcpu_add_default(vm, vcpuid, guest_code);
	return vm;
}

static int handle_uffd_page_request(int uffd, uint64_t addr)
{
	pid_t tid;
	struct uffdio_copy copy;
	int r;

	tid = syscall(__NR_gettid);

	copy.src = (uint64_t)guest_data_prototype;
	copy.dst = addr;
	copy.len = host_page_size;
	copy.mode = 0;

	r = ioctl(uffd, UFFDIO_COPY, &copy);
	if (r == -1) {
		DEBUG("Failed Paged in 0x%lx from thread %d with errno: %d\n",
		      addr, tid, errno);
		return r;
	}

	return 0;
}

bool quit_uffd_thread;

struct uffd_handler_args {
	int uffd;
	int pipefd;
	useconds_t delay;
};

static void *uffd_handler_thread_fn(void *arg)
{
	struct uffd_handler_args *uffd_args = (struct uffd_handler_args *)arg;
	int uffd = uffd_args->uffd;
	int pipefd = uffd_args->pipefd;
	useconds_t delay = uffd_args->delay;
	int64_t pages = 0;

	while (!quit_uffd_thread) {
		struct uffd_msg msg;
		struct pollfd pollfd[2];
		char tmp_chr;
		int r;
		uint64_t addr;

		pollfd[0].fd = uffd;
		pollfd[0].events = POLLIN;
		pollfd[1].fd = pipefd;
		pollfd[1].events = POLLIN;

		r = poll(pollfd, 2, -1);
		switch (r) {
		case -1:
			DEBUG("poll err");
			continue;
		case 0:
			continue;
		case 1:
			break;
		default:
			DEBUG("Polling uffd returned %d", r);
			return NULL;
		}

		if (pollfd[0].revents & POLLERR) {
			DEBUG("uffd revents has POLLERR");
			return NULL;
		}

		if (pollfd[1].revents & POLLIN) {
			r = read(pollfd[1].fd, &tmp_chr, 1);
			TEST_ASSERT(r == 1,
				    "Error reading pipefd in UFFD thread\n");
			return NULL;
		}

		if (!pollfd[0].revents & POLLIN)
			continue;

		r = read(uffd, &msg, sizeof(msg));
		if (r == -1) {
			if (errno == EAGAIN)
				continue;
			DEBUG("Read of uffd gor errno %d", errno);
			return NULL;
		}

		if (r != sizeof(msg)) {
			DEBUG("Read on uffd returned unexpected size: %d bytes",
			      r);
			return NULL;
		}

		if (!(msg.event & UFFD_EVENT_PAGEFAULT))
			continue;

		if (delay)
			usleep(delay);
		addr =  msg.arg.pagefault.address;
		r = handle_uffd_page_request(uffd, addr);
		if (r < 0)
			return NULL;
		pages++;
	}

	return NULL;
}

static int setup_demand_paging(struct kvm_vm *vm,
			       pthread_t *uffd_handler_thread, int pipefd,
			       useconds_t uffd_delay)
{
	int uffd;
	struct uffdio_api uffdio_api;
	struct uffdio_register uffdio_register;
	struct uffd_handler_args uffd_args;

	guest_data_prototype = malloc(host_page_size);
	TEST_ASSERT(guest_data_prototype,
		    "Failed to allocate buffer for guest data pattern");
	memset(guest_data_prototype, 0xAB, host_page_size);

	uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
	if (uffd == -1) {
		DEBUG("uffd creation failed\n");
		return -1;
	}

	uffdio_api.api = UFFD_API;
	uffdio_api.features = 0;
	if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1) {
		DEBUG("ioctl uffdio_api failed\n");
		return -1;
	}

	uffdio_register.range.start = (uint64_t)host_test_mem;
	uffdio_register.range.len = host_num_pages * host_page_size;
	uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
	if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1) {
		DEBUG("ioctl uffdio_register failed\n");
		return -1;
	}

	if ((uffdio_register.ioctls & UFFD_API_RANGE_IOCTLS) !=
			UFFD_API_RANGE_IOCTLS) {
		DEBUG("unexpected userfaultfd ioctl set\n");
		return -1;
	}

	uffd_args.uffd = uffd;
	uffd_args.pipefd = pipefd;
	uffd_args.delay = uffd_delay;
	pthread_create(uffd_handler_thread, NULL, uffd_handler_thread_fn,
		       &uffd_args);

	return 0;
}

#define GUEST_MEM_SHIFT 30 /* 1G */
#define PAGE_SHIFT_4K  12

static void run_test(enum vm_guest_mode mode, bool use_uffd,
		     useconds_t uffd_delay)
{
	pthread_t vcpu_thread;
	pthread_t uffd_handler_thread;
	int pipefd[2];
	struct kvm_vm *vm;
	int r;

	/*
	 * We reserve page table for 2 times of extra dirty mem which
	 * will definitely cover the original (1G+) test range.  Here
	 * we do the calculation with 4K page size which is the
	 * smallest so the page number will be enough for all archs
	 * (e.g., 64K page size guest will need even less memory for
	 * page tables).
	 */
	vm = create_vm(mode, VCPU_ID,
		       2ul << (GUEST_MEM_SHIFT - PAGE_SHIFT_4K),
		       guest_code);

	guest_page_size = vm_get_page_size(vm);
	/*
	 * A little more than 1G of guest page sized pages.  Cover the
	 * case where the size is not aligned to 64 pages.
	 */
	guest_num_pages = (1ul << (GUEST_MEM_SHIFT -
				   vm_get_page_shift(vm))) + 16;
#ifdef __s390x__
	/* Round up to multiple of 1M (segment size) */
	guest_num_pages = (guest_num_pages + 0xff) & ~0xffUL;
#endif

	host_page_size = getpagesize();
	host_num_pages = (guest_num_pages * guest_page_size) / host_page_size +
			 !!((guest_num_pages * guest_page_size) %
			    host_page_size);

	guest_test_phys_mem = (vm_get_max_gfn(vm) - guest_num_pages) *
			      guest_page_size;
	guest_test_phys_mem &= ~(host_page_size - 1);

#ifdef __s390x__
	/* Align to 1M (segment size) */
	guest_test_phys_mem &= ~((1 << 20) - 1);
#endif

	DEBUG("guest physical test memory offset: 0x%lx\n",
	      guest_test_phys_mem);


	/* Add an extra memory slot for testing demand paging */
	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
				    guest_test_phys_mem,
				    TEST_MEM_SLOT_INDEX,
				    guest_num_pages, 0);

	/* Do mapping for the demand paging memory slot */
	virt_map(vm, guest_test_virt_mem, guest_test_phys_mem,
		 guest_num_pages * guest_page_size, 0);

	/* Cache the HVA pointer of the region */
	host_test_mem = addr_gpa2hva(vm, (vm_paddr_t)guest_test_phys_mem);

	if (use_uffd) {
		/* Set up user fault fd to handle demand paging requests. */
		r = pipe2(pipefd, O_CLOEXEC | O_NONBLOCK);
		TEST_ASSERT(!r, "Failed to set up pipefd");

		r = setup_demand_paging(vm, &uffd_handler_thread, pipefd[0],
					uffd_delay);
		if (r < 0)
			exit(-r);
	}

#ifdef __x86_64__
	vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
#endif
#ifdef __aarch64__
	ucall_init(vm, NULL);
#endif

	/* Export the shared variables to the guest */
	sync_global_to_guest(vm, host_page_size);
	sync_global_to_guest(vm, guest_page_size);
	sync_global_to_guest(vm, guest_test_virt_mem);
	sync_global_to_guest(vm, guest_num_pages);

	pthread_create(&vcpu_thread, NULL, vcpu_worker, vm);

	/* Wait for the vcpu thread to quit */
	pthread_join(vcpu_thread, NULL);

	if (use_uffd) {
		char c;

		/* Tell the user fault fd handler thread to quit */
		r = write(pipefd[1], &c, 1);
		TEST_ASSERT(r == 1, "Unable to write to pipefd");

		pthread_join(uffd_handler_thread, NULL);
	}

	ucall_uninit(vm);
	kvm_vm_free(vm);

	free(guest_data_prototype);
}

struct guest_mode {
	bool supported;
	bool enabled;
};
static struct guest_mode guest_modes[NUM_VM_MODES];

#define guest_mode_init(mode, supported, enabled) ({ \
	guest_modes[mode] = (struct guest_mode){ supported, enabled }; \
})

static void help(char *name)
{
	int i;

	puts("");
	printf("usage: %s [-h] [-m mode] [-u] [-d uffd_delay_usec]\n", name);
	printf(" -m: specify the guest mode ID to test\n"
	       "     (default: test all supported modes)\n"
	       "     This option may be used multiple times.\n"
	       "     Guest mode IDs:\n");
	for (i = 0; i < NUM_VM_MODES; ++i) {
		printf("         %d:    %s%s\n", i, vm_guest_mode_string(i),
		       guest_modes[i].supported ? " (supported)" : "");
	}
	printf(" -u: use User Fault FD to handle vCPU page\n"
	       "     faults.\n");
	printf(" -d: add a delay in usec to the User Fault\n"
	       "     FD handler to simulate demand paging\n"
	       "     overheads. Ignored without -u.\n");
	puts("");
	exit(0);
}

int main(int argc, char *argv[])
{
	bool mode_selected = false;
	unsigned int mode;
	int opt, i;
	bool use_uffd = false;
	useconds_t uffd_delay = 0;

#ifdef __x86_64__
	guest_mode_init(VM_MODE_PXXV48_4K, true, true);
#endif
#ifdef __aarch64__
	guest_mode_init(VM_MODE_P40V48_4K, true, true);
	guest_mode_init(VM_MODE_P40V48_64K, true, true);
	{
		unsigned int limit = kvm_check_cap(KVM_CAP_ARM_VM_IPA_SIZE);

		if (limit >= 52)
			guest_mode_init(VM_MODE_P52V48_64K, true, true);
		if (limit >= 48) {
			guest_mode_init(VM_MODE_P48V48_4K, true, true);
			guest_mode_init(VM_MODE_P48V48_64K, true, true);
		}
	}
#endif
#ifdef __s390x__
	guest_mode_init(VM_MODE_P40V48_4K, true, true);
#endif

	while ((opt = getopt(argc, argv, "hm:ud:")) != -1) {
		switch (opt) {
		case 'm':
			if (!mode_selected) {
				for (i = 0; i < NUM_VM_MODES; ++i)
					guest_modes[i].enabled = false;
				mode_selected = true;
			}
			mode = strtoul(optarg, NULL, 10);
			TEST_ASSERT(mode < NUM_VM_MODES,
				    "Guest mode ID %d too big", mode);
			guest_modes[mode].enabled = true;
			break;
		case 'u':
			use_uffd = true;
			break;
		case 'd':
			uffd_delay = strtoul(optarg, NULL, 0);
			TEST_ASSERT(uffd_delay >= 0,
				    "A negative UFFD delay is not supported.");
			break;
		case 'h':
		default:
			help(argv[0]);
			break;
		}
	}

	for (i = 0; i < NUM_VM_MODES; ++i) {
		if (!guest_modes[i].enabled)
			continue;
		TEST_ASSERT(guest_modes[i].supported,
			    "Guest mode ID %d (%s) not supported.",
			    i, vm_guest_mode_string(i));
		run_test(i, use_uffd, uffd_delay);
	}

	return 0;
}

#else /* __NR_userfaultfd */

#warning "missing __NR_userfaultfd definition"

int main(void)
{
        printf("skip: Skipping userfaultfd test (missing __NR_userfaultfd)\n");
        return KSFT_SKIP;
}

#endif /* __NR_userfaultfd */