1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include "liburing/compat.h"
#include "liburing/io_uring.h"
#include "liburing.h"
#include "liburing/barrier.h"
static int __io_uring_get_cqe(struct io_uring *ring,
struct io_uring_cqe **cqe_ptr, unsigned submit,
unsigned wait_nr)
{
int ret, err = 0;
unsigned head;
do {
io_uring_for_each_cqe(ring, head, *cqe_ptr)
break;
if (*cqe_ptr) {
if ((*cqe_ptr)->user_data == LIBURING_UDATA_TIMEOUT) {
if ((*cqe_ptr)->res < 0)
err = (*cqe_ptr)->res;
io_uring_cq_advance(ring, 1);
if (!err)
continue;
*cqe_ptr = NULL;
}
break;
}
if (!wait_nr)
return -EAGAIN;
ret = io_uring_enter(ring->ring_fd, submit, wait_nr,
IORING_ENTER_GETEVENTS, NULL);
if (ret < 0)
return -errno;
} while (1);
return err;
}
/*
* Return an IO completion, if one is readily available. Returns 0 with
* cqe_ptr filled in on success, -errno on failure.
*/
int io_uring_peek_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_ptr)
{
return __io_uring_get_cqe(ring, cqe_ptr, 0, 0);
}
/*
* Fill in an array of IO completions up to count, if any are available.
* Returns the amount of IO completions filled.
*/
unsigned io_uring_peek_batch_cqe(struct io_uring *ring,
struct io_uring_cqe **cqes, unsigned count)
{
unsigned ready;
ready = io_uring_cq_ready(ring);
if (ready) {
unsigned head = *ring->cq.khead;
unsigned mask = *ring->cq.kring_mask;
unsigned last;
int i = 0;
count = count > ready ? ready : count;
last = head + count;
for (;head != last; head++, i++)
cqes[i] = &ring->cq.cqes[head & mask];
return count;
}
return 0;
}
/*
* Return an IO completion, waiting for it if necessary. Returns 0 with
* cqe_ptr filled in on success, -errno on failure.
*/
int io_uring_wait_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_ptr)
{
return __io_uring_get_cqe(ring, cqe_ptr, 0, 1);
}
/*
* Like io_uring_wait_cqe(), except it accepts a timeout value as well. Note
* that an sqe is used internally to handle the timeout. Applications using
* this function must never set sqe->user_data to LIBURING_UDATA_TIMEOUT!
*
* Note that the application need not call io_uring_submit() before calling
* this function, as we will do that on its behalf.
*/
int io_uring_wait_cqes_timeout(struct io_uring *ring,
struct io_uring_cqe **cqe_ptr,
unsigned wait_nr,
struct timespec *ts)
{
int ret;
if (wait_nr) {
struct io_uring_sqe *sqe;
/*
* If the SQ ring is full, we may need to submit IO first
*/
sqe = io_uring_get_sqe(ring);
if (!sqe) {
ret = io_uring_submit(ring);
if (ret < 0)
return ret;
sqe = io_uring_get_sqe(ring);
}
io_uring_prep_timeout(sqe, ts, wait_nr);
sqe->user_data = LIBURING_UDATA_TIMEOUT;
}
ret = io_uring_submit(ring);
if (ret < 0)
return ret;
return __io_uring_get_cqe(ring, cqe_ptr, 1, 1);
}
/*
* See io_uring_wait_cqes_timeout() - this function is the same, it just
* always uses '1' as the wait_nr.
*/
int io_uring_wait_cqe_timeout(struct io_uring *ring,
struct io_uring_cqe **cqe_ptr,
struct timespec *ts)
{
return io_uring_wait_cqes_timeout(ring, cqe_ptr, 1, ts);
}
/*
* Returns true if we're not using SQ thread (thus nobody submits but us)
* or if IORING_SQ_NEED_WAKEUP is set, so submit thread must be explicitly
* awakened. For the latter case, we set the thread wakeup flag.
*/
static inline bool sq_ring_needs_enter(struct io_uring *ring, unsigned *flags)
{
if (!(ring->flags & IORING_SETUP_SQPOLL))
return true;
if ((*ring->sq.kflags & IORING_SQ_NEED_WAKEUP)) {
*flags |= IORING_ENTER_SQ_WAKEUP;
return true;
}
return false;
}
/*
* Sync internal state with kernel ring state on the SQ side
*/
static int __io_uring_flush_sq(struct io_uring *ring)
{
struct io_uring_sq *sq = &ring->sq;
const unsigned mask = *sq->kring_mask;
unsigned ktail, submitted, to_submit;
if (sq->sqe_head == sq->sqe_tail)
return 0;
/*
* Fill in sqes that we have queued up, adding them to the kernel ring
*/
submitted = 0;
ktail = *sq->ktail;
to_submit = sq->sqe_tail - sq->sqe_head;
while (to_submit--) {
sq->array[ktail & mask] = sq->sqe_head & mask;
ktail++;
sq->sqe_head++;
submitted++;
}
/*
* Ensure that the kernel sees the SQE updates before it sees the tail
* update.
*/
if (submitted)
io_uring_smp_store_release(sq->ktail, ktail);
return submitted;
}
/*
* Submit sqes acquired from io_uring_get_sqe() to the kernel.
*
* Returns number of sqes submitted
*/
static int __io_uring_submit(struct io_uring *ring, unsigned submitted,
unsigned wait_nr)
{
unsigned flags;
int ret;
flags = 0;
if (wait_nr || sq_ring_needs_enter(ring, &flags)) {
if (wait_nr) {
if (wait_nr > submitted)
wait_nr = submitted;
flags |= IORING_ENTER_GETEVENTS;
}
ret = io_uring_enter(ring->ring_fd, submitted, wait_nr, flags,
NULL);
if (ret < 0)
return -errno;
} else
ret = submitted;
return ret;
}
/*
* Submit sqes acquired from io_uring_get_sqe() to the kernel.
*
* Returns number of sqes submitted
*/
int io_uring_submit(struct io_uring *ring)
{
int submitted;
submitted = __io_uring_flush_sq(ring);
if (submitted)
return __io_uring_submit(ring, submitted, 0);
return 0;
}
/*
* Like io_uring_submit(), but allows waiting for events as well.
*
* Returns number of sqes submitted
*/
int io_uring_submit_and_wait(struct io_uring *ring, unsigned wait_nr)
{
int submitted;
submitted = __io_uring_flush_sq(ring);
if (submitted)
return __io_uring_submit(ring, submitted, wait_nr);
return 0;
}
/*
* Return an sqe to fill. Application must later call io_uring_submit()
* when it's ready to tell the kernel about it. The caller may call this
* function multiple times before calling io_uring_submit().
*
* Returns a vacant sqe, or NULL if we're full.
*/
struct io_uring_sqe *io_uring_get_sqe(struct io_uring *ring)
{
struct io_uring_sq *sq = &ring->sq;
unsigned next = sq->sqe_tail + 1;
struct io_uring_sqe *sqe;
/*
* All sqes are used
*/
if (next - sq->sqe_head > *sq->kring_entries)
return NULL;
sqe = &sq->sqes[sq->sqe_tail & *sq->kring_mask];
sq->sqe_tail = next;
return sqe;
}
|