io_uring: clamp CQ size to SQ size
[fio.git] / engines / io_uring.c
CommitLineData
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"
6d975f2c 20#include "../lib/roundup.h"
52885fa2 21
bffad86f 22#ifdef ARCH_HAVE_IOURING
52885fa2 23
57fa61f0 24#include "../lib/types.h"
f3e769a4 25#include "../os/linux/io_uring.h"
e9f6567a 26#include "cmdprio.h"
9a2d78b3 27
bffad86f 28struct io_sq_ring {
e2239016
JA
29 unsigned *head;
30 unsigned *tail;
31 unsigned *ring_mask;
32 unsigned *ring_entries;
33 unsigned *flags;
34 unsigned *array;
52885fa2
JA
35};
36
bffad86f 37struct io_cq_ring {
e2239016
JA
38 unsigned *head;
39 unsigned *tail;
40 unsigned *ring_mask;
41 unsigned *ring_entries;
f0403f94 42 struct io_uring_cqe *cqes;
9a2d78b3
JA
43};
44
bffad86f 45struct ioring_mmap {
9a2d78b3
JA
46 void *ptr;
47 size_t len;
52885fa2
JA
48};
49
bffad86f 50struct ioring_data {
9a2d78b3
JA
51 int ring_fd;
52
52885fa2
JA
53 struct io_u **io_u_index;
54
5ffd5626
JA
55 int *fds;
56
bffad86f 57 struct io_sq_ring sq_ring;
f0403f94 58 struct io_uring_sqe *sqes;
9a2d78b3 59 struct iovec *iovecs;
b87aa01a 60 unsigned sq_ring_mask;
52885fa2 61
bffad86f 62 struct io_cq_ring cq_ring;
b87aa01a 63 unsigned cq_ring_mask;
52885fa2
JA
64
65 int queued;
66 int cq_ring_off;
b87aa01a 67 unsigned iodepth;
5a59a81d 68 int prepped;
96563db9 69
bffad86f 70 struct ioring_mmap mmap[3];
d6cbeab4
NC
71
72 struct cmdprio cmdprio;
52885fa2
JA
73};
74
bffad86f 75struct ioring_options {
a48f0cc7 76 struct thread_data *td;
52885fa2 77 unsigned int hipri;
d6cbeab4 78 struct cmdprio_options cmdprio_options;
52885fa2 79 unsigned int fixedbufs;
5ffd5626 80 unsigned int registerfiles;
3d7d00a3 81 unsigned int sqpoll_thread;
2ea53ca3
JA
82 unsigned int sqpoll_set;
83 unsigned int sqpoll_cpu;
b10b1e70 84 unsigned int nonvectored;
4a87b584 85 unsigned int uncached;
7d42e66e 86 unsigned int nowait;
5a59a81d 87 unsigned int force_async;
52885fa2
JA
88};
89
b10b1e70
JA
90static const int ddir_to_op[2][2] = {
91 { IORING_OP_READV, IORING_OP_READ },
92 { IORING_OP_WRITEV, IORING_OP_WRITE }
93};
94
3f1e3af7
KB
95static const int fixed_ddir_to_op[2] = {
96 IORING_OP_READ_FIXED,
97 IORING_OP_WRITE_FIXED
98};
99
2ea53ca3 100static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
a90cd050 101{
bffad86f 102 struct ioring_options *o = data;
a90cd050 103
2ea53ca3
JA
104 o->sqpoll_cpu = *val;
105 o->sqpoll_set = 1;
a90cd050
JA
106 return 0;
107}
108
52885fa2
JA
109static struct fio_option options[] = {
110 {
111 .name = "hipri",
112 .lname = "High Priority",
113 .type = FIO_OPT_STR_SET,
bffad86f 114 .off1 = offsetof(struct ioring_options, hipri),
52885fa2
JA
115 .help = "Use polled IO completions",
116 .category = FIO_OPT_C_ENGINE,
27f436d9 117 .group = FIO_OPT_G_IOURING,
52885fa2 118 },
b2a432bf
PC
119#ifdef FIO_HAVE_IOPRIO_CLASS
120 {
121 .name = "cmdprio_percentage",
122 .lname = "high priority percentage",
123 .type = FIO_OPT_INT,
e9f6567a 124 .off1 = offsetof(struct ioring_options,
d6cbeab4 125 cmdprio_options.percentage[DDIR_READ]),
e9f6567a 126 .off2 = offsetof(struct ioring_options,
d6cbeab4 127 cmdprio_options.percentage[DDIR_WRITE]),
e9f6567a 128 .minval = 0,
b2a432bf
PC
129 .maxval = 100,
130 .help = "Send high priority I/O this percentage of the time",
131 .category = FIO_OPT_C_ENGINE,
132 .group = FIO_OPT_G_IOURING,
133 },
12f9d54a
DLM
134 {
135 .name = "cmdprio_class",
136 .lname = "Asynchronous I/O priority class",
137 .type = FIO_OPT_INT,
138 .off1 = offsetof(struct ioring_options,
d6cbeab4 139 cmdprio_options.class[DDIR_READ]),
12f9d54a 140 .off2 = offsetof(struct ioring_options,
d6cbeab4 141 cmdprio_options.class[DDIR_WRITE]),
12f9d54a
DLM
142 .help = "Set asynchronous IO priority class",
143 .minval = IOPRIO_MIN_PRIO_CLASS + 1,
144 .maxval = IOPRIO_MAX_PRIO_CLASS,
145 .interval = 1,
146 .category = FIO_OPT_C_ENGINE,
147 .group = FIO_OPT_G_IOURING,
148 },
149 {
150 .name = "cmdprio",
151 .lname = "Asynchronous I/O priority level",
152 .type = FIO_OPT_INT,
153 .off1 = offsetof(struct ioring_options,
d6cbeab4 154 cmdprio_options.level[DDIR_READ]),
12f9d54a 155 .off2 = offsetof(struct ioring_options,
d6cbeab4 156 cmdprio_options.level[DDIR_WRITE]),
12f9d54a
DLM
157 .help = "Set asynchronous IO priority level",
158 .minval = IOPRIO_MIN_PRIO,
159 .maxval = IOPRIO_MAX_PRIO,
160 .interval = 1,
161 .category = FIO_OPT_C_ENGINE,
162 .group = FIO_OPT_G_IOURING,
163 },
a48f0cc7
DLM
164 {
165 .name = "cmdprio_bssplit",
166 .lname = "Priority percentage block size split",
d6cbeab4
NC
167 .type = FIO_OPT_STR_STORE,
168 .off1 = offsetof(struct ioring_options,
169 cmdprio_options.bssplit_str),
a48f0cc7
DLM
170 .help = "Set priority percentages for different block sizes",
171 .category = FIO_OPT_C_ENGINE,
172 .group = FIO_OPT_G_IOURING,
173 },
b2a432bf
PC
174#else
175 {
176 .name = "cmdprio_percentage",
177 .lname = "high priority percentage",
178 .type = FIO_OPT_UNSUPPORTED,
179 .help = "Your platform does not support I/O priority classes",
180 },
12f9d54a
DLM
181 {
182 .name = "cmdprio_class",
183 .lname = "Asynchronous I/O priority class",
184 .type = FIO_OPT_UNSUPPORTED,
185 .help = "Your platform does not support I/O priority classes",
186 },
187 {
188 .name = "cmdprio",
189 .lname = "Asynchronous I/O priority level",
190 .type = FIO_OPT_UNSUPPORTED,
191 .help = "Your platform does not support I/O priority classes",
192 },
a48f0cc7
DLM
193 {
194 .name = "cmdprio_bssplit",
195 .lname = "Priority percentage block size split",
196 .type = FIO_OPT_UNSUPPORTED,
197 .help = "Your platform does not support I/O priority classes",
198 },
b2a432bf 199#endif
52885fa2
JA
200 {
201 .name = "fixedbufs",
202 .lname = "Fixed (pre-mapped) IO buffers",
203 .type = FIO_OPT_STR_SET,
bffad86f 204 .off1 = offsetof(struct ioring_options, fixedbufs),
52885fa2
JA
205 .help = "Pre map IO buffers",
206 .category = FIO_OPT_C_ENGINE,
27f436d9 207 .group = FIO_OPT_G_IOURING,
52885fa2 208 },
5ffd5626
JA
209 {
210 .name = "registerfiles",
211 .lname = "Register file set",
212 .type = FIO_OPT_STR_SET,
213 .off1 = offsetof(struct ioring_options, registerfiles),
214 .help = "Pre-open/register files",
215 .category = FIO_OPT_C_ENGINE,
27f436d9 216 .group = FIO_OPT_G_IOURING,
5ffd5626 217 },
771c9901
JA
218 {
219 .name = "sqthread_poll",
3d7d00a3
JA
220 .lname = "Kernel SQ thread polling",
221 .type = FIO_OPT_INT,
222 .off1 = offsetof(struct ioring_options, sqpoll_thread),
223 .help = "Offload submission/completion to kernel thread",
224 .category = FIO_OPT_C_ENGINE,
27f436d9 225 .group = FIO_OPT_G_IOURING,
3d7d00a3
JA
226 },
227 {
228 .name = "sqthread_poll_cpu",
229 .lname = "SQ Thread Poll CPU",
2ea53ca3
JA
230 .type = FIO_OPT_INT,
231 .cb = fio_ioring_sqpoll_cb,
3d7d00a3 232 .help = "What CPU to run SQ thread polling on",
a90cd050 233 .category = FIO_OPT_C_ENGINE,
27f436d9 234 .group = FIO_OPT_G_IOURING,
a90cd050 235 },
b10b1e70
JA
236 {
237 .name = "nonvectored",
238 .lname = "Non-vectored",
239 .type = FIO_OPT_INT,
240 .off1 = offsetof(struct ioring_options, nonvectored),
556d8415 241 .def = "-1",
b10b1e70
JA
242 .help = "Use non-vectored read/write commands",
243 .category = FIO_OPT_C_ENGINE,
244 .group = FIO_OPT_G_IOURING,
245 },
4a87b584
JA
246 {
247 .name = "uncached",
248 .lname = "Uncached",
249 .type = FIO_OPT_INT,
250 .off1 = offsetof(struct ioring_options, uncached),
251 .help = "Use RWF_UNCACHED for buffered read/writes",
252 .category = FIO_OPT_C_ENGINE,
253 .group = FIO_OPT_G_IOURING,
254 },
7d42e66e
KK
255 {
256 .name = "nowait",
257 .lname = "RWF_NOWAIT",
258 .type = FIO_OPT_BOOL,
259 .off1 = offsetof(struct ioring_options, nowait),
260 .help = "Use RWF_NOWAIT for reads/writes",
261 .category = FIO_OPT_C_ENGINE,
262 .group = FIO_OPT_G_IOURING,
263 },
5a59a81d
JA
264 {
265 .name = "force_async",
266 .lname = "Force async",
267 .type = FIO_OPT_INT,
268 .off1 = offsetof(struct ioring_options, force_async),
269 .help = "Set IOSQE_ASYNC every N requests",
270 .category = FIO_OPT_C_ENGINE,
271 .group = FIO_OPT_G_IOURING,
272 },
52885fa2
JA
273 {
274 .name = NULL,
275 },
276};
277
bffad86f 278static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
52885fa2
JA
279 unsigned int min_complete, unsigned int flags)
280{
bfed648c 281 return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit,
521164fa 282 min_complete, flags, NULL, 0);
52885fa2
JA
283}
284
bffad86f 285static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
52885fa2 286{
bffad86f 287 struct ioring_data *ld = td->io_ops_data;
cfcc8564 288 struct ioring_options *o = td->eo;
52885fa2 289 struct fio_file *f = io_u->file;
f0403f94 290 struct io_uring_sqe *sqe;
52885fa2 291
f0403f94 292 sqe = &ld->sqes[io_u->index];
34d6090e 293
5ffd5626
JA
294 if (o->registerfiles) {
295 sqe->fd = f->engine_pos;
296 sqe->flags = IOSQE_FIXED_FILE;
297 } else {
298 sqe->fd = f->fd;
87b69ef2 299 sqe->flags = 0;
5ffd5626 300 }
52885fa2 301
e3970057 302 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
f0403f94 303 if (o->fixedbufs) {
3f1e3af7 304 sqe->opcode = fixed_ddir_to_op[io_u->ddir];
919850d2 305 sqe->addr = (unsigned long) io_u->xfer_buf;
f0403f94 306 sqe->len = io_u->xfer_buflen;
2ea53ca3 307 sqe->buf_index = io_u->index;
cfcc8564 308 } else {
832faaaf
JA
309 struct iovec *iov = &ld->iovecs[io_u->index];
310
311 /*
312 * Update based on actual io_u, requeue could have
313 * adjusted these
314 */
315 iov->iov_base = io_u->xfer_buf;
316 iov->iov_len = io_u->xfer_buflen;
317
3f1e3af7 318 sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
b10b1e70 319 if (o->nonvectored) {
832faaaf
JA
320 sqe->addr = (unsigned long) iov->iov_base;
321 sqe->len = iov->iov_len;
b10b1e70 322 } else {
832faaaf 323 sqe->addr = (unsigned long) iov;
b10b1e70
JA
324 sqe->len = 1;
325 }
cfcc8564 326 }
fd70e361 327 sqe->rw_flags = 0;
4a87b584 328 if (!td->o.odirect && o->uncached)
fd70e361 329 sqe->rw_flags |= RWF_UNCACHED;
7d42e66e
KK
330 if (o->nowait)
331 sqe->rw_flags |= RWF_NOWAIT;
8ff6b289
NC
332
333 /*
334 * Since io_uring can have a submission context (sqthread_poll)
335 * that is different from the process context, we cannot rely on
336 * the IO priority set by ioprio_set() (option prio/prioclass)
337 * to be inherited.
338 * td->ioprio will have the value of the "default prio", so set
339 * this unconditionally. This value might get overridden by
ff00f247 340 * fio_ioring_cmdprio_prep() if the option cmdprio_percentage or
8ff6b289
NC
341 * cmdprio_bssplit is used.
342 */
343 sqe->ioprio = td->ioprio;
f0403f94 344 sqe->off = io_u->offset;
48e698fa 345 } else if (ddir_sync(io_u->ddir)) {
7c70f506 346 sqe->ioprio = 0;
01387bfe
AF
347 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
348 sqe->off = f->first_write;
349 sqe->len = f->last_write - f->first_write;
350 sqe->sync_range_flags = td->o.sync_file_range;
351 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
352 } else {
7c70f506
JA
353 sqe->off = 0;
354 sqe->addr = 0;
355 sqe->len = 0;
01387bfe
AF
356 if (io_u->ddir == DDIR_DATASYNC)
357 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
358 sqe->opcode = IORING_OP_FSYNC;
359 }
48e698fa 360 }
52885fa2 361
5a59a81d
JA
362 if (o->force_async && ++ld->prepped == o->force_async) {
363 ld->prepped = 0;
364 sqe->flags |= IOSQE_ASYNC;
365 }
366
48e698fa 367 sqe->user_data = (unsigned long) io_u;
52885fa2
JA
368 return 0;
369}
370
bffad86f 371static struct io_u *fio_ioring_event(struct thread_data *td, int event)
52885fa2 372{
bffad86f 373 struct ioring_data *ld = td->io_ops_data;
f0403f94 374 struct io_uring_cqe *cqe;
52885fa2 375 struct io_u *io_u;
b87aa01a 376 unsigned index;
52885fa2 377
b87aa01a 378 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
52885fa2 379
f0403f94 380 cqe = &ld->cq_ring.cqes[index];
e3466352 381 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
52885fa2 382
f0403f94
JA
383 if (cqe->res != io_u->xfer_buflen) {
384 if (cqe->res > io_u->xfer_buflen)
385 io_u->error = -cqe->res;
52885fa2 386 else
f0403f94 387 io_u->resid = io_u->xfer_buflen - cqe->res;
52885fa2
JA
388 } else
389 io_u->error = 0;
390
391 return io_u;
392}
393
bffad86f 394static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
52885fa2
JA
395 unsigned int max)
396{
bffad86f
JA
397 struct ioring_data *ld = td->io_ops_data;
398 struct io_cq_ring *ring = &ld->cq_ring;
e2239016 399 unsigned head, reaped = 0;
52885fa2 400
9a2d78b3 401 head = *ring->head;
52885fa2 402 do {
9e26aff9 403 if (head == atomic_load_acquire(ring->tail))
52885fa2
JA
404 break;
405 reaped++;
406 head++;
52885fa2
JA
407 } while (reaped + events < max);
408
76ce63dd
AB
409 if (reaped)
410 atomic_store_release(ring->head, head);
411
52885fa2
JA
412 return reaped;
413}
414
bffad86f
JA
415static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
416 unsigned int max, const struct timespec *t)
52885fa2 417{
bffad86f 418 struct ioring_data *ld = td->io_ops_data;
52885fa2 419 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
bffad86f
JA
420 struct ioring_options *o = td->eo;
421 struct io_cq_ring *ring = &ld->cq_ring;
b87aa01a
JA
422 unsigned events = 0;
423 int r;
52885fa2 424
9a2d78b3 425 ld->cq_ring_off = *ring->head;
52885fa2 426 do {
bffad86f 427 r = fio_ioring_cqring_reap(td, events, max);
52885fa2
JA
428 if (r) {
429 events += r;
f7cbbbf8
ST
430 if (actual_min != 0)
431 actual_min -= r;
52885fa2
JA
432 continue;
433 }
434
3d7d00a3 435 if (!o->sqpoll_thread) {
9a2d78b3
JA
436 r = io_uring_enter(ld, 0, actual_min,
437 IORING_ENTER_GETEVENTS);
771c9901 438 if (r < 0) {
f6abd731 439 if (errno == EAGAIN || errno == EINTR)
771c9901 440 continue;
9a2d78b3 441 td_verror(td, errno, "io_uring_enter");
771c9901
JA
442 break;
443 }
52885fa2
JA
444 }
445 } while (events < min);
446
447 return r < 0 ? r : events;
448}
449
127715b6
NC
450static inline void fio_ioring_cmdprio_prep(struct thread_data *td,
451 struct io_u *io_u)
b2a432bf 452{
b2a432bf 453 struct ioring_data *ld = td->io_ops_data;
d6cbeab4 454 struct cmdprio *cmdprio = &ld->cmdprio;
127715b6
NC
455
456 if (fio_cmdprio_set_ioprio(td, cmdprio, io_u))
457 ld->sqes[io_u->index].ioprio = io_u->ioprio;
b2a432bf
PC
458}
459
bffad86f
JA
460static enum fio_q_status fio_ioring_queue(struct thread_data *td,
461 struct io_u *io_u)
52885fa2 462{
bffad86f
JA
463 struct ioring_data *ld = td->io_ops_data;
464 struct io_sq_ring *ring = &ld->sq_ring;
52885fa2
JA
465 unsigned tail, next_tail;
466
467 fio_ro_check(td, io_u);
468
b87aa01a 469 if (ld->queued == ld->iodepth)
52885fa2
JA
470 return FIO_Q_BUSY;
471
52885fa2
JA
472 if (io_u->ddir == DDIR_TRIM) {
473 if (ld->queued)
474 return FIO_Q_BUSY;
475
476 do_io_u_trim(td, io_u);
477 io_u_mark_submit(td, 1);
478 io_u_mark_complete(td, 1);
479 return FIO_Q_COMPLETED;
480 }
481
9a2d78b3 482 tail = *ring->tail;
52885fa2 483 next_tail = tail + 1;
9e26aff9 484 if (next_tail == atomic_load_acquire(ring->head))
52885fa2
JA
485 return FIO_Q_BUSY;
486
d6cbeab4 487 if (ld->cmdprio.mode != CMDPRIO_MODE_NONE)
ff00f247
NC
488 fio_ioring_cmdprio_prep(td, io_u);
489
b87aa01a 490 ring->array[tail & ld->sq_ring_mask] = io_u->index;
9e26aff9 491 atomic_store_release(ring->tail, next_tail);
52885fa2
JA
492
493 ld->queued++;
494 return FIO_Q_QUEUED;
495}
496
bffad86f 497static void fio_ioring_queued(struct thread_data *td, int start, int nr)
52885fa2 498{
bffad86f 499 struct ioring_data *ld = td->io_ops_data;
52885fa2
JA
500 struct timespec now;
501
502 if (!fio_fill_issue_time(td))
503 return;
504
505 fio_gettime(&now, NULL);
506
507 while (nr--) {
bffad86f 508 struct io_sq_ring *ring = &ld->sq_ring;
9a2d78b3 509 int index = ring->array[start & ld->sq_ring_mask];
f8289afc 510 struct io_u *io_u = ld->io_u_index[index];
52885fa2
JA
511
512 memcpy(&io_u->issue_time, &now, sizeof(now));
513 io_u_queued(td, io_u);
514
515 start++;
52885fa2
JA
516 }
517}
518
bffad86f 519static int fio_ioring_commit(struct thread_data *td)
52885fa2 520{
bffad86f
JA
521 struct ioring_data *ld = td->io_ops_data;
522 struct ioring_options *o = td->eo;
52885fa2
JA
523 int ret;
524
525 if (!ld->queued)
526 return 0;
527
3d7d00a3
JA
528 /*
529 * Kernel side does submission. just need to check if the ring is
530 * flagged as needing a kick, if so, call io_uring_enter(). This
531 * only happens if we've been idle too long.
532 */
533 if (o->sqpoll_thread) {
bffad86f 534 struct io_sq_ring *ring = &ld->sq_ring;
2dd96cc4 535 unsigned flags;
4cdbc048 536
2dd96cc4
JA
537 flags = atomic_load_acquire(ring->flags);
538 if (flags & IORING_SQ_NEED_WAKEUP)
b532dd6d
JA
539 io_uring_enter(ld, ld->queued, 0,
540 IORING_ENTER_SQ_WAKEUP);
771c9901
JA
541 ld->queued = 0;
542 return 0;
543 }
544
52885fa2 545 do {
9a2d78b3 546 unsigned start = *ld->sq_ring.head;
52885fa2
JA
547 long nr = ld->queued;
548
9a2d78b3 549 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
52885fa2 550 if (ret > 0) {
bffad86f 551 fio_ioring_queued(td, start, ret);
52885fa2
JA
552 io_u_mark_submit(td, ret);
553
554 ld->queued -= ret;
555 ret = 0;
a90cd050
JA
556 } else if (!ret) {
557 io_u_mark_submit(td, ret);
52885fa2 558 continue;
a90cd050 559 } else {
f6abd731 560 if (errno == EAGAIN || errno == EINTR) {
bffad86f 561 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
a90cd050
JA
562 if (ret)
563 continue;
564 /* Shouldn't happen */
565 usleep(1);
566 continue;
52885fa2 567 }
9a2d78b3 568 td_verror(td, errno, "io_uring_enter submit");
52885fa2 569 break;
a90cd050 570 }
52885fa2
JA
571 } while (ld->queued);
572
573 return ret;
574}
575
bffad86f 576static void fio_ioring_unmap(struct ioring_data *ld)
52885fa2 577{
9a2d78b3 578 int i;
52885fa2 579
59f94d26 580 for (i = 0; i < FIO_ARRAY_SIZE(ld->mmap); i++)
9a2d78b3
JA
581 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
582 close(ld->ring_fd);
b87aa01a
JA
583}
584
bffad86f 585static void fio_ioring_cleanup(struct thread_data *td)
52885fa2 586{
bffad86f 587 struct ioring_data *ld = td->io_ops_data;
52885fa2
JA
588
589 if (ld) {
52885fa2 590 if (!(td->flags & TD_F_CHILD))
bffad86f 591 fio_ioring_unmap(ld);
9a2d78b3 592
d6cbeab4 593 fio_cmdprio_cleanup(&ld->cmdprio);
52885fa2 594 free(ld->io_u_index);
9a2d78b3 595 free(ld->iovecs);
5ffd5626 596 free(ld->fds);
52885fa2
JA
597 free(ld);
598 }
599}
600
bffad86f 601static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
9a2d78b3 602{
bffad86f
JA
603 struct io_sq_ring *sring = &ld->sq_ring;
604 struct io_cq_ring *cring = &ld->cq_ring;
9a2d78b3
JA
605 void *ptr;
606
e2239016 607 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
9a2d78b3
JA
608 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
609 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
610 IORING_OFF_SQ_RING);
611 ld->mmap[0].ptr = ptr;
612 sring->head = ptr + p->sq_off.head;
613 sring->tail = ptr + p->sq_off.tail;
614 sring->ring_mask = ptr + p->sq_off.ring_mask;
615 sring->ring_entries = ptr + p->sq_off.ring_entries;
616 sring->flags = ptr + p->sq_off.flags;
ac122fea 617 sring->array = ptr + p->sq_off.array;
9a2d78b3
JA
618 ld->sq_ring_mask = *sring->ring_mask;
619
f0403f94
JA
620 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
621 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
9a2d78b3 622 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
f0403f94
JA
623 IORING_OFF_SQES);
624 ld->mmap[1].ptr = ld->sqes;
9a2d78b3 625
f0403f94
JA
626 ld->mmap[2].len = p->cq_off.cqes +
627 p->cq_entries * sizeof(struct io_uring_cqe);
9a2d78b3
JA
628 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
629 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
630 IORING_OFF_CQ_RING);
631 ld->mmap[2].ptr = ptr;
632 cring->head = ptr + p->cq_off.head;
633 cring->tail = ptr + p->cq_off.tail;
634 cring->ring_mask = ptr + p->cq_off.ring_mask;
635 cring->ring_entries = ptr + p->cq_off.ring_entries;
f0403f94 636 cring->cqes = ptr + p->cq_off.cqes;
9a2d78b3
JA
637 ld->cq_ring_mask = *cring->ring_mask;
638 return 0;
639}
640
556d8415
JA
641static void fio_ioring_probe(struct thread_data *td)
642{
643 struct ioring_data *ld = td->io_ops_data;
644 struct ioring_options *o = td->eo;
645 struct io_uring_probe *p;
646 int ret;
647
648 /* already set by user, don't touch */
649 if (o->nonvectored != -1)
650 return;
651
652 /* default to off, as that's always safe */
653 o->nonvectored = 0;
654
655 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
656 if (!p)
657 return;
658
659 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
660 ret = syscall(__NR_io_uring_register, ld->ring_fd,
661 IORING_REGISTER_PROBE, p, 256);
662 if (ret < 0)
663 goto out;
664
665 if (IORING_OP_WRITE > p->ops_len)
666 goto out;
667
668 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED) &&
669 (p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED))
670 o->nonvectored = 1;
671out:
672 free(p);
673}
674
bffad86f 675static int fio_ioring_queue_init(struct thread_data *td)
52885fa2 676{
bffad86f
JA
677 struct ioring_data *ld = td->io_ops_data;
678 struct ioring_options *o = td->eo;
52885fa2 679 int depth = td->o.iodepth;
bffad86f 680 struct io_uring_params p;
9a2d78b3
JA
681 int ret;
682
683 memset(&p, 0, sizeof(p));
52885fa2
JA
684
685 if (o->hipri)
bffad86f 686 p.flags |= IORING_SETUP_IOPOLL;
3d7d00a3
JA
687 if (o->sqpoll_thread) {
688 p.flags |= IORING_SETUP_SQPOLL;
689 if (o->sqpoll_set) {
690 p.flags |= IORING_SETUP_SQ_AFF;
691 p.sq_thread_cpu = o->sqpoll_cpu;
692 }
f635f1fb 693 }
a90cd050 694
1db268db
JA
695 /*
696 * Clamp CQ ring size at our SQ ring size, we don't need more entries
697 * than that.
698 */
699 p.flags |= IORING_SETUP_CQSIZE;
700 p.cq_entries = depth;
701
bfed648c 702 ret = syscall(__NR_io_uring_setup, depth, &p);
9a2d78b3
JA
703 if (ret < 0)
704 return ret;
705
706 ld->ring_fd = ret;
2ea53ca3 707
556d8415
JA
708 fio_ioring_probe(td);
709
2ea53ca3 710 if (o->fixedbufs) {
bfed648c 711 ret = syscall(__NR_io_uring_register, ld->ring_fd,
919850d2 712 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
2ea53ca3
JA
713 if (ret < 0)
714 return ret;
715 }
716
bffad86f 717 return fio_ioring_mmap(ld, &p);
52885fa2
JA
718}
719
5ffd5626
JA
720static int fio_ioring_register_files(struct thread_data *td)
721{
722 struct ioring_data *ld = td->io_ops_data;
723 struct fio_file *f;
724 unsigned int i;
725 int ret;
726
727 ld->fds = calloc(td->o.nr_files, sizeof(int));
728
729 for_each_file(td, f, i) {
730 ret = generic_open_file(td, f);
731 if (ret)
732 goto err;
733 ld->fds[i] = f->fd;
734 f->engine_pos = i;
735 }
736
bfed648c 737 ret = syscall(__NR_io_uring_register, ld->ring_fd,
5ffd5626
JA
738 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
739 if (ret) {
740err:
741 free(ld->fds);
742 ld->fds = NULL;
743 }
744
745 /*
746 * Pretend the file is closed again, and really close it if we hit
747 * an error.
748 */
749 for_each_file(td, f, i) {
750 if (ret) {
751 int fio_unused ret2;
752 ret2 = generic_close_file(td, f);
753 } else
754 f->fd = -1;
755 }
756
757 return ret;
758}
759
bffad86f 760static int fio_ioring_post_init(struct thread_data *td)
52885fa2 761{
bffad86f 762 struct ioring_data *ld = td->io_ops_data;
5ffd5626 763 struct ioring_options *o = td->eo;
52885fa2 764 struct io_u *io_u;
650346e1 765 int err, i;
52885fa2 766
650346e1
JA
767 for (i = 0; i < td->o.iodepth; i++) {
768 struct iovec *iov = &ld->iovecs[i];
9a2d78b3 769
650346e1
JA
770 io_u = ld->io_u_index[i];
771 iov->iov_base = io_u->buf;
772 iov->iov_len = td_max_bs(td);
52885fa2
JA
773 }
774
bffad86f 775 err = fio_ioring_queue_init(td);
52885fa2 776 if (err) {
0442b53f 777 int init_err = errno;
c4f5c92f 778
0442b53f 779 if (init_err == ENOSYS)
c4f5c92f 780 log_err("fio: your kernel doesn't support io_uring\n");
0442b53f 781 td_verror(td, init_err, "io_queue_init");
52885fa2
JA
782 return 1;
783 }
784
7c70f506
JA
785 for (i = 0; i < td->o.iodepth; i++) {
786 struct io_uring_sqe *sqe;
787
788 sqe = &ld->sqes[i];
789 memset(sqe, 0, sizeof(*sqe));
790 }
791
5ffd5626
JA
792 if (o->registerfiles) {
793 err = fio_ioring_register_files(td);
794 if (err) {
795 td_verror(td, errno, "ioring_register_files");
796 return 1;
797 }
798 }
799
52885fa2
JA
800 return 0;
801}
802
bffad86f 803static int fio_ioring_init(struct thread_data *td)
52885fa2 804{
5ffd5626 805 struct ioring_options *o = td->eo;
bffad86f 806 struct ioring_data *ld;
e9f6567a 807 int ret;
52885fa2 808
5ffd5626
JA
809 /* sqthread submission requires registered files */
810 if (o->sqpoll_thread)
811 o->registerfiles = 1;
812
813 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
814 log_err("fio: io_uring registered files require nr_files to "
815 "be identical to open_files\n");
816 return 1;
817 }
818
52885fa2
JA
819 ld = calloc(1, sizeof(*ld));
820
b87aa01a
JA
821 /* ring depth must be a power-of-2 */
822 ld->iodepth = td->o.iodepth;
823 td->o.iodepth = roundup_pow2(td->o.iodepth);
824
52885fa2
JA
825 /* io_u index */
826 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
650346e1 827 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
52885fa2
JA
828
829 td->io_ops_data = ld;
b2a432bf 830
d6cbeab4 831 ret = fio_cmdprio_init(td, &ld->cmdprio, &o->cmdprio_options);
e9f6567a
DLM
832 if (ret) {
833 td_verror(td, EINVAL, "fio_ioring_init");
b2a432bf
PC
834 return 1;
835 }
1af44196 836
52885fa2
JA
837 return 0;
838}
839
bffad86f 840static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
52885fa2 841{
bffad86f 842 struct ioring_data *ld = td->io_ops_data;
52885fa2
JA
843
844 ld->io_u_index[io_u->index] = io_u;
845 return 0;
846}
847
5ffd5626
JA
848static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
849{
850 struct ioring_data *ld = td->io_ops_data;
851 struct ioring_options *o = td->eo;
852
17318cf6 853 if (!ld || !o->registerfiles)
5ffd5626
JA
854 return generic_open_file(td, f);
855
856 f->fd = ld->fds[f->engine_pos];
857 return 0;
858}
859
860static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
861{
17318cf6 862 struct ioring_data *ld = td->io_ops_data;
5ffd5626
JA
863 struct ioring_options *o = td->eo;
864
17318cf6 865 if (!ld || !o->registerfiles)
5ffd5626
JA
866 return generic_close_file(td, f);
867
868 f->fd = -1;
869 return 0;
870}
871
52885fa2 872static struct ioengine_ops ioengine = {
bffad86f 873 .name = "io_uring",
52885fa2 874 .version = FIO_IOOPS_VERSION,
8bfe330e 875 .flags = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD,
bffad86f
JA
876 .init = fio_ioring_init,
877 .post_init = fio_ioring_post_init,
878 .io_u_init = fio_ioring_io_u_init,
879 .prep = fio_ioring_prep,
880 .queue = fio_ioring_queue,
881 .commit = fio_ioring_commit,
882 .getevents = fio_ioring_getevents,
883 .event = fio_ioring_event,
884 .cleanup = fio_ioring_cleanup,
5ffd5626
JA
885 .open_file = fio_ioring_open_file,
886 .close_file = fio_ioring_close_file,
52885fa2
JA
887 .get_file_size = generic_get_file_size,
888 .options = options,
bffad86f 889 .option_struct_size = sizeof(struct ioring_options),
52885fa2
JA
890};
891
bffad86f 892static void fio_init fio_ioring_register(void)
52885fa2 893{
52885fa2 894 register_ioengine(&ioengine);
52885fa2
JA
895}
896
bffad86f 897static void fio_exit fio_ioring_unregister(void)
52885fa2 898{
52885fa2 899 unregister_ioengine(&ioengine);
52885fa2 900}
1f90e9bb 901#endif