submit-reuse.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Test reads that will punt to blocking context, with immediate overwrite
  5. * of iovec->iov_base to NULL. If the kernel doesn't properly handle
  6. * reuse of the iovec, we should get -EFAULT.
  7. */
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <pthread.h>
  14. #include <sys/time.h>
  15. #include "helpers.h"
  16. #include "liburing.h"
  17. #define STR_SIZE 32768
  18. #define FILE_SIZE 65536
  19. struct thread_data {
  20. int fd1, fd2;
  21. volatile int do_exit;
  22. };
  23. static void *flusher(void *__data)
  24. {
  25. struct thread_data *data = __data;
  26. while (!data->do_exit) {
  27. posix_fadvise(data->fd1, 0, FILE_SIZE, POSIX_FADV_DONTNEED);
  28. posix_fadvise(data->fd2, 0, FILE_SIZE, POSIX_FADV_DONTNEED);
  29. usleep(10);
  30. }
  31. return NULL;
  32. }
  33. static char str1[STR_SIZE];
  34. static char str2[STR_SIZE];
  35. static struct io_uring ring;
  36. static int no_stable;
  37. static int prep(int fd, char *str, int split, int async)
  38. {
  39. struct io_uring_sqe *sqe;
  40. struct iovec iovs[16];
  41. int ret, i;
  42. if (split) {
  43. int vsize = STR_SIZE / 16;
  44. void *ptr = str;
  45. for (i = 0; i < 16; i++) {
  46. iovs[i].iov_base = ptr;
  47. iovs[i].iov_len = vsize;
  48. ptr += vsize;
  49. }
  50. } else {
  51. iovs[0].iov_base = str;
  52. iovs[0].iov_len = STR_SIZE;
  53. }
  54. sqe = io_uring_get_sqe(&ring);
  55. io_uring_prep_readv(sqe, fd, iovs, split ? 16 : 1, 0);
  56. sqe->user_data = fd;
  57. if (async)
  58. sqe->flags = IOSQE_ASYNC;
  59. ret = io_uring_submit(&ring);
  60. if (ret != 1) {
  61. fprintf(stderr, "submit got %d\n", ret);
  62. return 1;
  63. }
  64. if (split) {
  65. for (i = 0; i < 16; i++)
  66. iovs[i].iov_base = NULL;
  67. } else {
  68. iovs[0].iov_base = NULL;
  69. }
  70. return 0;
  71. }
  72. static int wait_nr(int nr)
  73. {
  74. int i, ret;
  75. for (i = 0; i < nr; i++) {
  76. struct io_uring_cqe *cqe;
  77. ret = io_uring_wait_cqe(&ring, &cqe);
  78. if (ret)
  79. return ret;
  80. if (cqe->res < 0) {
  81. fprintf(stderr, "cqe->res=%d\n", cqe->res);
  82. return 1;
  83. }
  84. io_uring_cqe_seen(&ring, cqe);
  85. }
  86. return 0;
  87. }
  88. static unsigned long long mtime_since(const struct timeval *s,
  89. const struct timeval *e)
  90. {
  91. long long sec, usec;
  92. sec = e->tv_sec - s->tv_sec;
  93. usec = (e->tv_usec - s->tv_usec);
  94. if (sec > 0 && usec < 0) {
  95. sec--;
  96. usec += 1000000;
  97. }
  98. sec *= 1000;
  99. usec /= 1000;
  100. return sec + usec;
  101. }
  102. static unsigned long long mtime_since_now(struct timeval *tv)
  103. {
  104. struct timeval end;
  105. gettimeofday(&end, NULL);
  106. return mtime_since(tv, &end);
  107. }
  108. static int test_reuse(int argc, char *argv[], int split, int async)
  109. {
  110. struct thread_data data;
  111. struct io_uring_params p = { };
  112. int fd1, fd2, ret, i;
  113. struct timeval tv;
  114. pthread_t thread;
  115. char *fname1 = ".reuse.1";
  116. int do_unlink = 1;
  117. void *tret;
  118. ret = io_uring_queue_init_params(32, &ring, &p);
  119. if (ret) {
  120. fprintf(stderr, "io_uring_queue_init: %d\n", ret);
  121. return 1;
  122. }
  123. if (!(p.features & IORING_FEAT_SUBMIT_STABLE)) {
  124. fprintf(stdout, "FEAT_SUBMIT_STABLE not there, skipping\n");
  125. io_uring_queue_exit(&ring);
  126. no_stable = 1;
  127. return 0;
  128. }
  129. if (argc > 1) {
  130. fname1 = argv[1];
  131. do_unlink = 0;
  132. } else {
  133. t_create_file(fname1, FILE_SIZE);
  134. }
  135. fd1 = open(fname1, O_RDONLY);
  136. if (do_unlink)
  137. unlink(fname1);
  138. if (fd1 < 0) {
  139. perror("open fname1");
  140. goto err;
  141. }
  142. t_create_file(".reuse.2", FILE_SIZE);
  143. fd2 = open(".reuse.2", O_RDONLY);
  144. unlink(".reuse.2");
  145. if (fd2 < 0) {
  146. perror("open .reuse.2");
  147. goto err;
  148. }
  149. data.fd1 = fd1;
  150. data.fd2 = fd2;
  151. data.do_exit = 0;
  152. pthread_create(&thread, NULL, flusher, &data);
  153. usleep(10000);
  154. gettimeofday(&tv, NULL);
  155. for (i = 0; i < 1000; i++) {
  156. ret = prep(fd1, str1, split, async);
  157. if (ret) {
  158. fprintf(stderr, "prep1 failed: %d\n", ret);
  159. goto err;
  160. }
  161. ret = prep(fd2, str2, split, async);
  162. if (ret) {
  163. fprintf(stderr, "prep1 failed: %d\n", ret);
  164. goto err;
  165. }
  166. ret = wait_nr(2);
  167. if (ret) {
  168. fprintf(stderr, "wait_nr: %d\n", ret);
  169. goto err;
  170. }
  171. if (mtime_since_now(&tv) > 5000)
  172. break;
  173. }
  174. data.do_exit = 1;
  175. pthread_join(thread, &tret);
  176. close(fd2);
  177. close(fd1);
  178. io_uring_queue_exit(&ring);
  179. return 0;
  180. err:
  181. io_uring_queue_exit(&ring);
  182. return 1;
  183. }
  184. int main(int argc, char *argv[])
  185. {
  186. int ret, i;
  187. for (i = 0; i < 4; i++) {
  188. int split, async;
  189. split = (i & 1) != 0;
  190. async = (i & 2) != 0;
  191. ret = test_reuse(argc, argv, split, async);
  192. if (ret) {
  193. fprintf(stderr, "test_reuse %d %d failed\n", split, async);
  194. return ret;
  195. }
  196. if (no_stable)
  197. break;
  198. }
  199. return 0;
  200. }