2 * Stress userfaultfd syscall.
4 * Copyright (C) 2015 Red Hat, Inc.
6 * This work is licensed under the terms of the GNU GPL, version 2. See
7 * the COPYING file in the top-level directory.
9 * This test allocates two virtual areas and bounces the physical
10 * memory across the two virtual areas (from area_src to area_dst)
13 * There are three threads running per CPU:
15 * 1) one per-CPU thread takes a per-page pthread_mutex in a random
16 * page of the area_dst (while the physical page may still be in
17 * area_src), and increments a per-page counter in the same page,
18 * and checks its value against a verification region.
20 * 2) another per-CPU thread handles the userfaults generated by
21 * thread 1 above. userfaultfd blocking reads or poll() modes are
22 * exercised interleaved.
24 * 3) one last per-CPU thread transfers the memory in the background
25 * at maximum bandwidth (if not already transferred by thread
26 * 2). Each cpu thread takes cares of transferring a portion of the
29 * When all threads of type 3 completed the transfer, one bounce is
30 * complete. area_src and area_dst are then swapped. All threads are
31 * respawned and so the bounce is immediately restarted in the
34 * per-CPU threads 1 by triggering userfaults inside
35 * pthread_mutex_lock will also verify the atomicity of the memory
36 * transfer (UFFDIO_COPY).
38 * The program takes two parameters: the amounts of physical memory in
39 * megabytes (MiB) of the area and the number of bounces to execute.
41 * # 100MiB 99999 bounces
42 * ./userfaultfd 100 99999
45 * ./userfaultfd 1000 99
47 * # 10MiB-~6GiB 999 bounces, continue forever unless an error triggers
48 * while ./userfaultfd $[RANDOM % 6000 + 10] 999; do true; done
56 #include <sys/types.h>
64 #include <sys/syscall.h>
65 #include <sys/ioctl.h>
67 #include <linux/userfaultfd.h>
69 #ifdef __NR_userfaultfd
71 static unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;
73 #define BOUNCE_RANDOM (1<<0)
74 #define BOUNCE_RACINGFAULTS (1<<1)
75 #define BOUNCE_VERIFY (1<<2)
76 #define BOUNCE_POLL (1<<3)
79 static unsigned long long *count_verify;
80 static int uffd, finished, *pipefd;
81 static char *area_src, *area_dst;
82 static char *zeropage;
85 /* pthread_mutex_t starts at page offset 0 */
86 #define area_mutex(___area, ___nr) \
87 ((pthread_mutex_t *) ((___area) + (___nr)*page_size))
89 * count is placed in the page after pthread_mutex_t naturally aligned
90 * to avoid non alignment faults on non-x86 archs.
92 #define area_count(___area, ___nr) \
93 ((volatile unsigned long long *) ((unsigned long) \
94 ((___area) + (___nr)*page_size + \
95 sizeof(pthread_mutex_t) + \
96 sizeof(unsigned long long) - 1) & \
97 ~(unsigned long)(sizeof(unsigned long long) \
100 static int my_bcmp(char *str1, char *str2, size_t n)
103 for (i = 0; i < n; i++)
104 if (str1[i] != str2[i])
109 static void *locking_thread(void *arg)
111 unsigned long cpu = (unsigned long) arg;
112 struct random_data rand;
113 unsigned long page_nr = *(&(page_nr)); /* uninitialized warning */
115 unsigned long long count;
120 if (bounces & BOUNCE_RANDOM) {
121 seed = (unsigned int) time(NULL) - bounces;
122 if (!(bounces & BOUNCE_RACINGFAULTS))
124 bzero(&rand, sizeof(rand));
125 bzero(&randstate, sizeof(randstate));
126 if (initstate_r(seed, randstate, sizeof(randstate), &rand))
127 fprintf(stderr, "srandom_r error\n"), exit(1);
130 if (!(bounces & BOUNCE_RACINGFAULTS))
131 page_nr += cpu * nr_pages_per_cpu;
135 if (bounces & BOUNCE_RANDOM) {
136 if (random_r(&rand, &rand_nr))
137 fprintf(stderr, "random_r 1 error\n"), exit(1);
139 if (sizeof(page_nr) > sizeof(rand_nr)) {
140 if (random_r(&rand, &rand_nr))
141 fprintf(stderr, "random_r 2 error\n"), exit(1);
142 page_nr |= (((unsigned long) rand_nr) << 16) <<
150 if (bounces & BOUNCE_VERIFY) {
151 count = *area_count(area_dst, page_nr);
154 "page_nr %lu wrong count %Lu %Lu\n",
156 count_verify[page_nr]), exit(1);
160 * We can't use bcmp (or memcmp) because that
161 * returns 0 erroneously if the memory is
162 * changing under it (even if the end of the
163 * page is never changing and always
167 if (!my_bcmp(area_dst + page_nr * page_size, zeropage,
170 "my_bcmp page_nr %lu wrong count %Lu %Lu\n",
172 count_verify[page_nr]), exit(1);
177 /* uncomment the below line to test with mutex */
178 /* pthread_mutex_lock(area_mutex(area_dst, page_nr)); */
179 while (!bcmp(area_dst + page_nr * page_size, zeropage,
185 /* uncomment below line to test with mutex */
186 /* pthread_mutex_unlock(area_mutex(area_dst, page_nr)); */
189 "page_nr %lu all zero thread %lu %p %lu\n",
190 page_nr, cpu, area_dst + page_nr * page_size,
198 pthread_mutex_lock(area_mutex(area_dst, page_nr));
199 count = *area_count(area_dst, page_nr);
200 if (count != count_verify[page_nr]) {
202 "page_nr %lu memory corruption %Lu %Lu\n",
204 count_verify[page_nr]), exit(1);
207 *area_count(area_dst, page_nr) = count_verify[page_nr] = count;
208 pthread_mutex_unlock(area_mutex(area_dst, page_nr));
210 if (time(NULL) - start > 1)
212 "userfault too slow %ld "
213 "possible false positive with overcommit\n",
220 static int copy_page(unsigned long offset)
222 struct uffdio_copy uffdio_copy;
224 if (offset >= nr_pages * page_size)
225 fprintf(stderr, "unexpected offset %lu\n",
227 uffdio_copy.dst = (unsigned long) area_dst + offset;
228 uffdio_copy.src = (unsigned long) area_src + offset;
229 uffdio_copy.len = page_size;
230 uffdio_copy.mode = 0;
231 uffdio_copy.copy = 0;
232 if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy)) {
233 /* real retval in ufdio_copy.copy */
234 if (uffdio_copy.copy != -EEXIST)
235 fprintf(stderr, "UFFDIO_COPY error %Ld\n",
236 uffdio_copy.copy), exit(1);
237 } else if (uffdio_copy.copy != page_size) {
238 fprintf(stderr, "UFFDIO_COPY unexpected copy %Ld\n",
239 uffdio_copy.copy), exit(1);
245 static void *uffd_poll_thread(void *arg)
247 unsigned long cpu = (unsigned long) arg;
248 struct pollfd pollfd[2];
251 unsigned long offset;
253 unsigned long userfaults = 0;
256 pollfd[0].events = POLLIN;
257 pollfd[1].fd = pipefd[cpu*2];
258 pollfd[1].events = POLLIN;
261 ret = poll(pollfd, 2, -1);
263 fprintf(stderr, "poll error %d\n", ret), exit(1);
265 perror("poll"), exit(1);
266 if (pollfd[1].revents & POLLIN) {
267 if (read(pollfd[1].fd, &tmp_chr, 1) != 1)
268 fprintf(stderr, "read pipefd error\n"),
272 if (!(pollfd[0].revents & POLLIN))
273 fprintf(stderr, "pollfd[0].revents %d\n",
274 pollfd[0].revents), exit(1);
275 ret = read(uffd, &msg, sizeof(msg));
279 perror("nonblocking read error"), exit(1);
281 if (msg.event != UFFD_EVENT_PAGEFAULT)
282 fprintf(stderr, "unexpected msg event %u\n",
284 if (msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
285 fprintf(stderr, "unexpected write fault\n"), exit(1);
286 offset = (char *)(unsigned long)msg.arg.pagefault.address -
288 offset &= ~(page_size-1);
289 if (copy_page(offset))
292 return (void *)userfaults;
295 pthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER;
297 static void *uffd_read_thread(void *arg)
299 unsigned long *this_cpu_userfaults;
301 unsigned long offset;
304 this_cpu_userfaults = (unsigned long *) arg;
305 *this_cpu_userfaults = 0;
307 pthread_mutex_unlock(&uffd_read_mutex);
308 /* from here cancellation is ok */
311 ret = read(uffd, &msg, sizeof(msg));
312 if (ret != sizeof(msg)) {
314 perror("blocking read error"), exit(1);
316 fprintf(stderr, "short read\n"), exit(1);
318 if (msg.event != UFFD_EVENT_PAGEFAULT)
319 fprintf(stderr, "unexpected msg event %u\n",
321 if (bounces & BOUNCE_VERIFY &&
322 msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
323 fprintf(stderr, "unexpected write fault\n"), exit(1);
324 offset = (char *)(unsigned long)msg.arg.pagefault.address -
326 offset &= ~(page_size-1);
327 if (copy_page(offset))
328 (*this_cpu_userfaults)++;
333 static void *background_thread(void *arg)
335 unsigned long cpu = (unsigned long) arg;
336 unsigned long page_nr;
338 for (page_nr = cpu * nr_pages_per_cpu;
339 page_nr < (cpu+1) * nr_pages_per_cpu;
341 copy_page(page_nr * page_size);
346 static int stress(unsigned long *userfaults)
349 pthread_t locking_threads[nr_cpus];
350 pthread_t uffd_threads[nr_cpus];
351 pthread_t background_threads[nr_cpus];
352 void **_userfaults = (void **) userfaults;
355 for (cpu = 0; cpu < nr_cpus; cpu++) {
356 if (pthread_create(&locking_threads[cpu], &attr,
357 locking_thread, (void *)cpu))
359 if (bounces & BOUNCE_POLL) {
360 if (pthread_create(&uffd_threads[cpu], &attr,
361 uffd_poll_thread, (void *)cpu))
364 if (pthread_create(&uffd_threads[cpu], &attr,
368 pthread_mutex_lock(&uffd_read_mutex);
370 if (pthread_create(&background_threads[cpu], &attr,
371 background_thread, (void *)cpu))
374 for (cpu = 0; cpu < nr_cpus; cpu++)
375 if (pthread_join(background_threads[cpu], NULL))
379 * Be strict and immediately zap area_src, the whole area has
380 * been transferred already by the background treads. The
381 * area_src could then be faulted in in a racy way by still
382 * running uffdio_threads reading zeropages after we zapped
383 * area_src (but they're guaranteed to get -EEXIST from
384 * UFFDIO_COPY without writing zero pages into area_dst
385 * because the background threads already completed).
387 if (madvise(area_src, nr_pages * page_size, MADV_DONTNEED)) {
392 for (cpu = 0; cpu < nr_cpus; cpu++) {
394 if (bounces & BOUNCE_POLL) {
395 if (write(pipefd[cpu*2+1], &c, 1) != 1) {
396 fprintf(stderr, "pipefd write error\n");
399 if (pthread_join(uffd_threads[cpu], &_userfaults[cpu]))
402 if (pthread_cancel(uffd_threads[cpu]))
404 if (pthread_join(uffd_threads[cpu], NULL))
410 for (cpu = 0; cpu < nr_cpus; cpu++)
411 if (pthread_join(locking_threads[cpu], NULL))
417 static int userfaultfd_stress(void)
422 struct uffdio_register uffdio_register;
423 struct uffdio_api uffdio_api;
426 unsigned long userfaults[nr_cpus];
428 if (posix_memalign(&area, page_size, nr_pages * page_size)) {
429 fprintf(stderr, "out of memory\n");
433 if (posix_memalign(&area, page_size, nr_pages * page_size)) {
434 fprintf(stderr, "out of memory\n");
439 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
442 "userfaultfd syscall not available in this kernel\n");
445 uffd_flags = fcntl(uffd, F_GETFD, NULL);
447 uffdio_api.api = UFFD_API;
448 uffdio_api.features = 0;
449 if (ioctl(uffd, UFFDIO_API, &uffdio_api)) {
450 fprintf(stderr, "UFFDIO_API\n");
453 if (uffdio_api.api != UFFD_API) {
454 fprintf(stderr, "UFFDIO_API error %Lu\n", uffdio_api.api);
458 count_verify = malloc(nr_pages * sizeof(unsigned long long));
460 perror("count_verify");
464 for (nr = 0; nr < nr_pages; nr++) {
465 *area_mutex(area_src, nr) = (pthread_mutex_t)
466 PTHREAD_MUTEX_INITIALIZER;
467 count_verify[nr] = *area_count(area_src, nr) = 1;
469 * In the transition between 255 to 256, powerpc will
470 * read out of order in my_bcmp and see both bytes as
471 * zero, so leave a placeholder below always non-zero
472 * after the count, to avoid my_bcmp to trigger false
475 *(area_count(area_src, nr) + 1) = 1;
478 pipefd = malloc(sizeof(int) * nr_cpus * 2);
483 for (cpu = 0; cpu < nr_cpus; cpu++) {
484 if (pipe2(&pipefd[cpu*2], O_CLOEXEC | O_NONBLOCK)) {
490 if (posix_memalign(&area, page_size, page_size)) {
491 fprintf(stderr, "out of memory\n");
495 bzero(zeropage, page_size);
497 pthread_mutex_lock(&uffd_read_mutex);
499 pthread_attr_init(&attr);
500 pthread_attr_setstacksize(&attr, 16*1024*1024);
504 unsigned long expected_ioctls;
506 printf("bounces: %d, mode:", bounces);
507 if (bounces & BOUNCE_RANDOM)
509 if (bounces & BOUNCE_RACINGFAULTS)
511 if (bounces & BOUNCE_VERIFY)
513 if (bounces & BOUNCE_POLL)
518 if (bounces & BOUNCE_POLL)
519 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
521 fcntl(uffd, F_SETFL, uffd_flags & ~O_NONBLOCK);
524 uffdio_register.range.start = (unsigned long) area_dst;
525 uffdio_register.range.len = nr_pages * page_size;
526 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
527 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
528 fprintf(stderr, "register failure\n");
531 expected_ioctls = (1 << _UFFDIO_WAKE) |
532 (1 << _UFFDIO_COPY) |
533 (1 << _UFFDIO_ZEROPAGE);
534 if ((uffdio_register.ioctls & expected_ioctls) !=
537 "unexpected missing ioctl for anon memory\n");
542 * The madvise done previously isn't enough: some
543 * uffd_thread could have read userfaults (one of
544 * those already resolved by the background thread)
545 * and it may be in the process of calling
546 * UFFDIO_COPY. UFFDIO_COPY will read the zapped
547 * area_src and it would map a zero page in it (of
548 * course such a UFFDIO_COPY is perfectly safe as it'd
549 * return -EEXIST). The problem comes at the next
550 * bounce though: that racing UFFDIO_COPY would
551 * generate zeropages in the area_src, so invalidating
552 * the previous MADV_DONTNEED. Without this additional
553 * MADV_DONTNEED those zeropages leftovers in the
554 * area_src would lead to -EEXIST failure during the
555 * next bounce, effectively leaving a zeropage in the
558 * Try to comment this out madvise to see the memory
559 * corruption being caught pretty quick.
561 * khugepaged is also inhibited to collapse THP after
562 * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's
563 * required to MADV_DONTNEED here.
565 if (madvise(area_dst, nr_pages * page_size, MADV_DONTNEED)) {
571 if (stress(userfaults))
575 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) {
576 fprintf(stderr, "register failure\n");
581 if (bounces & BOUNCE_VERIFY) {
582 for (nr = 0; nr < nr_pages; nr++) {
583 if (*area_count(area_dst, nr) != count_verify[nr]) {
585 "error area_count %Lu %Lu %lu\n",
586 *area_count(area_src, nr),
595 /* prepare next bounce */
600 printf("userfaults:");
601 for (cpu = 0; cpu < nr_cpus; cpu++)
602 printf(" %lu", userfaults[cpu]);
609 int main(int argc, char **argv)
612 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
613 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
614 page_size = sysconf(_SC_PAGE_SIZE);
615 if ((unsigned long) area_count(NULL, 0) + sizeof(unsigned long long) * 2
617 fprintf(stderr, "Impossible to run this test\n"), exit(2);
618 nr_pages_per_cpu = atol(argv[1]) * 1024*1024 / page_size /
620 if (!nr_pages_per_cpu) {
621 fprintf(stderr, "invalid MiB\n");
622 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
624 bounces = atoi(argv[2]);
626 fprintf(stderr, "invalid bounces\n");
627 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
629 nr_pages = nr_pages_per_cpu * nr_cpus;
630 printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
631 nr_pages, nr_pages_per_cpu);
632 return userfaultfd_stress();
635 #else /* __NR_userfaultfd */
637 #warning "missing __NR_userfaultfd definition"
641 printf("skip: Skipping userfaultfd test (missing __NR_userfaultfd)\n");
645 #endif /* __NR_userfaultfd */