sq-poll-dup.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test SQPOLL with IORING_SETUP_ATTACH_WQ and closing of
  5. * the original ring descriptor.
  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 <sys/types.h>
  14. #include <sys/eventfd.h>
  15. #include <sys/resource.h>
  16. #include "helpers.h"
  17. #include "liburing.h"
  18. #define FILE_SIZE (128 * 1024 * 1024)
  19. #define BS 4096
  20. #define BUFFERS 64
  21. #define NR_RINGS 4
  22. static struct iovec *vecs;
  23. static struct io_uring rings[NR_RINGS];
  24. static int wait_io(struct io_uring *ring, int nr_ios)
  25. {
  26. struct io_uring_cqe *cqe;
  27. int ret;
  28. while (nr_ios) {
  29. ret = io_uring_wait_cqe(ring, &cqe);
  30. if (ret) {
  31. fprintf(stderr, "wait_ret=%d\n", ret);
  32. return 1;
  33. }
  34. if (cqe->res != BS) {
  35. fprintf(stderr, "Unexpected ret %d\n", cqe->res);
  36. return 1;
  37. }
  38. io_uring_cqe_seen(ring, cqe);
  39. nr_ios--;
  40. }
  41. return 0;
  42. }
  43. static int queue_io(struct io_uring *ring, int fd, int nr_ios)
  44. {
  45. unsigned long off;
  46. int i;
  47. i = 0;
  48. off = 0;
  49. while (nr_ios) {
  50. struct io_uring_sqe *sqe;
  51. sqe = io_uring_get_sqe(ring);
  52. if (!sqe)
  53. break;
  54. io_uring_prep_read(sqe, fd, vecs[i].iov_base, vecs[i].iov_len, off);
  55. nr_ios--;
  56. i++;
  57. off += BS;
  58. }
  59. io_uring_submit(ring);
  60. return i;
  61. }
  62. static int do_io(int fd, int ring_start, int ring_end)
  63. {
  64. int i, rets[NR_RINGS];
  65. unsigned ios = 0;
  66. while (ios < 32) {
  67. for (i = ring_start; i < ring_end; i++) {
  68. int ret = queue_io(&rings[i], fd, BUFFERS);
  69. if (ret < 0)
  70. goto err;
  71. rets[i] = ret;
  72. }
  73. for (i = ring_start; i < ring_end; i++) {
  74. if (wait_io(&rings[i], rets[i]))
  75. goto err;
  76. }
  77. ios += BUFFERS;
  78. }
  79. return 0;
  80. err:
  81. return 1;
  82. }
  83. static int test(int fd, int do_dup_and_close, int close_ring)
  84. {
  85. int i, ret, ring_fd;
  86. for (i = 0; i < NR_RINGS; i++) {
  87. struct io_uring_params p = { };
  88. p.flags = IORING_SETUP_SQPOLL;
  89. p.sq_thread_idle = 100;
  90. if (i) {
  91. p.wq_fd = rings[0].ring_fd;
  92. p.flags |= IORING_SETUP_ATTACH_WQ;
  93. }
  94. ret = io_uring_queue_init_params(BUFFERS, &rings[i], &p);
  95. if (ret) {
  96. fprintf(stderr, "queue_init: %d/%d\n", ret, i);
  97. goto err;
  98. }
  99. /* no sharing for non-fixed either */
  100. if (!(p.features & IORING_FEAT_SQPOLL_NONFIXED)) {
  101. fprintf(stdout, "No SQPOLL sharing, skipping\n");
  102. return 0;
  103. }
  104. }
  105. /* test all rings */
  106. if (do_io(fd, 0, NR_RINGS))
  107. goto err;
  108. /* dup and close original ring fd */
  109. ring_fd = dup(rings[0].ring_fd);
  110. if (close_ring)
  111. close(rings[0].ring_fd);
  112. rings[0].ring_fd = rings[0].enter_ring_fd = ring_fd;
  113. if (do_dup_and_close)
  114. goto done;
  115. /* test all but closed one */
  116. if (do_io(fd, 1, NR_RINGS))
  117. goto err;
  118. /* test closed one */
  119. if (do_io(fd, 0, 1))
  120. goto err;
  121. /* make sure thread is idle so we enter the kernel */
  122. usleep(200000);
  123. /* test closed one */
  124. if (do_io(fd, 0, 1))
  125. goto err;
  126. done:
  127. for (i = 0; i < NR_RINGS; i++)
  128. io_uring_queue_exit(&rings[i]);
  129. return 0;
  130. err:
  131. return 1;
  132. }
  133. int main(int argc, char *argv[])
  134. {
  135. char *fname;
  136. int ret, fd;
  137. if (argc > 1) {
  138. fname = argv[1];
  139. } else {
  140. fname = ".basic-rw-poll-dup";
  141. t_create_file(fname, FILE_SIZE);
  142. }
  143. vecs = t_create_buffers(BUFFERS, BS);
  144. fd = open(fname, O_RDONLY | O_DIRECT);
  145. if (fd < 0) {
  146. int __e = errno;
  147. if (fname != argv[1])
  148. unlink(fname);
  149. if (__e == EINVAL)
  150. return T_EXIT_SKIP;
  151. perror("open");
  152. return -1;
  153. }
  154. if (fname != argv[1])
  155. unlink(fname);
  156. ret = test(fd, 0, 0);
  157. if (ret) {
  158. fprintf(stderr, "test 0 0 failed\n");
  159. goto err;
  160. }
  161. ret = test(fd, 0, 1);
  162. if (ret) {
  163. fprintf(stderr, "test 0 1 failed\n");
  164. goto err;
  165. }
  166. ret = test(fd, 1, 0);
  167. if (ret) {
  168. fprintf(stderr, "test 1 0 failed\n");
  169. goto err;
  170. }
  171. return 0;
  172. err:
  173. return 1;
  174. }