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 *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
133 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
134 if (*alloc_area == MAP_FAILED) {
135 fprintf(stderr, "mmap of anonymous memory failed");
140 static void noop_alias_mapping(__u64 *start, size_t len, unsigned long offset)
145 static int hugetlb_release_pages(char *rel_area)
149 if (fallocate(huge_fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
150 rel_area == huge_fd_off0 ? 0 :
151 nr_pages * page_size,
152 nr_pages * page_size)) {
161 static void hugetlb_allocate_area(void **alloc_area)
163 void *area_alias = NULL;
164 char **alloc_area_alias;
165 *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
166 (map_shared ? MAP_SHARED : MAP_PRIVATE) |
168 huge_fd, *alloc_area == area_src ? 0 :
169 nr_pages * page_size);
170 if (*alloc_area == MAP_FAILED) {
171 fprintf(stderr, "mmap of hugetlbfs file failed\n");
176 area_alias = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
177 MAP_SHARED | MAP_HUGETLB,
178 huge_fd, *alloc_area == area_src ? 0 :
179 nr_pages * page_size);
180 if (area_alias == MAP_FAILED) {
181 if (munmap(*alloc_area, nr_pages * page_size) < 0)
182 perror("hugetlb munmap"), exit(1);
187 if (*alloc_area == area_src) {
188 huge_fd_off0 = *alloc_area;
189 alloc_area_alias = &area_src_alias;
191 alloc_area_alias = &area_dst_alias;
194 *alloc_area_alias = area_alias;
197 static void hugetlb_alias_mapping(__u64 *start, size_t len, unsigned long offset)
202 * We can't zap just the pagetable with hugetlbfs because
203 * MADV_DONTEED won't work. So exercise -EEXIST on a alias
204 * mapping where the pagetables are not established initially,
205 * this way we'll exercise the -EEXEC at the fs level.
207 *start = (unsigned long) area_dst_alias + offset;
211 static int shmem_release_pages(char *rel_area)
215 if (madvise(rel_area, nr_pages * page_size, MADV_REMOVE)) {
223 static void shmem_allocate_area(void **alloc_area)
225 *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
226 MAP_ANONYMOUS | MAP_SHARED, -1, 0);
227 if (*alloc_area == MAP_FAILED) {
228 fprintf(stderr, "shared memory mmap failed\n");
233 struct uffd_test_ops {
234 unsigned long expected_ioctls;
235 void (*allocate_area)(void **alloc_area);
236 int (*release_pages)(char *rel_area);
237 void (*alias_mapping)(__u64 *start, size_t len, unsigned long offset);
240 #define ANON_EXPECTED_IOCTLS ((1 << _UFFDIO_WAKE) | \
241 (1 << _UFFDIO_COPY) | \
242 (1 << _UFFDIO_ZEROPAGE))
244 static struct uffd_test_ops anon_uffd_test_ops = {
245 .expected_ioctls = ANON_EXPECTED_IOCTLS,
246 .allocate_area = anon_allocate_area,
247 .release_pages = anon_release_pages,
248 .alias_mapping = noop_alias_mapping,
251 static struct uffd_test_ops shmem_uffd_test_ops = {
252 .expected_ioctls = ANON_EXPECTED_IOCTLS,
253 .allocate_area = shmem_allocate_area,
254 .release_pages = shmem_release_pages,
255 .alias_mapping = noop_alias_mapping,
258 static struct uffd_test_ops hugetlb_uffd_test_ops = {
259 .expected_ioctls = UFFD_API_RANGE_IOCTLS_BASIC,
260 .allocate_area = hugetlb_allocate_area,
261 .release_pages = hugetlb_release_pages,
262 .alias_mapping = hugetlb_alias_mapping,
265 static struct uffd_test_ops *uffd_test_ops;
267 static int my_bcmp(char *str1, char *str2, size_t n)
270 for (i = 0; i < n; i++)
271 if (str1[i] != str2[i])
276 static void *locking_thread(void *arg)
278 unsigned long cpu = (unsigned long) arg;
279 struct random_data rand;
280 unsigned long page_nr = *(&(page_nr)); /* uninitialized warning */
282 unsigned long long count;
287 if (bounces & BOUNCE_RANDOM) {
288 seed = (unsigned int) time(NULL) - bounces;
289 if (!(bounces & BOUNCE_RACINGFAULTS))
291 bzero(&rand, sizeof(rand));
292 bzero(&randstate, sizeof(randstate));
293 if (initstate_r(seed, randstate, sizeof(randstate), &rand))
294 fprintf(stderr, "srandom_r error\n"), exit(1);
297 if (!(bounces & BOUNCE_RACINGFAULTS))
298 page_nr += cpu * nr_pages_per_cpu;
302 if (bounces & BOUNCE_RANDOM) {
303 if (random_r(&rand, &rand_nr))
304 fprintf(stderr, "random_r 1 error\n"), exit(1);
306 if (sizeof(page_nr) > sizeof(rand_nr)) {
307 if (random_r(&rand, &rand_nr))
308 fprintf(stderr, "random_r 2 error\n"), exit(1);
309 page_nr |= (((unsigned long) rand_nr) << 16) <<
317 if (bounces & BOUNCE_VERIFY) {
318 count = *area_count(area_dst, page_nr);
321 "page_nr %lu wrong count %Lu %Lu\n",
323 count_verify[page_nr]), exit(1);
327 * We can't use bcmp (or memcmp) because that
328 * returns 0 erroneously if the memory is
329 * changing under it (even if the end of the
330 * page is never changing and always
334 if (!my_bcmp(area_dst + page_nr * page_size, zeropage,
337 "my_bcmp page_nr %lu wrong count %Lu %Lu\n",
339 count_verify[page_nr]), exit(1);
344 /* uncomment the below line to test with mutex */
345 /* pthread_mutex_lock(area_mutex(area_dst, page_nr)); */
346 while (!bcmp(area_dst + page_nr * page_size, zeropage,
352 /* uncomment below line to test with mutex */
353 /* pthread_mutex_unlock(area_mutex(area_dst, page_nr)); */
356 "page_nr %lu all zero thread %lu %p %lu\n",
357 page_nr, cpu, area_dst + page_nr * page_size,
365 pthread_mutex_lock(area_mutex(area_dst, page_nr));
366 count = *area_count(area_dst, page_nr);
367 if (count != count_verify[page_nr]) {
369 "page_nr %lu memory corruption %Lu %Lu\n",
371 count_verify[page_nr]), exit(1);
374 *area_count(area_dst, page_nr) = count_verify[page_nr] = count;
375 pthread_mutex_unlock(area_mutex(area_dst, page_nr));
377 if (time(NULL) - start > 1)
379 "userfault too slow %ld "
380 "possible false positive with overcommit\n",
387 static void retry_copy_page(int ufd, struct uffdio_copy *uffdio_copy,
388 unsigned long offset)
390 uffd_test_ops->alias_mapping(&uffdio_copy->dst,
393 if (ioctl(ufd, UFFDIO_COPY, uffdio_copy)) {
394 /* real retval in ufdio_copy.copy */
395 if (uffdio_copy->copy != -EEXIST)
396 fprintf(stderr, "UFFDIO_COPY retry error %Ld\n",
397 uffdio_copy->copy), exit(1);
399 fprintf(stderr, "UFFDIO_COPY retry unexpected %Ld\n",
400 uffdio_copy->copy), exit(1);
404 static int __copy_page(int ufd, unsigned long offset, bool retry)
406 struct uffdio_copy uffdio_copy;
408 if (offset >= nr_pages * page_size)
409 fprintf(stderr, "unexpected offset %lu\n",
411 uffdio_copy.dst = (unsigned long) area_dst + offset;
412 uffdio_copy.src = (unsigned long) area_src + offset;
413 uffdio_copy.len = page_size;
414 uffdio_copy.mode = 0;
415 uffdio_copy.copy = 0;
416 if (ioctl(ufd, UFFDIO_COPY, &uffdio_copy)) {
417 /* real retval in ufdio_copy.copy */
418 if (uffdio_copy.copy != -EEXIST)
419 fprintf(stderr, "UFFDIO_COPY error %Ld\n",
420 uffdio_copy.copy), exit(1);
421 } else if (uffdio_copy.copy != page_size) {
422 fprintf(stderr, "UFFDIO_COPY unexpected copy %Ld\n",
423 uffdio_copy.copy), exit(1);
425 if (test_uffdio_copy_eexist && retry) {
426 test_uffdio_copy_eexist = false;
427 retry_copy_page(ufd, &uffdio_copy, offset);
434 static int copy_page_retry(int ufd, unsigned long offset)
436 return __copy_page(ufd, offset, true);
439 static int copy_page(int ufd, unsigned long offset)
441 return __copy_page(ufd, offset, false);
444 static void *uffd_poll_thread(void *arg)
446 unsigned long cpu = (unsigned long) arg;
447 struct pollfd pollfd[2];
449 struct uffdio_register uffd_reg;
451 unsigned long offset;
453 unsigned long userfaults = 0;
456 pollfd[0].events = POLLIN;
457 pollfd[1].fd = pipefd[cpu*2];
458 pollfd[1].events = POLLIN;
461 ret = poll(pollfd, 2, -1);
463 fprintf(stderr, "poll error %d\n", ret), exit(1);
465 perror("poll"), exit(1);
466 if (pollfd[1].revents & POLLIN) {
467 if (read(pollfd[1].fd, &tmp_chr, 1) != 1)
468 fprintf(stderr, "read pipefd error\n"),
472 if (!(pollfd[0].revents & POLLIN))
473 fprintf(stderr, "pollfd[0].revents %d\n",
474 pollfd[0].revents), exit(1);
475 ret = read(uffd, &msg, sizeof(msg));
479 perror("nonblocking read error"), exit(1);
483 fprintf(stderr, "unexpected msg event %u\n",
486 case UFFD_EVENT_PAGEFAULT:
487 if (msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
488 fprintf(stderr, "unexpected write fault\n"), exit(1);
489 offset = (char *)(unsigned long)msg.arg.pagefault.address -
491 offset &= ~(page_size-1);
492 if (copy_page(uffd, offset))
495 case UFFD_EVENT_FORK:
497 uffd = msg.arg.fork.ufd;
500 case UFFD_EVENT_REMOVE:
501 uffd_reg.range.start = msg.arg.remove.start;
502 uffd_reg.range.len = msg.arg.remove.end -
503 msg.arg.remove.start;
504 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffd_reg.range))
505 fprintf(stderr, "remove failure\n"), exit(1);
507 case UFFD_EVENT_REMAP:
508 area_dst = (char *)(unsigned long)msg.arg.remap.to;
512 return (void *)userfaults;
515 pthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER;
517 static void *uffd_read_thread(void *arg)
519 unsigned long *this_cpu_userfaults;
521 unsigned long offset;
524 this_cpu_userfaults = (unsigned long *) arg;
525 *this_cpu_userfaults = 0;
527 pthread_mutex_unlock(&uffd_read_mutex);
528 /* from here cancellation is ok */
531 ret = read(uffd, &msg, sizeof(msg));
532 if (ret != sizeof(msg)) {
534 perror("blocking read error"), exit(1);
536 fprintf(stderr, "short read\n"), exit(1);
538 if (msg.event != UFFD_EVENT_PAGEFAULT)
539 fprintf(stderr, "unexpected msg event %u\n",
541 if (bounces & BOUNCE_VERIFY &&
542 msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
543 fprintf(stderr, "unexpected write fault\n"), exit(1);
544 offset = (char *)(unsigned long)msg.arg.pagefault.address -
546 offset &= ~(page_size-1);
547 if (copy_page(uffd, offset))
548 (*this_cpu_userfaults)++;
553 static void *background_thread(void *arg)
555 unsigned long cpu = (unsigned long) arg;
556 unsigned long page_nr;
558 for (page_nr = cpu * nr_pages_per_cpu;
559 page_nr < (cpu+1) * nr_pages_per_cpu;
561 copy_page_retry(uffd, page_nr * page_size);
566 static int stress(unsigned long *userfaults)
569 pthread_t locking_threads[nr_cpus];
570 pthread_t uffd_threads[nr_cpus];
571 pthread_t background_threads[nr_cpus];
572 void **_userfaults = (void **) userfaults;
575 for (cpu = 0; cpu < nr_cpus; cpu++) {
576 if (pthread_create(&locking_threads[cpu], &attr,
577 locking_thread, (void *)cpu))
579 if (bounces & BOUNCE_POLL) {
580 if (pthread_create(&uffd_threads[cpu], &attr,
581 uffd_poll_thread, (void *)cpu))
584 if (pthread_create(&uffd_threads[cpu], &attr,
588 pthread_mutex_lock(&uffd_read_mutex);
590 if (pthread_create(&background_threads[cpu], &attr,
591 background_thread, (void *)cpu))
594 for (cpu = 0; cpu < nr_cpus; cpu++)
595 if (pthread_join(background_threads[cpu], NULL))
599 * Be strict and immediately zap area_src, the whole area has
600 * been transferred already by the background treads. The
601 * area_src could then be faulted in in a racy way by still
602 * running uffdio_threads reading zeropages after we zapped
603 * area_src (but they're guaranteed to get -EEXIST from
604 * UFFDIO_COPY without writing zero pages into area_dst
605 * because the background threads already completed).
607 if (uffd_test_ops->release_pages(area_src))
610 for (cpu = 0; cpu < nr_cpus; cpu++) {
612 if (bounces & BOUNCE_POLL) {
613 if (write(pipefd[cpu*2+1], &c, 1) != 1) {
614 fprintf(stderr, "pipefd write error\n");
617 if (pthread_join(uffd_threads[cpu], &_userfaults[cpu]))
620 if (pthread_cancel(uffd_threads[cpu]))
622 if (pthread_join(uffd_threads[cpu], NULL))
628 for (cpu = 0; cpu < nr_cpus; cpu++)
629 if (pthread_join(locking_threads[cpu], NULL))
635 static int userfaultfd_open(int features)
637 struct uffdio_api uffdio_api;
639 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
642 "userfaultfd syscall not available in this kernel\n");
645 uffd_flags = fcntl(uffd, F_GETFD, NULL);
647 uffdio_api.api = UFFD_API;
648 uffdio_api.features = features;
649 if (ioctl(uffd, UFFDIO_API, &uffdio_api)) {
650 fprintf(stderr, "UFFDIO_API\n");
653 if (uffdio_api.api != UFFD_API) {
654 fprintf(stderr, "UFFDIO_API error %Lu\n", uffdio_api.api);
661 sigjmp_buf jbuf, *sigbuf;
663 static void sighndl(int sig, siginfo_t *siginfo, void *ptr)
667 siglongjmp(*sigbuf, 1);
673 * For non-cooperative userfaultfd test we fork() a process that will
674 * generate pagefaults, will mremap the area monitored by the
675 * userfaultfd and at last this process will release the monitored
677 * For the anonymous and shared memory the area is divided into two
678 * parts, the first part is accessed before mremap, and the second
679 * part is accessed after mremap. Since hugetlbfs does not support
680 * mremap, the entire monitored area is accessed in a single pass for
682 * The release of the pages currently generates event for shmem and
683 * anonymous memory (UFFD_EVENT_REMOVE), hence it is not checked
685 * For signal test(UFFD_FEATURE_SIGBUS), signal_test = 1, we register
686 * monitored area, generate pagefaults and test that signal is delivered.
687 * Use UFFDIO_COPY to allocate missing page and retry. For signal_test = 2
688 * test robustness use case - we release monitored area, fork a process
689 * that will generate pagefaults and verify signal is generated.
690 * This also tests UFFD_FEATURE_EVENT_FORK event along with the signal
691 * feature. Using monitor thread, verify no userfault events are generated.
693 static int faulting_process(int signal_test)
696 unsigned long long count;
697 unsigned long split_nr_pages;
698 unsigned long lastnr;
699 struct sigaction act;
700 unsigned long signalled = 0;
702 if (test_type != TEST_HUGETLB)
703 split_nr_pages = (nr_pages + 1) / 2;
705 split_nr_pages = nr_pages;
709 memset(&act, 0, sizeof(act));
710 act.sa_sigaction = sighndl;
711 act.sa_flags = SA_SIGINFO;
712 if (sigaction(SIGBUS, &act, 0)) {
716 lastnr = (unsigned long)-1;
719 for (nr = 0; nr < split_nr_pages; nr++) {
721 if (sigsetjmp(*sigbuf, 1) != 0) {
723 fprintf(stderr, "Signal repeated\n");
728 if (signal_test == 1) {
729 if (copy_page(uffd, nr * page_size))
738 count = *area_count(area_dst, nr);
739 if (count != count_verify[nr]) {
741 "nr %lu memory corruption %Lu %Lu\n",
743 count_verify[nr]), exit(1);
748 return signalled != split_nr_pages;
750 if (test_type == TEST_HUGETLB)
753 area_dst = mremap(area_dst, nr_pages * page_size, nr_pages * page_size,
754 MREMAP_MAYMOVE | MREMAP_FIXED, area_src);
755 if (area_dst == MAP_FAILED)
756 perror("mremap"), exit(1);
758 for (; nr < nr_pages; nr++) {
759 count = *area_count(area_dst, nr);
760 if (count != count_verify[nr]) {
762 "nr %lu memory corruption %Lu %Lu\n",
764 count_verify[nr]), exit(1);
768 if (uffd_test_ops->release_pages(area_dst))
771 for (nr = 0; nr < nr_pages; nr++) {
772 if (my_bcmp(area_dst + nr * page_size, zeropage, page_size))
773 fprintf(stderr, "nr %lu is not zero\n", nr), exit(1);
779 static void retry_uffdio_zeropage(int ufd,
780 struct uffdio_zeropage *uffdio_zeropage,
781 unsigned long offset)
783 uffd_test_ops->alias_mapping(&uffdio_zeropage->range.start,
784 uffdio_zeropage->range.len,
786 if (ioctl(ufd, UFFDIO_ZEROPAGE, uffdio_zeropage)) {
787 if (uffdio_zeropage->zeropage != -EEXIST)
788 fprintf(stderr, "UFFDIO_ZEROPAGE retry error %Ld\n",
789 uffdio_zeropage->zeropage), exit(1);
791 fprintf(stderr, "UFFDIO_ZEROPAGE retry unexpected %Ld\n",
792 uffdio_zeropage->zeropage), exit(1);
796 static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
798 struct uffdio_zeropage uffdio_zeropage;
800 unsigned long has_zeropage;
802 has_zeropage = uffd_test_ops->expected_ioctls & (1 << _UFFDIO_ZEROPAGE);
804 if (offset >= nr_pages * page_size)
805 fprintf(stderr, "unexpected offset %lu\n",
807 uffdio_zeropage.range.start = (unsigned long) area_dst + offset;
808 uffdio_zeropage.range.len = page_size;
809 uffdio_zeropage.mode = 0;
810 ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage);
812 /* real retval in ufdio_zeropage.zeropage */
814 if (uffdio_zeropage.zeropage == -EEXIST)
815 fprintf(stderr, "UFFDIO_ZEROPAGE -EEXIST\n"),
818 fprintf(stderr, "UFFDIO_ZEROPAGE error %Ld\n",
819 uffdio_zeropage.zeropage), exit(1);
821 if (uffdio_zeropage.zeropage != -EINVAL)
823 "UFFDIO_ZEROPAGE not -EINVAL %Ld\n",
824 uffdio_zeropage.zeropage), exit(1);
826 } else if (has_zeropage) {
827 if (uffdio_zeropage.zeropage != page_size) {
828 fprintf(stderr, "UFFDIO_ZEROPAGE unexpected %Ld\n",
829 uffdio_zeropage.zeropage), exit(1);
831 if (test_uffdio_zeropage_eexist && retry) {
832 test_uffdio_zeropage_eexist = false;
833 retry_uffdio_zeropage(ufd, &uffdio_zeropage,
840 "UFFDIO_ZEROPAGE succeeded %Ld\n",
841 uffdio_zeropage.zeropage), exit(1);
847 static int uffdio_zeropage(int ufd, unsigned long offset)
849 return __uffdio_zeropage(ufd, offset, false);
852 /* exercise UFFDIO_ZEROPAGE */
853 static int userfaultfd_zeropage_test(void)
855 struct uffdio_register uffdio_register;
856 unsigned long expected_ioctls;
858 printf("testing UFFDIO_ZEROPAGE: ");
861 if (uffd_test_ops->release_pages(area_dst))
864 if (userfaultfd_open(0) < 0)
866 uffdio_register.range.start = (unsigned long) area_dst;
867 uffdio_register.range.len = nr_pages * page_size;
868 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
869 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
870 fprintf(stderr, "register failure\n"), exit(1);
872 expected_ioctls = uffd_test_ops->expected_ioctls;
873 if ((uffdio_register.ioctls & expected_ioctls) !=
876 "unexpected missing ioctl for anon memory\n"),
879 if (uffdio_zeropage(uffd, 0)) {
880 if (my_bcmp(area_dst, zeropage, page_size))
881 fprintf(stderr, "zeropage is not zero\n"), exit(1);
889 static int userfaultfd_events_test(void)
891 struct uffdio_register uffdio_register;
892 unsigned long expected_ioctls;
893 unsigned long userfaults;
899 printf("testing events (fork, remap, remove): ");
902 if (uffd_test_ops->release_pages(area_dst))
905 features = UFFD_FEATURE_EVENT_FORK | UFFD_FEATURE_EVENT_REMAP |
906 UFFD_FEATURE_EVENT_REMOVE;
907 if (userfaultfd_open(features) < 0)
909 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
911 uffdio_register.range.start = (unsigned long) area_dst;
912 uffdio_register.range.len = nr_pages * page_size;
913 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
914 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
915 fprintf(stderr, "register failure\n"), exit(1);
917 expected_ioctls = uffd_test_ops->expected_ioctls;
918 if ((uffdio_register.ioctls & expected_ioctls) !=
921 "unexpected missing ioctl for anon memory\n"),
924 if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
925 perror("uffd_poll_thread create"), exit(1);
929 perror("fork"), exit(1);
932 return faulting_process(0);
934 waitpid(pid, &err, 0);
936 fprintf(stderr, "faulting process failed\n"), exit(1);
938 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
939 perror("pipe write"), exit(1);
940 if (pthread_join(uffd_mon, (void **)&userfaults))
944 printf("userfaults: %ld\n", userfaults);
946 return userfaults != nr_pages;
949 static int userfaultfd_sig_test(void)
951 struct uffdio_register uffdio_register;
952 unsigned long expected_ioctls;
953 unsigned long userfaults;
959 printf("testing signal delivery: ");
962 if (uffd_test_ops->release_pages(area_dst))
965 features = UFFD_FEATURE_EVENT_FORK|UFFD_FEATURE_SIGBUS;
966 if (userfaultfd_open(features) < 0)
968 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
970 uffdio_register.range.start = (unsigned long) area_dst;
971 uffdio_register.range.len = nr_pages * page_size;
972 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
973 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
974 fprintf(stderr, "register failure\n"), exit(1);
976 expected_ioctls = uffd_test_ops->expected_ioctls;
977 if ((uffdio_register.ioctls & expected_ioctls) !=
980 "unexpected missing ioctl for anon memory\n"),
983 if (faulting_process(1))
984 fprintf(stderr, "faulting process failed\n"), exit(1);
986 if (uffd_test_ops->release_pages(area_dst))
989 if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
990 perror("uffd_poll_thread create"), exit(1);
994 perror("fork"), exit(1);
997 exit(faulting_process(2));
999 waitpid(pid, &err, 0);
1001 fprintf(stderr, "faulting process failed\n"), exit(1);
1003 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
1004 perror("pipe write"), exit(1);
1005 if (pthread_join(uffd_mon, (void **)&userfaults))
1010 fprintf(stderr, "Signal test failed, userfaults: %ld\n",
1013 return userfaults != 0;
1015 static int userfaultfd_stress(void)
1020 struct uffdio_register uffdio_register;
1023 unsigned long userfaults[nr_cpus];
1025 uffd_test_ops->allocate_area((void **)&area_src);
1028 uffd_test_ops->allocate_area((void **)&area_dst);
1032 if (userfaultfd_open(0) < 0)
1035 count_verify = malloc(nr_pages * sizeof(unsigned long long));
1036 if (!count_verify) {
1037 perror("count_verify");
1041 for (nr = 0; nr < nr_pages; nr++) {
1042 *area_mutex(area_src, nr) = (pthread_mutex_t)
1043 PTHREAD_MUTEX_INITIALIZER;
1044 count_verify[nr] = *area_count(area_src, nr) = 1;
1046 * In the transition between 255 to 256, powerpc will
1047 * read out of order in my_bcmp and see both bytes as
1048 * zero, so leave a placeholder below always non-zero
1049 * after the count, to avoid my_bcmp to trigger false
1052 *(area_count(area_src, nr) + 1) = 1;
1055 pipefd = malloc(sizeof(int) * nr_cpus * 2);
1060 for (cpu = 0; cpu < nr_cpus; cpu++) {
1061 if (pipe2(&pipefd[cpu*2], O_CLOEXEC | O_NONBLOCK)) {
1067 if (posix_memalign(&area, page_size, page_size)) {
1068 fprintf(stderr, "out of memory\n");
1072 bzero(zeropage, page_size);
1074 pthread_mutex_lock(&uffd_read_mutex);
1076 pthread_attr_init(&attr);
1077 pthread_attr_setstacksize(&attr, 16*1024*1024);
1081 unsigned long expected_ioctls;
1083 printf("bounces: %d, mode:", bounces);
1084 if (bounces & BOUNCE_RANDOM)
1086 if (bounces & BOUNCE_RACINGFAULTS)
1088 if (bounces & BOUNCE_VERIFY)
1090 if (bounces & BOUNCE_POLL)
1095 if (bounces & BOUNCE_POLL)
1096 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
1098 fcntl(uffd, F_SETFL, uffd_flags & ~O_NONBLOCK);
1101 uffdio_register.range.start = (unsigned long) area_dst;
1102 uffdio_register.range.len = nr_pages * page_size;
1103 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
1104 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
1105 fprintf(stderr, "register failure\n");
1108 expected_ioctls = uffd_test_ops->expected_ioctls;
1109 if ((uffdio_register.ioctls & expected_ioctls) !=
1112 "unexpected missing ioctl for anon memory\n");
1116 if (area_dst_alias) {
1117 uffdio_register.range.start = (unsigned long)
1119 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
1120 fprintf(stderr, "register failure alias\n");
1126 * The madvise done previously isn't enough: some
1127 * uffd_thread could have read userfaults (one of
1128 * those already resolved by the background thread)
1129 * and it may be in the process of calling
1130 * UFFDIO_COPY. UFFDIO_COPY will read the zapped
1131 * area_src and it would map a zero page in it (of
1132 * course such a UFFDIO_COPY is perfectly safe as it'd
1133 * return -EEXIST). The problem comes at the next
1134 * bounce though: that racing UFFDIO_COPY would
1135 * generate zeropages in the area_src, so invalidating
1136 * the previous MADV_DONTNEED. Without this additional
1137 * MADV_DONTNEED those zeropages leftovers in the
1138 * area_src would lead to -EEXIST failure during the
1139 * next bounce, effectively leaving a zeropage in the
1142 * Try to comment this out madvise to see the memory
1143 * corruption being caught pretty quick.
1145 * khugepaged is also inhibited to collapse THP after
1146 * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's
1147 * required to MADV_DONTNEED here.
1149 if (uffd_test_ops->release_pages(area_dst))
1153 if (stress(userfaults))
1157 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) {
1158 fprintf(stderr, "unregister failure\n");
1161 if (area_dst_alias) {
1162 uffdio_register.range.start = (unsigned long) area_dst;
1163 if (ioctl(uffd, UFFDIO_UNREGISTER,
1164 &uffdio_register.range)) {
1165 fprintf(stderr, "unregister failure alias\n");
1171 if (bounces & BOUNCE_VERIFY) {
1172 for (nr = 0; nr < nr_pages; nr++) {
1173 if (*area_count(area_dst, nr) != count_verify[nr]) {
1175 "error area_count %Lu %Lu %lu\n",
1176 *area_count(area_src, nr),
1185 /* prepare next bounce */
1186 tmp_area = area_src;
1187 area_src = area_dst;
1188 area_dst = tmp_area;
1190 tmp_area = area_src_alias;
1191 area_src_alias = area_dst_alias;
1192 area_dst_alias = tmp_area;
1194 printf("userfaults:");
1195 for (cpu = 0; cpu < nr_cpus; cpu++)
1196 printf(" %lu", userfaults[cpu]);
1204 return userfaultfd_zeropage_test() || userfaultfd_sig_test()
1205 || userfaultfd_events_test();
1209 * Copied from mlock2-tests.c
1211 unsigned long default_huge_page_size(void)
1213 unsigned long hps = 0;
1216 FILE *f = fopen("/proc/meminfo", "r");
1220 while (getline(&line, &linelen, f) > 0) {
1221 if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
1232 static void set_test_type(const char *type)
1234 if (!strcmp(type, "anon")) {
1235 test_type = TEST_ANON;
1236 uffd_test_ops = &anon_uffd_test_ops;
1237 } else if (!strcmp(type, "hugetlb")) {
1238 test_type = TEST_HUGETLB;
1239 uffd_test_ops = &hugetlb_uffd_test_ops;
1240 } else if (!strcmp(type, "hugetlb_shared")) {
1242 test_type = TEST_HUGETLB;
1243 uffd_test_ops = &hugetlb_uffd_test_ops;
1244 } else if (!strcmp(type, "shmem")) {
1246 test_type = TEST_SHMEM;
1247 uffd_test_ops = &shmem_uffd_test_ops;
1249 fprintf(stderr, "Unknown test type: %s\n", type), exit(1);
1252 if (test_type == TEST_HUGETLB)
1253 page_size = default_huge_page_size();
1255 page_size = sysconf(_SC_PAGE_SIZE);
1258 fprintf(stderr, "Unable to determine page size\n"),
1260 if ((unsigned long) area_count(NULL, 0) + sizeof(unsigned long long) * 2
1262 fprintf(stderr, "Impossible to run this test\n"), exit(2);
1265 static void sigalrm(int sig)
1269 test_uffdio_copy_eexist = true;
1270 test_uffdio_zeropage_eexist = true;
1271 alarm(ALARM_INTERVAL_SECS);
1274 int main(int argc, char **argv)
1277 fprintf(stderr, "Usage: <test type> <MiB> <bounces> [hugetlbfs_file]\n"),
1280 if (signal(SIGALRM, sigalrm) == SIG_ERR)
1281 fprintf(stderr, "failed to arm SIGALRM"), exit(1);
1282 alarm(ALARM_INTERVAL_SECS);
1284 set_test_type(argv[1]);
1286 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1287 nr_pages_per_cpu = atol(argv[2]) * 1024*1024 / page_size /
1289 if (!nr_pages_per_cpu) {
1290 fprintf(stderr, "invalid MiB\n");
1291 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
1294 bounces = atoi(argv[3]);
1296 fprintf(stderr, "invalid bounces\n");
1297 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
1299 nr_pages = nr_pages_per_cpu * nr_cpus;
1301 if (test_type == TEST_HUGETLB) {
1303 fprintf(stderr, "Usage: hugetlb <MiB> <bounces> <hugetlbfs_file>\n"),
1305 huge_fd = open(argv[4], O_CREAT | O_RDWR, 0755);
1307 fprintf(stderr, "Open of %s failed", argv[3]);
1311 if (ftruncate(huge_fd, 0)) {
1312 fprintf(stderr, "ftruncate %s to size 0 failed", argv[3]);
1313 perror("ftruncate");
1317 printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
1318 nr_pages, nr_pages_per_cpu);
1319 return userfaultfd_stress();
1322 #else /* __NR_userfaultfd */
1324 #warning "missing __NR_userfaultfd definition"
1328 printf("skip: Skipping userfaultfd test (missing __NR_userfaultfd)\n");
1332 #endif /* __NR_userfaultfd */