summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/iommu/iommufd_fail_nth.c
blob: f590417cd67a95bf31065f9b7682bc7c627ebf3a (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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES
 *
 * These tests are "kernel integrity" tests. They are looking for kernel
 * WARN/OOPS/kasn/etc splats triggered by kernel sanitizers & debugging
 * features. It does not attempt to verify that the system calls are doing what
 * they are supposed to do.
 *
 * The basic philosophy is to run a sequence of calls that will succeed and then
 * sweep every failure injection point on that call chain to look for
 * interesting things in error handling.
 *
 * This test is best run with:
 *  echo 1 > /proc/sys/kernel/panic_on_warn
 * If something is actually going wrong.
 */
#include <fcntl.h>
#include <dirent.h>

#define __EXPORTED_HEADERS__
#include <linux/vfio.h>

#include "iommufd_utils.h"

static bool have_fault_injection;

static int writeat(int dfd, const char *fn, const char *val)
{
	size_t val_len = strlen(val);
	ssize_t res;
	int fd;

	fd = openat(dfd, fn, O_WRONLY);
	if (fd == -1)
		return -1;
	res = write(fd, val, val_len);
	assert(res == val_len);
	close(fd);
	return 0;
}

static __attribute__((constructor)) void setup_buffer(void)
{
	PAGE_SIZE = sysconf(_SC_PAGE_SIZE);

	BUFFER_SIZE = 2*1024*1024;

	buffer = mmap(0, BUFFER_SIZE, PROT_READ | PROT_WRITE,
		      MAP_SHARED | MAP_ANONYMOUS, -1, 0);
}

/*
 * This sets up fail_injection in a way that is useful for this test.
 * It does not attempt to restore things back to how they were.
 */
static __attribute__((constructor)) void setup_fault_injection(void)
{
	DIR *debugfs = opendir("/sys/kernel/debug/");
	struct dirent *dent;

	if (!debugfs)
		return;

	/* Allow any allocation call to be fault injected */
	if (writeat(dirfd(debugfs), "failslab/ignore-gfp-wait", "N"))
		return;
	writeat(dirfd(debugfs), "fail_page_alloc/ignore-gfp-wait", "N");
	writeat(dirfd(debugfs), "fail_page_alloc/ignore-gfp-highmem", "N");

	while ((dent = readdir(debugfs))) {
		char fn[300];

		if (strncmp(dent->d_name, "fail", 4) != 0)
			continue;

		/* We are looking for kernel splats, quiet down the log */
		snprintf(fn, sizeof(fn), "%s/verbose", dent->d_name);
		writeat(dirfd(debugfs), fn, "0");
	}
	closedir(debugfs);
	have_fault_injection = true;
}

struct fail_nth_state {
	int proc_fd;
	unsigned int iteration;
};

static void fail_nth_first(struct __test_metadata *_metadata,
			   struct fail_nth_state *nth_state)
{
	char buf[300];

	snprintf(buf, sizeof(buf), "/proc/self/task/%u/fail-nth", getpid());
	nth_state->proc_fd = open(buf, O_RDWR);
	ASSERT_NE(-1, nth_state->proc_fd);
}

static bool fail_nth_next(struct __test_metadata *_metadata,
			  struct fail_nth_state *nth_state,
			  int test_result)
{
	static const char disable_nth[] = "0";
	char buf[300];

	/*
	 * This is just an arbitrary limit based on the current kernel
	 * situation. Changes in the kernel can dramatically change the number of
	 * required fault injection sites, so if this hits it doesn't
	 * necessarily mean a test failure, just that the limit has to be made
	 * bigger.
	 */
	ASSERT_GT(400, nth_state->iteration);
	if (nth_state->iteration != 0) {
		ssize_t res;
		ssize_t res2;

		buf[0] = 0;
		/*
		 * Annoyingly disabling the nth can also fail. This means
		 * the test passed without triggering failure
		 */
		res = pread(nth_state->proc_fd, buf, sizeof(buf), 0);
		if (res == -1 && errno == EFAULT) {
			buf[0] = '1';
			buf[1] = '\n';
			res = 2;
		}

		res2 = pwrite(nth_state->proc_fd, disable_nth,
			      ARRAY_SIZE(disable_nth) - 1, 0);
		if (res2 == -1 && errno == EFAULT) {
			res2 = pwrite(nth_state->proc_fd, disable_nth,
				      ARRAY_SIZE(disable_nth) - 1, 0);
			buf[0] = '1';
			buf[1] = '\n';
		}
		ASSERT_EQ(ARRAY_SIZE(disable_nth) - 1, res2);

		/* printf("  nth %u result=%d nth=%u\n", nth_state->iteration,
		       test_result, atoi(buf)); */
		fflush(stdout);
		ASSERT_LT(1, res);
		if (res != 2 || buf[0] != '0' || buf[1] != '\n')
			return false;
	} else {
		/* printf("  nth %u result=%d\n", nth_state->iteration,
		       test_result); */
	}
	nth_state->iteration++;
	return true;
}

/*
 * This is called during the test to start failure injection. It allows the test
 * to do some setup that has already been swept and thus reduce the required
 * iterations.
 */
void __fail_nth_enable(struct __test_metadata *_metadata,
		       struct fail_nth_state *nth_state)
{
	char buf[300];
	size_t len;

	if (!nth_state->iteration)
		return;

	len = snprintf(buf, sizeof(buf), "%u", nth_state->iteration);
	ASSERT_EQ(len, pwrite(nth_state->proc_fd, buf, len, 0));
}
#define fail_nth_enable() __fail_nth_enable(_metadata, _nth_state)

#define TEST_FAIL_NTH(fixture_name, name)                                           \
	static int test_nth_##name(struct __test_metadata *_metadata,               \
				   FIXTURE_DATA(fixture_name) *self,                \
				   const FIXTURE_VARIANT(fixture_name)              \
					   *variant,                                \
				   struct fail_nth_state *_nth_state);              \
	TEST_F(fixture_name, name)                                                  \
	{                                                                           \
		struct fail_nth_state nth_state = {};                               \
		int test_result = 0;                                                \
										    \
		if (!have_fault_injection)                                          \
			SKIP(return,                                                \
				   "fault injection is not enabled in the kernel"); \
		fail_nth_first(_metadata, &nth_state);                              \
		ASSERT_EQ(0, test_nth_##name(_metadata, self, variant,              \
					     &nth_state));                          \
		while (fail_nth_next(_metadata, &nth_state, test_result)) {         \
			fixture_name##_teardown(_metadata, self, variant);          \
			fixture_name##_setup(_metadata, self, variant);             \
			test_result = test_nth_##name(_metadata, self,              \
						      variant, &nth_state);         \
		};                                                                  \
		ASSERT_EQ(0, test_result);                                          \
	}                                                                           \
	static int test_nth_##name(                                                 \
		struct __test_metadata __attribute__((unused)) *_metadata,          \
		FIXTURE_DATA(fixture_name) __attribute__((unused)) *self,           \
		const FIXTURE_VARIANT(fixture_name) __attribute__((unused))         \
			*variant,                                                   \
		struct fail_nth_state *_nth_state)

FIXTURE(basic_fail_nth)
{
	int fd;
	uint32_t access_id;
};

FIXTURE_SETUP(basic_fail_nth)
{
	self->fd = -1;
	self->access_id = 0;
}

FIXTURE_TEARDOWN(basic_fail_nth)
{
	int rc;

	if (self->access_id) {
		/* The access FD holds the iommufd open until it closes */
		rc = _test_cmd_destroy_access(self->access_id);
		assert(rc == 0);
	}
	teardown_iommufd(self->fd, _metadata);
}

/* Cover ioas.c */
TEST_FAIL_NTH(basic_fail_nth, basic)
{
	struct iommu_iova_range ranges[10];
	uint32_t ioas_id;
	__u64 iova;

	fail_nth_enable();

	self->fd = open("/dev/iommu", O_RDWR);
	if (self->fd == -1)
		return -1;

	if (_test_ioctl_ioas_alloc(self->fd, &ioas_id))
		return -1;

	{
		struct iommu_ioas_iova_ranges ranges_cmd = {
			.size = sizeof(ranges_cmd),
			.num_iovas = ARRAY_SIZE(ranges),
			.ioas_id = ioas_id,
			.allowed_iovas = (uintptr_t)ranges,
		};
		if (ioctl(self->fd, IOMMU_IOAS_IOVA_RANGES, &ranges_cmd))
			return -1;
	}

	{
		struct iommu_ioas_allow_iovas allow_cmd = {
			.size = sizeof(allow_cmd),
			.ioas_id = ioas_id,
			.num_iovas = 1,
			.allowed_iovas = (uintptr_t)ranges,
		};

		ranges[0].start = 16*1024;
		ranges[0].last = BUFFER_SIZE + 16 * 1024 * 600 - 1;
		if (ioctl(self->fd, IOMMU_IOAS_ALLOW_IOVAS, &allow_cmd))
			return -1;
	}

	if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, BUFFER_SIZE, &iova,
				 IOMMU_IOAS_MAP_WRITEABLE |
					 IOMMU_IOAS_MAP_READABLE))
		return -1;

	{
		struct iommu_ioas_copy copy_cmd = {
			.size = sizeof(copy_cmd),
			.flags = IOMMU_IOAS_MAP_WRITEABLE |
				 IOMMU_IOAS_MAP_READABLE,
			.dst_ioas_id = ioas_id,
			.src_ioas_id = ioas_id,
			.src_iova = iova,
			.length = sizeof(ranges),
		};

		if (ioctl(self->fd, IOMMU_IOAS_COPY, &copy_cmd))
			return -1;
	}

	if (_test_ioctl_ioas_unmap(self->fd, ioas_id, iova, BUFFER_SIZE,
				   NULL))
		return -1;
	/* Failure path of no IOVA to unmap */
	_test_ioctl_ioas_unmap(self->fd, ioas_id, iova, BUFFER_SIZE, NULL);
	return 0;
}

/* iopt_area_fill_domains() and iopt_area_fill_domain() */
TEST_FAIL_NTH(basic_fail_nth, map_domain)
{
	uint32_t ioas_id;
	__u32 stdev_id;
	__u32 hwpt_id;
	__u64 iova;

	self->fd = open("/dev/iommu", O_RDWR);
	if (self->fd == -1)
		return -1;

	if (_test_ioctl_ioas_alloc(self->fd, &ioas_id))
		return -1;

	if (_test_ioctl_set_temp_memory_limit(self->fd, 32))
		return -1;

	fail_nth_enable();

	if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL))
		return -1;

	if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, 262144, &iova,
				 IOMMU_IOAS_MAP_WRITEABLE |
					 IOMMU_IOAS_MAP_READABLE))
		return -1;

	if (_test_ioctl_destroy(self->fd, stdev_id))
		return -1;

	if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL))
		return -1;
	return 0;
}

TEST_FAIL_NTH(basic_fail_nth, map_two_domains)
{
	uint32_t ioas_id;
	__u32 stdev_id2;
	__u32 stdev_id;
	__u32 hwpt_id2;
	__u32 hwpt_id;
	__u64 iova;

	self->fd = open("/dev/iommu", O_RDWR);
	if (self->fd == -1)
		return -1;

	if (_test_ioctl_ioas_alloc(self->fd, &ioas_id))
		return -1;

	if (_test_ioctl_set_temp_memory_limit(self->fd, 32))
		return -1;

	if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL))
		return -1;

	fail_nth_enable();

	if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2,
				  NULL))
		return -1;

	if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, 262144, &iova,
				 IOMMU_IOAS_MAP_WRITEABLE |
					 IOMMU_IOAS_MAP_READABLE))
		return -1;

	if (_test_ioctl_destroy(self->fd, stdev_id))
		return -1;

	if (_test_ioctl_destroy(self->fd, stdev_id2))
		return -1;

	if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL))
		return -1;
	if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2,
				  NULL))
		return -1;
	return 0;
}

TEST_FAIL_NTH(basic_fail_nth, access_rw)
{
	uint64_t tmp_big[4096];
	uint32_t ioas_id;
	uint16_t tmp[32];
	__u64 iova;

	self->fd = open("/dev/iommu", O_RDWR);
	if (self->fd == -1)
		return -1;

	if (_test_ioctl_ioas_alloc(self->fd, &ioas_id))
		return -1;

	if (_test_ioctl_set_temp_memory_limit(self->fd, 32))
		return -1;

	if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, 262144, &iova,
				 IOMMU_IOAS_MAP_WRITEABLE |
					 IOMMU_IOAS_MAP_READABLE))
		return -1;

	fail_nth_enable();

	if (_test_cmd_create_access(self->fd, ioas_id, &self->access_id, 0))
		return -1;

	{
		struct iommu_test_cmd access_cmd = {
			.size = sizeof(access_cmd),
			.op = IOMMU_TEST_OP_ACCESS_RW,
			.id = self->access_id,
			.access_rw = { .iova = iova,
				       .length = sizeof(tmp),
				       .uptr = (uintptr_t)tmp },
		};

		// READ
		if (ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_RW),
			  &access_cmd))
			return -1;

		access_cmd.access_rw.flags = MOCK_ACCESS_RW_WRITE;
		if (ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_RW),
			  &access_cmd))
			return -1;

		access_cmd.access_rw.flags = MOCK_ACCESS_RW_SLOW_PATH;
		if (ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_RW),
			  &access_cmd))
			return -1;
		access_cmd.access_rw.flags = MOCK_ACCESS_RW_SLOW_PATH |
					     MOCK_ACCESS_RW_WRITE;
		if (ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_RW),
			  &access_cmd))
			return -1;
	}

	{
		struct iommu_test_cmd access_cmd = {
			.size = sizeof(access_cmd),
			.op = IOMMU_TEST_OP_ACCESS_RW,
			.id = self->access_id,
			.access_rw = { .iova = iova,
				       .flags = MOCK_ACCESS_RW_SLOW_PATH,
				       .length = sizeof(tmp_big),
				       .uptr = (uintptr_t)tmp_big },
		};

		if (ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_RW),
			  &access_cmd))
			return -1;
	}
	if (_test_cmd_destroy_access(self->access_id))
		return -1;
	self->access_id = 0;
	return 0;
}

/* pages.c access functions */
TEST_FAIL_NTH(basic_fail_nth, access_pin)
{
	uint32_t access_pages_id;
	uint32_t ioas_id;
	__u64 iova;

	self->fd = open("/dev/iommu", O_RDWR);
	if (self->fd == -1)
		return -1;

	if (_test_ioctl_ioas_alloc(self->fd, &ioas_id))
		return -1;

	if (_test_ioctl_set_temp_memory_limit(self->fd, 32))
		return -1;

	if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, BUFFER_SIZE, &iova,
				 IOMMU_IOAS_MAP_WRITEABLE |
					 IOMMU_IOAS_MAP_READABLE))
		return -1;

	if (_test_cmd_create_access(self->fd, ioas_id, &self->access_id,
				    MOCK_FLAGS_ACCESS_CREATE_NEEDS_PIN_PAGES))
		return -1;

	fail_nth_enable();

	{
		struct iommu_test_cmd access_cmd = {
			.size = sizeof(access_cmd),
			.op = IOMMU_TEST_OP_ACCESS_PAGES,
			.id = self->access_id,
			.access_pages = { .iova = iova,
					  .length = BUFFER_SIZE,
					  .uptr = (uintptr_t)buffer },
		};

		if (ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_RW),
			  &access_cmd))
			return -1;
		access_pages_id = access_cmd.access_pages.out_access_pages_id;
	}

	if (_test_cmd_destroy_access_pages(self->fd, self->access_id,
					   access_pages_id))
		return -1;

	if (_test_cmd_destroy_access(self->access_id))
		return -1;
	self->access_id = 0;
	return 0;
}

/* iopt_pages_fill_xarray() */
TEST_FAIL_NTH(basic_fail_nth, access_pin_domain)
{
	uint32_t access_pages_id;
	uint32_t ioas_id;
	__u32 stdev_id;
	__u32 hwpt_id;
	__u64 iova;

	self->fd = open("/dev/iommu", O_RDWR);
	if (self->fd == -1)
		return -1;

	if (_test_ioctl_ioas_alloc(self->fd, &ioas_id))
		return -1;

	if (_test_ioctl_set_temp_memory_limit(self->fd, 32))
		return -1;

	if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL))
		return -1;

	if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, BUFFER_SIZE, &iova,
				 IOMMU_IOAS_MAP_WRITEABLE |
					 IOMMU_IOAS_MAP_READABLE))
		return -1;

	if (_test_cmd_create_access(self->fd, ioas_id, &self->access_id,
				    MOCK_FLAGS_ACCESS_CREATE_NEEDS_PIN_PAGES))
		return -1;

	fail_nth_enable();

	{
		struct iommu_test_cmd access_cmd = {
			.size = sizeof(access_cmd),
			.op = IOMMU_TEST_OP_ACCESS_PAGES,
			.id = self->access_id,
			.access_pages = { .iova = iova,
					  .length = BUFFER_SIZE,
					  .uptr = (uintptr_t)buffer },
		};

		if (ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_RW),
			  &access_cmd))
			return -1;
		access_pages_id = access_cmd.access_pages.out_access_pages_id;
	}

	if (_test_cmd_destroy_access_pages(self->fd, self->access_id,
					   access_pages_id))
		return -1;

	if (_test_cmd_destroy_access(self->access_id))
		return -1;
	self->access_id = 0;

	if (_test_ioctl_destroy(self->fd, stdev_id))
		return -1;
	return 0;
}

/* device.c */
TEST_FAIL_NTH(basic_fail_nth, device)
{
	struct iommu_test_hw_info info;
	uint32_t ioas_id;
	uint32_t ioas_id2;
	uint32_t stdev_id;
	uint32_t idev_id;
	uint32_t hwpt_id;
	__u64 iova;

	self->fd = open("/dev/iommu", O_RDWR);
	if (self->fd == -1)
		return -1;

	if (_test_ioctl_ioas_alloc(self->fd, &ioas_id))
		return -1;

	if (_test_ioctl_ioas_alloc(self->fd, &ioas_id2))
		return -1;

	iova = MOCK_APERTURE_START;
	if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, PAGE_SIZE, &iova,
				 IOMMU_IOAS_MAP_FIXED_IOVA |
					 IOMMU_IOAS_MAP_WRITEABLE |
					 IOMMU_IOAS_MAP_READABLE))
		return -1;
	if (_test_ioctl_ioas_map(self->fd, ioas_id2, buffer, PAGE_SIZE, &iova,
				 IOMMU_IOAS_MAP_FIXED_IOVA |
					 IOMMU_IOAS_MAP_WRITEABLE |
					 IOMMU_IOAS_MAP_READABLE))
		return -1;

	fail_nth_enable();

	if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL,
				  &idev_id))
		return -1;

	if (_test_cmd_get_hw_info(self->fd, idev_id, &info, sizeof(info), NULL))
		return -1;

	if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, 0, &hwpt_id,
				 IOMMU_HWPT_DATA_NONE, 0, 0))
		return -1;

	if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL))
		return -1;

	if (_test_cmd_mock_domain_replace(self->fd, stdev_id, hwpt_id, NULL))
		return -1;
	return 0;
}

TEST_HARNESS_MAIN