blob: de2f9ec8a87fb342a7a595a13b009358d9eae000 [file] [log] [blame]
Andrea Arcangelic47174f2015-09-04 15:47:23 -07001/*
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>
Mike Rapoportda5502c2017-02-22 15:44:06 -080066#include <sys/wait.h>
Andrea Arcangelic47174f2015-09-04 15:47:23 -070067#include <pthread.h>
Thierry Redingd0a87112015-09-22 14:58:52 -070068#include <linux/userfaultfd.h>
Prakash Sangappa81aac3a2017-09-06 16:23:43 -070069#include <setjmp.h>
Andrea Arcangeli67e80322017-09-06 16:23:46 -070070#include <stdbool.h>
Andrea Arcangelic47174f2015-09-04 15:47:23 -070071
Michael Ellerman56ed8f12015-09-22 14:58:58 -070072#ifdef __NR_userfaultfd
Andrea Arcangelic47174f2015-09-04 15:47:23 -070073
74static unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;
75
76#define BOUNCE_RANDOM (1<<0)
77#define BOUNCE_RACINGFAULTS (1<<1)
78#define BOUNCE_VERIFY (1<<2)
79#define BOUNCE_POLL (1<<3)
80static int bounces;
81
Mike Rapoportb6ad1972017-05-03 14:54:54 -070082#define TEST_ANON 1
83#define TEST_HUGETLB 2
84#define TEST_SHMEM 3
85static int test_type;
86
Andrea Arcangeli67e80322017-09-06 16:23:46 -070087/* exercise the test_uffdio_*_eexist every ALARM_INTERVAL_SECS */
88#define ALARM_INTERVAL_SECS 10
89static volatile bool test_uffdio_copy_eexist = true;
90static volatile bool test_uffdio_zeropage_eexist = true;
91
92static bool map_shared;
Mike Kravetz9903bd72017-02-22 15:43:07 -080093static int huge_fd;
94static char *huge_fd_off0;
Andrea Arcangelic47174f2015-09-04 15:47:23 -070095static unsigned long long *count_verify;
Mike Rapoport6228b8f2017-02-22 15:44:01 -080096static int uffd, uffd_flags, finished, *pipefd;
Andrea Arcangeli67e80322017-09-06 16:23:46 -070097static char *area_src, *area_src_alias, *area_dst, *area_dst_alias;
Andrea Arcangelic47174f2015-09-04 15:47:23 -070098static char *zeropage;
99pthread_attr_t attr;
100
101/* pthread_mutex_t starts at page offset 0 */
102#define area_mutex(___area, ___nr) \
103 ((pthread_mutex_t *) ((___area) + (___nr)*page_size))
104/*
105 * count is placed in the page after pthread_mutex_t naturally aligned
106 * to avoid non alignment faults on non-x86 archs.
107 */
108#define area_count(___area, ___nr) \
109 ((volatile unsigned long long *) ((unsigned long) \
110 ((___area) + (___nr)*page_size + \
111 sizeof(pthread_mutex_t) + \
112 sizeof(unsigned long long) - 1) & \
113 ~(unsigned long)(sizeof(unsigned long long) \
114 - 1)))
115
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700116static int anon_release_pages(char *rel_area)
Mike Kravetz9903bd72017-02-22 15:43:07 -0800117{
118 int ret = 0;
119
120 if (madvise(rel_area, nr_pages * page_size, MADV_DONTNEED)) {
121 perror("madvise");
122 ret = 1;
123 }
124
125 return ret;
126}
127
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700128static void anon_allocate_area(void **alloc_area)
Mike Kravetz9903bd72017-02-22 15:43:07 -0800129{
130 if (posix_memalign(alloc_area, page_size, nr_pages * page_size)) {
131 fprintf(stderr, "out of memory\n");
132 *alloc_area = NULL;
133 }
134}
135
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700136static void noop_alias_mapping(__u64 *start, size_t len, unsigned long offset)
137{
138}
Mike Rapoport419624d2017-02-22 15:43:46 -0800139
140/* HugeTLB memory */
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700141static int hugetlb_release_pages(char *rel_area)
Mike Kravetz9903bd72017-02-22 15:43:07 -0800142{
143 int ret = 0;
144
145 if (fallocate(huge_fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
146 rel_area == huge_fd_off0 ? 0 :
147 nr_pages * page_size,
148 nr_pages * page_size)) {
149 perror("fallocate");
150 ret = 1;
151 }
152
153 return ret;
154}
155
156
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700157static void hugetlb_allocate_area(void **alloc_area)
Mike Kravetz9903bd72017-02-22 15:43:07 -0800158{
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700159 void *area_alias = NULL;
160 char **alloc_area_alias;
Mike Kravetz9903bd72017-02-22 15:43:07 -0800161 *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700162 (map_shared ? MAP_SHARED : MAP_PRIVATE) |
163 MAP_HUGETLB,
164 huge_fd, *alloc_area == area_src ? 0 :
165 nr_pages * page_size);
Mike Kravetz9903bd72017-02-22 15:43:07 -0800166 if (*alloc_area == MAP_FAILED) {
167 fprintf(stderr, "mmap of hugetlbfs file failed\n");
168 *alloc_area = NULL;
169 }
170
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700171 if (map_shared) {
172 area_alias = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
173 MAP_SHARED | MAP_HUGETLB,
174 huge_fd, *alloc_area == area_src ? 0 :
175 nr_pages * page_size);
176 if (area_alias == MAP_FAILED) {
177 if (munmap(*alloc_area, nr_pages * page_size) < 0)
178 perror("hugetlb munmap"), exit(1);
179 *alloc_area = NULL;
180 return;
181 }
182 }
183 if (*alloc_area == area_src) {
Mike Kravetz9903bd72017-02-22 15:43:07 -0800184 huge_fd_off0 = *alloc_area;
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700185 alloc_area_alias = &area_src_alias;
186 } else {
187 alloc_area_alias = &area_dst_alias;
188 }
189 if (area_alias)
190 *alloc_area_alias = area_alias;
191}
192
193static void hugetlb_alias_mapping(__u64 *start, size_t len, unsigned long offset)
194{
195 if (!map_shared)
196 return;
197 /*
198 * We can't zap just the pagetable with hugetlbfs because
199 * MADV_DONTEED won't work. So exercise -EEXIST on a alias
200 * mapping where the pagetables are not established initially,
201 * this way we'll exercise the -EEXEC at the fs level.
202 */
203 *start = (unsigned long) area_dst_alias + offset;
Mike Kravetz9903bd72017-02-22 15:43:07 -0800204}
205
Mike Rapoport419624d2017-02-22 15:43:46 -0800206/* Shared memory */
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700207static int shmem_release_pages(char *rel_area)
Mike Rapoport419624d2017-02-22 15:43:46 -0800208{
209 int ret = 0;
210
211 if (madvise(rel_area, nr_pages * page_size, MADV_REMOVE)) {
212 perror("madvise");
213 ret = 1;
214 }
215
216 return ret;
217}
218
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700219static void shmem_allocate_area(void **alloc_area)
Mike Rapoport419624d2017-02-22 15:43:46 -0800220{
221 *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
222 MAP_ANONYMOUS | MAP_SHARED, -1, 0);
223 if (*alloc_area == MAP_FAILED) {
224 fprintf(stderr, "shared memory mmap failed\n");
225 *alloc_area = NULL;
226 }
227}
228
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700229struct uffd_test_ops {
230 unsigned long expected_ioctls;
231 void (*allocate_area)(void **alloc_area);
232 int (*release_pages)(char *rel_area);
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700233 void (*alias_mapping)(__u64 *start, size_t len, unsigned long offset);
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700234};
Mike Kravetz9903bd72017-02-22 15:43:07 -0800235
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700236#define ANON_EXPECTED_IOCTLS ((1 << _UFFDIO_WAKE) | \
237 (1 << _UFFDIO_COPY) | \
238 (1 << _UFFDIO_ZEROPAGE))
239
240static struct uffd_test_ops anon_uffd_test_ops = {
241 .expected_ioctls = ANON_EXPECTED_IOCTLS,
242 .allocate_area = anon_allocate_area,
243 .release_pages = anon_release_pages,
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700244 .alias_mapping = noop_alias_mapping,
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700245};
246
247static struct uffd_test_ops shmem_uffd_test_ops = {
Mike Rapoport824f9732017-09-06 16:23:16 -0700248 .expected_ioctls = ANON_EXPECTED_IOCTLS,
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700249 .allocate_area = shmem_allocate_area,
250 .release_pages = shmem_release_pages,
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700251 .alias_mapping = noop_alias_mapping,
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700252};
253
254static struct uffd_test_ops hugetlb_uffd_test_ops = {
255 .expected_ioctls = UFFD_API_RANGE_IOCTLS_BASIC,
256 .allocate_area = hugetlb_allocate_area,
257 .release_pages = hugetlb_release_pages,
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700258 .alias_mapping = hugetlb_alias_mapping,
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700259};
260
261static struct uffd_test_ops *uffd_test_ops;
Mike Rapoport419624d2017-02-22 15:43:46 -0800262
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700263static int my_bcmp(char *str1, char *str2, size_t n)
264{
265 unsigned long i;
266 for (i = 0; i < n; i++)
267 if (str1[i] != str2[i])
268 return 1;
269 return 0;
270}
271
272static void *locking_thread(void *arg)
273{
274 unsigned long cpu = (unsigned long) arg;
275 struct random_data rand;
276 unsigned long page_nr = *(&(page_nr)); /* uninitialized warning */
277 int32_t rand_nr;
278 unsigned long long count;
279 char randstate[64];
280 unsigned int seed;
281 time_t start;
282
283 if (bounces & BOUNCE_RANDOM) {
284 seed = (unsigned int) time(NULL) - bounces;
285 if (!(bounces & BOUNCE_RACINGFAULTS))
286 seed += cpu;
287 bzero(&rand, sizeof(rand));
288 bzero(&randstate, sizeof(randstate));
289 if (initstate_r(seed, randstate, sizeof(randstate), &rand))
290 fprintf(stderr, "srandom_r error\n"), exit(1);
291 } else {
292 page_nr = -bounces;
293 if (!(bounces & BOUNCE_RACINGFAULTS))
294 page_nr += cpu * nr_pages_per_cpu;
295 }
296
297 while (!finished) {
298 if (bounces & BOUNCE_RANDOM) {
299 if (random_r(&rand, &rand_nr))
300 fprintf(stderr, "random_r 1 error\n"), exit(1);
301 page_nr = rand_nr;
302 if (sizeof(page_nr) > sizeof(rand_nr)) {
303 if (random_r(&rand, &rand_nr))
304 fprintf(stderr, "random_r 2 error\n"), exit(1);
Geert Uytterhoevenaf8713b2015-09-08 14:58:25 -0700305 page_nr |= (((unsigned long) rand_nr) << 16) <<
306 16;
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700307 }
308 } else
309 page_nr += 1;
310 page_nr %= nr_pages;
311
312 start = time(NULL);
313 if (bounces & BOUNCE_VERIFY) {
314 count = *area_count(area_dst, page_nr);
315 if (!count)
316 fprintf(stderr,
317 "page_nr %lu wrong count %Lu %Lu\n",
318 page_nr, count,
319 count_verify[page_nr]), exit(1);
320
321
322 /*
323 * We can't use bcmp (or memcmp) because that
324 * returns 0 erroneously if the memory is
325 * changing under it (even if the end of the
326 * page is never changing and always
327 * different).
328 */
329#if 1
330 if (!my_bcmp(area_dst + page_nr * page_size, zeropage,
331 page_size))
332 fprintf(stderr,
333 "my_bcmp page_nr %lu wrong count %Lu %Lu\n",
334 page_nr, count,
335 count_verify[page_nr]), exit(1);
336#else
337 unsigned long loops;
338
339 loops = 0;
340 /* uncomment the below line to test with mutex */
341 /* pthread_mutex_lock(area_mutex(area_dst, page_nr)); */
342 while (!bcmp(area_dst + page_nr * page_size, zeropage,
343 page_size)) {
344 loops += 1;
345 if (loops > 10)
346 break;
347 }
348 /* uncomment below line to test with mutex */
349 /* pthread_mutex_unlock(area_mutex(area_dst, page_nr)); */
350 if (loops) {
351 fprintf(stderr,
352 "page_nr %lu all zero thread %lu %p %lu\n",
353 page_nr, cpu, area_dst + page_nr * page_size,
354 loops);
355 if (loops > 10)
356 exit(1);
357 }
358#endif
359 }
360
361 pthread_mutex_lock(area_mutex(area_dst, page_nr));
362 count = *area_count(area_dst, page_nr);
363 if (count != count_verify[page_nr]) {
364 fprintf(stderr,
365 "page_nr %lu memory corruption %Lu %Lu\n",
366 page_nr, count,
367 count_verify[page_nr]), exit(1);
368 }
369 count++;
370 *area_count(area_dst, page_nr) = count_verify[page_nr] = count;
371 pthread_mutex_unlock(area_mutex(area_dst, page_nr));
372
373 if (time(NULL) - start > 1)
374 fprintf(stderr,
375 "userfault too slow %ld "
376 "possible false positive with overcommit\n",
377 time(NULL) - start);
378 }
379
380 return NULL;
381}
382
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700383static void retry_copy_page(int ufd, struct uffdio_copy *uffdio_copy,
384 unsigned long offset)
385{
386 uffd_test_ops->alias_mapping(&uffdio_copy->dst,
387 uffdio_copy->len,
388 offset);
389 if (ioctl(ufd, UFFDIO_COPY, uffdio_copy)) {
390 /* real retval in ufdio_copy.copy */
391 if (uffdio_copy->copy != -EEXIST)
392 fprintf(stderr, "UFFDIO_COPY retry error %Ld\n",
393 uffdio_copy->copy), exit(1);
394 } else {
395 fprintf(stderr, "UFFDIO_COPY retry unexpected %Ld\n",
396 uffdio_copy->copy), exit(1);
397 }
398}
399
Andrea Arcangeli7ddd8fa2017-10-13 15:57:54 -0700400static int __copy_page(int ufd, unsigned long offset, bool retry)
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700401{
402 struct uffdio_copy uffdio_copy;
403
404 if (offset >= nr_pages * page_size)
405 fprintf(stderr, "unexpected offset %lu\n",
406 offset), exit(1);
407 uffdio_copy.dst = (unsigned long) area_dst + offset;
408 uffdio_copy.src = (unsigned long) area_src + offset;
409 uffdio_copy.len = page_size;
410 uffdio_copy.mode = 0;
411 uffdio_copy.copy = 0;
Mike Rapoportaa0d2722017-02-22 15:44:04 -0800412 if (ioctl(ufd, UFFDIO_COPY, &uffdio_copy)) {
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700413 /* real retval in ufdio_copy.copy */
414 if (uffdio_copy.copy != -EEXIST)
415 fprintf(stderr, "UFFDIO_COPY error %Ld\n",
416 uffdio_copy.copy), exit(1);
417 } else if (uffdio_copy.copy != page_size) {
418 fprintf(stderr, "UFFDIO_COPY unexpected copy %Ld\n",
419 uffdio_copy.copy), exit(1);
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700420 } else {
Andrea Arcangeli7ddd8fa2017-10-13 15:57:54 -0700421 if (test_uffdio_copy_eexist && retry) {
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700422 test_uffdio_copy_eexist = false;
423 retry_copy_page(ufd, &uffdio_copy, offset);
424 }
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700425 return 1;
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700426 }
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700427 return 0;
428}
429
Andrea Arcangeli7ddd8fa2017-10-13 15:57:54 -0700430static int copy_page_retry(int ufd, unsigned long offset)
431{
432 return __copy_page(ufd, offset, true);
433}
434
435static int copy_page(int ufd, unsigned long offset)
436{
437 return __copy_page(ufd, offset, false);
438}
439
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700440static void *uffd_poll_thread(void *arg)
441{
442 unsigned long cpu = (unsigned long) arg;
443 struct pollfd pollfd[2];
444 struct uffd_msg msg;
Mike Rapoportda5502c2017-02-22 15:44:06 -0800445 struct uffdio_register uffd_reg;
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700446 int ret;
447 unsigned long offset;
448 char tmp_chr;
449 unsigned long userfaults = 0;
450
451 pollfd[0].fd = uffd;
452 pollfd[0].events = POLLIN;
453 pollfd[1].fd = pipefd[cpu*2];
454 pollfd[1].events = POLLIN;
455
456 for (;;) {
457 ret = poll(pollfd, 2, -1);
458 if (!ret)
459 fprintf(stderr, "poll error %d\n", ret), exit(1);
460 if (ret < 0)
461 perror("poll"), exit(1);
462 if (pollfd[1].revents & POLLIN) {
463 if (read(pollfd[1].fd, &tmp_chr, 1) != 1)
464 fprintf(stderr, "read pipefd error\n"),
465 exit(1);
466 break;
467 }
468 if (!(pollfd[0].revents & POLLIN))
469 fprintf(stderr, "pollfd[0].revents %d\n",
470 pollfd[0].revents), exit(1);
471 ret = read(uffd, &msg, sizeof(msg));
472 if (ret < 0) {
473 if (errno == EAGAIN)
474 continue;
475 perror("nonblocking read error"), exit(1);
476 }
Mike Rapoportda5502c2017-02-22 15:44:06 -0800477 switch (msg.event) {
478 default:
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700479 fprintf(stderr, "unexpected msg event %u\n",
480 msg.event), exit(1);
Mike Rapoportda5502c2017-02-22 15:44:06 -0800481 break;
482 case UFFD_EVENT_PAGEFAULT:
483 if (msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
484 fprintf(stderr, "unexpected write fault\n"), exit(1);
485 offset = (char *)(unsigned long)msg.arg.pagefault.address -
486 area_dst;
487 offset &= ~(page_size-1);
488 if (copy_page(uffd, offset))
489 userfaults++;
490 break;
491 case UFFD_EVENT_FORK:
Prakash Sangappa81aac3a2017-09-06 16:23:43 -0700492 close(uffd);
Mike Rapoportda5502c2017-02-22 15:44:06 -0800493 uffd = msg.arg.fork.ufd;
494 pollfd[0].fd = uffd;
495 break;
Mike Rapoportd8119142017-02-24 14:56:02 -0800496 case UFFD_EVENT_REMOVE:
497 uffd_reg.range.start = msg.arg.remove.start;
498 uffd_reg.range.len = msg.arg.remove.end -
499 msg.arg.remove.start;
Mike Rapoportda5502c2017-02-22 15:44:06 -0800500 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffd_reg.range))
Mike Rapoportd8119142017-02-24 14:56:02 -0800501 fprintf(stderr, "remove failure\n"), exit(1);
Mike Rapoportda5502c2017-02-22 15:44:06 -0800502 break;
503 case UFFD_EVENT_REMAP:
504 area_dst = (char *)(unsigned long)msg.arg.remap.to;
505 break;
506 }
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700507 }
508 return (void *)userfaults;
509}
510
511pthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER;
512
513static void *uffd_read_thread(void *arg)
514{
515 unsigned long *this_cpu_userfaults;
516 struct uffd_msg msg;
517 unsigned long offset;
518 int ret;
519
520 this_cpu_userfaults = (unsigned long *) arg;
521 *this_cpu_userfaults = 0;
522
523 pthread_mutex_unlock(&uffd_read_mutex);
524 /* from here cancellation is ok */
525
526 for (;;) {
527 ret = read(uffd, &msg, sizeof(msg));
528 if (ret != sizeof(msg)) {
529 if (ret < 0)
530 perror("blocking read error"), exit(1);
531 else
532 fprintf(stderr, "short read\n"), exit(1);
533 }
534 if (msg.event != UFFD_EVENT_PAGEFAULT)
535 fprintf(stderr, "unexpected msg event %u\n",
536 msg.event), exit(1);
537 if (bounces & BOUNCE_VERIFY &&
538 msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
539 fprintf(stderr, "unexpected write fault\n"), exit(1);
Geert Uytterhoevenaf8713b2015-09-08 14:58:25 -0700540 offset = (char *)(unsigned long)msg.arg.pagefault.address -
541 area_dst;
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700542 offset &= ~(page_size-1);
Mike Rapoportaa0d2722017-02-22 15:44:04 -0800543 if (copy_page(uffd, offset))
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700544 (*this_cpu_userfaults)++;
545 }
546 return (void *)NULL;
547}
548
549static void *background_thread(void *arg)
550{
551 unsigned long cpu = (unsigned long) arg;
552 unsigned long page_nr;
553
554 for (page_nr = cpu * nr_pages_per_cpu;
555 page_nr < (cpu+1) * nr_pages_per_cpu;
556 page_nr++)
Andrea Arcangeli7ddd8fa2017-10-13 15:57:54 -0700557 copy_page_retry(uffd, page_nr * page_size);
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700558
559 return NULL;
560}
561
562static int stress(unsigned long *userfaults)
563{
564 unsigned long cpu;
565 pthread_t locking_threads[nr_cpus];
566 pthread_t uffd_threads[nr_cpus];
567 pthread_t background_threads[nr_cpus];
568 void **_userfaults = (void **) userfaults;
569
570 finished = 0;
571 for (cpu = 0; cpu < nr_cpus; cpu++) {
572 if (pthread_create(&locking_threads[cpu], &attr,
573 locking_thread, (void *)cpu))
574 return 1;
575 if (bounces & BOUNCE_POLL) {
576 if (pthread_create(&uffd_threads[cpu], &attr,
577 uffd_poll_thread, (void *)cpu))
578 return 1;
579 } else {
580 if (pthread_create(&uffd_threads[cpu], &attr,
581 uffd_read_thread,
582 &_userfaults[cpu]))
583 return 1;
584 pthread_mutex_lock(&uffd_read_mutex);
585 }
586 if (pthread_create(&background_threads[cpu], &attr,
587 background_thread, (void *)cpu))
588 return 1;
589 }
590 for (cpu = 0; cpu < nr_cpus; cpu++)
591 if (pthread_join(background_threads[cpu], NULL))
592 return 1;
593
594 /*
595 * Be strict and immediately zap area_src, the whole area has
596 * been transferred already by the background treads. The
597 * area_src could then be faulted in in a racy way by still
598 * running uffdio_threads reading zeropages after we zapped
599 * area_src (but they're guaranteed to get -EEXIST from
600 * UFFDIO_COPY without writing zero pages into area_dst
601 * because the background threads already completed).
602 */
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700603 if (uffd_test_ops->release_pages(area_src))
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700604 return 1;
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700605
606 for (cpu = 0; cpu < nr_cpus; cpu++) {
607 char c;
608 if (bounces & BOUNCE_POLL) {
609 if (write(pipefd[cpu*2+1], &c, 1) != 1) {
610 fprintf(stderr, "pipefd write error\n");
611 return 1;
612 }
613 if (pthread_join(uffd_threads[cpu], &_userfaults[cpu]))
614 return 1;
615 } else {
616 if (pthread_cancel(uffd_threads[cpu]))
617 return 1;
618 if (pthread_join(uffd_threads[cpu], NULL))
619 return 1;
620 }
621 }
622
623 finished = 1;
624 for (cpu = 0; cpu < nr_cpus; cpu++)
625 if (pthread_join(locking_threads[cpu], NULL))
626 return 1;
627
628 return 0;
629}
630
Mike Rapoportda5502c2017-02-22 15:44:06 -0800631static int userfaultfd_open(int features)
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700632{
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700633 struct uffdio_api uffdio_api;
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700634
635 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
636 if (uffd < 0) {
637 fprintf(stderr,
638 "userfaultfd syscall not available in this kernel\n");
639 return 1;
640 }
641 uffd_flags = fcntl(uffd, F_GETFD, NULL);
642
643 uffdio_api.api = UFFD_API;
Mike Rapoportda5502c2017-02-22 15:44:06 -0800644 uffdio_api.features = features;
Andrea Arcangelic47174f2015-09-04 15:47:23 -0700645 if (ioctl(uffd, UFFDIO_API, &uffdio_api)) {
646 fprintf(stderr, "UFFDIO_API\n");
647 return 1;
648 }
649 if (uffdio_api.api != UFFD_API) {
650 fprintf(stderr, "UFFDIO_API error %Lu\n", uffdio_api.api);
651 return 1;
652 }
653
Mike Rapoport6228b8f2017-02-22 15:44:01 -0800654 return 0;
655}
656
Prakash Sangappa81aac3a2017-09-06 16:23:43 -0700657sigjmp_buf jbuf, *sigbuf;
658
659static void sighndl(int sig, siginfo_t *siginfo, void *ptr)
660{
661 if (sig == SIGBUS) {
662 if (sigbuf)
663 siglongjmp(*sigbuf, 1);
664 abort();
665 }
666}
667
Mike Rapoportda5502c2017-02-22 15:44:06 -0800668/*
669 * For non-cooperative userfaultfd test we fork() a process that will
670 * generate pagefaults, will mremap the area monitored by the
671 * userfaultfd and at last this process will release the monitored
672 * area.
673 * For the anonymous and shared memory the area is divided into two
674 * parts, the first part is accessed before mremap, and the second
675 * part is accessed after mremap. Since hugetlbfs does not support
676 * mremap, the entire monitored area is accessed in a single pass for
677 * HUGETLB_TEST.
Mike Rapoport64527f52017-02-24 14:56:08 -0800678 * The release of the pages currently generates event for shmem and
Mike Rapoportd8119142017-02-24 14:56:02 -0800679 * anonymous memory (UFFD_EVENT_REMOVE), hence it is not checked
Mike Rapoport64527f52017-02-24 14:56:08 -0800680 * for hugetlb.
Prakash Sangappa81aac3a2017-09-06 16:23:43 -0700681 * For signal test(UFFD_FEATURE_SIGBUS), signal_test = 1, we register
682 * monitored area, generate pagefaults and test that signal is delivered.
683 * Use UFFDIO_COPY to allocate missing page and retry. For signal_test = 2
684 * test robustness use case - we release monitored area, fork a process
685 * that will generate pagefaults and verify signal is generated.
686 * This also tests UFFD_FEATURE_EVENT_FORK event along with the signal
687 * feature. Using monitor thread, verify no userfault events are generated.
Mike Rapoportda5502c2017-02-22 15:44:06 -0800688 */
Prakash Sangappa81aac3a2017-09-06 16:23:43 -0700689static int faulting_process(int signal_test)
Mike Rapoportda5502c2017-02-22 15:44:06 -0800690{
691 unsigned long nr;
692 unsigned long long count;
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700693 unsigned long split_nr_pages;
Prakash Sangappa81aac3a2017-09-06 16:23:43 -0700694 unsigned long lastnr;
695 struct sigaction act;
696 unsigned long signalled = 0;
Mike Rapoportda5502c2017-02-22 15:44:06 -0800697
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700698 if (test_type != TEST_HUGETLB)
699 split_nr_pages = (nr_pages + 1) / 2;
700 else
701 split_nr_pages = nr_pages;
Mike Rapoportda5502c2017-02-22 15:44:06 -0800702
Prakash Sangappa81aac3a2017-09-06 16:23:43 -0700703 if (signal_test) {
704 sigbuf = &jbuf;
705 memset(&act, 0, sizeof(act));
706 act.sa_sigaction = sighndl;
707 act.sa_flags = SA_SIGINFO;
708 if (sigaction(SIGBUS, &act, 0)) {
709 perror("sigaction");
710 return 1;
711 }
712 lastnr = (unsigned long)-1;
713 }
714
Mike Rapoportda5502c2017-02-22 15:44:06 -0800715 for (nr = 0; nr < split_nr_pages; nr++) {
Prakash Sangappa81aac3a2017-09-06 16:23:43 -0700716 if (signal_test) {
717 if (sigsetjmp(*sigbuf, 1) != 0) {
718 if (nr == lastnr) {
719 fprintf(stderr, "Signal repeated\n");
720 return 1;
721 }
722
723 lastnr = nr;
724 if (signal_test == 1) {
725 if (copy_page(uffd, nr * page_size))
726 signalled++;
727 } else {
728 signalled++;
729 continue;
730 }
731 }
732 }
733
Mike Rapoportda5502c2017-02-22 15:44:06 -0800734 count = *area_count(area_dst, nr);
735 if (count != count_verify[nr]) {
736 fprintf(stderr,
737 "nr %lu memory corruption %Lu %Lu\n",
738 nr, count,
739 count_verify[nr]), exit(1);
740 }
741 }
742
Prakash Sangappa81aac3a2017-09-06 16:23:43 -0700743 if (signal_test)
744 return signalled != split_nr_pages;
745
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700746 if (test_type == TEST_HUGETLB)
747 return 0;
748
Mike Rapoportda5502c2017-02-22 15:44:06 -0800749 area_dst = mremap(area_dst, nr_pages * page_size, nr_pages * page_size,
750 MREMAP_MAYMOVE | MREMAP_FIXED, area_src);
751 if (area_dst == MAP_FAILED)
752 perror("mremap"), exit(1);
753
754 for (; nr < nr_pages; nr++) {
755 count = *area_count(area_dst, nr);
756 if (count != count_verify[nr]) {
757 fprintf(stderr,
758 "nr %lu memory corruption %Lu %Lu\n",
759 nr, count,
760 count_verify[nr]), exit(1);
761 }
762 }
763
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700764 if (uffd_test_ops->release_pages(area_dst))
Mike Rapoportda5502c2017-02-22 15:44:06 -0800765 return 1;
766
767 for (nr = 0; nr < nr_pages; nr++) {
768 if (my_bcmp(area_dst + nr * page_size, zeropage, page_size))
769 fprintf(stderr, "nr %lu is not zero\n", nr), exit(1);
770 }
Mike Rapoportda5502c2017-02-22 15:44:06 -0800771
Mike Rapoportda5502c2017-02-22 15:44:06 -0800772 return 0;
773}
774
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700775static void retry_uffdio_zeropage(int ufd,
776 struct uffdio_zeropage *uffdio_zeropage,
777 unsigned long offset)
778{
779 uffd_test_ops->alias_mapping(&uffdio_zeropage->range.start,
780 uffdio_zeropage->range.len,
781 offset);
782 if (ioctl(ufd, UFFDIO_ZEROPAGE, uffdio_zeropage)) {
783 if (uffdio_zeropage->zeropage != -EEXIST)
784 fprintf(stderr, "UFFDIO_ZEROPAGE retry error %Ld\n",
785 uffdio_zeropage->zeropage), exit(1);
786 } else {
787 fprintf(stderr, "UFFDIO_ZEROPAGE retry unexpected %Ld\n",
788 uffdio_zeropage->zeropage), exit(1);
789 }
790}
791
Andrea Arcangeli7ddd8fa2017-10-13 15:57:54 -0700792static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
Andrea Arcangeli7a0c4cf2017-02-22 15:44:10 -0800793{
794 struct uffdio_zeropage uffdio_zeropage;
795 int ret;
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700796 unsigned long has_zeropage;
797
798 has_zeropage = uffd_test_ops->expected_ioctls & (1 << _UFFDIO_ZEROPAGE);
Andrea Arcangeli7a0c4cf2017-02-22 15:44:10 -0800799
800 if (offset >= nr_pages * page_size)
801 fprintf(stderr, "unexpected offset %lu\n",
802 offset), exit(1);
803 uffdio_zeropage.range.start = (unsigned long) area_dst + offset;
804 uffdio_zeropage.range.len = page_size;
805 uffdio_zeropage.mode = 0;
806 ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage);
807 if (ret) {
808 /* real retval in ufdio_zeropage.zeropage */
809 if (has_zeropage) {
810 if (uffdio_zeropage.zeropage == -EEXIST)
811 fprintf(stderr, "UFFDIO_ZEROPAGE -EEXIST\n"),
812 exit(1);
813 else
814 fprintf(stderr, "UFFDIO_ZEROPAGE error %Ld\n",
815 uffdio_zeropage.zeropage), exit(1);
816 } else {
817 if (uffdio_zeropage.zeropage != -EINVAL)
818 fprintf(stderr,
819 "UFFDIO_ZEROPAGE not -EINVAL %Ld\n",
820 uffdio_zeropage.zeropage), exit(1);
821 }
822 } else if (has_zeropage) {
823 if (uffdio_zeropage.zeropage != page_size) {
824 fprintf(stderr, "UFFDIO_ZEROPAGE unexpected %Ld\n",
825 uffdio_zeropage.zeropage), exit(1);
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700826 } else {
Andrea Arcangeli7ddd8fa2017-10-13 15:57:54 -0700827 if (test_uffdio_zeropage_eexist && retry) {
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700828 test_uffdio_zeropage_eexist = false;
829 retry_uffdio_zeropage(ufd, &uffdio_zeropage,
830 offset);
831 }
Andrea Arcangeli7a0c4cf2017-02-22 15:44:10 -0800832 return 1;
Andrea Arcangeli67e80322017-09-06 16:23:46 -0700833 }
Andrea Arcangeli7a0c4cf2017-02-22 15:44:10 -0800834 } else {
835 fprintf(stderr,
836 "UFFDIO_ZEROPAGE succeeded %Ld\n",
837 uffdio_zeropage.zeropage), exit(1);
838 }
839
840 return 0;
841}
842
Andrea Arcangeli7ddd8fa2017-10-13 15:57:54 -0700843static int uffdio_zeropage(int ufd, unsigned long offset)
844{
845 return __uffdio_zeropage(ufd, offset, false);
846}
847
Andrea Arcangeli7a0c4cf2017-02-22 15:44:10 -0800848/* exercise UFFDIO_ZEROPAGE */
849static int userfaultfd_zeropage_test(void)
850{
851 struct uffdio_register uffdio_register;
852 unsigned long expected_ioctls;
853
854 printf("testing UFFDIO_ZEROPAGE: ");
855 fflush(stdout);
856
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700857 if (uffd_test_ops->release_pages(area_dst))
Andrea Arcangeli7a0c4cf2017-02-22 15:44:10 -0800858 return 1;
859
860 if (userfaultfd_open(0) < 0)
861 return 1;
862 uffdio_register.range.start = (unsigned long) area_dst;
863 uffdio_register.range.len = nr_pages * page_size;
864 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
865 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
866 fprintf(stderr, "register failure\n"), exit(1);
867
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700868 expected_ioctls = uffd_test_ops->expected_ioctls;
Andrea Arcangeli7a0c4cf2017-02-22 15:44:10 -0800869 if ((uffdio_register.ioctls & expected_ioctls) !=
870 expected_ioctls)
871 fprintf(stderr,
872 "unexpected missing ioctl for anon memory\n"),
873 exit(1);
874
875 if (uffdio_zeropage(uffd, 0)) {
876 if (my_bcmp(area_dst, zeropage, page_size))
877 fprintf(stderr, "zeropage is not zero\n"), exit(1);
878 }
879
880 close(uffd);
881 printf("done.\n");
882 return 0;
883}
884
Mike Rapoportda5502c2017-02-22 15:44:06 -0800885static int userfaultfd_events_test(void)
886{
887 struct uffdio_register uffdio_register;
888 unsigned long expected_ioctls;
889 unsigned long userfaults;
890 pthread_t uffd_mon;
891 int err, features;
892 pid_t pid;
893 char c;
894
Mike Rapoportd8119142017-02-24 14:56:02 -0800895 printf("testing events (fork, remap, remove): ");
Mike Rapoportda5502c2017-02-22 15:44:06 -0800896 fflush(stdout);
897
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700898 if (uffd_test_ops->release_pages(area_dst))
Mike Rapoportda5502c2017-02-22 15:44:06 -0800899 return 1;
900
901 features = UFFD_FEATURE_EVENT_FORK | UFFD_FEATURE_EVENT_REMAP |
Mike Rapoportd8119142017-02-24 14:56:02 -0800902 UFFD_FEATURE_EVENT_REMOVE;
Mike Rapoportda5502c2017-02-22 15:44:06 -0800903 if (userfaultfd_open(features) < 0)
904 return 1;
905 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
906
907 uffdio_register.range.start = (unsigned long) area_dst;
908 uffdio_register.range.len = nr_pages * page_size;
909 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
910 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
911 fprintf(stderr, "register failure\n"), exit(1);
912
Mike Rapoportb6ad1972017-05-03 14:54:54 -0700913 expected_ioctls = uffd_test_ops->expected_ioctls;
Mike Rapoportda5502c2017-02-22 15:44:06 -0800914 if ((uffdio_register.ioctls & expected_ioctls) !=
915 expected_ioctls)
916 fprintf(stderr,
917 "unexpected missing ioctl for anon memory\n"),
918 exit(1);
919
920 if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
921 perror("uffd_poll_thread create"), exit(1);
922
923 pid = fork();
924 if (pid < 0)
925 perror("fork"), exit(1);
926
927 if (!pid)
Prakash Sangappa81aac3a2017-09-06 16:23:43 -0700928 return faulting_process(0);
Mike Rapoportda5502c2017-02-22 15:44:06 -0800929
930 waitpid(pid, &err, 0);
931 if (err)
932 fprintf(stderr, "faulting process failed\n"), exit(1);
933
934 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
935 perror("pipe write"), exit(1);
936 if (pthread_join(uffd_mon, (void **)&userfaults))
937 return 1;
938
939 close(uffd);
940 printf("userfaults: %ld\n", userfaults);
941
942 return userfaults != nr_pages;
943}
944
Prakash Sangappa81aac3a2017-09-06 16:23:43 -0700945static int userfaultfd_sig_test(void)
946{
947 struct uffdio_register uffdio_register;
948 unsigned long expected_ioctls;
949 unsigned long userfaults;
950 pthread_t uffd_mon;
951 int err, features;
952 pid_t pid;
953 char c;
954
955 printf("testing signal delivery: ");
956 fflush(stdout);
957
958 if (uffd_test_ops->release_pages(area_dst))
959 return 1;
960
961 features = UFFD_FEATURE_EVENT_FORK|UFFD_FEATURE_SIGBUS;
962 if (userfaultfd_open(features) < 0)
963 return 1;
964 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
965
966 uffdio_register.range.start = (unsigned long) area_dst;
967 uffdio_register.range.len = nr_pages * page_size;
968 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
969 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
970 fprintf(stderr, "register failure\n"), exit(1);
971
972 expected_ioctls = uffd_test_ops->expected_ioctls;
973 if ((uffdio_register.ioctls & expected_ioctls) !=
974 expected_ioctls)
975 fprintf(stderr,
976 "unexpected missing ioctl for anon memory\n"),
977 exit(1);
978
979 if (faulting_process(1))
980 fprintf(stderr, "faulting process failed\n"), exit(1);
981
982 if (uffd_test_ops->release_pages(area_dst))
983 return 1;
984
985 if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
986 perror("uffd_poll_thread create"), exit(1);
987
988 pid = fork();
989 if (pid < 0)
990 perror("fork"), exit(1);
991
992 if (!pid)
993 exit(faulting_process(2));
994
995 waitpid(pid, &err, 0);
996 if (err)
997 fprintf(stderr, "faulting process failed\n"), exit(1);
998
999 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
1000 perror("pipe write"), exit(1);
1001 if (pthread_join(uffd_mon, (void **)&userfaults))
1002 return 1;
1003
1004 printf("done.\n");
Andrea Arcangelid312cb12017-09-06 16:23:49 -07001005 if (userfaults)
1006 fprintf(stderr, "Signal test failed, userfaults: %ld\n",
1007 userfaults);
Prakash Sangappa81aac3a2017-09-06 16:23:43 -07001008 close(uffd);
1009 return userfaults != 0;
1010}
Mike Rapoport6228b8f2017-02-22 15:44:01 -08001011static int userfaultfd_stress(void)
1012{
1013 void *area;
1014 char *tmp_area;
1015 unsigned long nr;
1016 struct uffdio_register uffdio_register;
1017 unsigned long cpu;
1018 int err;
1019 unsigned long userfaults[nr_cpus];
1020
Mike Rapoportb6ad1972017-05-03 14:54:54 -07001021 uffd_test_ops->allocate_area((void **)&area_src);
Mike Rapoport6228b8f2017-02-22 15:44:01 -08001022 if (!area_src)
1023 return 1;
Mike Rapoportb6ad1972017-05-03 14:54:54 -07001024 uffd_test_ops->allocate_area((void **)&area_dst);
Mike Rapoport6228b8f2017-02-22 15:44:01 -08001025 if (!area_dst)
1026 return 1;
1027
Mike Rapoportda5502c2017-02-22 15:44:06 -08001028 if (userfaultfd_open(0) < 0)
Mike Rapoport6228b8f2017-02-22 15:44:01 -08001029 return 1;
1030
Andrea Arcangelic47174f2015-09-04 15:47:23 -07001031 count_verify = malloc(nr_pages * sizeof(unsigned long long));
1032 if (!count_verify) {
1033 perror("count_verify");
1034 return 1;
1035 }
1036
1037 for (nr = 0; nr < nr_pages; nr++) {
1038 *area_mutex(area_src, nr) = (pthread_mutex_t)
1039 PTHREAD_MUTEX_INITIALIZER;
1040 count_verify[nr] = *area_count(area_src, nr) = 1;
Andrea Arcangeli1f5fee22015-09-22 14:59:00 -07001041 /*
1042 * In the transition between 255 to 256, powerpc will
1043 * read out of order in my_bcmp and see both bytes as
1044 * zero, so leave a placeholder below always non-zero
1045 * after the count, to avoid my_bcmp to trigger false
1046 * positives.
1047 */
1048 *(area_count(area_src, nr) + 1) = 1;
Andrea Arcangelic47174f2015-09-04 15:47:23 -07001049 }
1050
1051 pipefd = malloc(sizeof(int) * nr_cpus * 2);
1052 if (!pipefd) {
1053 perror("pipefd");
1054 return 1;
1055 }
1056 for (cpu = 0; cpu < nr_cpus; cpu++) {
1057 if (pipe2(&pipefd[cpu*2], O_CLOEXEC | O_NONBLOCK)) {
1058 perror("pipe");
1059 return 1;
1060 }
1061 }
1062
1063 if (posix_memalign(&area, page_size, page_size)) {
1064 fprintf(stderr, "out of memory\n");
1065 return 1;
1066 }
1067 zeropage = area;
1068 bzero(zeropage, page_size);
1069
1070 pthread_mutex_lock(&uffd_read_mutex);
1071
1072 pthread_attr_init(&attr);
1073 pthread_attr_setstacksize(&attr, 16*1024*1024);
1074
Andrea Arcangelia5932bf2015-09-22 14:59:03 -07001075 err = 0;
Andrea Arcangelic47174f2015-09-04 15:47:23 -07001076 while (bounces--) {
1077 unsigned long expected_ioctls;
1078
1079 printf("bounces: %d, mode:", bounces);
1080 if (bounces & BOUNCE_RANDOM)
1081 printf(" rnd");
1082 if (bounces & BOUNCE_RACINGFAULTS)
1083 printf(" racing");
1084 if (bounces & BOUNCE_VERIFY)
1085 printf(" ver");
1086 if (bounces & BOUNCE_POLL)
1087 printf(" poll");
1088 printf(", ");
1089 fflush(stdout);
1090
1091 if (bounces & BOUNCE_POLL)
1092 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
1093 else
1094 fcntl(uffd, F_SETFL, uffd_flags & ~O_NONBLOCK);
1095
1096 /* register */
1097 uffdio_register.range.start = (unsigned long) area_dst;
1098 uffdio_register.range.len = nr_pages * page_size;
1099 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
1100 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
1101 fprintf(stderr, "register failure\n");
1102 return 1;
1103 }
Mike Rapoportb6ad1972017-05-03 14:54:54 -07001104 expected_ioctls = uffd_test_ops->expected_ioctls;
Andrea Arcangelic47174f2015-09-04 15:47:23 -07001105 if ((uffdio_register.ioctls & expected_ioctls) !=
1106 expected_ioctls) {
1107 fprintf(stderr,
1108 "unexpected missing ioctl for anon memory\n");
1109 return 1;
1110 }
1111
Andrea Arcangeli67e80322017-09-06 16:23:46 -07001112 if (area_dst_alias) {
1113 uffdio_register.range.start = (unsigned long)
1114 area_dst_alias;
1115 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
1116 fprintf(stderr, "register failure alias\n");
1117 return 1;
1118 }
1119 }
1120
Andrea Arcangelic47174f2015-09-04 15:47:23 -07001121 /*
1122 * The madvise done previously isn't enough: some
1123 * uffd_thread could have read userfaults (one of
1124 * those already resolved by the background thread)
1125 * and it may be in the process of calling
1126 * UFFDIO_COPY. UFFDIO_COPY will read the zapped
1127 * area_src and it would map a zero page in it (of
1128 * course such a UFFDIO_COPY is perfectly safe as it'd
1129 * return -EEXIST). The problem comes at the next
1130 * bounce though: that racing UFFDIO_COPY would
1131 * generate zeropages in the area_src, so invalidating
1132 * the previous MADV_DONTNEED. Without this additional
1133 * MADV_DONTNEED those zeropages leftovers in the
1134 * area_src would lead to -EEXIST failure during the
1135 * next bounce, effectively leaving a zeropage in the
1136 * area_dst.
1137 *
1138 * Try to comment this out madvise to see the memory
1139 * corruption being caught pretty quick.
1140 *
1141 * khugepaged is also inhibited to collapse THP after
1142 * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's
1143 * required to MADV_DONTNEED here.
1144 */
Mike Rapoportb6ad1972017-05-03 14:54:54 -07001145 if (uffd_test_ops->release_pages(area_dst))
Andrea Arcangelic47174f2015-09-04 15:47:23 -07001146 return 1;
Andrea Arcangelic47174f2015-09-04 15:47:23 -07001147
1148 /* bounce pass */
1149 if (stress(userfaults))
1150 return 1;
1151
1152 /* unregister */
1153 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) {
Andrea Arcangeli67e80322017-09-06 16:23:46 -07001154 fprintf(stderr, "unregister failure\n");
Andrea Arcangelic47174f2015-09-04 15:47:23 -07001155 return 1;
1156 }
Andrea Arcangeli67e80322017-09-06 16:23:46 -07001157 if (area_dst_alias) {
1158 uffdio_register.range.start = (unsigned long) area_dst;
1159 if (ioctl(uffd, UFFDIO_UNREGISTER,
1160 &uffdio_register.range)) {
1161 fprintf(stderr, "unregister failure alias\n");
1162 return 1;
1163 }
1164 }
Andrea Arcangelic47174f2015-09-04 15:47:23 -07001165
1166 /* verification */
1167 if (bounces & BOUNCE_VERIFY) {
1168 for (nr = 0; nr < nr_pages; nr++) {
Andrea Arcangelic47174f2015-09-04 15:47:23 -07001169 if (*area_count(area_dst, nr) != count_verify[nr]) {
1170 fprintf(stderr,
1171 "error area_count %Lu %Lu %lu\n",
1172 *area_count(area_src, nr),
1173 count_verify[nr],
1174 nr);
Andrea Arcangelia5932bf2015-09-22 14:59:03 -07001175 err = 1;
Andrea Arcangelic47174f2015-09-04 15:47:23 -07001176 bounces = 0;
1177 }
1178 }
1179 }
1180
1181 /* prepare next bounce */
1182 tmp_area = area_src;
1183 area_src = area_dst;
1184 area_dst = tmp_area;
1185
Andrea Arcangeli67e80322017-09-06 16:23:46 -07001186 tmp_area = area_src_alias;
1187 area_src_alias = area_dst_alias;
1188 area_dst_alias = tmp_area;
1189
Andrea Arcangelic47174f2015-09-04 15:47:23 -07001190 printf("userfaults:");
1191 for (cpu = 0; cpu < nr_cpus; cpu++)
1192 printf(" %lu", userfaults[cpu]);
1193 printf("\n");
1194 }
1195
Mike Rapoportda5502c2017-02-22 15:44:06 -08001196 if (err)
1197 return err;
1198
1199 close(uffd);
Prakash Sangappa81aac3a2017-09-06 16:23:43 -07001200 return userfaultfd_zeropage_test() || userfaultfd_sig_test()
1201 || userfaultfd_events_test();
Andrea Arcangelic47174f2015-09-04 15:47:23 -07001202}
1203
Mike Kravetz9903bd72017-02-22 15:43:07 -08001204/*
1205 * Copied from mlock2-tests.c
1206 */
1207unsigned long default_huge_page_size(void)
1208{
1209 unsigned long hps = 0;
1210 char *line = NULL;
1211 size_t linelen = 0;
1212 FILE *f = fopen("/proc/meminfo", "r");
1213
1214 if (!f)
1215 return 0;
1216 while (getline(&line, &linelen, f) > 0) {
1217 if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
1218 hps <<= 10;
1219 break;
1220 }
1221 }
1222
1223 free(line);
1224 fclose(f);
1225 return hps;
1226}
1227
Mike Rapoportb6ad1972017-05-03 14:54:54 -07001228static void set_test_type(const char *type)
Mike Kravetz9903bd72017-02-22 15:43:07 -08001229{
Mike Rapoportb6ad1972017-05-03 14:54:54 -07001230 if (!strcmp(type, "anon")) {
1231 test_type = TEST_ANON;
1232 uffd_test_ops = &anon_uffd_test_ops;
1233 } else if (!strcmp(type, "hugetlb")) {
1234 test_type = TEST_HUGETLB;
1235 uffd_test_ops = &hugetlb_uffd_test_ops;
Andrea Arcangeli67e80322017-09-06 16:23:46 -07001236 } else if (!strcmp(type, "hugetlb_shared")) {
1237 map_shared = true;
1238 test_type = TEST_HUGETLB;
1239 uffd_test_ops = &hugetlb_uffd_test_ops;
Mike Rapoportb6ad1972017-05-03 14:54:54 -07001240 } else if (!strcmp(type, "shmem")) {
Andrea Arcangeli67e80322017-09-06 16:23:46 -07001241 map_shared = true;
Mike Rapoportb6ad1972017-05-03 14:54:54 -07001242 test_type = TEST_SHMEM;
1243 uffd_test_ops = &shmem_uffd_test_ops;
1244 } else {
1245 fprintf(stderr, "Unknown test type: %s\n", type), exit(1);
1246 }
1247
1248 if (test_type == TEST_HUGETLB)
1249 page_size = default_huge_page_size();
1250 else
1251 page_size = sysconf(_SC_PAGE_SIZE);
1252
Mike Kravetz9903bd72017-02-22 15:43:07 -08001253 if (!page_size)
Mike Rapoportb6ad1972017-05-03 14:54:54 -07001254 fprintf(stderr, "Unable to determine page size\n"),
Mike Kravetz9903bd72017-02-22 15:43:07 -08001255 exit(2);
1256 if ((unsigned long) area_count(NULL, 0) + sizeof(unsigned long long) * 2
1257 > page_size)
1258 fprintf(stderr, "Impossible to run this test\n"), exit(2);
Mike Rapoportb6ad1972017-05-03 14:54:54 -07001259}
1260
Andrea Arcangeli67e80322017-09-06 16:23:46 -07001261static void sigalrm(int sig)
1262{
1263 if (sig != SIGALRM)
1264 abort();
1265 test_uffdio_copy_eexist = true;
1266 test_uffdio_zeropage_eexist = true;
1267 alarm(ALARM_INTERVAL_SECS);
1268}
1269
Mike Rapoportb6ad1972017-05-03 14:54:54 -07001270int main(int argc, char **argv)
1271{
1272 if (argc < 4)
1273 fprintf(stderr, "Usage: <test type> <MiB> <bounces> [hugetlbfs_file]\n"),
1274 exit(1);
1275
Andrea Arcangeli67e80322017-09-06 16:23:46 -07001276 if (signal(SIGALRM, sigalrm) == SIG_ERR)
1277 fprintf(stderr, "failed to arm SIGALRM"), exit(1);
1278 alarm(ALARM_INTERVAL_SECS);
1279
Mike Rapoportb6ad1972017-05-03 14:54:54 -07001280 set_test_type(argv[1]);
1281
1282 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1283 nr_pages_per_cpu = atol(argv[2]) * 1024*1024 / page_size /
Mike Kravetz9903bd72017-02-22 15:43:07 -08001284 nr_cpus;
1285 if (!nr_pages_per_cpu) {
1286 fprintf(stderr, "invalid MiB\n");
1287 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
1288 }
Mike Rapoportb6ad1972017-05-03 14:54:54 -07001289
1290 bounces = atoi(argv[3]);
Mike Kravetz9903bd72017-02-22 15:43:07 -08001291 if (bounces <= 0) {
1292 fprintf(stderr, "invalid bounces\n");
1293 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
1294 }
1295 nr_pages = nr_pages_per_cpu * nr_cpus;
Mike Rapoportb6ad1972017-05-03 14:54:54 -07001296
1297 if (test_type == TEST_HUGETLB) {
1298 if (argc < 5)
1299 fprintf(stderr, "Usage: hugetlb <MiB> <bounces> <hugetlbfs_file>\n"),
1300 exit(1);
1301 huge_fd = open(argv[4], O_CREAT | O_RDWR, 0755);
1302 if (huge_fd < 0) {
1303 fprintf(stderr, "Open of %s failed", argv[3]);
1304 perror("open");
1305 exit(1);
1306 }
1307 if (ftruncate(huge_fd, 0)) {
1308 fprintf(stderr, "ftruncate %s to size 0 failed", argv[3]);
1309 perror("ftruncate");
1310 exit(1);
1311 }
Mike Kravetz9903bd72017-02-22 15:43:07 -08001312 }
1313 printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
1314 nr_pages, nr_pages_per_cpu);
1315 return userfaultfd_stress();
1316}
1317
Michael Ellerman56ed8f12015-09-22 14:58:58 -07001318#else /* __NR_userfaultfd */
1319
1320#warning "missing __NR_userfaultfd definition"
1321
1322int main(void)
1323{
1324 printf("skip: Skipping userfaultfd test (missing __NR_userfaultfd)\n");
1325 return 0;
1326}
1327
1328#endif /* __NR_userfaultfd */