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
;
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
);
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
;
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
81 if (*sq
->khead
!= *sq
->ktail
) {
82 submitted
= *sq
->kring_entries
;
86 if (sq
->sqe_head
== sq
->sqe_tail
)
90 * Fill in sqes that we have queued up, adding them to the kernel ring
93 ktail
= ktail_next
= *sq
->ktail
;
94 to_submit
= sq
->sqe_tail
- sq
->sqe_head
;
99 sq
->array
[ktail
& mask
] = sq
->sqe_head
& mask
;
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
119 * The kernel has the matching read barrier for reading the
126 ret
= io_uring_enter(ring
->ring_fd
, submitted
, 0,
127 IORING_ENTER_GETEVENTS
, NULL
);
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
;
150 if (next
- sq
->sqe_head
> *sq
->kring_entries
)
153 sqe
= &sq
->sqes
[sq
->sqe_tail
& *sq
->kring_mask
];