Commit | Line | Data |
---|---|---|
52885fa2 | 1 | /* |
bffad86f | 2 | * io_uring engine |
52885fa2 | 3 | * |
bffad86f | 4 | * IO engine using the new native Linux aio io_uring interface. See: |
a90cd050 | 5 | * |
bffad86f | 6 | * http://git.kernel.dk/cgit/linux-block/log/?h=io_uring |
52885fa2 JA |
7 | * |
8 | */ | |
9 | #include <stdlib.h> | |
10 | #include <unistd.h> | |
11 | #include <errno.h> | |
52885fa2 JA |
12 | #include <sys/time.h> |
13 | #include <sys/resource.h> | |
14 | ||
15 | #include "../fio.h" | |
16 | #include "../lib/pow2.h" | |
17 | #include "../optgroup.h" | |
18 | #include "../lib/memalign.h" | |
b87aa01a | 19 | #include "../lib/fls.h" |
52885fa2 | 20 | |
bffad86f | 21 | #ifdef ARCH_HAVE_IOURING |
52885fa2 | 22 | |
57fa61f0 | 23 | #include "../lib/types.h" |
f3e769a4 | 24 | #include "../os/linux/io_uring.h" |
9a2d78b3 | 25 | |
bffad86f | 26 | struct io_sq_ring { |
e2239016 JA |
27 | unsigned *head; |
28 | unsigned *tail; | |
29 | unsigned *ring_mask; | |
30 | unsigned *ring_entries; | |
31 | unsigned *flags; | |
32 | unsigned *array; | |
52885fa2 JA |
33 | }; |
34 | ||
bffad86f | 35 | struct io_cq_ring { |
e2239016 JA |
36 | unsigned *head; |
37 | unsigned *tail; | |
38 | unsigned *ring_mask; | |
39 | unsigned *ring_entries; | |
f0403f94 | 40 | struct io_uring_cqe *cqes; |
9a2d78b3 JA |
41 | }; |
42 | ||
bffad86f | 43 | struct ioring_mmap { |
9a2d78b3 JA |
44 | void *ptr; |
45 | size_t len; | |
52885fa2 JA |
46 | }; |
47 | ||
bffad86f | 48 | struct ioring_data { |
9a2d78b3 JA |
49 | int ring_fd; |
50 | ||
52885fa2 JA |
51 | struct io_u **io_u_index; |
52 | ||
5ffd5626 JA |
53 | int *fds; |
54 | ||
bffad86f | 55 | struct io_sq_ring sq_ring; |
f0403f94 | 56 | struct io_uring_sqe *sqes; |
9a2d78b3 | 57 | struct iovec *iovecs; |
b87aa01a | 58 | unsigned sq_ring_mask; |
52885fa2 | 59 | |
bffad86f | 60 | struct io_cq_ring cq_ring; |
b87aa01a | 61 | unsigned cq_ring_mask; |
52885fa2 JA |
62 | |
63 | int queued; | |
64 | int cq_ring_off; | |
b87aa01a | 65 | unsigned iodepth; |
1af44196 XW |
66 | bool ioprio_class_set; |
67 | bool ioprio_set; | |
96563db9 | 68 | |
bffad86f | 69 | struct ioring_mmap mmap[3]; |
52885fa2 JA |
70 | }; |
71 | ||
bffad86f | 72 | struct ioring_options { |
52885fa2 JA |
73 | void *pad; |
74 | unsigned int hipri; | |
b2a432bf | 75 | unsigned int cmdprio_percentage; |
52885fa2 | 76 | unsigned int fixedbufs; |
5ffd5626 | 77 | unsigned int registerfiles; |
3d7d00a3 | 78 | unsigned int sqpoll_thread; |
2ea53ca3 JA |
79 | unsigned int sqpoll_set; |
80 | unsigned int sqpoll_cpu; | |
b10b1e70 | 81 | unsigned int nonvectored; |
4a87b584 | 82 | unsigned int uncached; |
7d42e66e | 83 | unsigned int nowait; |
52885fa2 JA |
84 | }; |
85 | ||
b10b1e70 JA |
86 | static const int ddir_to_op[2][2] = { |
87 | { IORING_OP_READV, IORING_OP_READ }, | |
88 | { IORING_OP_WRITEV, IORING_OP_WRITE } | |
89 | }; | |
90 | ||
3f1e3af7 KB |
91 | static const int fixed_ddir_to_op[2] = { |
92 | IORING_OP_READ_FIXED, | |
93 | IORING_OP_WRITE_FIXED | |
94 | }; | |
95 | ||
2ea53ca3 | 96 | static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val) |
a90cd050 | 97 | { |
bffad86f | 98 | struct ioring_options *o = data; |
a90cd050 | 99 | |
2ea53ca3 JA |
100 | o->sqpoll_cpu = *val; |
101 | o->sqpoll_set = 1; | |
a90cd050 JA |
102 | return 0; |
103 | } | |
104 | ||
52885fa2 JA |
105 | static struct fio_option options[] = { |
106 | { | |
107 | .name = "hipri", | |
108 | .lname = "High Priority", | |
109 | .type = FIO_OPT_STR_SET, | |
bffad86f | 110 | .off1 = offsetof(struct ioring_options, hipri), |
52885fa2 JA |
111 | .help = "Use polled IO completions", |
112 | .category = FIO_OPT_C_ENGINE, | |
27f436d9 | 113 | .group = FIO_OPT_G_IOURING, |
52885fa2 | 114 | }, |
b2a432bf PC |
115 | #ifdef FIO_HAVE_IOPRIO_CLASS |
116 | { | |
117 | .name = "cmdprio_percentage", | |
118 | .lname = "high priority percentage", | |
119 | .type = FIO_OPT_INT, | |
120 | .off1 = offsetof(struct ioring_options, cmdprio_percentage), | |
121 | .minval = 1, | |
122 | .maxval = 100, | |
123 | .help = "Send high priority I/O this percentage of the time", | |
124 | .category = FIO_OPT_C_ENGINE, | |
125 | .group = FIO_OPT_G_IOURING, | |
126 | }, | |
127 | #else | |
128 | { | |
129 | .name = "cmdprio_percentage", | |
130 | .lname = "high priority percentage", | |
131 | .type = FIO_OPT_UNSUPPORTED, | |
132 | .help = "Your platform does not support I/O priority classes", | |
133 | }, | |
134 | #endif | |
52885fa2 JA |
135 | { |
136 | .name = "fixedbufs", | |
137 | .lname = "Fixed (pre-mapped) IO buffers", | |
138 | .type = FIO_OPT_STR_SET, | |
bffad86f | 139 | .off1 = offsetof(struct ioring_options, fixedbufs), |
52885fa2 JA |
140 | .help = "Pre map IO buffers", |
141 | .category = FIO_OPT_C_ENGINE, | |
27f436d9 | 142 | .group = FIO_OPT_G_IOURING, |
52885fa2 | 143 | }, |
5ffd5626 JA |
144 | { |
145 | .name = "registerfiles", | |
146 | .lname = "Register file set", | |
147 | .type = FIO_OPT_STR_SET, | |
148 | .off1 = offsetof(struct ioring_options, registerfiles), | |
149 | .help = "Pre-open/register files", | |
150 | .category = FIO_OPT_C_ENGINE, | |
27f436d9 | 151 | .group = FIO_OPT_G_IOURING, |
5ffd5626 | 152 | }, |
771c9901 JA |
153 | { |
154 | .name = "sqthread_poll", | |
3d7d00a3 JA |
155 | .lname = "Kernel SQ thread polling", |
156 | .type = FIO_OPT_INT, | |
157 | .off1 = offsetof(struct ioring_options, sqpoll_thread), | |
158 | .help = "Offload submission/completion to kernel thread", | |
159 | .category = FIO_OPT_C_ENGINE, | |
27f436d9 | 160 | .group = FIO_OPT_G_IOURING, |
3d7d00a3 JA |
161 | }, |
162 | { | |
163 | .name = "sqthread_poll_cpu", | |
164 | .lname = "SQ Thread Poll CPU", | |
2ea53ca3 JA |
165 | .type = FIO_OPT_INT, |
166 | .cb = fio_ioring_sqpoll_cb, | |
3d7d00a3 | 167 | .help = "What CPU to run SQ thread polling on", |
a90cd050 | 168 | .category = FIO_OPT_C_ENGINE, |
27f436d9 | 169 | .group = FIO_OPT_G_IOURING, |
a90cd050 | 170 | }, |
b10b1e70 JA |
171 | { |
172 | .name = "nonvectored", | |
173 | .lname = "Non-vectored", | |
174 | .type = FIO_OPT_INT, | |
175 | .off1 = offsetof(struct ioring_options, nonvectored), | |
176 | .help = "Use non-vectored read/write commands", | |
177 | .category = FIO_OPT_C_ENGINE, | |
178 | .group = FIO_OPT_G_IOURING, | |
179 | }, | |
4a87b584 JA |
180 | { |
181 | .name = "uncached", | |
182 | .lname = "Uncached", | |
183 | .type = FIO_OPT_INT, | |
184 | .off1 = offsetof(struct ioring_options, uncached), | |
185 | .help = "Use RWF_UNCACHED for buffered read/writes", | |
186 | .category = FIO_OPT_C_ENGINE, | |
187 | .group = FIO_OPT_G_IOURING, | |
188 | }, | |
7d42e66e KK |
189 | { |
190 | .name = "nowait", | |
191 | .lname = "RWF_NOWAIT", | |
192 | .type = FIO_OPT_BOOL, | |
193 | .off1 = offsetof(struct ioring_options, nowait), | |
194 | .help = "Use RWF_NOWAIT for reads/writes", | |
195 | .category = FIO_OPT_C_ENGINE, | |
196 | .group = FIO_OPT_G_IOURING, | |
197 | }, | |
52885fa2 JA |
198 | { |
199 | .name = NULL, | |
200 | }, | |
201 | }; | |
202 | ||
bffad86f | 203 | static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit, |
52885fa2 JA |
204 | unsigned int min_complete, unsigned int flags) |
205 | { | |
bfed648c | 206 | return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit, |
521164fa | 207 | min_complete, flags, NULL, 0); |
52885fa2 JA |
208 | } |
209 | ||
bffad86f | 210 | static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u) |
52885fa2 | 211 | { |
bffad86f | 212 | struct ioring_data *ld = td->io_ops_data; |
cfcc8564 | 213 | struct ioring_options *o = td->eo; |
52885fa2 | 214 | struct fio_file *f = io_u->file; |
f0403f94 | 215 | struct io_uring_sqe *sqe; |
52885fa2 | 216 | |
f0403f94 | 217 | sqe = &ld->sqes[io_u->index]; |
34d6090e AF |
218 | |
219 | /* zero out fields not used in this submission */ | |
220 | memset(sqe, 0, sizeof(*sqe)); | |
221 | ||
5ffd5626 JA |
222 | if (o->registerfiles) { |
223 | sqe->fd = f->engine_pos; | |
224 | sqe->flags = IOSQE_FIXED_FILE; | |
225 | } else { | |
226 | sqe->fd = f->fd; | |
5ffd5626 | 227 | } |
52885fa2 | 228 | |
e3970057 | 229 | if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) { |
f0403f94 | 230 | if (o->fixedbufs) { |
3f1e3af7 | 231 | sqe->opcode = fixed_ddir_to_op[io_u->ddir]; |
919850d2 | 232 | sqe->addr = (unsigned long) io_u->xfer_buf; |
f0403f94 | 233 | sqe->len = io_u->xfer_buflen; |
2ea53ca3 | 234 | sqe->buf_index = io_u->index; |
cfcc8564 | 235 | } else { |
3f1e3af7 | 236 | sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored]; |
b10b1e70 JA |
237 | if (o->nonvectored) { |
238 | sqe->addr = (unsigned long) | |
239 | ld->iovecs[io_u->index].iov_base; | |
240 | sqe->len = ld->iovecs[io_u->index].iov_len; | |
241 | } else { | |
242 | sqe->addr = (unsigned long) &ld->iovecs[io_u->index]; | |
243 | sqe->len = 1; | |
244 | } | |
cfcc8564 | 245 | } |
4a87b584 JA |
246 | if (!td->o.odirect && o->uncached) |
247 | sqe->rw_flags = RWF_UNCACHED; | |
7d42e66e KK |
248 | if (o->nowait) |
249 | sqe->rw_flags |= RWF_NOWAIT; | |
1af44196 | 250 | if (ld->ioprio_class_set) |
b7ed2a86 | 251 | sqe->ioprio = td->o.ioprio_class << 13; |
1af44196 | 252 | if (ld->ioprio_set) |
b7ed2a86 | 253 | sqe->ioprio |= td->o.ioprio; |
f0403f94 | 254 | sqe->off = io_u->offset; |
48e698fa | 255 | } else if (ddir_sync(io_u->ddir)) { |
01387bfe AF |
256 | if (io_u->ddir == DDIR_SYNC_FILE_RANGE) { |
257 | sqe->off = f->first_write; | |
258 | sqe->len = f->last_write - f->first_write; | |
259 | sqe->sync_range_flags = td->o.sync_file_range; | |
260 | sqe->opcode = IORING_OP_SYNC_FILE_RANGE; | |
261 | } else { | |
01387bfe AF |
262 | if (io_u->ddir == DDIR_DATASYNC) |
263 | sqe->fsync_flags |= IORING_FSYNC_DATASYNC; | |
264 | sqe->opcode = IORING_OP_FSYNC; | |
265 | } | |
48e698fa | 266 | } |
52885fa2 | 267 | |
48e698fa | 268 | sqe->user_data = (unsigned long) io_u; |
52885fa2 JA |
269 | return 0; |
270 | } | |
271 | ||
bffad86f | 272 | static struct io_u *fio_ioring_event(struct thread_data *td, int event) |
52885fa2 | 273 | { |
bffad86f | 274 | struct ioring_data *ld = td->io_ops_data; |
f0403f94 | 275 | struct io_uring_cqe *cqe; |
52885fa2 | 276 | struct io_u *io_u; |
b87aa01a | 277 | unsigned index; |
52885fa2 | 278 | |
b87aa01a | 279 | index = (event + ld->cq_ring_off) & ld->cq_ring_mask; |
52885fa2 | 280 | |
f0403f94 | 281 | cqe = &ld->cq_ring.cqes[index]; |
e3466352 | 282 | io_u = (struct io_u *) (uintptr_t) cqe->user_data; |
52885fa2 | 283 | |
f0403f94 JA |
284 | if (cqe->res != io_u->xfer_buflen) { |
285 | if (cqe->res > io_u->xfer_buflen) | |
286 | io_u->error = -cqe->res; | |
52885fa2 | 287 | else |
f0403f94 | 288 | io_u->resid = io_u->xfer_buflen - cqe->res; |
52885fa2 JA |
289 | } else |
290 | io_u->error = 0; | |
291 | ||
292 | return io_u; | |
293 | } | |
294 | ||
bffad86f | 295 | static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events, |
52885fa2 JA |
296 | unsigned int max) |
297 | { | |
bffad86f JA |
298 | struct ioring_data *ld = td->io_ops_data; |
299 | struct io_cq_ring *ring = &ld->cq_ring; | |
e2239016 | 300 | unsigned head, reaped = 0; |
52885fa2 | 301 | |
9a2d78b3 | 302 | head = *ring->head; |
52885fa2 JA |
303 | do { |
304 | read_barrier(); | |
9a2d78b3 | 305 | if (head == *ring->tail) |
52885fa2 JA |
306 | break; |
307 | reaped++; | |
308 | head++; | |
52885fa2 JA |
309 | } while (reaped + events < max); |
310 | ||
9a2d78b3 | 311 | *ring->head = head; |
52885fa2 JA |
312 | write_barrier(); |
313 | return reaped; | |
314 | } | |
315 | ||
bffad86f JA |
316 | static int fio_ioring_getevents(struct thread_data *td, unsigned int min, |
317 | unsigned int max, const struct timespec *t) | |
52885fa2 | 318 | { |
bffad86f | 319 | struct ioring_data *ld = td->io_ops_data; |
52885fa2 | 320 | unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min; |
bffad86f JA |
321 | struct ioring_options *o = td->eo; |
322 | struct io_cq_ring *ring = &ld->cq_ring; | |
b87aa01a JA |
323 | unsigned events = 0; |
324 | int r; | |
52885fa2 | 325 | |
9a2d78b3 | 326 | ld->cq_ring_off = *ring->head; |
52885fa2 | 327 | do { |
bffad86f | 328 | r = fio_ioring_cqring_reap(td, events, max); |
52885fa2 JA |
329 | if (r) { |
330 | events += r; | |
f7cbbbf8 ST |
331 | if (actual_min != 0) |
332 | actual_min -= r; | |
52885fa2 JA |
333 | continue; |
334 | } | |
335 | ||
3d7d00a3 | 336 | if (!o->sqpoll_thread) { |
9a2d78b3 JA |
337 | r = io_uring_enter(ld, 0, actual_min, |
338 | IORING_ENTER_GETEVENTS); | |
771c9901 | 339 | if (r < 0) { |
f6abd731 | 340 | if (errno == EAGAIN || errno == EINTR) |
771c9901 | 341 | continue; |
9a2d78b3 | 342 | td_verror(td, errno, "io_uring_enter"); |
771c9901 JA |
343 | break; |
344 | } | |
52885fa2 JA |
345 | } |
346 | } while (events < min); | |
347 | ||
348 | return r < 0 ? r : events; | |
349 | } | |
350 | ||
b2a432bf PC |
351 | static void fio_ioring_prio_prep(struct thread_data *td, struct io_u *io_u) |
352 | { | |
353 | struct ioring_options *o = td->eo; | |
354 | struct ioring_data *ld = td->io_ops_data; | |
355 | if (rand_between(&td->prio_state, 0, 99) < o->cmdprio_percentage) { | |
356 | ld->sqes[io_u->index].ioprio = IOPRIO_CLASS_RT << IOPRIO_CLASS_SHIFT; | |
357 | io_u->flags |= IO_U_F_PRIORITY; | |
358 | } | |
359 | return; | |
360 | } | |
361 | ||
bffad86f JA |
362 | static enum fio_q_status fio_ioring_queue(struct thread_data *td, |
363 | struct io_u *io_u) | |
52885fa2 | 364 | { |
bffad86f JA |
365 | struct ioring_data *ld = td->io_ops_data; |
366 | struct io_sq_ring *ring = &ld->sq_ring; | |
b2a432bf | 367 | struct ioring_options *o = td->eo; |
52885fa2 JA |
368 | unsigned tail, next_tail; |
369 | ||
370 | fio_ro_check(td, io_u); | |
371 | ||
b87aa01a | 372 | if (ld->queued == ld->iodepth) |
52885fa2 JA |
373 | return FIO_Q_BUSY; |
374 | ||
52885fa2 JA |
375 | if (io_u->ddir == DDIR_TRIM) { |
376 | if (ld->queued) | |
377 | return FIO_Q_BUSY; | |
378 | ||
379 | do_io_u_trim(td, io_u); | |
380 | io_u_mark_submit(td, 1); | |
381 | io_u_mark_complete(td, 1); | |
382 | return FIO_Q_COMPLETED; | |
383 | } | |
384 | ||
9a2d78b3 | 385 | tail = *ring->tail; |
52885fa2 | 386 | next_tail = tail + 1; |
52885fa2 | 387 | read_barrier(); |
9a2d78b3 | 388 | if (next_tail == *ring->head) |
52885fa2 JA |
389 | return FIO_Q_BUSY; |
390 | ||
b2a432bf PC |
391 | if (o->cmdprio_percentage) |
392 | fio_ioring_prio_prep(td, io_u); | |
b87aa01a | 393 | ring->array[tail & ld->sq_ring_mask] = io_u->index; |
9a2d78b3 | 394 | *ring->tail = next_tail; |
52885fa2 JA |
395 | write_barrier(); |
396 | ||
397 | ld->queued++; | |
398 | return FIO_Q_QUEUED; | |
399 | } | |
400 | ||
bffad86f | 401 | static void fio_ioring_queued(struct thread_data *td, int start, int nr) |
52885fa2 | 402 | { |
bffad86f | 403 | struct ioring_data *ld = td->io_ops_data; |
52885fa2 JA |
404 | struct timespec now; |
405 | ||
406 | if (!fio_fill_issue_time(td)) | |
407 | return; | |
408 | ||
409 | fio_gettime(&now, NULL); | |
410 | ||
411 | while (nr--) { | |
bffad86f | 412 | struct io_sq_ring *ring = &ld->sq_ring; |
9a2d78b3 | 413 | int index = ring->array[start & ld->sq_ring_mask]; |
f8289afc | 414 | struct io_u *io_u = ld->io_u_index[index]; |
52885fa2 JA |
415 | |
416 | memcpy(&io_u->issue_time, &now, sizeof(now)); | |
417 | io_u_queued(td, io_u); | |
418 | ||
419 | start++; | |
52885fa2 JA |
420 | } |
421 | } | |
422 | ||
bffad86f | 423 | static int fio_ioring_commit(struct thread_data *td) |
52885fa2 | 424 | { |
bffad86f JA |
425 | struct ioring_data *ld = td->io_ops_data; |
426 | struct ioring_options *o = td->eo; | |
52885fa2 JA |
427 | int ret; |
428 | ||
429 | if (!ld->queued) | |
430 | return 0; | |
431 | ||
3d7d00a3 JA |
432 | /* |
433 | * Kernel side does submission. just need to check if the ring is | |
434 | * flagged as needing a kick, if so, call io_uring_enter(). This | |
435 | * only happens if we've been idle too long. | |
436 | */ | |
437 | if (o->sqpoll_thread) { | |
bffad86f | 438 | struct io_sq_ring *ring = &ld->sq_ring; |
4cdbc048 | 439 | |
2ea53ca3 | 440 | read_barrier(); |
9a2d78b3 | 441 | if (*ring->flags & IORING_SQ_NEED_WAKEUP) |
b532dd6d JA |
442 | io_uring_enter(ld, ld->queued, 0, |
443 | IORING_ENTER_SQ_WAKEUP); | |
771c9901 JA |
444 | ld->queued = 0; |
445 | return 0; | |
446 | } | |
447 | ||
52885fa2 | 448 | do { |
9a2d78b3 | 449 | unsigned start = *ld->sq_ring.head; |
52885fa2 JA |
450 | long nr = ld->queued; |
451 | ||
9a2d78b3 | 452 | ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS); |
52885fa2 | 453 | if (ret > 0) { |
bffad86f | 454 | fio_ioring_queued(td, start, ret); |
52885fa2 JA |
455 | io_u_mark_submit(td, ret); |
456 | ||
457 | ld->queued -= ret; | |
458 | ret = 0; | |
a90cd050 JA |
459 | } else if (!ret) { |
460 | io_u_mark_submit(td, ret); | |
52885fa2 | 461 | continue; |
a90cd050 | 462 | } else { |
f6abd731 | 463 | if (errno == EAGAIN || errno == EINTR) { |
bffad86f | 464 | ret = fio_ioring_cqring_reap(td, 0, ld->queued); |
a90cd050 JA |
465 | if (ret) |
466 | continue; | |
467 | /* Shouldn't happen */ | |
468 | usleep(1); | |
469 | continue; | |
52885fa2 | 470 | } |
9a2d78b3 | 471 | td_verror(td, errno, "io_uring_enter submit"); |
52885fa2 | 472 | break; |
a90cd050 | 473 | } |
52885fa2 JA |
474 | } while (ld->queued); |
475 | ||
476 | return ret; | |
477 | } | |
478 | ||
bffad86f | 479 | static void fio_ioring_unmap(struct ioring_data *ld) |
52885fa2 | 480 | { |
9a2d78b3 | 481 | int i; |
52885fa2 | 482 | |
9a2d78b3 JA |
483 | for (i = 0; i < ARRAY_SIZE(ld->mmap); i++) |
484 | munmap(ld->mmap[i].ptr, ld->mmap[i].len); | |
485 | close(ld->ring_fd); | |
b87aa01a JA |
486 | } |
487 | ||
bffad86f | 488 | static void fio_ioring_cleanup(struct thread_data *td) |
52885fa2 | 489 | { |
bffad86f | 490 | struct ioring_data *ld = td->io_ops_data; |
52885fa2 JA |
491 | |
492 | if (ld) { | |
52885fa2 | 493 | if (!(td->flags & TD_F_CHILD)) |
bffad86f | 494 | fio_ioring_unmap(ld); |
9a2d78b3 | 495 | |
52885fa2 | 496 | free(ld->io_u_index); |
9a2d78b3 | 497 | free(ld->iovecs); |
5ffd5626 | 498 | free(ld->fds); |
52885fa2 JA |
499 | free(ld); |
500 | } | |
501 | } | |
502 | ||
bffad86f | 503 | static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p) |
9a2d78b3 | 504 | { |
bffad86f JA |
505 | struct io_sq_ring *sring = &ld->sq_ring; |
506 | struct io_cq_ring *cring = &ld->cq_ring; | |
9a2d78b3 JA |
507 | void *ptr; |
508 | ||
e2239016 | 509 | ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32); |
9a2d78b3 JA |
510 | ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE, |
511 | MAP_SHARED | MAP_POPULATE, ld->ring_fd, | |
512 | IORING_OFF_SQ_RING); | |
513 | ld->mmap[0].ptr = ptr; | |
514 | sring->head = ptr + p->sq_off.head; | |
515 | sring->tail = ptr + p->sq_off.tail; | |
516 | sring->ring_mask = ptr + p->sq_off.ring_mask; | |
517 | sring->ring_entries = ptr + p->sq_off.ring_entries; | |
518 | sring->flags = ptr + p->sq_off.flags; | |
ac122fea | 519 | sring->array = ptr + p->sq_off.array; |
9a2d78b3 JA |
520 | ld->sq_ring_mask = *sring->ring_mask; |
521 | ||
f0403f94 JA |
522 | ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe); |
523 | ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE, | |
9a2d78b3 | 524 | MAP_SHARED | MAP_POPULATE, ld->ring_fd, |
f0403f94 JA |
525 | IORING_OFF_SQES); |
526 | ld->mmap[1].ptr = ld->sqes; | |
9a2d78b3 | 527 | |
f0403f94 JA |
528 | ld->mmap[2].len = p->cq_off.cqes + |
529 | p->cq_entries * sizeof(struct io_uring_cqe); | |
9a2d78b3 JA |
530 | ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE, |
531 | MAP_SHARED | MAP_POPULATE, ld->ring_fd, | |
532 | IORING_OFF_CQ_RING); | |
533 | ld->mmap[2].ptr = ptr; | |
534 | cring->head = ptr + p->cq_off.head; | |
535 | cring->tail = ptr + p->cq_off.tail; | |
536 | cring->ring_mask = ptr + p->cq_off.ring_mask; | |
537 | cring->ring_entries = ptr + p->cq_off.ring_entries; | |
f0403f94 | 538 | cring->cqes = ptr + p->cq_off.cqes; |
9a2d78b3 JA |
539 | ld->cq_ring_mask = *cring->ring_mask; |
540 | return 0; | |
541 | } | |
542 | ||
bffad86f | 543 | static int fio_ioring_queue_init(struct thread_data *td) |
52885fa2 | 544 | { |
bffad86f JA |
545 | struct ioring_data *ld = td->io_ops_data; |
546 | struct ioring_options *o = td->eo; | |
52885fa2 | 547 | int depth = td->o.iodepth; |
bffad86f | 548 | struct io_uring_params p; |
9a2d78b3 JA |
549 | int ret; |
550 | ||
551 | memset(&p, 0, sizeof(p)); | |
52885fa2 JA |
552 | |
553 | if (o->hipri) | |
bffad86f | 554 | p.flags |= IORING_SETUP_IOPOLL; |
3d7d00a3 JA |
555 | if (o->sqpoll_thread) { |
556 | p.flags |= IORING_SETUP_SQPOLL; | |
557 | if (o->sqpoll_set) { | |
558 | p.flags |= IORING_SETUP_SQ_AFF; | |
559 | p.sq_thread_cpu = o->sqpoll_cpu; | |
560 | } | |
f635f1fb | 561 | } |
a90cd050 | 562 | |
bfed648c | 563 | ret = syscall(__NR_io_uring_setup, depth, &p); |
9a2d78b3 JA |
564 | if (ret < 0) |
565 | return ret; | |
566 | ||
567 | ld->ring_fd = ret; | |
2ea53ca3 JA |
568 | |
569 | if (o->fixedbufs) { | |
2d644205 JA |
570 | struct rlimit rlim = { |
571 | .rlim_cur = RLIM_INFINITY, | |
572 | .rlim_max = RLIM_INFINITY, | |
573 | }; | |
574 | ||
575 | if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) | |
576 | return -1; | |
577 | ||
bfed648c | 578 | ret = syscall(__NR_io_uring_register, ld->ring_fd, |
919850d2 | 579 | IORING_REGISTER_BUFFERS, ld->iovecs, depth); |
2ea53ca3 JA |
580 | if (ret < 0) |
581 | return ret; | |
582 | } | |
583 | ||
bffad86f | 584 | return fio_ioring_mmap(ld, &p); |
52885fa2 JA |
585 | } |
586 | ||
5ffd5626 JA |
587 | static int fio_ioring_register_files(struct thread_data *td) |
588 | { | |
589 | struct ioring_data *ld = td->io_ops_data; | |
590 | struct fio_file *f; | |
591 | unsigned int i; | |
592 | int ret; | |
593 | ||
594 | ld->fds = calloc(td->o.nr_files, sizeof(int)); | |
595 | ||
596 | for_each_file(td, f, i) { | |
597 | ret = generic_open_file(td, f); | |
598 | if (ret) | |
599 | goto err; | |
600 | ld->fds[i] = f->fd; | |
601 | f->engine_pos = i; | |
602 | } | |
603 | ||
bfed648c | 604 | ret = syscall(__NR_io_uring_register, ld->ring_fd, |
5ffd5626 JA |
605 | IORING_REGISTER_FILES, ld->fds, td->o.nr_files); |
606 | if (ret) { | |
607 | err: | |
608 | free(ld->fds); | |
609 | ld->fds = NULL; | |
610 | } | |
611 | ||
612 | /* | |
613 | * Pretend the file is closed again, and really close it if we hit | |
614 | * an error. | |
615 | */ | |
616 | for_each_file(td, f, i) { | |
617 | if (ret) { | |
618 | int fio_unused ret2; | |
619 | ret2 = generic_close_file(td, f); | |
620 | } else | |
621 | f->fd = -1; | |
622 | } | |
623 | ||
624 | return ret; | |
625 | } | |
626 | ||
bffad86f | 627 | static int fio_ioring_post_init(struct thread_data *td) |
52885fa2 | 628 | { |
bffad86f | 629 | struct ioring_data *ld = td->io_ops_data; |
5ffd5626 | 630 | struct ioring_options *o = td->eo; |
52885fa2 | 631 | struct io_u *io_u; |
650346e1 | 632 | int err, i; |
52885fa2 | 633 | |
650346e1 JA |
634 | for (i = 0; i < td->o.iodepth; i++) { |
635 | struct iovec *iov = &ld->iovecs[i]; | |
9a2d78b3 | 636 | |
650346e1 JA |
637 | io_u = ld->io_u_index[i]; |
638 | iov->iov_base = io_u->buf; | |
639 | iov->iov_len = td_max_bs(td); | |
52885fa2 JA |
640 | } |
641 | ||
bffad86f | 642 | err = fio_ioring_queue_init(td); |
52885fa2 | 643 | if (err) { |
d63a472d | 644 | td_verror(td, errno, "io_queue_init"); |
52885fa2 JA |
645 | return 1; |
646 | } | |
647 | ||
5ffd5626 JA |
648 | if (o->registerfiles) { |
649 | err = fio_ioring_register_files(td); | |
650 | if (err) { | |
651 | td_verror(td, errno, "ioring_register_files"); | |
652 | return 1; | |
653 | } | |
654 | } | |
655 | ||
52885fa2 JA |
656 | return 0; |
657 | } | |
658 | ||
9a2d78b3 JA |
659 | static unsigned roundup_pow2(unsigned depth) |
660 | { | |
661 | return 1UL << __fls(depth - 1); | |
662 | } | |
663 | ||
bffad86f | 664 | static int fio_ioring_init(struct thread_data *td) |
52885fa2 | 665 | { |
5ffd5626 | 666 | struct ioring_options *o = td->eo; |
bffad86f | 667 | struct ioring_data *ld; |
b2a432bf | 668 | struct thread_options *to = &td->o; |
52885fa2 | 669 | |
5ffd5626 JA |
670 | /* sqthread submission requires registered files */ |
671 | if (o->sqpoll_thread) | |
672 | o->registerfiles = 1; | |
673 | ||
674 | if (o->registerfiles && td->o.nr_files != td->o.open_files) { | |
675 | log_err("fio: io_uring registered files require nr_files to " | |
676 | "be identical to open_files\n"); | |
677 | return 1; | |
678 | } | |
679 | ||
52885fa2 JA |
680 | ld = calloc(1, sizeof(*ld)); |
681 | ||
b87aa01a JA |
682 | /* ring depth must be a power-of-2 */ |
683 | ld->iodepth = td->o.iodepth; | |
684 | td->o.iodepth = roundup_pow2(td->o.iodepth); | |
685 | ||
52885fa2 JA |
686 | /* io_u index */ |
687 | ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *)); | |
650346e1 | 688 | ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec)); |
52885fa2 JA |
689 | |
690 | td->io_ops_data = ld; | |
b2a432bf PC |
691 | |
692 | /* | |
693 | * Check for option conflicts | |
694 | */ | |
695 | if ((fio_option_is_set(to, ioprio) || fio_option_is_set(to, ioprio_class)) && | |
696 | o->cmdprio_percentage != 0) { | |
697 | log_err("%s: cmdprio_percentage option and mutually exclusive " | |
698 | "prio or prioclass option is set, exiting\n", to->name); | |
699 | td_verror(td, EINVAL, "fio_io_uring_init"); | |
700 | return 1; | |
701 | } | |
1af44196 XW |
702 | |
703 | if (fio_option_is_set(&td->o, ioprio_class)) | |
704 | ld->ioprio_class_set = true; | |
705 | if (fio_option_is_set(&td->o, ioprio)) | |
706 | ld->ioprio_set = true; | |
707 | ||
52885fa2 JA |
708 | return 0; |
709 | } | |
710 | ||
bffad86f | 711 | static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u) |
52885fa2 | 712 | { |
bffad86f | 713 | struct ioring_data *ld = td->io_ops_data; |
52885fa2 JA |
714 | |
715 | ld->io_u_index[io_u->index] = io_u; | |
716 | return 0; | |
717 | } | |
718 | ||
5ffd5626 JA |
719 | static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f) |
720 | { | |
721 | struct ioring_data *ld = td->io_ops_data; | |
722 | struct ioring_options *o = td->eo; | |
723 | ||
17318cf6 | 724 | if (!ld || !o->registerfiles) |
5ffd5626 JA |
725 | return generic_open_file(td, f); |
726 | ||
727 | f->fd = ld->fds[f->engine_pos]; | |
728 | return 0; | |
729 | } | |
730 | ||
731 | static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f) | |
732 | { | |
17318cf6 | 733 | struct ioring_data *ld = td->io_ops_data; |
5ffd5626 JA |
734 | struct ioring_options *o = td->eo; |
735 | ||
17318cf6 | 736 | if (!ld || !o->registerfiles) |
5ffd5626 JA |
737 | return generic_close_file(td, f); |
738 | ||
739 | f->fd = -1; | |
740 | return 0; | |
741 | } | |
742 | ||
52885fa2 | 743 | static struct ioengine_ops ioengine = { |
bffad86f | 744 | .name = "io_uring", |
52885fa2 | 745 | .version = FIO_IOOPS_VERSION, |
04ba61df | 746 | .flags = FIO_ASYNCIO_SYNC_TRIM, |
bffad86f JA |
747 | .init = fio_ioring_init, |
748 | .post_init = fio_ioring_post_init, | |
749 | .io_u_init = fio_ioring_io_u_init, | |
750 | .prep = fio_ioring_prep, | |
751 | .queue = fio_ioring_queue, | |
752 | .commit = fio_ioring_commit, | |
753 | .getevents = fio_ioring_getevents, | |
754 | .event = fio_ioring_event, | |
755 | .cleanup = fio_ioring_cleanup, | |
5ffd5626 JA |
756 | .open_file = fio_ioring_open_file, |
757 | .close_file = fio_ioring_close_file, | |
52885fa2 JA |
758 | .get_file_size = generic_get_file_size, |
759 | .options = options, | |
bffad86f | 760 | .option_struct_size = sizeof(struct ioring_options), |
52885fa2 JA |
761 | }; |
762 | ||
bffad86f | 763 | static void fio_init fio_ioring_register(void) |
52885fa2 | 764 | { |
52885fa2 | 765 | register_ioengine(&ioengine); |
52885fa2 JA |
766 | } |
767 | ||
bffad86f | 768 | static void fio_exit fio_ioring_unregister(void) |
52885fa2 | 769 | { |
52885fa2 | 770 | unregister_ioengine(&ioengine); |
52885fa2 | 771 | } |
1f90e9bb | 772 | #endif |