poll.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test io_uring poll handling
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <poll.h>
  14. #include <sys/wait.h>
  15. #include <assert.h>
  16. #include "helpers.h"
  17. #include "liburing.h"
  18. static void do_setsockopt(int fd, int level, int optname, int val)
  19. {
  20. if (setsockopt(fd, level, optname, &val, sizeof(val)))
  21. t_error(1, errno, "setsockopt %d.%d: %d", level, optname, val);
  22. }
  23. static bool check_cq_empty(struct io_uring *ring)
  24. {
  25. struct io_uring_cqe *cqe = NULL;
  26. int ret;
  27. ret = io_uring_peek_cqe(ring, &cqe); /* nothing should be there */
  28. return ret == -EAGAIN;
  29. }
  30. static int test_basic(void)
  31. {
  32. struct io_uring_cqe *cqe;
  33. struct io_uring_sqe *sqe;
  34. struct io_uring ring;
  35. int pipe1[2];
  36. pid_t p;
  37. int ret;
  38. if (pipe(pipe1) != 0) {
  39. perror("pipe");
  40. return 1;
  41. }
  42. p = fork();
  43. if (p == -1) {
  44. perror("fork");
  45. exit(2);
  46. } else if (p == 0) {
  47. ret = io_uring_queue_init(1, &ring, 0);
  48. if (ret) {
  49. fprintf(stderr, "child: ring setup failed: %d\n", ret);
  50. return 1;
  51. }
  52. sqe = io_uring_get_sqe(&ring);
  53. if (!sqe) {
  54. fprintf(stderr, "get sqe failed\n");
  55. return 1;
  56. }
  57. io_uring_prep_poll_add(sqe, pipe1[0], POLLIN);
  58. io_uring_sqe_set_data(sqe, sqe);
  59. ret = io_uring_submit(&ring);
  60. if (ret <= 0) {
  61. fprintf(stderr, "child: sqe submit failed: %d\n", ret);
  62. return 1;
  63. }
  64. do {
  65. ret = io_uring_wait_cqe(&ring, &cqe);
  66. if (ret < 0) {
  67. fprintf(stderr, "child: wait completion %d\n", ret);
  68. break;
  69. }
  70. io_uring_cqe_seen(&ring, cqe);
  71. } while (ret != 0);
  72. if (ret < 0)
  73. return 1;
  74. if (cqe->user_data != (unsigned long) sqe) {
  75. fprintf(stderr, "child: cqe doesn't match sqe\n");
  76. return 1;
  77. }
  78. if ((cqe->res & POLLIN) != POLLIN) {
  79. fprintf(stderr, "child: bad return value %ld\n",
  80. (long) cqe->res);
  81. return 1;
  82. }
  83. io_uring_queue_exit(&ring);
  84. exit(0);
  85. }
  86. do {
  87. errno = 0;
  88. ret = write(pipe1[1], "foo", 3);
  89. } while (ret == -1 && errno == EINTR);
  90. if (ret != 3) {
  91. fprintf(stderr, "parent: bad write return %d\n", ret);
  92. return 1;
  93. }
  94. close(pipe1[0]);
  95. close(pipe1[1]);
  96. return 0;
  97. }
  98. static int test_missing_events(void)
  99. {
  100. struct io_uring_cqe *cqe;
  101. struct io_uring_sqe *sqe;
  102. struct io_uring ring;
  103. int i, ret, sp[2];
  104. char buf[2] = {};
  105. int res_mask = 0;
  106. ret = io_uring_queue_init(8, &ring, IORING_SETUP_SINGLE_ISSUER |
  107. IORING_SETUP_DEFER_TASKRUN);
  108. if (ret) {
  109. fprintf(stderr, "ring setup failed: %d\n", ret);
  110. return 1;
  111. }
  112. if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) != 0) {
  113. perror("Failed to create Unix-domain socket pair\n");
  114. return 1;
  115. }
  116. do_setsockopt(sp[0], SOL_SOCKET, SO_SNDBUF, 1);
  117. ret = send(sp[0], buf, sizeof(buf), 0);
  118. if (ret != sizeof(buf)) {
  119. perror("send failed\n");
  120. return 1;
  121. }
  122. sqe = io_uring_get_sqe(&ring);
  123. io_uring_prep_poll_multishot(sqe, sp[0], POLLIN|POLLOUT);
  124. ret = io_uring_submit(&ring);
  125. if (ret != 1) {
  126. fprintf(stderr, "sqe submit failed: %d\n", ret);
  127. return 1;
  128. }
  129. /* trigger EPOLLIN */
  130. ret = send(sp[1], buf, sizeof(buf), 0);
  131. if (ret != sizeof(buf)) {
  132. fprintf(stderr, "send sp[1] failed %i %i\n", ret, errno);
  133. return 1;
  134. }
  135. /* trigger EPOLLOUT */
  136. ret = recv(sp[1], buf, sizeof(buf), 0);
  137. if (ret != sizeof(buf)) {
  138. perror("recv failed\n");
  139. return 1;
  140. }
  141. for (i = 0; ; i++) {
  142. if (i == 0)
  143. ret = io_uring_wait_cqe(&ring, &cqe);
  144. else
  145. ret = io_uring_peek_cqe(&ring, &cqe);
  146. if (i != 0 && ret == -EAGAIN) {
  147. break;
  148. }
  149. if (ret) {
  150. fprintf(stderr, "wait completion %d, %i\n", ret, i);
  151. return 1;
  152. }
  153. res_mask |= cqe->res;
  154. io_uring_cqe_seen(&ring, cqe);
  155. }
  156. if ((res_mask & (POLLIN|POLLOUT)) != (POLLIN|POLLOUT)) {
  157. fprintf(stderr, "missing poll events %i\n", res_mask);
  158. return 1;
  159. }
  160. io_uring_queue_exit(&ring);
  161. close(sp[0]);
  162. close(sp[1]);
  163. return 0;
  164. }
  165. #define NR_SQES 2048
  166. static int test_self_poll(void)
  167. {
  168. struct io_uring_cqe *cqe;
  169. struct io_uring_sqe *sqe;
  170. struct io_uring ring;
  171. int ret, i, j;
  172. ret = io_uring_queue_init(NR_SQES, &ring, 0);
  173. if (ret) {
  174. fprintf(stderr, "ring setup failed: %d\n", ret);
  175. return T_EXIT_FAIL;
  176. }
  177. for (j = 0; j < 32; j++) {
  178. for (i = 0; i < NR_SQES; i++) {
  179. sqe = io_uring_get_sqe(&ring);
  180. io_uring_prep_poll_add(sqe, ring.ring_fd, POLLIN);
  181. }
  182. ret = io_uring_submit(&ring);
  183. assert(ret == NR_SQES);
  184. }
  185. sqe = io_uring_get_sqe(&ring);
  186. io_uring_prep_nop(sqe);
  187. ret = io_uring_submit(&ring);
  188. assert(ret == 1);
  189. ret = io_uring_wait_cqe(&ring, &cqe);
  190. io_uring_cqe_seen(&ring, cqe);
  191. io_uring_queue_exit(&ring);
  192. return T_EXIT_PASS;
  193. }
  194. static int test_disabled_ring_lazy_polling(int early_poll)
  195. {
  196. struct io_uring_cqe *cqe;
  197. struct io_uring_sqe *sqe;
  198. struct io_uring ring, ring2;
  199. unsigned head;
  200. int ret, i = 0;
  201. ret = io_uring_queue_init(8, &ring, IORING_SETUP_SINGLE_ISSUER |
  202. IORING_SETUP_DEFER_TASKRUN |
  203. IORING_SETUP_R_DISABLED);
  204. if (ret) {
  205. fprintf(stderr, "ring setup failed: %d\n", ret);
  206. return 1;
  207. }
  208. ret = io_uring_queue_init(8, &ring2, 0);
  209. if (ret) {
  210. fprintf(stderr, "ring2 setup failed: %d\n", ret);
  211. return 1;
  212. }
  213. if (early_poll) {
  214. /* start polling disabled DEFER_TASKRUN ring */
  215. sqe = io_uring_get_sqe(&ring2);
  216. io_uring_prep_poll_add(sqe, ring.ring_fd, POLLIN);
  217. ret = io_uring_submit(&ring2);
  218. assert(ret == 1);
  219. assert(check_cq_empty(&ring2));
  220. }
  221. /* enable rings, which should also activate pollwq */
  222. ret = io_uring_enable_rings(&ring);
  223. assert(ret >= 0);
  224. if (!early_poll) {
  225. /* start polling enabled DEFER_TASKRUN ring */
  226. sqe = io_uring_get_sqe(&ring2);
  227. io_uring_prep_poll_add(sqe, ring.ring_fd, POLLIN);
  228. ret = io_uring_submit(&ring2);
  229. assert(ret == 1);
  230. assert(check_cq_empty(&ring2));
  231. }
  232. sqe = io_uring_get_sqe(&ring);
  233. io_uring_prep_nop(sqe);
  234. ret = io_uring_submit(&ring);
  235. assert(ret == 1);
  236. io_uring_for_each_cqe(&ring2, head, cqe) {
  237. i++;
  238. }
  239. if (i != 1) {
  240. fprintf(stderr, "fail, polling stuck\n");
  241. return 1;
  242. }
  243. io_uring_queue_exit(&ring);
  244. io_uring_queue_exit(&ring2);
  245. return 0;
  246. }
  247. int main(int argc, char *argv[])
  248. {
  249. int ret;
  250. if (argc > 1)
  251. return 0;
  252. ret = test_basic();
  253. if (ret) {
  254. fprintf(stderr, "test_basic() failed %i\n", ret);
  255. return T_EXIT_FAIL;
  256. }
  257. if (t_probe_defer_taskrun()) {
  258. ret = test_missing_events();
  259. if (ret) {
  260. fprintf(stderr, "test_missing_events() failed %i\n", ret);
  261. return T_EXIT_FAIL;
  262. }
  263. ret = test_disabled_ring_lazy_polling(false);
  264. if (ret) {
  265. fprintf(stderr, "test_disabled_ring_lazy_polling(false) failed %i\n", ret);
  266. return T_EXIT_FAIL;
  267. }
  268. ret = test_disabled_ring_lazy_polling(true);
  269. if (ret) {
  270. fprintf(stderr, "test_disabled_ring_lazy_polling(true) failed %i\n", ret);
  271. return T_EXIT_FAIL;
  272. }
  273. }
  274. ret = test_self_poll();
  275. if (ret) {
  276. fprintf(stderr, "test_self_poll failed\n");
  277. return T_EXIT_FAIL;
  278. }
  279. return 0;
  280. }