userfaultfd.c 27 KB

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