poll-cancel-all.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: Test IORING_ASYNC_CANCEL_{ALL,FD}
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <poll.h>
  13. #include "liburing.h"
  14. static int no_cancel_flags;
  15. static int test1(struct io_uring *ring, int *fd)
  16. {
  17. struct io_uring_sqe *sqe;
  18. struct io_uring_cqe *cqe;
  19. int ret, i;
  20. for (i = 0; i < 8; i++) {
  21. sqe = io_uring_get_sqe(ring);
  22. if (!sqe) {
  23. fprintf(stderr, "get sqe failed\n");
  24. return 1;
  25. }
  26. io_uring_prep_poll_add(sqe, fd[0], POLLIN);
  27. sqe->user_data = i + 1;
  28. }
  29. ret = io_uring_submit(ring);
  30. if (ret < 8) {
  31. fprintf(stderr, "sqe submit failed: %d\n", ret);
  32. return 1;
  33. }
  34. sqe = io_uring_get_sqe(ring);
  35. if (!sqe) {
  36. fprintf(stderr, "get sqe failed\n");
  37. return 1;
  38. }
  39. /*
  40. * Mark CANCEL_ALL to cancel all matching the key, and use
  41. * CANCEL_FD to cancel requests matching the specified fd.
  42. * This should cancel all the pending poll requests on the pipe
  43. * input.
  44. */
  45. io_uring_prep_cancel(sqe, 0, IORING_ASYNC_CANCEL_ALL);
  46. sqe->cancel_flags |= IORING_ASYNC_CANCEL_FD;
  47. sqe->fd = fd[0];
  48. sqe->user_data = 100;
  49. ret = io_uring_submit(ring);
  50. if (ret < 1) {
  51. fprintf(stderr, "child: sqe submit failed: %d\n", ret);
  52. return 1;
  53. }
  54. for (i = 0; i < 9; i++) {
  55. if (no_cancel_flags)
  56. break;
  57. ret = io_uring_wait_cqe(ring, &cqe);
  58. if (ret) {
  59. fprintf(stderr, "wait=%d\n", ret);
  60. return 1;
  61. }
  62. switch (cqe->user_data) {
  63. case 100:
  64. if (cqe->res == -EINVAL) {
  65. no_cancel_flags = 1;
  66. break;
  67. }
  68. if (cqe->res != 8) {
  69. fprintf(stderr, "canceled %d\n", cqe->res);
  70. return 1;
  71. }
  72. break;
  73. case 1 ... 8:
  74. if (cqe->res != -ECANCELED) {
  75. fprintf(stderr, "poll res %d\n", cqe->res);
  76. return 1;
  77. }
  78. break;
  79. default:
  80. fprintf(stderr, "invalid user_data %lu\n",
  81. (unsigned long) cqe->user_data);
  82. return 1;
  83. }
  84. io_uring_cqe_seen(ring, cqe);
  85. }
  86. return 0;
  87. }
  88. static int test2(struct io_uring *ring, int *fd)
  89. {
  90. struct io_uring_sqe *sqe;
  91. struct io_uring_cqe *cqe;
  92. int ret, i, fd2[2];
  93. if (pipe(fd2) < 0) {
  94. perror("pipe");
  95. return 1;
  96. }
  97. for (i = 0; i < 8; i++) {
  98. sqe = io_uring_get_sqe(ring);
  99. if (!sqe) {
  100. fprintf(stderr, "get sqe failed\n");
  101. goto err;
  102. }
  103. if (!(i & 1))
  104. io_uring_prep_poll_add(sqe, fd[0], POLLIN);
  105. else
  106. io_uring_prep_poll_add(sqe, fd2[0], POLLIN);
  107. sqe->user_data = i & 1;
  108. }
  109. ret = io_uring_submit(ring);
  110. if (ret < 8) {
  111. fprintf(stderr, "sqe submit failed: %d\n", ret);
  112. goto err;
  113. }
  114. sqe = io_uring_get_sqe(ring);
  115. if (!sqe) {
  116. fprintf(stderr, "get sqe failed\n");
  117. goto err;
  118. }
  119. /*
  120. * Mark CANCEL_ALL to cancel all matching the key, and use
  121. * CANCEL_FD to cancel requests matching the specified fd.
  122. * This should cancel all the pending poll requests on the pipe
  123. * input.
  124. */
  125. io_uring_prep_cancel(sqe, 0, IORING_ASYNC_CANCEL_ALL);
  126. sqe->cancel_flags |= IORING_ASYNC_CANCEL_FD;
  127. sqe->fd = fd[0];
  128. sqe->user_data = 100;
  129. ret = io_uring_submit(ring);
  130. if (ret < 1) {
  131. fprintf(stderr, "sqe submit failed: %d\n", ret);
  132. goto err;
  133. }
  134. for (i = 0; i < 5; i++) {
  135. ret = io_uring_wait_cqe(ring, &cqe);
  136. if (ret) {
  137. fprintf(stderr, "wait=%d\n", ret);
  138. goto err;
  139. }
  140. switch (cqe->user_data) {
  141. case 100:
  142. if (cqe->res != 4) {
  143. fprintf(stderr, "canceled %d\n", cqe->res);
  144. goto err;
  145. }
  146. break;
  147. case 0:
  148. if (cqe->res != -ECANCELED) {
  149. fprintf(stderr, "poll res %d\n", cqe->res);
  150. goto err;
  151. }
  152. break;
  153. default:
  154. fprintf(stderr, "invalid user_data %lu\n",
  155. (unsigned long) cqe->user_data);
  156. goto err;
  157. }
  158. io_uring_cqe_seen(ring, cqe);
  159. }
  160. usleep(1000);
  161. /*
  162. * Should not have any pending CQEs now
  163. */
  164. ret = io_uring_peek_cqe(ring, &cqe);
  165. if (!ret) {
  166. fprintf(stderr, "Unexpected extra cancel cqe\n");
  167. goto err;
  168. }
  169. sqe = io_uring_get_sqe(ring);
  170. if (!sqe) {
  171. fprintf(stderr, "get sqe failed\n");
  172. goto err;
  173. }
  174. /*
  175. * Mark CANCEL_ALL to cancel all matching the key, and use
  176. * CANCEL_FD to cancel requests matching the specified fd.
  177. * This should cancel all the pending poll requests on the pipe
  178. * input.
  179. */
  180. io_uring_prep_cancel(sqe, 0, IORING_ASYNC_CANCEL_ALL);
  181. sqe->cancel_flags |= IORING_ASYNC_CANCEL_FD;
  182. sqe->fd = fd2[0];
  183. sqe->user_data = 100;
  184. ret = io_uring_submit(ring);
  185. if (ret < 1) {
  186. fprintf(stderr, "sqe submit failed: %d\n", ret);
  187. goto err;
  188. }
  189. for (i = 0; i < 5; i++) {
  190. ret = io_uring_wait_cqe(ring, &cqe);
  191. if (ret) {
  192. fprintf(stderr, "wait=%d\n", ret);
  193. goto err;
  194. }
  195. switch (cqe->user_data) {
  196. case 100:
  197. if (cqe->res != 4) {
  198. fprintf(stderr, "canceled %d\n", cqe->res);
  199. goto err;
  200. }
  201. break;
  202. case 1:
  203. if (cqe->res != -ECANCELED) {
  204. fprintf(stderr, "poll res %d\n", cqe->res);
  205. goto err;
  206. }
  207. break;
  208. default:
  209. fprintf(stderr, "invalid user_data %lu\n",
  210. (unsigned long) cqe->user_data);
  211. goto err;
  212. }
  213. io_uring_cqe_seen(ring, cqe);
  214. }
  215. close(fd2[0]);
  216. close(fd2[1]);
  217. return 0;
  218. err:
  219. close(fd2[0]);
  220. close(fd2[1]);
  221. return 1;
  222. }
  223. static int test3(struct io_uring *ring, int *fd)
  224. {
  225. struct io_uring_sqe *sqe;
  226. struct io_uring_cqe *cqe;
  227. int ret, i, fd2[2];
  228. if (pipe(fd2) < 0) {
  229. perror("pipe");
  230. return 1;
  231. }
  232. for (i = 0; i < 8; i++) {
  233. sqe = io_uring_get_sqe(ring);
  234. if (!sqe) {
  235. fprintf(stderr, "get sqe failed\n");
  236. goto err;
  237. }
  238. if (!(i & 1)) {
  239. io_uring_prep_poll_add(sqe, fd[0], POLLIN);
  240. sqe->flags |= IOSQE_ASYNC;
  241. } else
  242. io_uring_prep_poll_add(sqe, fd2[0], POLLIN);
  243. sqe->user_data = i & 1;
  244. }
  245. ret = io_uring_submit(ring);
  246. if (ret < 8) {
  247. fprintf(stderr, "child: sqe submit failed: %d\n", ret);
  248. goto err;
  249. }
  250. usleep(10000);
  251. sqe = io_uring_get_sqe(ring);
  252. if (!sqe) {
  253. fprintf(stderr, "get sqe failed\n");
  254. goto err;
  255. }
  256. /*
  257. * Mark CANCEL_ALL to cancel all matching the key, and use
  258. * CANCEL_FD to cancel requests matching the specified fd.
  259. * This should cancel all the pending poll requests on the pipe
  260. * input.
  261. */
  262. io_uring_prep_cancel(sqe, 0, IORING_ASYNC_CANCEL_ALL);
  263. sqe->cancel_flags |= IORING_ASYNC_CANCEL_ANY;
  264. sqe->fd = 0;
  265. sqe->user_data = 100;
  266. ret = io_uring_submit(ring);
  267. if (ret < 1) {
  268. fprintf(stderr, "child: sqe submit failed: %d\n", ret);
  269. goto err;
  270. }
  271. for (i = 0; i < 9; i++) {
  272. ret = io_uring_wait_cqe(ring, &cqe);
  273. if (ret) {
  274. fprintf(stderr, "wait=%d\n", ret);
  275. goto err;
  276. }
  277. switch (cqe->user_data) {
  278. case 100:
  279. if (cqe->res != 8) {
  280. fprintf(stderr, "canceled %d\n", cqe->res);
  281. goto err;
  282. }
  283. break;
  284. case 0:
  285. case 1:
  286. if (cqe->res != -ECANCELED) {
  287. fprintf(stderr, "poll res %d\n", cqe->res);
  288. goto err;
  289. }
  290. break;
  291. default:
  292. fprintf(stderr, "invalid user_data %lu\n",
  293. (unsigned long) cqe->user_data);
  294. goto err;
  295. }
  296. io_uring_cqe_seen(ring, cqe);
  297. }
  298. close(fd2[0]);
  299. close(fd2[1]);
  300. return 0;
  301. err:
  302. close(fd2[0]);
  303. close(fd2[1]);
  304. return 1;
  305. }
  306. static int test4(struct io_uring *ring, int *fd)
  307. {
  308. struct io_uring_sqe *sqe;
  309. struct io_uring_cqe *cqe;
  310. char buffer[32];
  311. int ret, i;
  312. for (i = 0; i < 8; i++) {
  313. sqe = io_uring_get_sqe(ring);
  314. if (!sqe) {
  315. fprintf(stderr, "get sqe failed\n");
  316. goto err;
  317. }
  318. io_uring_prep_read(sqe, fd[0], &buffer, sizeof(buffer), 0);
  319. sqe->flags |= IOSQE_ASYNC;
  320. sqe->user_data = i + 1;
  321. }
  322. ret = io_uring_submit(ring);
  323. if (ret < 8) {
  324. fprintf(stderr, "child: sqe submit failed: %d\n", ret);
  325. goto err;
  326. }
  327. usleep(10000);
  328. sqe = io_uring_get_sqe(ring);
  329. if (!sqe) {
  330. fprintf(stderr, "get sqe failed\n");
  331. goto err;
  332. }
  333. /*
  334. * Mark CANCEL_ALL to cancel all matching the key, and use
  335. * CANCEL_FD to cancel requests matching the specified fd.
  336. * This should cancel all the pending poll requests on the pipe
  337. * input.
  338. */
  339. io_uring_prep_cancel(sqe, 0, IORING_ASYNC_CANCEL_ALL);
  340. sqe->cancel_flags |= IORING_ASYNC_CANCEL_ANY;
  341. sqe->fd = 0;
  342. sqe->user_data = 100;
  343. ret = io_uring_submit(ring);
  344. if (ret < 1) {
  345. fprintf(stderr, "child: sqe submit failed: %d\n", ret);
  346. goto err;
  347. }
  348. for (i = 0; i < 9; i++) {
  349. ret = io_uring_wait_cqe(ring, &cqe);
  350. if (ret) {
  351. fprintf(stderr, "wait=%d\n", ret);
  352. goto err;
  353. }
  354. switch (cqe->user_data) {
  355. case 100:
  356. if (cqe->res != 8) {
  357. fprintf(stderr, "canceled %d\n", cqe->res);
  358. goto err;
  359. }
  360. break;
  361. case 1 ... 8:
  362. if (cqe->res != -ECANCELED) {
  363. fprintf(stderr, "poll res %d\n", cqe->res);
  364. goto err;
  365. }
  366. break;
  367. default:
  368. fprintf(stderr, "invalid user_data %lu\n",
  369. (unsigned long) cqe->user_data);
  370. goto err;
  371. }
  372. io_uring_cqe_seen(ring, cqe);
  373. }
  374. return 0;
  375. err:
  376. return 1;
  377. }
  378. int main(int argc, char *argv[])
  379. {
  380. struct io_uring ring;
  381. int ret, fd[2];
  382. if (argc > 1)
  383. return 0;
  384. if (pipe(fd) < 0) {
  385. perror("pipe");
  386. return 1;
  387. }
  388. ret = io_uring_queue_init(8, &ring, 0);
  389. if (ret) {
  390. fprintf(stderr, "ring setup failed: %d\n", ret);
  391. return 1;
  392. }
  393. ret = test1(&ring, fd);
  394. if (ret) {
  395. fprintf(stderr, "test1 failed\n");
  396. return ret;
  397. }
  398. if (no_cancel_flags)
  399. return 0;
  400. ret = test2(&ring, fd);
  401. if (ret) {
  402. fprintf(stderr, "test2 failed\n");
  403. return ret;
  404. }
  405. ret = test3(&ring, fd);
  406. if (ret) {
  407. fprintf(stderr, "test3 failed\n");
  408. return ret;
  409. }
  410. ret = test4(&ring, fd);
  411. if (ret) {
  412. fprintf(stderr, "test4 failed\n");
  413. return ret;
  414. }
  415. return 0;
  416. }