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>
70 #ifdef __NR_userfaultfd
72 static unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;
74 #define BOUNCE_RANDOM (1<<0)
75 #define BOUNCE_RACINGFAULTS (1<<1)
76 #define BOUNCE_VERIFY (1<<2)
77 #define BOUNCE_POLL (1<<3)
82 static char *huge_fd_off0;
84 static unsigned long long *count_verify;
85 static int uffd, uffd_flags, finished, *pipefd;
86 static char *area_src, *area_dst;
87 static char *zeropage;
90 /* pthread_mutex_t starts at page offset 0 */
91 #define area_mutex(___area, ___nr) \
92 ((pthread_mutex_t *) ((___area) + (___nr)*page_size))
94 * count is placed in the page after pthread_mutex_t naturally aligned
95 * to avoid non alignment faults on non-x86 archs.
97 #define area_count(___area, ___nr) \
98 ((volatile unsigned long long *) ((unsigned long) \
99 ((___area) + (___nr)*page_size + \
100 sizeof(pthread_mutex_t) + \
101 sizeof(unsigned long long) - 1) & \
102 ~(unsigned long)(sizeof(unsigned long long) \
105 #if !defined(HUGETLB_TEST) && !defined(SHMEM_TEST)
107 /* Anonymous memory */
108 #define EXPECTED_IOCTLS ((1 << _UFFDIO_WAKE) | \
109 (1 << _UFFDIO_COPY) | \
110 (1 << _UFFDIO_ZEROPAGE))
112 static int release_pages(char *rel_area)
116 if (madvise(rel_area, nr_pages * page_size, MADV_DONTNEED)) {
124 static void allocate_area(void **alloc_area)
126 if (posix_memalign(alloc_area, page_size, nr_pages * page_size)) {
127 fprintf(stderr, "out of memory\n");
132 #else /* HUGETLB_TEST or SHMEM_TEST */
134 #define EXPECTED_IOCTLS UFFD_API_RANGE_IOCTLS_BASIC
139 static int release_pages(char *rel_area)
143 if (fallocate(huge_fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
144 rel_area == huge_fd_off0 ? 0 :
145 nr_pages * page_size,
146 nr_pages * page_size)) {
155 static void allocate_area(void **alloc_area)
157 *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
158 MAP_PRIVATE | MAP_HUGETLB, huge_fd,
159 *alloc_area == area_src ? 0 :
160 nr_pages * page_size);
161 if (*alloc_area == MAP_FAILED) {
162 fprintf(stderr, "mmap of hugetlbfs file failed\n");
166 if (*alloc_area == area_src)
167 huge_fd_off0 = *alloc_area;
170 #elif defined(SHMEM_TEST)
173 static int release_pages(char *rel_area)
177 if (madvise(rel_area, nr_pages * page_size, MADV_REMOVE)) {
185 static void allocate_area(void **alloc_area)
187 *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
188 MAP_ANONYMOUS | MAP_SHARED, -1, 0);
189 if (*alloc_area == MAP_FAILED) {
190 fprintf(stderr, "shared memory mmap failed\n");
195 #else /* SHMEM_TEST */
196 #error "Undefined test type"
197 #endif /* HUGETLB_TEST */
199 #endif /* !defined(HUGETLB_TEST) && !defined(SHMEM_TEST) */
201 static int my_bcmp(char *str1, char *str2, size_t n)
204 for (i = 0; i < n; i++)
205 if (str1[i] != str2[i])
210 static void *locking_thread(void *arg)
212 unsigned long cpu = (unsigned long) arg;
213 struct random_data rand;
214 unsigned long page_nr = *(&(page_nr)); /* uninitialized warning */
216 unsigned long long count;
221 if (bounces & BOUNCE_RANDOM) {
222 seed = (unsigned int) time(NULL) - bounces;
223 if (!(bounces & BOUNCE_RACINGFAULTS))
225 bzero(&rand, sizeof(rand));
226 bzero(&randstate, sizeof(randstate));
227 if (initstate_r(seed, randstate, sizeof(randstate), &rand))
228 fprintf(stderr, "srandom_r error\n"), exit(1);
231 if (!(bounces & BOUNCE_RACINGFAULTS))
232 page_nr += cpu * nr_pages_per_cpu;
236 if (bounces & BOUNCE_RANDOM) {
237 if (random_r(&rand, &rand_nr))
238 fprintf(stderr, "random_r 1 error\n"), exit(1);
240 if (sizeof(page_nr) > sizeof(rand_nr)) {
241 if (random_r(&rand, &rand_nr))
242 fprintf(stderr, "random_r 2 error\n"), exit(1);
243 page_nr |= (((unsigned long) rand_nr) << 16) <<
251 if (bounces & BOUNCE_VERIFY) {
252 count = *area_count(area_dst, page_nr);
255 "page_nr %lu wrong count %Lu %Lu\n",
257 count_verify[page_nr]), exit(1);
261 * We can't use bcmp (or memcmp) because that
262 * returns 0 erroneously if the memory is
263 * changing under it (even if the end of the
264 * page is never changing and always
268 if (!my_bcmp(area_dst + page_nr * page_size, zeropage,
271 "my_bcmp page_nr %lu wrong count %Lu %Lu\n",
273 count_verify[page_nr]), exit(1);
278 /* uncomment the below line to test with mutex */
279 /* pthread_mutex_lock(area_mutex(area_dst, page_nr)); */
280 while (!bcmp(area_dst + page_nr * page_size, zeropage,
286 /* uncomment below line to test with mutex */
287 /* pthread_mutex_unlock(area_mutex(area_dst, page_nr)); */
290 "page_nr %lu all zero thread %lu %p %lu\n",
291 page_nr, cpu, area_dst + page_nr * page_size,
299 pthread_mutex_lock(area_mutex(area_dst, page_nr));
300 count = *area_count(area_dst, page_nr);
301 if (count != count_verify[page_nr]) {
303 "page_nr %lu memory corruption %Lu %Lu\n",
305 count_verify[page_nr]), exit(1);
308 *area_count(area_dst, page_nr) = count_verify[page_nr] = count;
309 pthread_mutex_unlock(area_mutex(area_dst, page_nr));
311 if (time(NULL) - start > 1)
313 "userfault too slow %ld "
314 "possible false positive with overcommit\n",
321 static int copy_page(int ufd, unsigned long offset)
323 struct uffdio_copy uffdio_copy;
325 if (offset >= nr_pages * page_size)
326 fprintf(stderr, "unexpected offset %lu\n",
328 uffdio_copy.dst = (unsigned long) area_dst + offset;
329 uffdio_copy.src = (unsigned long) area_src + offset;
330 uffdio_copy.len = page_size;
331 uffdio_copy.mode = 0;
332 uffdio_copy.copy = 0;
333 if (ioctl(ufd, UFFDIO_COPY, &uffdio_copy)) {
334 /* real retval in ufdio_copy.copy */
335 if (uffdio_copy.copy != -EEXIST)
336 fprintf(stderr, "UFFDIO_COPY error %Ld\n",
337 uffdio_copy.copy), exit(1);
338 } else if (uffdio_copy.copy != page_size) {
339 fprintf(stderr, "UFFDIO_COPY unexpected copy %Ld\n",
340 uffdio_copy.copy), exit(1);
346 static void *uffd_poll_thread(void *arg)
348 unsigned long cpu = (unsigned long) arg;
349 struct pollfd pollfd[2];
351 struct uffdio_register uffd_reg;
353 unsigned long offset;
355 unsigned long userfaults = 0;
358 pollfd[0].events = POLLIN;
359 pollfd[1].fd = pipefd[cpu*2];
360 pollfd[1].events = POLLIN;
363 ret = poll(pollfd, 2, -1);
365 fprintf(stderr, "poll error %d\n", ret), exit(1);
367 perror("poll"), exit(1);
368 if (pollfd[1].revents & POLLIN) {
369 if (read(pollfd[1].fd, &tmp_chr, 1) != 1)
370 fprintf(stderr, "read pipefd error\n"),
374 if (!(pollfd[0].revents & POLLIN))
375 fprintf(stderr, "pollfd[0].revents %d\n",
376 pollfd[0].revents), exit(1);
377 ret = read(uffd, &msg, sizeof(msg));
381 perror("nonblocking read error"), exit(1);
385 fprintf(stderr, "unexpected msg event %u\n",
388 case UFFD_EVENT_PAGEFAULT:
389 if (msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
390 fprintf(stderr, "unexpected write fault\n"), exit(1);
391 offset = (char *)(unsigned long)msg.arg.pagefault.address -
393 offset &= ~(page_size-1);
394 if (copy_page(uffd, offset))
397 case UFFD_EVENT_FORK:
398 uffd = msg.arg.fork.ufd;
401 case UFFD_EVENT_REMOVE:
402 uffd_reg.range.start = msg.arg.remove.start;
403 uffd_reg.range.len = msg.arg.remove.end -
404 msg.arg.remove.start;
405 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffd_reg.range))
406 fprintf(stderr, "remove failure\n"), exit(1);
408 case UFFD_EVENT_REMAP:
409 area_dst = (char *)(unsigned long)msg.arg.remap.to;
413 return (void *)userfaults;
416 pthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER;
418 static void *uffd_read_thread(void *arg)
420 unsigned long *this_cpu_userfaults;
422 unsigned long offset;
425 this_cpu_userfaults = (unsigned long *) arg;
426 *this_cpu_userfaults = 0;
428 pthread_mutex_unlock(&uffd_read_mutex);
429 /* from here cancellation is ok */
432 ret = read(uffd, &msg, sizeof(msg));
433 if (ret != sizeof(msg)) {
435 perror("blocking read error"), exit(1);
437 fprintf(stderr, "short read\n"), exit(1);
439 if (msg.event != UFFD_EVENT_PAGEFAULT)
440 fprintf(stderr, "unexpected msg event %u\n",
442 if (bounces & BOUNCE_VERIFY &&
443 msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
444 fprintf(stderr, "unexpected write fault\n"), exit(1);
445 offset = (char *)(unsigned long)msg.arg.pagefault.address -
447 offset &= ~(page_size-1);
448 if (copy_page(uffd, offset))
449 (*this_cpu_userfaults)++;
454 static void *background_thread(void *arg)
456 unsigned long cpu = (unsigned long) arg;
457 unsigned long page_nr;
459 for (page_nr = cpu * nr_pages_per_cpu;
460 page_nr < (cpu+1) * nr_pages_per_cpu;
462 copy_page(uffd, page_nr * page_size);
467 static int stress(unsigned long *userfaults)
470 pthread_t locking_threads[nr_cpus];
471 pthread_t uffd_threads[nr_cpus];
472 pthread_t background_threads[nr_cpus];
473 void **_userfaults = (void **) userfaults;
476 for (cpu = 0; cpu < nr_cpus; cpu++) {
477 if (pthread_create(&locking_threads[cpu], &attr,
478 locking_thread, (void *)cpu))
480 if (bounces & BOUNCE_POLL) {
481 if (pthread_create(&uffd_threads[cpu], &attr,
482 uffd_poll_thread, (void *)cpu))
485 if (pthread_create(&uffd_threads[cpu], &attr,
489 pthread_mutex_lock(&uffd_read_mutex);
491 if (pthread_create(&background_threads[cpu], &attr,
492 background_thread, (void *)cpu))
495 for (cpu = 0; cpu < nr_cpus; cpu++)
496 if (pthread_join(background_threads[cpu], NULL))
500 * Be strict and immediately zap area_src, the whole area has
501 * been transferred already by the background treads. The
502 * area_src could then be faulted in in a racy way by still
503 * running uffdio_threads reading zeropages after we zapped
504 * area_src (but they're guaranteed to get -EEXIST from
505 * UFFDIO_COPY without writing zero pages into area_dst
506 * because the background threads already completed).
508 if (release_pages(area_src))
511 for (cpu = 0; cpu < nr_cpus; cpu++) {
513 if (bounces & BOUNCE_POLL) {
514 if (write(pipefd[cpu*2+1], &c, 1) != 1) {
515 fprintf(stderr, "pipefd write error\n");
518 if (pthread_join(uffd_threads[cpu], &_userfaults[cpu]))
521 if (pthread_cancel(uffd_threads[cpu]))
523 if (pthread_join(uffd_threads[cpu], NULL))
529 for (cpu = 0; cpu < nr_cpus; cpu++)
530 if (pthread_join(locking_threads[cpu], NULL))
536 static int userfaultfd_open(int features)
538 struct uffdio_api uffdio_api;
540 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
543 "userfaultfd syscall not available in this kernel\n");
546 uffd_flags = fcntl(uffd, F_GETFD, NULL);
548 uffdio_api.api = UFFD_API;
549 uffdio_api.features = features;
550 if (ioctl(uffd, UFFDIO_API, &uffdio_api)) {
551 fprintf(stderr, "UFFDIO_API\n");
554 if (uffdio_api.api != UFFD_API) {
555 fprintf(stderr, "UFFDIO_API error %Lu\n", uffdio_api.api);
563 * For non-cooperative userfaultfd test we fork() a process that will
564 * generate pagefaults, will mremap the area monitored by the
565 * userfaultfd and at last this process will release the monitored
567 * For the anonymous and shared memory the area is divided into two
568 * parts, the first part is accessed before mremap, and the second
569 * part is accessed after mremap. Since hugetlbfs does not support
570 * mremap, the entire monitored area is accessed in a single pass for
572 * The release of the pages currently generates event only for
573 * anonymous memory (UFFD_EVENT_REMOVE), hence it is not checked
574 * for hugetlb and shmem.
576 static int faulting_process(void)
579 unsigned long long count;
582 unsigned long split_nr_pages = (nr_pages + 1) / 2;
584 unsigned long split_nr_pages = nr_pages;
587 for (nr = 0; nr < split_nr_pages; nr++) {
588 count = *area_count(area_dst, nr);
589 if (count != count_verify[nr]) {
591 "nr %lu memory corruption %Lu %Lu\n",
593 count_verify[nr]), exit(1);
598 area_dst = mremap(area_dst, nr_pages * page_size, nr_pages * page_size,
599 MREMAP_MAYMOVE | MREMAP_FIXED, area_src);
600 if (area_dst == MAP_FAILED)
601 perror("mremap"), exit(1);
603 for (; nr < nr_pages; nr++) {
604 count = *area_count(area_dst, nr);
605 if (count != count_verify[nr]) {
607 "nr %lu memory corruption %Lu %Lu\n",
609 count_verify[nr]), exit(1);
614 if (release_pages(area_dst))
617 for (nr = 0; nr < nr_pages; nr++) {
618 if (my_bcmp(area_dst + nr * page_size, zeropage, page_size))
619 fprintf(stderr, "nr %lu is not zero\n", nr), exit(1);
621 #endif /* SHMEM_TEST */
623 #endif /* HUGETLB_TEST */
628 static int uffdio_zeropage(int ufd, unsigned long offset)
630 struct uffdio_zeropage uffdio_zeropage;
632 unsigned long has_zeropage = EXPECTED_IOCTLS & (1 << _UFFDIO_ZEROPAGE);
634 if (offset >= nr_pages * page_size)
635 fprintf(stderr, "unexpected offset %lu\n",
637 uffdio_zeropage.range.start = (unsigned long) area_dst + offset;
638 uffdio_zeropage.range.len = page_size;
639 uffdio_zeropage.mode = 0;
640 ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage);
642 /* real retval in ufdio_zeropage.zeropage */
644 if (uffdio_zeropage.zeropage == -EEXIST)
645 fprintf(stderr, "UFFDIO_ZEROPAGE -EEXIST\n"),
648 fprintf(stderr, "UFFDIO_ZEROPAGE error %Ld\n",
649 uffdio_zeropage.zeropage), exit(1);
651 if (uffdio_zeropage.zeropage != -EINVAL)
653 "UFFDIO_ZEROPAGE not -EINVAL %Ld\n",
654 uffdio_zeropage.zeropage), exit(1);
656 } else if (has_zeropage) {
657 if (uffdio_zeropage.zeropage != page_size) {
658 fprintf(stderr, "UFFDIO_ZEROPAGE unexpected %Ld\n",
659 uffdio_zeropage.zeropage), exit(1);
664 "UFFDIO_ZEROPAGE succeeded %Ld\n",
665 uffdio_zeropage.zeropage), exit(1);
671 /* exercise UFFDIO_ZEROPAGE */
672 static int userfaultfd_zeropage_test(void)
674 struct uffdio_register uffdio_register;
675 unsigned long expected_ioctls;
677 printf("testing UFFDIO_ZEROPAGE: ");
680 if (release_pages(area_dst))
683 if (userfaultfd_open(0) < 0)
685 uffdio_register.range.start = (unsigned long) area_dst;
686 uffdio_register.range.len = nr_pages * page_size;
687 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
688 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
689 fprintf(stderr, "register failure\n"), exit(1);
691 expected_ioctls = EXPECTED_IOCTLS;
692 if ((uffdio_register.ioctls & expected_ioctls) !=
695 "unexpected missing ioctl for anon memory\n"),
698 if (uffdio_zeropage(uffd, 0)) {
699 if (my_bcmp(area_dst, zeropage, page_size))
700 fprintf(stderr, "zeropage is not zero\n"), exit(1);
708 static int userfaultfd_events_test(void)
710 struct uffdio_register uffdio_register;
711 unsigned long expected_ioctls;
712 unsigned long userfaults;
718 printf("testing events (fork, remap, remove): ");
721 if (release_pages(area_dst))
724 features = UFFD_FEATURE_EVENT_FORK | UFFD_FEATURE_EVENT_REMAP |
725 UFFD_FEATURE_EVENT_REMOVE;
726 if (userfaultfd_open(features) < 0)
728 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
730 uffdio_register.range.start = (unsigned long) area_dst;
731 uffdio_register.range.len = nr_pages * page_size;
732 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
733 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
734 fprintf(stderr, "register failure\n"), exit(1);
736 expected_ioctls = EXPECTED_IOCTLS;
737 if ((uffdio_register.ioctls & expected_ioctls) !=
740 "unexpected missing ioctl for anon memory\n"),
743 if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
744 perror("uffd_poll_thread create"), exit(1);
748 perror("fork"), exit(1);
751 return faulting_process();
753 waitpid(pid, &err, 0);
755 fprintf(stderr, "faulting process failed\n"), exit(1);
757 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
758 perror("pipe write"), exit(1);
759 if (pthread_join(uffd_mon, (void **)&userfaults))
763 printf("userfaults: %ld\n", userfaults);
765 return userfaults != nr_pages;
768 static int userfaultfd_stress(void)
773 struct uffdio_register uffdio_register;
776 unsigned long userfaults[nr_cpus];
778 allocate_area((void **)&area_src);
781 allocate_area((void **)&area_dst);
785 if (userfaultfd_open(0) < 0)
788 count_verify = malloc(nr_pages * sizeof(unsigned long long));
790 perror("count_verify");
794 for (nr = 0; nr < nr_pages; nr++) {
795 *area_mutex(area_src, nr) = (pthread_mutex_t)
796 PTHREAD_MUTEX_INITIALIZER;
797 count_verify[nr] = *area_count(area_src, nr) = 1;
799 * In the transition between 255 to 256, powerpc will
800 * read out of order in my_bcmp and see both bytes as
801 * zero, so leave a placeholder below always non-zero
802 * after the count, to avoid my_bcmp to trigger false
805 *(area_count(area_src, nr) + 1) = 1;
808 pipefd = malloc(sizeof(int) * nr_cpus * 2);
813 for (cpu = 0; cpu < nr_cpus; cpu++) {
814 if (pipe2(&pipefd[cpu*2], O_CLOEXEC | O_NONBLOCK)) {
820 if (posix_memalign(&area, page_size, page_size)) {
821 fprintf(stderr, "out of memory\n");
825 bzero(zeropage, page_size);
827 pthread_mutex_lock(&uffd_read_mutex);
829 pthread_attr_init(&attr);
830 pthread_attr_setstacksize(&attr, 16*1024*1024);
834 unsigned long expected_ioctls;
836 printf("bounces: %d, mode:", bounces);
837 if (bounces & BOUNCE_RANDOM)
839 if (bounces & BOUNCE_RACINGFAULTS)
841 if (bounces & BOUNCE_VERIFY)
843 if (bounces & BOUNCE_POLL)
848 if (bounces & BOUNCE_POLL)
849 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
851 fcntl(uffd, F_SETFL, uffd_flags & ~O_NONBLOCK);
854 uffdio_register.range.start = (unsigned long) area_dst;
855 uffdio_register.range.len = nr_pages * page_size;
856 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
857 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
858 fprintf(stderr, "register failure\n");
861 expected_ioctls = EXPECTED_IOCTLS;
862 if ((uffdio_register.ioctls & expected_ioctls) !=
865 "unexpected missing ioctl for anon memory\n");
870 * The madvise done previously isn't enough: some
871 * uffd_thread could have read userfaults (one of
872 * those already resolved by the background thread)
873 * and it may be in the process of calling
874 * UFFDIO_COPY. UFFDIO_COPY will read the zapped
875 * area_src and it would map a zero page in it (of
876 * course such a UFFDIO_COPY is perfectly safe as it'd
877 * return -EEXIST). The problem comes at the next
878 * bounce though: that racing UFFDIO_COPY would
879 * generate zeropages in the area_src, so invalidating
880 * the previous MADV_DONTNEED. Without this additional
881 * MADV_DONTNEED those zeropages leftovers in the
882 * area_src would lead to -EEXIST failure during the
883 * next bounce, effectively leaving a zeropage in the
886 * Try to comment this out madvise to see the memory
887 * corruption being caught pretty quick.
889 * khugepaged is also inhibited to collapse THP after
890 * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's
891 * required to MADV_DONTNEED here.
893 if (release_pages(area_dst))
897 if (stress(userfaults))
901 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) {
902 fprintf(stderr, "register failure\n");
907 if (bounces & BOUNCE_VERIFY) {
908 for (nr = 0; nr < nr_pages; nr++) {
909 if (*area_count(area_dst, nr) != count_verify[nr]) {
911 "error area_count %Lu %Lu %lu\n",
912 *area_count(area_src, nr),
921 /* prepare next bounce */
926 printf("userfaults:");
927 for (cpu = 0; cpu < nr_cpus; cpu++)
928 printf(" %lu", userfaults[cpu]);
936 return userfaultfd_zeropage_test() || userfaultfd_events_test();
941 int main(int argc, char **argv)
944 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
945 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
946 page_size = sysconf(_SC_PAGE_SIZE);
947 if ((unsigned long) area_count(NULL, 0) + sizeof(unsigned long long) * 2
949 fprintf(stderr, "Impossible to run this test\n"), exit(2);
950 nr_pages_per_cpu = atol(argv[1]) * 1024*1024 / page_size /
952 if (!nr_pages_per_cpu) {
953 fprintf(stderr, "invalid MiB\n");
954 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
956 bounces = atoi(argv[2]);
958 fprintf(stderr, "invalid bounces\n");
959 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
961 nr_pages = nr_pages_per_cpu * nr_cpus;
962 printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
963 nr_pages, nr_pages_per_cpu);
964 return userfaultfd_stress();
967 #else /* HUGETLB_TEST */
970 * Copied from mlock2-tests.c
972 unsigned long default_huge_page_size(void)
974 unsigned long hps = 0;
977 FILE *f = fopen("/proc/meminfo", "r");
981 while (getline(&line, &linelen, f) > 0) {
982 if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
993 int main(int argc, char **argv)
996 fprintf(stderr, "Usage: <MiB> <bounces> <hugetlbfs_file>\n"),
998 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
999 page_size = default_huge_page_size();
1001 fprintf(stderr, "Unable to determine huge page size\n"),
1003 if ((unsigned long) area_count(NULL, 0) + sizeof(unsigned long long) * 2
1005 fprintf(stderr, "Impossible to run this test\n"), exit(2);
1006 nr_pages_per_cpu = atol(argv[1]) * 1024*1024 / page_size /
1008 if (!nr_pages_per_cpu) {
1009 fprintf(stderr, "invalid MiB\n");
1010 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
1012 bounces = atoi(argv[2]);
1014 fprintf(stderr, "invalid bounces\n");
1015 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
1017 nr_pages = nr_pages_per_cpu * nr_cpus;
1018 huge_fd = open(argv[3], O_CREAT | O_RDWR, 0755);
1020 fprintf(stderr, "Open of %s failed", argv[3]);
1024 if (ftruncate(huge_fd, 0)) {
1025 fprintf(stderr, "ftruncate %s to size 0 failed", argv[3]);
1026 perror("ftruncate");
1029 printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
1030 nr_pages, nr_pages_per_cpu);
1031 return userfaultfd_stress();
1035 #else /* __NR_userfaultfd */
1037 #warning "missing __NR_userfaultfd definition"
1041 printf("skip: Skipping userfaultfd test (missing __NR_userfaultfd)\n");
1045 #endif /* __NR_userfaultfd */