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 "../../../../include/uapi/linux/userfaultfd.h"
70 #define __NR_userfaultfd 323
71 #elif defined(__i386__)
72 #define __NR_userfaultfd 374
73 #elif defined(__powewrpc__)
74 #define __NR_userfaultfd 364
76 #error "missing __NR_userfaultfd definition"
79 static unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;
81 #define BOUNCE_RANDOM (1<<0)
82 #define BOUNCE_RACINGFAULTS (1<<1)
83 #define BOUNCE_VERIFY (1<<2)
84 #define BOUNCE_POLL (1<<3)
87 static unsigned long long *count_verify;
88 static int uffd, finished, *pipefd;
89 static char *area_src, *area_dst;
90 static char *zeropage;
93 /* pthread_mutex_t starts at page offset 0 */
94 #define area_mutex(___area, ___nr) \
95 ((pthread_mutex_t *) ((___area) + (___nr)*page_size))
97 * count is placed in the page after pthread_mutex_t naturally aligned
98 * to avoid non alignment faults on non-x86 archs.
100 #define area_count(___area, ___nr) \
101 ((volatile unsigned long long *) ((unsigned long) \
102 ((___area) + (___nr)*page_size + \
103 sizeof(pthread_mutex_t) + \
104 sizeof(unsigned long long) - 1) & \
105 ~(unsigned long)(sizeof(unsigned long long) \
108 static int my_bcmp(char *str1, char *str2, size_t n)
111 for (i = 0; i < n; i++)
112 if (str1[i] != str2[i])
117 static void *locking_thread(void *arg)
119 unsigned long cpu = (unsigned long) arg;
120 struct random_data rand;
121 unsigned long page_nr = *(&(page_nr)); /* uninitialized warning */
123 unsigned long long count;
128 if (bounces & BOUNCE_RANDOM) {
129 seed = (unsigned int) time(NULL) - bounces;
130 if (!(bounces & BOUNCE_RACINGFAULTS))
132 bzero(&rand, sizeof(rand));
133 bzero(&randstate, sizeof(randstate));
134 if (initstate_r(seed, randstate, sizeof(randstate), &rand))
135 fprintf(stderr, "srandom_r error\n"), exit(1);
138 if (!(bounces & BOUNCE_RACINGFAULTS))
139 page_nr += cpu * nr_pages_per_cpu;
143 if (bounces & BOUNCE_RANDOM) {
144 if (random_r(&rand, &rand_nr))
145 fprintf(stderr, "random_r 1 error\n"), exit(1);
147 if (sizeof(page_nr) > sizeof(rand_nr)) {
148 if (random_r(&rand, &rand_nr))
149 fprintf(stderr, "random_r 2 error\n"), exit(1);
150 page_nr |= (((unsigned long) rand_nr) << 16) <<
158 if (bounces & BOUNCE_VERIFY) {
159 count = *area_count(area_dst, page_nr);
162 "page_nr %lu wrong count %Lu %Lu\n",
164 count_verify[page_nr]), exit(1);
168 * We can't use bcmp (or memcmp) because that
169 * returns 0 erroneously if the memory is
170 * changing under it (even if the end of the
171 * page is never changing and always
175 if (!my_bcmp(area_dst + page_nr * page_size, zeropage,
178 "my_bcmp page_nr %lu wrong count %Lu %Lu\n",
180 count_verify[page_nr]), exit(1);
185 /* uncomment the below line to test with mutex */
186 /* pthread_mutex_lock(area_mutex(area_dst, page_nr)); */
187 while (!bcmp(area_dst + page_nr * page_size, zeropage,
193 /* uncomment below line to test with mutex */
194 /* pthread_mutex_unlock(area_mutex(area_dst, page_nr)); */
197 "page_nr %lu all zero thread %lu %p %lu\n",
198 page_nr, cpu, area_dst + page_nr * page_size,
206 pthread_mutex_lock(area_mutex(area_dst, page_nr));
207 count = *area_count(area_dst, page_nr);
208 if (count != count_verify[page_nr]) {
210 "page_nr %lu memory corruption %Lu %Lu\n",
212 count_verify[page_nr]), exit(1);
215 *area_count(area_dst, page_nr) = count_verify[page_nr] = count;
216 pthread_mutex_unlock(area_mutex(area_dst, page_nr));
218 if (time(NULL) - start > 1)
220 "userfault too slow %ld "
221 "possible false positive with overcommit\n",
228 static int copy_page(unsigned long offset)
230 struct uffdio_copy uffdio_copy;
232 if (offset >= nr_pages * page_size)
233 fprintf(stderr, "unexpected offset %lu\n",
235 uffdio_copy.dst = (unsigned long) area_dst + offset;
236 uffdio_copy.src = (unsigned long) area_src + offset;
237 uffdio_copy.len = page_size;
238 uffdio_copy.mode = 0;
239 uffdio_copy.copy = 0;
240 if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy)) {
241 /* real retval in ufdio_copy.copy */
242 if (uffdio_copy.copy != -EEXIST)
243 fprintf(stderr, "UFFDIO_COPY error %Ld\n",
244 uffdio_copy.copy), exit(1);
245 } else if (uffdio_copy.copy != page_size) {
246 fprintf(stderr, "UFFDIO_COPY unexpected copy %Ld\n",
247 uffdio_copy.copy), exit(1);
253 static void *uffd_poll_thread(void *arg)
255 unsigned long cpu = (unsigned long) arg;
256 struct pollfd pollfd[2];
259 unsigned long offset;
261 unsigned long userfaults = 0;
264 pollfd[0].events = POLLIN;
265 pollfd[1].fd = pipefd[cpu*2];
266 pollfd[1].events = POLLIN;
269 ret = poll(pollfd, 2, -1);
271 fprintf(stderr, "poll error %d\n", ret), exit(1);
273 perror("poll"), exit(1);
274 if (pollfd[1].revents & POLLIN) {
275 if (read(pollfd[1].fd, &tmp_chr, 1) != 1)
276 fprintf(stderr, "read pipefd error\n"),
280 if (!(pollfd[0].revents & POLLIN))
281 fprintf(stderr, "pollfd[0].revents %d\n",
282 pollfd[0].revents), exit(1);
283 ret = read(uffd, &msg, sizeof(msg));
287 perror("nonblocking read error"), exit(1);
289 if (msg.event != UFFD_EVENT_PAGEFAULT)
290 fprintf(stderr, "unexpected msg event %u\n",
292 if (msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
293 fprintf(stderr, "unexpected write fault\n"), exit(1);
294 offset = (char *)(unsigned long)msg.arg.pagefault.address -
296 offset &= ~(page_size-1);
297 if (copy_page(offset))
300 return (void *)userfaults;
303 pthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER;
305 static void *uffd_read_thread(void *arg)
307 unsigned long *this_cpu_userfaults;
309 unsigned long offset;
312 this_cpu_userfaults = (unsigned long *) arg;
313 *this_cpu_userfaults = 0;
315 pthread_mutex_unlock(&uffd_read_mutex);
316 /* from here cancellation is ok */
319 ret = read(uffd, &msg, sizeof(msg));
320 if (ret != sizeof(msg)) {
322 perror("blocking read error"), exit(1);
324 fprintf(stderr, "short read\n"), exit(1);
326 if (msg.event != UFFD_EVENT_PAGEFAULT)
327 fprintf(stderr, "unexpected msg event %u\n",
329 if (bounces & BOUNCE_VERIFY &&
330 msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
331 fprintf(stderr, "unexpected write fault\n"), exit(1);
332 offset = (char *)(unsigned long)msg.arg.pagefault.address -
334 offset &= ~(page_size-1);
335 if (copy_page(offset))
336 (*this_cpu_userfaults)++;
341 static void *background_thread(void *arg)
343 unsigned long cpu = (unsigned long) arg;
344 unsigned long page_nr;
346 for (page_nr = cpu * nr_pages_per_cpu;
347 page_nr < (cpu+1) * nr_pages_per_cpu;
349 copy_page(page_nr * page_size);
354 static int stress(unsigned long *userfaults)
357 pthread_t locking_threads[nr_cpus];
358 pthread_t uffd_threads[nr_cpus];
359 pthread_t background_threads[nr_cpus];
360 void **_userfaults = (void **) userfaults;
363 for (cpu = 0; cpu < nr_cpus; cpu++) {
364 if (pthread_create(&locking_threads[cpu], &attr,
365 locking_thread, (void *)cpu))
367 if (bounces & BOUNCE_POLL) {
368 if (pthread_create(&uffd_threads[cpu], &attr,
369 uffd_poll_thread, (void *)cpu))
372 if (pthread_create(&uffd_threads[cpu], &attr,
376 pthread_mutex_lock(&uffd_read_mutex);
378 if (pthread_create(&background_threads[cpu], &attr,
379 background_thread, (void *)cpu))
382 for (cpu = 0; cpu < nr_cpus; cpu++)
383 if (pthread_join(background_threads[cpu], NULL))
387 * Be strict and immediately zap area_src, the whole area has
388 * been transferred already by the background treads. The
389 * area_src could then be faulted in in a racy way by still
390 * running uffdio_threads reading zeropages after we zapped
391 * area_src (but they're guaranteed to get -EEXIST from
392 * UFFDIO_COPY without writing zero pages into area_dst
393 * because the background threads already completed).
395 if (madvise(area_src, nr_pages * page_size, MADV_DONTNEED)) {
400 for (cpu = 0; cpu < nr_cpus; cpu++) {
402 if (bounces & BOUNCE_POLL) {
403 if (write(pipefd[cpu*2+1], &c, 1) != 1) {
404 fprintf(stderr, "pipefd write error\n");
407 if (pthread_join(uffd_threads[cpu], &_userfaults[cpu]))
410 if (pthread_cancel(uffd_threads[cpu]))
412 if (pthread_join(uffd_threads[cpu], NULL))
418 for (cpu = 0; cpu < nr_cpus; cpu++)
419 if (pthread_join(locking_threads[cpu], NULL))
425 static int userfaultfd_stress(void)
430 struct uffdio_register uffdio_register;
431 struct uffdio_api uffdio_api;
434 unsigned long userfaults[nr_cpus];
436 if (posix_memalign(&area, page_size, nr_pages * page_size)) {
437 fprintf(stderr, "out of memory\n");
441 if (posix_memalign(&area, page_size, nr_pages * page_size)) {
442 fprintf(stderr, "out of memory\n");
447 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
450 "userfaultfd syscall not available in this kernel\n");
453 uffd_flags = fcntl(uffd, F_GETFD, NULL);
455 uffdio_api.api = UFFD_API;
456 uffdio_api.features = 0;
457 if (ioctl(uffd, UFFDIO_API, &uffdio_api)) {
458 fprintf(stderr, "UFFDIO_API\n");
461 if (uffdio_api.api != UFFD_API) {
462 fprintf(stderr, "UFFDIO_API error %Lu\n", uffdio_api.api);
466 count_verify = malloc(nr_pages * sizeof(unsigned long long));
468 perror("count_verify");
472 for (nr = 0; nr < nr_pages; nr++) {
473 *area_mutex(area_src, nr) = (pthread_mutex_t)
474 PTHREAD_MUTEX_INITIALIZER;
475 count_verify[nr] = *area_count(area_src, nr) = 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);
503 unsigned long expected_ioctls;
505 printf("bounces: %d, mode:", bounces);
506 if (bounces & BOUNCE_RANDOM)
508 if (bounces & BOUNCE_RACINGFAULTS)
510 if (bounces & BOUNCE_VERIFY)
512 if (bounces & BOUNCE_POLL)
517 if (bounces & BOUNCE_POLL)
518 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
520 fcntl(uffd, F_SETFL, uffd_flags & ~O_NONBLOCK);
523 uffdio_register.range.start = (unsigned long) area_dst;
524 uffdio_register.range.len = nr_pages * page_size;
525 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
526 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
527 fprintf(stderr, "register failure\n");
530 expected_ioctls = (1 << _UFFDIO_WAKE) |
531 (1 << _UFFDIO_COPY) |
532 (1 << _UFFDIO_ZEROPAGE);
533 if ((uffdio_register.ioctls & expected_ioctls) !=
536 "unexpected missing ioctl for anon memory\n");
541 * The madvise done previously isn't enough: some
542 * uffd_thread could have read userfaults (one of
543 * those already resolved by the background thread)
544 * and it may be in the process of calling
545 * UFFDIO_COPY. UFFDIO_COPY will read the zapped
546 * area_src and it would map a zero page in it (of
547 * course such a UFFDIO_COPY is perfectly safe as it'd
548 * return -EEXIST). The problem comes at the next
549 * bounce though: that racing UFFDIO_COPY would
550 * generate zeropages in the area_src, so invalidating
551 * the previous MADV_DONTNEED. Without this additional
552 * MADV_DONTNEED those zeropages leftovers in the
553 * area_src would lead to -EEXIST failure during the
554 * next bounce, effectively leaving a zeropage in the
557 * Try to comment this out madvise to see the memory
558 * corruption being caught pretty quick.
560 * khugepaged is also inhibited to collapse THP after
561 * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's
562 * required to MADV_DONTNEED here.
564 if (madvise(area_dst, nr_pages * page_size, MADV_DONTNEED)) {
570 if (stress(userfaults))
574 if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) {
575 fprintf(stderr, "register failure\n");
580 if (bounces & BOUNCE_VERIFY) {
581 for (nr = 0; nr < nr_pages; nr++) {
582 if (my_bcmp(area_dst,
583 area_dst + nr * page_size,
584 sizeof(pthread_mutex_t))) {
586 "error mutex 2 %lu\n",
590 if (*area_count(area_dst, nr) != count_verify[nr]) {
592 "error area_count %Lu %Lu %lu\n",
593 *area_count(area_src, nr),
601 /* prepare next bounce */
606 printf("userfaults:");
607 for (cpu = 0; cpu < nr_cpus; cpu++)
608 printf(" %lu", userfaults[cpu]);
615 int main(int argc, char **argv)
618 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
619 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
620 page_size = sysconf(_SC_PAGE_SIZE);
621 if ((unsigned long) area_count(NULL, 0) + sizeof(unsigned long long) >
623 fprintf(stderr, "Impossible to run this test\n"), exit(2);
624 nr_pages_per_cpu = atol(argv[1]) * 1024*1024 / page_size /
626 if (!nr_pages_per_cpu) {
627 fprintf(stderr, "invalid MiB\n");
628 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
630 bounces = atoi(argv[2]);
632 fprintf(stderr, "invalid bounces\n");
633 fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
635 nr_pages = nr_pages_per_cpu * nr_cpus;
636 printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
637 nr_pages, nr_pages_per_cpu);
638 return userfaultfd_stress();