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)
81 #define TEST_HUGETLB 2
86 static char *huge_fd_off0;
87 static unsigned long long *count_verify;
88 static int uffd, uffd_flags, finished, *pipefd;
89 static char *area_src, *area_dst;
90 static char *zeropage;
93 /* pthread_mutex_t starts at page offset 0 */
94 #define area_mutex(___area, ___nr) \
95 ((pthread_mutex_t *) ((___area) + (___nr)*page_size))
97 * count is placed in the page after pthread_mutex_t naturally aligned
98 * to avoid non alignment faults on non-x86 archs.
100 #define area_count(___area, ___nr) \
101 ((volatile unsigned long long *) ((unsigned long) \
102 ((___area) + (___nr)*page_size + \
103 sizeof(pthread_mutex_t) + \
104 sizeof(unsigned long long) - 1) & \
105 ~(unsigned long)(sizeof(unsigned long long) \
108 static int anon_release_pages(char *rel_area)
112 if (madvise(rel_area, nr_pages * page_size, MADV_DONTNEED)) {
120 static void anon_allocate_area(void **alloc_area)
122 if (posix_memalign(alloc_area, page_size, nr_pages * page_size)) {
123 fprintf(stderr, "out of memory\n");
130 static int hugetlb_release_pages(char *rel_area)
134 if (fallocate(huge_fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
135 rel_area == huge_fd_off0 ? 0 :
136 nr_pages * page_size,
137 nr_pages * page_size)) {
146 static void hugetlb_allocate_area(void **alloc_area)
148 *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
149 MAP_PRIVATE | MAP_HUGETLB, huge_fd,
150 *alloc_area == area_src ? 0 :
151 nr_pages * page_size);
152 if (*alloc_area == MAP_FAILED) {
153 fprintf(stderr, "mmap of hugetlbfs file failed\n");
157 if (*alloc_area == area_src)
158 huge_fd_off0 = *alloc_area;
162 static int shmem_release_pages(char *rel_area)
166 if (madvise(rel_area, nr_pages * page_size, MADV_REMOVE)) {
174 static void shmem_allocate_area(void **alloc_area)
176 *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
177 MAP_ANONYMOUS | MAP_SHARED, -1, 0);
178 if (*alloc_area == MAP_FAILED) {
179 fprintf(stderr, "shared memory mmap failed\n");
184 struct uffd_test_ops {
185 unsigned long expected_ioctls;
186 void (*allocate_area)(void **alloc_area);
187 int (*release_pages)(char *rel_area);
190 #define ANON_EXPECTED_IOCTLS ((1 << _UFFDIO_WAKE) | \
191 (1 << _UFFDIO_COPY) | \
192 (1 << _UFFDIO_ZEROPAGE))
194 static struct uffd_test_ops anon_uffd_test_ops = {
195 .expected_ioctls = ANON_EXPECTED_IOCTLS,
196 .allocate_area = anon_allocate_area,
197 .release_pages = anon_release_pages,
200 static struct uffd_test_ops shmem_uffd_test_ops = {
201 .expected_ioctls = UFFD_API_RANGE_IOCTLS_BASIC,
202 .allocate_area = shmem_allocate_area,
203 .release_pages = shmem_release_pages,
206 static struct uffd_test_ops hugetlb_uffd_test_ops = {
207 .expected_ioctls = UFFD_API_RANGE_IOCTLS_BASIC,
208 .allocate_area = hugetlb_allocate_area,
209 .release_pages = hugetlb_release_pages,
212 static struct uffd_test_ops *uffd_test_ops;
214 static int my_bcmp(char *str1, char *str2, size_t n)
217 for (i = 0; i < n; i++)
218 if (str1[i] != str2[i])
223 static void *locking_thread(void *arg)
225 unsigned long cpu = (unsigned long) arg;
226 struct random_data rand;
227 unsigned long page_nr = *(&(page_nr)); /* uninitialized warning */
229 unsigned long long count;
234 if (bounces & BOUNCE_RANDOM) {
235 seed = (unsigned int) time(NULL) - bounces;
236 if (!(bounces & BOUNCE_RACINGFAULTS))
238 bzero(&rand, sizeof(rand));
239 bzero(&randstate, sizeof(randstate));
240 if (initstate_r(seed, randstate, sizeof(randstate), &rand))
241 fprintf(stderr, "srandom_r error\n"), exit(1);
244 if (!(bounces & BOUNCE_RACINGFAULTS))
245 page_nr += cpu * nr_pages_per_cpu;
249 if (bounces & BOUNCE_RANDOM) {
250 if (random_r(&rand, &rand_nr))
251 fprintf(stderr, "random_r 1 error\n"), exit(1);
253 if (sizeof(page_nr) > sizeof(rand_nr)) {
254 if (random_r(&rand, &rand_nr))
255 fprintf(stderr, "random_r 2 error\n"), exit(1);
256 page_nr |= (((unsigned long) rand_nr) << 16) <<
264 if (bounces & BOUNCE_VERIFY) {
265 count = *area_count(area_dst, page_nr);
268 "page_nr %lu wrong count %Lu %Lu\n",
270 count_verify[page_nr]), exit(1);
274 * We can't use bcmp (or memcmp) because that
275 * returns 0 erroneously if the memory is
276 * changing under it (even if the end of the
277 * page is never changing and always
281 if (!my_bcmp(area_dst + page_nr * page_size, zeropage,
284 "my_bcmp page_nr %lu wrong count %Lu %Lu\n",
286 count_verify[page_nr]), exit(1);
291 /* uncomment the below line to test with mutex */
292 /* pthread_mutex_lock(area_mutex(area_dst, page_nr)); */
293 while (!bcmp(area_dst + page_nr * page_size, zeropage,
299 /* uncomment below line to test with mutex */
300 /* pthread_mutex_unlock(area_mutex(area_dst, page_nr)); */
303 "page_nr %lu all zero thread %lu %p %lu\n",
304 page_nr, cpu, area_dst + page_nr * page_size,
312 pthread_mutex_lock(area_mutex(area_dst, page_nr));
313 count = *area_count(area_dst, page_nr);
314 if (count != count_verify[page_nr]) {
316 "page_nr %lu memory corruption %Lu %Lu\n",
318 count_verify[page_nr]), exit(1);
321 *area_count(area_dst, page_nr) = count_verify[page_nr] = count;
322 pthread_mutex_unlock(area_mutex(area_dst, page_nr));
324 if (time(NULL) - start > 1)
326 "userfault too slow %ld "
327 "possible false positive with overcommit\n",
334 static int copy_page(int ufd, unsigned long offset)
336 struct uffdio_copy uffdio_copy;
338 if (offset >= nr_pages * page_size)
339 fprintf(stderr, "unexpected offset %lu\n",
341 uffdio_copy.dst = (unsigned long) area_dst + offset;
342 uffdio_copy.src = (unsigned long) area_src + offset;
343 uffdio_copy.len = page_size;
344 uffdio_copy.mode = 0;
345 uffdio_copy.copy = 0;
346 if (ioctl(ufd, UFFDIO_COPY, &uffdio_copy)) {
347 /* real retval in ufdio_copy.copy */
348 if (uffdio_copy.copy != -EEXIST)
349 fprintf(stderr, "UFFDIO_COPY error %Ld\n",
350 uffdio_copy.copy), exit(1);
351 } else if (uffdio_copy.copy != page_size) {
352 fprintf(stderr, "UFFDIO_COPY unexpected copy %Ld\n",
353 uffdio_copy.copy), exit(1);
359 static void *uffd_poll_thread(void *arg)
361 unsigned long cpu = (unsigned long) arg;
362 struct pollfd pollfd[2];
364 struct uffdio_register uffd_reg;
366 unsigned long offset;
368 unsigned long userfaults = 0;
371 pollfd[0].events = POLLIN;
372 pollfd[1].fd = pipefd[cpu*2];
373 pollfd[1].events = POLLIN;
376 ret = poll(pollfd, 2, -1);
378 fprintf(stderr, "poll error %d\n", ret), exit(1);
380 perror("poll"), exit(1);
381 if (pollfd[1].revents & POLLIN) {
382 if (read(pollfd[1].fd, &tmp_chr, 1) != 1)
383 fprintf(stderr, "read pipefd error\n"),
387 if (!(pollfd[0].revents & POLLIN))
388 fprintf(stderr, "pollfd[0].revents %d\n",
389 pollfd[0].revents), exit(1);
390 ret = read(uffd, &msg, sizeof(msg));
394 perror("nonblocking read error"), exit(1);
398 fprintf(stderr, "unexpected msg event %u\n",
401 case UFFD_EVENT_PAGEFAULT:
402 if (msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
403 fprintf(stderr, "unexpected write fault\n"), exit(1);
404 offset = (char *)(unsigned long)msg.arg.pagefault.address -
406 offset &= ~(page_size-1);
407 if (copy_page(uffd, offset))
410 case UFFD_EVENT_FORK:
411 uffd = msg.arg.fork.ufd;
414 case UFFD_EVENT_REMOVE:
415 uffd_reg.range.start = msg.arg.remove.start;
416 uffd_reg.range.len = msg.arg.remove.end -
417 msg.arg.remove.start;
418 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffd_reg.range))
419 fprintf(stderr, "remove failure\n"), exit(1);
421 case UFFD_EVENT_REMAP:
422 area_dst = (char *)(unsigned long)msg.arg.remap.to;
426 return (void *)userfaults;
429 pthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER;
431 static void *uffd_read_thread(void *arg)
433 unsigned long *this_cpu_userfaults;
435 unsigned long offset;
438 this_cpu_userfaults = (unsigned long *) arg;
439 *this_cpu_userfaults = 0;
441 pthread_mutex_unlock(&uffd_read_mutex);
442 /* from here cancellation is ok */
445 ret = read(uffd, &msg, sizeof(msg));
446 if (ret != sizeof(msg)) {
448 perror("blocking read error"), exit(1);
450 fprintf(stderr, "short read\n"), exit(1);
452 if (msg.event != UFFD_EVENT_PAGEFAULT)
453 fprintf(stderr, "unexpected msg event %u\n",
455 if (bounces & BOUNCE_VERIFY &&
456 msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
457 fprintf(stderr, "unexpected write fault\n"), exit(1);
458 offset = (char *)(unsigned long)msg.arg.pagefault.address -
460 offset &= ~(page_size-1);
461 if (copy_page(uffd, offset))
462 (*this_cpu_userfaults)++;
467 static void *background_thread(void *arg)
469 unsigned long cpu = (unsigned long) arg;
470 unsigned long page_nr;
472 for (page_nr = cpu * nr_pages_per_cpu;
473 page_nr < (cpu+1) * nr_pages_per_cpu;
475 copy_page(uffd, page_nr * page_size);
480 static int stress(unsigned long *userfaults)
483 pthread_t locking_threads[nr_cpus];
484 pthread_t uffd_threads[nr_cpus];
485 pthread_t background_threads[nr_cpus];
486 void **_userfaults = (void **) userfaults;
489 for (cpu = 0; cpu < nr_cpus; cpu++) {
490 if (pthread_create(&locking_threads[cpu], &attr,
491 locking_thread, (void *)cpu))
493 if (bounces & BOUNCE_POLL) {
494 if (pthread_create(&uffd_threads[cpu], &attr,
495 uffd_poll_thread, (void *)cpu))
498 if (pthread_create(&uffd_threads[cpu], &attr,
502 pthread_mutex_lock(&uffd_read_mutex);
504 if (pthread_create(&background_threads[cpu], &attr,
505 background_thread, (void *)cpu))
508 for (cpu = 0; cpu < nr_cpus; cpu++)
509 if (pthread_join(background_threads[cpu], NULL))
513 * Be strict and immediately zap area_src, the whole area has
514 * been transferred already by the background treads. The
515 * area_src could then be faulted in in a racy way by still
516 * running uffdio_threads reading zeropages after we zapped
517 * area_src (but they're guaranteed to get -EEXIST from
518 * UFFDIO_COPY without writing zero pages into area_dst
519 * because the background threads already completed).
521 if (uffd_test_ops->release_pages(area_src))
524 for (cpu = 0; cpu < nr_cpus; cpu++) {
526 if (bounces & BOUNCE_POLL) {
527 if (write(pipefd[cpu*2+1], &c, 1) != 1) {
528 fprintf(stderr, "pipefd write error\n");
531 if (pthread_join(uffd_threads[cpu], &_userfaults[cpu]))
534 if (pthread_cancel(uffd_threads[cpu]))
536 if (pthread_join(uffd_threads[cpu], NULL))
542 for (cpu = 0; cpu < nr_cpus; cpu++)
543 if (pthread_join(locking_threads[cpu], NULL))
549 static int userfaultfd_open(int features)
551 struct uffdio_api uffdio_api;
553 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
556 "userfaultfd syscall not available in this kernel\n");
559 uffd_flags = fcntl(uffd, F_GETFD, NULL);
561 uffdio_api.api = UFFD_API;
562 uffdio_api.features = features;
563 if (ioctl(uffd, UFFDIO_API, &uffdio_api)) {
564 fprintf(stderr, "UFFDIO_API\n");
567 if (uffdio_api.api != UFFD_API) {
568 fprintf(stderr, "UFFDIO_API error %Lu\n", uffdio_api.api);
576 * For non-cooperative userfaultfd test we fork() a process that will
577 * generate pagefaults, will mremap the area monitored by the
578 * userfaultfd and at last this process will release the monitored
580 * For the anonymous and shared memory the area is divided into two
581 * parts, the first part is accessed before mremap, and the second
582 * part is accessed after mremap. Since hugetlbfs does not support
583 * mremap, the entire monitored area is accessed in a single pass for
585 * The release of the pages currently generates event for shmem and
586 * anonymous memory (UFFD_EVENT_REMOVE), hence it is not checked
589 static int faulting_process(void)
592 unsigned long long count;
593 unsigned long split_nr_pages;
595 if (test_type != TEST_HUGETLB)
596 split_nr_pages = (nr_pages + 1) / 2;
598 split_nr_pages = nr_pages;
600 for (nr = 0; nr < split_nr_pages; nr++) {
601 count = *area_count(area_dst, nr);
602 if (count != count_verify[nr]) {
604 "nr %lu memory corruption %Lu %Lu\n",
606 count_verify[nr]), exit(1);
610 if (test_type == TEST_HUGETLB)
613 area_dst = mremap(area_dst, nr_pages * page_size, nr_pages * page_size,
614 MREMAP_MAYMOVE | MREMAP_FIXED, area_src);
615 if (area_dst == MAP_FAILED)
616 perror("mremap"), exit(1);
618 for (; nr < nr_pages; nr++) {
619 count = *area_count(area_dst, nr);
620 if (count != count_verify[nr]) {
622 "nr %lu memory corruption %Lu %Lu\n",
624 count_verify[nr]), exit(1);
628 if (uffd_test_ops->release_pages(area_dst))
631 for (nr = 0; nr < nr_pages; nr++) {
632 if (my_bcmp(area_dst + nr * page_size, zeropage, page_size))
633 fprintf(stderr, "nr %lu is not zero\n", nr), exit(1);
639 static int uffdio_zeropage(int ufd, unsigned long offset)
641 struct uffdio_zeropage uffdio_zeropage;
643 unsigned long has_zeropage;
645 has_zeropage = uffd_test_ops->expected_ioctls & (1 << _UFFDIO_ZEROPAGE);
647 if (offset >= nr_pages * page_size)
648 fprintf(stderr, "unexpected offset %lu\n",
650 uffdio_zeropage.range.start = (unsigned long) area_dst + offset;
651 uffdio_zeropage.range.len = page_size;
652 uffdio_zeropage.mode = 0;
653 ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage);
655 /* real retval in ufdio_zeropage.zeropage */
657 if (uffdio_zeropage.zeropage == -EEXIST)
658 fprintf(stderr, "UFFDIO_ZEROPAGE -EEXIST\n"),
661 fprintf(stderr, "UFFDIO_ZEROPAGE error %Ld\n",
662 uffdio_zeropage.zeropage), exit(1);
664 if (uffdio_zeropage.zeropage != -EINVAL)
666 "UFFDIO_ZEROPAGE not -EINVAL %Ld\n",
667 uffdio_zeropage.zeropage), exit(1);
669 } else if (has_zeropage) {
670 if (uffdio_zeropage.zeropage != page_size) {
671 fprintf(stderr, "UFFDIO_ZEROPAGE unexpected %Ld\n",
672 uffdio_zeropage.zeropage), exit(1);
677 "UFFDIO_ZEROPAGE succeeded %Ld\n",
678 uffdio_zeropage.zeropage), exit(1);
684 /* exercise UFFDIO_ZEROPAGE */
685 static int userfaultfd_zeropage_test(void)
687 struct uffdio_register uffdio_register;
688 unsigned long expected_ioctls;
690 printf("testing UFFDIO_ZEROPAGE: ");
693 if (uffd_test_ops->release_pages(area_dst))
696 if (userfaultfd_open(0) < 0)
698 uffdio_register.range.start = (unsigned long) area_dst;
699 uffdio_register.range.len = nr_pages * page_size;
700 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
701 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
702 fprintf(stderr, "register failure\n"), exit(1);
704 expected_ioctls = uffd_test_ops->expected_ioctls;
705 if ((uffdio_register.ioctls & expected_ioctls) !=
708 "unexpected missing ioctl for anon memory\n"),
711 if (uffdio_zeropage(uffd, 0)) {
712 if (my_bcmp(area_dst, zeropage, page_size))
713 fprintf(stderr, "zeropage is not zero\n"), exit(1);
721 static int userfaultfd_events_test(void)
723 struct uffdio_register uffdio_register;
724 unsigned long expected_ioctls;
725 unsigned long userfaults;
731 printf("testing events (fork, remap, remove): ");
734 if (uffd_test_ops->release_pages(area_dst))
737 features = UFFD_FEATURE_EVENT_FORK | UFFD_FEATURE_EVENT_REMAP |
738 UFFD_FEATURE_EVENT_REMOVE;
739 if (userfaultfd_open(features) < 0)
741 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
743 uffdio_register.range.start = (unsigned long) area_dst;
744 uffdio_register.range.len = nr_pages * page_size;
745 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
746 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
747 fprintf(stderr, "register failure\n"), exit(1);
749 expected_ioctls = uffd_test_ops->expected_ioctls;
750 if ((uffdio_register.ioctls & expected_ioctls) !=
753 "unexpected missing ioctl for anon memory\n"),
756 if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
757 perror("uffd_poll_thread create"), exit(1);
761 perror("fork"), exit(1);
764 return faulting_process();
766 waitpid(pid, &err, 0);
768 fprintf(stderr, "faulting process failed\n"), exit(1);
770 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
771 perror("pipe write"), exit(1);
772 if (pthread_join(uffd_mon, (void **)&userfaults))
776 printf("userfaults: %ld\n", userfaults);
778 return userfaults != nr_pages;
781 static int userfaultfd_stress(void)
786 struct uffdio_register uffdio_register;
789 unsigned long userfaults[nr_cpus];
791 uffd_test_ops->allocate_area((void **)&area_src);
794 uffd_test_ops->allocate_area((void **)&area_dst);
798 if (userfaultfd_open(0) < 0)
801 count_verify = malloc(nr_pages * sizeof(unsigned long long));
803 perror("count_verify");
807 for (nr = 0; nr < nr_pages; nr++) {
808 *area_mutex(area_src, nr) = (pthread_mutex_t)
809 PTHREAD_MUTEX_INITIALIZER;
810 count_verify[nr] = *area_count(area_src, nr) = 1;
812 * In the transition between 255 to 256, powerpc will
813 * read out of order in my_bcmp and see both bytes as
814 * zero, so leave a placeholder below always non-zero
815 * after the count, to avoid my_bcmp to trigger false
818 *(area_count(area_src, nr) + 1) = 1;
821 pipefd = malloc(sizeof(int) * nr_cpus * 2);
826 for (cpu = 0; cpu < nr_cpus; cpu++) {
827 if (pipe2(&pipefd[cpu*2], O_CLOEXEC | O_NONBLOCK)) {
833 if (posix_memalign(&area, page_size, page_size)) {
834 fprintf(stderr, "out of memory\n");
838 bzero(zeropage, page_size);
840 pthread_mutex_lock(&uffd_read_mutex);
842 pthread_attr_init(&attr);
843 pthread_attr_setstacksize(&attr, 16*1024*1024);
847 unsigned long expected_ioctls;
849 printf("bounces: %d, mode:", bounces);
850 if (bounces & BOUNCE_RANDOM)
852 if (bounces & BOUNCE_RACINGFAULTS)
854 if (bounces & BOUNCE_VERIFY)
856 if (bounces & BOUNCE_POLL)
861 if (bounces & BOUNCE_POLL)
862 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
864 fcntl(uffd, F_SETFL, uffd_flags & ~O_NONBLOCK);
867 uffdio_register.range.start = (unsigned long) area_dst;
868 uffdio_register.range.len = nr_pages * page_size;
869 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
870 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
871 fprintf(stderr, "register failure\n");
874 expected_ioctls = uffd_test_ops->expected_ioctls;
875 if ((uffdio_register.ioctls & expected_ioctls) !=
878 "unexpected missing ioctl for anon memory\n");
883 * The madvise done previously isn't enough: some
884 * uffd_thread could have read userfaults (one of
885 * those already resolved by the background thread)
886 * and it may be in the process of calling
887 * UFFDIO_COPY. UFFDIO_COPY will read the zapped
888 * area_src and it would map a zero page in it (of
889 * course such a UFFDIO_COPY is perfectly safe as it'd
890 * return -EEXIST). The problem comes at the next
891 * bounce though: that racing UFFDIO_COPY would
892 * generate zeropages in the area_src, so invalidating
893 * the previous MADV_DONTNEED. Without this additional
894 * MADV_DONTNEED those zeropages leftovers in the
895 * area_src would lead to -EEXIST failure during the
896 * next bounce, effectively leaving a zeropage in the
899 * Try to comment this out madvise to see the memory
900 * corruption being caught pretty quick.
902 * khugepaged is also inhibited to collapse THP after
903 * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's
904 * required to MADV_DONTNEED here.
906 if (uffd_test_ops->release_pages(area_dst))
910 if (stress(userfaults))
914 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) {
915 fprintf(stderr, "register failure\n");
920 if (bounces & BOUNCE_VERIFY) {
921 for (nr = 0; nr < nr_pages; nr++) {
922 if (*area_count(area_dst, nr) != count_verify[nr]) {
924 "error area_count %Lu %Lu %lu\n",
925 *area_count(area_src, nr),
934 /* prepare next bounce */
939 printf("userfaults:");
940 for (cpu = 0; cpu < nr_cpus; cpu++)
941 printf(" %lu", userfaults[cpu]);
949 return userfaultfd_zeropage_test() || userfaultfd_events_test();
953 * Copied from mlock2-tests.c
955 unsigned long default_huge_page_size(void)
957 unsigned long hps = 0;
960 FILE *f = fopen("/proc/meminfo", "r");
964 while (getline(&line, &linelen, f) > 0) {
965 if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
976 static void set_test_type(const char *type)
978 if (!strcmp(type, "anon")) {
979 test_type = TEST_ANON;
980 uffd_test_ops = &anon_uffd_test_ops;
981 } else if (!strcmp(type, "hugetlb")) {
982 test_type = TEST_HUGETLB;
983 uffd_test_ops = &hugetlb_uffd_test_ops;
984 } else if (!strcmp(type, "shmem")) {
985 test_type = TEST_SHMEM;
986 uffd_test_ops = &shmem_uffd_test_ops;
988 fprintf(stderr, "Unknown test type: %s\n", type), exit(1);
991 if (test_type == TEST_HUGETLB)
992 page_size = default_huge_page_size();
994 page_size = sysconf(_SC_PAGE_SIZE);
997 fprintf(stderr, "Unable to determine page size\n"),
999 if ((unsigned long) area_count(NULL, 0) + sizeof(unsigned long long) * 2
1001 fprintf(stderr, "Impossible to run this test\n"), exit(2);
1004 int main(int argc, char **argv)
1007 fprintf(stderr, "Usage: <test type> <MiB> <bounces> [hugetlbfs_file]\n"),
1010 set_test_type(argv[1]);
1012 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1013 nr_pages_per_cpu = atol(argv[2]) * 1024*1024 / page_size /
1015 if (!nr_pages_per_cpu) {
1016 fprintf(stderr, "invalid MiB\n");
1017 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
1020 bounces = atoi(argv[3]);
1022 fprintf(stderr, "invalid bounces\n");
1023 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
1025 nr_pages = nr_pages_per_cpu * nr_cpus;
1027 if (test_type == TEST_HUGETLB) {
1029 fprintf(stderr, "Usage: hugetlb <MiB> <bounces> <hugetlbfs_file>\n"),
1031 huge_fd = open(argv[4], O_CREAT | O_RDWR, 0755);
1033 fprintf(stderr, "Open of %s failed", argv[3]);
1037 if (ftruncate(huge_fd, 0)) {
1038 fprintf(stderr, "ftruncate %s to size 0 failed", argv[3]);
1039 perror("ftruncate");
1043 printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
1044 nr_pages, nr_pages_per_cpu);
1045 return userfaultfd_stress();
1048 #else /* __NR_userfaultfd */
1050 #warning "missing __NR_userfaultfd definition"
1054 printf("skip: Skipping userfaultfd test (missing __NR_userfaultfd)\n");
1058 #endif /* __NR_userfaultfd */