1 // SPDX-License-Identifier: GPL-2.0-only
3 * Stress userfaultfd syscall.
5 * Copyright (C) 2015 Red Hat, Inc.
7 * This test allocates two virtual areas and bounces the physical
8 * memory across the two virtual areas (from area_src to area_dst)
11 * There are three threads running per CPU:
13 * 1) one per-CPU thread takes a per-page pthread_mutex in a random
14 * page of the area_dst (while the physical page may still be in
15 * area_src), and increments a per-page counter in the same page,
16 * and checks its value against a verification region.
18 * 2) another per-CPU thread handles the userfaults generated by
19 * thread 1 above. userfaultfd blocking reads or poll() modes are
20 * exercised interleaved.
22 * 3) one last per-CPU thread transfers the memory in the background
23 * at maximum bandwidth (if not already transferred by thread
24 * 2). Each cpu thread takes cares of transferring a portion of the
27 * When all threads of type 3 completed the transfer, one bounce is
28 * complete. area_src and area_dst are then swapped. All threads are
29 * respawned and so the bounce is immediately restarted in the
32 * per-CPU threads 1 by triggering userfaults inside
33 * pthread_mutex_lock will also verify the atomicity of the memory
34 * transfer (UFFDIO_COPY).
42 #include <sys/types.h>
50 #include <sys/syscall.h>
51 #include <sys/ioctl.h>
54 #include <linux/userfaultfd.h>
60 #include <sys/random.h>
62 #include "../kselftest.h"
64 #ifdef __NR_userfaultfd
66 static unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;
68 #define BOUNCE_RANDOM (1<<0)
69 #define BOUNCE_RACINGFAULTS (1<<1)
70 #define BOUNCE_VERIFY (1<<2)
71 #define BOUNCE_POLL (1<<3)
75 #define TEST_HUGETLB 2
79 /* exercise the test_uffdio_*_eexist every ALARM_INTERVAL_SECS */
80 #define ALARM_INTERVAL_SECS 10
81 static volatile bool test_uffdio_copy_eexist = true;
82 static volatile bool test_uffdio_zeropage_eexist = true;
83 /* Whether to test uffd write-protection */
84 static bool test_uffdio_wp = false;
85 /* Whether to test uffd minor faults */
86 static bool test_uffdio_minor = false;
88 static bool map_shared;
91 static char *huge_fd_off0;
92 static unsigned long long *count_verify;
94 static int uffd_flags, finished, *pipefd;
95 static char *area_src, *area_src_alias, *area_dst, *area_dst_alias;
96 static char *zeropage;
99 /* Userfaultfd test statistics */
102 unsigned long missing_faults;
103 unsigned long wp_faults;
104 unsigned long minor_faults;
107 /* pthread_mutex_t starts at page offset 0 */
108 #define area_mutex(___area, ___nr) \
109 ((pthread_mutex_t *) ((___area) + (___nr)*page_size))
111 * count is placed in the page after pthread_mutex_t naturally aligned
112 * to avoid non alignment faults on non-x86 archs.
114 #define area_count(___area, ___nr) \
115 ((volatile unsigned long long *) ((unsigned long) \
116 ((___area) + (___nr)*page_size + \
117 sizeof(pthread_mutex_t) + \
118 sizeof(unsigned long long) - 1) & \
119 ~(unsigned long)(sizeof(unsigned long long) \
122 const char *examples =
123 "# Run anonymous memory test on 100MiB region with 99999 bounces:\n"
124 "./userfaultfd anon 100 99999\n\n"
125 "# Run share memory test on 1GiB region with 99 bounces:\n"
126 "./userfaultfd shmem 1000 99\n\n"
127 "# Run hugetlb memory test on 256MiB region with 50 bounces (using /dev/hugepages/hugefile):\n"
128 "./userfaultfd hugetlb 256 50 /dev/hugepages/hugefile\n\n"
129 "# Run the same hugetlb test but using shmem:\n"
130 "./userfaultfd hugetlb_shared 256 50 /dev/hugepages/hugefile\n\n"
131 "# 10MiB-~6GiB 999 bounces anonymous test, "
132 "continue forever unless an error triggers\n"
133 "while ./userfaultfd anon $[RANDOM % 6000 + 10] 999; do true; done\n\n";
135 static void usage(void)
137 fprintf(stderr, "\nUsage: ./userfaultfd <test type> <MiB> <bounces> "
138 "[hugetlbfs_file]\n\n");
139 fprintf(stderr, "Supported <test type>: anon, hugetlb, "
140 "hugetlb_shared, shmem\n\n");
141 fprintf(stderr, "Examples:\n\n");
142 fprintf(stderr, "%s", examples);
146 #define _err(fmt, ...) \
149 fprintf(stderr, "ERROR: " fmt, ##__VA_ARGS__); \
150 fprintf(stderr, " (errno=%d, line=%d)\n", \
154 #define err(fmt, ...) \
156 _err(fmt, ##__VA_ARGS__); \
160 static void uffd_stats_reset(struct uffd_stats *uffd_stats,
161 unsigned long n_cpus)
165 for (i = 0; i < n_cpus; i++) {
166 uffd_stats[i].cpu = i;
167 uffd_stats[i].missing_faults = 0;
168 uffd_stats[i].wp_faults = 0;
169 uffd_stats[i].minor_faults = 0;
173 static void uffd_stats_report(struct uffd_stats *stats, int n_cpus)
176 unsigned long long miss_total = 0, wp_total = 0, minor_total = 0;
178 for (i = 0; i < n_cpus; i++) {
179 miss_total += stats[i].missing_faults;
180 wp_total += stats[i].wp_faults;
181 minor_total += stats[i].minor_faults;
184 printf("userfaults: ");
186 printf("%llu missing (", miss_total);
187 for (i = 0; i < n_cpus; i++)
188 printf("%lu+", stats[i].missing_faults);
192 printf("%llu wp (", wp_total);
193 for (i = 0; i < n_cpus; i++)
194 printf("%lu+", stats[i].wp_faults);
198 printf("%llu minor (", minor_total);
199 for (i = 0; i < n_cpus; i++)
200 printf("%lu+", stats[i].minor_faults);
206 static void anon_release_pages(char *rel_area)
208 if (madvise(rel_area, nr_pages * page_size, MADV_DONTNEED))
209 err("madvise(MADV_DONTNEED) failed");
212 static void anon_allocate_area(void **alloc_area)
214 *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
215 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
216 if (*alloc_area == MAP_FAILED)
217 err("mmap of anonymous memory failed");
220 static void noop_alias_mapping(__u64 *start, size_t len, unsigned long offset)
224 static void hugetlb_release_pages(char *rel_area)
226 if (fallocate(huge_fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
227 rel_area == huge_fd_off0 ? 0 : nr_pages * page_size,
228 nr_pages * page_size))
229 err("fallocate() failed");
232 static void hugetlb_allocate_area(void **alloc_area)
234 void *area_alias = NULL;
235 char **alloc_area_alias;
237 *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
238 (map_shared ? MAP_SHARED : MAP_PRIVATE) |
240 (*alloc_area == area_src ? 0 : MAP_NORESERVE),
241 huge_fd, *alloc_area == area_src ? 0 :
242 nr_pages * page_size);
243 if (*alloc_area == MAP_FAILED)
244 err("mmap of hugetlbfs file failed");
247 area_alias = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
248 MAP_SHARED | MAP_HUGETLB,
249 huge_fd, *alloc_area == area_src ? 0 :
250 nr_pages * page_size);
251 if (area_alias == MAP_FAILED)
252 err("mmap of hugetlb file alias failed");
255 if (*alloc_area == area_src) {
256 huge_fd_off0 = *alloc_area;
257 alloc_area_alias = &area_src_alias;
259 alloc_area_alias = &area_dst_alias;
262 *alloc_area_alias = area_alias;
265 static void hugetlb_alias_mapping(__u64 *start, size_t len, unsigned long offset)
270 * We can't zap just the pagetable with hugetlbfs because
271 * MADV_DONTEED won't work. So exercise -EEXIST on a alias
272 * mapping where the pagetables are not established initially,
273 * this way we'll exercise the -EEXEC at the fs level.
275 *start = (unsigned long) area_dst_alias + offset;
278 static void shmem_release_pages(char *rel_area)
280 if (madvise(rel_area, nr_pages * page_size, MADV_REMOVE))
281 err("madvise(MADV_REMOVE) failed");
284 static void shmem_allocate_area(void **alloc_area)
286 void *area_alias = NULL;
287 bool is_src = alloc_area == (void **)&area_src;
288 unsigned long offset = is_src ? 0 : nr_pages * page_size;
290 *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
291 MAP_SHARED, shm_fd, offset);
292 if (*alloc_area == MAP_FAILED)
293 err("mmap of memfd failed");
295 area_alias = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
296 MAP_SHARED, shm_fd, offset);
297 if (area_alias == MAP_FAILED)
298 err("mmap of memfd alias failed");
301 area_src_alias = area_alias;
303 area_dst_alias = area_alias;
306 static void shmem_alias_mapping(__u64 *start, size_t len, unsigned long offset)
308 *start = (unsigned long)area_dst_alias + offset;
311 struct uffd_test_ops {
312 void (*allocate_area)(void **alloc_area);
313 void (*release_pages)(char *rel_area);
314 void (*alias_mapping)(__u64 *start, size_t len, unsigned long offset);
317 static struct uffd_test_ops anon_uffd_test_ops = {
318 .allocate_area = anon_allocate_area,
319 .release_pages = anon_release_pages,
320 .alias_mapping = noop_alias_mapping,
323 static struct uffd_test_ops shmem_uffd_test_ops = {
324 .allocate_area = shmem_allocate_area,
325 .release_pages = shmem_release_pages,
326 .alias_mapping = shmem_alias_mapping,
329 static struct uffd_test_ops hugetlb_uffd_test_ops = {
330 .allocate_area = hugetlb_allocate_area,
331 .release_pages = hugetlb_release_pages,
332 .alias_mapping = hugetlb_alias_mapping,
335 static struct uffd_test_ops *uffd_test_ops;
337 static inline uint64_t uffd_minor_feature(void)
339 if (test_type == TEST_HUGETLB && map_shared)
340 return UFFD_FEATURE_MINOR_HUGETLBFS;
341 else if (test_type == TEST_SHMEM)
342 return UFFD_FEATURE_MINOR_SHMEM;
347 static uint64_t get_expected_ioctls(uint64_t mode)
349 uint64_t ioctls = UFFD_API_RANGE_IOCTLS;
351 if (test_type == TEST_HUGETLB)
352 ioctls &= ~(1 << _UFFDIO_ZEROPAGE);
354 if (!((mode & UFFDIO_REGISTER_MODE_WP) && test_uffdio_wp))
355 ioctls &= ~(1 << _UFFDIO_WRITEPROTECT);
357 if (!((mode & UFFDIO_REGISTER_MODE_MINOR) && test_uffdio_minor))
358 ioctls &= ~(1 << _UFFDIO_CONTINUE);
363 static void assert_expected_ioctls_present(uint64_t mode, uint64_t ioctls)
365 uint64_t expected = get_expected_ioctls(mode);
366 uint64_t actual = ioctls & expected;
368 if (actual != expected) {
369 err("missing ioctl(s): expected %"PRIx64" actual: %"PRIx64,
374 static void userfaultfd_open(uint64_t *features)
376 struct uffdio_api uffdio_api;
378 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY);
380 err("userfaultfd syscall not available in this kernel");
381 uffd_flags = fcntl(uffd, F_GETFD, NULL);
383 uffdio_api.api = UFFD_API;
384 uffdio_api.features = *features;
385 if (ioctl(uffd, UFFDIO_API, &uffdio_api))
386 err("UFFDIO_API failed.\nPlease make sure to "
387 "run with either root or ptrace capability.");
388 if (uffdio_api.api != UFFD_API)
389 err("UFFDIO_API error: %" PRIu64, (uint64_t)uffdio_api.api);
391 *features = uffdio_api.features;
394 static inline void munmap_area(void **area)
397 if (munmap(*area, nr_pages * page_size))
403 static void uffd_test_ctx_clear(void)
408 for (i = 0; i < nr_cpus * 2; ++i) {
409 if (close(pipefd[i]))
428 munmap_area((void **)&area_src);
429 munmap_area((void **)&area_src_alias);
430 munmap_area((void **)&area_dst);
431 munmap_area((void **)&area_dst_alias);
434 static void uffd_test_ctx_init(uint64_t features)
436 unsigned long nr, cpu;
438 uffd_test_ctx_clear();
440 uffd_test_ops->allocate_area((void **)&area_src);
441 uffd_test_ops->allocate_area((void **)&area_dst);
443 userfaultfd_open(&features);
445 count_verify = malloc(nr_pages * sizeof(unsigned long long));
449 for (nr = 0; nr < nr_pages; nr++) {
450 *area_mutex(area_src, nr) =
451 (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
452 count_verify[nr] = *area_count(area_src, nr) = 1;
454 * In the transition between 255 to 256, powerpc will
455 * read out of order in my_bcmp and see both bytes as
456 * zero, so leave a placeholder below always non-zero
457 * after the count, to avoid my_bcmp to trigger false
460 *(area_count(area_src, nr) + 1) = 1;
464 * After initialization of area_src, we must explicitly release pages
465 * for area_dst to make sure it's fully empty. Otherwise we could have
466 * some area_dst pages be errornously initialized with zero pages,
467 * hence we could hit memory corruption later in the test.
469 * One example is when THP is globally enabled, above allocate_area()
470 * calls could have the two areas merged into a single VMA (as they
471 * will have the same VMA flags so they're mergeable). When we
472 * initialize the area_src above, it's possible that some part of
473 * area_dst could have been faulted in via one huge THP that will be
474 * shared between area_src and area_dst. It could cause some of the
475 * area_dst won't be trapped by missing userfaults.
477 * This release_pages() will guarantee even if that happened, we'll
478 * proactively split the thp and drop any accidentally initialized
479 * pages within area_dst.
481 uffd_test_ops->release_pages(area_dst);
483 pipefd = malloc(sizeof(int) * nr_cpus * 2);
486 for (cpu = 0; cpu < nr_cpus; cpu++)
487 if (pipe2(&pipefd[cpu * 2], O_CLOEXEC | O_NONBLOCK))
491 static int my_bcmp(char *str1, char *str2, size_t n)
494 for (i = 0; i < n; i++)
495 if (str1[i] != str2[i])
500 static void wp_range(int ufd, __u64 start, __u64 len, bool wp)
502 struct uffdio_writeprotect prms;
504 /* Write protection page faults */
505 prms.range.start = start;
506 prms.range.len = len;
507 /* Undo write-protect, do wakeup after that */
508 prms.mode = wp ? UFFDIO_WRITEPROTECT_MODE_WP : 0;
510 if (ioctl(ufd, UFFDIO_WRITEPROTECT, &prms))
511 err("clear WP failed: address=0x%"PRIx64, (uint64_t)start);
514 static void continue_range(int ufd, __u64 start, __u64 len)
516 struct uffdio_continue req;
519 req.range.start = start;
523 if (ioctl(ufd, UFFDIO_CONTINUE, &req))
524 err("UFFDIO_CONTINUE failed for address 0x%" PRIx64,
528 * Error handling within the kernel for continue is subtly different
529 * from copy or zeropage, so it may be a source of bugs. Trigger an
530 * error (-EEXIST) on purpose, to verify doing so doesn't cause a BUG.
533 ret = ioctl(ufd, UFFDIO_CONTINUE, &req);
534 if (ret >= 0 || req.mapped != -EEXIST)
535 err("failed to exercise UFFDIO_CONTINUE error handling, ret=%d, mapped=%" PRId64,
536 ret, (int64_t) req.mapped);
539 static void *locking_thread(void *arg)
541 unsigned long cpu = (unsigned long) arg;
542 unsigned long page_nr = *(&(page_nr)); /* uninitialized warning */
543 unsigned long long count;
545 if (!(bounces & BOUNCE_RANDOM)) {
547 if (!(bounces & BOUNCE_RACINGFAULTS))
548 page_nr += cpu * nr_pages_per_cpu;
552 if (bounces & BOUNCE_RANDOM) {
553 if (getrandom(&page_nr, sizeof(page_nr), 0) != sizeof(page_nr))
554 err("getrandom failed");
558 pthread_mutex_lock(area_mutex(area_dst, page_nr));
559 count = *area_count(area_dst, page_nr);
560 if (count != count_verify[page_nr])
561 err("page_nr %lu memory corruption %llu %llu",
562 page_nr, count, count_verify[page_nr]);
564 *area_count(area_dst, page_nr) = count_verify[page_nr] = count;
565 pthread_mutex_unlock(area_mutex(area_dst, page_nr));
571 static void retry_copy_page(int ufd, struct uffdio_copy *uffdio_copy,
572 unsigned long offset)
574 uffd_test_ops->alias_mapping(&uffdio_copy->dst,
577 if (ioctl(ufd, UFFDIO_COPY, uffdio_copy)) {
578 /* real retval in ufdio_copy.copy */
579 if (uffdio_copy->copy != -EEXIST)
580 err("UFFDIO_COPY retry error: %"PRId64,
581 (int64_t)uffdio_copy->copy);
583 err("UFFDIO_COPY retry unexpected: %"PRId64,
584 (int64_t)uffdio_copy->copy);
588 static void wake_range(int ufd, unsigned long addr, unsigned long len)
590 struct uffdio_range uffdio_wake;
592 uffdio_wake.start = addr;
593 uffdio_wake.len = len;
595 if (ioctl(ufd, UFFDIO_WAKE, &uffdio_wake))
596 fprintf(stderr, "error waking %lu\n",
600 static int __copy_page(int ufd, unsigned long offset, bool retry)
602 struct uffdio_copy uffdio_copy;
604 if (offset >= nr_pages * page_size)
605 err("unexpected offset %lu\n", offset);
606 uffdio_copy.dst = (unsigned long) area_dst + offset;
607 uffdio_copy.src = (unsigned long) area_src + offset;
608 uffdio_copy.len = page_size;
610 uffdio_copy.mode = UFFDIO_COPY_MODE_WP;
612 uffdio_copy.mode = 0;
613 uffdio_copy.copy = 0;
614 if (ioctl(ufd, UFFDIO_COPY, &uffdio_copy)) {
615 /* real retval in ufdio_copy.copy */
616 if (uffdio_copy.copy != -EEXIST)
617 err("UFFDIO_COPY error: %"PRId64,
618 (int64_t)uffdio_copy.copy);
619 wake_range(ufd, uffdio_copy.dst, page_size);
620 } else if (uffdio_copy.copy != page_size) {
621 err("UFFDIO_COPY error: %"PRId64, (int64_t)uffdio_copy.copy);
623 if (test_uffdio_copy_eexist && retry) {
624 test_uffdio_copy_eexist = false;
625 retry_copy_page(ufd, &uffdio_copy, offset);
632 static int copy_page_retry(int ufd, unsigned long offset)
634 return __copy_page(ufd, offset, true);
637 static int copy_page(int ufd, unsigned long offset)
639 return __copy_page(ufd, offset, false);
642 static int uffd_read_msg(int ufd, struct uffd_msg *msg)
644 int ret = read(uffd, msg, sizeof(*msg));
646 if (ret != sizeof(*msg)) {
648 if (errno == EAGAIN || errno == EINTR)
650 err("blocking read error");
659 static void uffd_handle_page_fault(struct uffd_msg *msg,
660 struct uffd_stats *stats)
662 unsigned long offset;
664 if (msg->event != UFFD_EVENT_PAGEFAULT)
665 err("unexpected msg event %u", msg->event);
667 if (msg->arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WP) {
668 /* Write protect page faults */
669 wp_range(uffd, msg->arg.pagefault.address, page_size, false);
671 } else if (msg->arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_MINOR) {
678 * To prove we can modify the original range for testing
679 * purposes, we're going to bit flip this range before
682 * Note that this requires all minor page fault tests operate on
683 * area_dst (non-UFFD-registered) and area_dst_alias
687 area = (uint8_t *)(area_dst +
688 ((char *)msg->arg.pagefault.address -
690 for (b = 0; b < page_size; ++b)
692 continue_range(uffd, msg->arg.pagefault.address, page_size);
693 stats->minor_faults++;
695 /* Missing page faults */
696 if (msg->arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
697 err("unexpected write fault");
699 offset = (char *)(unsigned long)msg->arg.pagefault.address - area_dst;
700 offset &= ~(page_size-1);
702 if (copy_page(uffd, offset))
703 stats->missing_faults++;
707 static void *uffd_poll_thread(void *arg)
709 struct uffd_stats *stats = (struct uffd_stats *)arg;
710 unsigned long cpu = stats->cpu;
711 struct pollfd pollfd[2];
713 struct uffdio_register uffd_reg;
718 pollfd[0].events = POLLIN;
719 pollfd[1].fd = pipefd[cpu*2];
720 pollfd[1].events = POLLIN;
723 ret = poll(pollfd, 2, -1);
725 if (errno == EINTR || errno == EAGAIN)
727 err("poll error: %d", ret);
729 if (pollfd[1].revents & POLLIN) {
730 if (read(pollfd[1].fd, &tmp_chr, 1) != 1)
731 err("read pipefd error");
734 if (!(pollfd[0].revents & POLLIN))
735 err("pollfd[0].revents %d", pollfd[0].revents);
736 if (uffd_read_msg(uffd, &msg))
740 err("unexpected msg event %u\n", msg.event);
742 case UFFD_EVENT_PAGEFAULT:
743 uffd_handle_page_fault(&msg, stats);
745 case UFFD_EVENT_FORK:
747 uffd = msg.arg.fork.ufd;
750 case UFFD_EVENT_REMOVE:
751 uffd_reg.range.start = msg.arg.remove.start;
752 uffd_reg.range.len = msg.arg.remove.end -
753 msg.arg.remove.start;
754 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffd_reg.range))
755 err("remove failure");
757 case UFFD_EVENT_REMAP:
758 area_dst = (char *)(unsigned long)msg.arg.remap.to;
766 pthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER;
768 static void *uffd_read_thread(void *arg)
770 struct uffd_stats *stats = (struct uffd_stats *)arg;
773 pthread_mutex_unlock(&uffd_read_mutex);
774 /* from here cancellation is ok */
777 if (uffd_read_msg(uffd, &msg))
779 uffd_handle_page_fault(&msg, stats);
785 static void *background_thread(void *arg)
787 unsigned long cpu = (unsigned long) arg;
788 unsigned long page_nr, start_nr, mid_nr, end_nr;
790 start_nr = cpu * nr_pages_per_cpu;
791 end_nr = (cpu+1) * nr_pages_per_cpu;
792 mid_nr = (start_nr + end_nr) / 2;
794 /* Copy the first half of the pages */
795 for (page_nr = start_nr; page_nr < mid_nr; page_nr++)
796 copy_page_retry(uffd, page_nr * page_size);
799 * If we need to test uffd-wp, set it up now. Then we'll have
800 * at least the first half of the pages mapped already which
801 * can be write-protected for testing
804 wp_range(uffd, (unsigned long)area_dst + start_nr * page_size,
805 nr_pages_per_cpu * page_size, true);
808 * Continue the 2nd half of the page copying, handling write
809 * protection faults if any
811 for (page_nr = mid_nr; page_nr < end_nr; page_nr++)
812 copy_page_retry(uffd, page_nr * page_size);
817 static int stress(struct uffd_stats *uffd_stats)
820 pthread_t locking_threads[nr_cpus];
821 pthread_t uffd_threads[nr_cpus];
822 pthread_t background_threads[nr_cpus];
825 for (cpu = 0; cpu < nr_cpus; cpu++) {
826 if (pthread_create(&locking_threads[cpu], &attr,
827 locking_thread, (void *)cpu))
829 if (bounces & BOUNCE_POLL) {
830 if (pthread_create(&uffd_threads[cpu], &attr,
832 (void *)&uffd_stats[cpu]))
835 if (pthread_create(&uffd_threads[cpu], &attr,
837 (void *)&uffd_stats[cpu]))
839 pthread_mutex_lock(&uffd_read_mutex);
841 if (pthread_create(&background_threads[cpu], &attr,
842 background_thread, (void *)cpu))
845 for (cpu = 0; cpu < nr_cpus; cpu++)
846 if (pthread_join(background_threads[cpu], NULL))
850 * Be strict and immediately zap area_src, the whole area has
851 * been transferred already by the background treads. The
852 * area_src could then be faulted in in a racy way by still
853 * running uffdio_threads reading zeropages after we zapped
854 * area_src (but they're guaranteed to get -EEXIST from
855 * UFFDIO_COPY without writing zero pages into area_dst
856 * because the background threads already completed).
858 uffd_test_ops->release_pages(area_src);
861 for (cpu = 0; cpu < nr_cpus; cpu++)
862 if (pthread_join(locking_threads[cpu], NULL))
865 for (cpu = 0; cpu < nr_cpus; cpu++) {
867 if (bounces & BOUNCE_POLL) {
868 if (write(pipefd[cpu*2+1], &c, 1) != 1)
869 err("pipefd write error");
870 if (pthread_join(uffd_threads[cpu],
871 (void *)&uffd_stats[cpu]))
874 if (pthread_cancel(uffd_threads[cpu]))
876 if (pthread_join(uffd_threads[cpu], NULL))
884 sigjmp_buf jbuf, *sigbuf;
886 static void sighndl(int sig, siginfo_t *siginfo, void *ptr)
890 siglongjmp(*sigbuf, 1);
896 * For non-cooperative userfaultfd test we fork() a process that will
897 * generate pagefaults, will mremap the area monitored by the
898 * userfaultfd and at last this process will release the monitored
900 * For the anonymous and shared memory the area is divided into two
901 * parts, the first part is accessed before mremap, and the second
902 * part is accessed after mremap. Since hugetlbfs does not support
903 * mremap, the entire monitored area is accessed in a single pass for
905 * The release of the pages currently generates event for shmem and
906 * anonymous memory (UFFD_EVENT_REMOVE), hence it is not checked
908 * For signal test(UFFD_FEATURE_SIGBUS), signal_test = 1, we register
909 * monitored area, generate pagefaults and test that signal is delivered.
910 * Use UFFDIO_COPY to allocate missing page and retry. For signal_test = 2
911 * test robustness use case - we release monitored area, fork a process
912 * that will generate pagefaults and verify signal is generated.
913 * This also tests UFFD_FEATURE_EVENT_FORK event along with the signal
914 * feature. Using monitor thread, verify no userfault events are generated.
916 static int faulting_process(int signal_test)
919 unsigned long long count;
920 unsigned long split_nr_pages;
921 unsigned long lastnr;
922 struct sigaction act;
923 unsigned long signalled = 0;
925 if (test_type != TEST_HUGETLB)
926 split_nr_pages = (nr_pages + 1) / 2;
928 split_nr_pages = nr_pages;
932 memset(&act, 0, sizeof(act));
933 act.sa_sigaction = sighndl;
934 act.sa_flags = SA_SIGINFO;
935 if (sigaction(SIGBUS, &act, 0))
937 lastnr = (unsigned long)-1;
940 for (nr = 0; nr < split_nr_pages; nr++) {
942 unsigned long offset = nr * page_size;
945 if (sigsetjmp(*sigbuf, 1) != 0) {
946 if (steps == 1 && nr == lastnr)
947 err("Signal repeated");
950 if (signal_test == 1) {
952 /* This is a MISSING request */
954 if (copy_page(uffd, offset))
957 /* This is a WP request */
971 count = *area_count(area_dst, nr);
972 if (count != count_verify[nr])
973 err("nr %lu memory corruption %llu %llu\n",
974 nr, count, count_verify[nr]);
976 * Trigger write protection if there is by writing
977 * the same value back.
979 *area_count(area_dst, nr) = count;
983 return signalled != split_nr_pages;
985 if (test_type == TEST_HUGETLB)
988 area_dst = mremap(area_dst, nr_pages * page_size, nr_pages * page_size,
989 MREMAP_MAYMOVE | MREMAP_FIXED, area_src);
990 if (area_dst == MAP_FAILED)
992 /* Reset area_src since we just clobbered it */
995 for (; nr < nr_pages; nr++) {
996 count = *area_count(area_dst, nr);
997 if (count != count_verify[nr]) {
998 err("nr %lu memory corruption %llu %llu\n",
999 nr, count, count_verify[nr]);
1002 * Trigger write protection if there is by writing
1003 * the same value back.
1005 *area_count(area_dst, nr) = count;
1008 uffd_test_ops->release_pages(area_dst);
1010 for (nr = 0; nr < nr_pages; nr++)
1011 if (my_bcmp(area_dst + nr * page_size, zeropage, page_size))
1012 err("nr %lu is not zero", nr);
1017 static void retry_uffdio_zeropage(int ufd,
1018 struct uffdio_zeropage *uffdio_zeropage,
1019 unsigned long offset)
1021 uffd_test_ops->alias_mapping(&uffdio_zeropage->range.start,
1022 uffdio_zeropage->range.len,
1024 if (ioctl(ufd, UFFDIO_ZEROPAGE, uffdio_zeropage)) {
1025 if (uffdio_zeropage->zeropage != -EEXIST)
1026 err("UFFDIO_ZEROPAGE error: %"PRId64,
1027 (int64_t)uffdio_zeropage->zeropage);
1029 err("UFFDIO_ZEROPAGE error: %"PRId64,
1030 (int64_t)uffdio_zeropage->zeropage);
1034 static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
1036 struct uffdio_zeropage uffdio_zeropage;
1038 bool has_zeropage = get_expected_ioctls(0) & (1 << _UFFDIO_ZEROPAGE);
1041 if (offset >= nr_pages * page_size)
1042 err("unexpected offset %lu", offset);
1043 uffdio_zeropage.range.start = (unsigned long) area_dst + offset;
1044 uffdio_zeropage.range.len = page_size;
1045 uffdio_zeropage.mode = 0;
1046 ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage);
1047 res = uffdio_zeropage.zeropage;
1049 /* real retval in ufdio_zeropage.zeropage */
1051 err("UFFDIO_ZEROPAGE error: %"PRId64, (int64_t)res);
1052 else if (res != -EINVAL)
1053 err("UFFDIO_ZEROPAGE not -EINVAL");
1054 } else if (has_zeropage) {
1055 if (res != page_size) {
1056 err("UFFDIO_ZEROPAGE unexpected size");
1058 if (test_uffdio_zeropage_eexist && retry) {
1059 test_uffdio_zeropage_eexist = false;
1060 retry_uffdio_zeropage(ufd, &uffdio_zeropage,
1066 err("UFFDIO_ZEROPAGE succeeded");
1071 static int uffdio_zeropage(int ufd, unsigned long offset)
1073 return __uffdio_zeropage(ufd, offset, false);
1076 /* exercise UFFDIO_ZEROPAGE */
1077 static int userfaultfd_zeropage_test(void)
1079 struct uffdio_register uffdio_register;
1081 printf("testing UFFDIO_ZEROPAGE: ");
1084 uffd_test_ctx_init(0);
1086 uffdio_register.range.start = (unsigned long) area_dst;
1087 uffdio_register.range.len = nr_pages * page_size;
1088 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
1090 uffdio_register.mode |= UFFDIO_REGISTER_MODE_WP;
1091 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
1092 err("register failure");
1094 assert_expected_ioctls_present(
1095 uffdio_register.mode, uffdio_register.ioctls);
1097 if (uffdio_zeropage(uffd, 0))
1098 if (my_bcmp(area_dst, zeropage, page_size))
1099 err("zeropage is not zero");
1105 static int userfaultfd_events_test(void)
1107 struct uffdio_register uffdio_register;
1112 struct uffd_stats stats = { 0 };
1114 printf("testing events (fork, remap, remove): ");
1117 features = UFFD_FEATURE_EVENT_FORK | UFFD_FEATURE_EVENT_REMAP |
1118 UFFD_FEATURE_EVENT_REMOVE;
1119 uffd_test_ctx_init(features);
1121 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
1123 uffdio_register.range.start = (unsigned long) area_dst;
1124 uffdio_register.range.len = nr_pages * page_size;
1125 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
1127 uffdio_register.mode |= UFFDIO_REGISTER_MODE_WP;
1128 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
1129 err("register failure");
1131 assert_expected_ioctls_present(
1132 uffdio_register.mode, uffdio_register.ioctls);
1134 if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, &stats))
1135 err("uffd_poll_thread create");
1142 exit(faulting_process(0));
1144 waitpid(pid, &err, 0);
1146 err("faulting process failed");
1147 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
1149 if (pthread_join(uffd_mon, NULL))
1152 uffd_stats_report(&stats, 1);
1154 return stats.missing_faults != nr_pages;
1157 static int userfaultfd_sig_test(void)
1159 struct uffdio_register uffdio_register;
1160 unsigned long userfaults;
1165 struct uffd_stats stats = { 0 };
1167 printf("testing signal delivery: ");
1170 features = UFFD_FEATURE_EVENT_FORK|UFFD_FEATURE_SIGBUS;
1171 uffd_test_ctx_init(features);
1173 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
1175 uffdio_register.range.start = (unsigned long) area_dst;
1176 uffdio_register.range.len = nr_pages * page_size;
1177 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
1179 uffdio_register.mode |= UFFDIO_REGISTER_MODE_WP;
1180 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
1181 err("register failure");
1183 assert_expected_ioctls_present(
1184 uffdio_register.mode, uffdio_register.ioctls);
1186 if (faulting_process(1))
1187 err("faulting process failed");
1189 uffd_test_ops->release_pages(area_dst);
1191 if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, &stats))
1192 err("uffd_poll_thread create");
1199 exit(faulting_process(2));
1201 waitpid(pid, &err, 0);
1203 err("faulting process failed");
1204 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
1206 if (pthread_join(uffd_mon, (void **)&userfaults))
1211 err("Signal test failed, userfaults: %ld", userfaults);
1213 return userfaults != 0;
1216 static int userfaultfd_minor_test(void)
1218 struct uffdio_register uffdio_register;
1221 uint8_t expected_byte;
1222 void *expected_page;
1224 struct uffd_stats stats = { 0 };
1226 if (!test_uffdio_minor)
1229 printf("testing minor faults: ");
1232 uffd_test_ctx_init(uffd_minor_feature());
1234 uffdio_register.range.start = (unsigned long)area_dst_alias;
1235 uffdio_register.range.len = nr_pages * page_size;
1236 uffdio_register.mode = UFFDIO_REGISTER_MODE_MINOR;
1237 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
1238 err("register failure");
1240 assert_expected_ioctls_present(
1241 uffdio_register.mode, uffdio_register.ioctls);
1244 * After registering with UFFD, populate the non-UFFD-registered side of
1245 * the shared mapping. This should *not* trigger any UFFD minor faults.
1247 for (p = 0; p < nr_pages; ++p) {
1248 memset(area_dst + (p * page_size), p % ((uint8_t)-1),
1252 if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, &stats))
1253 err("uffd_poll_thread create");
1256 * Read each of the pages back using the UFFD-registered mapping. We
1257 * expect that the first time we touch a page, it will result in a minor
1258 * fault. uffd_poll_thread will resolve the fault by bit-flipping the
1259 * page's contents, and then issuing a CONTINUE ioctl.
1262 if (posix_memalign(&expected_page, page_size, page_size))
1263 err("out of memory");
1265 for (p = 0; p < nr_pages; ++p) {
1266 expected_byte = ~((uint8_t)(p % ((uint8_t)-1)));
1267 memset(expected_page, expected_byte, page_size);
1268 if (my_bcmp(expected_page, area_dst_alias + (p * page_size),
1270 err("unexpected page contents after minor fault");
1273 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
1275 if (pthread_join(uffd_mon, NULL))
1278 uffd_stats_report(&stats, 1);
1280 return stats.missing_faults != 0 || stats.minor_faults != nr_pages;
1283 #define BIT_ULL(nr) (1ULL << (nr))
1284 #define PM_SOFT_DIRTY BIT_ULL(55)
1285 #define PM_MMAP_EXCLUSIVE BIT_ULL(56)
1286 #define PM_UFFD_WP BIT_ULL(57)
1287 #define PM_FILE BIT_ULL(61)
1288 #define PM_SWAP BIT_ULL(62)
1289 #define PM_PRESENT BIT_ULL(63)
1291 static int pagemap_open(void)
1293 int fd = open("/proc/self/pagemap", O_RDONLY);
1296 err("open pagemap");
1301 static uint64_t pagemap_read_vaddr(int fd, void *vaddr)
1306 ret = pread(fd, &value, sizeof(uint64_t),
1307 ((uint64_t)vaddr >> 12) * sizeof(uint64_t));
1308 if (ret != sizeof(uint64_t))
1309 err("pread() on pagemap failed");
1314 /* This macro let __LINE__ works in err() */
1315 #define pagemap_check_wp(value, wp) do { \
1316 if (!!(value & PM_UFFD_WP) != wp) \
1317 err("pagemap uffd-wp bit error: 0x%"PRIx64, value); \
1320 static int pagemap_test_fork(bool present)
1322 pid_t child = fork();
1327 /* Open the pagemap fd of the child itself */
1328 fd = pagemap_open();
1329 value = pagemap_read_vaddr(fd, area_dst);
1331 * After fork() uffd-wp bit should be gone as long as we're
1332 * without UFFD_FEATURE_EVENT_FORK
1334 pagemap_check_wp(value, false);
1338 waitpid(child, &result, 0);
1342 static void userfaultfd_pagemap_test(unsigned int test_pgsize)
1344 struct uffdio_register uffdio_register;
1348 /* Pagemap tests uffd-wp only */
1349 if (!test_uffdio_wp)
1352 /* Not enough memory to test this page size */
1353 if (test_pgsize > nr_pages * page_size)
1356 printf("testing uffd-wp with pagemap (pgsize=%u): ", test_pgsize);
1357 /* Flush so it doesn't flush twice in parent/child later */
1360 uffd_test_ctx_init(0);
1362 if (test_pgsize > page_size) {
1363 /* This is a thp test */
1364 if (madvise(area_dst, nr_pages * page_size, MADV_HUGEPAGE))
1365 err("madvise(MADV_HUGEPAGE) failed");
1366 } else if (test_pgsize == page_size) {
1367 /* This is normal page test; force no thp */
1368 if (madvise(area_dst, nr_pages * page_size, MADV_NOHUGEPAGE))
1369 err("madvise(MADV_NOHUGEPAGE) failed");
1372 uffdio_register.range.start = (unsigned long) area_dst;
1373 uffdio_register.range.len = nr_pages * page_size;
1374 uffdio_register.mode = UFFDIO_REGISTER_MODE_WP;
1375 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
1376 err("register failed");
1378 pagemap_fd = pagemap_open();
1380 /* Touch the page */
1382 wp_range(uffd, (uint64_t)area_dst, test_pgsize, true);
1383 value = pagemap_read_vaddr(pagemap_fd, area_dst);
1384 pagemap_check_wp(value, true);
1385 /* Make sure uffd-wp bit dropped when fork */
1386 if (pagemap_test_fork(true))
1387 err("Detected stall uffd-wp bit in child");
1389 /* Exclusive required or PAGEOUT won't work */
1390 if (!(value & PM_MMAP_EXCLUSIVE))
1391 err("multiple mapping detected: 0x%"PRIx64, value);
1393 if (madvise(area_dst, test_pgsize, MADV_PAGEOUT))
1394 err("madvise(MADV_PAGEOUT) failed");
1396 /* Uffd-wp should persist even swapped out */
1397 value = pagemap_read_vaddr(pagemap_fd, area_dst);
1398 pagemap_check_wp(value, true);
1399 /* Make sure uffd-wp bit dropped when fork */
1400 if (pagemap_test_fork(false))
1401 err("Detected stall uffd-wp bit in child");
1403 /* Unprotect; this tests swap pte modifications */
1404 wp_range(uffd, (uint64_t)area_dst, page_size, false);
1405 value = pagemap_read_vaddr(pagemap_fd, area_dst);
1406 pagemap_check_wp(value, false);
1408 /* Fault in the page from disk */
1410 value = pagemap_read_vaddr(pagemap_fd, area_dst);
1411 pagemap_check_wp(value, false);
1417 static int userfaultfd_stress(void)
1422 struct uffdio_register uffdio_register;
1423 struct uffd_stats uffd_stats[nr_cpus];
1425 uffd_test_ctx_init(0);
1427 if (posix_memalign(&area, page_size, page_size))
1428 err("out of memory");
1430 bzero(zeropage, page_size);
1432 pthread_mutex_lock(&uffd_read_mutex);
1434 pthread_attr_init(&attr);
1435 pthread_attr_setstacksize(&attr, 16*1024*1024);
1438 printf("bounces: %d, mode:", bounces);
1439 if (bounces & BOUNCE_RANDOM)
1441 if (bounces & BOUNCE_RACINGFAULTS)
1443 if (bounces & BOUNCE_VERIFY)
1445 if (bounces & BOUNCE_POLL)
1452 if (bounces & BOUNCE_POLL)
1453 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
1455 fcntl(uffd, F_SETFL, uffd_flags & ~O_NONBLOCK);
1458 uffdio_register.range.start = (unsigned long) area_dst;
1459 uffdio_register.range.len = nr_pages * page_size;
1460 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
1462 uffdio_register.mode |= UFFDIO_REGISTER_MODE_WP;
1463 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
1464 err("register failure");
1465 assert_expected_ioctls_present(
1466 uffdio_register.mode, uffdio_register.ioctls);
1468 if (area_dst_alias) {
1469 uffdio_register.range.start = (unsigned long)
1471 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
1472 err("register failure alias");
1476 * The madvise done previously isn't enough: some
1477 * uffd_thread could have read userfaults (one of
1478 * those already resolved by the background thread)
1479 * and it may be in the process of calling
1480 * UFFDIO_COPY. UFFDIO_COPY will read the zapped
1481 * area_src and it would map a zero page in it (of
1482 * course such a UFFDIO_COPY is perfectly safe as it'd
1483 * return -EEXIST). The problem comes at the next
1484 * bounce though: that racing UFFDIO_COPY would
1485 * generate zeropages in the area_src, so invalidating
1486 * the previous MADV_DONTNEED. Without this additional
1487 * MADV_DONTNEED those zeropages leftovers in the
1488 * area_src would lead to -EEXIST failure during the
1489 * next bounce, effectively leaving a zeropage in the
1492 * Try to comment this out madvise to see the memory
1493 * corruption being caught pretty quick.
1495 * khugepaged is also inhibited to collapse THP after
1496 * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's
1497 * required to MADV_DONTNEED here.
1499 uffd_test_ops->release_pages(area_dst);
1501 uffd_stats_reset(uffd_stats, nr_cpus);
1504 if (stress(uffd_stats))
1507 /* Clear all the write protections if there is any */
1509 wp_range(uffd, (unsigned long)area_dst,
1510 nr_pages * page_size, false);
1513 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range))
1514 err("unregister failure");
1515 if (area_dst_alias) {
1516 uffdio_register.range.start = (unsigned long) area_dst;
1517 if (ioctl(uffd, UFFDIO_UNREGISTER,
1518 &uffdio_register.range))
1519 err("unregister failure alias");
1523 if (bounces & BOUNCE_VERIFY)
1524 for (nr = 0; nr < nr_pages; nr++)
1525 if (*area_count(area_dst, nr) != count_verify[nr])
1526 err("error area_count %llu %llu %lu\n",
1527 *area_count(area_src, nr),
1528 count_verify[nr], nr);
1530 /* prepare next bounce */
1531 tmp_area = area_src;
1532 area_src = area_dst;
1533 area_dst = tmp_area;
1535 tmp_area = area_src_alias;
1536 area_src_alias = area_dst_alias;
1537 area_dst_alias = tmp_area;
1539 uffd_stats_report(uffd_stats, nr_cpus);
1542 if (test_type == TEST_ANON) {
1544 * shmem/hugetlb won't be able to run since they have different
1545 * behavior on fork() (file-backed memory normally drops ptes
1546 * directly when fork), meanwhile the pagemap test will verify
1547 * pgtable entry of fork()ed child.
1549 userfaultfd_pagemap_test(page_size);
1551 * Hard-code for x86_64 for now for 2M THP, as x86_64 is
1552 * currently the only one that supports uffd-wp
1554 userfaultfd_pagemap_test(page_size * 512);
1557 return userfaultfd_zeropage_test() || userfaultfd_sig_test()
1558 || userfaultfd_events_test() || userfaultfd_minor_test();
1562 * Copied from mlock2-tests.c
1564 unsigned long default_huge_page_size(void)
1566 unsigned long hps = 0;
1569 FILE *f = fopen("/proc/meminfo", "r");
1573 while (getline(&line, &linelen, f) > 0) {
1574 if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
1585 static void set_test_type(const char *type)
1587 uint64_t features = UFFD_API_FEATURES;
1589 if (!strcmp(type, "anon")) {
1590 test_type = TEST_ANON;
1591 uffd_test_ops = &anon_uffd_test_ops;
1592 /* Only enable write-protect test for anonymous test */
1593 test_uffdio_wp = true;
1594 } else if (!strcmp(type, "hugetlb")) {
1595 test_type = TEST_HUGETLB;
1596 uffd_test_ops = &hugetlb_uffd_test_ops;
1597 } else if (!strcmp(type, "hugetlb_shared")) {
1599 test_type = TEST_HUGETLB;
1600 uffd_test_ops = &hugetlb_uffd_test_ops;
1601 /* Minor faults require shared hugetlb; only enable here. */
1602 test_uffdio_minor = true;
1603 } else if (!strcmp(type, "shmem")) {
1605 test_type = TEST_SHMEM;
1606 uffd_test_ops = &shmem_uffd_test_ops;
1607 test_uffdio_minor = true;
1609 err("Unknown test type: %s", type);
1612 if (test_type == TEST_HUGETLB)
1613 page_size = default_huge_page_size();
1615 page_size = sysconf(_SC_PAGE_SIZE);
1618 err("Unable to determine page size");
1619 if ((unsigned long) area_count(NULL, 0) + sizeof(unsigned long long) * 2
1621 err("Impossible to run this test");
1624 * Whether we can test certain features depends not just on test type,
1625 * but also on whether or not this particular kernel supports the
1629 userfaultfd_open(&features);
1631 test_uffdio_wp = test_uffdio_wp &&
1632 (features & UFFD_FEATURE_PAGEFAULT_FLAG_WP);
1633 test_uffdio_minor = test_uffdio_minor &&
1634 (features & uffd_minor_feature());
1640 static void sigalrm(int sig)
1644 test_uffdio_copy_eexist = true;
1645 test_uffdio_zeropage_eexist = true;
1646 alarm(ALARM_INTERVAL_SECS);
1649 int main(int argc, char **argv)
1654 if (signal(SIGALRM, sigalrm) == SIG_ERR)
1655 err("failed to arm SIGALRM");
1656 alarm(ALARM_INTERVAL_SECS);
1658 set_test_type(argv[1]);
1660 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1661 nr_pages_per_cpu = atol(argv[2]) * 1024*1024 / page_size /
1663 if (!nr_pages_per_cpu) {
1664 _err("invalid MiB");
1668 bounces = atoi(argv[3]);
1670 _err("invalid bounces");
1673 nr_pages = nr_pages_per_cpu * nr_cpus;
1675 if (test_type == TEST_HUGETLB) {
1678 huge_fd = open(argv[4], O_CREAT | O_RDWR, 0755);
1680 err("Open of %s failed", argv[4]);
1681 if (ftruncate(huge_fd, 0))
1682 err("ftruncate %s to size 0 failed", argv[4]);
1683 } else if (test_type == TEST_SHMEM) {
1684 shm_fd = memfd_create(argv[0], 0);
1686 err("memfd_create");
1687 if (ftruncate(shm_fd, nr_pages * page_size * 2))
1689 if (fallocate(shm_fd,
1690 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0,
1691 nr_pages * page_size * 2))
1694 printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
1695 nr_pages, nr_pages_per_cpu);
1696 return userfaultfd_stress();
1699 #else /* __NR_userfaultfd */
1701 #warning "missing __NR_userfaultfd definition"
1705 printf("skip: Skipping userfaultfd test (missing __NR_userfaultfd)\n");
1709 #endif /* __NR_userfaultfd */