userfaultfd.c 17 KB

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