sync-cancel.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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;
  16. static int test_sync_cancel_timeout(struct io_uring *ring, int async)
  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.addr = 0x89;
  40. reg.timeout.tv_nsec = 1;
  41. ret = io_uring_register_sync_cancel(ring, &reg);
  42. if (async) {
  43. /* we expect -ETIME here, but can race and get 0 */
  44. if (ret != -ETIME && ret != 0) {
  45. fprintf(stderr, "sync_cancel=%d\n", ret);
  46. return 1;
  47. }
  48. } else {
  49. if (ret < 0) {
  50. fprintf(stderr, "sync_cancel=%d\n", ret);
  51. return 1;
  52. }
  53. }
  54. /*
  55. * we could _almost_ use peek_cqe() here, but there is still
  56. * a small gap where io-wq is done with the request and on
  57. * its way to posting a completion, but hasn't done it just
  58. * yet. the request is canceled and won't be doing any IO
  59. * to buffers etc, but the cqe may not have quite arrived yet.
  60. */
  61. ret = io_uring_wait_cqe(ring, &cqe);
  62. if (ret) {
  63. fprintf(stderr, "peek=%d\n", ret);
  64. return 1;
  65. }
  66. if (cqe->res >= 0) {
  67. fprintf(stderr, "cqe->res=%d\n", cqe->res);
  68. return 1;
  69. }
  70. io_uring_cqe_seen(ring, cqe);
  71. return 0;
  72. }
  73. static int test_sync_cancel(struct io_uring *ring, int async, int nr_all,
  74. int use_fd)
  75. {
  76. struct io_uring_sync_cancel_reg reg = { };
  77. struct io_uring_sqe *sqe;
  78. struct io_uring_cqe *cqe;
  79. int ret, fds[2], to_prep, i;
  80. char buf[32];
  81. if (pipe(fds) < 0) {
  82. perror("pipe");
  83. return 1;
  84. }
  85. to_prep = 1;
  86. if (nr_all)
  87. to_prep = 4;
  88. for (i = 0; i < to_prep; i++) {
  89. sqe = io_uring_get_sqe(ring);
  90. io_uring_prep_read(sqe, fds[0], buf, sizeof(buf), 0);
  91. sqe->user_data = 0x89;
  92. if (async)
  93. sqe->flags |= IOSQE_ASYNC;
  94. }
  95. ret = io_uring_submit(ring);
  96. if (ret != to_prep) {
  97. fprintf(stderr, "submit=%d\n", ret);
  98. return 1;
  99. }
  100. usleep(10000);
  101. if (!use_fd)
  102. reg.addr = 0x89;
  103. else
  104. reg.fd = fds[0];
  105. reg.timeout.tv_sec = 200;
  106. if (nr_all)
  107. reg.flags |= IORING_ASYNC_CANCEL_ALL;
  108. if (use_fd)
  109. reg.flags |= IORING_ASYNC_CANCEL_FD;
  110. ret = io_uring_register_sync_cancel(ring, &reg);
  111. if (ret < 0) {
  112. if (ret == -EINVAL && !no_sync_cancel) {
  113. no_sync_cancel = 1;
  114. return 0;
  115. }
  116. fprintf(stderr, "sync_cancel=%d\n", ret);
  117. return 1;
  118. }
  119. for (i = 0; i < to_prep; i++) {
  120. /*
  121. * we could _almost_ use peek_cqe() here, but there is still
  122. * a small gap where io-wq is done with the request and on
  123. * its way to posting a completion, but hasn't done it just
  124. * yet. the request is canceled and won't be doing any IO
  125. * to buffers etc, but the cqe may not have quite arrived yet.
  126. */
  127. ret = io_uring_wait_cqe(ring, &cqe);
  128. if (ret) {
  129. fprintf(stderr, "peek=%d\n", ret);
  130. return 1;
  131. }
  132. if (cqe->res >= 0) {
  133. fprintf(stderr, "cqe->res=%d\n", cqe->res);
  134. return 1;
  135. }
  136. io_uring_cqe_seen(ring, cqe);
  137. }
  138. return 0;
  139. }
  140. int main(int argc, char *argv[])
  141. {
  142. struct io_uring ring;
  143. int ret;
  144. if (argc > 1)
  145. return T_EXIT_SKIP;
  146. ret = t_create_ring(7, &ring, 0);
  147. if (ret == T_SETUP_SKIP)
  148. return T_EXIT_SKIP;
  149. else if (ret != T_SETUP_OK)
  150. return ret;
  151. ret = test_sync_cancel(&ring, 0, 0, 0);
  152. if (ret) {
  153. fprintf(stderr, "test_sync_cancel 0 0 0 failed\n");
  154. return T_EXIT_FAIL;
  155. }
  156. if (no_sync_cancel)
  157. return T_EXIT_SKIP;
  158. ret = test_sync_cancel(&ring, 1, 0, 0);
  159. if (ret) {
  160. fprintf(stderr, "test_sync_cancel 1 0 0 failed\n");
  161. return T_EXIT_FAIL;
  162. }
  163. ret = test_sync_cancel(&ring, 0, 1, 0);
  164. if (ret) {
  165. fprintf(stderr, "test_sync_cancel 0 1 0 failed\n");
  166. return T_EXIT_FAIL;
  167. }
  168. ret = test_sync_cancel(&ring, 1, 1, 0);
  169. if (ret) {
  170. fprintf(stderr, "test_sync_cancel 1 1 0 failed\n");
  171. return T_EXIT_FAIL;
  172. }
  173. ret = test_sync_cancel(&ring, 0, 0, 1);
  174. if (ret) {
  175. fprintf(stderr, "test_sync_cancel 0 0 1 failed\n");
  176. return T_EXIT_FAIL;
  177. }
  178. ret = test_sync_cancel(&ring, 1, 0, 1);
  179. if (ret) {
  180. fprintf(stderr, "test_sync_cancel 1 0 1 failed\n");
  181. return T_EXIT_FAIL;
  182. }
  183. ret = test_sync_cancel(&ring, 0, 1, 1);
  184. if (ret) {
  185. fprintf(stderr, "test_sync_cancel 0 1 1 failed\n");
  186. return T_EXIT_FAIL;
  187. }
  188. ret = test_sync_cancel(&ring, 1, 1, 1);
  189. if (ret) {
  190. fprintf(stderr, "test_sync_cancel 1 1 1 failed\n");
  191. return T_EXIT_FAIL;
  192. }
  193. ret = test_sync_cancel_timeout(&ring, 0);
  194. if (ret) {
  195. fprintf(stderr, "test_sync_cancel_timeout 0\n");
  196. return T_EXIT_FAIL;
  197. }
  198. /* must be last, leaves request */
  199. ret = test_sync_cancel_timeout(&ring, 1);
  200. if (ret) {
  201. fprintf(stderr, "test_sync_cancel_timeout 1\n");
  202. return T_EXIT_FAIL;
  203. }
  204. return T_EXIT_PASS;
  205. }