2 * Stress userfaultfd syscall.
4 * Copyright (C) 2015 Red Hat, Inc.
6 * This work is licensed under the terms of the GNU GPL, version 2. See
7 * the COPYING file in the top-level directory.
9 * This test allocates two virtual areas and bounces the physical
10 * memory across the two virtual areas (from area_src to area_dst)
13 * There are three threads running per CPU:
15 * 1) one per-CPU thread takes a per-page pthread_mutex in a random
16 * page of the area_dst (while the physical page may still be in
17 * area_src), and increments a per-page counter in the same page,
18 * and checks its value against a verification region.
20 * 2) another per-CPU thread handles the userfaults generated by
21 * thread 1 above. userfaultfd blocking reads or poll() modes are
22 * exercised interleaved.
24 * 3) one last per-CPU thread transfers the memory in the background
25 * at maximum bandwidth (if not already transferred by thread
26 * 2). Each cpu thread takes cares of transferring a portion of the
29 * When all threads of type 3 completed the transfer, one bounce is
30 * complete. area_src and area_dst are then swapped. All threads are
31 * respawned and so the bounce is immediately restarted in the
34 * per-CPU threads 1 by triggering userfaults inside
35 * pthread_mutex_lock will also verify the atomicity of the memory
36 * transfer (UFFDIO_COPY).
44 #include <sys/types.h>
52 #include <sys/syscall.h>
53 #include <sys/ioctl.h>
56 #include <linux/userfaultfd.h>
60 #include "../kselftest.h"
62 #ifdef __NR_userfaultfd
64 static unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;
66 #define BOUNCE_RANDOM (1<<0)
67 #define BOUNCE_RACINGFAULTS (1<<1)
68 #define BOUNCE_VERIFY (1<<2)
69 #define BOUNCE_POLL (1<<3)
73 #define TEST_HUGETLB 2
77 /* exercise the test_uffdio_*_eexist every ALARM_INTERVAL_SECS */
78 #define ALARM_INTERVAL_SECS 10
79 static volatile bool test_uffdio_copy_eexist = true;
80 static volatile bool test_uffdio_zeropage_eexist = true;
82 static bool map_shared;
84 static char *huge_fd_off0;
85 static unsigned long long *count_verify;
86 static int uffd, uffd_flags, finished, *pipefd;
87 static char *area_src, *area_src_alias, *area_dst, *area_dst_alias;
88 static char *zeropage;
91 /* pthread_mutex_t starts at page offset 0 */
92 #define area_mutex(___area, ___nr) \
93 ((pthread_mutex_t *) ((___area) + (___nr)*page_size))
95 * count is placed in the page after pthread_mutex_t naturally aligned
96 * to avoid non alignment faults on non-x86 archs.
98 #define area_count(___area, ___nr) \
99 ((volatile unsigned long long *) ((unsigned long) \
100 ((___area) + (___nr)*page_size + \
101 sizeof(pthread_mutex_t) + \
102 sizeof(unsigned long long) - 1) & \
103 ~(unsigned long)(sizeof(unsigned long long) \
106 const char *examples =
107 "# Run anonymous memory test on 100MiB region with 99999 bounces:\n"
108 "./userfaultfd anon 100 99999\n\n"
109 "# Run share memory test on 1GiB region with 99 bounces:\n"
110 "./userfaultfd shmem 1000 99\n\n"
111 "# Run hugetlb memory test on 256MiB region with 50 bounces (using /dev/hugepages/hugefile):\n"
112 "./userfaultfd hugetlb 256 50 /dev/hugepages/hugefile\n\n"
113 "# Run the same hugetlb test but using shmem:\n"
114 "./userfaultfd hugetlb_shared 256 50 /dev/hugepages/hugefile\n\n"
115 "# 10MiB-~6GiB 999 bounces anonymous test, "
116 "continue forever unless an error triggers\n"
117 "while ./userfaultfd anon $[RANDOM % 6000 + 10] 999; do true; done\n\n";
119 static void usage(void)
121 fprintf(stderr, "\nUsage: ./userfaultfd <test type> <MiB> <bounces> "
122 "[hugetlbfs_file]\n\n");
123 fprintf(stderr, "Supported <test type>: anon, hugetlb, "
124 "hugetlb_shared, shmem\n\n");
125 fprintf(stderr, "Examples:\n\n");
126 fprintf(stderr, examples);
130 static int anon_release_pages(char *rel_area)
134 if (madvise(rel_area, nr_pages * page_size, MADV_DONTNEED)) {
142 static void anon_allocate_area(void **alloc_area)
144 if (posix_memalign(alloc_area, page_size, nr_pages * page_size)) {
145 fprintf(stderr, "out of memory\n");
150 static void noop_alias_mapping(__u64 *start, size_t len, unsigned long offset)
155 static int hugetlb_release_pages(char *rel_area)
159 if (fallocate(huge_fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
160 rel_area == huge_fd_off0 ? 0 :
161 nr_pages * page_size,
162 nr_pages * page_size)) {
171 static void hugetlb_allocate_area(void **alloc_area)
173 void *area_alias = NULL;
174 char **alloc_area_alias;
175 *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
176 (map_shared ? MAP_SHARED : MAP_PRIVATE) |
178 huge_fd, *alloc_area == area_src ? 0 :
179 nr_pages * page_size);
180 if (*alloc_area == MAP_FAILED) {
181 fprintf(stderr, "mmap of hugetlbfs file failed\n");
186 area_alias = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
187 MAP_SHARED | MAP_HUGETLB,
188 huge_fd, *alloc_area == area_src ? 0 :
189 nr_pages * page_size);
190 if (area_alias == MAP_FAILED) {
191 if (munmap(*alloc_area, nr_pages * page_size) < 0)
192 perror("hugetlb munmap"), exit(1);
197 if (*alloc_area == area_src) {
198 huge_fd_off0 = *alloc_area;
199 alloc_area_alias = &area_src_alias;
201 alloc_area_alias = &area_dst_alias;
204 *alloc_area_alias = area_alias;
207 static void hugetlb_alias_mapping(__u64 *start, size_t len, unsigned long offset)
212 * We can't zap just the pagetable with hugetlbfs because
213 * MADV_DONTEED won't work. So exercise -EEXIST on a alias
214 * mapping where the pagetables are not established initially,
215 * this way we'll exercise the -EEXEC at the fs level.
217 *start = (unsigned long) area_dst_alias + offset;
221 static int shmem_release_pages(char *rel_area)
225 if (madvise(rel_area, nr_pages * page_size, MADV_REMOVE)) {
233 static void shmem_allocate_area(void **alloc_area)
235 *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
236 MAP_ANONYMOUS | MAP_SHARED, -1, 0);
237 if (*alloc_area == MAP_FAILED) {
238 fprintf(stderr, "shared memory mmap failed\n");
243 struct uffd_test_ops {
244 unsigned long expected_ioctls;
245 void (*allocate_area)(void **alloc_area);
246 int (*release_pages)(char *rel_area);
247 void (*alias_mapping)(__u64 *start, size_t len, unsigned long offset);
250 #define ANON_EXPECTED_IOCTLS ((1 << _UFFDIO_WAKE) | \
251 (1 << _UFFDIO_COPY) | \
252 (1 << _UFFDIO_ZEROPAGE))
254 static struct uffd_test_ops anon_uffd_test_ops = {
255 .expected_ioctls = ANON_EXPECTED_IOCTLS,
256 .allocate_area = anon_allocate_area,
257 .release_pages = anon_release_pages,
258 .alias_mapping = noop_alias_mapping,
261 static struct uffd_test_ops shmem_uffd_test_ops = {
262 .expected_ioctls = ANON_EXPECTED_IOCTLS,
263 .allocate_area = shmem_allocate_area,
264 .release_pages = shmem_release_pages,
265 .alias_mapping = noop_alias_mapping,
268 static struct uffd_test_ops hugetlb_uffd_test_ops = {
269 .expected_ioctls = UFFD_API_RANGE_IOCTLS_BASIC,
270 .allocate_area = hugetlb_allocate_area,
271 .release_pages = hugetlb_release_pages,
272 .alias_mapping = hugetlb_alias_mapping,
275 static struct uffd_test_ops *uffd_test_ops;
277 static int my_bcmp(char *str1, char *str2, size_t n)
280 for (i = 0; i < n; i++)
281 if (str1[i] != str2[i])
286 static void *locking_thread(void *arg)
288 unsigned long cpu = (unsigned long) arg;
289 struct random_data rand;
290 unsigned long page_nr = *(&(page_nr)); /* uninitialized warning */
292 unsigned long long count;
297 if (bounces & BOUNCE_RANDOM) {
298 seed = (unsigned int) time(NULL) - bounces;
299 if (!(bounces & BOUNCE_RACINGFAULTS))
301 bzero(&rand, sizeof(rand));
302 bzero(&randstate, sizeof(randstate));
303 if (initstate_r(seed, randstate, sizeof(randstate), &rand))
304 fprintf(stderr, "srandom_r error\n"), exit(1);
307 if (!(bounces & BOUNCE_RACINGFAULTS))
308 page_nr += cpu * nr_pages_per_cpu;
312 if (bounces & BOUNCE_RANDOM) {
313 if (random_r(&rand, &rand_nr))
314 fprintf(stderr, "random_r 1 error\n"), exit(1);
316 if (sizeof(page_nr) > sizeof(rand_nr)) {
317 if (random_r(&rand, &rand_nr))
318 fprintf(stderr, "random_r 2 error\n"), exit(1);
319 page_nr |= (((unsigned long) rand_nr) << 16) <<
327 if (bounces & BOUNCE_VERIFY) {
328 count = *area_count(area_dst, page_nr);
331 "page_nr %lu wrong count %Lu %Lu\n",
333 count_verify[page_nr]), exit(1);
337 * We can't use bcmp (or memcmp) because that
338 * returns 0 erroneously if the memory is
339 * changing under it (even if the end of the
340 * page is never changing and always
344 if (!my_bcmp(area_dst + page_nr * page_size, zeropage,
347 "my_bcmp page_nr %lu wrong count %Lu %Lu\n",
349 count_verify[page_nr]), exit(1);
354 /* uncomment the below line to test with mutex */
355 /* pthread_mutex_lock(area_mutex(area_dst, page_nr)); */
356 while (!bcmp(area_dst + page_nr * page_size, zeropage,
362 /* uncomment below line to test with mutex */
363 /* pthread_mutex_unlock(area_mutex(area_dst, page_nr)); */
366 "page_nr %lu all zero thread %lu %p %lu\n",
367 page_nr, cpu, area_dst + page_nr * page_size,
375 pthread_mutex_lock(area_mutex(area_dst, page_nr));
376 count = *area_count(area_dst, page_nr);
377 if (count != count_verify[page_nr]) {
379 "page_nr %lu memory corruption %Lu %Lu\n",
381 count_verify[page_nr]), exit(1);
384 *area_count(area_dst, page_nr) = count_verify[page_nr] = count;
385 pthread_mutex_unlock(area_mutex(area_dst, page_nr));
387 if (time(NULL) - start > 1)
389 "userfault too slow %ld "
390 "possible false positive with overcommit\n",
397 static void retry_copy_page(int ufd, struct uffdio_copy *uffdio_copy,
398 unsigned long offset)
400 uffd_test_ops->alias_mapping(&uffdio_copy->dst,
403 if (ioctl(ufd, UFFDIO_COPY, uffdio_copy)) {
404 /* real retval in ufdio_copy.copy */
405 if (uffdio_copy->copy != -EEXIST)
406 fprintf(stderr, "UFFDIO_COPY retry error %Ld\n",
407 uffdio_copy->copy), exit(1);
409 fprintf(stderr, "UFFDIO_COPY retry unexpected %Ld\n",
410 uffdio_copy->copy), exit(1);
414 static int __copy_page(int ufd, unsigned long offset, bool retry)
416 struct uffdio_copy uffdio_copy;
418 if (offset >= nr_pages * page_size)
419 fprintf(stderr, "unexpected offset %lu\n",
421 uffdio_copy.dst = (unsigned long) area_dst + offset;
422 uffdio_copy.src = (unsigned long) area_src + offset;
423 uffdio_copy.len = page_size;
424 uffdio_copy.mode = 0;
425 uffdio_copy.copy = 0;
426 if (ioctl(ufd, UFFDIO_COPY, &uffdio_copy)) {
427 /* real retval in ufdio_copy.copy */
428 if (uffdio_copy.copy != -EEXIST)
429 fprintf(stderr, "UFFDIO_COPY error %Ld\n",
430 uffdio_copy.copy), exit(1);
431 } else if (uffdio_copy.copy != page_size) {
432 fprintf(stderr, "UFFDIO_COPY unexpected copy %Ld\n",
433 uffdio_copy.copy), exit(1);
435 if (test_uffdio_copy_eexist && retry) {
436 test_uffdio_copy_eexist = false;
437 retry_copy_page(ufd, &uffdio_copy, offset);
444 static int copy_page_retry(int ufd, unsigned long offset)
446 return __copy_page(ufd, offset, true);
449 static int copy_page(int ufd, unsigned long offset)
451 return __copy_page(ufd, offset, false);
454 static void *uffd_poll_thread(void *arg)
456 unsigned long cpu = (unsigned long) arg;
457 struct pollfd pollfd[2];
459 struct uffdio_register uffd_reg;
461 unsigned long offset;
463 unsigned long userfaults = 0;
466 pollfd[0].events = POLLIN;
467 pollfd[1].fd = pipefd[cpu*2];
468 pollfd[1].events = POLLIN;
471 ret = poll(pollfd, 2, -1);
473 fprintf(stderr, "poll error %d\n", ret), exit(1);
475 perror("poll"), exit(1);
476 if (pollfd[1].revents & POLLIN) {
477 if (read(pollfd[1].fd, &tmp_chr, 1) != 1)
478 fprintf(stderr, "read pipefd error\n"),
482 if (!(pollfd[0].revents & POLLIN))
483 fprintf(stderr, "pollfd[0].revents %d\n",
484 pollfd[0].revents), exit(1);
485 ret = read(uffd, &msg, sizeof(msg));
489 perror("nonblocking read error"), exit(1);
493 fprintf(stderr, "unexpected msg event %u\n",
496 case UFFD_EVENT_PAGEFAULT:
497 if (msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
498 fprintf(stderr, "unexpected write fault\n"), exit(1);
499 offset = (char *)(unsigned long)msg.arg.pagefault.address -
501 offset &= ~(page_size-1);
502 if (copy_page(uffd, offset))
505 case UFFD_EVENT_FORK:
507 uffd = msg.arg.fork.ufd;
510 case UFFD_EVENT_REMOVE:
511 uffd_reg.range.start = msg.arg.remove.start;
512 uffd_reg.range.len = msg.arg.remove.end -
513 msg.arg.remove.start;
514 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffd_reg.range))
515 fprintf(stderr, "remove failure\n"), exit(1);
517 case UFFD_EVENT_REMAP:
518 area_dst = (char *)(unsigned long)msg.arg.remap.to;
522 return (void *)userfaults;
525 pthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER;
527 static void *uffd_read_thread(void *arg)
529 unsigned long *this_cpu_userfaults;
531 unsigned long offset;
534 this_cpu_userfaults = (unsigned long *) arg;
535 *this_cpu_userfaults = 0;
537 pthread_mutex_unlock(&uffd_read_mutex);
538 /* from here cancellation is ok */
541 ret = read(uffd, &msg, sizeof(msg));
542 if (ret != sizeof(msg)) {
544 perror("blocking read error"), exit(1);
546 fprintf(stderr, "short read\n"), exit(1);
548 if (msg.event != UFFD_EVENT_PAGEFAULT)
549 fprintf(stderr, "unexpected msg event %u\n",
551 if (bounces & BOUNCE_VERIFY &&
552 msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
553 fprintf(stderr, "unexpected write fault\n"), exit(1);
554 offset = (char *)(unsigned long)msg.arg.pagefault.address -
556 offset &= ~(page_size-1);
557 if (copy_page(uffd, offset))
558 (*this_cpu_userfaults)++;
563 static void *background_thread(void *arg)
565 unsigned long cpu = (unsigned long) arg;
566 unsigned long page_nr;
568 for (page_nr = cpu * nr_pages_per_cpu;
569 page_nr < (cpu+1) * nr_pages_per_cpu;
571 copy_page_retry(uffd, page_nr * page_size);
576 static int stress(unsigned long *userfaults)
579 pthread_t locking_threads[nr_cpus];
580 pthread_t uffd_threads[nr_cpus];
581 pthread_t background_threads[nr_cpus];
582 void **_userfaults = (void **) userfaults;
585 for (cpu = 0; cpu < nr_cpus; cpu++) {
586 if (pthread_create(&locking_threads[cpu], &attr,
587 locking_thread, (void *)cpu))
589 if (bounces & BOUNCE_POLL) {
590 if (pthread_create(&uffd_threads[cpu], &attr,
591 uffd_poll_thread, (void *)cpu))
594 if (pthread_create(&uffd_threads[cpu], &attr,
598 pthread_mutex_lock(&uffd_read_mutex);
600 if (pthread_create(&background_threads[cpu], &attr,
601 background_thread, (void *)cpu))
604 for (cpu = 0; cpu < nr_cpus; cpu++)
605 if (pthread_join(background_threads[cpu], NULL))
609 * Be strict and immediately zap area_src, the whole area has
610 * been transferred already by the background treads. The
611 * area_src could then be faulted in in a racy way by still
612 * running uffdio_threads reading zeropages after we zapped
613 * area_src (but they're guaranteed to get -EEXIST from
614 * UFFDIO_COPY without writing zero pages into area_dst
615 * because the background threads already completed).
617 if (uffd_test_ops->release_pages(area_src))
620 for (cpu = 0; cpu < nr_cpus; cpu++) {
622 if (bounces & BOUNCE_POLL) {
623 if (write(pipefd[cpu*2+1], &c, 1) != 1) {
624 fprintf(stderr, "pipefd write error\n");
627 if (pthread_join(uffd_threads[cpu], &_userfaults[cpu]))
630 if (pthread_cancel(uffd_threads[cpu]))
632 if (pthread_join(uffd_threads[cpu], NULL))
638 for (cpu = 0; cpu < nr_cpus; cpu++)
639 if (pthread_join(locking_threads[cpu], NULL))
645 static int userfaultfd_open(int features)
647 struct uffdio_api uffdio_api;
649 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
652 "userfaultfd syscall not available in this kernel\n");
655 uffd_flags = fcntl(uffd, F_GETFD, NULL);
657 uffdio_api.api = UFFD_API;
658 uffdio_api.features = features;
659 if (ioctl(uffd, UFFDIO_API, &uffdio_api)) {
660 fprintf(stderr, "UFFDIO_API\n");
663 if (uffdio_api.api != UFFD_API) {
664 fprintf(stderr, "UFFDIO_API error %Lu\n", uffdio_api.api);
671 sigjmp_buf jbuf, *sigbuf;
673 static void sighndl(int sig, siginfo_t *siginfo, void *ptr)
677 siglongjmp(*sigbuf, 1);
683 * For non-cooperative userfaultfd test we fork() a process that will
684 * generate pagefaults, will mremap the area monitored by the
685 * userfaultfd and at last this process will release the monitored
687 * For the anonymous and shared memory the area is divided into two
688 * parts, the first part is accessed before mremap, and the second
689 * part is accessed after mremap. Since hugetlbfs does not support
690 * mremap, the entire monitored area is accessed in a single pass for
692 * The release of the pages currently generates event for shmem and
693 * anonymous memory (UFFD_EVENT_REMOVE), hence it is not checked
695 * For signal test(UFFD_FEATURE_SIGBUS), signal_test = 1, we register
696 * monitored area, generate pagefaults and test that signal is delivered.
697 * Use UFFDIO_COPY to allocate missing page and retry. For signal_test = 2
698 * test robustness use case - we release monitored area, fork a process
699 * that will generate pagefaults and verify signal is generated.
700 * This also tests UFFD_FEATURE_EVENT_FORK event along with the signal
701 * feature. Using monitor thread, verify no userfault events are generated.
703 static int faulting_process(int signal_test)
706 unsigned long long count;
707 unsigned long split_nr_pages;
708 unsigned long lastnr;
709 struct sigaction act;
710 unsigned long signalled = 0;
712 if (test_type != TEST_HUGETLB)
713 split_nr_pages = (nr_pages + 1) / 2;
715 split_nr_pages = nr_pages;
719 memset(&act, 0, sizeof(act));
720 act.sa_sigaction = sighndl;
721 act.sa_flags = SA_SIGINFO;
722 if (sigaction(SIGBUS, &act, 0)) {
726 lastnr = (unsigned long)-1;
729 for (nr = 0; nr < split_nr_pages; nr++) {
731 if (sigsetjmp(*sigbuf, 1) != 0) {
733 fprintf(stderr, "Signal repeated\n");
738 if (signal_test == 1) {
739 if (copy_page(uffd, nr * page_size))
748 count = *area_count(area_dst, nr);
749 if (count != count_verify[nr]) {
751 "nr %lu memory corruption %Lu %Lu\n",
753 count_verify[nr]), exit(1);
758 return signalled != split_nr_pages;
760 if (test_type == TEST_HUGETLB)
763 area_dst = mremap(area_dst, nr_pages * page_size, nr_pages * page_size,
764 MREMAP_MAYMOVE | MREMAP_FIXED, area_src);
765 if (area_dst == MAP_FAILED)
766 perror("mremap"), exit(1);
768 for (; nr < nr_pages; nr++) {
769 count = *area_count(area_dst, nr);
770 if (count != count_verify[nr]) {
772 "nr %lu memory corruption %Lu %Lu\n",
774 count_verify[nr]), exit(1);
778 if (uffd_test_ops->release_pages(area_dst))
781 for (nr = 0; nr < nr_pages; nr++) {
782 if (my_bcmp(area_dst + nr * page_size, zeropage, page_size))
783 fprintf(stderr, "nr %lu is not zero\n", nr), exit(1);
789 static void retry_uffdio_zeropage(int ufd,
790 struct uffdio_zeropage *uffdio_zeropage,
791 unsigned long offset)
793 uffd_test_ops->alias_mapping(&uffdio_zeropage->range.start,
794 uffdio_zeropage->range.len,
796 if (ioctl(ufd, UFFDIO_ZEROPAGE, uffdio_zeropage)) {
797 if (uffdio_zeropage->zeropage != -EEXIST)
798 fprintf(stderr, "UFFDIO_ZEROPAGE retry error %Ld\n",
799 uffdio_zeropage->zeropage), exit(1);
801 fprintf(stderr, "UFFDIO_ZEROPAGE retry unexpected %Ld\n",
802 uffdio_zeropage->zeropage), exit(1);
806 static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
808 struct uffdio_zeropage uffdio_zeropage;
810 unsigned long has_zeropage;
812 has_zeropage = uffd_test_ops->expected_ioctls & (1 << _UFFDIO_ZEROPAGE);
814 if (offset >= nr_pages * page_size)
815 fprintf(stderr, "unexpected offset %lu\n",
817 uffdio_zeropage.range.start = (unsigned long) area_dst + offset;
818 uffdio_zeropage.range.len = page_size;
819 uffdio_zeropage.mode = 0;
820 ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage);
822 /* real retval in ufdio_zeropage.zeropage */
824 if (uffdio_zeropage.zeropage == -EEXIST)
825 fprintf(stderr, "UFFDIO_ZEROPAGE -EEXIST\n"),
828 fprintf(stderr, "UFFDIO_ZEROPAGE error %Ld\n",
829 uffdio_zeropage.zeropage), exit(1);
831 if (uffdio_zeropage.zeropage != -EINVAL)
833 "UFFDIO_ZEROPAGE not -EINVAL %Ld\n",
834 uffdio_zeropage.zeropage), exit(1);
836 } else if (has_zeropage) {
837 if (uffdio_zeropage.zeropage != page_size) {
838 fprintf(stderr, "UFFDIO_ZEROPAGE unexpected %Ld\n",
839 uffdio_zeropage.zeropage), exit(1);
841 if (test_uffdio_zeropage_eexist && retry) {
842 test_uffdio_zeropage_eexist = false;
843 retry_uffdio_zeropage(ufd, &uffdio_zeropage,
850 "UFFDIO_ZEROPAGE succeeded %Ld\n",
851 uffdio_zeropage.zeropage), exit(1);
857 static int uffdio_zeropage(int ufd, unsigned long offset)
859 return __uffdio_zeropage(ufd, offset, false);
862 /* exercise UFFDIO_ZEROPAGE */
863 static int userfaultfd_zeropage_test(void)
865 struct uffdio_register uffdio_register;
866 unsigned long expected_ioctls;
868 printf("testing UFFDIO_ZEROPAGE: ");
871 if (uffd_test_ops->release_pages(area_dst))
874 if (userfaultfd_open(0) < 0)
876 uffdio_register.range.start = (unsigned long) area_dst;
877 uffdio_register.range.len = nr_pages * page_size;
878 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
879 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
880 fprintf(stderr, "register failure\n"), exit(1);
882 expected_ioctls = uffd_test_ops->expected_ioctls;
883 if ((uffdio_register.ioctls & expected_ioctls) !=
886 "unexpected missing ioctl for anon memory\n"),
889 if (uffdio_zeropage(uffd, 0)) {
890 if (my_bcmp(area_dst, zeropage, page_size))
891 fprintf(stderr, "zeropage is not zero\n"), exit(1);
899 static int userfaultfd_events_test(void)
901 struct uffdio_register uffdio_register;
902 unsigned long expected_ioctls;
903 unsigned long userfaults;
909 printf("testing events (fork, remap, remove): ");
912 if (uffd_test_ops->release_pages(area_dst))
915 features = UFFD_FEATURE_EVENT_FORK | UFFD_FEATURE_EVENT_REMAP |
916 UFFD_FEATURE_EVENT_REMOVE;
917 if (userfaultfd_open(features) < 0)
919 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
921 uffdio_register.range.start = (unsigned long) area_dst;
922 uffdio_register.range.len = nr_pages * page_size;
923 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
924 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
925 fprintf(stderr, "register failure\n"), exit(1);
927 expected_ioctls = uffd_test_ops->expected_ioctls;
928 if ((uffdio_register.ioctls & expected_ioctls) !=
931 "unexpected missing ioctl for anon memory\n"),
934 if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
935 perror("uffd_poll_thread create"), exit(1);
939 perror("fork"), exit(1);
942 return faulting_process(0);
944 waitpid(pid, &err, 0);
946 fprintf(stderr, "faulting process failed\n"), exit(1);
948 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
949 perror("pipe write"), exit(1);
950 if (pthread_join(uffd_mon, (void **)&userfaults))
954 printf("userfaults: %ld\n", userfaults);
956 return userfaults != nr_pages;
959 static int userfaultfd_sig_test(void)
961 struct uffdio_register uffdio_register;
962 unsigned long expected_ioctls;
963 unsigned long userfaults;
969 printf("testing signal delivery: ");
972 if (uffd_test_ops->release_pages(area_dst))
975 features = UFFD_FEATURE_EVENT_FORK|UFFD_FEATURE_SIGBUS;
976 if (userfaultfd_open(features) < 0)
978 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
980 uffdio_register.range.start = (unsigned long) area_dst;
981 uffdio_register.range.len = nr_pages * page_size;
982 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
983 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
984 fprintf(stderr, "register failure\n"), exit(1);
986 expected_ioctls = uffd_test_ops->expected_ioctls;
987 if ((uffdio_register.ioctls & expected_ioctls) !=
990 "unexpected missing ioctl for anon memory\n"),
993 if (faulting_process(1))
994 fprintf(stderr, "faulting process failed\n"), exit(1);
996 if (uffd_test_ops->release_pages(area_dst))
999 if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
1000 perror("uffd_poll_thread create"), exit(1);
1004 perror("fork"), exit(1);
1007 exit(faulting_process(2));
1009 waitpid(pid, &err, 0);
1011 fprintf(stderr, "faulting process failed\n"), exit(1);
1013 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
1014 perror("pipe write"), exit(1);
1015 if (pthread_join(uffd_mon, (void **)&userfaults))
1020 fprintf(stderr, "Signal test failed, userfaults: %ld\n",
1023 return userfaults != 0;
1025 static int userfaultfd_stress(void)
1030 struct uffdio_register uffdio_register;
1033 unsigned long userfaults[nr_cpus];
1035 uffd_test_ops->allocate_area((void **)&area_src);
1038 uffd_test_ops->allocate_area((void **)&area_dst);
1042 if (userfaultfd_open(0) < 0)
1045 count_verify = malloc(nr_pages * sizeof(unsigned long long));
1046 if (!count_verify) {
1047 perror("count_verify");
1051 for (nr = 0; nr < nr_pages; nr++) {
1052 *area_mutex(area_src, nr) = (pthread_mutex_t)
1053 PTHREAD_MUTEX_INITIALIZER;
1054 count_verify[nr] = *area_count(area_src, nr) = 1;
1056 * In the transition between 255 to 256, powerpc will
1057 * read out of order in my_bcmp and see both bytes as
1058 * zero, so leave a placeholder below always non-zero
1059 * after the count, to avoid my_bcmp to trigger false
1062 *(area_count(area_src, nr) + 1) = 1;
1065 pipefd = malloc(sizeof(int) * nr_cpus * 2);
1070 for (cpu = 0; cpu < nr_cpus; cpu++) {
1071 if (pipe2(&pipefd[cpu*2], O_CLOEXEC | O_NONBLOCK)) {
1077 if (posix_memalign(&area, page_size, page_size)) {
1078 fprintf(stderr, "out of memory\n");
1082 bzero(zeropage, page_size);
1084 pthread_mutex_lock(&uffd_read_mutex);
1086 pthread_attr_init(&attr);
1087 pthread_attr_setstacksize(&attr, 16*1024*1024);
1091 unsigned long expected_ioctls;
1093 printf("bounces: %d, mode:", bounces);
1094 if (bounces & BOUNCE_RANDOM)
1096 if (bounces & BOUNCE_RACINGFAULTS)
1098 if (bounces & BOUNCE_VERIFY)
1100 if (bounces & BOUNCE_POLL)
1105 if (bounces & BOUNCE_POLL)
1106 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
1108 fcntl(uffd, F_SETFL, uffd_flags & ~O_NONBLOCK);
1111 uffdio_register.range.start = (unsigned long) area_dst;
1112 uffdio_register.range.len = nr_pages * page_size;
1113 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
1114 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
1115 fprintf(stderr, "register failure\n");
1118 expected_ioctls = uffd_test_ops->expected_ioctls;
1119 if ((uffdio_register.ioctls & expected_ioctls) !=
1122 "unexpected missing ioctl for anon memory\n");
1126 if (area_dst_alias) {
1127 uffdio_register.range.start = (unsigned long)
1129 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
1130 fprintf(stderr, "register failure alias\n");
1136 * The madvise done previously isn't enough: some
1137 * uffd_thread could have read userfaults (one of
1138 * those already resolved by the background thread)
1139 * and it may be in the process of calling
1140 * UFFDIO_COPY. UFFDIO_COPY will read the zapped
1141 * area_src and it would map a zero page in it (of
1142 * course such a UFFDIO_COPY is perfectly safe as it'd
1143 * return -EEXIST). The problem comes at the next
1144 * bounce though: that racing UFFDIO_COPY would
1145 * generate zeropages in the area_src, so invalidating
1146 * the previous MADV_DONTNEED. Without this additional
1147 * MADV_DONTNEED those zeropages leftovers in the
1148 * area_src would lead to -EEXIST failure during the
1149 * next bounce, effectively leaving a zeropage in the
1152 * Try to comment this out madvise to see the memory
1153 * corruption being caught pretty quick.
1155 * khugepaged is also inhibited to collapse THP after
1156 * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's
1157 * required to MADV_DONTNEED here.
1159 if (uffd_test_ops->release_pages(area_dst))
1163 if (stress(userfaults))
1167 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) {
1168 fprintf(stderr, "unregister failure\n");
1171 if (area_dst_alias) {
1172 uffdio_register.range.start = (unsigned long) area_dst;
1173 if (ioctl(uffd, UFFDIO_UNREGISTER,
1174 &uffdio_register.range)) {
1175 fprintf(stderr, "unregister failure alias\n");
1181 if (bounces & BOUNCE_VERIFY) {
1182 for (nr = 0; nr < nr_pages; nr++) {
1183 if (*area_count(area_dst, nr) != count_verify[nr]) {
1185 "error area_count %Lu %Lu %lu\n",
1186 *area_count(area_src, nr),
1195 /* prepare next bounce */
1196 tmp_area = area_src;
1197 area_src = area_dst;
1198 area_dst = tmp_area;
1200 tmp_area = area_src_alias;
1201 area_src_alias = area_dst_alias;
1202 area_dst_alias = tmp_area;
1204 printf("userfaults:");
1205 for (cpu = 0; cpu < nr_cpus; cpu++)
1206 printf(" %lu", userfaults[cpu]);
1214 return userfaultfd_zeropage_test() || userfaultfd_sig_test()
1215 || userfaultfd_events_test();
1219 * Copied from mlock2-tests.c
1221 unsigned long default_huge_page_size(void)
1223 unsigned long hps = 0;
1226 FILE *f = fopen("/proc/meminfo", "r");
1230 while (getline(&line, &linelen, f) > 0) {
1231 if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
1242 static void set_test_type(const char *type)
1244 if (!strcmp(type, "anon")) {
1245 test_type = TEST_ANON;
1246 uffd_test_ops = &anon_uffd_test_ops;
1247 } else if (!strcmp(type, "hugetlb")) {
1248 test_type = TEST_HUGETLB;
1249 uffd_test_ops = &hugetlb_uffd_test_ops;
1250 } else if (!strcmp(type, "hugetlb_shared")) {
1252 test_type = TEST_HUGETLB;
1253 uffd_test_ops = &hugetlb_uffd_test_ops;
1254 } else if (!strcmp(type, "shmem")) {
1256 test_type = TEST_SHMEM;
1257 uffd_test_ops = &shmem_uffd_test_ops;
1259 fprintf(stderr, "Unknown test type: %s\n", type), exit(1);
1262 if (test_type == TEST_HUGETLB)
1263 page_size = default_huge_page_size();
1265 page_size = sysconf(_SC_PAGE_SIZE);
1268 fprintf(stderr, "Unable to determine page size\n"),
1270 if ((unsigned long) area_count(NULL, 0) + sizeof(unsigned long long) * 2
1272 fprintf(stderr, "Impossible to run this test\n"), exit(2);
1275 static void sigalrm(int sig)
1279 test_uffdio_copy_eexist = true;
1280 test_uffdio_zeropage_eexist = true;
1281 alarm(ALARM_INTERVAL_SECS);
1284 int main(int argc, char **argv)
1289 if (signal(SIGALRM, sigalrm) == SIG_ERR)
1290 fprintf(stderr, "failed to arm SIGALRM"), exit(1);
1291 alarm(ALARM_INTERVAL_SECS);
1293 set_test_type(argv[1]);
1295 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1296 nr_pages_per_cpu = atol(argv[2]) * 1024*1024 / page_size /
1298 if (!nr_pages_per_cpu) {
1299 fprintf(stderr, "invalid MiB\n");
1303 bounces = atoi(argv[3]);
1305 fprintf(stderr, "invalid bounces\n");
1308 nr_pages = nr_pages_per_cpu * nr_cpus;
1310 if (test_type == TEST_HUGETLB) {
1313 huge_fd = open(argv[4], O_CREAT | O_RDWR, 0755);
1315 fprintf(stderr, "Open of %s failed", argv[3]);
1319 if (ftruncate(huge_fd, 0)) {
1320 fprintf(stderr, "ftruncate %s to size 0 failed", argv[3]);
1321 perror("ftruncate");
1325 printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
1326 nr_pages, nr_pages_per_cpu);
1327 return userfaultfd_stress();
1330 #else /* __NR_userfaultfd */
1332 #warning "missing __NR_userfaultfd definition"
1336 printf("skip: Skipping userfaultfd test (missing __NR_userfaultfd)\n");
1340 #endif /* __NR_userfaultfd */