11 static int __io_uring_get_completion(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;
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.
30 if (head != *cq->ktail) {
31 *cqe_ptr = &cq->cqes[head & mask];
36 ret = io_uring_enter(ring->ring_fd, 0, 1,
37 IORING_ENTER_GETEVENTS, NULL);
43 *cq->khead = head + 1;
45 * Ensure that the kernel sees our new head, the kernel has
46 * the matching read barrier.
55 * Return an IO completion, if one is readily available
57 int io_uring_get_completion(struct io_uring *ring,
58 struct io_uring_cqe **cqe_ptr)
60 return __io_uring_get_completion(ring, cqe_ptr, 0);
64 * Return an IO completion, waiting for it if necessary
66 int io_uring_wait_completion(struct io_uring *ring,
67 struct io_uring_cqe **cqe_ptr)
69 return __io_uring_get_completion(ring, cqe_ptr, 1);
73 * Submit sqes acquired from io_uring_get_sqe() to the kernel.
75 * Returns number of sqes submitted
77 int io_uring_submit(struct io_uring *ring)
79 struct io_uring_sq *sq = &ring->sq;
80 const unsigned mask = *sq->kring_mask;
81 unsigned ktail, ktail_next, submitted;
85 * If we have pending IO in the kring, submit it first. We need a
86 * read barrier here to match the kernels store barrier when updating
90 if (*sq->khead != *sq->ktail) {
91 submitted = *sq->kring_entries;
95 if (sq->sqe_head == sq->sqe_tail)
99 * Fill in sqes that we have queued up, adding them to the kernel ring
102 ktail = ktail_next = *sq->ktail;
103 while (sq->sqe_head < sq->sqe_tail) {
107 sq->array[ktail & mask] = sq->sqe_head & mask;
117 if (*sq->ktail != ktail) {
119 * First write barrier ensures that the SQE stores are updated
120 * with the tail update. This is needed so that the kernel
121 * will never see a tail update without the preceeding sQE
127 * The kernel has the matching read barrier for reading the
134 ret = io_uring_enter(ring->ring_fd, submitted, 0,
135 IORING_ENTER_GETEVENTS, NULL);
143 * Return an sqe to fill. Application must later call io_uring_submit()
144 * when it's ready to tell the kernel about it. The caller may call this
145 * function multiple times before calling io_uring_submit().
147 * Returns a vacant sqe, or NULL if we're full.
149 struct io_uring_sqe *io_uring_get_sqe(struct io_uring *ring)
151 struct io_uring_sq *sq = &ring->sq;
152 unsigned next = sq->sqe_tail + 1;
153 struct io_uring_sqe *sqe;
158 if (next - sq->sqe_head > *sq->kring_entries)
161 sqe = &sq->sqes[sq->sqe_tail & *sq->kring_mask];