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, "%s", 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 int uffd_read_msg(int ufd, struct uffd_msg *msg)
456 int ret = read(uffd, msg, sizeof(*msg));
458 if (ret != sizeof(*msg)) {
463 perror("blocking read error"), exit(1);
465 fprintf(stderr, "short read\n"), exit(1);
472 /* Return 1 if page fault handled by us; otherwise 0 */
473 static int uffd_handle_page_fault(struct uffd_msg *msg)
475 unsigned long offset;
477 if (msg->event != UFFD_EVENT_PAGEFAULT)
478 fprintf(stderr, "unexpected msg event %u\n",
479 msg->event), exit(1);
481 if (bounces & BOUNCE_VERIFY &&
482 msg->arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
483 fprintf(stderr, "unexpected write fault\n"), exit(1);
485 offset = (char *)(unsigned long)msg->arg.pagefault.address - area_dst;
486 offset &= ~(page_size-1);
488 return copy_page(uffd, offset);
491 static void *uffd_poll_thread(void *arg)
493 unsigned long cpu = (unsigned long) arg;
494 struct pollfd pollfd[2];
496 struct uffdio_register uffd_reg;
499 unsigned long userfaults = 0;
502 pollfd[0].events = POLLIN;
503 pollfd[1].fd = pipefd[cpu*2];
504 pollfd[1].events = POLLIN;
507 ret = poll(pollfd, 2, -1);
509 fprintf(stderr, "poll error %d\n", ret), exit(1);
511 perror("poll"), exit(1);
512 if (pollfd[1].revents & POLLIN) {
513 if (read(pollfd[1].fd, &tmp_chr, 1) != 1)
514 fprintf(stderr, "read pipefd error\n"),
518 if (!(pollfd[0].revents & POLLIN))
519 fprintf(stderr, "pollfd[0].revents %d\n",
520 pollfd[0].revents), exit(1);
521 if (uffd_read_msg(uffd, &msg))
525 fprintf(stderr, "unexpected msg event %u\n",
528 case UFFD_EVENT_PAGEFAULT:
529 userfaults += uffd_handle_page_fault(&msg);
531 case UFFD_EVENT_FORK:
533 uffd = msg.arg.fork.ufd;
536 case UFFD_EVENT_REMOVE:
537 uffd_reg.range.start = msg.arg.remove.start;
538 uffd_reg.range.len = msg.arg.remove.end -
539 msg.arg.remove.start;
540 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffd_reg.range))
541 fprintf(stderr, "remove failure\n"), exit(1);
543 case UFFD_EVENT_REMAP:
544 area_dst = (char *)(unsigned long)msg.arg.remap.to;
548 return (void *)userfaults;
551 pthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER;
553 static void *uffd_read_thread(void *arg)
555 unsigned long *this_cpu_userfaults;
558 this_cpu_userfaults = (unsigned long *) arg;
559 *this_cpu_userfaults = 0;
561 pthread_mutex_unlock(&uffd_read_mutex);
562 /* from here cancellation is ok */
565 if (uffd_read_msg(uffd, &msg))
567 (*this_cpu_userfaults) += uffd_handle_page_fault(&msg);
572 static void *background_thread(void *arg)
574 unsigned long cpu = (unsigned long) arg;
575 unsigned long page_nr;
577 for (page_nr = cpu * nr_pages_per_cpu;
578 page_nr < (cpu+1) * nr_pages_per_cpu;
580 copy_page_retry(uffd, page_nr * page_size);
585 static int stress(unsigned long *userfaults)
588 pthread_t locking_threads[nr_cpus];
589 pthread_t uffd_threads[nr_cpus];
590 pthread_t background_threads[nr_cpus];
591 void **_userfaults = (void **) userfaults;
594 for (cpu = 0; cpu < nr_cpus; cpu++) {
595 if (pthread_create(&locking_threads[cpu], &attr,
596 locking_thread, (void *)cpu))
598 if (bounces & BOUNCE_POLL) {
599 if (pthread_create(&uffd_threads[cpu], &attr,
600 uffd_poll_thread, (void *)cpu))
603 if (pthread_create(&uffd_threads[cpu], &attr,
607 pthread_mutex_lock(&uffd_read_mutex);
609 if (pthread_create(&background_threads[cpu], &attr,
610 background_thread, (void *)cpu))
613 for (cpu = 0; cpu < nr_cpus; cpu++)
614 if (pthread_join(background_threads[cpu], NULL))
618 * Be strict and immediately zap area_src, the whole area has
619 * been transferred already by the background treads. The
620 * area_src could then be faulted in in a racy way by still
621 * running uffdio_threads reading zeropages after we zapped
622 * area_src (but they're guaranteed to get -EEXIST from
623 * UFFDIO_COPY without writing zero pages into area_dst
624 * because the background threads already completed).
626 if (uffd_test_ops->release_pages(area_src))
631 for (cpu = 0; cpu < nr_cpus; cpu++)
632 if (pthread_join(locking_threads[cpu], NULL))
635 for (cpu = 0; cpu < nr_cpus; cpu++) {
637 if (bounces & BOUNCE_POLL) {
638 if (write(pipefd[cpu*2+1], &c, 1) != 1) {
639 fprintf(stderr, "pipefd write error\n");
642 if (pthread_join(uffd_threads[cpu], &_userfaults[cpu]))
645 if (pthread_cancel(uffd_threads[cpu]))
647 if (pthread_join(uffd_threads[cpu], NULL))
655 static int userfaultfd_open(int features)
657 struct uffdio_api uffdio_api;
659 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
662 "userfaultfd syscall not available in this kernel\n");
665 uffd_flags = fcntl(uffd, F_GETFD, NULL);
667 uffdio_api.api = UFFD_API;
668 uffdio_api.features = features;
669 if (ioctl(uffd, UFFDIO_API, &uffdio_api)) {
670 fprintf(stderr, "UFFDIO_API\n");
673 if (uffdio_api.api != UFFD_API) {
674 fprintf(stderr, "UFFDIO_API error %Lu\n", uffdio_api.api);
681 sigjmp_buf jbuf, *sigbuf;
683 static void sighndl(int sig, siginfo_t *siginfo, void *ptr)
687 siglongjmp(*sigbuf, 1);
693 * For non-cooperative userfaultfd test we fork() a process that will
694 * generate pagefaults, will mremap the area monitored by the
695 * userfaultfd and at last this process will release the monitored
697 * For the anonymous and shared memory the area is divided into two
698 * parts, the first part is accessed before mremap, and the second
699 * part is accessed after mremap. Since hugetlbfs does not support
700 * mremap, the entire monitored area is accessed in a single pass for
702 * The release of the pages currently generates event for shmem and
703 * anonymous memory (UFFD_EVENT_REMOVE), hence it is not checked
705 * For signal test(UFFD_FEATURE_SIGBUS), signal_test = 1, we register
706 * monitored area, generate pagefaults and test that signal is delivered.
707 * Use UFFDIO_COPY to allocate missing page and retry. For signal_test = 2
708 * test robustness use case - we release monitored area, fork a process
709 * that will generate pagefaults and verify signal is generated.
710 * This also tests UFFD_FEATURE_EVENT_FORK event along with the signal
711 * feature. Using monitor thread, verify no userfault events are generated.
713 static int faulting_process(int signal_test)
716 unsigned long long count;
717 unsigned long split_nr_pages;
718 unsigned long lastnr;
719 struct sigaction act;
720 unsigned long signalled = 0;
722 if (test_type != TEST_HUGETLB)
723 split_nr_pages = (nr_pages + 1) / 2;
725 split_nr_pages = nr_pages;
729 memset(&act, 0, sizeof(act));
730 act.sa_sigaction = sighndl;
731 act.sa_flags = SA_SIGINFO;
732 if (sigaction(SIGBUS, &act, 0)) {
736 lastnr = (unsigned long)-1;
739 for (nr = 0; nr < split_nr_pages; nr++) {
741 if (sigsetjmp(*sigbuf, 1) != 0) {
743 fprintf(stderr, "Signal repeated\n");
748 if (signal_test == 1) {
749 if (copy_page(uffd, nr * page_size))
758 count = *area_count(area_dst, nr);
759 if (count != count_verify[nr]) {
761 "nr %lu memory corruption %Lu %Lu\n",
763 count_verify[nr]), exit(1);
768 return signalled != split_nr_pages;
770 if (test_type == TEST_HUGETLB)
773 area_dst = mremap(area_dst, nr_pages * page_size, nr_pages * page_size,
774 MREMAP_MAYMOVE | MREMAP_FIXED, area_src);
775 if (area_dst == MAP_FAILED)
776 perror("mremap"), exit(1);
778 for (; nr < nr_pages; nr++) {
779 count = *area_count(area_dst, nr);
780 if (count != count_verify[nr]) {
782 "nr %lu memory corruption %Lu %Lu\n",
784 count_verify[nr]), exit(1);
788 if (uffd_test_ops->release_pages(area_dst))
791 for (nr = 0; nr < nr_pages; nr++) {
792 if (my_bcmp(area_dst + nr * page_size, zeropage, page_size))
793 fprintf(stderr, "nr %lu is not zero\n", nr), exit(1);
799 static void retry_uffdio_zeropage(int ufd,
800 struct uffdio_zeropage *uffdio_zeropage,
801 unsigned long offset)
803 uffd_test_ops->alias_mapping(&uffdio_zeropage->range.start,
804 uffdio_zeropage->range.len,
806 if (ioctl(ufd, UFFDIO_ZEROPAGE, uffdio_zeropage)) {
807 if (uffdio_zeropage->zeropage != -EEXIST)
808 fprintf(stderr, "UFFDIO_ZEROPAGE retry error %Ld\n",
809 uffdio_zeropage->zeropage), exit(1);
811 fprintf(stderr, "UFFDIO_ZEROPAGE retry unexpected %Ld\n",
812 uffdio_zeropage->zeropage), exit(1);
816 static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
818 struct uffdio_zeropage uffdio_zeropage;
820 unsigned long has_zeropage;
822 has_zeropage = uffd_test_ops->expected_ioctls & (1 << _UFFDIO_ZEROPAGE);
824 if (offset >= nr_pages * page_size)
825 fprintf(stderr, "unexpected offset %lu\n",
827 uffdio_zeropage.range.start = (unsigned long) area_dst + offset;
828 uffdio_zeropage.range.len = page_size;
829 uffdio_zeropage.mode = 0;
830 ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage);
832 /* real retval in ufdio_zeropage.zeropage */
834 if (uffdio_zeropage.zeropage == -EEXIST)
835 fprintf(stderr, "UFFDIO_ZEROPAGE -EEXIST\n"),
838 fprintf(stderr, "UFFDIO_ZEROPAGE error %Ld\n",
839 uffdio_zeropage.zeropage), exit(1);
841 if (uffdio_zeropage.zeropage != -EINVAL)
843 "UFFDIO_ZEROPAGE not -EINVAL %Ld\n",
844 uffdio_zeropage.zeropage), exit(1);
846 } else if (has_zeropage) {
847 if (uffdio_zeropage.zeropage != page_size) {
848 fprintf(stderr, "UFFDIO_ZEROPAGE unexpected %Ld\n",
849 uffdio_zeropage.zeropage), exit(1);
851 if (test_uffdio_zeropage_eexist && retry) {
852 test_uffdio_zeropage_eexist = false;
853 retry_uffdio_zeropage(ufd, &uffdio_zeropage,
860 "UFFDIO_ZEROPAGE succeeded %Ld\n",
861 uffdio_zeropage.zeropage), exit(1);
867 static int uffdio_zeropage(int ufd, unsigned long offset)
869 return __uffdio_zeropage(ufd, offset, false);
872 /* exercise UFFDIO_ZEROPAGE */
873 static int userfaultfd_zeropage_test(void)
875 struct uffdio_register uffdio_register;
876 unsigned long expected_ioctls;
878 printf("testing UFFDIO_ZEROPAGE: ");
881 if (uffd_test_ops->release_pages(area_dst))
884 if (userfaultfd_open(0) < 0)
886 uffdio_register.range.start = (unsigned long) area_dst;
887 uffdio_register.range.len = nr_pages * page_size;
888 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
889 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
890 fprintf(stderr, "register failure\n"), exit(1);
892 expected_ioctls = uffd_test_ops->expected_ioctls;
893 if ((uffdio_register.ioctls & expected_ioctls) !=
896 "unexpected missing ioctl for anon memory\n"),
899 if (uffdio_zeropage(uffd, 0)) {
900 if (my_bcmp(area_dst, zeropage, page_size))
901 fprintf(stderr, "zeropage is not zero\n"), exit(1);
909 static int userfaultfd_events_test(void)
911 struct uffdio_register uffdio_register;
912 unsigned long expected_ioctls;
913 unsigned long userfaults;
919 printf("testing events (fork, remap, remove): ");
922 if (uffd_test_ops->release_pages(area_dst))
925 features = UFFD_FEATURE_EVENT_FORK | UFFD_FEATURE_EVENT_REMAP |
926 UFFD_FEATURE_EVENT_REMOVE;
927 if (userfaultfd_open(features) < 0)
929 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
931 uffdio_register.range.start = (unsigned long) area_dst;
932 uffdio_register.range.len = nr_pages * page_size;
933 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
934 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
935 fprintf(stderr, "register failure\n"), exit(1);
937 expected_ioctls = uffd_test_ops->expected_ioctls;
938 if ((uffdio_register.ioctls & expected_ioctls) !=
941 "unexpected missing ioctl for anon memory\n"),
944 if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
945 perror("uffd_poll_thread create"), exit(1);
949 perror("fork"), exit(1);
952 return faulting_process(0);
954 waitpid(pid, &err, 0);
956 fprintf(stderr, "faulting process failed\n"), exit(1);
958 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
959 perror("pipe write"), exit(1);
960 if (pthread_join(uffd_mon, (void **)&userfaults))
964 printf("userfaults: %ld\n", userfaults);
966 return userfaults != nr_pages;
969 static int userfaultfd_sig_test(void)
971 struct uffdio_register uffdio_register;
972 unsigned long expected_ioctls;
973 unsigned long userfaults;
979 printf("testing signal delivery: ");
982 if (uffd_test_ops->release_pages(area_dst))
985 features = UFFD_FEATURE_EVENT_FORK|UFFD_FEATURE_SIGBUS;
986 if (userfaultfd_open(features) < 0)
988 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
990 uffdio_register.range.start = (unsigned long) area_dst;
991 uffdio_register.range.len = nr_pages * page_size;
992 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
993 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
994 fprintf(stderr, "register failure\n"), exit(1);
996 expected_ioctls = uffd_test_ops->expected_ioctls;
997 if ((uffdio_register.ioctls & expected_ioctls) !=
1000 "unexpected missing ioctl for anon memory\n"),
1003 if (faulting_process(1))
1004 fprintf(stderr, "faulting process failed\n"), exit(1);
1006 if (uffd_test_ops->release_pages(area_dst))
1009 if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
1010 perror("uffd_poll_thread create"), exit(1);
1014 perror("fork"), exit(1);
1017 exit(faulting_process(2));
1019 waitpid(pid, &err, 0);
1021 fprintf(stderr, "faulting process failed\n"), exit(1);
1023 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
1024 perror("pipe write"), exit(1);
1025 if (pthread_join(uffd_mon, (void **)&userfaults))
1030 fprintf(stderr, "Signal test failed, userfaults: %ld\n",
1033 return userfaults != 0;
1035 static int userfaultfd_stress(void)
1040 struct uffdio_register uffdio_register;
1043 unsigned long userfaults[nr_cpus];
1045 uffd_test_ops->allocate_area((void **)&area_src);
1048 uffd_test_ops->allocate_area((void **)&area_dst);
1052 if (userfaultfd_open(0) < 0)
1055 count_verify = malloc(nr_pages * sizeof(unsigned long long));
1056 if (!count_verify) {
1057 perror("count_verify");
1061 for (nr = 0; nr < nr_pages; nr++) {
1062 *area_mutex(area_src, nr) = (pthread_mutex_t)
1063 PTHREAD_MUTEX_INITIALIZER;
1064 count_verify[nr] = *area_count(area_src, nr) = 1;
1066 * In the transition between 255 to 256, powerpc will
1067 * read out of order in my_bcmp and see both bytes as
1068 * zero, so leave a placeholder below always non-zero
1069 * after the count, to avoid my_bcmp to trigger false
1072 *(area_count(area_src, nr) + 1) = 1;
1075 pipefd = malloc(sizeof(int) * nr_cpus * 2);
1080 for (cpu = 0; cpu < nr_cpus; cpu++) {
1081 if (pipe2(&pipefd[cpu*2], O_CLOEXEC | O_NONBLOCK)) {
1087 if (posix_memalign(&area, page_size, page_size)) {
1088 fprintf(stderr, "out of memory\n");
1092 bzero(zeropage, page_size);
1094 pthread_mutex_lock(&uffd_read_mutex);
1096 pthread_attr_init(&attr);
1097 pthread_attr_setstacksize(&attr, 16*1024*1024);
1101 unsigned long expected_ioctls;
1103 printf("bounces: %d, mode:", bounces);
1104 if (bounces & BOUNCE_RANDOM)
1106 if (bounces & BOUNCE_RACINGFAULTS)
1108 if (bounces & BOUNCE_VERIFY)
1110 if (bounces & BOUNCE_POLL)
1115 if (bounces & BOUNCE_POLL)
1116 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
1118 fcntl(uffd, F_SETFL, uffd_flags & ~O_NONBLOCK);
1121 uffdio_register.range.start = (unsigned long) area_dst;
1122 uffdio_register.range.len = nr_pages * page_size;
1123 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
1124 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
1125 fprintf(stderr, "register failure\n");
1128 expected_ioctls = uffd_test_ops->expected_ioctls;
1129 if ((uffdio_register.ioctls & expected_ioctls) !=
1132 "unexpected missing ioctl for anon memory\n");
1136 if (area_dst_alias) {
1137 uffdio_register.range.start = (unsigned long)
1139 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
1140 fprintf(stderr, "register failure alias\n");
1146 * The madvise done previously isn't enough: some
1147 * uffd_thread could have read userfaults (one of
1148 * those already resolved by the background thread)
1149 * and it may be in the process of calling
1150 * UFFDIO_COPY. UFFDIO_COPY will read the zapped
1151 * area_src and it would map a zero page in it (of
1152 * course such a UFFDIO_COPY is perfectly safe as it'd
1153 * return -EEXIST). The problem comes at the next
1154 * bounce though: that racing UFFDIO_COPY would
1155 * generate zeropages in the area_src, so invalidating
1156 * the previous MADV_DONTNEED. Without this additional
1157 * MADV_DONTNEED those zeropages leftovers in the
1158 * area_src would lead to -EEXIST failure during the
1159 * next bounce, effectively leaving a zeropage in the
1162 * Try to comment this out madvise to see the memory
1163 * corruption being caught pretty quick.
1165 * khugepaged is also inhibited to collapse THP after
1166 * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's
1167 * required to MADV_DONTNEED here.
1169 if (uffd_test_ops->release_pages(area_dst))
1173 if (stress(userfaults))
1177 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) {
1178 fprintf(stderr, "unregister failure\n");
1181 if (area_dst_alias) {
1182 uffdio_register.range.start = (unsigned long) area_dst;
1183 if (ioctl(uffd, UFFDIO_UNREGISTER,
1184 &uffdio_register.range)) {
1185 fprintf(stderr, "unregister failure alias\n");
1191 if (bounces & BOUNCE_VERIFY) {
1192 for (nr = 0; nr < nr_pages; nr++) {
1193 if (*area_count(area_dst, nr) != count_verify[nr]) {
1195 "error area_count %Lu %Lu %lu\n",
1196 *area_count(area_src, nr),
1205 /* prepare next bounce */
1206 tmp_area = area_src;
1207 area_src = area_dst;
1208 area_dst = tmp_area;
1210 tmp_area = area_src_alias;
1211 area_src_alias = area_dst_alias;
1212 area_dst_alias = tmp_area;
1214 printf("userfaults:");
1215 for (cpu = 0; cpu < nr_cpus; cpu++)
1216 printf(" %lu", userfaults[cpu]);
1224 return userfaultfd_zeropage_test() || userfaultfd_sig_test()
1225 || userfaultfd_events_test();
1229 * Copied from mlock2-tests.c
1231 unsigned long default_huge_page_size(void)
1233 unsigned long hps = 0;
1236 FILE *f = fopen("/proc/meminfo", "r");
1240 while (getline(&line, &linelen, f) > 0) {
1241 if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
1252 static void set_test_type(const char *type)
1254 if (!strcmp(type, "anon")) {
1255 test_type = TEST_ANON;
1256 uffd_test_ops = &anon_uffd_test_ops;
1257 } else if (!strcmp(type, "hugetlb")) {
1258 test_type = TEST_HUGETLB;
1259 uffd_test_ops = &hugetlb_uffd_test_ops;
1260 } else if (!strcmp(type, "hugetlb_shared")) {
1262 test_type = TEST_HUGETLB;
1263 uffd_test_ops = &hugetlb_uffd_test_ops;
1264 } else if (!strcmp(type, "shmem")) {
1266 test_type = TEST_SHMEM;
1267 uffd_test_ops = &shmem_uffd_test_ops;
1269 fprintf(stderr, "Unknown test type: %s\n", type), exit(1);
1272 if (test_type == TEST_HUGETLB)
1273 page_size = default_huge_page_size();
1275 page_size = sysconf(_SC_PAGE_SIZE);
1278 fprintf(stderr, "Unable to determine page size\n"),
1280 if ((unsigned long) area_count(NULL, 0) + sizeof(unsigned long long) * 2
1282 fprintf(stderr, "Impossible to run this test\n"), exit(2);
1285 static void sigalrm(int sig)
1289 test_uffdio_copy_eexist = true;
1290 test_uffdio_zeropage_eexist = true;
1291 alarm(ALARM_INTERVAL_SECS);
1294 int main(int argc, char **argv)
1299 if (signal(SIGALRM, sigalrm) == SIG_ERR)
1300 fprintf(stderr, "failed to arm SIGALRM"), exit(1);
1301 alarm(ALARM_INTERVAL_SECS);
1303 set_test_type(argv[1]);
1305 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1306 nr_pages_per_cpu = atol(argv[2]) * 1024*1024 / page_size /
1308 if (!nr_pages_per_cpu) {
1309 fprintf(stderr, "invalid MiB\n");
1313 bounces = atoi(argv[3]);
1315 fprintf(stderr, "invalid bounces\n");
1318 nr_pages = nr_pages_per_cpu * nr_cpus;
1320 if (test_type == TEST_HUGETLB) {
1323 huge_fd = open(argv[4], O_CREAT | O_RDWR, 0755);
1325 fprintf(stderr, "Open of %s failed", argv[3]);
1329 if (ftruncate(huge_fd, 0)) {
1330 fprintf(stderr, "ftruncate %s to size 0 failed", argv[3]);
1331 perror("ftruncate");
1335 printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
1336 nr_pages, nr_pages_per_cpu);
1337 return userfaultfd_stress();
1340 #else /* __NR_userfaultfd */
1342 #warning "missing __NR_userfaultfd definition"
1346 printf("skip: Skipping userfaultfd test (missing __NR_userfaultfd)\n");
1350 #endif /* __NR_userfaultfd */