uffd-stress.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Stress userfaultfd syscall.
  4. *
  5. * Copyright (C) 2015 Red Hat, Inc.
  6. *
  7. * This test allocates two virtual areas and bounces the physical
  8. * memory across the two virtual areas (from area_src to area_dst)
  9. * using userfaultfd.
  10. *
  11. * There are three threads running per CPU:
  12. *
  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.
  17. *
  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.
  21. *
  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
  25. * area.
  26. *
  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
  30. * opposite direction.
  31. *
  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).
  35. */
  36. #include "uffd-common.h"
  37. uint64_t features;
  38. #ifdef __NR_userfaultfd
  39. #define BOUNCE_RANDOM (1<<0)
  40. #define BOUNCE_RACINGFAULTS (1<<1)
  41. #define BOUNCE_VERIFY (1<<2)
  42. #define BOUNCE_POLL (1<<3)
  43. static int bounces;
  44. /* defined globally for this particular test as the sigalrm handler
  45. * depends on test_uffdio_*_eexist.
  46. * XXX: define gopts in main() when we figure out a way to deal with
  47. * test_uffdio_*_eexist.
  48. */
  49. static uffd_global_test_opts_t *gopts;
  50. /* exercise the test_uffdio_*_eexist every ALARM_INTERVAL_SECS */
  51. #define ALARM_INTERVAL_SECS 10
  52. static char *zeropage;
  53. pthread_attr_t attr;
  54. #define swap(a, b) \
  55. do { __auto_type __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
  56. const char *examples =
  57. "# Run anonymous memory test on 100MiB region with 99999 bounces:\n"
  58. "./uffd-stress anon 100 99999\n\n"
  59. "# Run share memory test on 1GiB region with 99 bounces:\n"
  60. "./uffd-stress shmem 1000 99\n\n"
  61. "# Run hugetlb memory test on 256MiB region with 50 bounces:\n"
  62. "./uffd-stress hugetlb 256 50\n\n"
  63. "# Run the same hugetlb test but using private file:\n"
  64. "./uffd-stress hugetlb-private 256 50\n\n"
  65. "# 10MiB-~6GiB 999 bounces anonymous test, "
  66. "continue forever unless an error triggers\n"
  67. "while ./uffd-stress anon $[RANDOM % 6000 + 10] 999; do true; done\n\n";
  68. static void usage(void)
  69. {
  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);
  75. exit(1);
  76. }
  77. static void uffd_stats_reset(uffd_global_test_opts_t *gopts, struct uffd_args *args,
  78. unsigned long n_cpus)
  79. {
  80. int i;
  81. for (i = 0; i < n_cpus; i++) {
  82. args[i].cpu = i;
  83. args[i].apply_wp = gopts->test_uffdio_wp;
  84. args[i].missing_faults = 0;
  85. args[i].wp_faults = 0;
  86. args[i].minor_faults = 0;
  87. args[i].gopts = gopts;
  88. }
  89. }
  90. static void *locking_thread(void *arg)
  91. {
  92. struct uffd_args *args = (struct uffd_args *) arg;
  93. uffd_global_test_opts_t *gopts = args->gopts;
  94. unsigned long cpu = (unsigned long) args->cpu;
  95. unsigned long page_nr;
  96. unsigned long long count;
  97. if (!(bounces & BOUNCE_RANDOM)) {
  98. page_nr = -bounces;
  99. if (!(bounces & BOUNCE_RACINGFAULTS))
  100. page_nr += cpu * gopts->nr_pages_per_cpu;
  101. }
  102. while (!gopts->finished) {
  103. if (bounces & BOUNCE_RANDOM) {
  104. if (getrandom(&page_nr, sizeof(page_nr), 0) != sizeof(page_nr))
  105. err("getrandom failed");
  106. } else
  107. page_nr += 1;
  108. page_nr %= gopts->nr_pages;
  109. pthread_mutex_lock(area_mutex(gopts->area_dst, page_nr, gopts));
  110. count = *area_count(gopts->area_dst, page_nr, gopts);
  111. if (count != gopts->count_verify[page_nr])
  112. err("page_nr %lu memory corruption %llu %llu",
  113. page_nr, count, gopts->count_verify[page_nr]);
  114. count++;
  115. *area_count(gopts->area_dst, page_nr, gopts) = gopts->count_verify[page_nr] = count;
  116. pthread_mutex_unlock(area_mutex(gopts->area_dst, page_nr, gopts));
  117. }
  118. return NULL;
  119. }
  120. static int copy_page_retry(uffd_global_test_opts_t *gopts, unsigned long offset)
  121. {
  122. return __copy_page(gopts, offset, true, gopts->test_uffdio_wp);
  123. }
  124. pthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER;
  125. static void *uffd_read_thread(void *arg)
  126. {
  127. struct uffd_args *args = (struct uffd_args *)arg;
  128. uffd_global_test_opts_t *gopts = args->gopts;
  129. struct uffd_msg msg;
  130. pthread_mutex_unlock(&uffd_read_mutex);
  131. /* from here cancellation is ok */
  132. for (;;) {
  133. if (uffd_read_msg(gopts, &msg))
  134. continue;
  135. uffd_handle_page_fault(gopts, &msg, args);
  136. }
  137. return NULL;
  138. }
  139. static void *background_thread(void *arg)
  140. {
  141. struct uffd_args *args = (struct uffd_args *) arg;
  142. uffd_global_test_opts_t *gopts = args->gopts;
  143. unsigned long cpu = (unsigned long) args->cpu;
  144. unsigned long page_nr, start_nr, mid_nr, end_nr;
  145. start_nr = cpu * gopts->nr_pages_per_cpu;
  146. end_nr = (cpu+1) * gopts->nr_pages_per_cpu;
  147. mid_nr = (start_nr + end_nr) / 2;
  148. /* Copy the first half of the pages */
  149. for (page_nr = start_nr; page_nr < mid_nr; page_nr++)
  150. copy_page_retry(gopts, page_nr * gopts->page_size);
  151. /*
  152. * If we need to test uffd-wp, set it up now. Then we'll have
  153. * at least the first half of the pages mapped already which
  154. * can be write-protected for testing
  155. */
  156. if (gopts->test_uffdio_wp)
  157. wp_range(gopts->uffd, (unsigned long)gopts->area_dst + start_nr * gopts->page_size,
  158. gopts->nr_pages_per_cpu * gopts->page_size, true);
  159. /*
  160. * Continue the 2nd half of the page copying, handling write
  161. * protection faults if any
  162. */
  163. for (page_nr = mid_nr; page_nr < end_nr; page_nr++)
  164. copy_page_retry(gopts, page_nr * gopts->page_size);
  165. return NULL;
  166. }
  167. static int stress(struct uffd_args *args)
  168. {
  169. unsigned long cpu;
  170. uffd_global_test_opts_t *gopts = args->gopts;
  171. pthread_t locking_threads[gopts->nr_parallel];
  172. pthread_t uffd_threads[gopts->nr_parallel];
  173. pthread_t background_threads[gopts->nr_parallel];
  174. gopts->finished = 0;
  175. for (cpu = 0; cpu < gopts->nr_parallel; cpu++) {
  176. if (pthread_create(&locking_threads[cpu], &attr,
  177. locking_thread, (void *)&args[cpu]))
  178. return 1;
  179. if (bounces & BOUNCE_POLL) {
  180. if (pthread_create(&uffd_threads[cpu],
  181. &attr,
  182. uffd_poll_thread,
  183. (void *) &args[cpu]))
  184. err("uffd_poll_thread create");
  185. } else {
  186. if (pthread_create(&uffd_threads[cpu], &attr,
  187. uffd_read_thread,
  188. (void *)&args[cpu]))
  189. return 1;
  190. pthread_mutex_lock(&uffd_read_mutex);
  191. }
  192. if (pthread_create(&background_threads[cpu], &attr,
  193. background_thread, (void *)&args[cpu]))
  194. return 1;
  195. }
  196. for (cpu = 0; cpu < gopts->nr_parallel; cpu++)
  197. if (pthread_join(background_threads[cpu], NULL))
  198. return 1;
  199. /*
  200. * Be strict and immediately zap area_src, the whole area has
  201. * been transferred already by the background treads. The
  202. * area_src could then be faulted in a racy way by still
  203. * running uffdio_threads reading zeropages after we zapped
  204. * area_src (but they're guaranteed to get -EEXIST from
  205. * UFFDIO_COPY without writing zero pages into area_dst
  206. * because the background threads already completed).
  207. */
  208. uffd_test_ops->release_pages(gopts, gopts->area_src);
  209. gopts->finished = 1;
  210. for (cpu = 0; cpu < gopts->nr_parallel; cpu++)
  211. if (pthread_join(locking_threads[cpu], NULL))
  212. return 1;
  213. for (cpu = 0; cpu < gopts->nr_parallel; cpu++) {
  214. char c = '\0';
  215. if (bounces & BOUNCE_POLL) {
  216. if (write(gopts->pipefd[cpu*2+1], &c, 1) != 1)
  217. err("pipefd write error");
  218. if (pthread_join(uffd_threads[cpu],
  219. (void *)&args[cpu]))
  220. return 1;
  221. } else {
  222. if (pthread_cancel(uffd_threads[cpu]))
  223. return 1;
  224. if (pthread_join(uffd_threads[cpu], NULL))
  225. return 1;
  226. }
  227. }
  228. return 0;
  229. }
  230. static int userfaultfd_stress(uffd_global_test_opts_t *gopts)
  231. {
  232. void *area;
  233. unsigned long nr;
  234. struct uffd_args args[gopts->nr_parallel];
  235. uint64_t mem_size = gopts->nr_pages * gopts->page_size;
  236. int flags = 0;
  237. memset(args, 0, sizeof(struct uffd_args) * gopts->nr_parallel);
  238. if (features & UFFD_FEATURE_WP_UNPOPULATED && gopts->test_type == TEST_ANON)
  239. flags = UFFD_FEATURE_WP_UNPOPULATED;
  240. if (uffd_test_ctx_init(gopts, flags, NULL))
  241. err("context init failed");
  242. if (posix_memalign(&area, gopts->page_size, gopts->page_size))
  243. err("out of memory");
  244. zeropage = area;
  245. bzero(zeropage, gopts->page_size);
  246. pthread_mutex_lock(&uffd_read_mutex);
  247. pthread_attr_init(&attr);
  248. pthread_attr_setstacksize(&attr, 16*1024*1024);
  249. while (bounces--) {
  250. printf("bounces: %d, mode:", bounces);
  251. if (bounces & BOUNCE_RANDOM)
  252. printf(" rnd");
  253. if (bounces & BOUNCE_RACINGFAULTS)
  254. printf(" racing");
  255. if (bounces & BOUNCE_VERIFY)
  256. printf(" ver");
  257. if (bounces & BOUNCE_POLL)
  258. printf(" poll");
  259. else
  260. printf(" read");
  261. printf(", ");
  262. fflush(stdout);
  263. if (bounces & BOUNCE_POLL)
  264. fcntl(gopts->uffd, F_SETFL, gopts->uffd_flags | O_NONBLOCK);
  265. else
  266. fcntl(gopts->uffd, F_SETFL, gopts->uffd_flags & ~O_NONBLOCK);
  267. /* register */
  268. if (uffd_register(gopts->uffd, gopts->area_dst, mem_size,
  269. true, gopts->test_uffdio_wp, false))
  270. err("register failure");
  271. if (gopts->area_dst_alias) {
  272. if (uffd_register(gopts->uffd, gopts->area_dst_alias, mem_size,
  273. true, gopts->test_uffdio_wp, false))
  274. err("register failure alias");
  275. }
  276. /*
  277. * The madvise done previously isn't enough: some
  278. * uffd_thread could have read userfaults (one of
  279. * those already resolved by the background thread)
  280. * and it may be in the process of calling
  281. * UFFDIO_COPY. UFFDIO_COPY will read the zapped
  282. * area_src and it would map a zero page in it (of
  283. * course such a UFFDIO_COPY is perfectly safe as it'd
  284. * return -EEXIST). The problem comes at the next
  285. * bounce though: that racing UFFDIO_COPY would
  286. * generate zeropages in the area_src, so invalidating
  287. * the previous MADV_DONTNEED. Without this additional
  288. * MADV_DONTNEED those zeropages leftovers in the
  289. * area_src would lead to -EEXIST failure during the
  290. * next bounce, effectively leaving a zeropage in the
  291. * area_dst.
  292. *
  293. * Try to comment this out madvise to see the memory
  294. * corruption being caught pretty quick.
  295. *
  296. * khugepaged is also inhibited to collapse THP after
  297. * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's
  298. * required to MADV_DONTNEED here.
  299. */
  300. uffd_test_ops->release_pages(gopts, gopts->area_dst);
  301. uffd_stats_reset(gopts, args, gopts->nr_parallel);
  302. /* bounce pass */
  303. if (stress(args)) {
  304. uffd_test_ctx_clear(gopts);
  305. return 1;
  306. }
  307. /* Clear all the write protections if there is any */
  308. if (gopts->test_uffdio_wp)
  309. wp_range(gopts->uffd, (unsigned long)gopts->area_dst,
  310. gopts->nr_pages * gopts->page_size, false);
  311. /* unregister */
  312. if (uffd_unregister(gopts->uffd, gopts->area_dst, mem_size))
  313. err("unregister failure");
  314. if (gopts->area_dst_alias) {
  315. if (uffd_unregister(gopts->uffd, gopts->area_dst_alias, mem_size))
  316. err("unregister failure alias");
  317. }
  318. /* verification */
  319. if (bounces & BOUNCE_VERIFY)
  320. for (nr = 0; nr < gopts->nr_pages; nr++)
  321. if (*area_count(gopts->area_dst, nr, gopts) !=
  322. gopts->count_verify[nr])
  323. err("error area_count %llu %llu %lu\n",
  324. *area_count(gopts->area_src, nr, gopts),
  325. gopts->count_verify[nr], nr);
  326. /* prepare next bounce */
  327. swap(gopts->area_src, gopts->area_dst);
  328. swap(gopts->area_src_alias, gopts->area_dst_alias);
  329. uffd_stats_report(args, gopts->nr_parallel);
  330. }
  331. uffd_test_ctx_clear(gopts);
  332. return 0;
  333. }
  334. static void set_test_type(uffd_global_test_opts_t *gopts, const char *type)
  335. {
  336. if (!strcmp(type, "anon")) {
  337. gopts->test_type = TEST_ANON;
  338. uffd_test_ops = &anon_uffd_test_ops;
  339. } else if (!strcmp(type, "hugetlb")) {
  340. gopts->test_type = TEST_HUGETLB;
  341. uffd_test_ops = &hugetlb_uffd_test_ops;
  342. gopts->map_shared = true;
  343. } else if (!strcmp(type, "hugetlb-private")) {
  344. gopts->test_type = TEST_HUGETLB;
  345. uffd_test_ops = &hugetlb_uffd_test_ops;
  346. } else if (!strcmp(type, "shmem")) {
  347. gopts->map_shared = true;
  348. gopts->test_type = TEST_SHMEM;
  349. uffd_test_ops = &shmem_uffd_test_ops;
  350. } else if (!strcmp(type, "shmem-private")) {
  351. gopts->test_type = TEST_SHMEM;
  352. uffd_test_ops = &shmem_uffd_test_ops;
  353. }
  354. }
  355. static void parse_test_type_arg(uffd_global_test_opts_t *gopts, const char *raw_type)
  356. {
  357. set_test_type(gopts, raw_type);
  358. if (!gopts->test_type)
  359. err("failed to parse test type argument: '%s'", raw_type);
  360. if (gopts->test_type == TEST_HUGETLB)
  361. gopts->page_size = default_huge_page_size();
  362. else
  363. gopts->page_size = sysconf(_SC_PAGE_SIZE);
  364. if (!gopts->page_size)
  365. err("Unable to determine page size");
  366. if ((unsigned long) area_count(NULL, 0, gopts) + sizeof(unsigned long long) * 2
  367. > gopts->page_size)
  368. err("Impossible to run this test");
  369. /*
  370. * Whether we can test certain features depends not just on test type,
  371. * but also on whether or not this particular kernel supports the
  372. * feature.
  373. */
  374. if (uffd_get_features(&features) && errno == ENOENT)
  375. ksft_exit_skip("failed to get available features (%d)\n", errno);
  376. gopts->test_uffdio_wp = gopts->test_uffdio_wp &&
  377. (features & UFFD_FEATURE_PAGEFAULT_FLAG_WP);
  378. if (gopts->test_type != TEST_ANON && !(features & UFFD_FEATURE_WP_HUGETLBFS_SHMEM))
  379. gopts->test_uffdio_wp = false;
  380. close(gopts->uffd);
  381. gopts->uffd = -1;
  382. }
  383. static void sigalrm(int sig)
  384. {
  385. if (sig != SIGALRM)
  386. abort();
  387. gopts->test_uffdio_copy_eexist = true;
  388. alarm(ALARM_INTERVAL_SECS);
  389. }
  390. int main(int argc, char **argv)
  391. {
  392. unsigned long nr_cpus;
  393. size_t bytes;
  394. gopts = (uffd_global_test_opts_t *) malloc(sizeof(uffd_global_test_opts_t));
  395. if (argc < 4)
  396. usage();
  397. if (signal(SIGALRM, sigalrm) == SIG_ERR)
  398. err("failed to arm SIGALRM");
  399. alarm(ALARM_INTERVAL_SECS);
  400. parse_test_type_arg(gopts, argv[1]);
  401. bytes = atol(argv[2]) * 1024 * 1024;
  402. nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  403. if (nr_cpus > 32) {
  404. /* Don't let calculation below go to zero. */
  405. ksft_print_msg("_SC_NPROCESSORS_ONLN (%lu) too large, capping nr_threads to 32\n",
  406. nr_cpus);
  407. gopts->nr_parallel = 32;
  408. } else {
  409. gopts->nr_parallel = nr_cpus;
  410. }
  411. /*
  412. * src and dst each require bytes / page_size number of hugepages.
  413. * Ensure nr_parallel - 1 hugepages on top of that to account
  414. * for racy extra reservation of hugepages.
  415. */
  416. if (gopts->test_type == TEST_HUGETLB &&
  417. get_free_hugepages() < 2 * (bytes / gopts->page_size) + gopts->nr_parallel - 1) {
  418. printf("skip: Skipping userfaultfd... not enough hugepages\n");
  419. return KSFT_SKIP;
  420. }
  421. gopts->nr_pages_per_cpu = bytes / gopts->page_size / gopts->nr_parallel;
  422. if (!gopts->nr_pages_per_cpu) {
  423. _err("pages_per_cpu = 0, cannot test (%lu / %lu / %lu)",
  424. bytes, gopts->page_size, gopts->nr_parallel);
  425. usage();
  426. }
  427. bounces = atoi(argv[3]);
  428. if (bounces <= 0) {
  429. _err("invalid bounces");
  430. usage();
  431. }
  432. gopts->nr_pages = gopts->nr_pages_per_cpu * gopts->nr_parallel;
  433. printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
  434. gopts->nr_pages, gopts->nr_pages_per_cpu);
  435. return userfaultfd_stress(gopts);
  436. }
  437. #else /* __NR_userfaultfd */
  438. #warning "missing __NR_userfaultfd definition"
  439. int main(void)
  440. {
  441. printf("skip: Skipping userfaultfd test (missing __NR_userfaultfd)\n");
  442. return KSFT_SKIP;
  443. }
  444. #endif /* __NR_userfaultfd */