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).
38 * The program takes two parameters: the amounts of physical memory in
39 * megabytes (MiB) of the area and the number of bounces to execute.
41 * # 100MiB 99999 bounces
42 * ./userfaultfd 100 99999
45 * ./userfaultfd 1000 99
47 * # 10MiB-~6GiB 999 bounces, continue forever unless an error triggers
48 * while ./userfaultfd $[RANDOM % 6000 + 10] 999; do true; done
56 #include <sys/types.h>
64 #include <sys/syscall.h>
65 #include <sys/ioctl.h>
68 #include <linux/userfaultfd.h>
72 #include "../kselftest.h"
74 #ifdef __NR_userfaultfd
76 static unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;
78 #define BOUNCE_RANDOM (1<<0)
79 #define BOUNCE_RACINGFAULTS (1<<1)
80 #define BOUNCE_VERIFY (1<<2)
81 #define BOUNCE_POLL (1<<3)
85 #define TEST_HUGETLB 2
89 /* exercise the test_uffdio_*_eexist every ALARM_INTERVAL_SECS */
90 #define ALARM_INTERVAL_SECS 10
91 static volatile bool test_uffdio_copy_eexist = true;
92 static volatile bool test_uffdio_zeropage_eexist = true;
94 static bool map_shared;
96 static char *huge_fd_off0;
97 static unsigned long long *count_verify;
98 static int uffd, uffd_flags, finished, *pipefd;
99 static char *area_src, *area_src_alias, *area_dst, *area_dst_alias;
100 static char *zeropage;
103 /* pthread_mutex_t starts at page offset 0 */
104 #define area_mutex(___area, ___nr) \
105 ((pthread_mutex_t *) ((___area) + (___nr)*page_size))
107 * count is placed in the page after pthread_mutex_t naturally aligned
108 * to avoid non alignment faults on non-x86 archs.
110 #define area_count(___area, ___nr) \
111 ((volatile unsigned long long *) ((unsigned long) \
112 ((___area) + (___nr)*page_size + \
113 sizeof(pthread_mutex_t) + \
114 sizeof(unsigned long long) - 1) & \
115 ~(unsigned long)(sizeof(unsigned long long) \
118 static int anon_release_pages(char *rel_area)
122 if (madvise(rel_area, nr_pages * page_size, MADV_DONTNEED)) {
130 static void anon_allocate_area(void **alloc_area)
132 if (posix_memalign(alloc_area, page_size, nr_pages * page_size)) {
133 fprintf(stderr, "out of memory\n");
138 static void noop_alias_mapping(__u64 *start, size_t len, unsigned long offset)
143 static int hugetlb_release_pages(char *rel_area)
147 if (fallocate(huge_fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
148 rel_area == huge_fd_off0 ? 0 :
149 nr_pages * page_size,
150 nr_pages * page_size)) {
159 static void hugetlb_allocate_area(void **alloc_area)
161 void *area_alias = NULL;
162 char **alloc_area_alias;
163 *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
164 (map_shared ? MAP_SHARED : MAP_PRIVATE) |
166 huge_fd, *alloc_area == area_src ? 0 :
167 nr_pages * page_size);
168 if (*alloc_area == MAP_FAILED) {
169 fprintf(stderr, "mmap of hugetlbfs file failed\n");
174 area_alias = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
175 MAP_SHARED | MAP_HUGETLB,
176 huge_fd, *alloc_area == area_src ? 0 :
177 nr_pages * page_size);
178 if (area_alias == MAP_FAILED) {
179 if (munmap(*alloc_area, nr_pages * page_size) < 0)
180 perror("hugetlb munmap"), exit(1);
185 if (*alloc_area == area_src) {
186 huge_fd_off0 = *alloc_area;
187 alloc_area_alias = &area_src_alias;
189 alloc_area_alias = &area_dst_alias;
192 *alloc_area_alias = area_alias;
195 static void hugetlb_alias_mapping(__u64 *start, size_t len, unsigned long offset)
200 * We can't zap just the pagetable with hugetlbfs because
201 * MADV_DONTEED won't work. So exercise -EEXIST on a alias
202 * mapping where the pagetables are not established initially,
203 * this way we'll exercise the -EEXEC at the fs level.
205 *start = (unsigned long) area_dst_alias + offset;
209 static int shmem_release_pages(char *rel_area)
213 if (madvise(rel_area, nr_pages * page_size, MADV_REMOVE)) {
221 static void shmem_allocate_area(void **alloc_area)
223 *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
224 MAP_ANONYMOUS | MAP_SHARED, -1, 0);
225 if (*alloc_area == MAP_FAILED) {
226 fprintf(stderr, "shared memory mmap failed\n");
231 struct uffd_test_ops {
232 unsigned long expected_ioctls;
233 void (*allocate_area)(void **alloc_area);
234 int (*release_pages)(char *rel_area);
235 void (*alias_mapping)(__u64 *start, size_t len, unsigned long offset);
238 #define ANON_EXPECTED_IOCTLS ((1 << _UFFDIO_WAKE) | \
239 (1 << _UFFDIO_COPY) | \
240 (1 << _UFFDIO_ZEROPAGE))
242 static struct uffd_test_ops anon_uffd_test_ops = {
243 .expected_ioctls = ANON_EXPECTED_IOCTLS,
244 .allocate_area = anon_allocate_area,
245 .release_pages = anon_release_pages,
246 .alias_mapping = noop_alias_mapping,
249 static struct uffd_test_ops shmem_uffd_test_ops = {
250 .expected_ioctls = ANON_EXPECTED_IOCTLS,
251 .allocate_area = shmem_allocate_area,
252 .release_pages = shmem_release_pages,
253 .alias_mapping = noop_alias_mapping,
256 static struct uffd_test_ops hugetlb_uffd_test_ops = {
257 .expected_ioctls = UFFD_API_RANGE_IOCTLS_BASIC,
258 .allocate_area = hugetlb_allocate_area,
259 .release_pages = hugetlb_release_pages,
260 .alias_mapping = hugetlb_alias_mapping,
263 static struct uffd_test_ops *uffd_test_ops;
265 static int my_bcmp(char *str1, char *str2, size_t n)
268 for (i = 0; i < n; i++)
269 if (str1[i] != str2[i])
274 static void *locking_thread(void *arg)
276 unsigned long cpu = (unsigned long) arg;
277 struct random_data rand;
278 unsigned long page_nr = *(&(page_nr)); /* uninitialized warning */
280 unsigned long long count;
285 if (bounces & BOUNCE_RANDOM) {
286 seed = (unsigned int) time(NULL) - bounces;
287 if (!(bounces & BOUNCE_RACINGFAULTS))
289 bzero(&rand, sizeof(rand));
290 bzero(&randstate, sizeof(randstate));
291 if (initstate_r(seed, randstate, sizeof(randstate), &rand))
292 fprintf(stderr, "srandom_r error\n"), exit(1);
295 if (!(bounces & BOUNCE_RACINGFAULTS))
296 page_nr += cpu * nr_pages_per_cpu;
300 if (bounces & BOUNCE_RANDOM) {
301 if (random_r(&rand, &rand_nr))
302 fprintf(stderr, "random_r 1 error\n"), exit(1);
304 if (sizeof(page_nr) > sizeof(rand_nr)) {
305 if (random_r(&rand, &rand_nr))
306 fprintf(stderr, "random_r 2 error\n"), exit(1);
307 page_nr |= (((unsigned long) rand_nr) << 16) <<
315 if (bounces & BOUNCE_VERIFY) {
316 count = *area_count(area_dst, page_nr);
319 "page_nr %lu wrong count %Lu %Lu\n",
321 count_verify[page_nr]), exit(1);
325 * We can't use bcmp (or memcmp) because that
326 * returns 0 erroneously if the memory is
327 * changing under it (even if the end of the
328 * page is never changing and always
332 if (!my_bcmp(area_dst + page_nr * page_size, zeropage,
335 "my_bcmp page_nr %lu wrong count %Lu %Lu\n",
337 count_verify[page_nr]), exit(1);
342 /* uncomment the below line to test with mutex */
343 /* pthread_mutex_lock(area_mutex(area_dst, page_nr)); */
344 while (!bcmp(area_dst + page_nr * page_size, zeropage,
350 /* uncomment below line to test with mutex */
351 /* pthread_mutex_unlock(area_mutex(area_dst, page_nr)); */
354 "page_nr %lu all zero thread %lu %p %lu\n",
355 page_nr, cpu, area_dst + page_nr * page_size,
363 pthread_mutex_lock(area_mutex(area_dst, page_nr));
364 count = *area_count(area_dst, page_nr);
365 if (count != count_verify[page_nr]) {
367 "page_nr %lu memory corruption %Lu %Lu\n",
369 count_verify[page_nr]), exit(1);
372 *area_count(area_dst, page_nr) = count_verify[page_nr] = count;
373 pthread_mutex_unlock(area_mutex(area_dst, page_nr));
375 if (time(NULL) - start > 1)
377 "userfault too slow %ld "
378 "possible false positive with overcommit\n",
385 static void retry_copy_page(int ufd, struct uffdio_copy *uffdio_copy,
386 unsigned long offset)
388 uffd_test_ops->alias_mapping(&uffdio_copy->dst,
391 if (ioctl(ufd, UFFDIO_COPY, uffdio_copy)) {
392 /* real retval in ufdio_copy.copy */
393 if (uffdio_copy->copy != -EEXIST)
394 fprintf(stderr, "UFFDIO_COPY retry error %Ld\n",
395 uffdio_copy->copy), exit(1);
397 fprintf(stderr, "UFFDIO_COPY retry unexpected %Ld\n",
398 uffdio_copy->copy), exit(1);
402 static int __copy_page(int ufd, unsigned long offset, bool retry)
404 struct uffdio_copy uffdio_copy;
406 if (offset >= nr_pages * page_size)
407 fprintf(stderr, "unexpected offset %lu\n",
409 uffdio_copy.dst = (unsigned long) area_dst + offset;
410 uffdio_copy.src = (unsigned long) area_src + offset;
411 uffdio_copy.len = page_size;
412 uffdio_copy.mode = 0;
413 uffdio_copy.copy = 0;
414 if (ioctl(ufd, UFFDIO_COPY, &uffdio_copy)) {
415 /* real retval in ufdio_copy.copy */
416 if (uffdio_copy.copy != -EEXIST)
417 fprintf(stderr, "UFFDIO_COPY error %Ld\n",
418 uffdio_copy.copy), exit(1);
419 } else if (uffdio_copy.copy != page_size) {
420 fprintf(stderr, "UFFDIO_COPY unexpected copy %Ld\n",
421 uffdio_copy.copy), exit(1);
423 if (test_uffdio_copy_eexist && retry) {
424 test_uffdio_copy_eexist = false;
425 retry_copy_page(ufd, &uffdio_copy, offset);
432 static int copy_page_retry(int ufd, unsigned long offset)
434 return __copy_page(ufd, offset, true);
437 static int copy_page(int ufd, unsigned long offset)
439 return __copy_page(ufd, offset, false);
442 static void *uffd_poll_thread(void *arg)
444 unsigned long cpu = (unsigned long) arg;
445 struct pollfd pollfd[2];
447 struct uffdio_register uffd_reg;
449 unsigned long offset;
451 unsigned long userfaults = 0;
454 pollfd[0].events = POLLIN;
455 pollfd[1].fd = pipefd[cpu*2];
456 pollfd[1].events = POLLIN;
459 ret = poll(pollfd, 2, -1);
461 fprintf(stderr, "poll error %d\n", ret), exit(1);
463 perror("poll"), exit(1);
464 if (pollfd[1].revents & POLLIN) {
465 if (read(pollfd[1].fd, &tmp_chr, 1) != 1)
466 fprintf(stderr, "read pipefd error\n"),
470 if (!(pollfd[0].revents & POLLIN))
471 fprintf(stderr, "pollfd[0].revents %d\n",
472 pollfd[0].revents), exit(1);
473 ret = read(uffd, &msg, sizeof(msg));
477 perror("nonblocking read error"), exit(1);
481 fprintf(stderr, "unexpected msg event %u\n",
484 case UFFD_EVENT_PAGEFAULT:
485 if (msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
486 fprintf(stderr, "unexpected write fault\n"), exit(1);
487 offset = (char *)(unsigned long)msg.arg.pagefault.address -
489 offset &= ~(page_size-1);
490 if (copy_page(uffd, offset))
493 case UFFD_EVENT_FORK:
495 uffd = msg.arg.fork.ufd;
498 case UFFD_EVENT_REMOVE:
499 uffd_reg.range.start = msg.arg.remove.start;
500 uffd_reg.range.len = msg.arg.remove.end -
501 msg.arg.remove.start;
502 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffd_reg.range))
503 fprintf(stderr, "remove failure\n"), exit(1);
505 case UFFD_EVENT_REMAP:
506 area_dst = (char *)(unsigned long)msg.arg.remap.to;
510 return (void *)userfaults;
513 pthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER;
515 static void *uffd_read_thread(void *arg)
517 unsigned long *this_cpu_userfaults;
519 unsigned long offset;
522 this_cpu_userfaults = (unsigned long *) arg;
523 *this_cpu_userfaults = 0;
525 pthread_mutex_unlock(&uffd_read_mutex);
526 /* from here cancellation is ok */
529 ret = read(uffd, &msg, sizeof(msg));
530 if (ret != sizeof(msg)) {
532 perror("blocking read error"), exit(1);
534 fprintf(stderr, "short read\n"), exit(1);
536 if (msg.event != UFFD_EVENT_PAGEFAULT)
537 fprintf(stderr, "unexpected msg event %u\n",
539 if (bounces & BOUNCE_VERIFY &&
540 msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
541 fprintf(stderr, "unexpected write fault\n"), exit(1);
542 offset = (char *)(unsigned long)msg.arg.pagefault.address -
544 offset &= ~(page_size-1);
545 if (copy_page(uffd, offset))
546 (*this_cpu_userfaults)++;
551 static void *background_thread(void *arg)
553 unsigned long cpu = (unsigned long) arg;
554 unsigned long page_nr;
556 for (page_nr = cpu * nr_pages_per_cpu;
557 page_nr < (cpu+1) * nr_pages_per_cpu;
559 copy_page_retry(uffd, page_nr * page_size);
564 static int stress(unsigned long *userfaults)
567 pthread_t locking_threads[nr_cpus];
568 pthread_t uffd_threads[nr_cpus];
569 pthread_t background_threads[nr_cpus];
570 void **_userfaults = (void **) userfaults;
573 for (cpu = 0; cpu < nr_cpus; cpu++) {
574 if (pthread_create(&locking_threads[cpu], &attr,
575 locking_thread, (void *)cpu))
577 if (bounces & BOUNCE_POLL) {
578 if (pthread_create(&uffd_threads[cpu], &attr,
579 uffd_poll_thread, (void *)cpu))
582 if (pthread_create(&uffd_threads[cpu], &attr,
586 pthread_mutex_lock(&uffd_read_mutex);
588 if (pthread_create(&background_threads[cpu], &attr,
589 background_thread, (void *)cpu))
592 for (cpu = 0; cpu < nr_cpus; cpu++)
593 if (pthread_join(background_threads[cpu], NULL))
597 * Be strict and immediately zap area_src, the whole area has
598 * been transferred already by the background treads. The
599 * area_src could then be faulted in in a racy way by still
600 * running uffdio_threads reading zeropages after we zapped
601 * area_src (but they're guaranteed to get -EEXIST from
602 * UFFDIO_COPY without writing zero pages into area_dst
603 * because the background threads already completed).
605 if (uffd_test_ops->release_pages(area_src))
608 for (cpu = 0; cpu < nr_cpus; cpu++) {
610 if (bounces & BOUNCE_POLL) {
611 if (write(pipefd[cpu*2+1], &c, 1) != 1) {
612 fprintf(stderr, "pipefd write error\n");
615 if (pthread_join(uffd_threads[cpu], &_userfaults[cpu]))
618 if (pthread_cancel(uffd_threads[cpu]))
620 if (pthread_join(uffd_threads[cpu], NULL))
626 for (cpu = 0; cpu < nr_cpus; cpu++)
627 if (pthread_join(locking_threads[cpu], NULL))
633 static int userfaultfd_open(int features)
635 struct uffdio_api uffdio_api;
637 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
640 "userfaultfd syscall not available in this kernel\n");
643 uffd_flags = fcntl(uffd, F_GETFD, NULL);
645 uffdio_api.api = UFFD_API;
646 uffdio_api.features = features;
647 if (ioctl(uffd, UFFDIO_API, &uffdio_api)) {
648 fprintf(stderr, "UFFDIO_API\n");
651 if (uffdio_api.api != UFFD_API) {
652 fprintf(stderr, "UFFDIO_API error %Lu\n", uffdio_api.api);
659 sigjmp_buf jbuf, *sigbuf;
661 static void sighndl(int sig, siginfo_t *siginfo, void *ptr)
665 siglongjmp(*sigbuf, 1);
671 * For non-cooperative userfaultfd test we fork() a process that will
672 * generate pagefaults, will mremap the area monitored by the
673 * userfaultfd and at last this process will release the monitored
675 * For the anonymous and shared memory the area is divided into two
676 * parts, the first part is accessed before mremap, and the second
677 * part is accessed after mremap. Since hugetlbfs does not support
678 * mremap, the entire monitored area is accessed in a single pass for
680 * The release of the pages currently generates event for shmem and
681 * anonymous memory (UFFD_EVENT_REMOVE), hence it is not checked
683 * For signal test(UFFD_FEATURE_SIGBUS), signal_test = 1, we register
684 * monitored area, generate pagefaults and test that signal is delivered.
685 * Use UFFDIO_COPY to allocate missing page and retry. For signal_test = 2
686 * test robustness use case - we release monitored area, fork a process
687 * that will generate pagefaults and verify signal is generated.
688 * This also tests UFFD_FEATURE_EVENT_FORK event along with the signal
689 * feature. Using monitor thread, verify no userfault events are generated.
691 static int faulting_process(int signal_test)
694 unsigned long long count;
695 unsigned long split_nr_pages;
696 unsigned long lastnr;
697 struct sigaction act;
698 unsigned long signalled = 0;
700 if (test_type != TEST_HUGETLB)
701 split_nr_pages = (nr_pages + 1) / 2;
703 split_nr_pages = nr_pages;
707 memset(&act, 0, sizeof(act));
708 act.sa_sigaction = sighndl;
709 act.sa_flags = SA_SIGINFO;
710 if (sigaction(SIGBUS, &act, 0)) {
714 lastnr = (unsigned long)-1;
717 for (nr = 0; nr < split_nr_pages; nr++) {
719 if (sigsetjmp(*sigbuf, 1) != 0) {
721 fprintf(stderr, "Signal repeated\n");
726 if (signal_test == 1) {
727 if (copy_page(uffd, nr * page_size))
736 count = *area_count(area_dst, nr);
737 if (count != count_verify[nr]) {
739 "nr %lu memory corruption %Lu %Lu\n",
741 count_verify[nr]), exit(1);
746 return signalled != split_nr_pages;
748 if (test_type == TEST_HUGETLB)
751 area_dst = mremap(area_dst, nr_pages * page_size, nr_pages * page_size,
752 MREMAP_MAYMOVE | MREMAP_FIXED, area_src);
753 if (area_dst == MAP_FAILED)
754 perror("mremap"), exit(1);
756 for (; nr < nr_pages; nr++) {
757 count = *area_count(area_dst, nr);
758 if (count != count_verify[nr]) {
760 "nr %lu memory corruption %Lu %Lu\n",
762 count_verify[nr]), exit(1);
766 if (uffd_test_ops->release_pages(area_dst))
769 for (nr = 0; nr < nr_pages; nr++) {
770 if (my_bcmp(area_dst + nr * page_size, zeropage, page_size))
771 fprintf(stderr, "nr %lu is not zero\n", nr), exit(1);
777 static void retry_uffdio_zeropage(int ufd,
778 struct uffdio_zeropage *uffdio_zeropage,
779 unsigned long offset)
781 uffd_test_ops->alias_mapping(&uffdio_zeropage->range.start,
782 uffdio_zeropage->range.len,
784 if (ioctl(ufd, UFFDIO_ZEROPAGE, uffdio_zeropage)) {
785 if (uffdio_zeropage->zeropage != -EEXIST)
786 fprintf(stderr, "UFFDIO_ZEROPAGE retry error %Ld\n",
787 uffdio_zeropage->zeropage), exit(1);
789 fprintf(stderr, "UFFDIO_ZEROPAGE retry unexpected %Ld\n",
790 uffdio_zeropage->zeropage), exit(1);
794 static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
796 struct uffdio_zeropage uffdio_zeropage;
798 unsigned long has_zeropage;
800 has_zeropage = uffd_test_ops->expected_ioctls & (1 << _UFFDIO_ZEROPAGE);
802 if (offset >= nr_pages * page_size)
803 fprintf(stderr, "unexpected offset %lu\n",
805 uffdio_zeropage.range.start = (unsigned long) area_dst + offset;
806 uffdio_zeropage.range.len = page_size;
807 uffdio_zeropage.mode = 0;
808 ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage);
810 /* real retval in ufdio_zeropage.zeropage */
812 if (uffdio_zeropage.zeropage == -EEXIST)
813 fprintf(stderr, "UFFDIO_ZEROPAGE -EEXIST\n"),
816 fprintf(stderr, "UFFDIO_ZEROPAGE error %Ld\n",
817 uffdio_zeropage.zeropage), exit(1);
819 if (uffdio_zeropage.zeropage != -EINVAL)
821 "UFFDIO_ZEROPAGE not -EINVAL %Ld\n",
822 uffdio_zeropage.zeropage), exit(1);
824 } else if (has_zeropage) {
825 if (uffdio_zeropage.zeropage != page_size) {
826 fprintf(stderr, "UFFDIO_ZEROPAGE unexpected %Ld\n",
827 uffdio_zeropage.zeropage), exit(1);
829 if (test_uffdio_zeropage_eexist && retry) {
830 test_uffdio_zeropage_eexist = false;
831 retry_uffdio_zeropage(ufd, &uffdio_zeropage,
838 "UFFDIO_ZEROPAGE succeeded %Ld\n",
839 uffdio_zeropage.zeropage), exit(1);
845 static int uffdio_zeropage(int ufd, unsigned long offset)
847 return __uffdio_zeropage(ufd, offset, false);
850 /* exercise UFFDIO_ZEROPAGE */
851 static int userfaultfd_zeropage_test(void)
853 struct uffdio_register uffdio_register;
854 unsigned long expected_ioctls;
856 printf("testing UFFDIO_ZEROPAGE: ");
859 if (uffd_test_ops->release_pages(area_dst))
862 if (userfaultfd_open(0) < 0)
864 uffdio_register.range.start = (unsigned long) area_dst;
865 uffdio_register.range.len = nr_pages * page_size;
866 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
867 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
868 fprintf(stderr, "register failure\n"), exit(1);
870 expected_ioctls = uffd_test_ops->expected_ioctls;
871 if ((uffdio_register.ioctls & expected_ioctls) !=
874 "unexpected missing ioctl for anon memory\n"),
877 if (uffdio_zeropage(uffd, 0)) {
878 if (my_bcmp(area_dst, zeropage, page_size))
879 fprintf(stderr, "zeropage is not zero\n"), exit(1);
887 static int userfaultfd_events_test(void)
889 struct uffdio_register uffdio_register;
890 unsigned long expected_ioctls;
891 unsigned long userfaults;
897 printf("testing events (fork, remap, remove): ");
900 if (uffd_test_ops->release_pages(area_dst))
903 features = UFFD_FEATURE_EVENT_FORK | UFFD_FEATURE_EVENT_REMAP |
904 UFFD_FEATURE_EVENT_REMOVE;
905 if (userfaultfd_open(features) < 0)
907 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
909 uffdio_register.range.start = (unsigned long) area_dst;
910 uffdio_register.range.len = nr_pages * page_size;
911 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
912 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
913 fprintf(stderr, "register failure\n"), exit(1);
915 expected_ioctls = uffd_test_ops->expected_ioctls;
916 if ((uffdio_register.ioctls & expected_ioctls) !=
919 "unexpected missing ioctl for anon memory\n"),
922 if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
923 perror("uffd_poll_thread create"), exit(1);
927 perror("fork"), exit(1);
930 return faulting_process(0);
932 waitpid(pid, &err, 0);
934 fprintf(stderr, "faulting process failed\n"), exit(1);
936 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
937 perror("pipe write"), exit(1);
938 if (pthread_join(uffd_mon, (void **)&userfaults))
942 printf("userfaults: %ld\n", userfaults);
944 return userfaults != nr_pages;
947 static int userfaultfd_sig_test(void)
949 struct uffdio_register uffdio_register;
950 unsigned long expected_ioctls;
951 unsigned long userfaults;
957 printf("testing signal delivery: ");
960 if (uffd_test_ops->release_pages(area_dst))
963 features = UFFD_FEATURE_EVENT_FORK|UFFD_FEATURE_SIGBUS;
964 if (userfaultfd_open(features) < 0)
966 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
968 uffdio_register.range.start = (unsigned long) area_dst;
969 uffdio_register.range.len = nr_pages * page_size;
970 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
971 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
972 fprintf(stderr, "register failure\n"), exit(1);
974 expected_ioctls = uffd_test_ops->expected_ioctls;
975 if ((uffdio_register.ioctls & expected_ioctls) !=
978 "unexpected missing ioctl for anon memory\n"),
981 if (faulting_process(1))
982 fprintf(stderr, "faulting process failed\n"), exit(1);
984 if (uffd_test_ops->release_pages(area_dst))
987 if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
988 perror("uffd_poll_thread create"), exit(1);
992 perror("fork"), exit(1);
995 exit(faulting_process(2));
997 waitpid(pid, &err, 0);
999 fprintf(stderr, "faulting process failed\n"), exit(1);
1001 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
1002 perror("pipe write"), exit(1);
1003 if (pthread_join(uffd_mon, (void **)&userfaults))
1008 fprintf(stderr, "Signal test failed, userfaults: %ld\n",
1011 return userfaults != 0;
1013 static int userfaultfd_stress(void)
1018 struct uffdio_register uffdio_register;
1021 unsigned long userfaults[nr_cpus];
1023 uffd_test_ops->allocate_area((void **)&area_src);
1026 uffd_test_ops->allocate_area((void **)&area_dst);
1030 if (userfaultfd_open(0) < 0)
1033 count_verify = malloc(nr_pages * sizeof(unsigned long long));
1034 if (!count_verify) {
1035 perror("count_verify");
1039 for (nr = 0; nr < nr_pages; nr++) {
1040 *area_mutex(area_src, nr) = (pthread_mutex_t)
1041 PTHREAD_MUTEX_INITIALIZER;
1042 count_verify[nr] = *area_count(area_src, nr) = 1;
1044 * In the transition between 255 to 256, powerpc will
1045 * read out of order in my_bcmp and see both bytes as
1046 * zero, so leave a placeholder below always non-zero
1047 * after the count, to avoid my_bcmp to trigger false
1050 *(area_count(area_src, nr) + 1) = 1;
1053 pipefd = malloc(sizeof(int) * nr_cpus * 2);
1058 for (cpu = 0; cpu < nr_cpus; cpu++) {
1059 if (pipe2(&pipefd[cpu*2], O_CLOEXEC | O_NONBLOCK)) {
1065 if (posix_memalign(&area, page_size, page_size)) {
1066 fprintf(stderr, "out of memory\n");
1070 bzero(zeropage, page_size);
1072 pthread_mutex_lock(&uffd_read_mutex);
1074 pthread_attr_init(&attr);
1075 pthread_attr_setstacksize(&attr, 16*1024*1024);
1079 unsigned long expected_ioctls;
1081 printf("bounces: %d, mode:", bounces);
1082 if (bounces & BOUNCE_RANDOM)
1084 if (bounces & BOUNCE_RACINGFAULTS)
1086 if (bounces & BOUNCE_VERIFY)
1088 if (bounces & BOUNCE_POLL)
1093 if (bounces & BOUNCE_POLL)
1094 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
1096 fcntl(uffd, F_SETFL, uffd_flags & ~O_NONBLOCK);
1099 uffdio_register.range.start = (unsigned long) area_dst;
1100 uffdio_register.range.len = nr_pages * page_size;
1101 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
1102 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
1103 fprintf(stderr, "register failure\n");
1106 expected_ioctls = uffd_test_ops->expected_ioctls;
1107 if ((uffdio_register.ioctls & expected_ioctls) !=
1110 "unexpected missing ioctl for anon memory\n");
1114 if (area_dst_alias) {
1115 uffdio_register.range.start = (unsigned long)
1117 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
1118 fprintf(stderr, "register failure alias\n");
1124 * The madvise done previously isn't enough: some
1125 * uffd_thread could have read userfaults (one of
1126 * those already resolved by the background thread)
1127 * and it may be in the process of calling
1128 * UFFDIO_COPY. UFFDIO_COPY will read the zapped
1129 * area_src and it would map a zero page in it (of
1130 * course such a UFFDIO_COPY is perfectly safe as it'd
1131 * return -EEXIST). The problem comes at the next
1132 * bounce though: that racing UFFDIO_COPY would
1133 * generate zeropages in the area_src, so invalidating
1134 * the previous MADV_DONTNEED. Without this additional
1135 * MADV_DONTNEED those zeropages leftovers in the
1136 * area_src would lead to -EEXIST failure during the
1137 * next bounce, effectively leaving a zeropage in the
1140 * Try to comment this out madvise to see the memory
1141 * corruption being caught pretty quick.
1143 * khugepaged is also inhibited to collapse THP after
1144 * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's
1145 * required to MADV_DONTNEED here.
1147 if (uffd_test_ops->release_pages(area_dst))
1151 if (stress(userfaults))
1155 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) {
1156 fprintf(stderr, "unregister failure\n");
1159 if (area_dst_alias) {
1160 uffdio_register.range.start = (unsigned long) area_dst;
1161 if (ioctl(uffd, UFFDIO_UNREGISTER,
1162 &uffdio_register.range)) {
1163 fprintf(stderr, "unregister failure alias\n");
1169 if (bounces & BOUNCE_VERIFY) {
1170 for (nr = 0; nr < nr_pages; nr++) {
1171 if (*area_count(area_dst, nr) != count_verify[nr]) {
1173 "error area_count %Lu %Lu %lu\n",
1174 *area_count(area_src, nr),
1183 /* prepare next bounce */
1184 tmp_area = area_src;
1185 area_src = area_dst;
1186 area_dst = tmp_area;
1188 tmp_area = area_src_alias;
1189 area_src_alias = area_dst_alias;
1190 area_dst_alias = tmp_area;
1192 printf("userfaults:");
1193 for (cpu = 0; cpu < nr_cpus; cpu++)
1194 printf(" %lu", userfaults[cpu]);
1202 return userfaultfd_zeropage_test() || userfaultfd_sig_test()
1203 || userfaultfd_events_test();
1207 * Copied from mlock2-tests.c
1209 unsigned long default_huge_page_size(void)
1211 unsigned long hps = 0;
1214 FILE *f = fopen("/proc/meminfo", "r");
1218 while (getline(&line, &linelen, f) > 0) {
1219 if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
1230 static void set_test_type(const char *type)
1232 if (!strcmp(type, "anon")) {
1233 test_type = TEST_ANON;
1234 uffd_test_ops = &anon_uffd_test_ops;
1235 } else if (!strcmp(type, "hugetlb")) {
1236 test_type = TEST_HUGETLB;
1237 uffd_test_ops = &hugetlb_uffd_test_ops;
1238 } else if (!strcmp(type, "hugetlb_shared")) {
1240 test_type = TEST_HUGETLB;
1241 uffd_test_ops = &hugetlb_uffd_test_ops;
1242 } else if (!strcmp(type, "shmem")) {
1244 test_type = TEST_SHMEM;
1245 uffd_test_ops = &shmem_uffd_test_ops;
1247 fprintf(stderr, "Unknown test type: %s\n", type), exit(1);
1250 if (test_type == TEST_HUGETLB)
1251 page_size = default_huge_page_size();
1253 page_size = sysconf(_SC_PAGE_SIZE);
1256 fprintf(stderr, "Unable to determine page size\n"),
1258 if ((unsigned long) area_count(NULL, 0) + sizeof(unsigned long long) * 2
1260 fprintf(stderr, "Impossible to run this test\n"), exit(2);
1263 static void sigalrm(int sig)
1267 test_uffdio_copy_eexist = true;
1268 test_uffdio_zeropage_eexist = true;
1269 alarm(ALARM_INTERVAL_SECS);
1272 int main(int argc, char **argv)
1275 fprintf(stderr, "Usage: <test type> <MiB> <bounces> [hugetlbfs_file]\n"),
1278 if (signal(SIGALRM, sigalrm) == SIG_ERR)
1279 fprintf(stderr, "failed to arm SIGALRM"), exit(1);
1280 alarm(ALARM_INTERVAL_SECS);
1282 set_test_type(argv[1]);
1284 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1285 nr_pages_per_cpu = atol(argv[2]) * 1024*1024 / page_size /
1287 if (!nr_pages_per_cpu) {
1288 fprintf(stderr, "invalid MiB\n");
1289 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
1292 bounces = atoi(argv[3]);
1294 fprintf(stderr, "invalid bounces\n");
1295 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
1297 nr_pages = nr_pages_per_cpu * nr_cpus;
1299 if (test_type == TEST_HUGETLB) {
1301 fprintf(stderr, "Usage: hugetlb <MiB> <bounces> <hugetlbfs_file>\n"),
1303 huge_fd = open(argv[4], O_CREAT | O_RDWR, 0755);
1305 fprintf(stderr, "Open of %s failed", argv[3]);
1309 if (ftruncate(huge_fd, 0)) {
1310 fprintf(stderr, "ftruncate %s to size 0 failed", argv[3]);
1311 perror("ftruncate");
1315 printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
1316 nr_pages, nr_pages_per_cpu);
1317 return userfaultfd_stress();
1320 #else /* __NR_userfaultfd */
1322 #warning "missing __NR_userfaultfd definition"
1326 printf("skip: Skipping userfaultfd test (missing __NR_userfaultfd)\n");
1330 #endif /* __NR_userfaultfd */