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