io_uring_enter.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * io_uring_enter.c
  5. *
  6. * Description: Unit tests for the io_uring_enter system call.
  7. *
  8. * Copyright 2019, Red Hat, Inc.
  9. * Author: Jeff Moyer <jmoyer@redhat.com>
  10. */
  11. #include <stdio.h>
  12. #include <fcntl.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <sys/sysinfo.h>
  18. #include <poll.h>
  19. #include <assert.h>
  20. #include <sys/uio.h>
  21. #include <sys/mman.h>
  22. #include <linux/mman.h>
  23. #include <sys/time.h>
  24. #include <sys/resource.h>
  25. #include <limits.h>
  26. #include <sys/time.h>
  27. #include "helpers.h"
  28. #include "liburing.h"
  29. #include "liburing/barrier.h"
  30. #include "../src/syscall.h"
  31. #define IORING_MAX_ENTRIES 4096
  32. #define IORING_MAX_ENTRIES_FALLBACK 128
  33. static int expect_fail(int fd, unsigned int to_submit,
  34. unsigned int min_complete, unsigned int flags,
  35. sigset_t *sig, int error)
  36. {
  37. int ret;
  38. ret = io_uring_enter(fd, to_submit, min_complete, flags, sig);
  39. if (ret >= 0) {
  40. fprintf(stderr, "expected %s, but call succeeded\n", strerror(-error));
  41. return 1;
  42. }
  43. if (ret != error) {
  44. fprintf(stderr, "expected %d, got %d\n", error, ret);
  45. return 1;
  46. }
  47. return 0;
  48. }
  49. static int try_io_uring_enter(int fd, unsigned int to_submit,
  50. unsigned int min_complete, unsigned int flags,
  51. sigset_t *sig, int expect)
  52. {
  53. int ret;
  54. if (expect < 0)
  55. return expect_fail(fd, to_submit, min_complete, flags, sig,
  56. expect);
  57. ret = io_uring_enter(fd, to_submit, min_complete, flags, sig);
  58. if (ret != expect) {
  59. fprintf(stderr, "Expected %d, got %d\n", expect, ret);
  60. return 1;
  61. }
  62. return 0;
  63. }
  64. /*
  65. * prep a read I/O. index is treated like a block number.
  66. */
  67. static int setup_file(char *template, off_t len)
  68. {
  69. int fd, ret;
  70. char buf[4096];
  71. fd = mkstemp(template);
  72. if (fd < 0) {
  73. perror("mkstemp");
  74. exit(1);
  75. }
  76. ret = ftruncate(fd, len);
  77. if (ret < 0) {
  78. perror("ftruncate");
  79. exit(1);
  80. }
  81. ret = read(fd, buf, 4096);
  82. if (ret != 4096) {
  83. fprintf(stderr, "read returned %d, expected 4096\n", ret);
  84. exit(1);
  85. }
  86. return fd;
  87. }
  88. static void io_prep_read(struct io_uring_sqe *sqe, int fd, off_t offset,
  89. size_t len)
  90. {
  91. struct iovec *iov;
  92. iov = t_malloc(sizeof(*iov));
  93. assert(iov);
  94. iov->iov_base = t_malloc(len);
  95. assert(iov->iov_base);
  96. iov->iov_len = len;
  97. io_uring_prep_readv(sqe, fd, iov, 1, offset);
  98. io_uring_sqe_set_data(sqe, iov); // free on completion
  99. }
  100. static void reap_events(struct io_uring *ring, unsigned nr)
  101. {
  102. int ret;
  103. unsigned left = nr;
  104. struct io_uring_cqe *cqe;
  105. struct iovec *iov;
  106. struct timeval start, now, elapsed;
  107. gettimeofday(&start, NULL);
  108. while (left) {
  109. ret = io_uring_wait_cqe(ring, &cqe);
  110. if (ret < 0) {
  111. fprintf(stderr, "io_uring_wait_cqe returned %d\n", ret);
  112. exit(1);
  113. }
  114. if (cqe->res != 4096)
  115. fprintf(stderr, "cqe->res: %d, expected 4096\n", cqe->res);
  116. iov = io_uring_cqe_get_data(cqe);
  117. free(iov->iov_base);
  118. free(iov);
  119. left--;
  120. io_uring_cqe_seen(ring, cqe);
  121. gettimeofday(&now, NULL);
  122. timersub(&now, &start, &elapsed);
  123. if (elapsed.tv_sec > 10) {
  124. fprintf(stderr, "Timed out waiting for I/Os to complete.\n");
  125. fprintf(stderr, "%u expected, %u completed\n", nr, left);
  126. break;
  127. }
  128. }
  129. }
  130. static void submit_io(struct io_uring *ring, unsigned nr)
  131. {
  132. int fd, ret;
  133. off_t file_len;
  134. unsigned i;
  135. static char template[32] = "/tmp/io_uring_enter-test.XXXXXX";
  136. struct io_uring_sqe *sqe;
  137. file_len = nr * 4096;
  138. fd = setup_file(template, file_len);
  139. for (i = 0; i < nr; i++) {
  140. /* allocate an sqe */
  141. sqe = io_uring_get_sqe(ring);
  142. /* fill it in */
  143. io_prep_read(sqe, fd, i * 4096, 4096);
  144. }
  145. /* submit the I/Os */
  146. ret = io_uring_submit(ring);
  147. unlink(template);
  148. if (ret < 0) {
  149. perror("io_uring_enter");
  150. exit(1);
  151. }
  152. }
  153. int main(int argc, char **argv)
  154. {
  155. int ret;
  156. unsigned int status = 0;
  157. struct io_uring ring;
  158. struct io_uring_sq *sq = &ring.sq;
  159. unsigned ktail, mask, index;
  160. unsigned sq_entries;
  161. unsigned completed, dropped;
  162. if (argc > 1)
  163. return T_EXIT_SKIP;
  164. ret = io_uring_queue_init(IORING_MAX_ENTRIES, &ring, 0);
  165. if (ret == -ENOMEM)
  166. ret = io_uring_queue_init(IORING_MAX_ENTRIES_FALLBACK, &ring, 0);
  167. if (ret < 0) {
  168. perror("io_uring_queue_init");
  169. exit(T_EXIT_FAIL);
  170. }
  171. mask = sq->ring_mask;
  172. /* invalid flags */
  173. status |= try_io_uring_enter(ring.ring_fd, 1, 0, ~0U, NULL, -EINVAL);
  174. /* invalid fd, EBADF */
  175. status |= try_io_uring_enter(-1, 0, 0, 0, NULL, -EBADF);
  176. /* valid, non-ring fd, EOPNOTSUPP */
  177. status |= try_io_uring_enter(0, 0, 0, 0, NULL, -EOPNOTSUPP);
  178. /* to_submit: 0, flags: 0; should get back 0. */
  179. status |= try_io_uring_enter(ring.ring_fd, 0, 0, 0, NULL, 0);
  180. /* fill the sq ring */
  181. sq_entries = ring.sq.ring_entries;
  182. submit_io(&ring, sq_entries);
  183. ret = io_uring_enter(ring.ring_fd, 0, sq_entries,
  184. IORING_ENTER_GETEVENTS, NULL);
  185. if (ret < 0) {
  186. fprintf(stderr, "io_uring_enter: %s\n", strerror(-ret));
  187. status = 1;
  188. } else {
  189. /*
  190. * This is a non-IOPOLL ring, which means that io_uring_enter
  191. * should not return until min_complete events are available
  192. * in the completion queue.
  193. */
  194. completed = *ring.cq.ktail - *ring.cq.khead;
  195. if (completed != sq_entries) {
  196. fprintf(stderr, "Submitted %u I/Os, but only got %u completions\n",
  197. sq_entries, completed);
  198. status = 1;
  199. }
  200. reap_events(&ring, sq_entries);
  201. }
  202. /*
  203. * Add an invalid index to the submission queue. This should
  204. * result in the dropped counter increasing.
  205. */
  206. index = sq->ring_entries + 1; // invalid index
  207. dropped = *sq->kdropped;
  208. ktail = *sq->ktail;
  209. sq->array[ktail & mask] = index;
  210. ++ktail;
  211. /*
  212. * Ensure that the kernel sees the SQE update before it sees the tail
  213. * update.
  214. */
  215. io_uring_smp_store_release(sq->ktail, ktail);
  216. ret = io_uring_enter(ring.ring_fd, 1, 0, 0, NULL);
  217. /* now check to see if our sqe was dropped */
  218. if (*sq->kdropped == dropped) {
  219. fprintf(stderr, "dropped counter did not increase\n");
  220. status = 1;
  221. }
  222. if (!status)
  223. return T_EXIT_PASS;
  224. fprintf(stderr, "FAIL\n");
  225. return T_EXIT_FAIL;
  226. }