userfaultfd.c 34 KB

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