userfaultfd: selftest: cleanup help messages
[muen/linux.git] / tools / testing / selftests / vm / userfaultfd.c
1 /*
2  * Stress userfaultfd syscall.
3  *
4  *  Copyright (C) 2015  Red Hat, Inc.
5  *
6  *  This work is licensed under the terms of the GNU GPL, version 2. See
7  *  the COPYING file in the top-level directory.
8  *
9  * This test allocates two virtual areas and bounces the physical
10  * memory across the two virtual areas (from area_src to area_dst)
11  * using userfaultfd.
12  *
13  * There are three threads running per CPU:
14  *
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.
19  *
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.
23  *
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
27  *    area.
28  *
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
32  * opposite direction.
33  *
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).
37  */
38
39 #define _GNU_SOURCE
40 #include <stdio.h>
41 #include <errno.h>
42 #include <unistd.h>
43 #include <stdlib.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #include <time.h>
48 #include <signal.h>
49 #include <poll.h>
50 #include <string.h>
51 #include <sys/mman.h>
52 #include <sys/syscall.h>
53 #include <sys/ioctl.h>
54 #include <sys/wait.h>
55 #include <pthread.h>
56 #include <linux/userfaultfd.h>
57 #include <setjmp.h>
58 #include <stdbool.h>
59
60 #include "../kselftest.h"
61
62 #ifdef __NR_userfaultfd
63
64 static unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;
65
66 #define BOUNCE_RANDOM           (1<<0)
67 #define BOUNCE_RACINGFAULTS     (1<<1)
68 #define BOUNCE_VERIFY           (1<<2)
69 #define BOUNCE_POLL             (1<<3)
70 static int bounces;
71
72 #define TEST_ANON       1
73 #define TEST_HUGETLB    2
74 #define TEST_SHMEM      3
75 static int test_type;
76
77 /* exercise the test_uffdio_*_eexist every ALARM_INTERVAL_SECS */
78 #define ALARM_INTERVAL_SECS 10
79 static volatile bool test_uffdio_copy_eexist = true;
80 static volatile bool test_uffdio_zeropage_eexist = true;
81
82 static bool map_shared;
83 static int huge_fd;
84 static char *huge_fd_off0;
85 static unsigned long long *count_verify;
86 static int uffd, uffd_flags, finished, *pipefd;
87 static char *area_src, *area_src_alias, *area_dst, *area_dst_alias;
88 static char *zeropage;
89 pthread_attr_t attr;
90
91 /* pthread_mutex_t starts at page offset 0 */
92 #define area_mutex(___area, ___nr)                                      \
93         ((pthread_mutex_t *) ((___area) + (___nr)*page_size))
94 /*
95  * count is placed in the page after pthread_mutex_t naturally aligned
96  * to avoid non alignment faults on non-x86 archs.
97  */
98 #define area_count(___area, ___nr)                                      \
99         ((volatile unsigned long long *) ((unsigned long)               \
100                                  ((___area) + (___nr)*page_size +       \
101                                   sizeof(pthread_mutex_t) +             \
102                                   sizeof(unsigned long long) - 1) &     \
103                                  ~(unsigned long)(sizeof(unsigned long long) \
104                                                   -  1)))
105
106 const char *examples =
107     "# Run anonymous memory test on 100MiB region with 99999 bounces:\n"
108     "./userfaultfd anon 100 99999\n\n"
109     "# Run share memory test on 1GiB region with 99 bounces:\n"
110     "./userfaultfd shmem 1000 99\n\n"
111     "# Run hugetlb memory test on 256MiB region with 50 bounces (using /dev/hugepages/hugefile):\n"
112     "./userfaultfd hugetlb 256 50 /dev/hugepages/hugefile\n\n"
113     "# Run the same hugetlb test but using shmem:\n"
114     "./userfaultfd hugetlb_shared 256 50 /dev/hugepages/hugefile\n\n"
115     "# 10MiB-~6GiB 999 bounces anonymous test, "
116     "continue forever unless an error triggers\n"
117     "while ./userfaultfd anon $[RANDOM % 6000 + 10] 999; do true; done\n\n";
118
119 static void usage(void)
120 {
121         fprintf(stderr, "\nUsage: ./userfaultfd <test type> <MiB> <bounces> "
122                 "[hugetlbfs_file]\n\n");
123         fprintf(stderr, "Supported <test type>: anon, hugetlb, "
124                 "hugetlb_shared, shmem\n\n");
125         fprintf(stderr, "Examples:\n\n");
126         fprintf(stderr, examples);
127         exit(1);
128 }
129
130 static int anon_release_pages(char *rel_area)
131 {
132         int ret = 0;
133
134         if (madvise(rel_area, nr_pages * page_size, MADV_DONTNEED)) {
135                 perror("madvise");
136                 ret = 1;
137         }
138
139         return ret;
140 }
141
142 static void anon_allocate_area(void **alloc_area)
143 {
144         if (posix_memalign(alloc_area, page_size, nr_pages * page_size)) {
145                 fprintf(stderr, "out of memory\n");
146                 *alloc_area = NULL;
147         }
148 }
149
150 static void noop_alias_mapping(__u64 *start, size_t len, unsigned long offset)
151 {
152 }
153
154 /* HugeTLB memory */
155 static int hugetlb_release_pages(char *rel_area)
156 {
157         int ret = 0;
158
159         if (fallocate(huge_fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
160                                 rel_area == huge_fd_off0 ? 0 :
161                                 nr_pages * page_size,
162                                 nr_pages * page_size)) {
163                 perror("fallocate");
164                 ret = 1;
165         }
166
167         return ret;
168 }
169
170
171 static void hugetlb_allocate_area(void **alloc_area)
172 {
173         void *area_alias = NULL;
174         char **alloc_area_alias;
175         *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
176                            (map_shared ? MAP_SHARED : MAP_PRIVATE) |
177                            MAP_HUGETLB,
178                            huge_fd, *alloc_area == area_src ? 0 :
179                            nr_pages * page_size);
180         if (*alloc_area == MAP_FAILED) {
181                 fprintf(stderr, "mmap of hugetlbfs file failed\n");
182                 *alloc_area = NULL;
183         }
184
185         if (map_shared) {
186                 area_alias = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
187                                   MAP_SHARED | MAP_HUGETLB,
188                                   huge_fd, *alloc_area == area_src ? 0 :
189                                   nr_pages * page_size);
190                 if (area_alias == MAP_FAILED) {
191                         if (munmap(*alloc_area, nr_pages * page_size) < 0)
192                                 perror("hugetlb munmap"), exit(1);
193                         *alloc_area = NULL;
194                         return;
195                 }
196         }
197         if (*alloc_area == area_src) {
198                 huge_fd_off0 = *alloc_area;
199                 alloc_area_alias = &area_src_alias;
200         } else {
201                 alloc_area_alias = &area_dst_alias;
202         }
203         if (area_alias)
204                 *alloc_area_alias = area_alias;
205 }
206
207 static void hugetlb_alias_mapping(__u64 *start, size_t len, unsigned long offset)
208 {
209         if (!map_shared)
210                 return;
211         /*
212          * We can't zap just the pagetable with hugetlbfs because
213          * MADV_DONTEED won't work. So exercise -EEXIST on a alias
214          * mapping where the pagetables are not established initially,
215          * this way we'll exercise the -EEXEC at the fs level.
216          */
217         *start = (unsigned long) area_dst_alias + offset;
218 }
219
220 /* Shared memory */
221 static int shmem_release_pages(char *rel_area)
222 {
223         int ret = 0;
224
225         if (madvise(rel_area, nr_pages * page_size, MADV_REMOVE)) {
226                 perror("madvise");
227                 ret = 1;
228         }
229
230         return ret;
231 }
232
233 static void shmem_allocate_area(void **alloc_area)
234 {
235         *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
236                            MAP_ANONYMOUS | MAP_SHARED, -1, 0);
237         if (*alloc_area == MAP_FAILED) {
238                 fprintf(stderr, "shared memory mmap failed\n");
239                 *alloc_area = NULL;
240         }
241 }
242
243 struct uffd_test_ops {
244         unsigned long expected_ioctls;
245         void (*allocate_area)(void **alloc_area);
246         int (*release_pages)(char *rel_area);
247         void (*alias_mapping)(__u64 *start, size_t len, unsigned long offset);
248 };
249
250 #define ANON_EXPECTED_IOCTLS            ((1 << _UFFDIO_WAKE) | \
251                                          (1 << _UFFDIO_COPY) | \
252                                          (1 << _UFFDIO_ZEROPAGE))
253
254 static struct uffd_test_ops anon_uffd_test_ops = {
255         .expected_ioctls = ANON_EXPECTED_IOCTLS,
256         .allocate_area  = anon_allocate_area,
257         .release_pages  = anon_release_pages,
258         .alias_mapping = noop_alias_mapping,
259 };
260
261 static struct uffd_test_ops shmem_uffd_test_ops = {
262         .expected_ioctls = ANON_EXPECTED_IOCTLS,
263         .allocate_area  = shmem_allocate_area,
264         .release_pages  = shmem_release_pages,
265         .alias_mapping = noop_alias_mapping,
266 };
267
268 static struct uffd_test_ops hugetlb_uffd_test_ops = {
269         .expected_ioctls = UFFD_API_RANGE_IOCTLS_BASIC,
270         .allocate_area  = hugetlb_allocate_area,
271         .release_pages  = hugetlb_release_pages,
272         .alias_mapping = hugetlb_alias_mapping,
273 };
274
275 static struct uffd_test_ops *uffd_test_ops;
276
277 static int my_bcmp(char *str1, char *str2, size_t n)
278 {
279         unsigned long i;
280         for (i = 0; i < n; i++)
281                 if (str1[i] != str2[i])
282                         return 1;
283         return 0;
284 }
285
286 static void *locking_thread(void *arg)
287 {
288         unsigned long cpu = (unsigned long) arg;
289         struct random_data rand;
290         unsigned long page_nr = *(&(page_nr)); /* uninitialized warning */
291         int32_t rand_nr;
292         unsigned long long count;
293         char randstate[64];
294         unsigned int seed;
295         time_t start;
296
297         if (bounces & BOUNCE_RANDOM) {
298                 seed = (unsigned int) time(NULL) - bounces;
299                 if (!(bounces & BOUNCE_RACINGFAULTS))
300                         seed += cpu;
301                 bzero(&rand, sizeof(rand));
302                 bzero(&randstate, sizeof(randstate));
303                 if (initstate_r(seed, randstate, sizeof(randstate), &rand))
304                         fprintf(stderr, "srandom_r error\n"), exit(1);
305         } else {
306                 page_nr = -bounces;
307                 if (!(bounces & BOUNCE_RACINGFAULTS))
308                         page_nr += cpu * nr_pages_per_cpu;
309         }
310
311         while (!finished) {
312                 if (bounces & BOUNCE_RANDOM) {
313                         if (random_r(&rand, &rand_nr))
314                                 fprintf(stderr, "random_r 1 error\n"), exit(1);
315                         page_nr = rand_nr;
316                         if (sizeof(page_nr) > sizeof(rand_nr)) {
317                                 if (random_r(&rand, &rand_nr))
318                                         fprintf(stderr, "random_r 2 error\n"), exit(1);
319                                 page_nr |= (((unsigned long) rand_nr) << 16) <<
320                                            16;
321                         }
322                 } else
323                         page_nr += 1;
324                 page_nr %= nr_pages;
325
326                 start = time(NULL);
327                 if (bounces & BOUNCE_VERIFY) {
328                         count = *area_count(area_dst, page_nr);
329                         if (!count)
330                                 fprintf(stderr,
331                                         "page_nr %lu wrong count %Lu %Lu\n",
332                                         page_nr, count,
333                                         count_verify[page_nr]), exit(1);
334
335
336                         /*
337                          * We can't use bcmp (or memcmp) because that
338                          * returns 0 erroneously if the memory is
339                          * changing under it (even if the end of the
340                          * page is never changing and always
341                          * different).
342                          */
343 #if 1
344                         if (!my_bcmp(area_dst + page_nr * page_size, zeropage,
345                                      page_size))
346                                 fprintf(stderr,
347                                         "my_bcmp page_nr %lu wrong count %Lu %Lu\n",
348                                         page_nr, count,
349                                         count_verify[page_nr]), exit(1);
350 #else
351                         unsigned long loops;
352
353                         loops = 0;
354                         /* uncomment the below line to test with mutex */
355                         /* pthread_mutex_lock(area_mutex(area_dst, page_nr)); */
356                         while (!bcmp(area_dst + page_nr * page_size, zeropage,
357                                      page_size)) {
358                                 loops += 1;
359                                 if (loops > 10)
360                                         break;
361                         }
362                         /* uncomment below line to test with mutex */
363                         /* pthread_mutex_unlock(area_mutex(area_dst, page_nr)); */
364                         if (loops) {
365                                 fprintf(stderr,
366                                         "page_nr %lu all zero thread %lu %p %lu\n",
367                                         page_nr, cpu, area_dst + page_nr * page_size,
368                                         loops);
369                                 if (loops > 10)
370                                         exit(1);
371                         }
372 #endif
373                 }
374
375                 pthread_mutex_lock(area_mutex(area_dst, page_nr));
376                 count = *area_count(area_dst, page_nr);
377                 if (count != count_verify[page_nr]) {
378                         fprintf(stderr,
379                                 "page_nr %lu memory corruption %Lu %Lu\n",
380                                 page_nr, count,
381                                 count_verify[page_nr]), exit(1);
382                 }
383                 count++;
384                 *area_count(area_dst, page_nr) = count_verify[page_nr] = count;
385                 pthread_mutex_unlock(area_mutex(area_dst, page_nr));
386
387                 if (time(NULL) - start > 1)
388                         fprintf(stderr,
389                                 "userfault too slow %ld "
390                                 "possible false positive with overcommit\n",
391                                 time(NULL) - start);
392         }
393
394         return NULL;
395 }
396
397 static void retry_copy_page(int ufd, struct uffdio_copy *uffdio_copy,
398                             unsigned long offset)
399 {
400         uffd_test_ops->alias_mapping(&uffdio_copy->dst,
401                                      uffdio_copy->len,
402                                      offset);
403         if (ioctl(ufd, UFFDIO_COPY, uffdio_copy)) {
404                 /* real retval in ufdio_copy.copy */
405                 if (uffdio_copy->copy != -EEXIST)
406                         fprintf(stderr, "UFFDIO_COPY retry error %Ld\n",
407                                 uffdio_copy->copy), exit(1);
408         } else {
409                 fprintf(stderr, "UFFDIO_COPY retry unexpected %Ld\n",
410                         uffdio_copy->copy), exit(1);
411         }
412 }
413
414 static int __copy_page(int ufd, unsigned long offset, bool retry)
415 {
416         struct uffdio_copy uffdio_copy;
417
418         if (offset >= nr_pages * page_size)
419                 fprintf(stderr, "unexpected offset %lu\n",
420                         offset), exit(1);
421         uffdio_copy.dst = (unsigned long) area_dst + offset;
422         uffdio_copy.src = (unsigned long) area_src + offset;
423         uffdio_copy.len = page_size;
424         uffdio_copy.mode = 0;
425         uffdio_copy.copy = 0;
426         if (ioctl(ufd, UFFDIO_COPY, &uffdio_copy)) {
427                 /* real retval in ufdio_copy.copy */
428                 if (uffdio_copy.copy != -EEXIST)
429                         fprintf(stderr, "UFFDIO_COPY error %Ld\n",
430                                 uffdio_copy.copy), exit(1);
431         } else if (uffdio_copy.copy != page_size) {
432                 fprintf(stderr, "UFFDIO_COPY unexpected copy %Ld\n",
433                         uffdio_copy.copy), exit(1);
434         } else {
435                 if (test_uffdio_copy_eexist && retry) {
436                         test_uffdio_copy_eexist = false;
437                         retry_copy_page(ufd, &uffdio_copy, offset);
438                 }
439                 return 1;
440         }
441         return 0;
442 }
443
444 static int copy_page_retry(int ufd, unsigned long offset)
445 {
446         return __copy_page(ufd, offset, true);
447 }
448
449 static int copy_page(int ufd, unsigned long offset)
450 {
451         return __copy_page(ufd, offset, false);
452 }
453
454 static void *uffd_poll_thread(void *arg)
455 {
456         unsigned long cpu = (unsigned long) arg;
457         struct pollfd pollfd[2];
458         struct uffd_msg msg;
459         struct uffdio_register uffd_reg;
460         int ret;
461         unsigned long offset;
462         char tmp_chr;
463         unsigned long userfaults = 0;
464
465         pollfd[0].fd = uffd;
466         pollfd[0].events = POLLIN;
467         pollfd[1].fd = pipefd[cpu*2];
468         pollfd[1].events = POLLIN;
469
470         for (;;) {
471                 ret = poll(pollfd, 2, -1);
472                 if (!ret)
473                         fprintf(stderr, "poll error %d\n", ret), exit(1);
474                 if (ret < 0)
475                         perror("poll"), exit(1);
476                 if (pollfd[1].revents & POLLIN) {
477                         if (read(pollfd[1].fd, &tmp_chr, 1) != 1)
478                                 fprintf(stderr, "read pipefd error\n"),
479                                         exit(1);
480                         break;
481                 }
482                 if (!(pollfd[0].revents & POLLIN))
483                         fprintf(stderr, "pollfd[0].revents %d\n",
484                                 pollfd[0].revents), exit(1);
485                 ret = read(uffd, &msg, sizeof(msg));
486                 if (ret < 0) {
487                         if (errno == EAGAIN)
488                                 continue;
489                         perror("nonblocking read error"), exit(1);
490                 }
491                 switch (msg.event) {
492                 default:
493                         fprintf(stderr, "unexpected msg event %u\n",
494                                 msg.event), exit(1);
495                         break;
496                 case UFFD_EVENT_PAGEFAULT:
497                         if (msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
498                                 fprintf(stderr, "unexpected write fault\n"), exit(1);
499                         offset = (char *)(unsigned long)msg.arg.pagefault.address -
500                                 area_dst;
501                         offset &= ~(page_size-1);
502                         if (copy_page(uffd, offset))
503                                 userfaults++;
504                         break;
505                 case UFFD_EVENT_FORK:
506                         close(uffd);
507                         uffd = msg.arg.fork.ufd;
508                         pollfd[0].fd = uffd;
509                         break;
510                 case UFFD_EVENT_REMOVE:
511                         uffd_reg.range.start = msg.arg.remove.start;
512                         uffd_reg.range.len = msg.arg.remove.end -
513                                 msg.arg.remove.start;
514                         if (ioctl(uffd, UFFDIO_UNREGISTER, &uffd_reg.range))
515                                 fprintf(stderr, "remove failure\n"), exit(1);
516                         break;
517                 case UFFD_EVENT_REMAP:
518                         area_dst = (char *)(unsigned long)msg.arg.remap.to;
519                         break;
520                 }
521         }
522         return (void *)userfaults;
523 }
524
525 pthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER;
526
527 static void *uffd_read_thread(void *arg)
528 {
529         unsigned long *this_cpu_userfaults;
530         struct uffd_msg msg;
531         unsigned long offset;
532         int ret;
533
534         this_cpu_userfaults = (unsigned long *) arg;
535         *this_cpu_userfaults = 0;
536
537         pthread_mutex_unlock(&uffd_read_mutex);
538         /* from here cancellation is ok */
539
540         for (;;) {
541                 ret = read(uffd, &msg, sizeof(msg));
542                 if (ret != sizeof(msg)) {
543                         if (ret < 0)
544                                 perror("blocking read error"), exit(1);
545                         else
546                                 fprintf(stderr, "short read\n"), exit(1);
547                 }
548                 if (msg.event != UFFD_EVENT_PAGEFAULT)
549                         fprintf(stderr, "unexpected msg event %u\n",
550                                 msg.event), exit(1);
551                 if (bounces & BOUNCE_VERIFY &&
552                     msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
553                         fprintf(stderr, "unexpected write fault\n"), exit(1);
554                 offset = (char *)(unsigned long)msg.arg.pagefault.address -
555                          area_dst;
556                 offset &= ~(page_size-1);
557                 if (copy_page(uffd, offset))
558                         (*this_cpu_userfaults)++;
559         }
560         return (void *)NULL;
561 }
562
563 static void *background_thread(void *arg)
564 {
565         unsigned long cpu = (unsigned long) arg;
566         unsigned long page_nr;
567
568         for (page_nr = cpu * nr_pages_per_cpu;
569              page_nr < (cpu+1) * nr_pages_per_cpu;
570              page_nr++)
571                 copy_page_retry(uffd, page_nr * page_size);
572
573         return NULL;
574 }
575
576 static int stress(unsigned long *userfaults)
577 {
578         unsigned long cpu;
579         pthread_t locking_threads[nr_cpus];
580         pthread_t uffd_threads[nr_cpus];
581         pthread_t background_threads[nr_cpus];
582         void **_userfaults = (void **) userfaults;
583
584         finished = 0;
585         for (cpu = 0; cpu < nr_cpus; cpu++) {
586                 if (pthread_create(&locking_threads[cpu], &attr,
587                                    locking_thread, (void *)cpu))
588                         return 1;
589                 if (bounces & BOUNCE_POLL) {
590                         if (pthread_create(&uffd_threads[cpu], &attr,
591                                            uffd_poll_thread, (void *)cpu))
592                                 return 1;
593                 } else {
594                         if (pthread_create(&uffd_threads[cpu], &attr,
595                                            uffd_read_thread,
596                                            &_userfaults[cpu]))
597                                 return 1;
598                         pthread_mutex_lock(&uffd_read_mutex);
599                 }
600                 if (pthread_create(&background_threads[cpu], &attr,
601                                    background_thread, (void *)cpu))
602                         return 1;
603         }
604         for (cpu = 0; cpu < nr_cpus; cpu++)
605                 if (pthread_join(background_threads[cpu], NULL))
606                         return 1;
607
608         /*
609          * Be strict and immediately zap area_src, the whole area has
610          * been transferred already by the background treads. The
611          * area_src could then be faulted in in a racy way by still
612          * running uffdio_threads reading zeropages after we zapped
613          * area_src (but they're guaranteed to get -EEXIST from
614          * UFFDIO_COPY without writing zero pages into area_dst
615          * because the background threads already completed).
616          */
617         if (uffd_test_ops->release_pages(area_src))
618                 return 1;
619
620         for (cpu = 0; cpu < nr_cpus; cpu++) {
621                 char c;
622                 if (bounces & BOUNCE_POLL) {
623                         if (write(pipefd[cpu*2+1], &c, 1) != 1) {
624                                 fprintf(stderr, "pipefd write error\n");
625                                 return 1;
626                         }
627                         if (pthread_join(uffd_threads[cpu], &_userfaults[cpu]))
628                                 return 1;
629                 } else {
630                         if (pthread_cancel(uffd_threads[cpu]))
631                                 return 1;
632                         if (pthread_join(uffd_threads[cpu], NULL))
633                                 return 1;
634                 }
635         }
636
637         finished = 1;
638         for (cpu = 0; cpu < nr_cpus; cpu++)
639                 if (pthread_join(locking_threads[cpu], NULL))
640                         return 1;
641
642         return 0;
643 }
644
645 static int userfaultfd_open(int features)
646 {
647         struct uffdio_api uffdio_api;
648
649         uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
650         if (uffd < 0) {
651                 fprintf(stderr,
652                         "userfaultfd syscall not available in this kernel\n");
653                 return 1;
654         }
655         uffd_flags = fcntl(uffd, F_GETFD, NULL);
656
657         uffdio_api.api = UFFD_API;
658         uffdio_api.features = features;
659         if (ioctl(uffd, UFFDIO_API, &uffdio_api)) {
660                 fprintf(stderr, "UFFDIO_API\n");
661                 return 1;
662         }
663         if (uffdio_api.api != UFFD_API) {
664                 fprintf(stderr, "UFFDIO_API error %Lu\n", uffdio_api.api);
665                 return 1;
666         }
667
668         return 0;
669 }
670
671 sigjmp_buf jbuf, *sigbuf;
672
673 static void sighndl(int sig, siginfo_t *siginfo, void *ptr)
674 {
675         if (sig == SIGBUS) {
676                 if (sigbuf)
677                         siglongjmp(*sigbuf, 1);
678                 abort();
679         }
680 }
681
682 /*
683  * For non-cooperative userfaultfd test we fork() a process that will
684  * generate pagefaults, will mremap the area monitored by the
685  * userfaultfd and at last this process will release the monitored
686  * area.
687  * For the anonymous and shared memory the area is divided into two
688  * parts, the first part is accessed before mremap, and the second
689  * part is accessed after mremap. Since hugetlbfs does not support
690  * mremap, the entire monitored area is accessed in a single pass for
691  * HUGETLB_TEST.
692  * The release of the pages currently generates event for shmem and
693  * anonymous memory (UFFD_EVENT_REMOVE), hence it is not checked
694  * for hugetlb.
695  * For signal test(UFFD_FEATURE_SIGBUS), signal_test = 1, we register
696  * monitored area, generate pagefaults and test that signal is delivered.
697  * Use UFFDIO_COPY to allocate missing page and retry. For signal_test = 2
698  * test robustness use case - we release monitored area, fork a process
699  * that will generate pagefaults and verify signal is generated.
700  * This also tests UFFD_FEATURE_EVENT_FORK event along with the signal
701  * feature. Using monitor thread, verify no userfault events are generated.
702  */
703 static int faulting_process(int signal_test)
704 {
705         unsigned long nr;
706         unsigned long long count;
707         unsigned long split_nr_pages;
708         unsigned long lastnr;
709         struct sigaction act;
710         unsigned long signalled = 0;
711
712         if (test_type != TEST_HUGETLB)
713                 split_nr_pages = (nr_pages + 1) / 2;
714         else
715                 split_nr_pages = nr_pages;
716
717         if (signal_test) {
718                 sigbuf = &jbuf;
719                 memset(&act, 0, sizeof(act));
720                 act.sa_sigaction = sighndl;
721                 act.sa_flags = SA_SIGINFO;
722                 if (sigaction(SIGBUS, &act, 0)) {
723                         perror("sigaction");
724                         return 1;
725                 }
726                 lastnr = (unsigned long)-1;
727         }
728
729         for (nr = 0; nr < split_nr_pages; nr++) {
730                 if (signal_test) {
731                         if (sigsetjmp(*sigbuf, 1) != 0) {
732                                 if (nr == lastnr) {
733                                         fprintf(stderr, "Signal repeated\n");
734                                         return 1;
735                                 }
736
737                                 lastnr = nr;
738                                 if (signal_test == 1) {
739                                         if (copy_page(uffd, nr * page_size))
740                                                 signalled++;
741                                 } else {
742                                         signalled++;
743                                         continue;
744                                 }
745                         }
746                 }
747
748                 count = *area_count(area_dst, nr);
749                 if (count != count_verify[nr]) {
750                         fprintf(stderr,
751                                 "nr %lu memory corruption %Lu %Lu\n",
752                                 nr, count,
753                                 count_verify[nr]), exit(1);
754                 }
755         }
756
757         if (signal_test)
758                 return signalled != split_nr_pages;
759
760         if (test_type == TEST_HUGETLB)
761                 return 0;
762
763         area_dst = mremap(area_dst, nr_pages * page_size,  nr_pages * page_size,
764                           MREMAP_MAYMOVE | MREMAP_FIXED, area_src);
765         if (area_dst == MAP_FAILED)
766                 perror("mremap"), exit(1);
767
768         for (; nr < nr_pages; nr++) {
769                 count = *area_count(area_dst, nr);
770                 if (count != count_verify[nr]) {
771                         fprintf(stderr,
772                                 "nr %lu memory corruption %Lu %Lu\n",
773                                 nr, count,
774                                 count_verify[nr]), exit(1);
775                 }
776         }
777
778         if (uffd_test_ops->release_pages(area_dst))
779                 return 1;
780
781         for (nr = 0; nr < nr_pages; nr++) {
782                 if (my_bcmp(area_dst + nr * page_size, zeropage, page_size))
783                         fprintf(stderr, "nr %lu is not zero\n", nr), exit(1);
784         }
785
786         return 0;
787 }
788
789 static void retry_uffdio_zeropage(int ufd,
790                                   struct uffdio_zeropage *uffdio_zeropage,
791                                   unsigned long offset)
792 {
793         uffd_test_ops->alias_mapping(&uffdio_zeropage->range.start,
794                                      uffdio_zeropage->range.len,
795                                      offset);
796         if (ioctl(ufd, UFFDIO_ZEROPAGE, uffdio_zeropage)) {
797                 if (uffdio_zeropage->zeropage != -EEXIST)
798                         fprintf(stderr, "UFFDIO_ZEROPAGE retry error %Ld\n",
799                                 uffdio_zeropage->zeropage), exit(1);
800         } else {
801                 fprintf(stderr, "UFFDIO_ZEROPAGE retry unexpected %Ld\n",
802                         uffdio_zeropage->zeropage), exit(1);
803         }
804 }
805
806 static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
807 {
808         struct uffdio_zeropage uffdio_zeropage;
809         int ret;
810         unsigned long has_zeropage;
811
812         has_zeropage = uffd_test_ops->expected_ioctls & (1 << _UFFDIO_ZEROPAGE);
813
814         if (offset >= nr_pages * page_size)
815                 fprintf(stderr, "unexpected offset %lu\n",
816                         offset), exit(1);
817         uffdio_zeropage.range.start = (unsigned long) area_dst + offset;
818         uffdio_zeropage.range.len = page_size;
819         uffdio_zeropage.mode = 0;
820         ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage);
821         if (ret) {
822                 /* real retval in ufdio_zeropage.zeropage */
823                 if (has_zeropage) {
824                         if (uffdio_zeropage.zeropage == -EEXIST)
825                                 fprintf(stderr, "UFFDIO_ZEROPAGE -EEXIST\n"),
826                                         exit(1);
827                         else
828                                 fprintf(stderr, "UFFDIO_ZEROPAGE error %Ld\n",
829                                         uffdio_zeropage.zeropage), exit(1);
830                 } else {
831                         if (uffdio_zeropage.zeropage != -EINVAL)
832                                 fprintf(stderr,
833                                         "UFFDIO_ZEROPAGE not -EINVAL %Ld\n",
834                                         uffdio_zeropage.zeropage), exit(1);
835                 }
836         } else if (has_zeropage) {
837                 if (uffdio_zeropage.zeropage != page_size) {
838                         fprintf(stderr, "UFFDIO_ZEROPAGE unexpected %Ld\n",
839                                 uffdio_zeropage.zeropage), exit(1);
840                 } else {
841                         if (test_uffdio_zeropage_eexist && retry) {
842                                 test_uffdio_zeropage_eexist = false;
843                                 retry_uffdio_zeropage(ufd, &uffdio_zeropage,
844                                                       offset);
845                         }
846                         return 1;
847                 }
848         } else {
849                 fprintf(stderr,
850                         "UFFDIO_ZEROPAGE succeeded %Ld\n",
851                         uffdio_zeropage.zeropage), exit(1);
852         }
853
854         return 0;
855 }
856
857 static int uffdio_zeropage(int ufd, unsigned long offset)
858 {
859         return __uffdio_zeropage(ufd, offset, false);
860 }
861
862 /* exercise UFFDIO_ZEROPAGE */
863 static int userfaultfd_zeropage_test(void)
864 {
865         struct uffdio_register uffdio_register;
866         unsigned long expected_ioctls;
867
868         printf("testing UFFDIO_ZEROPAGE: ");
869         fflush(stdout);
870
871         if (uffd_test_ops->release_pages(area_dst))
872                 return 1;
873
874         if (userfaultfd_open(0) < 0)
875                 return 1;
876         uffdio_register.range.start = (unsigned long) area_dst;
877         uffdio_register.range.len = nr_pages * page_size;
878         uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
879         if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
880                 fprintf(stderr, "register failure\n"), exit(1);
881
882         expected_ioctls = uffd_test_ops->expected_ioctls;
883         if ((uffdio_register.ioctls & expected_ioctls) !=
884             expected_ioctls)
885                 fprintf(stderr,
886                         "unexpected missing ioctl for anon memory\n"),
887                         exit(1);
888
889         if (uffdio_zeropage(uffd, 0)) {
890                 if (my_bcmp(area_dst, zeropage, page_size))
891                         fprintf(stderr, "zeropage is not zero\n"), exit(1);
892         }
893
894         close(uffd);
895         printf("done.\n");
896         return 0;
897 }
898
899 static int userfaultfd_events_test(void)
900 {
901         struct uffdio_register uffdio_register;
902         unsigned long expected_ioctls;
903         unsigned long userfaults;
904         pthread_t uffd_mon;
905         int err, features;
906         pid_t pid;
907         char c;
908
909         printf("testing events (fork, remap, remove): ");
910         fflush(stdout);
911
912         if (uffd_test_ops->release_pages(area_dst))
913                 return 1;
914
915         features = UFFD_FEATURE_EVENT_FORK | UFFD_FEATURE_EVENT_REMAP |
916                 UFFD_FEATURE_EVENT_REMOVE;
917         if (userfaultfd_open(features) < 0)
918                 return 1;
919         fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
920
921         uffdio_register.range.start = (unsigned long) area_dst;
922         uffdio_register.range.len = nr_pages * page_size;
923         uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
924         if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
925                 fprintf(stderr, "register failure\n"), exit(1);
926
927         expected_ioctls = uffd_test_ops->expected_ioctls;
928         if ((uffdio_register.ioctls & expected_ioctls) !=
929             expected_ioctls)
930                 fprintf(stderr,
931                         "unexpected missing ioctl for anon memory\n"),
932                         exit(1);
933
934         if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
935                 perror("uffd_poll_thread create"), exit(1);
936
937         pid = fork();
938         if (pid < 0)
939                 perror("fork"), exit(1);
940
941         if (!pid)
942                 return faulting_process(0);
943
944         waitpid(pid, &err, 0);
945         if (err)
946                 fprintf(stderr, "faulting process failed\n"), exit(1);
947
948         if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
949                 perror("pipe write"), exit(1);
950         if (pthread_join(uffd_mon, (void **)&userfaults))
951                 return 1;
952
953         close(uffd);
954         printf("userfaults: %ld\n", userfaults);
955
956         return userfaults != nr_pages;
957 }
958
959 static int userfaultfd_sig_test(void)
960 {
961         struct uffdio_register uffdio_register;
962         unsigned long expected_ioctls;
963         unsigned long userfaults;
964         pthread_t uffd_mon;
965         int err, features;
966         pid_t pid;
967         char c;
968
969         printf("testing signal delivery: ");
970         fflush(stdout);
971
972         if (uffd_test_ops->release_pages(area_dst))
973                 return 1;
974
975         features = UFFD_FEATURE_EVENT_FORK|UFFD_FEATURE_SIGBUS;
976         if (userfaultfd_open(features) < 0)
977                 return 1;
978         fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
979
980         uffdio_register.range.start = (unsigned long) area_dst;
981         uffdio_register.range.len = nr_pages * page_size;
982         uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
983         if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
984                 fprintf(stderr, "register failure\n"), exit(1);
985
986         expected_ioctls = uffd_test_ops->expected_ioctls;
987         if ((uffdio_register.ioctls & expected_ioctls) !=
988             expected_ioctls)
989                 fprintf(stderr,
990                         "unexpected missing ioctl for anon memory\n"),
991                         exit(1);
992
993         if (faulting_process(1))
994                 fprintf(stderr, "faulting process failed\n"), exit(1);
995
996         if (uffd_test_ops->release_pages(area_dst))
997                 return 1;
998
999         if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
1000                 perror("uffd_poll_thread create"), exit(1);
1001
1002         pid = fork();
1003         if (pid < 0)
1004                 perror("fork"), exit(1);
1005
1006         if (!pid)
1007                 exit(faulting_process(2));
1008
1009         waitpid(pid, &err, 0);
1010         if (err)
1011                 fprintf(stderr, "faulting process failed\n"), exit(1);
1012
1013         if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
1014                 perror("pipe write"), exit(1);
1015         if (pthread_join(uffd_mon, (void **)&userfaults))
1016                 return 1;
1017
1018         printf("done.\n");
1019         if (userfaults)
1020                 fprintf(stderr, "Signal test failed, userfaults: %ld\n",
1021                         userfaults);
1022         close(uffd);
1023         return userfaults != 0;
1024 }
1025 static int userfaultfd_stress(void)
1026 {
1027         void *area;
1028         char *tmp_area;
1029         unsigned long nr;
1030         struct uffdio_register uffdio_register;
1031         unsigned long cpu;
1032         int err;
1033         unsigned long userfaults[nr_cpus];
1034
1035         uffd_test_ops->allocate_area((void **)&area_src);
1036         if (!area_src)
1037                 return 1;
1038         uffd_test_ops->allocate_area((void **)&area_dst);
1039         if (!area_dst)
1040                 return 1;
1041
1042         if (userfaultfd_open(0) < 0)
1043                 return 1;
1044
1045         count_verify = malloc(nr_pages * sizeof(unsigned long long));
1046         if (!count_verify) {
1047                 perror("count_verify");
1048                 return 1;
1049         }
1050
1051         for (nr = 0; nr < nr_pages; nr++) {
1052                 *area_mutex(area_src, nr) = (pthread_mutex_t)
1053                         PTHREAD_MUTEX_INITIALIZER;
1054                 count_verify[nr] = *area_count(area_src, nr) = 1;
1055                 /*
1056                  * In the transition between 255 to 256, powerpc will
1057                  * read out of order in my_bcmp and see both bytes as
1058                  * zero, so leave a placeholder below always non-zero
1059                  * after the count, to avoid my_bcmp to trigger false
1060                  * positives.
1061                  */
1062                 *(area_count(area_src, nr) + 1) = 1;
1063         }
1064
1065         pipefd = malloc(sizeof(int) * nr_cpus * 2);
1066         if (!pipefd) {
1067                 perror("pipefd");
1068                 return 1;
1069         }
1070         for (cpu = 0; cpu < nr_cpus; cpu++) {
1071                 if (pipe2(&pipefd[cpu*2], O_CLOEXEC | O_NONBLOCK)) {
1072                         perror("pipe");
1073                         return 1;
1074                 }
1075         }
1076
1077         if (posix_memalign(&area, page_size, page_size)) {
1078                 fprintf(stderr, "out of memory\n");
1079                 return 1;
1080         }
1081         zeropage = area;
1082         bzero(zeropage, page_size);
1083
1084         pthread_mutex_lock(&uffd_read_mutex);
1085
1086         pthread_attr_init(&attr);
1087         pthread_attr_setstacksize(&attr, 16*1024*1024);
1088
1089         err = 0;
1090         while (bounces--) {
1091                 unsigned long expected_ioctls;
1092
1093                 printf("bounces: %d, mode:", bounces);
1094                 if (bounces & BOUNCE_RANDOM)
1095                         printf(" rnd");
1096                 if (bounces & BOUNCE_RACINGFAULTS)
1097                         printf(" racing");
1098                 if (bounces & BOUNCE_VERIFY)
1099                         printf(" ver");
1100                 if (bounces & BOUNCE_POLL)
1101                         printf(" poll");
1102                 printf(", ");
1103                 fflush(stdout);
1104
1105                 if (bounces & BOUNCE_POLL)
1106                         fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
1107                 else
1108                         fcntl(uffd, F_SETFL, uffd_flags & ~O_NONBLOCK);
1109
1110                 /* register */
1111                 uffdio_register.range.start = (unsigned long) area_dst;
1112                 uffdio_register.range.len = nr_pages * page_size;
1113                 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
1114                 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
1115                         fprintf(stderr, "register failure\n");
1116                         return 1;
1117                 }
1118                 expected_ioctls = uffd_test_ops->expected_ioctls;
1119                 if ((uffdio_register.ioctls & expected_ioctls) !=
1120                     expected_ioctls) {
1121                         fprintf(stderr,
1122                                 "unexpected missing ioctl for anon memory\n");
1123                         return 1;
1124                 }
1125
1126                 if (area_dst_alias) {
1127                         uffdio_register.range.start = (unsigned long)
1128                                 area_dst_alias;
1129                         if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
1130                                 fprintf(stderr, "register failure alias\n");
1131                                 return 1;
1132                         }
1133                 }
1134
1135                 /*
1136                  * The madvise done previously isn't enough: some
1137                  * uffd_thread could have read userfaults (one of
1138                  * those already resolved by the background thread)
1139                  * and it may be in the process of calling
1140                  * UFFDIO_COPY. UFFDIO_COPY will read the zapped
1141                  * area_src and it would map a zero page in it (of
1142                  * course such a UFFDIO_COPY is perfectly safe as it'd
1143                  * return -EEXIST). The problem comes at the next
1144                  * bounce though: that racing UFFDIO_COPY would
1145                  * generate zeropages in the area_src, so invalidating
1146                  * the previous MADV_DONTNEED. Without this additional
1147                  * MADV_DONTNEED those zeropages leftovers in the
1148                  * area_src would lead to -EEXIST failure during the
1149                  * next bounce, effectively leaving a zeropage in the
1150                  * area_dst.
1151                  *
1152                  * Try to comment this out madvise to see the memory
1153                  * corruption being caught pretty quick.
1154                  *
1155                  * khugepaged is also inhibited to collapse THP after
1156                  * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's
1157                  * required to MADV_DONTNEED here.
1158                  */
1159                 if (uffd_test_ops->release_pages(area_dst))
1160                         return 1;
1161
1162                 /* bounce pass */
1163                 if (stress(userfaults))
1164                         return 1;
1165
1166                 /* unregister */
1167                 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) {
1168                         fprintf(stderr, "unregister failure\n");
1169                         return 1;
1170                 }
1171                 if (area_dst_alias) {
1172                         uffdio_register.range.start = (unsigned long) area_dst;
1173                         if (ioctl(uffd, UFFDIO_UNREGISTER,
1174                                   &uffdio_register.range)) {
1175                                 fprintf(stderr, "unregister failure alias\n");
1176                                 return 1;
1177                         }
1178                 }
1179
1180                 /* verification */
1181                 if (bounces & BOUNCE_VERIFY) {
1182                         for (nr = 0; nr < nr_pages; nr++) {
1183                                 if (*area_count(area_dst, nr) != count_verify[nr]) {
1184                                         fprintf(stderr,
1185                                                 "error area_count %Lu %Lu %lu\n",
1186                                                 *area_count(area_src, nr),
1187                                                 count_verify[nr],
1188                                                 nr);
1189                                         err = 1;
1190                                         bounces = 0;
1191                                 }
1192                         }
1193                 }
1194
1195                 /* prepare next bounce */
1196                 tmp_area = area_src;
1197                 area_src = area_dst;
1198                 area_dst = tmp_area;
1199
1200                 tmp_area = area_src_alias;
1201                 area_src_alias = area_dst_alias;
1202                 area_dst_alias = tmp_area;
1203
1204                 printf("userfaults:");
1205                 for (cpu = 0; cpu < nr_cpus; cpu++)
1206                         printf(" %lu", userfaults[cpu]);
1207                 printf("\n");
1208         }
1209
1210         if (err)
1211                 return err;
1212
1213         close(uffd);
1214         return userfaultfd_zeropage_test() || userfaultfd_sig_test()
1215                 || userfaultfd_events_test();
1216 }
1217
1218 /*
1219  * Copied from mlock2-tests.c
1220  */
1221 unsigned long default_huge_page_size(void)
1222 {
1223         unsigned long hps = 0;
1224         char *line = NULL;
1225         size_t linelen = 0;
1226         FILE *f = fopen("/proc/meminfo", "r");
1227
1228         if (!f)
1229                 return 0;
1230         while (getline(&line, &linelen, f) > 0) {
1231                 if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
1232                         hps <<= 10;
1233                         break;
1234                 }
1235         }
1236
1237         free(line);
1238         fclose(f);
1239         return hps;
1240 }
1241
1242 static void set_test_type(const char *type)
1243 {
1244         if (!strcmp(type, "anon")) {
1245                 test_type = TEST_ANON;
1246                 uffd_test_ops = &anon_uffd_test_ops;
1247         } else if (!strcmp(type, "hugetlb")) {
1248                 test_type = TEST_HUGETLB;
1249                 uffd_test_ops = &hugetlb_uffd_test_ops;
1250         } else if (!strcmp(type, "hugetlb_shared")) {
1251                 map_shared = true;
1252                 test_type = TEST_HUGETLB;
1253                 uffd_test_ops = &hugetlb_uffd_test_ops;
1254         } else if (!strcmp(type, "shmem")) {
1255                 map_shared = true;
1256                 test_type = TEST_SHMEM;
1257                 uffd_test_ops = &shmem_uffd_test_ops;
1258         } else {
1259                 fprintf(stderr, "Unknown test type: %s\n", type), exit(1);
1260         }
1261
1262         if (test_type == TEST_HUGETLB)
1263                 page_size = default_huge_page_size();
1264         else
1265                 page_size = sysconf(_SC_PAGE_SIZE);
1266
1267         if (!page_size)
1268                 fprintf(stderr, "Unable to determine page size\n"),
1269                                 exit(2);
1270         if ((unsigned long) area_count(NULL, 0) + sizeof(unsigned long long) * 2
1271             > page_size)
1272                 fprintf(stderr, "Impossible to run this test\n"), exit(2);
1273 }
1274
1275 static void sigalrm(int sig)
1276 {
1277         if (sig != SIGALRM)
1278                 abort();
1279         test_uffdio_copy_eexist = true;
1280         test_uffdio_zeropage_eexist = true;
1281         alarm(ALARM_INTERVAL_SECS);
1282 }
1283
1284 int main(int argc, char **argv)
1285 {
1286         if (argc < 4)
1287                 usage();
1288
1289         if (signal(SIGALRM, sigalrm) == SIG_ERR)
1290                 fprintf(stderr, "failed to arm SIGALRM"), exit(1);
1291         alarm(ALARM_INTERVAL_SECS);
1292
1293         set_test_type(argv[1]);
1294
1295         nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1296         nr_pages_per_cpu = atol(argv[2]) * 1024*1024 / page_size /
1297                 nr_cpus;
1298         if (!nr_pages_per_cpu) {
1299                 fprintf(stderr, "invalid MiB\n");
1300                 usage();
1301         }
1302
1303         bounces = atoi(argv[3]);
1304         if (bounces <= 0) {
1305                 fprintf(stderr, "invalid bounces\n");
1306                 usage();
1307         }
1308         nr_pages = nr_pages_per_cpu * nr_cpus;
1309
1310         if (test_type == TEST_HUGETLB) {
1311                 if (argc < 5)
1312                         usage();
1313                 huge_fd = open(argv[4], O_CREAT | O_RDWR, 0755);
1314                 if (huge_fd < 0) {
1315                         fprintf(stderr, "Open of %s failed", argv[3]);
1316                         perror("open");
1317                         exit(1);
1318                 }
1319                 if (ftruncate(huge_fd, 0)) {
1320                         fprintf(stderr, "ftruncate %s to size 0 failed", argv[3]);
1321                         perror("ftruncate");
1322                         exit(1);
1323                 }
1324         }
1325         printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
1326                nr_pages, nr_pages_per_cpu);
1327         return userfaultfd_stress();
1328 }
1329
1330 #else /* __NR_userfaultfd */
1331
1332 #warning "missing __NR_userfaultfd definition"
1333
1334 int main(void)
1335 {
1336         printf("skip: Skipping userfaultfd test (missing __NR_userfaultfd)\n");
1337         return KSFT_SKIP;
1338 }
1339
1340 #endif /* __NR_userfaultfd */