userfaultfd.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  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 <sys/wait.h>
  66. #include <pthread.h>
  67. #include <linux/userfaultfd.h>
  68. #include <setjmp.h>
  69. #include <stdbool.h>
  70. #include "../kselftest.h"
  71. #ifdef __NR_userfaultfd
  72. static unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;
  73. #define BOUNCE_RANDOM (1<<0)
  74. #define BOUNCE_RACINGFAULTS (1<<1)
  75. #define BOUNCE_VERIFY (1<<2)
  76. #define BOUNCE_POLL (1<<3)
  77. static int bounces;
  78. #define TEST_ANON 1
  79. #define TEST_HUGETLB 2
  80. #define TEST_SHMEM 3
  81. static int test_type;
  82. /* exercise the test_uffdio_*_eexist every ALARM_INTERVAL_SECS */
  83. #define ALARM_INTERVAL_SECS 10
  84. static volatile bool test_uffdio_copy_eexist = true;
  85. static volatile bool test_uffdio_zeropage_eexist = true;
  86. static bool map_shared;
  87. static int huge_fd;
  88. static char *huge_fd_off0;
  89. static unsigned long long *count_verify;
  90. static int uffd, uffd_flags, finished, *pipefd;
  91. static char *area_src, *area_src_alias, *area_dst, *area_dst_alias;
  92. static char *zeropage;
  93. pthread_attr_t attr;
  94. /* pthread_mutex_t starts at page offset 0 */
  95. #define area_mutex(___area, ___nr) \
  96. ((pthread_mutex_t *) ((___area) + (___nr)*page_size))
  97. /*
  98. * count is placed in the page after pthread_mutex_t naturally aligned
  99. * to avoid non alignment faults on non-x86 archs.
  100. */
  101. #define area_count(___area, ___nr) \
  102. ((volatile unsigned long long *) ((unsigned long) \
  103. ((___area) + (___nr)*page_size + \
  104. sizeof(pthread_mutex_t) + \
  105. sizeof(unsigned long long) - 1) & \
  106. ~(unsigned long)(sizeof(unsigned long long) \
  107. - 1)))
  108. static int anon_release_pages(char *rel_area)
  109. {
  110. int ret = 0;
  111. if (madvise(rel_area, nr_pages * page_size, MADV_DONTNEED)) {
  112. perror("madvise");
  113. ret = 1;
  114. }
  115. return ret;
  116. }
  117. static void anon_allocate_area(void **alloc_area)
  118. {
  119. if (posix_memalign(alloc_area, page_size, nr_pages * page_size)) {
  120. fprintf(stderr, "out of memory\n");
  121. *alloc_area = NULL;
  122. }
  123. }
  124. static void noop_alias_mapping(__u64 *start, size_t len, unsigned long offset)
  125. {
  126. }
  127. /* HugeTLB memory */
  128. static int hugetlb_release_pages(char *rel_area)
  129. {
  130. int ret = 0;
  131. if (fallocate(huge_fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
  132. rel_area == huge_fd_off0 ? 0 :
  133. nr_pages * page_size,
  134. nr_pages * page_size)) {
  135. perror("fallocate");
  136. ret = 1;
  137. }
  138. return ret;
  139. }
  140. static void hugetlb_allocate_area(void **alloc_area)
  141. {
  142. void *area_alias = NULL;
  143. char **alloc_area_alias;
  144. *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
  145. (map_shared ? MAP_SHARED : MAP_PRIVATE) |
  146. MAP_HUGETLB,
  147. huge_fd, *alloc_area == area_src ? 0 :
  148. nr_pages * page_size);
  149. if (*alloc_area == MAP_FAILED) {
  150. fprintf(stderr, "mmap of hugetlbfs file failed\n");
  151. *alloc_area = NULL;
  152. }
  153. if (map_shared) {
  154. area_alias = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
  155. MAP_SHARED | MAP_HUGETLB,
  156. huge_fd, *alloc_area == area_src ? 0 :
  157. nr_pages * page_size);
  158. if (area_alias == MAP_FAILED) {
  159. if (munmap(*alloc_area, nr_pages * page_size) < 0)
  160. perror("hugetlb munmap"), exit(1);
  161. *alloc_area = NULL;
  162. return;
  163. }
  164. }
  165. if (*alloc_area == area_src) {
  166. huge_fd_off0 = *alloc_area;
  167. alloc_area_alias = &area_src_alias;
  168. } else {
  169. alloc_area_alias = &area_dst_alias;
  170. }
  171. if (area_alias)
  172. *alloc_area_alias = area_alias;
  173. }
  174. static void hugetlb_alias_mapping(__u64 *start, size_t len, unsigned long offset)
  175. {
  176. if (!map_shared)
  177. return;
  178. /*
  179. * We can't zap just the pagetable with hugetlbfs because
  180. * MADV_DONTEED won't work. So exercise -EEXIST on a alias
  181. * mapping where the pagetables are not established initially,
  182. * this way we'll exercise the -EEXEC at the fs level.
  183. */
  184. *start = (unsigned long) area_dst_alias + offset;
  185. }
  186. /* Shared memory */
  187. static int shmem_release_pages(char *rel_area)
  188. {
  189. int ret = 0;
  190. if (madvise(rel_area, nr_pages * page_size, MADV_REMOVE)) {
  191. perror("madvise");
  192. ret = 1;
  193. }
  194. return ret;
  195. }
  196. static void shmem_allocate_area(void **alloc_area)
  197. {
  198. *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
  199. MAP_ANONYMOUS | MAP_SHARED, -1, 0);
  200. if (*alloc_area == MAP_FAILED) {
  201. fprintf(stderr, "shared memory mmap failed\n");
  202. *alloc_area = NULL;
  203. }
  204. }
  205. struct uffd_test_ops {
  206. unsigned long expected_ioctls;
  207. void (*allocate_area)(void **alloc_area);
  208. int (*release_pages)(char *rel_area);
  209. void (*alias_mapping)(__u64 *start, size_t len, unsigned long offset);
  210. };
  211. #define ANON_EXPECTED_IOCTLS ((1 << _UFFDIO_WAKE) | \
  212. (1 << _UFFDIO_COPY) | \
  213. (1 << _UFFDIO_ZEROPAGE))
  214. static struct uffd_test_ops anon_uffd_test_ops = {
  215. .expected_ioctls = ANON_EXPECTED_IOCTLS,
  216. .allocate_area = anon_allocate_area,
  217. .release_pages = anon_release_pages,
  218. .alias_mapping = noop_alias_mapping,
  219. };
  220. static struct uffd_test_ops shmem_uffd_test_ops = {
  221. .expected_ioctls = ANON_EXPECTED_IOCTLS,
  222. .allocate_area = shmem_allocate_area,
  223. .release_pages = shmem_release_pages,
  224. .alias_mapping = noop_alias_mapping,
  225. };
  226. static struct uffd_test_ops hugetlb_uffd_test_ops = {
  227. .expected_ioctls = UFFD_API_RANGE_IOCTLS_BASIC,
  228. .allocate_area = hugetlb_allocate_area,
  229. .release_pages = hugetlb_release_pages,
  230. .alias_mapping = hugetlb_alias_mapping,
  231. };
  232. static struct uffd_test_ops *uffd_test_ops;
  233. static int my_bcmp(char *str1, char *str2, size_t n)
  234. {
  235. unsigned long i;
  236. for (i = 0; i < n; i++)
  237. if (str1[i] != str2[i])
  238. return 1;
  239. return 0;
  240. }
  241. static void *locking_thread(void *arg)
  242. {
  243. unsigned long cpu = (unsigned long) arg;
  244. struct random_data rand;
  245. unsigned long page_nr = *(&(page_nr)); /* uninitialized warning */
  246. int32_t rand_nr;
  247. unsigned long long count;
  248. char randstate[64];
  249. unsigned int seed;
  250. time_t start;
  251. if (bounces & BOUNCE_RANDOM) {
  252. seed = (unsigned int) time(NULL) - bounces;
  253. if (!(bounces & BOUNCE_RACINGFAULTS))
  254. seed += cpu;
  255. bzero(&rand, sizeof(rand));
  256. bzero(&randstate, sizeof(randstate));
  257. if (initstate_r(seed, randstate, sizeof(randstate), &rand))
  258. fprintf(stderr, "srandom_r error\n"), exit(1);
  259. } else {
  260. page_nr = -bounces;
  261. if (!(bounces & BOUNCE_RACINGFAULTS))
  262. page_nr += cpu * nr_pages_per_cpu;
  263. }
  264. while (!finished) {
  265. if (bounces & BOUNCE_RANDOM) {
  266. if (random_r(&rand, &rand_nr))
  267. fprintf(stderr, "random_r 1 error\n"), exit(1);
  268. page_nr = rand_nr;
  269. if (sizeof(page_nr) > sizeof(rand_nr)) {
  270. if (random_r(&rand, &rand_nr))
  271. fprintf(stderr, "random_r 2 error\n"), exit(1);
  272. page_nr |= (((unsigned long) rand_nr) << 16) <<
  273. 16;
  274. }
  275. } else
  276. page_nr += 1;
  277. page_nr %= nr_pages;
  278. start = time(NULL);
  279. if (bounces & BOUNCE_VERIFY) {
  280. count = *area_count(area_dst, page_nr);
  281. if (!count)
  282. fprintf(stderr,
  283. "page_nr %lu wrong count %Lu %Lu\n",
  284. page_nr, count,
  285. count_verify[page_nr]), exit(1);
  286. /*
  287. * We can't use bcmp (or memcmp) because that
  288. * returns 0 erroneously if the memory is
  289. * changing under it (even if the end of the
  290. * page is never changing and always
  291. * different).
  292. */
  293. #if 1
  294. if (!my_bcmp(area_dst + page_nr * page_size, zeropage,
  295. page_size))
  296. fprintf(stderr,
  297. "my_bcmp page_nr %lu wrong count %Lu %Lu\n",
  298. page_nr, count,
  299. count_verify[page_nr]), exit(1);
  300. #else
  301. unsigned long loops;
  302. loops = 0;
  303. /* uncomment the below line to test with mutex */
  304. /* pthread_mutex_lock(area_mutex(area_dst, page_nr)); */
  305. while (!bcmp(area_dst + page_nr * page_size, zeropage,
  306. page_size)) {
  307. loops += 1;
  308. if (loops > 10)
  309. break;
  310. }
  311. /* uncomment below line to test with mutex */
  312. /* pthread_mutex_unlock(area_mutex(area_dst, page_nr)); */
  313. if (loops) {
  314. fprintf(stderr,
  315. "page_nr %lu all zero thread %lu %p %lu\n",
  316. page_nr, cpu, area_dst + page_nr * page_size,
  317. loops);
  318. if (loops > 10)
  319. exit(1);
  320. }
  321. #endif
  322. }
  323. pthread_mutex_lock(area_mutex(area_dst, page_nr));
  324. count = *area_count(area_dst, page_nr);
  325. if (count != count_verify[page_nr]) {
  326. fprintf(stderr,
  327. "page_nr %lu memory corruption %Lu %Lu\n",
  328. page_nr, count,
  329. count_verify[page_nr]), exit(1);
  330. }
  331. count++;
  332. *area_count(area_dst, page_nr) = count_verify[page_nr] = count;
  333. pthread_mutex_unlock(area_mutex(area_dst, page_nr));
  334. if (time(NULL) - start > 1)
  335. fprintf(stderr,
  336. "userfault too slow %ld "
  337. "possible false positive with overcommit\n",
  338. time(NULL) - start);
  339. }
  340. return NULL;
  341. }
  342. static void retry_copy_page(int ufd, struct uffdio_copy *uffdio_copy,
  343. unsigned long offset)
  344. {
  345. uffd_test_ops->alias_mapping(&uffdio_copy->dst,
  346. uffdio_copy->len,
  347. offset);
  348. if (ioctl(ufd, UFFDIO_COPY, uffdio_copy)) {
  349. /* real retval in ufdio_copy.copy */
  350. if (uffdio_copy->copy != -EEXIST)
  351. fprintf(stderr, "UFFDIO_COPY retry error %Ld\n",
  352. uffdio_copy->copy), exit(1);
  353. } else {
  354. fprintf(stderr, "UFFDIO_COPY retry unexpected %Ld\n",
  355. uffdio_copy->copy), exit(1);
  356. }
  357. }
  358. static int __copy_page(int ufd, unsigned long offset, bool retry)
  359. {
  360. struct uffdio_copy uffdio_copy;
  361. if (offset >= nr_pages * page_size)
  362. fprintf(stderr, "unexpected offset %lu\n",
  363. offset), exit(1);
  364. uffdio_copy.dst = (unsigned long) area_dst + offset;
  365. uffdio_copy.src = (unsigned long) area_src + offset;
  366. uffdio_copy.len = page_size;
  367. uffdio_copy.mode = 0;
  368. uffdio_copy.copy = 0;
  369. if (ioctl(ufd, UFFDIO_COPY, &uffdio_copy)) {
  370. /* real retval in ufdio_copy.copy */
  371. if (uffdio_copy.copy != -EEXIST)
  372. fprintf(stderr, "UFFDIO_COPY error %Ld\n",
  373. uffdio_copy.copy), exit(1);
  374. } else if (uffdio_copy.copy != page_size) {
  375. fprintf(stderr, "UFFDIO_COPY unexpected copy %Ld\n",
  376. uffdio_copy.copy), exit(1);
  377. } else {
  378. if (test_uffdio_copy_eexist && retry) {
  379. test_uffdio_copy_eexist = false;
  380. retry_copy_page(ufd, &uffdio_copy, offset);
  381. }
  382. return 1;
  383. }
  384. return 0;
  385. }
  386. static int copy_page_retry(int ufd, unsigned long offset)
  387. {
  388. return __copy_page(ufd, offset, true);
  389. }
  390. static int copy_page(int ufd, unsigned long offset)
  391. {
  392. return __copy_page(ufd, offset, false);
  393. }
  394. static void *uffd_poll_thread(void *arg)
  395. {
  396. unsigned long cpu = (unsigned long) arg;
  397. struct pollfd pollfd[2];
  398. struct uffd_msg msg;
  399. struct uffdio_register uffd_reg;
  400. int ret;
  401. unsigned long offset;
  402. char tmp_chr;
  403. unsigned long userfaults = 0;
  404. pollfd[0].fd = uffd;
  405. pollfd[0].events = POLLIN;
  406. pollfd[1].fd = pipefd[cpu*2];
  407. pollfd[1].events = POLLIN;
  408. for (;;) {
  409. ret = poll(pollfd, 2, -1);
  410. if (!ret)
  411. fprintf(stderr, "poll error %d\n", ret), exit(1);
  412. if (ret < 0)
  413. perror("poll"), exit(1);
  414. if (pollfd[1].revents & POLLIN) {
  415. if (read(pollfd[1].fd, &tmp_chr, 1) != 1)
  416. fprintf(stderr, "read pipefd error\n"),
  417. exit(1);
  418. break;
  419. }
  420. if (!(pollfd[0].revents & POLLIN))
  421. fprintf(stderr, "pollfd[0].revents %d\n",
  422. pollfd[0].revents), exit(1);
  423. ret = read(uffd, &msg, sizeof(msg));
  424. if (ret < 0) {
  425. if (errno == EAGAIN)
  426. continue;
  427. perror("nonblocking read error"), exit(1);
  428. }
  429. switch (msg.event) {
  430. default:
  431. fprintf(stderr, "unexpected msg event %u\n",
  432. msg.event), exit(1);
  433. break;
  434. case UFFD_EVENT_PAGEFAULT:
  435. if (msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
  436. fprintf(stderr, "unexpected write fault\n"), exit(1);
  437. offset = (char *)(unsigned long)msg.arg.pagefault.address -
  438. area_dst;
  439. offset &= ~(page_size-1);
  440. if (copy_page(uffd, offset))
  441. userfaults++;
  442. break;
  443. case UFFD_EVENT_FORK:
  444. close(uffd);
  445. uffd = msg.arg.fork.ufd;
  446. pollfd[0].fd = uffd;
  447. break;
  448. case UFFD_EVENT_REMOVE:
  449. uffd_reg.range.start = msg.arg.remove.start;
  450. uffd_reg.range.len = msg.arg.remove.end -
  451. msg.arg.remove.start;
  452. if (ioctl(uffd, UFFDIO_UNREGISTER, &uffd_reg.range))
  453. fprintf(stderr, "remove failure\n"), exit(1);
  454. break;
  455. case UFFD_EVENT_REMAP:
  456. area_dst = (char *)(unsigned long)msg.arg.remap.to;
  457. break;
  458. }
  459. }
  460. return (void *)userfaults;
  461. }
  462. pthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER;
  463. static void *uffd_read_thread(void *arg)
  464. {
  465. unsigned long *this_cpu_userfaults;
  466. struct uffd_msg msg;
  467. unsigned long offset;
  468. int ret;
  469. this_cpu_userfaults = (unsigned long *) arg;
  470. *this_cpu_userfaults = 0;
  471. pthread_mutex_unlock(&uffd_read_mutex);
  472. /* from here cancellation is ok */
  473. for (;;) {
  474. ret = read(uffd, &msg, sizeof(msg));
  475. if (ret != sizeof(msg)) {
  476. if (ret < 0)
  477. perror("blocking read error"), exit(1);
  478. else
  479. fprintf(stderr, "short read\n"), exit(1);
  480. }
  481. if (msg.event != UFFD_EVENT_PAGEFAULT)
  482. fprintf(stderr, "unexpected msg event %u\n",
  483. msg.event), exit(1);
  484. if (bounces & BOUNCE_VERIFY &&
  485. msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
  486. fprintf(stderr, "unexpected write fault\n"), exit(1);
  487. offset = (char *)(unsigned long)msg.arg.pagefault.address -
  488. area_dst;
  489. offset &= ~(page_size-1);
  490. if (copy_page(uffd, offset))
  491. (*this_cpu_userfaults)++;
  492. }
  493. return (void *)NULL;
  494. }
  495. static void *background_thread(void *arg)
  496. {
  497. unsigned long cpu = (unsigned long) arg;
  498. unsigned long page_nr;
  499. for (page_nr = cpu * nr_pages_per_cpu;
  500. page_nr < (cpu+1) * nr_pages_per_cpu;
  501. page_nr++)
  502. copy_page_retry(uffd, page_nr * page_size);
  503. return NULL;
  504. }
  505. static int stress(unsigned long *userfaults)
  506. {
  507. unsigned long cpu;
  508. pthread_t locking_threads[nr_cpus];
  509. pthread_t uffd_threads[nr_cpus];
  510. pthread_t background_threads[nr_cpus];
  511. void **_userfaults = (void **) userfaults;
  512. finished = 0;
  513. for (cpu = 0; cpu < nr_cpus; cpu++) {
  514. if (pthread_create(&locking_threads[cpu], &attr,
  515. locking_thread, (void *)cpu))
  516. return 1;
  517. if (bounces & BOUNCE_POLL) {
  518. if (pthread_create(&uffd_threads[cpu], &attr,
  519. uffd_poll_thread, (void *)cpu))
  520. return 1;
  521. } else {
  522. if (pthread_create(&uffd_threads[cpu], &attr,
  523. uffd_read_thread,
  524. &_userfaults[cpu]))
  525. return 1;
  526. pthread_mutex_lock(&uffd_read_mutex);
  527. }
  528. if (pthread_create(&background_threads[cpu], &attr,
  529. background_thread, (void *)cpu))
  530. return 1;
  531. }
  532. for (cpu = 0; cpu < nr_cpus; cpu++)
  533. if (pthread_join(background_threads[cpu], NULL))
  534. return 1;
  535. /*
  536. * Be strict and immediately zap area_src, the whole area has
  537. * been transferred already by the background treads. The
  538. * area_src could then be faulted in in a racy way by still
  539. * running uffdio_threads reading zeropages after we zapped
  540. * area_src (but they're guaranteed to get -EEXIST from
  541. * UFFDIO_COPY without writing zero pages into area_dst
  542. * because the background threads already completed).
  543. */
  544. if (uffd_test_ops->release_pages(area_src))
  545. return 1;
  546. for (cpu = 0; cpu < nr_cpus; cpu++) {
  547. char c;
  548. if (bounces & BOUNCE_POLL) {
  549. if (write(pipefd[cpu*2+1], &c, 1) != 1) {
  550. fprintf(stderr, "pipefd write error\n");
  551. return 1;
  552. }
  553. if (pthread_join(uffd_threads[cpu], &_userfaults[cpu]))
  554. return 1;
  555. } else {
  556. if (pthread_cancel(uffd_threads[cpu]))
  557. return 1;
  558. if (pthread_join(uffd_threads[cpu], NULL))
  559. return 1;
  560. }
  561. }
  562. finished = 1;
  563. for (cpu = 0; cpu < nr_cpus; cpu++)
  564. if (pthread_join(locking_threads[cpu], NULL))
  565. return 1;
  566. return 0;
  567. }
  568. static int userfaultfd_open(int features)
  569. {
  570. struct uffdio_api uffdio_api;
  571. uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
  572. if (uffd < 0) {
  573. fprintf(stderr,
  574. "userfaultfd syscall not available in this kernel\n");
  575. return 1;
  576. }
  577. uffd_flags = fcntl(uffd, F_GETFD, NULL);
  578. uffdio_api.api = UFFD_API;
  579. uffdio_api.features = features;
  580. if (ioctl(uffd, UFFDIO_API, &uffdio_api)) {
  581. fprintf(stderr, "UFFDIO_API\n");
  582. return 1;
  583. }
  584. if (uffdio_api.api != UFFD_API) {
  585. fprintf(stderr, "UFFDIO_API error %Lu\n", uffdio_api.api);
  586. return 1;
  587. }
  588. return 0;
  589. }
  590. sigjmp_buf jbuf, *sigbuf;
  591. static void sighndl(int sig, siginfo_t *siginfo, void *ptr)
  592. {
  593. if (sig == SIGBUS) {
  594. if (sigbuf)
  595. siglongjmp(*sigbuf, 1);
  596. abort();
  597. }
  598. }
  599. /*
  600. * For non-cooperative userfaultfd test we fork() a process that will
  601. * generate pagefaults, will mremap the area monitored by the
  602. * userfaultfd and at last this process will release the monitored
  603. * area.
  604. * For the anonymous and shared memory the area is divided into two
  605. * parts, the first part is accessed before mremap, and the second
  606. * part is accessed after mremap. Since hugetlbfs does not support
  607. * mremap, the entire monitored area is accessed in a single pass for
  608. * HUGETLB_TEST.
  609. * The release of the pages currently generates event for shmem and
  610. * anonymous memory (UFFD_EVENT_REMOVE), hence it is not checked
  611. * for hugetlb.
  612. * For signal test(UFFD_FEATURE_SIGBUS), signal_test = 1, we register
  613. * monitored area, generate pagefaults and test that signal is delivered.
  614. * Use UFFDIO_COPY to allocate missing page and retry. For signal_test = 2
  615. * test robustness use case - we release monitored area, fork a process
  616. * that will generate pagefaults and verify signal is generated.
  617. * This also tests UFFD_FEATURE_EVENT_FORK event along with the signal
  618. * feature. Using monitor thread, verify no userfault events are generated.
  619. */
  620. static int faulting_process(int signal_test)
  621. {
  622. unsigned long nr;
  623. unsigned long long count;
  624. unsigned long split_nr_pages;
  625. unsigned long lastnr;
  626. struct sigaction act;
  627. unsigned long signalled = 0;
  628. if (test_type != TEST_HUGETLB)
  629. split_nr_pages = (nr_pages + 1) / 2;
  630. else
  631. split_nr_pages = nr_pages;
  632. if (signal_test) {
  633. sigbuf = &jbuf;
  634. memset(&act, 0, sizeof(act));
  635. act.sa_sigaction = sighndl;
  636. act.sa_flags = SA_SIGINFO;
  637. if (sigaction(SIGBUS, &act, 0)) {
  638. perror("sigaction");
  639. return 1;
  640. }
  641. lastnr = (unsigned long)-1;
  642. }
  643. for (nr = 0; nr < split_nr_pages; nr++) {
  644. if (signal_test) {
  645. if (sigsetjmp(*sigbuf, 1) != 0) {
  646. if (nr == lastnr) {
  647. fprintf(stderr, "Signal repeated\n");
  648. return 1;
  649. }
  650. lastnr = nr;
  651. if (signal_test == 1) {
  652. if (copy_page(uffd, nr * page_size))
  653. signalled++;
  654. } else {
  655. signalled++;
  656. continue;
  657. }
  658. }
  659. }
  660. count = *area_count(area_dst, nr);
  661. if (count != count_verify[nr]) {
  662. fprintf(stderr,
  663. "nr %lu memory corruption %Lu %Lu\n",
  664. nr, count,
  665. count_verify[nr]), exit(1);
  666. }
  667. }
  668. if (signal_test)
  669. return signalled != split_nr_pages;
  670. if (test_type == TEST_HUGETLB)
  671. return 0;
  672. area_dst = mremap(area_dst, nr_pages * page_size, nr_pages * page_size,
  673. MREMAP_MAYMOVE | MREMAP_FIXED, area_src);
  674. if (area_dst == MAP_FAILED)
  675. perror("mremap"), exit(1);
  676. for (; nr < nr_pages; nr++) {
  677. count = *area_count(area_dst, nr);
  678. if (count != count_verify[nr]) {
  679. fprintf(stderr,
  680. "nr %lu memory corruption %Lu %Lu\n",
  681. nr, count,
  682. count_verify[nr]), exit(1);
  683. }
  684. }
  685. if (uffd_test_ops->release_pages(area_dst))
  686. return 1;
  687. for (nr = 0; nr < nr_pages; nr++) {
  688. if (my_bcmp(area_dst + nr * page_size, zeropage, page_size))
  689. fprintf(stderr, "nr %lu is not zero\n", nr), exit(1);
  690. }
  691. return 0;
  692. }
  693. static void retry_uffdio_zeropage(int ufd,
  694. struct uffdio_zeropage *uffdio_zeropage,
  695. unsigned long offset)
  696. {
  697. uffd_test_ops->alias_mapping(&uffdio_zeropage->range.start,
  698. uffdio_zeropage->range.len,
  699. offset);
  700. if (ioctl(ufd, UFFDIO_ZEROPAGE, uffdio_zeropage)) {
  701. if (uffdio_zeropage->zeropage != -EEXIST)
  702. fprintf(stderr, "UFFDIO_ZEROPAGE retry error %Ld\n",
  703. uffdio_zeropage->zeropage), exit(1);
  704. } else {
  705. fprintf(stderr, "UFFDIO_ZEROPAGE retry unexpected %Ld\n",
  706. uffdio_zeropage->zeropage), exit(1);
  707. }
  708. }
  709. static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
  710. {
  711. struct uffdio_zeropage uffdio_zeropage;
  712. int ret;
  713. unsigned long has_zeropage;
  714. has_zeropage = uffd_test_ops->expected_ioctls & (1 << _UFFDIO_ZEROPAGE);
  715. if (offset >= nr_pages * page_size)
  716. fprintf(stderr, "unexpected offset %lu\n",
  717. offset), exit(1);
  718. uffdio_zeropage.range.start = (unsigned long) area_dst + offset;
  719. uffdio_zeropage.range.len = page_size;
  720. uffdio_zeropage.mode = 0;
  721. ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage);
  722. if (ret) {
  723. /* real retval in ufdio_zeropage.zeropage */
  724. if (has_zeropage) {
  725. if (uffdio_zeropage.zeropage == -EEXIST)
  726. fprintf(stderr, "UFFDIO_ZEROPAGE -EEXIST\n"),
  727. exit(1);
  728. else
  729. fprintf(stderr, "UFFDIO_ZEROPAGE error %Ld\n",
  730. uffdio_zeropage.zeropage), exit(1);
  731. } else {
  732. if (uffdio_zeropage.zeropage != -EINVAL)
  733. fprintf(stderr,
  734. "UFFDIO_ZEROPAGE not -EINVAL %Ld\n",
  735. uffdio_zeropage.zeropage), exit(1);
  736. }
  737. } else if (has_zeropage) {
  738. if (uffdio_zeropage.zeropage != page_size) {
  739. fprintf(stderr, "UFFDIO_ZEROPAGE unexpected %Ld\n",
  740. uffdio_zeropage.zeropage), exit(1);
  741. } else {
  742. if (test_uffdio_zeropage_eexist && retry) {
  743. test_uffdio_zeropage_eexist = false;
  744. retry_uffdio_zeropage(ufd, &uffdio_zeropage,
  745. offset);
  746. }
  747. return 1;
  748. }
  749. } else {
  750. fprintf(stderr,
  751. "UFFDIO_ZEROPAGE succeeded %Ld\n",
  752. uffdio_zeropage.zeropage), exit(1);
  753. }
  754. return 0;
  755. }
  756. static int uffdio_zeropage(int ufd, unsigned long offset)
  757. {
  758. return __uffdio_zeropage(ufd, offset, false);
  759. }
  760. /* exercise UFFDIO_ZEROPAGE */
  761. static int userfaultfd_zeropage_test(void)
  762. {
  763. struct uffdio_register uffdio_register;
  764. unsigned long expected_ioctls;
  765. printf("testing UFFDIO_ZEROPAGE: ");
  766. fflush(stdout);
  767. if (uffd_test_ops->release_pages(area_dst))
  768. return 1;
  769. if (userfaultfd_open(0) < 0)
  770. return 1;
  771. uffdio_register.range.start = (unsigned long) area_dst;
  772. uffdio_register.range.len = nr_pages * page_size;
  773. uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
  774. if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
  775. fprintf(stderr, "register failure\n"), exit(1);
  776. expected_ioctls = uffd_test_ops->expected_ioctls;
  777. if ((uffdio_register.ioctls & expected_ioctls) !=
  778. expected_ioctls)
  779. fprintf(stderr,
  780. "unexpected missing ioctl for anon memory\n"),
  781. exit(1);
  782. if (uffdio_zeropage(uffd, 0)) {
  783. if (my_bcmp(area_dst, zeropage, page_size))
  784. fprintf(stderr, "zeropage is not zero\n"), exit(1);
  785. }
  786. close(uffd);
  787. printf("done.\n");
  788. return 0;
  789. }
  790. static int userfaultfd_events_test(void)
  791. {
  792. struct uffdio_register uffdio_register;
  793. unsigned long expected_ioctls;
  794. unsigned long userfaults;
  795. pthread_t uffd_mon;
  796. int err, features;
  797. pid_t pid;
  798. char c;
  799. printf("testing events (fork, remap, remove): ");
  800. fflush(stdout);
  801. if (uffd_test_ops->release_pages(area_dst))
  802. return 1;
  803. features = UFFD_FEATURE_EVENT_FORK | UFFD_FEATURE_EVENT_REMAP |
  804. UFFD_FEATURE_EVENT_REMOVE;
  805. if (userfaultfd_open(features) < 0)
  806. return 1;
  807. fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
  808. uffdio_register.range.start = (unsigned long) area_dst;
  809. uffdio_register.range.len = nr_pages * page_size;
  810. uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
  811. if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
  812. fprintf(stderr, "register failure\n"), exit(1);
  813. expected_ioctls = uffd_test_ops->expected_ioctls;
  814. if ((uffdio_register.ioctls & expected_ioctls) !=
  815. expected_ioctls)
  816. fprintf(stderr,
  817. "unexpected missing ioctl for anon memory\n"),
  818. exit(1);
  819. if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
  820. perror("uffd_poll_thread create"), exit(1);
  821. pid = fork();
  822. if (pid < 0)
  823. perror("fork"), exit(1);
  824. if (!pid)
  825. return faulting_process(0);
  826. waitpid(pid, &err, 0);
  827. if (err)
  828. fprintf(stderr, "faulting process failed\n"), exit(1);
  829. if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
  830. perror("pipe write"), exit(1);
  831. if (pthread_join(uffd_mon, (void **)&userfaults))
  832. return 1;
  833. close(uffd);
  834. printf("userfaults: %ld\n", userfaults);
  835. return userfaults != nr_pages;
  836. }
  837. static int userfaultfd_sig_test(void)
  838. {
  839. struct uffdio_register uffdio_register;
  840. unsigned long expected_ioctls;
  841. unsigned long userfaults;
  842. pthread_t uffd_mon;
  843. int err, features;
  844. pid_t pid;
  845. char c;
  846. printf("testing signal delivery: ");
  847. fflush(stdout);
  848. if (uffd_test_ops->release_pages(area_dst))
  849. return 1;
  850. features = UFFD_FEATURE_EVENT_FORK|UFFD_FEATURE_SIGBUS;
  851. if (userfaultfd_open(features) < 0)
  852. return 1;
  853. fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
  854. uffdio_register.range.start = (unsigned long) area_dst;
  855. uffdio_register.range.len = nr_pages * page_size;
  856. uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
  857. if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
  858. fprintf(stderr, "register failure\n"), exit(1);
  859. expected_ioctls = uffd_test_ops->expected_ioctls;
  860. if ((uffdio_register.ioctls & expected_ioctls) !=
  861. expected_ioctls)
  862. fprintf(stderr,
  863. "unexpected missing ioctl for anon memory\n"),
  864. exit(1);
  865. if (faulting_process(1))
  866. fprintf(stderr, "faulting process failed\n"), exit(1);
  867. if (uffd_test_ops->release_pages(area_dst))
  868. return 1;
  869. if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
  870. perror("uffd_poll_thread create"), exit(1);
  871. pid = fork();
  872. if (pid < 0)
  873. perror("fork"), exit(1);
  874. if (!pid)
  875. exit(faulting_process(2));
  876. waitpid(pid, &err, 0);
  877. if (err)
  878. fprintf(stderr, "faulting process failed\n"), exit(1);
  879. if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
  880. perror("pipe write"), exit(1);
  881. if (pthread_join(uffd_mon, (void **)&userfaults))
  882. return 1;
  883. printf("done.\n");
  884. if (userfaults)
  885. fprintf(stderr, "Signal test failed, userfaults: %ld\n",
  886. userfaults);
  887. close(uffd);
  888. return userfaults != 0;
  889. }
  890. static int userfaultfd_stress(void)
  891. {
  892. void *area;
  893. char *tmp_area;
  894. unsigned long nr;
  895. struct uffdio_register uffdio_register;
  896. unsigned long cpu;
  897. int err;
  898. unsigned long userfaults[nr_cpus];
  899. uffd_test_ops->allocate_area((void **)&area_src);
  900. if (!area_src)
  901. return 1;
  902. uffd_test_ops->allocate_area((void **)&area_dst);
  903. if (!area_dst)
  904. return 1;
  905. if (userfaultfd_open(0) < 0)
  906. return 1;
  907. count_verify = malloc(nr_pages * sizeof(unsigned long long));
  908. if (!count_verify) {
  909. perror("count_verify");
  910. return 1;
  911. }
  912. for (nr = 0; nr < nr_pages; nr++) {
  913. *area_mutex(area_src, nr) = (pthread_mutex_t)
  914. PTHREAD_MUTEX_INITIALIZER;
  915. count_verify[nr] = *area_count(area_src, nr) = 1;
  916. /*
  917. * In the transition between 255 to 256, powerpc will
  918. * read out of order in my_bcmp and see both bytes as
  919. * zero, so leave a placeholder below always non-zero
  920. * after the count, to avoid my_bcmp to trigger false
  921. * positives.
  922. */
  923. *(area_count(area_src, nr) + 1) = 1;
  924. }
  925. pipefd = malloc(sizeof(int) * nr_cpus * 2);
  926. if (!pipefd) {
  927. perror("pipefd");
  928. return 1;
  929. }
  930. for (cpu = 0; cpu < nr_cpus; cpu++) {
  931. if (pipe2(&pipefd[cpu*2], O_CLOEXEC | O_NONBLOCK)) {
  932. perror("pipe");
  933. return 1;
  934. }
  935. }
  936. if (posix_memalign(&area, page_size, page_size)) {
  937. fprintf(stderr, "out of memory\n");
  938. return 1;
  939. }
  940. zeropage = area;
  941. bzero(zeropage, page_size);
  942. pthread_mutex_lock(&uffd_read_mutex);
  943. pthread_attr_init(&attr);
  944. pthread_attr_setstacksize(&attr, 16*1024*1024);
  945. err = 0;
  946. while (bounces--) {
  947. unsigned long expected_ioctls;
  948. printf("bounces: %d, mode:", bounces);
  949. if (bounces & BOUNCE_RANDOM)
  950. printf(" rnd");
  951. if (bounces & BOUNCE_RACINGFAULTS)
  952. printf(" racing");
  953. if (bounces & BOUNCE_VERIFY)
  954. printf(" ver");
  955. if (bounces & BOUNCE_POLL)
  956. printf(" poll");
  957. printf(", ");
  958. fflush(stdout);
  959. if (bounces & BOUNCE_POLL)
  960. fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
  961. else
  962. fcntl(uffd, F_SETFL, uffd_flags & ~O_NONBLOCK);
  963. /* register */
  964. uffdio_register.range.start = (unsigned long) area_dst;
  965. uffdio_register.range.len = nr_pages * page_size;
  966. uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
  967. if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
  968. fprintf(stderr, "register failure\n");
  969. return 1;
  970. }
  971. expected_ioctls = uffd_test_ops->expected_ioctls;
  972. if ((uffdio_register.ioctls & expected_ioctls) !=
  973. expected_ioctls) {
  974. fprintf(stderr,
  975. "unexpected missing ioctl for anon memory\n");
  976. return 1;
  977. }
  978. if (area_dst_alias) {
  979. uffdio_register.range.start = (unsigned long)
  980. area_dst_alias;
  981. if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
  982. fprintf(stderr, "register failure alias\n");
  983. return 1;
  984. }
  985. }
  986. /*
  987. * The madvise done previously isn't enough: some
  988. * uffd_thread could have read userfaults (one of
  989. * those already resolved by the background thread)
  990. * and it may be in the process of calling
  991. * UFFDIO_COPY. UFFDIO_COPY will read the zapped
  992. * area_src and it would map a zero page in it (of
  993. * course such a UFFDIO_COPY is perfectly safe as it'd
  994. * return -EEXIST). The problem comes at the next
  995. * bounce though: that racing UFFDIO_COPY would
  996. * generate zeropages in the area_src, so invalidating
  997. * the previous MADV_DONTNEED. Without this additional
  998. * MADV_DONTNEED those zeropages leftovers in the
  999. * area_src would lead to -EEXIST failure during the
  1000. * next bounce, effectively leaving a zeropage in the
  1001. * area_dst.
  1002. *
  1003. * Try to comment this out madvise to see the memory
  1004. * corruption being caught pretty quick.
  1005. *
  1006. * khugepaged is also inhibited to collapse THP after
  1007. * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's
  1008. * required to MADV_DONTNEED here.
  1009. */
  1010. if (uffd_test_ops->release_pages(area_dst))
  1011. return 1;
  1012. /* bounce pass */
  1013. if (stress(userfaults))
  1014. return 1;
  1015. /* unregister */
  1016. if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) {
  1017. fprintf(stderr, "unregister failure\n");
  1018. return 1;
  1019. }
  1020. if (area_dst_alias) {
  1021. uffdio_register.range.start = (unsigned long) area_dst;
  1022. if (ioctl(uffd, UFFDIO_UNREGISTER,
  1023. &uffdio_register.range)) {
  1024. fprintf(stderr, "unregister failure alias\n");
  1025. return 1;
  1026. }
  1027. }
  1028. /* verification */
  1029. if (bounces & BOUNCE_VERIFY) {
  1030. for (nr = 0; nr < nr_pages; nr++) {
  1031. if (*area_count(area_dst, nr) != count_verify[nr]) {
  1032. fprintf(stderr,
  1033. "error area_count %Lu %Lu %lu\n",
  1034. *area_count(area_src, nr),
  1035. count_verify[nr],
  1036. nr);
  1037. err = 1;
  1038. bounces = 0;
  1039. }
  1040. }
  1041. }
  1042. /* prepare next bounce */
  1043. tmp_area = area_src;
  1044. area_src = area_dst;
  1045. area_dst = tmp_area;
  1046. tmp_area = area_src_alias;
  1047. area_src_alias = area_dst_alias;
  1048. area_dst_alias = tmp_area;
  1049. printf("userfaults:");
  1050. for (cpu = 0; cpu < nr_cpus; cpu++)
  1051. printf(" %lu", userfaults[cpu]);
  1052. printf("\n");
  1053. }
  1054. if (err)
  1055. return err;
  1056. close(uffd);
  1057. return userfaultfd_zeropage_test() || userfaultfd_sig_test()
  1058. || userfaultfd_events_test();
  1059. }
  1060. /*
  1061. * Copied from mlock2-tests.c
  1062. */
  1063. unsigned long default_huge_page_size(void)
  1064. {
  1065. unsigned long hps = 0;
  1066. char *line = NULL;
  1067. size_t linelen = 0;
  1068. FILE *f = fopen("/proc/meminfo", "r");
  1069. if (!f)
  1070. return 0;
  1071. while (getline(&line, &linelen, f) > 0) {
  1072. if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
  1073. hps <<= 10;
  1074. break;
  1075. }
  1076. }
  1077. free(line);
  1078. fclose(f);
  1079. return hps;
  1080. }
  1081. static void set_test_type(const char *type)
  1082. {
  1083. if (!strcmp(type, "anon")) {
  1084. test_type = TEST_ANON;
  1085. uffd_test_ops = &anon_uffd_test_ops;
  1086. } else if (!strcmp(type, "hugetlb")) {
  1087. test_type = TEST_HUGETLB;
  1088. uffd_test_ops = &hugetlb_uffd_test_ops;
  1089. } else if (!strcmp(type, "hugetlb_shared")) {
  1090. map_shared = true;
  1091. test_type = TEST_HUGETLB;
  1092. uffd_test_ops = &hugetlb_uffd_test_ops;
  1093. } else if (!strcmp(type, "shmem")) {
  1094. map_shared = true;
  1095. test_type = TEST_SHMEM;
  1096. uffd_test_ops = &shmem_uffd_test_ops;
  1097. } else {
  1098. fprintf(stderr, "Unknown test type: %s\n", type), exit(1);
  1099. }
  1100. if (test_type == TEST_HUGETLB)
  1101. page_size = default_huge_page_size();
  1102. else
  1103. page_size = sysconf(_SC_PAGE_SIZE);
  1104. if (!page_size)
  1105. fprintf(stderr, "Unable to determine page size\n"),
  1106. exit(2);
  1107. if ((unsigned long) area_count(NULL, 0) + sizeof(unsigned long long) * 2
  1108. > page_size)
  1109. fprintf(stderr, "Impossible to run this test\n"), exit(2);
  1110. }
  1111. static void sigalrm(int sig)
  1112. {
  1113. if (sig != SIGALRM)
  1114. abort();
  1115. test_uffdio_copy_eexist = true;
  1116. test_uffdio_zeropage_eexist = true;
  1117. alarm(ALARM_INTERVAL_SECS);
  1118. }
  1119. int main(int argc, char **argv)
  1120. {
  1121. if (argc < 4)
  1122. fprintf(stderr, "Usage: <test type> <MiB> <bounces> [hugetlbfs_file]\n"),
  1123. exit(1);
  1124. if (signal(SIGALRM, sigalrm) == SIG_ERR)
  1125. fprintf(stderr, "failed to arm SIGALRM"), exit(1);
  1126. alarm(ALARM_INTERVAL_SECS);
  1127. set_test_type(argv[1]);
  1128. nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  1129. nr_pages_per_cpu = atol(argv[2]) * 1024*1024 / page_size /
  1130. nr_cpus;
  1131. if (!nr_pages_per_cpu) {
  1132. fprintf(stderr, "invalid MiB\n");
  1133. fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
  1134. }
  1135. bounces = atoi(argv[3]);
  1136. if (bounces <= 0) {
  1137. fprintf(stderr, "invalid bounces\n");
  1138. fprintf(stderr, "Usage: <MiB> <bounces>\n"), exit(1);
  1139. }
  1140. nr_pages = nr_pages_per_cpu * nr_cpus;
  1141. if (test_type == TEST_HUGETLB) {
  1142. if (argc < 5)
  1143. fprintf(stderr, "Usage: hugetlb <MiB> <bounces> <hugetlbfs_file>\n"),
  1144. exit(1);
  1145. huge_fd = open(argv[4], O_CREAT | O_RDWR, 0755);
  1146. if (huge_fd < 0) {
  1147. fprintf(stderr, "Open of %s failed", argv[3]);
  1148. perror("open");
  1149. exit(1);
  1150. }
  1151. if (ftruncate(huge_fd, 0)) {
  1152. fprintf(stderr, "ftruncate %s to size 0 failed", argv[3]);
  1153. perror("ftruncate");
  1154. exit(1);
  1155. }
  1156. }
  1157. printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
  1158. nr_pages, nr_pages_per_cpu);
  1159. return userfaultfd_stress();
  1160. }
  1161. #else /* __NR_userfaultfd */
  1162. #warning "missing __NR_userfaultfd definition"
  1163. int main(void)
  1164. {
  1165. printf("skip: Skipping userfaultfd test (missing __NR_userfaultfd)\n");
  1166. return KSFT_SKIP;
  1167. }
  1168. #endif /* __NR_userfaultfd */