sync-cancel.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test io_uring_register_sync_cancel()
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include "liburing.h"
  14. #include "helpers.h"
  15. static int no_sync_cancel, no_sync_cancel_op;
  16. static int test_sync_cancel_timeout(struct io_uring *ring, int async, int by_op)
  17. {
  18. struct io_uring_sync_cancel_reg reg = { };
  19. struct io_uring_sqe *sqe;
  20. struct io_uring_cqe *cqe;
  21. int ret, fds[2], to_prep;
  22. char buf[32];
  23. if (pipe(fds) < 0) {
  24. perror("pipe");
  25. return 1;
  26. }
  27. to_prep = 1;
  28. sqe = io_uring_get_sqe(ring);
  29. io_uring_prep_read(sqe, fds[0], buf, sizeof(buf), 0);
  30. sqe->user_data = 0x89;
  31. if (async)
  32. sqe->flags |= IOSQE_ASYNC;
  33. ret = io_uring_submit(ring);
  34. if (ret != to_prep) {
  35. fprintf(stderr, "submit=%d\n", ret);
  36. return 1;
  37. }
  38. usleep(10000);
  39. reg.flags = IORING_ASYNC_CANCEL_OP;
  40. reg.opcode = IORING_OP_READ;
  41. reg.timeout.tv_nsec = 1;
  42. ret = io_uring_register_sync_cancel(ring, &reg);
  43. /* earlier kernels had sync cancel, but not per-op */
  44. if (ret == -EINVAL) {
  45. no_sync_cancel_op = 1;
  46. return 0;
  47. }
  48. if (async) {
  49. /* we expect -ETIME here, but can race and get 0 */
  50. if (ret != -ETIME && ret != 0) {
  51. fprintf(stderr, "sync_cancel=%d\n", ret);
  52. return 1;
  53. }
  54. } else {
  55. if (ret < 0) {
  56. fprintf(stderr, "sync_cancel=%d\n", ret);
  57. return 1;
  58. }
  59. }
  60. /*
  61. * we could _almost_ use peek_cqe() here, but there is still
  62. * a small gap where io-wq is done with the request and on
  63. * its way to posting a completion, but hasn't done it just
  64. * yet. the request is canceled and won't be doing any IO
  65. * to buffers etc, but the cqe may not have quite arrived yet.
  66. */
  67. ret = io_uring_wait_cqe(ring, &cqe);
  68. if (ret) {
  69. fprintf(stderr, "peek=%d\n", ret);
  70. return 1;
  71. }
  72. if (cqe->res >= 0) {
  73. fprintf(stderr, "cqe->res=%d\n", cqe->res);
  74. return 1;
  75. }
  76. io_uring_cqe_seen(ring, cqe);
  77. return 0;
  78. }
  79. static int test_sync_cancel(struct io_uring *ring, int async, int nr_all,
  80. int use_fd, int by_op)
  81. {
  82. struct io_uring_sync_cancel_reg reg = { };
  83. struct io_uring_sqe *sqe;
  84. struct io_uring_cqe *cqe;
  85. int ret, fds[2], to_prep, i;
  86. char buf[32];
  87. if (pipe(fds) < 0) {
  88. perror("pipe");
  89. return 1;
  90. }
  91. to_prep = 1;
  92. if (nr_all)
  93. to_prep = 4;
  94. for (i = 0; i < to_prep; i++) {
  95. sqe = io_uring_get_sqe(ring);
  96. io_uring_prep_read(sqe, fds[0], buf, sizeof(buf), 0);
  97. sqe->user_data = 0x89;
  98. if (async)
  99. sqe->flags |= IOSQE_ASYNC;
  100. }
  101. ret = io_uring_submit(ring);
  102. if (ret != to_prep) {
  103. fprintf(stderr, "submit=%d\n", ret);
  104. return 1;
  105. }
  106. usleep(10000);
  107. if (!use_fd)
  108. reg.addr = 0x89;
  109. else
  110. reg.fd = fds[0];
  111. reg.timeout.tv_sec = 200;
  112. if (nr_all)
  113. reg.flags |= IORING_ASYNC_CANCEL_ALL;
  114. if (use_fd)
  115. reg.flags |= IORING_ASYNC_CANCEL_FD;
  116. ret = io_uring_register_sync_cancel(ring, &reg);
  117. if (ret < 0) {
  118. if (ret == -EINVAL && !no_sync_cancel) {
  119. no_sync_cancel = 1;
  120. return 0;
  121. }
  122. fprintf(stderr, "sync_cancel=%d\n", ret);
  123. return 1;
  124. }
  125. for (i = 0; i < to_prep; i++) {
  126. /*
  127. * we could _almost_ use peek_cqe() here, but there is still
  128. * a small gap where io-wq is done with the request and on
  129. * its way to posting a completion, but hasn't done it just
  130. * yet. the request is canceled and won't be doing any IO
  131. * to buffers etc, but the cqe may not have quite arrived yet.
  132. */
  133. ret = io_uring_wait_cqe(ring, &cqe);
  134. if (ret) {
  135. fprintf(stderr, "peek=%d\n", ret);
  136. return 1;
  137. }
  138. if (cqe->res >= 0) {
  139. fprintf(stderr, "cqe->res=%d\n", cqe->res);
  140. return 1;
  141. }
  142. io_uring_cqe_seen(ring, cqe);
  143. }
  144. return 0;
  145. }
  146. int main(int argc, char *argv[])
  147. {
  148. struct io_uring ring;
  149. int ret;
  150. if (argc > 1)
  151. return T_EXIT_SKIP;
  152. ret = t_create_ring(7, &ring, 0);
  153. if (ret == T_SETUP_SKIP)
  154. return T_EXIT_SKIP;
  155. else if (ret != T_SETUP_OK)
  156. return ret;
  157. ret = test_sync_cancel(&ring, 0, 0, 0, 0);
  158. if (ret) {
  159. fprintf(stderr, "test_sync_cancel 0 0 0 failed\n");
  160. return T_EXIT_FAIL;
  161. }
  162. if (no_sync_cancel)
  163. return T_EXIT_SKIP;
  164. ret = test_sync_cancel(&ring, 0, 0, 0, 1);
  165. if (ret) {
  166. fprintf(stderr, "test_sync_cancel 0 0 1 failed\n");
  167. return T_EXIT_FAIL;
  168. }
  169. ret = test_sync_cancel(&ring, 1, 0, 0, 0);
  170. if (ret) {
  171. fprintf(stderr, "test_sync_cancel 1 0 0 0 failed\n");
  172. return T_EXIT_FAIL;
  173. }
  174. ret = test_sync_cancel(&ring, 1, 0, 0, 1);
  175. if (ret) {
  176. fprintf(stderr, "test_sync_cancel 1 0 0 1 failed\n");
  177. return T_EXIT_FAIL;
  178. }
  179. ret = test_sync_cancel(&ring, 0, 1, 0, 0);
  180. if (ret) {
  181. fprintf(stderr, "test_sync_cancel 0 1 0 0 failed\n");
  182. return T_EXIT_FAIL;
  183. }
  184. ret = test_sync_cancel(&ring, 0, 1, 0, 1);
  185. if (ret) {
  186. fprintf(stderr, "test_sync_cancel 0 1 0 1 failed\n");
  187. return T_EXIT_FAIL;
  188. }
  189. ret = test_sync_cancel(&ring, 1, 1, 0, 0);
  190. if (ret) {
  191. fprintf(stderr, "test_sync_cancel 1 1 0 0 failed\n");
  192. return T_EXIT_FAIL;
  193. }
  194. ret = test_sync_cancel(&ring, 0, 0, 1, 0);
  195. if (ret) {
  196. fprintf(stderr, "test_sync_cancel 0 0 1 0 failed\n");
  197. return T_EXIT_FAIL;
  198. }
  199. ret = test_sync_cancel(&ring, 1, 0, 1, 0);
  200. if (ret) {
  201. fprintf(stderr, "test_sync_cancel 1 0 1 0 failed\n");
  202. return T_EXIT_FAIL;
  203. }
  204. ret = test_sync_cancel(&ring, 0, 1, 1, 0);
  205. if (ret) {
  206. fprintf(stderr, "test_sync_cancel 0 1 1 0 failed\n");
  207. return T_EXIT_FAIL;
  208. }
  209. ret = test_sync_cancel(&ring, 1, 1, 1, 0);
  210. if (ret) {
  211. fprintf(stderr, "test_sync_cancel 1 1 1 0 failed\n");
  212. return T_EXIT_FAIL;
  213. }
  214. ret = test_sync_cancel_timeout(&ring, 0, 0);
  215. if (ret) {
  216. fprintf(stderr, "test_sync_cancel_timeout 0 0\n");
  217. return T_EXIT_FAIL;
  218. }
  219. if (no_sync_cancel_op)
  220. return T_EXIT_PASS;
  221. ret = test_sync_cancel_timeout(&ring, 0, 1);
  222. if (ret) {
  223. fprintf(stderr, "test_sync_cancel_timeout 0 1\n");
  224. return T_EXIT_FAIL;
  225. }
  226. /* must be last, leaves request */
  227. ret = test_sync_cancel_timeout(&ring, 1, 0);
  228. if (ret) {
  229. fprintf(stderr, "test_sync_cancel_timeout 1\n");
  230. return T_EXIT_FAIL;
  231. }
  232. return T_EXIT_PASS;
  233. }