split dev_queue
[cor.git] / tools / io_uring / queue.c
blob321819c132c7753e8028727d0a29fc80fa818ea6
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <sys/mman.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <string.h>
8 #include "liburing.h"
9 #include "barrier.h"
11 static int __io_uring_get_cqe(struct io_uring *ring,
12 struct io_uring_cqe **cqe_ptr, int wait)
14 struct io_uring_cq *cq = &ring->cq;
15 const unsigned mask = *cq->kring_mask;
16 unsigned head;
17 int ret;
19 *cqe_ptr = NULL;
20 head = *cq->khead;
21 do {
23 * It's necessary to use a read_barrier() before reading
24 * the CQ tail, since the kernel updates it locklessly. The
25 * kernel has the matching store barrier for the update. The
26 * kernel also ensures that previous stores to CQEs are ordered
27 * with the tail update.
29 read_barrier();
30 if (head != *cq->ktail) {
31 *cqe_ptr = &cq->cqes[head & mask];
32 break;
34 if (!wait)
35 break;
36 ret = io_uring_enter(ring->ring_fd, 0, 1,
37 IORING_ENTER_GETEVENTS, NULL);
38 if (ret < 0)
39 return -errno;
40 } while (1);
42 return 0;
46 * Return an IO completion, if one is readily available. Returns 0 with
47 * cqe_ptr filled in on success, -errno on failure.
49 int io_uring_peek_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_ptr)
51 return __io_uring_get_cqe(ring, cqe_ptr, 0);
55 * Return an IO completion, waiting for it if necessary. Returns 0 with
56 * cqe_ptr filled in on success, -errno on failure.
58 int io_uring_wait_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_ptr)
60 return __io_uring_get_cqe(ring, cqe_ptr, 1);
64 * Submit sqes acquired from io_uring_get_sqe() to the kernel.
66 * Returns number of sqes submitted
68 int io_uring_submit(struct io_uring *ring)
70 struct io_uring_sq *sq = &ring->sq;
71 const unsigned mask = *sq->kring_mask;
72 unsigned ktail, ktail_next, submitted, to_submit;
73 int ret;
76 * If we have pending IO in the kring, submit it first. We need a
77 * read barrier here to match the kernels store barrier when updating
78 * the SQ head.
80 read_barrier();
81 if (*sq->khead != *sq->ktail) {
82 submitted = *sq->kring_entries;
83 goto submit;
86 if (sq->sqe_head == sq->sqe_tail)
87 return 0;
90 * Fill in sqes that we have queued up, adding them to the kernel ring
92 submitted = 0;
93 ktail = ktail_next = *sq->ktail;
94 to_submit = sq->sqe_tail - sq->sqe_head;
95 while (to_submit--) {
96 ktail_next++;
97 read_barrier();
99 sq->array[ktail & mask] = sq->sqe_head & mask;
100 ktail = ktail_next;
102 sq->sqe_head++;
103 submitted++;
106 if (!submitted)
107 return 0;
109 if (*sq->ktail != ktail) {
111 * First write barrier ensures that the SQE stores are updated
112 * with the tail update. This is needed so that the kernel
113 * will never see a tail update without the preceeding sQE
114 * stores being done.
116 write_barrier();
117 *sq->ktail = ktail;
119 * The kernel has the matching read barrier for reading the
120 * SQ tail.
122 write_barrier();
125 submit:
126 ret = io_uring_enter(ring->ring_fd, submitted, 0,
127 IORING_ENTER_GETEVENTS, NULL);
128 if (ret < 0)
129 return -errno;
131 return ret;
135 * Return an sqe to fill. Application must later call io_uring_submit()
136 * when it's ready to tell the kernel about it. The caller may call this
137 * function multiple times before calling io_uring_submit().
139 * Returns a vacant sqe, or NULL if we're full.
141 struct io_uring_sqe *io_uring_get_sqe(struct io_uring *ring)
143 struct io_uring_sq *sq = &ring->sq;
144 unsigned next = sq->sqe_tail + 1;
145 struct io_uring_sqe *sqe;
148 * All sqes are used
150 if (next - sq->sqe_head > *sq->kring_entries)
151 return NULL;
153 sqe = &sq->sqes[sq->sqe_tail & *sq->kring_mask];
154 sq->sqe_tail = next;
155 return sqe;