userfaultfd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  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. #define _GNU_SOURCE
  51. #include <stdio.h>
  52. #include <errno.h>
  53. #include <unistd.h>
  54. #include <stdlib.h>
  55. #include <sys/types.h>
  56. #include <sys/stat.h>
  57. #include <fcntl.h>
  58. #include <time.h>
  59. #include <signal.h>
  60. #include <poll.h>
  61. #include <string.h>
  62. #include <sys/mman.h>
  63. #include <sys/syscall.h>
  64. #include <sys/ioctl.h>
  65. #include <pthread.h>
  66. #include <linux/userfaultfd.h>
  67. #ifdef __NR_userfaultfd
  68. static unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;
  69. #define BOUNCE_RANDOM (1<<0)
  70. #define BOUNCE_RACINGFAULTS (1<<1)
  71. #define BOUNCE_VERIFY (1<<2)
  72. #define BOUNCE_POLL (1<<3)
  73. static int bounces;
  74. static unsigned long long *count_verify;
  75. static int uffd, finished, *pipefd;
  76. static char *area_src, *area_dst;
  77. static char *zeropage;
  78. pthread_attr_t attr;
  79. /* pthread_mutex_t starts at page offset 0 */
  80. #define area_mutex(___area, ___nr) \
  81. ((pthread_mutex_t *) ((___area) + (___nr)*page_size))
  82. /*
  83. * count is placed in the page after pthread_mutex_t naturally aligned
  84. * to avoid non alignment faults on non-x86 archs.
  85. */
  86. #define area_count(___area, ___nr) \
  87. ((volatile unsigned long long *) ((unsigned long) \
  88. ((___area) + (___nr)*page_size + \
  89. sizeof(pthread_mutex_t) + \
  90. sizeof(unsigned long long) - 1) & \
  91. ~(unsigned long)(sizeof(unsigned long long) \
  92. - 1)))
  93. static int my_bcmp(char *str1, char *str2, size_t n)
  94. {
  95. unsigned long i;
  96. for (i = 0; i < n; i++)
  97. if (str1[i] != str2[i])
  98. return 1;
  99. return 0;
  100. }
  101. static void *locking_thread(void *arg)
  102. {
  103. unsigned long cpu = (unsigned long) arg;
  104. struct random_data rand;
  105. unsigned long page_nr = *(&(page_nr)); /* uninitialized warning */
  106. int32_t rand_nr;
  107. unsigned long long count;
  108. char randstate[64];
  109. unsigned int seed;
  110. time_t start;
  111. if (bounces & BOUNCE_RANDOM) {
  112. seed = (unsigned int) time(NULL) - bounces;
  113. if (!(bounces & BOUNCE_RACINGFAULTS))
  114. seed += cpu;
  115. bzero(&rand, sizeof(rand));
  116. bzero(&randstate, sizeof(randstate));
  117. if (initstate_r(seed, randstate, sizeof(randstate), &rand))
  118. fprintf(stderr, "srandom_r error\n"), exit(1);
  119. } else {
  120. page_nr = -bounces;
  121. if (!(bounces & BOUNCE_RACINGFAULTS))
  122. page_nr += cpu * nr_pages_per_cpu;
  123. }
  124. while (!finished) {
  125. if (bounces & BOUNCE_RANDOM) {
  126. if (random_r(&rand, &rand_nr))
  127. fprintf(stderr, "random_r 1 error\n"), exit(1);
  128. page_nr = rand_nr;
  129. if (sizeof(page_nr) > sizeof(rand_nr)) {
  130. if (random_r(&rand, &rand_nr))
  131. fprintf(stderr, "random_r 2 error\n"), exit(1);
  132. page_nr |= (((unsigned long) rand_nr) << 16) <<
  133. 16;
  134. }
  135. } else
  136. page_nr += 1;
  137. page_nr %= nr_pages;
  138. start = time(NULL);
  139. if (bounces & BOUNCE_VERIFY) {
  140. count = *area_count(area_dst, page_nr);
  141. if (!count)
  142. fprintf(stderr,
  143. "page_nr %lu wrong count %Lu %Lu\n",
  144. page_nr, count,
  145. count_verify[page_nr]), exit(1);
  146. /*
  147. * We can't use bcmp (or memcmp) because that
  148. * returns 0 erroneously if the memory is
  149. * changing under it (even if the end of the
  150. * page is never changing and always
  151. * different).
  152. */
  153. #if 1
  154. if (!my_bcmp(area_dst + page_nr * page_size, zeropage,
  155. page_size))
  156. fprintf(stderr,
  157. "my_bcmp page_nr %lu wrong count %Lu %Lu\n",
  158. page_nr, count,
  159. count_verify[page_nr]), exit(1);
  160. #else
  161. unsigned long loops;
  162. loops = 0;
  163. /* uncomment the below line to test with mutex */
  164. /* pthread_mutex_lock(area_mutex(area_dst, page_nr)); */
  165. while (!bcmp(area_dst + page_nr * page_size, zeropage,
  166. page_size)) {
  167. loops += 1;
  168. if (loops > 10)
  169. break;
  170. }
  171. /* uncomment below line to test with mutex */
  172. /* pthread_mutex_unlock(area_mutex(area_dst, page_nr)); */
  173. if (loops) {
  174. fprintf(stderr,
  175. "page_nr %lu all zero thread %lu %p %lu\n",
  176. page_nr, cpu, area_dst + page_nr * page_size,
  177. loops);
  178. if (loops > 10)
  179. exit(1);
  180. }
  181. #endif
  182. }
  183. pthread_mutex_lock(area_mutex(area_dst, page_nr));
  184. count = *area_count(area_dst, page_nr);
  185. if (count != count_verify[page_nr]) {
  186. fprintf(stderr,
  187. "page_nr %lu memory corruption %Lu %Lu\n",
  188. page_nr, count,
  189. count_verify[page_nr]), exit(1);
  190. }
  191. count++;
  192. *area_count(area_dst, page_nr) = count_verify[page_nr] = count;
  193. pthread_mutex_unlock(area_mutex(area_dst, page_nr));
  194. if (time(NULL) - start > 1)
  195. fprintf(stderr,
  196. "userfault too slow %ld "
  197. "possible false positive with overcommit\n",
  198. time(NULL) - start);
  199. }
  200. return NULL;
  201. }
  202. static int copy_page(unsigned long offset)
  203. {
  204. struct uffdio_copy uffdio_copy;
  205. if (offset >= nr_pages * page_size)
  206. fprintf(stderr, "unexpected offset %lu\n",
  207. offset), exit(1);
  208. uffdio_copy.dst = (unsigned long) area_dst + offset;
  209. uffdio_copy.src = (unsigned long) area_src + offset;
  210. uffdio_copy.len = page_size;
  211. uffdio_copy.mode = 0;
  212. uffdio_copy.copy = 0;
  213. if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy)) {
  214. /* real retval in ufdio_copy.copy */
  215. if (uffdio_copy.copy != -EEXIST)
  216. fprintf(stderr, "UFFDIO_COPY error %Ld\n",
  217. uffdio_copy.copy), exit(1);
  218. } else if (uffdio_copy.copy != page_size) {
  219. fprintf(stderr, "UFFDIO_COPY unexpected copy %Ld\n",
  220. uffdio_copy.copy), exit(1);
  221. } else
  222. return 1;
  223. return 0;
  224. }
  225. static void *uffd_poll_thread(void *arg)
  226. {
  227. unsigned long cpu = (unsigned long) arg;
  228. struct pollfd pollfd[2];
  229. struct uffd_msg msg;
  230. int ret;
  231. unsigned long offset;
  232. char tmp_chr;
  233. unsigned long userfaults = 0;
  234. pollfd[0].fd = uffd;
  235. pollfd[0].events = POLLIN;
  236. pollfd[1].fd = pipefd[cpu*2];
  237. pollfd[1].events = POLLIN;
  238. for (;;) {
  239. ret = poll(pollfd, 2, -1);
  240. if (!ret)
  241. fprintf(stderr, "poll error %d\n", ret), exit(1);
  242. if (ret < 0)
  243. perror("poll"), exit(1);
  244. if (pollfd[1].revents & POLLIN) {
  245. if (read(pollfd[1].fd, &tmp_chr, 1) != 1)
  246. fprintf(stderr, "read pipefd error\n"),
  247. exit(1);
  248. break;
  249. }
  250. if (!(pollfd[0].revents & POLLIN))
  251. fprintf(stderr, "pollfd[0].revents %d\n",
  252. pollfd[0].revents), exit(1);
  253. ret = read(uffd, &msg, sizeof(msg));
  254. if (ret < 0) {
  255. if (errno == EAGAIN)
  256. continue;
  257. perror("nonblocking read error"), exit(1);
  258. }
  259. if (msg.event != UFFD_EVENT_PAGEFAULT)
  260. fprintf(stderr, "unexpected msg event %u\n",
  261. msg.event), exit(1);
  262. if (msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
  263. fprintf(stderr, "unexpected write fault\n"), exit(1);
  264. offset = (char *)(unsigned long)msg.arg.pagefault.address -
  265. area_dst;
  266. offset &= ~(page_size-1);
  267. if (copy_page(offset))
  268. userfaults++;
  269. }
  270. return (void *)userfaults;
  271. }
  272. pthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER;
  273. static void *uffd_read_thread(void *arg)
  274. {
  275. unsigned long *this_cpu_userfaults;
  276. struct uffd_msg msg;
  277. unsigned long offset;
  278. int ret;
  279. this_cpu_userfaults = (unsigned long *) arg;
  280. *this_cpu_userfaults = 0;
  281. pthread_mutex_unlock(&uffd_read_mutex);
  282. /* from here cancellation is ok */
  283. for (;;) {
  284. ret = read(uffd, &msg, sizeof(msg));
  285. if (ret != sizeof(msg)) {
  286. if (ret < 0)
  287. perror("blocking read error"), exit(1);
  288. else
  289. fprintf(stderr, "short read\n"), exit(1);
  290. }
  291. if (msg.event != UFFD_EVENT_PAGEFAULT)
  292. fprintf(stderr, "unexpected msg event %u\n",
  293. msg.event), exit(1);
  294. if (bounces & BOUNCE_VERIFY &&
  295. msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
  296. fprintf(stderr, "unexpected write fault\n"), exit(1);
  297. offset = (char *)(unsigned long)msg.arg.pagefault.address -
  298. area_dst;
  299. offset &= ~(page_size-1);
  300. if (copy_page(offset))
  301. (*this_cpu_userfaults)++;
  302. }
  303. return (void *)NULL;
  304. }
  305. static void *background_thread(void *arg)
  306. {
  307. unsigned long cpu = (unsigned long) arg;
  308. unsigned long page_nr;
  309. for (page_nr = cpu * nr_pages_per_cpu;
  310. page_nr < (cpu+1) * nr_pages_per_cpu;
  311. page_nr++)
  312. copy_page(page_nr * page_size);
  313. return NULL;
  314. }
  315. static int stress(unsigned long *userfaults)
  316. {
  317. unsigned long cpu;
  318. pthread_t locking_threads[nr_cpus];
  319. pthread_t uffd_threads[nr_cpus];
  320. pthread_t background_threads[nr_cpus];
  321. void **_userfaults = (void **) userfaults;
  322. finished = 0;
  323. for (cpu = 0; cpu < nr_cpus; cpu++) {
  324. if (pthread_create(&locking_threads[cpu], &attr,
  325. locking_thread, (void *)cpu))
  326. return 1;
  327. if (bounces & BOUNCE_POLL) {
  328. if (pthread_create(&uffd_threads[cpu], &attr,
  329. uffd_poll_thread, (void *)cpu))
  330. return 1;
  331. } else {
  332. if (pthread_create(&uffd_threads[cpu], &attr,
  333. uffd_read_thread,
  334. &_userfaults[cpu]))
  335. return 1;
  336. pthread_mutex_lock(&uffd_read_mutex);
  337. }
  338. if (pthread_create(&background_threads[cpu], &attr,
  339. background_thread, (void *)cpu))
  340. return 1;
  341. }
  342. for (cpu = 0; cpu < nr_cpus; cpu++)
  343. if (pthread_join(background_threads[cpu], NULL))
  344. return 1;
  345. /*
  346. * Be strict and immediately zap area_src, the whole area has
  347. * been transferred already by the background treads. The
  348. * area_src could then be faulted in in a racy way by still
  349. * running uffdio_threads reading zeropages after we zapped
  350. * area_src (but they're guaranteed to get -EEXIST from
  351. * UFFDIO_COPY without writing zero pages into area_dst
  352. * because the background threads already completed).
  353. */
  354. if (madvise(area_src, nr_pages * page_size, MADV_DONTNEED)) {
  355. perror("madvise");
  356. return 1;
  357. }
  358. for (cpu = 0; cpu < nr_cpus; cpu++) {
  359. char c;
  360. if (bounces & BOUNCE_POLL) {
  361. if (write(pipefd[cpu*2+1], &c, 1) != 1) {
  362. fprintf(stderr, "pipefd write error\n");
  363. return 1;
  364. }
  365. if (pthread_join(uffd_threads[cpu], &_userfaults[cpu]))
  366. return 1;
  367. } else {
  368. if (pthread_cancel(uffd_threads[cpu]))
  369. return 1;
  370. if (pthread_join(uffd_threads[cpu], NULL))
  371. return 1;
  372. }
  373. }
  374. finished = 1;
  375. for (cpu = 0; cpu < nr_cpus; cpu++)
  376. if (pthread_join(locking_threads[cpu], NULL))
  377. return 1;
  378. return 0;
  379. }
  380. static int userfaultfd_stress(void)
  381. {
  382. void *area;
  383. char *tmp_area;
  384. unsigned long nr;
  385. struct uffdio_register uffdio_register;
  386. struct uffdio_api uffdio_api;
  387. unsigned long cpu;
  388. int uffd_flags, err;
  389. unsigned long userfaults[nr_cpus];
  390. if (posix_memalign(&area, page_size, nr_pages * page_size)) {
  391. fprintf(stderr, "out of memory\n");
  392. return 1;
  393. }
  394. area_src = area;
  395. if (posix_memalign(&area, page_size, nr_pages * page_size)) {
  396. fprintf(stderr, "out of memory\n");
  397. return 1;
  398. }
  399. area_dst = area;
  400. uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
  401. if (uffd < 0) {
  402. fprintf(stderr,
  403. "userfaultfd syscall not available in this kernel\n");
  404. return 1;
  405. }
  406. uffd_flags = fcntl(uffd, F_GETFD, NULL);
  407. uffdio_api.api = UFFD_API;
  408. uffdio_api.features = 0;
  409. if (ioctl(uffd, UFFDIO_API, &uffdio_api)) {
  410. fprintf(stderr, "UFFDIO_API\n");
  411. return 1;
  412. }
  413. if (uffdio_api.api != UFFD_API) {
  414. fprintf(stderr, "UFFDIO_API error %Lu\n", uffdio_api.api);
  415. return 1;
  416. }
  417. count_verify = malloc(nr_pages * sizeof(unsigned long long));
  418. if (!count_verify) {
  419. perror("count_verify");
  420. return 1;
  421. }
  422. for (nr = 0; nr < nr_pages; nr++) {
  423. *area_mutex(area_src, nr) = (pthread_mutex_t)
  424. PTHREAD_MUTEX_INITIALIZER;
  425. count_verify[nr] = *area_count(area_src, nr) = 1;
  426. /*
  427. * In the transition between 255 to 256, powerpc will
  428. * read out of order in my_bcmp and see both bytes as
  429. * zero, so leave a placeholder below always non-zero
  430. * after the count, to avoid my_bcmp to trigger false
  431. * positives.
  432. */
  433. *(area_count(area_src, nr) + 1) = 1;
  434. }
  435. pipefd = malloc(sizeof(int) * nr_cpus * 2);
  436. if (!pipefd) {
  437. perror("pipefd");
  438. return 1;
  439. }
  440. for (cpu = 0; cpu < nr_cpus; cpu++) {
  441. if (pipe2(&pipefd[cpu*2], O_CLOEXEC | O_NONBLOCK)) {
  442. perror("pipe");
  443. return 1;
  444. }
  445. }
  446. if (posix_memalign(&area, page_size, page_size)) {
  447. fprintf(stderr, "out of memory\n");
  448. return 1;
  449. }
  450. zeropage = area;
  451. bzero(zeropage, page_size);
  452. pthread_mutex_lock(&uffd_read_mutex);
  453. pthread_attr_init(&attr);
  454. pthread_attr_setstacksize(&attr, 16*1024*1024);
  455. err = 0;
  456. while (bounces--) {
  457. unsigned long expected_ioctls;
  458. printf("bounces: %d, mode:", bounces);
  459. if (bounces & BOUNCE_RANDOM)
  460. printf(" rnd");
  461. if (bounces & BOUNCE_RACINGFAULTS)
  462. printf(" racing");
  463. if (bounces & BOUNCE_VERIFY)
  464. printf(" ver");
  465. if (bounces & BOUNCE_POLL)
  466. printf(" poll");
  467. printf(", ");
  468. fflush(stdout);
  469. if (bounces & BOUNCE_POLL)
  470. fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
  471. else
  472. fcntl(uffd, F_SETFL, uffd_flags & ~O_NONBLOCK);
  473. /* register */
  474. uffdio_register.range.start = (unsigned long) area_dst;
  475. uffdio_register.range.len = nr_pages * page_size;
  476. uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
  477. if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
  478. fprintf(stderr, "register failure\n");
  479. return 1;
  480. }
  481. expected_ioctls = (1 << _UFFDIO_WAKE) |
  482. (1 << _UFFDIO_COPY) |
  483. (1 << _UFFDIO_ZEROPAGE);
  484. if ((uffdio_register.ioctls & expected_ioctls) !=
  485. expected_ioctls) {
  486. fprintf(stderr,
  487. "unexpected missing ioctl for anon memory\n");
  488. return 1;
  489. }
  490. /*
  491. * The madvise done previously isn't enough: some
  492. * uffd_thread could have read userfaults (one of
  493. * those already resolved by the background thread)
  494. * and it may be in the process of calling
  495. * UFFDIO_COPY. UFFDIO_COPY will read the zapped
  496. * area_src and it would map a zero page in it (of
  497. * course such a UFFDIO_COPY is perfectly safe as it'd
  498. * return -EEXIST). The problem comes at the next
  499. * bounce though: that racing UFFDIO_COPY would
  500. * generate zeropages in the area_src, so invalidating
  501. * the previous MADV_DONTNEED. Without this additional
  502. * MADV_DONTNEED those zeropages leftovers in the
  503. * area_src would lead to -EEXIST failure during the
  504. * next bounce, effectively leaving a zeropage in the
  505. * area_dst.
  506. *
  507. * Try to comment this out madvise to see the memory
  508. * corruption being caught pretty quick.
  509. *
  510. * khugepaged is also inhibited to collapse THP after
  511. * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's
  512. * required to MADV_DONTNEED here.
  513. */
  514. if (madvise(area_dst, nr_pages * page_size, MADV_DONTNEED)) {
  515. perror("madvise 2");
  516. return 1;
  517. }
  518. /* bounce pass */
  519. if (stress(userfaults))
  520. return 1;
  521. /* unregister */
  522. if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) {
  523. fprintf(stderr, "register failure\n");
  524. return 1;
  525. }
  526. /* verification */
  527. if (bounces & BOUNCE_VERIFY) {
  528. for (nr = 0; nr < nr_pages; nr++) {
  529. if (*area_count(area_dst, nr) != count_verify[nr]) {
  530. fprintf(stderr,
  531. "error area_count %Lu %Lu %lu\n",
  532. *area_count(area_src, nr),
  533. count_verify[nr],
  534. nr);
  535. err = 1;
  536. bounces = 0;
  537. }
  538. }
  539. }
  540. /* prepare next bounce */
  541. tmp_area = area_src;
  542. area_src = area_dst;
  543. area_dst = tmp_area;
  544. printf("userfaults:");
  545. for (cpu = 0; cpu < nr_cpus; cpu++)
  546. printf(" %lu", userfaults[cpu]);
  547. printf("\n");
  548. }
  549. return err;
  550. }
  551. int main(int argc, char **argv)
  552. {
  553. if (argc < 3)
  554. fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
  555. nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  556. page_size = sysconf(_SC_PAGE_SIZE);
  557. if ((unsigned long) area_count(NULL, 0) + sizeof(unsigned long long) * 2
  558. > page_size)
  559. fprintf(stderr, "Impossible to run this test\n"), exit(2);
  560. nr_pages_per_cpu = atol(argv[1]) * 1024*1024 / page_size /
  561. nr_cpus;
  562. if (!nr_pages_per_cpu) {
  563. fprintf(stderr, "invalid MiB\n");
  564. fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
  565. }
  566. bounces = atoi(argv[2]);
  567. if (bounces <= 0) {
  568. fprintf(stderr, "invalid bounces\n");
  569. fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
  570. }
  571. nr_pages = nr_pages_per_cpu * nr_cpus;
  572. printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
  573. nr_pages, nr_pages_per_cpu);
  574. return userfaultfd_stress();
  575. }
  576. #else /* __NR_userfaultfd */
  577. #warning "missing __NR_userfaultfd definition"
  578. int main(void)
  579. {
  580. printf("skip: Skipping userfaultfd test (missing __NR_userfaultfd)\n");
  581. return 0;
  582. }
  583. #endif /* __NR_userfaultfd */