poll-cancel-all.c 9.5 KB

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