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