submit-reuse.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 int test_reuse(int argc, char *argv[], int split, int async)
  89. {
  90. struct thread_data data;
  91. struct io_uring_params p = { };
  92. int fd1, fd2, ret, i;
  93. struct timeval tv;
  94. pthread_t thread;
  95. char *fname1 = ".reuse.1";
  96. int do_unlink = 1;
  97. void *tret;
  98. ret = io_uring_queue_init_params(32, &ring, &p);
  99. if (ret) {
  100. fprintf(stderr, "io_uring_queue_init: %d\n", ret);
  101. return 1;
  102. }
  103. if (!(p.features & IORING_FEAT_SUBMIT_STABLE)) {
  104. fprintf(stdout, "FEAT_SUBMIT_STABLE not there, skipping\n");
  105. io_uring_queue_exit(&ring);
  106. no_stable = 1;
  107. return 0;
  108. }
  109. if (argc > 1) {
  110. fname1 = argv[1];
  111. do_unlink = 0;
  112. } else {
  113. t_create_file(fname1, FILE_SIZE);
  114. }
  115. fd1 = open(fname1, O_RDONLY);
  116. if (do_unlink)
  117. unlink(fname1);
  118. if (fd1 < 0) {
  119. if (errno == EPERM || errno == EACCES)
  120. return T_EXIT_SKIP;
  121. perror("open fname1");
  122. goto err;
  123. }
  124. t_create_file(".reuse.2", FILE_SIZE);
  125. fd2 = open(".reuse.2", O_RDONLY);
  126. unlink(".reuse.2");
  127. if (fd2 < 0) {
  128. perror("open .reuse.2");
  129. goto err;
  130. }
  131. data.fd1 = fd1;
  132. data.fd2 = fd2;
  133. data.do_exit = 0;
  134. pthread_create(&thread, NULL, flusher, &data);
  135. usleep(10000);
  136. gettimeofday(&tv, NULL);
  137. for (i = 0; i < 1000; i++) {
  138. ret = prep(fd1, str1, split, async);
  139. if (ret) {
  140. fprintf(stderr, "prep1 failed: %d\n", ret);
  141. goto err;
  142. }
  143. ret = prep(fd2, str2, split, async);
  144. if (ret) {
  145. fprintf(stderr, "prep1 failed: %d\n", ret);
  146. goto err;
  147. }
  148. ret = wait_nr(2);
  149. if (ret) {
  150. fprintf(stderr, "wait_nr: %d\n", ret);
  151. goto err;
  152. }
  153. if (mtime_since_now(&tv) > 5000)
  154. break;
  155. }
  156. data.do_exit = 1;
  157. pthread_join(thread, &tret);
  158. close(fd2);
  159. close(fd1);
  160. io_uring_queue_exit(&ring);
  161. return 0;
  162. err:
  163. io_uring_queue_exit(&ring);
  164. return 1;
  165. }
  166. int main(int argc, char *argv[])
  167. {
  168. int ret, i;
  169. for (i = 0; i < 4; i++) {
  170. int split, async;
  171. split = (i & 1) != 0;
  172. async = (i & 2) != 0;
  173. ret = test_reuse(argc, argv, split, async);
  174. if (ret == T_EXIT_SKIP)
  175. continue;
  176. if (ret) {
  177. fprintf(stderr, "test_reuse %d %d failed\n", split, async);
  178. return ret;
  179. }
  180. if (no_stable)
  181. break;
  182. }
  183. return 0;
  184. }