1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES
4 * These tests are "kernel integrity" tests. They are looking for kernel
5 * WARN/OOPS/kasn/etc splats triggered by kernel sanitizers & debugging
6 * features. It does not attempt to verify that the system calls are doing what
7 * they are supposed to do.
9 * The basic philosophy is to run a sequence of calls that will succeed and then
10 * sweep every failure injection point on that call chain to look for
11 * interesting things in error handling.
13 * This test is best run with:
14 * echo 1 > /proc/sys/kernel/panic_on_warn
15 * If something is actually going wrong.
20 #define __EXPORTED_HEADERS__
21 #include <linux/vfio.h>
23 #include "iommufd_utils.h"
25 static bool have_fault_injection
;
27 static int writeat(int dfd
, const char *fn
, const char *val
)
29 size_t val_len
= strlen(val
);
33 fd
= openat(dfd
, fn
, O_WRONLY
);
36 res
= write(fd
, val
, val_len
);
37 assert(res
== val_len
);
42 static __attribute__((constructor
)) void setup_buffer(void)
44 PAGE_SIZE
= sysconf(_SC_PAGE_SIZE
);
46 BUFFER_SIZE
= 2*1024*1024;
48 buffer
= mmap(0, BUFFER_SIZE
, PROT_READ
| PROT_WRITE
,
49 MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
51 mfd_buffer
= memfd_mmap(BUFFER_SIZE
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
56 * This sets up fail_injection in a way that is useful for this test.
57 * It does not attempt to restore things back to how they were.
59 static __attribute__((constructor
)) void setup_fault_injection(void)
61 DIR *debugfs
= opendir("/sys/kernel/debug/");
67 /* Allow any allocation call to be fault injected */
68 if (writeat(dirfd(debugfs
), "failslab/ignore-gfp-wait", "N"))
70 writeat(dirfd(debugfs
), "fail_page_alloc/ignore-gfp-wait", "N");
71 writeat(dirfd(debugfs
), "fail_page_alloc/ignore-gfp-highmem", "N");
73 while ((dent
= readdir(debugfs
))) {
76 if (strncmp(dent
->d_name
, "fail", 4) != 0)
79 /* We are looking for kernel splats, quiet down the log */
80 snprintf(fn
, sizeof(fn
), "%s/verbose", dent
->d_name
);
81 writeat(dirfd(debugfs
), fn
, "0");
84 have_fault_injection
= true;
87 struct fail_nth_state
{
89 unsigned int iteration
;
92 static void fail_nth_first(struct __test_metadata
*_metadata
,
93 struct fail_nth_state
*nth_state
)
97 snprintf(buf
, sizeof(buf
), "/proc/self/task/%u/fail-nth", getpid());
98 nth_state
->proc_fd
= open(buf
, O_RDWR
);
99 ASSERT_NE(-1, nth_state
->proc_fd
);
102 static bool fail_nth_next(struct __test_metadata
*_metadata
,
103 struct fail_nth_state
*nth_state
,
106 static const char disable_nth
[] = "0";
110 * This is just an arbitrary limit based on the current kernel
111 * situation. Changes in the kernel can dramatically change the number of
112 * required fault injection sites, so if this hits it doesn't
113 * necessarily mean a test failure, just that the limit has to be made
116 ASSERT_GT(400, nth_state
->iteration
);
117 if (nth_state
->iteration
!= 0) {
123 * Annoyingly disabling the nth can also fail. This means
124 * the test passed without triggering failure
126 res
= pread(nth_state
->proc_fd
, buf
, sizeof(buf
), 0);
127 if (res
== -1 && errno
== EFAULT
) {
133 res2
= pwrite(nth_state
->proc_fd
, disable_nth
,
134 ARRAY_SIZE(disable_nth
) - 1, 0);
135 if (res2
== -1 && errno
== EFAULT
) {
136 res2
= pwrite(nth_state
->proc_fd
, disable_nth
,
137 ARRAY_SIZE(disable_nth
) - 1, 0);
141 ASSERT_EQ(ARRAY_SIZE(disable_nth
) - 1, res2
);
143 /* printf(" nth %u result=%d nth=%u\n", nth_state->iteration,
144 test_result, atoi(buf)); */
147 if (res
!= 2 || buf
[0] != '0' || buf
[1] != '\n')
150 /* printf(" nth %u result=%d\n", nth_state->iteration,
153 nth_state
->iteration
++;
158 * This is called during the test to start failure injection. It allows the test
159 * to do some setup that has already been swept and thus reduce the required
162 void __fail_nth_enable(struct __test_metadata
*_metadata
,
163 struct fail_nth_state
*nth_state
)
168 if (!nth_state
->iteration
)
171 len
= snprintf(buf
, sizeof(buf
), "%u", nth_state
->iteration
);
172 ASSERT_EQ(len
, pwrite(nth_state
->proc_fd
, buf
, len
, 0));
174 #define fail_nth_enable() __fail_nth_enable(_metadata, _nth_state)
176 #define TEST_FAIL_NTH(fixture_name, name) \
177 static int test_nth_##name(struct __test_metadata *_metadata, \
178 FIXTURE_DATA(fixture_name) *self, \
179 const FIXTURE_VARIANT(fixture_name) \
181 struct fail_nth_state *_nth_state); \
182 TEST_F(fixture_name, name) \
184 struct fail_nth_state nth_state = {}; \
185 int test_result = 0; \
187 if (!have_fault_injection) \
189 "fault injection is not enabled in the kernel"); \
190 fail_nth_first(_metadata, &nth_state); \
191 ASSERT_EQ(0, test_nth_##name(_metadata, self, variant, \
193 while (fail_nth_next(_metadata, &nth_state, test_result)) { \
194 fixture_name##_teardown(_metadata, self, variant); \
195 fixture_name##_setup(_metadata, self, variant); \
196 test_result = test_nth_##name(_metadata, self, \
197 variant, &nth_state); \
199 ASSERT_EQ(0, test_result); \
201 static int test_nth_##name( \
202 struct __test_metadata __attribute__((unused)) *_metadata, \
203 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
204 const FIXTURE_VARIANT(fixture_name) __attribute__((unused)) \
206 struct fail_nth_state *_nth_state)
208 FIXTURE(basic_fail_nth
)
214 FIXTURE_SETUP(basic_fail_nth
)
220 FIXTURE_TEARDOWN(basic_fail_nth
)
224 if (self
->access_id
) {
225 /* The access FD holds the iommufd open until it closes */
226 rc
= _test_cmd_destroy_access(self
->access_id
);
229 teardown_iommufd(self
->fd
, _metadata
);
233 TEST_FAIL_NTH(basic_fail_nth
, basic
)
235 struct iommu_iova_range ranges
[10];
241 self
->fd
= open("/dev/iommu", O_RDWR
);
245 if (_test_ioctl_ioas_alloc(self
->fd
, &ioas_id
))
249 struct iommu_ioas_iova_ranges ranges_cmd
= {
250 .size
= sizeof(ranges_cmd
),
251 .num_iovas
= ARRAY_SIZE(ranges
),
253 .allowed_iovas
= (uintptr_t)ranges
,
255 if (ioctl(self
->fd
, IOMMU_IOAS_IOVA_RANGES
, &ranges_cmd
))
260 struct iommu_ioas_allow_iovas allow_cmd
= {
261 .size
= sizeof(allow_cmd
),
264 .allowed_iovas
= (uintptr_t)ranges
,
267 ranges
[0].start
= 16*1024;
268 ranges
[0].last
= BUFFER_SIZE
+ 16 * 1024 * 600 - 1;
269 if (ioctl(self
->fd
, IOMMU_IOAS_ALLOW_IOVAS
, &allow_cmd
))
273 if (_test_ioctl_ioas_map(self
->fd
, ioas_id
, buffer
, BUFFER_SIZE
, &iova
,
274 IOMMU_IOAS_MAP_WRITEABLE
|
275 IOMMU_IOAS_MAP_READABLE
))
279 struct iommu_ioas_copy copy_cmd
= {
280 .size
= sizeof(copy_cmd
),
281 .flags
= IOMMU_IOAS_MAP_WRITEABLE
|
282 IOMMU_IOAS_MAP_READABLE
,
283 .dst_ioas_id
= ioas_id
,
284 .src_ioas_id
= ioas_id
,
286 .length
= sizeof(ranges
),
289 if (ioctl(self
->fd
, IOMMU_IOAS_COPY
, ©_cmd
))
293 if (_test_ioctl_ioas_unmap(self
->fd
, ioas_id
, iova
, BUFFER_SIZE
,
296 /* Failure path of no IOVA to unmap */
297 _test_ioctl_ioas_unmap(self
->fd
, ioas_id
, iova
, BUFFER_SIZE
, NULL
);
301 /* iopt_area_fill_domains() and iopt_area_fill_domain() */
302 TEST_FAIL_NTH(basic_fail_nth
, map_domain
)
309 self
->fd
= open("/dev/iommu", O_RDWR
);
313 if (_test_ioctl_ioas_alloc(self
->fd
, &ioas_id
))
316 if (_test_ioctl_set_temp_memory_limit(self
->fd
, 32))
321 if (_test_cmd_mock_domain(self
->fd
, ioas_id
, &stdev_id
, &hwpt_id
, NULL
))
324 if (_test_ioctl_ioas_map(self
->fd
, ioas_id
, buffer
, 262144, &iova
,
325 IOMMU_IOAS_MAP_WRITEABLE
|
326 IOMMU_IOAS_MAP_READABLE
))
329 if (_test_ioctl_destroy(self
->fd
, stdev_id
))
332 if (_test_cmd_mock_domain(self
->fd
, ioas_id
, &stdev_id
, &hwpt_id
, NULL
))
337 /* iopt_area_fill_domains() and iopt_area_fill_domain() */
338 TEST_FAIL_NTH(basic_fail_nth
, map_file_domain
)
345 self
->fd
= open("/dev/iommu", O_RDWR
);
349 if (_test_ioctl_ioas_alloc(self
->fd
, &ioas_id
))
352 if (_test_ioctl_set_temp_memory_limit(self
->fd
, 32))
357 if (_test_cmd_mock_domain(self
->fd
, ioas_id
, &stdev_id
, &hwpt_id
, NULL
))
360 if (_test_ioctl_ioas_map_file(self
->fd
, ioas_id
, mfd
, 0, 262144, &iova
,
361 IOMMU_IOAS_MAP_WRITEABLE
|
362 IOMMU_IOAS_MAP_READABLE
))
365 if (_test_ioctl_destroy(self
->fd
, stdev_id
))
368 if (_test_cmd_mock_domain(self
->fd
, ioas_id
, &stdev_id
, &hwpt_id
, NULL
))
373 TEST_FAIL_NTH(basic_fail_nth
, map_two_domains
)
382 self
->fd
= open("/dev/iommu", O_RDWR
);
386 if (_test_ioctl_ioas_alloc(self
->fd
, &ioas_id
))
389 if (_test_ioctl_set_temp_memory_limit(self
->fd
, 32))
392 if (_test_cmd_mock_domain(self
->fd
, ioas_id
, &stdev_id
, &hwpt_id
, NULL
))
397 if (_test_cmd_mock_domain(self
->fd
, ioas_id
, &stdev_id2
, &hwpt_id2
,
401 if (_test_ioctl_ioas_map(self
->fd
, ioas_id
, buffer
, 262144, &iova
,
402 IOMMU_IOAS_MAP_WRITEABLE
|
403 IOMMU_IOAS_MAP_READABLE
))
406 if (_test_ioctl_destroy(self
->fd
, stdev_id
))
409 if (_test_ioctl_destroy(self
->fd
, stdev_id2
))
412 if (_test_cmd_mock_domain(self
->fd
, ioas_id
, &stdev_id
, &hwpt_id
, NULL
))
414 if (_test_cmd_mock_domain(self
->fd
, ioas_id
, &stdev_id2
, &hwpt_id2
,
420 TEST_FAIL_NTH(basic_fail_nth
, access_rw
)
422 uint64_t tmp_big
[4096];
427 self
->fd
= open("/dev/iommu", O_RDWR
);
431 if (_test_ioctl_ioas_alloc(self
->fd
, &ioas_id
))
434 if (_test_ioctl_set_temp_memory_limit(self
->fd
, 32))
437 if (_test_ioctl_ioas_map(self
->fd
, ioas_id
, buffer
, 262144, &iova
,
438 IOMMU_IOAS_MAP_WRITEABLE
|
439 IOMMU_IOAS_MAP_READABLE
))
444 if (_test_cmd_create_access(self
->fd
, ioas_id
, &self
->access_id
, 0))
448 struct iommu_test_cmd access_cmd
= {
449 .size
= sizeof(access_cmd
),
450 .op
= IOMMU_TEST_OP_ACCESS_RW
,
451 .id
= self
->access_id
,
452 .access_rw
= { .iova
= iova
,
453 .length
= sizeof(tmp
),
454 .uptr
= (uintptr_t)tmp
},
458 if (ioctl(self
->fd
, _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_RW
),
462 access_cmd
.access_rw
.flags
= MOCK_ACCESS_RW_WRITE
;
463 if (ioctl(self
->fd
, _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_RW
),
467 access_cmd
.access_rw
.flags
= MOCK_ACCESS_RW_SLOW_PATH
;
468 if (ioctl(self
->fd
, _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_RW
),
471 access_cmd
.access_rw
.flags
= MOCK_ACCESS_RW_SLOW_PATH
|
472 MOCK_ACCESS_RW_WRITE
;
473 if (ioctl(self
->fd
, _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_RW
),
479 struct iommu_test_cmd access_cmd
= {
480 .size
= sizeof(access_cmd
),
481 .op
= IOMMU_TEST_OP_ACCESS_RW
,
482 .id
= self
->access_id
,
483 .access_rw
= { .iova
= iova
,
484 .flags
= MOCK_ACCESS_RW_SLOW_PATH
,
485 .length
= sizeof(tmp_big
),
486 .uptr
= (uintptr_t)tmp_big
},
489 if (ioctl(self
->fd
, _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_RW
),
493 if (_test_cmd_destroy_access(self
->access_id
))
499 /* pages.c access functions */
500 TEST_FAIL_NTH(basic_fail_nth
, access_pin
)
502 uint32_t access_pages_id
;
506 self
->fd
= open("/dev/iommu", O_RDWR
);
510 if (_test_ioctl_ioas_alloc(self
->fd
, &ioas_id
))
513 if (_test_ioctl_set_temp_memory_limit(self
->fd
, 32))
516 if (_test_ioctl_ioas_map(self
->fd
, ioas_id
, buffer
, BUFFER_SIZE
, &iova
,
517 IOMMU_IOAS_MAP_WRITEABLE
|
518 IOMMU_IOAS_MAP_READABLE
))
521 if (_test_cmd_create_access(self
->fd
, ioas_id
, &self
->access_id
,
522 MOCK_FLAGS_ACCESS_CREATE_NEEDS_PIN_PAGES
))
528 struct iommu_test_cmd access_cmd
= {
529 .size
= sizeof(access_cmd
),
530 .op
= IOMMU_TEST_OP_ACCESS_PAGES
,
531 .id
= self
->access_id
,
532 .access_pages
= { .iova
= iova
,
533 .length
= BUFFER_SIZE
,
534 .uptr
= (uintptr_t)buffer
},
537 if (ioctl(self
->fd
, _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_RW
),
540 access_pages_id
= access_cmd
.access_pages
.out_access_pages_id
;
543 if (_test_cmd_destroy_access_pages(self
->fd
, self
->access_id
,
547 if (_test_cmd_destroy_access(self
->access_id
))
553 /* iopt_pages_fill_xarray() */
554 TEST_FAIL_NTH(basic_fail_nth
, access_pin_domain
)
556 uint32_t access_pages_id
;
562 self
->fd
= open("/dev/iommu", O_RDWR
);
566 if (_test_ioctl_ioas_alloc(self
->fd
, &ioas_id
))
569 if (_test_ioctl_set_temp_memory_limit(self
->fd
, 32))
572 if (_test_cmd_mock_domain(self
->fd
, ioas_id
, &stdev_id
, &hwpt_id
, NULL
))
575 if (_test_ioctl_ioas_map(self
->fd
, ioas_id
, buffer
, BUFFER_SIZE
, &iova
,
576 IOMMU_IOAS_MAP_WRITEABLE
|
577 IOMMU_IOAS_MAP_READABLE
))
580 if (_test_cmd_create_access(self
->fd
, ioas_id
, &self
->access_id
,
581 MOCK_FLAGS_ACCESS_CREATE_NEEDS_PIN_PAGES
))
587 struct iommu_test_cmd access_cmd
= {
588 .size
= sizeof(access_cmd
),
589 .op
= IOMMU_TEST_OP_ACCESS_PAGES
,
590 .id
= self
->access_id
,
591 .access_pages
= { .iova
= iova
,
592 .length
= BUFFER_SIZE
,
593 .uptr
= (uintptr_t)buffer
},
596 if (ioctl(self
->fd
, _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_RW
),
599 access_pages_id
= access_cmd
.access_pages
.out_access_pages_id
;
602 if (_test_cmd_destroy_access_pages(self
->fd
, self
->access_id
,
606 if (_test_cmd_destroy_access(self
->access_id
))
610 if (_test_ioctl_destroy(self
->fd
, stdev_id
))
616 TEST_FAIL_NTH(basic_fail_nth
, device
)
618 struct iommu_hwpt_selftest data
= {
619 .iotlb
= IOMMU_TEST_IOTLB_DEFAULT
,
621 struct iommu_test_hw_info info
;
622 uint32_t fault_id
, fault_fd
;
623 uint32_t fault_hwpt_id
;
633 self
->fd
= open("/dev/iommu", O_RDWR
);
637 if (_test_ioctl_ioas_alloc(self
->fd
, &ioas_id
))
640 if (_test_ioctl_ioas_alloc(self
->fd
, &ioas_id2
))
643 iova
= MOCK_APERTURE_START
;
644 if (_test_ioctl_ioas_map(self
->fd
, ioas_id
, buffer
, PAGE_SIZE
, &iova
,
645 IOMMU_IOAS_MAP_FIXED_IOVA
|
646 IOMMU_IOAS_MAP_WRITEABLE
|
647 IOMMU_IOAS_MAP_READABLE
))
649 if (_test_ioctl_ioas_map(self
->fd
, ioas_id2
, buffer
, PAGE_SIZE
, &iova
,
650 IOMMU_IOAS_MAP_FIXED_IOVA
|
651 IOMMU_IOAS_MAP_WRITEABLE
|
652 IOMMU_IOAS_MAP_READABLE
))
657 if (_test_cmd_mock_domain(self
->fd
, ioas_id
, &stdev_id
, NULL
,
661 if (_test_cmd_get_hw_info(self
->fd
, idev_id
, &info
, sizeof(info
), NULL
))
664 if (_test_cmd_hwpt_alloc(self
->fd
, idev_id
, ioas_id
, 0, 0, &hwpt_id
,
665 IOMMU_HWPT_DATA_NONE
, 0, 0))
668 if (_test_cmd_mock_domain_replace(self
->fd
, stdev_id
, ioas_id2
, NULL
))
671 if (_test_cmd_mock_domain_replace(self
->fd
, stdev_id
, hwpt_id
, NULL
))
674 if (_test_cmd_hwpt_alloc(self
->fd
, idev_id
, ioas_id
, 0,
675 IOMMU_HWPT_ALLOC_NEST_PARENT
, &hwpt_id
,
676 IOMMU_HWPT_DATA_NONE
, 0, 0))
679 if (_test_cmd_viommu_alloc(self
->fd
, idev_id
, hwpt_id
,
680 IOMMU_VIOMMU_TYPE_SELFTEST
, 0, &viommu_id
))
683 if (_test_cmd_vdevice_alloc(self
->fd
, viommu_id
, idev_id
, 0, &vdev_id
))
686 if (_test_ioctl_fault_alloc(self
->fd
, &fault_id
, &fault_fd
))
690 if (_test_cmd_hwpt_alloc(self
->fd
, idev_id
, hwpt_id
, fault_id
,
691 IOMMU_HWPT_FAULT_ID_VALID
, &fault_hwpt_id
,
692 IOMMU_HWPT_DATA_SELFTEST
, &data
, sizeof(data
)))