1 // SPDX-License-Identifier: GPL-2.0-only
3 * Userfaultfd unit tests.
5 * Copyright (C) 2015-2023 Red Hat, Inc.
8 #include <asm-generic/unistd.h>
9 #include "uffd-common.h"
11 #include "../../../../mm/gup_test.h"
13 /* The unit test doesn't need a large or random size, make it 32MB for now */
14 #define UFFD_TEST_MEM_SIZE (32UL << 20)
16 #define MEM_ANON BIT_ULL(0)
17 #define MEM_SHMEM BIT_ULL(1)
18 #define MEM_SHMEM_PRIVATE BIT_ULL(2)
19 #define MEM_HUGETLB BIT_ULL(3)
20 #define MEM_HUGETLB_PRIVATE BIT_ULL(4)
22 #define MEM_ALL (MEM_ANON | MEM_SHMEM | MEM_SHMEM_PRIVATE | \
23 MEM_HUGETLB | MEM_HUGETLB_PRIVATE)
25 #define ALIGN_UP(x, align_to) \
26 ((__typeof__(x))((((unsigned long)(x)) + ((align_to)-1)) & ~((align_to)-1)))
30 unsigned int mem_flag;
31 uffd_test_ops_t *mem_ops;
34 typedef struct mem_type mem_type_t;
36 mem_type_t mem_types[] = {
40 .mem_ops = &anon_uffd_test_ops,
45 .mem_flag = MEM_SHMEM,
46 .mem_ops = &shmem_uffd_test_ops,
50 .name = "shmem-private",
51 .mem_flag = MEM_SHMEM_PRIVATE,
52 .mem_ops = &shmem_uffd_test_ops,
57 .mem_flag = MEM_HUGETLB,
58 .mem_ops = &hugetlb_uffd_test_ops,
62 .name = "hugetlb-private",
63 .mem_flag = MEM_HUGETLB_PRIVATE,
64 .mem_ops = &hugetlb_uffd_test_ops,
69 /* Arguments to be passed over to each uffd unit test */
70 struct uffd_test_args {
73 typedef struct uffd_test_args uffd_test_args_t;
75 /* Returns: UFFD_TEST_* */
76 typedef void (*uffd_test_fn)(uffd_test_args_t *);
81 unsigned int mem_targets;
82 uint64_t uffd_feature_required;
83 uffd_test_case_ops_t *test_case_ops;
86 static void uffd_test_report(void)
88 printf("Userfaults unit tests: pass=%u, skip=%u, fail=%u (total=%u)\n",
95 static void uffd_test_pass(void)
101 #define uffd_test_start(...) do { \
102 printf("Testing "); \
103 printf(__VA_ARGS__); \
108 #define uffd_test_fail(...) do { \
109 printf("failed [reason: "); \
110 printf(__VA_ARGS__); \
112 ksft_inc_fail_cnt(); \
115 static void uffd_test_skip(const char *message)
117 printf("skipped [reason: %s]\n", message);
118 ksft_inc_xskip_cnt();
122 * Returns 1 if specific userfaultfd supported, 0 otherwise. Note, we'll
123 * return 1 even if some test failed as long as uffd supported, because in
124 * that case we still want to proceed with the rest uffd unit tests.
126 static int test_uffd_api(bool use_dev)
128 struct uffdio_api uffdio_api;
131 uffd_test_start("UFFDIO_API (with %s)",
132 use_dev ? "/dev/userfaultfd" : "syscall");
135 uffd = uffd_open_dev(UFFD_FLAGS);
137 uffd = uffd_open_sys(UFFD_FLAGS);
139 uffd_test_skip("cannot open userfaultfd handle");
143 /* Test wrong UFFD_API */
144 uffdio_api.api = 0xab;
145 uffdio_api.features = 0;
146 if (ioctl(uffd, UFFDIO_API, &uffdio_api) == 0) {
147 uffd_test_fail("UFFDIO_API should fail with wrong api but didn't");
151 /* Test wrong feature bit */
152 uffdio_api.api = UFFD_API;
153 uffdio_api.features = BIT_ULL(63);
154 if (ioctl(uffd, UFFDIO_API, &uffdio_api) == 0) {
155 uffd_test_fail("UFFDIO_API should fail with wrong feature but didn't");
159 /* Test normal UFFDIO_API */
160 uffdio_api.api = UFFD_API;
161 uffdio_api.features = 0;
162 if (ioctl(uffd, UFFDIO_API, &uffdio_api)) {
163 uffd_test_fail("UFFDIO_API should succeed but failed");
167 /* Test double requests of UFFDIO_API with a random feature set */
168 uffdio_api.features = BIT_ULL(0);
169 if (ioctl(uffd, UFFDIO_API, &uffdio_api) == 0) {
170 uffd_test_fail("UFFDIO_API should reject initialized uffd");
177 /* We have a valid uffd handle */
182 * This function initializes the global variables. TODO: remove global
183 * vars and then remove this.
186 uffd_setup_environment(uffd_test_args_t *args, uffd_test_case_t *test,
187 mem_type_t *mem_type, const char **errmsg)
189 map_shared = mem_type->shared;
190 uffd_test_ops = mem_type->mem_ops;
191 uffd_test_case_ops = test->test_case_ops;
193 if (mem_type->mem_flag & (MEM_HUGETLB_PRIVATE | MEM_HUGETLB))
194 page_size = default_huge_page_size();
198 nr_pages = UFFD_TEST_MEM_SIZE / page_size;
199 /* TODO: remove this global var.. it's so ugly */
202 /* Initialize test arguments */
203 args->mem_type = mem_type;
205 return uffd_test_ctx_init(test->uffd_feature_required, errmsg);
208 static bool uffd_feature_supported(uffd_test_case_t *test)
212 if (uffd_get_features(&features))
215 return (features & test->uffd_feature_required) ==
216 test->uffd_feature_required;
219 static int pagemap_open(void)
221 int fd = open("/proc/self/pagemap", O_RDONLY);
229 /* This macro let __LINE__ works in err() */
230 #define pagemap_check_wp(value, wp) do { \
231 if (!!(value & PM_UFFD_WP) != wp) \
232 err("pagemap uffd-wp bit error: 0x%"PRIx64, value); \
236 int parent_uffd, child_uffd;
239 static void *fork_event_consumer(void *data)
241 fork_event_args *args = data;
242 struct uffd_msg msg = { 0 };
244 ready_for_fork = true;
246 /* Read until a full msg received */
247 while (uffd_read_msg(args->parent_uffd, &msg));
249 if (msg.event != UFFD_EVENT_FORK)
250 err("wrong message: %u\n", msg.event);
252 /* Just to be properly freed later */
253 args->child_uffd = msg.arg.fork.ufd;
263 * Returns 0 if succeed, <0 for errors. pin_pages() needs to be paired
264 * with unpin_pages(). Currently it needs to be RO longterm pin to satisfy
265 * all needs of the test cases (e.g., trigger unshare, trigger fork() early
268 static int pin_pages(pin_args *args, void *buffer, size_t size)
270 struct pin_longterm_test test = {
271 .addr = (uintptr_t)buffer,
278 err("already pinned");
280 args->gup_fd = open("/sys/kernel/debug/gup_test", O_RDWR);
281 if (args->gup_fd < 0)
284 if (ioctl(args->gup_fd, PIN_LONGTERM_TEST_START, &test)) {
285 /* Even if gup_test existed, can be an old gup_test / kernel */
293 static void unpin_pages(pin_args *args)
296 err("unpin without pin first");
297 if (ioctl(args->gup_fd, PIN_LONGTERM_TEST_STOP))
298 err("PIN_LONGTERM_TEST_STOP");
300 args->pinned = false;
303 static int pagemap_test_fork(int uffd, bool with_event, bool test_pin)
305 fork_event_args args = { .parent_uffd = uffd, .child_uffd = -1 };
311 /* Prepare a thread to resolve EVENT_FORK */
313 ready_for_fork = false;
314 if (pthread_create(&thread, NULL, fork_event_consumer, &args))
315 err("pthread_create()");
316 while (!ready_for_fork)
317 ; /* Wait for the poll_thread to start executing before forking */
322 /* Open the pagemap fd of the child itself */
327 if (test_pin && pin_pages(&args, area_dst, page_size))
329 * Normally when reach here we have pinned in
330 * previous tests, so shouldn't fail anymore
332 err("pin page failed in child");
334 value = pagemap_get_entry(fd, area_dst);
336 * After fork(), we should handle uffd-wp bit differently:
338 * (1) when with EVENT_FORK, it should persist
339 * (2) when without EVENT_FORK, it should be dropped
341 pagemap_check_wp(value, with_event);
347 waitpid(child, &result, 0);
350 if (pthread_join(thread, NULL))
351 err("pthread_join()");
352 if (args.child_uffd < 0)
353 err("Didn't receive child uffd");
354 close(args.child_uffd);
360 static void uffd_wp_unpopulated_test(uffd_test_args_t *args)
365 if (uffd_register(uffd, area_dst, nr_pages * page_size,
367 err("register failed");
369 pagemap_fd = pagemap_open();
371 /* Test applying pte marker to anon unpopulated */
372 wp_range(uffd, (uint64_t)area_dst, page_size, true);
373 value = pagemap_get_entry(pagemap_fd, area_dst);
374 pagemap_check_wp(value, true);
376 /* Test unprotect on anon pte marker */
377 wp_range(uffd, (uint64_t)area_dst, page_size, false);
378 value = pagemap_get_entry(pagemap_fd, area_dst);
379 pagemap_check_wp(value, false);
381 /* Test zap on anon marker */
382 wp_range(uffd, (uint64_t)area_dst, page_size, true);
383 if (madvise(area_dst, page_size, MADV_DONTNEED))
384 err("madvise(MADV_DONTNEED) failed");
385 value = pagemap_get_entry(pagemap_fd, area_dst);
386 pagemap_check_wp(value, false);
388 /* Test fault in after marker removed */
390 value = pagemap_get_entry(pagemap_fd, area_dst);
391 pagemap_check_wp(value, false);
392 /* Drop it to make pte none again */
393 if (madvise(area_dst, page_size, MADV_DONTNEED))
394 err("madvise(MADV_DONTNEED) failed");
396 /* Test read-zero-page upon pte marker */
397 wp_range(uffd, (uint64_t)area_dst, page_size, true);
398 *(volatile char *)area_dst;
399 /* Drop it to make pte none again */
400 if (madvise(area_dst, page_size, MADV_DONTNEED))
401 err("madvise(MADV_DONTNEED) failed");
406 static void uffd_wp_fork_test_common(uffd_test_args_t *args,
412 if (uffd_register(uffd, area_dst, nr_pages * page_size,
414 err("register failed");
416 pagemap_fd = pagemap_open();
420 wp_range(uffd, (uint64_t)area_dst, page_size, true);
421 value = pagemap_get_entry(pagemap_fd, area_dst);
422 pagemap_check_wp(value, true);
423 if (pagemap_test_fork(uffd, with_event, false)) {
424 uffd_test_fail("Detected %s uffd-wp bit in child in present pte",
425 with_event ? "missing" : "stall");
430 * This is an attempt for zapping the pgtable so as to test the
433 * For private mappings, PAGEOUT will only work on exclusive ptes
434 * (PM_MMAP_EXCLUSIVE) which we should satisfy.
436 * For shared, PAGEOUT may not work. Use DONTNEED instead which
437 * plays a similar role of zapping (rather than freeing the page)
438 * to expose pte markers.
440 if (args->mem_type->shared) {
441 if (madvise(area_dst, page_size, MADV_DONTNEED))
442 err("MADV_DONTNEED");
445 * NOTE: ignore retval because private-hugetlb doesn't yet
446 * support swapping, so it could fail.
448 madvise(area_dst, page_size, MADV_PAGEOUT);
451 /* Uffd-wp should persist even swapped out */
452 value = pagemap_get_entry(pagemap_fd, area_dst);
453 pagemap_check_wp(value, true);
454 if (pagemap_test_fork(uffd, with_event, false)) {
455 uffd_test_fail("Detected %s uffd-wp bit in child in zapped pte",
456 with_event ? "missing" : "stall");
460 /* Unprotect; this tests swap pte modifications */
461 wp_range(uffd, (uint64_t)area_dst, page_size, false);
462 value = pagemap_get_entry(pagemap_fd, area_dst);
463 pagemap_check_wp(value, false);
465 /* Fault in the page from disk */
467 value = pagemap_get_entry(pagemap_fd, area_dst);
468 pagemap_check_wp(value, false);
471 if (uffd_unregister(uffd, area_dst, nr_pages * page_size))
472 err("unregister failed");
476 static void uffd_wp_fork_test(uffd_test_args_t *args)
478 uffd_wp_fork_test_common(args, false);
481 static void uffd_wp_fork_with_event_test(uffd_test_args_t *args)
483 uffd_wp_fork_test_common(args, true);
486 static void uffd_wp_fork_pin_test_common(uffd_test_args_t *args,
490 pin_args pin_args = {};
492 if (uffd_register(uffd, area_dst, page_size, false, true, false))
493 err("register failed");
495 pagemap_fd = pagemap_open();
499 wp_range(uffd, (uint64_t)area_dst, page_size, true);
502 * 1. First pin, then fork(). This tests fork() special path when
503 * doing early CoW if the page is private.
505 if (pin_pages(&pin_args, area_dst, page_size)) {
506 uffd_test_skip("Possibly CONFIG_GUP_TEST missing "
509 uffd_unregister(uffd, area_dst, page_size);
513 if (pagemap_test_fork(uffd, with_event, false)) {
514 uffd_test_fail("Detected %s uffd-wp bit in early CoW of fork()",
515 with_event ? "missing" : "stall");
516 unpin_pages(&pin_args);
520 unpin_pages(&pin_args);
523 * 2. First fork(), then pin (in the child, where test_pin==true).
524 * This tests COR, aka, page unsharing on private memories.
526 if (pagemap_test_fork(uffd, with_event, true)) {
527 uffd_test_fail("Detected %s uffd-wp bit when RO pin",
528 with_event ? "missing" : "stall");
533 if (uffd_unregister(uffd, area_dst, page_size))
534 err("register failed");
538 static void uffd_wp_fork_pin_test(uffd_test_args_t *args)
540 uffd_wp_fork_pin_test_common(args, false);
543 static void uffd_wp_fork_pin_with_event_test(uffd_test_args_t *args)
545 uffd_wp_fork_pin_test_common(args, true);
548 static void check_memory_contents(char *p)
551 uint8_t expected_byte;
553 for (i = 0; i < nr_pages; ++i) {
554 expected_byte = ~((uint8_t)(i % ((uint8_t)-1)));
555 for (j = 0; j < page_size; j++) {
556 uint8_t v = *(uint8_t *)(p + (i * page_size) + j);
557 if (v != expected_byte)
558 err("unexpected page contents");
563 static void uffd_minor_test_common(bool test_collapse, bool test_wp)
568 struct uffd_args args = { 0 };
571 * NOTE: MADV_COLLAPSE is not yet compatible with WP, so testing
572 * both do not make much sense.
574 assert(!(test_collapse && test_wp));
576 if (uffd_register(uffd, area_dst_alias, nr_pages * page_size,
577 /* NOTE! MADV_COLLAPSE may not work with uffd-wp */
578 false, test_wp, true))
579 err("register failure");
582 * After registering with UFFD, populate the non-UFFD-registered side of
583 * the shared mapping. This should *not* trigger any UFFD minor faults.
585 for (p = 0; p < nr_pages; ++p)
586 memset(area_dst + (p * page_size), p % ((uint8_t)-1),
589 args.apply_wp = test_wp;
590 if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args))
591 err("uffd_poll_thread create");
594 * Read each of the pages back using the UFFD-registered mapping. We
595 * expect that the first time we touch a page, it will result in a minor
596 * fault. uffd_poll_thread will resolve the fault by bit-flipping the
597 * page's contents, and then issuing a CONTINUE ioctl.
599 check_memory_contents(area_dst_alias);
601 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
603 if (pthread_join(uffd_mon, NULL))
604 err("join() failed");
607 if (madvise(area_dst_alias, nr_pages * page_size,
609 /* It's fine to fail for this one... */
610 uffd_test_skip("MADV_COLLAPSE failed");
614 uffd_test_ops->check_pmd_mapping(area_dst,
615 nr_pages * page_size /
616 read_pmd_pagesize());
618 * This won't cause uffd-fault - it purely just makes sure there
621 check_memory_contents(area_dst_alias);
624 if (args.missing_faults != 0 || args.minor_faults != nr_pages)
625 uffd_test_fail("stats check error");
630 void uffd_minor_test(uffd_test_args_t *args)
632 uffd_minor_test_common(false, false);
635 void uffd_minor_wp_test(uffd_test_args_t *args)
637 uffd_minor_test_common(false, true);
640 void uffd_minor_collapse_test(uffd_test_args_t *args)
642 uffd_minor_test_common(true, false);
645 static sigjmp_buf jbuf, *sigbuf;
647 static void sighndl(int sig, siginfo_t *siginfo, void *ptr)
651 siglongjmp(*sigbuf, 1);
657 * For non-cooperative userfaultfd test we fork() a process that will
658 * generate pagefaults, will mremap the area monitored by the
659 * userfaultfd and at last this process will release the monitored
661 * For the anonymous and shared memory the area is divided into two
662 * parts, the first part is accessed before mremap, and the second
663 * part is accessed after mremap. Since hugetlbfs does not support
664 * mremap, the entire monitored area is accessed in a single pass for
666 * The release of the pages currently generates event for shmem and
667 * anonymous memory (UFFD_EVENT_REMOVE), hence it is not checked
669 * For signal test(UFFD_FEATURE_SIGBUS), signal_test = 1, we register
670 * monitored area, generate pagefaults and test that signal is delivered.
671 * Use UFFDIO_COPY to allocate missing page and retry. For signal_test = 2
672 * test robustness use case - we release monitored area, fork a process
673 * that will generate pagefaults and verify signal is generated.
674 * This also tests UFFD_FEATURE_EVENT_FORK event along with the signal
675 * feature. Using monitor thread, verify no userfault events are generated.
677 static int faulting_process(int signal_test, bool wp)
680 unsigned long long count;
681 unsigned long split_nr_pages;
682 unsigned long lastnr;
683 struct sigaction act;
684 volatile unsigned long signalled = 0;
686 split_nr_pages = (nr_pages + 1) / 2;
690 memset(&act, 0, sizeof(act));
691 act.sa_sigaction = sighndl;
692 act.sa_flags = SA_SIGINFO;
693 if (sigaction(SIGBUS, &act, 0))
695 lastnr = (unsigned long)-1;
698 for (nr = 0; nr < split_nr_pages; nr++) {
699 volatile int steps = 1;
700 unsigned long offset = nr * page_size;
703 if (sigsetjmp(*sigbuf, 1) != 0) {
704 if (steps == 1 && nr == lastnr)
705 err("Signal repeated");
708 if (signal_test == 1) {
710 /* This is a MISSING request */
712 if (copy_page(uffd, offset, wp))
715 /* This is a WP request */
729 count = *area_count(area_dst, nr);
730 if (count != count_verify[nr])
731 err("nr %lu memory corruption %llu %llu\n",
732 nr, count, count_verify[nr]);
734 * Trigger write protection if there is by writing
735 * the same value back.
737 *area_count(area_dst, nr) = count;
741 return signalled != split_nr_pages;
743 area_dst = mremap(area_dst, nr_pages * page_size, nr_pages * page_size,
744 MREMAP_MAYMOVE | MREMAP_FIXED, area_src);
745 if (area_dst == MAP_FAILED)
747 /* Reset area_src since we just clobbered it */
750 for (; nr < nr_pages; nr++) {
751 count = *area_count(area_dst, nr);
752 if (count != count_verify[nr]) {
753 err("nr %lu memory corruption %llu %llu\n",
754 nr, count, count_verify[nr]);
757 * Trigger write protection if there is by writing
758 * the same value back.
760 *area_count(area_dst, nr) = count;
763 uffd_test_ops->release_pages(area_dst);
765 for (nr = 0; nr < nr_pages; nr++)
766 for (i = 0; i < page_size; i++)
767 if (*(area_dst + nr * page_size + i) != 0)
768 err("page %lu offset %lu is not zero", nr, i);
773 static void uffd_sigbus_test_common(bool wp)
775 unsigned long userfaults;
780 struct uffd_args args = { 0 };
782 ready_for_fork = false;
784 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
786 if (uffd_register(uffd, area_dst, nr_pages * page_size,
788 err("register failure");
790 if (faulting_process(1, wp))
791 err("faulting process failed");
793 uffd_test_ops->release_pages(area_dst);
796 if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args))
797 err("uffd_poll_thread create");
799 while (!ready_for_fork)
800 ; /* Wait for the poll_thread to start executing before forking */
807 exit(faulting_process(2, wp));
809 waitpid(pid, &err, 0);
811 err("faulting process failed");
812 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
814 if (pthread_join(uffd_mon, (void **)&userfaults))
815 err("pthread_join()");
818 uffd_test_fail("Signal test failed, userfaults: %ld", userfaults);
823 static void uffd_sigbus_test(uffd_test_args_t *args)
825 uffd_sigbus_test_common(false);
828 static void uffd_sigbus_wp_test(uffd_test_args_t *args)
830 uffd_sigbus_test_common(true);
833 static void uffd_events_test_common(bool wp)
839 struct uffd_args args = { 0 };
841 ready_for_fork = false;
843 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
844 if (uffd_register(uffd, area_dst, nr_pages * page_size,
846 err("register failure");
849 if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args))
850 err("uffd_poll_thread create");
852 while (!ready_for_fork)
853 ; /* Wait for the poll_thread to start executing before forking */
860 exit(faulting_process(0, wp));
862 waitpid(pid, &err, 0);
864 err("faulting process failed");
865 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
867 if (pthread_join(uffd_mon, NULL))
868 err("pthread_join()");
870 if (args.missing_faults != nr_pages)
871 uffd_test_fail("Fault counts wrong");
876 static void uffd_events_test(uffd_test_args_t *args)
878 uffd_events_test_common(false);
881 static void uffd_events_wp_test(uffd_test_args_t *args)
883 uffd_events_test_common(true);
886 static void retry_uffdio_zeropage(int ufd,
887 struct uffdio_zeropage *uffdio_zeropage)
889 uffd_test_ops->alias_mapping(&uffdio_zeropage->range.start,
890 uffdio_zeropage->range.len,
892 if (ioctl(ufd, UFFDIO_ZEROPAGE, uffdio_zeropage)) {
893 if (uffdio_zeropage->zeropage != -EEXIST)
894 err("UFFDIO_ZEROPAGE error: %"PRId64,
895 (int64_t)uffdio_zeropage->zeropage);
897 err("UFFDIO_ZEROPAGE error: %"PRId64,
898 (int64_t)uffdio_zeropage->zeropage);
902 static bool do_uffdio_zeropage(int ufd, bool has_zeropage)
904 struct uffdio_zeropage uffdio_zeropage = { 0 };
908 uffdio_zeropage.range.start = (unsigned long) area_dst;
909 uffdio_zeropage.range.len = page_size;
910 uffdio_zeropage.mode = 0;
911 ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage);
912 res = uffdio_zeropage.zeropage;
914 /* real retval in ufdio_zeropage.zeropage */
916 err("UFFDIO_ZEROPAGE error: %"PRId64, (int64_t)res);
917 else if (res != -EINVAL)
918 err("UFFDIO_ZEROPAGE not -EINVAL");
919 } else if (has_zeropage) {
920 if (res != page_size)
921 err("UFFDIO_ZEROPAGE unexpected size");
923 retry_uffdio_zeropage(ufd, &uffdio_zeropage);
926 err("UFFDIO_ZEROPAGE succeeded");
932 * Registers a range with MISSING mode only for zeropage test. Return true
933 * if UFFDIO_ZEROPAGE supported, false otherwise. Can't use uffd_register()
934 * because we want to detect .ioctls along the way.
937 uffd_register_detect_zeropage(int uffd, void *addr, uint64_t len)
941 if (uffd_register_with_ioctls(uffd, addr, len, true,
942 false, false, &ioctls))
943 err("zeropage register fail");
945 return ioctls & (1 << _UFFDIO_ZEROPAGE);
948 /* exercise UFFDIO_ZEROPAGE */
949 static void uffd_zeropage_test(uffd_test_args_t *args)
954 has_zeropage = uffd_register_detect_zeropage(uffd, area_dst, page_size);
956 /* Ignore the retval; we already have it */
957 uffd_register_detect_zeropage(uffd, area_dst_alias, page_size);
959 if (do_uffdio_zeropage(uffd, has_zeropage))
960 for (i = 0; i < page_size; i++)
961 if (area_dst[i] != 0)
962 err("data non-zero at offset %d\n", i);
964 if (uffd_unregister(uffd, area_dst, page_size))
967 if (area_dst_alias && uffd_unregister(uffd, area_dst_alias, page_size))
973 static void uffd_register_poison(int uffd, void *addr, uint64_t len)
976 uint64_t expected = (1 << _UFFDIO_COPY) | (1 << _UFFDIO_POISON);
978 if (uffd_register_with_ioctls(uffd, addr, len, true,
979 false, false, &ioctls))
980 err("poison register fail");
982 if ((ioctls & expected) != expected)
983 err("registered area doesn't support COPY and POISON ioctls");
986 static void do_uffdio_poison(int uffd, unsigned long offset)
988 struct uffdio_poison uffdio_poison = { 0 };
992 uffdio_poison.range.start = (unsigned long) area_dst + offset;
993 uffdio_poison.range.len = page_size;
994 uffdio_poison.mode = 0;
995 ret = ioctl(uffd, UFFDIO_POISON, &uffdio_poison);
996 res = uffdio_poison.updated;
999 err("UFFDIO_POISON error: %"PRId64, (int64_t)res);
1000 else if (res != page_size)
1001 err("UFFDIO_POISON unexpected size: %"PRId64, (int64_t)res);
1004 static void uffd_poison_handle_fault(
1005 struct uffd_msg *msg, struct uffd_args *args)
1007 unsigned long offset;
1009 if (msg->event != UFFD_EVENT_PAGEFAULT)
1010 err("unexpected msg event %u", msg->event);
1012 if (msg->arg.pagefault.flags &
1013 (UFFD_PAGEFAULT_FLAG_WP | UFFD_PAGEFAULT_FLAG_MINOR))
1014 err("unexpected fault type %llu", msg->arg.pagefault.flags);
1016 offset = (char *)(unsigned long)msg->arg.pagefault.address - area_dst;
1017 offset &= ~(page_size-1);
1019 /* Odd pages -> copy zeroed page; even pages -> poison. */
1020 if (offset & page_size)
1021 copy_page(uffd, offset, false);
1023 do_uffdio_poison(uffd, offset);
1026 static void uffd_poison_test(uffd_test_args_t *targs)
1030 struct uffd_args args = { 0 };
1031 struct sigaction act = { 0 };
1032 unsigned long nr_sigbus = 0;
1035 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
1037 uffd_register_poison(uffd, area_dst, nr_pages * page_size);
1038 memset(area_src, 0, nr_pages * page_size);
1040 args.handle_fault = uffd_poison_handle_fault;
1041 if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args))
1042 err("uffd_poll_thread create");
1045 act.sa_sigaction = sighndl;
1046 act.sa_flags = SA_SIGINFO;
1047 if (sigaction(SIGBUS, &act, 0))
1050 for (nr = 0; nr < nr_pages; ++nr) {
1051 unsigned long offset = nr * page_size;
1052 const char *bytes = (const char *) area_dst + offset;
1055 if (sigsetjmp(*sigbuf, 1)) {
1057 * Access below triggered a SIGBUS, which was caught by
1058 * sighndl, which then jumped here. Count this SIGBUS,
1059 * and move on to next page.
1065 for (i = bytes; i < bytes + page_size; ++i) {
1067 err("nonzero byte in area_dst (%p) at %p: %u",
1072 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
1074 if (pthread_join(uffd_mon, NULL))
1075 err("pthread_join()");
1077 if (nr_sigbus != nr_pages / 2)
1078 err("expected to receive %lu SIGBUS, actually received %lu",
1079 nr_pages / 2, nr_sigbus);
1085 uffd_move_handle_fault_common(struct uffd_msg *msg, struct uffd_args *args,
1088 unsigned long offset;
1090 if (msg->event != UFFD_EVENT_PAGEFAULT)
1091 err("unexpected msg event %u", msg->event);
1093 if (msg->arg.pagefault.flags &
1094 (UFFD_PAGEFAULT_FLAG_WP | UFFD_PAGEFAULT_FLAG_MINOR | UFFD_PAGEFAULT_FLAG_WRITE))
1095 err("unexpected fault type %llu", msg->arg.pagefault.flags);
1097 offset = (char *)(unsigned long)msg->arg.pagefault.address - area_dst;
1100 if (move_page(uffd, offset, len))
1101 args->missing_faults++;
1104 static void uffd_move_handle_fault(struct uffd_msg *msg,
1105 struct uffd_args *args)
1107 uffd_move_handle_fault_common(msg, args, page_size);
1110 static void uffd_move_pmd_handle_fault(struct uffd_msg *msg,
1111 struct uffd_args *args)
1113 uffd_move_handle_fault_common(msg, args, read_pmd_pagesize());
1117 uffd_move_test_common(uffd_test_args_t *targs, unsigned long chunk_size,
1118 void (*handle_fault)(struct uffd_msg *msg, struct uffd_args *args))
1123 unsigned long long count;
1124 struct uffd_args args = { 0 };
1125 char *orig_area_src, *orig_area_dst;
1126 unsigned long step_size, step_count;
1127 unsigned long src_offs = 0;
1128 unsigned long dst_offs = 0;
1130 /* Prevent source pages from being mapped more than once */
1131 if (madvise(area_src, nr_pages * page_size, MADV_DONTFORK))
1132 err("madvise(MADV_DONTFORK) failure");
1134 if (uffd_register(uffd, area_dst, nr_pages * page_size,
1135 true, false, false))
1136 err("register failure");
1138 args.handle_fault = handle_fault;
1139 if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args))
1140 err("uffd_poll_thread create");
1142 step_size = chunk_size / page_size;
1143 step_count = nr_pages / step_size;
1145 if (chunk_size > page_size) {
1146 char *aligned_src = ALIGN_UP(area_src, chunk_size);
1147 char *aligned_dst = ALIGN_UP(area_dst, chunk_size);
1149 if (aligned_src != area_src || aligned_dst != area_dst) {
1150 src_offs = (aligned_src - area_src) / page_size;
1151 dst_offs = (aligned_dst - area_dst) / page_size;
1154 orig_area_src = area_src;
1155 orig_area_dst = area_dst;
1156 area_src = aligned_src;
1157 area_dst = aligned_dst;
1161 * Read each of the pages back using the UFFD-registered mapping. We
1162 * expect that the first time we touch a page, it will result in a missing
1163 * fault. uffd_poll_thread will resolve the fault by moving source
1164 * page to destination.
1166 for (nr = 0; nr < step_count * step_size; nr += step_size) {
1169 /* Check area_src content */
1170 for (i = 0; i < step_size; i++) {
1171 count = *area_count(area_src, nr + i);
1172 if (count != count_verify[src_offs + nr + i])
1173 err("nr %lu source memory invalid %llu %llu\n",
1174 nr + i, count, count_verify[src_offs + nr + i]);
1177 /* Faulting into area_dst should move the page or the huge page */
1178 for (i = 0; i < step_size; i++) {
1179 count = *area_count(area_dst, nr + i);
1180 if (count != count_verify[dst_offs + nr + i])
1181 err("nr %lu memory corruption %llu %llu\n",
1182 nr, count, count_verify[dst_offs + nr + i]);
1185 /* Re-check area_src content which should be empty */
1186 for (i = 0; i < step_size; i++) {
1187 count = *area_count(area_src, nr + i);
1189 err("nr %lu move failed %llu %llu\n",
1190 nr, count, count_verify[src_offs + nr + i]);
1193 if (step_size > page_size) {
1194 area_src = orig_area_src;
1195 area_dst = orig_area_dst;
1198 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
1200 if (pthread_join(uffd_mon, NULL))
1201 err("join() failed");
1203 if (args.missing_faults != step_count || args.minor_faults != 0)
1204 uffd_test_fail("stats check error");
1209 static void uffd_move_test(uffd_test_args_t *targs)
1211 uffd_move_test_common(targs, page_size, uffd_move_handle_fault);
1214 static void uffd_move_pmd_test(uffd_test_args_t *targs)
1216 if (madvise(area_dst, nr_pages * page_size, MADV_HUGEPAGE))
1217 err("madvise(MADV_HUGEPAGE) failure");
1218 uffd_move_test_common(targs, read_pmd_pagesize(),
1219 uffd_move_pmd_handle_fault);
1222 static void uffd_move_pmd_split_test(uffd_test_args_t *targs)
1224 if (madvise(area_dst, nr_pages * page_size, MADV_NOHUGEPAGE))
1225 err("madvise(MADV_NOHUGEPAGE) failure");
1226 uffd_move_test_common(targs, read_pmd_pagesize(),
1227 uffd_move_pmd_handle_fault);
1230 static int prevent_hugepages(const char **errmsg)
1232 /* This should be done before source area is populated */
1233 if (madvise(area_src, nr_pages * page_size, MADV_NOHUGEPAGE)) {
1234 /* Ignore only if CONFIG_TRANSPARENT_HUGEPAGE=n */
1235 if (errno != EINVAL) {
1237 *errmsg = "madvise(MADV_NOHUGEPAGE) failed";
1244 static int request_hugepages(const char **errmsg)
1246 /* This should be done before source area is populated */
1247 if (madvise(area_src, nr_pages * page_size, MADV_HUGEPAGE)) {
1249 *errmsg = (errno == EINVAL) ?
1250 "CONFIG_TRANSPARENT_HUGEPAGE is not set" :
1251 "madvise(MADV_HUGEPAGE) failed";
1258 struct uffd_test_case_ops uffd_move_test_case_ops = {
1259 .post_alloc = prevent_hugepages,
1262 struct uffd_test_case_ops uffd_move_test_pmd_case_ops = {
1263 .post_alloc = request_hugepages,
1267 * Test the returned uffdio_register.ioctls with different register modes.
1268 * Note that _UFFDIO_ZEROPAGE is tested separately in the zeropage test.
1271 do_register_ioctls_test(uffd_test_args_t *args, bool miss, bool wp, bool minor)
1273 uint64_t ioctls = 0, expected = BIT_ULL(_UFFDIO_WAKE);
1274 mem_type_t *mem_type = args->mem_type;
1277 ret = uffd_register_with_ioctls(uffd, area_dst, page_size,
1278 miss, wp, minor, &ioctls);
1281 * Handle special cases of UFFDIO_REGISTER here where it should
1282 * just fail with -EINVAL first..
1284 * Case 1: register MINOR on anon
1285 * Case 2: register with no mode selected
1287 if ((minor && (mem_type->mem_flag == MEM_ANON)) ||
1288 (!miss && !wp && !minor)) {
1290 err("register (miss=%d, wp=%d, minor=%d) failed "
1291 "with wrong errno=%d", miss, wp, minor, ret);
1295 /* UFFDIO_REGISTER should succeed, then check ioctls returned */
1297 expected |= BIT_ULL(_UFFDIO_COPY);
1299 expected |= BIT_ULL(_UFFDIO_WRITEPROTECT);
1301 expected |= BIT_ULL(_UFFDIO_CONTINUE);
1303 if ((ioctls & expected) != expected)
1304 err("unexpected uffdio_register.ioctls "
1305 "(miss=%d, wp=%d, minor=%d): expected=0x%"PRIx64", "
1306 "returned=0x%"PRIx64, miss, wp, minor, expected, ioctls);
1308 if (uffd_unregister(uffd, area_dst, page_size))
1312 static void uffd_register_ioctls_test(uffd_test_args_t *args)
1314 int miss, wp, minor;
1316 for (miss = 0; miss <= 1; miss++)
1317 for (wp = 0; wp <= 1; wp++)
1318 for (minor = 0; minor <= 1; minor++)
1319 do_register_ioctls_test(args, miss, wp, minor);
1324 uffd_test_case_t uffd_tests[] = {
1326 /* Test returned uffdio_register.ioctls. */
1327 .name = "register-ioctls",
1328 .uffd_fn = uffd_register_ioctls_test,
1329 .mem_targets = MEM_ALL,
1330 .uffd_feature_required = UFFD_FEATURE_MISSING_HUGETLBFS |
1331 UFFD_FEATURE_MISSING_SHMEM |
1332 UFFD_FEATURE_PAGEFAULT_FLAG_WP |
1333 UFFD_FEATURE_WP_HUGETLBFS_SHMEM |
1334 UFFD_FEATURE_MINOR_HUGETLBFS |
1335 UFFD_FEATURE_MINOR_SHMEM,
1339 .uffd_fn = uffd_zeropage_test,
1340 .mem_targets = MEM_ALL,
1341 .uffd_feature_required = 0,
1345 .uffd_fn = uffd_move_test,
1346 .mem_targets = MEM_ANON,
1347 .uffd_feature_required = UFFD_FEATURE_MOVE,
1348 .test_case_ops = &uffd_move_test_case_ops,
1352 .uffd_fn = uffd_move_pmd_test,
1353 .mem_targets = MEM_ANON,
1354 .uffd_feature_required = UFFD_FEATURE_MOVE,
1355 .test_case_ops = &uffd_move_test_pmd_case_ops,
1358 .name = "move-pmd-split",
1359 .uffd_fn = uffd_move_pmd_split_test,
1360 .mem_targets = MEM_ANON,
1361 .uffd_feature_required = UFFD_FEATURE_MOVE,
1362 .test_case_ops = &uffd_move_test_pmd_case_ops,
1366 .uffd_fn = uffd_wp_fork_test,
1367 .mem_targets = MEM_ALL,
1368 .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP |
1369 UFFD_FEATURE_WP_HUGETLBFS_SHMEM,
1372 .name = "wp-fork-with-event",
1373 .uffd_fn = uffd_wp_fork_with_event_test,
1374 .mem_targets = MEM_ALL,
1375 .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP |
1376 UFFD_FEATURE_WP_HUGETLBFS_SHMEM |
1377 /* when set, child process should inherit uffd-wp bits */
1378 UFFD_FEATURE_EVENT_FORK,
1381 .name = "wp-fork-pin",
1382 .uffd_fn = uffd_wp_fork_pin_test,
1383 .mem_targets = MEM_ALL,
1384 .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP |
1385 UFFD_FEATURE_WP_HUGETLBFS_SHMEM,
1388 .name = "wp-fork-pin-with-event",
1389 .uffd_fn = uffd_wp_fork_pin_with_event_test,
1390 .mem_targets = MEM_ALL,
1391 .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP |
1392 UFFD_FEATURE_WP_HUGETLBFS_SHMEM |
1393 /* when set, child process should inherit uffd-wp bits */
1394 UFFD_FEATURE_EVENT_FORK,
1397 .name = "wp-unpopulated",
1398 .uffd_fn = uffd_wp_unpopulated_test,
1399 .mem_targets = MEM_ANON,
1400 .uffd_feature_required =
1401 UFFD_FEATURE_PAGEFAULT_FLAG_WP | UFFD_FEATURE_WP_UNPOPULATED,
1405 .uffd_fn = uffd_minor_test,
1406 .mem_targets = MEM_SHMEM | MEM_HUGETLB,
1407 .uffd_feature_required =
1408 UFFD_FEATURE_MINOR_HUGETLBFS | UFFD_FEATURE_MINOR_SHMEM,
1412 .uffd_fn = uffd_minor_wp_test,
1413 .mem_targets = MEM_SHMEM | MEM_HUGETLB,
1414 .uffd_feature_required =
1415 UFFD_FEATURE_MINOR_HUGETLBFS | UFFD_FEATURE_MINOR_SHMEM |
1416 UFFD_FEATURE_PAGEFAULT_FLAG_WP |
1418 * HACK: here we leveraged WP_UNPOPULATED to detect whether
1419 * minor mode supports wr-protect. There's no feature flag
1420 * for it so this is the best we can test against.
1422 UFFD_FEATURE_WP_UNPOPULATED,
1425 .name = "minor-collapse",
1426 .uffd_fn = uffd_minor_collapse_test,
1427 /* MADV_COLLAPSE only works with shmem */
1428 .mem_targets = MEM_SHMEM,
1429 /* We can't test MADV_COLLAPSE, so try our luck */
1430 .uffd_feature_required = UFFD_FEATURE_MINOR_SHMEM,
1434 .uffd_fn = uffd_sigbus_test,
1435 .mem_targets = MEM_ALL,
1436 .uffd_feature_required = UFFD_FEATURE_SIGBUS |
1437 UFFD_FEATURE_EVENT_FORK,
1440 .name = "sigbus-wp",
1441 .uffd_fn = uffd_sigbus_wp_test,
1442 .mem_targets = MEM_ALL,
1443 .uffd_feature_required = UFFD_FEATURE_SIGBUS |
1444 UFFD_FEATURE_EVENT_FORK | UFFD_FEATURE_PAGEFAULT_FLAG_WP |
1445 UFFD_FEATURE_WP_HUGETLBFS_SHMEM,
1449 .uffd_fn = uffd_events_test,
1450 .mem_targets = MEM_ALL,
1451 .uffd_feature_required = UFFD_FEATURE_EVENT_FORK |
1452 UFFD_FEATURE_EVENT_REMAP | UFFD_FEATURE_EVENT_REMOVE,
1455 .name = "events-wp",
1456 .uffd_fn = uffd_events_wp_test,
1457 .mem_targets = MEM_ALL,
1458 .uffd_feature_required = UFFD_FEATURE_EVENT_FORK |
1459 UFFD_FEATURE_EVENT_REMAP | UFFD_FEATURE_EVENT_REMOVE |
1460 UFFD_FEATURE_PAGEFAULT_FLAG_WP |
1461 UFFD_FEATURE_WP_HUGETLBFS_SHMEM,
1465 .uffd_fn = uffd_poison_test,
1466 .mem_targets = MEM_ALL,
1467 .uffd_feature_required = UFFD_FEATURE_POISON,
1471 static void usage(const char *prog)
1473 printf("usage: %s [-f TESTNAME]\n", prog);
1475 puts(" -f: test name to filter (e.g., event)");
1476 puts(" -h: show the help msg");
1477 puts(" -l: list tests only");
1482 int main(int argc, char *argv[])
1484 int n_tests = sizeof(uffd_tests) / sizeof(uffd_test_case_t);
1485 int n_mems = sizeof(mem_types) / sizeof(mem_type_t);
1486 const char *test_filter = NULL;
1487 bool list_only = false;
1488 uffd_test_case_t *test;
1489 mem_type_t *mem_type;
1490 uffd_test_args_t args;
1495 while ((opt = getopt(argc, argv, "f:hl")) != -1) {
1498 test_filter = optarg;
1511 if (!test_filter && !list_only) {
1512 has_uffd = test_uffd_api(false);
1513 has_uffd |= test_uffd_api(true);
1516 printf("Userfaultfd not supported or unprivileged, skip all tests\n");
1521 for (i = 0; i < n_tests; i++) {
1522 test = &uffd_tests[i];
1523 if (test_filter && !strstr(test->name, test_filter))
1526 printf("%s\n", test->name);
1529 for (j = 0; j < n_mems; j++) {
1530 mem_type = &mem_types[j];
1531 if (!(test->mem_targets & mem_type->mem_flag))
1534 uffd_test_start("%s on %s", test->name, mem_type->name);
1535 if ((mem_type->mem_flag == MEM_HUGETLB ||
1536 mem_type->mem_flag == MEM_HUGETLB_PRIVATE) &&
1537 (default_huge_page_size() == 0)) {
1538 uffd_test_skip("huge page size is 0, feature missing?");
1541 if (!uffd_feature_supported(test)) {
1542 uffd_test_skip("feature missing");
1545 if (uffd_setup_environment(&args, test, mem_type,
1547 uffd_test_skip(errmsg);
1550 test->uffd_fn(&args);
1551 uffd_test_ctx_clear();
1558 return ksft_get_fail_cnt() ? KSFT_FAIL : KSFT_PASS;