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