1 // SPDX-License-Identifier: GPL-2.0-only
3 * Stress userfaultfd syscall.
5 * Copyright (C) 2015 Red Hat, Inc.
7 * This test allocates two virtual areas and bounces the physical
8 * memory across the two virtual areas (from area_src to area_dst)
11 * There are three threads running per CPU:
13 * 1) one per-CPU thread takes a per-page pthread_mutex in a random
14 * page of the area_dst (while the physical page may still be in
15 * area_src), and increments a per-page counter in the same page,
16 * and checks its value against a verification region.
18 * 2) another per-CPU thread handles the userfaults generated by
19 * thread 1 above. userfaultfd blocking reads or poll() modes are
20 * exercised interleaved.
22 * 3) one last per-CPU thread transfers the memory in the background
23 * at maximum bandwidth (if not already transferred by thread
24 * 2). Each cpu thread takes cares of transferring a portion of the
27 * When all threads of type 3 completed the transfer, one bounce is
28 * complete. area_src and area_dst are then swapped. All threads are
29 * respawned and so the bounce is immediately restarted in the
32 * per-CPU threads 1 by triggering userfaults inside
33 * pthread_mutex_lock will also verify the atomicity of the memory
34 * transfer (UFFDIO_COPY).
36 #include <asm-generic/unistd.h>
37 #include "uffd-common.h"
41 #define BOUNCE_RANDOM (1<<0)
42 #define BOUNCE_RACINGFAULTS (1<<1)
43 #define BOUNCE_VERIFY (1<<2)
44 #define BOUNCE_POLL (1<<3)
47 /* exercise the test_uffdio_*_eexist every ALARM_INTERVAL_SECS */
48 #define ALARM_INTERVAL_SECS 10
49 static char *zeropage
;
53 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
55 const char *examples
=
56 "# Run anonymous memory test on 100MiB region with 99999 bounces:\n"
57 "./uffd-stress anon 100 99999\n\n"
58 "# Run share memory test on 1GiB region with 99 bounces:\n"
59 "./uffd-stress shmem 1000 99\n\n"
60 "# Run hugetlb memory test on 256MiB region with 50 bounces:\n"
61 "./uffd-stress hugetlb 256 50\n\n"
62 "# Run the same hugetlb test but using private file:\n"
63 "./uffd-stress hugetlb-private 256 50\n\n"
64 "# 10MiB-~6GiB 999 bounces anonymous test, "
65 "continue forever unless an error triggers\n"
66 "while ./uffd-stress anon $[RANDOM % 6000 + 10] 999; do true; done\n\n";
68 static void usage(void)
70 fprintf(stderr
, "\nUsage: ./uffd-stress <test type> <MiB> <bounces>\n\n");
71 fprintf(stderr
, "Supported <test type>: anon, hugetlb, "
72 "hugetlb-private, shmem, shmem-private\n\n");
73 fprintf(stderr
, "Examples:\n\n");
74 fprintf(stderr
, "%s", examples
);
78 static void uffd_stats_reset(struct uffd_args
*args
, unsigned long n_cpus
)
82 for (i
= 0; i
< n_cpus
; i
++) {
84 args
[i
].apply_wp
= test_uffdio_wp
;
85 args
[i
].missing_faults
= 0;
86 args
[i
].wp_faults
= 0;
87 args
[i
].minor_faults
= 0;
91 static void *locking_thread(void *arg
)
93 unsigned long cpu
= (unsigned long) arg
;
94 unsigned long page_nr
;
95 unsigned long long count
;
97 if (!(bounces
& BOUNCE_RANDOM
)) {
99 if (!(bounces
& BOUNCE_RACINGFAULTS
))
100 page_nr
+= cpu
* nr_pages_per_cpu
;
104 if (bounces
& BOUNCE_RANDOM
) {
105 if (getrandom(&page_nr
, sizeof(page_nr
), 0) != sizeof(page_nr
))
106 err("getrandom failed");
110 pthread_mutex_lock(area_mutex(area_dst
, page_nr
));
111 count
= *area_count(area_dst
, page_nr
);
112 if (count
!= count_verify
[page_nr
])
113 err("page_nr %lu memory corruption %llu %llu",
114 page_nr
, count
, count_verify
[page_nr
]);
116 *area_count(area_dst
, page_nr
) = count_verify
[page_nr
] = count
;
117 pthread_mutex_unlock(area_mutex(area_dst
, page_nr
));
123 static int copy_page_retry(int ufd
, unsigned long offset
)
125 return __copy_page(ufd
, offset
, true, test_uffdio_wp
);
128 pthread_mutex_t uffd_read_mutex
= PTHREAD_MUTEX_INITIALIZER
;
130 static void *uffd_read_thread(void *arg
)
132 struct uffd_args
*args
= (struct uffd_args
*)arg
;
135 pthread_mutex_unlock(&uffd_read_mutex
);
136 /* from here cancellation is ok */
139 if (uffd_read_msg(uffd
, &msg
))
141 uffd_handle_page_fault(&msg
, args
);
147 static void *background_thread(void *arg
)
149 unsigned long cpu
= (unsigned long) arg
;
150 unsigned long page_nr
, start_nr
, mid_nr
, end_nr
;
152 start_nr
= cpu
* nr_pages_per_cpu
;
153 end_nr
= (cpu
+1) * nr_pages_per_cpu
;
154 mid_nr
= (start_nr
+ end_nr
) / 2;
156 /* Copy the first half of the pages */
157 for (page_nr
= start_nr
; page_nr
< mid_nr
; page_nr
++)
158 copy_page_retry(uffd
, page_nr
* page_size
);
161 * If we need to test uffd-wp, set it up now. Then we'll have
162 * at least the first half of the pages mapped already which
163 * can be write-protected for testing
166 wp_range(uffd
, (unsigned long)area_dst
+ start_nr
* page_size
,
167 nr_pages_per_cpu
* page_size
, true);
170 * Continue the 2nd half of the page copying, handling write
171 * protection faults if any
173 for (page_nr
= mid_nr
; page_nr
< end_nr
; page_nr
++)
174 copy_page_retry(uffd
, page_nr
* page_size
);
179 static int stress(struct uffd_args
*args
)
182 pthread_t locking_threads
[nr_cpus
];
183 pthread_t uffd_threads
[nr_cpus
];
184 pthread_t background_threads
[nr_cpus
];
187 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
188 if (pthread_create(&locking_threads
[cpu
], &attr
,
189 locking_thread
, (void *)cpu
))
191 if (bounces
& BOUNCE_POLL
) {
192 if (pthread_create(&uffd_threads
[cpu
], &attr
, uffd_poll_thread
, &args
[cpu
]))
193 err("uffd_poll_thread create");
195 if (pthread_create(&uffd_threads
[cpu
], &attr
,
199 pthread_mutex_lock(&uffd_read_mutex
);
201 if (pthread_create(&background_threads
[cpu
], &attr
,
202 background_thread
, (void *)cpu
))
205 for (cpu
= 0; cpu
< nr_cpus
; cpu
++)
206 if (pthread_join(background_threads
[cpu
], NULL
))
210 * Be strict and immediately zap area_src, the whole area has
211 * been transferred already by the background treads. The
212 * area_src could then be faulted in a racy way by still
213 * running uffdio_threads reading zeropages after we zapped
214 * area_src (but they're guaranteed to get -EEXIST from
215 * UFFDIO_COPY without writing zero pages into area_dst
216 * because the background threads already completed).
218 uffd_test_ops
->release_pages(area_src
);
221 for (cpu
= 0; cpu
< nr_cpus
; cpu
++)
222 if (pthread_join(locking_threads
[cpu
], NULL
))
225 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
227 if (bounces
& BOUNCE_POLL
) {
228 if (write(pipefd
[cpu
*2+1], &c
, 1) != 1)
229 err("pipefd write error");
230 if (pthread_join(uffd_threads
[cpu
],
234 if (pthread_cancel(uffd_threads
[cpu
]))
236 if (pthread_join(uffd_threads
[cpu
], NULL
))
244 static int userfaultfd_stress(void)
248 struct uffd_args args
[nr_cpus
];
249 uint64_t mem_size
= nr_pages
* page_size
;
252 memset(args
, 0, sizeof(struct uffd_args
) * nr_cpus
);
254 if (features
& UFFD_FEATURE_WP_UNPOPULATED
&& test_type
== TEST_ANON
)
255 flags
= UFFD_FEATURE_WP_UNPOPULATED
;
257 if (uffd_test_ctx_init(flags
, NULL
))
258 err("context init failed");
260 if (posix_memalign(&area
, page_size
, page_size
))
261 err("out of memory");
263 bzero(zeropage
, page_size
);
265 pthread_mutex_lock(&uffd_read_mutex
);
267 pthread_attr_init(&attr
);
268 pthread_attr_setstacksize(&attr
, 16*1024*1024);
271 printf("bounces: %d, mode:", bounces
);
272 if (bounces
& BOUNCE_RANDOM
)
274 if (bounces
& BOUNCE_RACINGFAULTS
)
276 if (bounces
& BOUNCE_VERIFY
)
278 if (bounces
& BOUNCE_POLL
)
285 if (bounces
& BOUNCE_POLL
)
286 fcntl(uffd
, F_SETFL
, uffd_flags
| O_NONBLOCK
);
288 fcntl(uffd
, F_SETFL
, uffd_flags
& ~O_NONBLOCK
);
291 if (uffd_register(uffd
, area_dst
, mem_size
,
292 true, test_uffdio_wp
, false))
293 err("register failure");
295 if (area_dst_alias
) {
296 if (uffd_register(uffd
, area_dst_alias
, mem_size
,
297 true, test_uffdio_wp
, false))
298 err("register failure alias");
302 * The madvise done previously isn't enough: some
303 * uffd_thread could have read userfaults (one of
304 * those already resolved by the background thread)
305 * and it may be in the process of calling
306 * UFFDIO_COPY. UFFDIO_COPY will read the zapped
307 * area_src and it would map a zero page in it (of
308 * course such a UFFDIO_COPY is perfectly safe as it'd
309 * return -EEXIST). The problem comes at the next
310 * bounce though: that racing UFFDIO_COPY would
311 * generate zeropages in the area_src, so invalidating
312 * the previous MADV_DONTNEED. Without this additional
313 * MADV_DONTNEED those zeropages leftovers in the
314 * area_src would lead to -EEXIST failure during the
315 * next bounce, effectively leaving a zeropage in the
318 * Try to comment this out madvise to see the memory
319 * corruption being caught pretty quick.
321 * khugepaged is also inhibited to collapse THP after
322 * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's
323 * required to MADV_DONTNEED here.
325 uffd_test_ops
->release_pages(area_dst
);
327 uffd_stats_reset(args
, nr_cpus
);
331 uffd_test_ctx_clear();
335 /* Clear all the write protections if there is any */
337 wp_range(uffd
, (unsigned long)area_dst
,
338 nr_pages
* page_size
, false);
341 if (uffd_unregister(uffd
, area_dst
, mem_size
))
342 err("unregister failure");
343 if (area_dst_alias
) {
344 if (uffd_unregister(uffd
, area_dst_alias
, mem_size
))
345 err("unregister failure alias");
349 if (bounces
& BOUNCE_VERIFY
)
350 for (nr
= 0; nr
< nr_pages
; nr
++)
351 if (*area_count(area_dst
, nr
) != count_verify
[nr
])
352 err("error area_count %llu %llu %lu\n",
353 *area_count(area_src
, nr
),
354 count_verify
[nr
], nr
);
356 /* prepare next bounce */
357 swap(area_src
, area_dst
);
359 swap(area_src_alias
, area_dst_alias
);
361 uffd_stats_report(args
, nr_cpus
);
363 uffd_test_ctx_clear();
368 static void set_test_type(const char *type
)
370 if (!strcmp(type
, "anon")) {
371 test_type
= TEST_ANON
;
372 uffd_test_ops
= &anon_uffd_test_ops
;
373 } else if (!strcmp(type
, "hugetlb")) {
374 test_type
= TEST_HUGETLB
;
375 uffd_test_ops
= &hugetlb_uffd_test_ops
;
377 } else if (!strcmp(type
, "hugetlb-private")) {
378 test_type
= TEST_HUGETLB
;
379 uffd_test_ops
= &hugetlb_uffd_test_ops
;
380 } else if (!strcmp(type
, "shmem")) {
382 test_type
= TEST_SHMEM
;
383 uffd_test_ops
= &shmem_uffd_test_ops
;
384 } else if (!strcmp(type
, "shmem-private")) {
385 test_type
= TEST_SHMEM
;
386 uffd_test_ops
= &shmem_uffd_test_ops
;
390 static void parse_test_type_arg(const char *raw_type
)
392 set_test_type(raw_type
);
395 err("failed to parse test type argument: '%s'", raw_type
);
397 if (test_type
== TEST_HUGETLB
)
398 page_size
= default_huge_page_size();
400 page_size
= sysconf(_SC_PAGE_SIZE
);
403 err("Unable to determine page size");
404 if ((unsigned long) area_count(NULL
, 0) + sizeof(unsigned long long) * 2
406 err("Impossible to run this test");
409 * Whether we can test certain features depends not just on test type,
410 * but also on whether or not this particular kernel supports the
414 if (uffd_get_features(&features
))
415 err("failed to get available features");
417 test_uffdio_wp
= test_uffdio_wp
&&
418 (features
& UFFD_FEATURE_PAGEFAULT_FLAG_WP
);
420 if (test_type
!= TEST_ANON
&& !(features
& UFFD_FEATURE_WP_HUGETLBFS_SHMEM
))
421 test_uffdio_wp
= false;
427 static void sigalrm(int sig
)
431 test_uffdio_copy_eexist
= true;
432 alarm(ALARM_INTERVAL_SECS
);
435 int main(int argc
, char **argv
)
442 if (signal(SIGALRM
, sigalrm
) == SIG_ERR
)
443 err("failed to arm SIGALRM");
444 alarm(ALARM_INTERVAL_SECS
);
446 parse_test_type_arg(argv
[1]);
447 bytes
= atol(argv
[2]) * 1024 * 1024;
449 if (test_type
== TEST_HUGETLB
&&
450 get_free_hugepages() < bytes
/ page_size
) {
451 printf("skip: Skipping userfaultfd... not enough hugepages\n");
455 nr_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
457 nr_pages_per_cpu
= bytes
/ page_size
/ nr_cpus
;
458 if (!nr_pages_per_cpu
) {
463 bounces
= atoi(argv
[3]);
465 _err("invalid bounces");
468 nr_pages
= nr_pages_per_cpu
* nr_cpus
;
470 printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
471 nr_pages
, nr_pages_per_cpu
);
472 return userfaultfd_stress();