ioengines: don't record issue_time if ioengines already do it
[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 #include "nvme.h"
28
29 #include <sys/stat.h>
30
31 enum uring_cmd_type {
32         FIO_URING_CMD_NVME = 1,
33 };
34
35 struct io_sq_ring {
36         unsigned *head;
37         unsigned *tail;
38         unsigned *ring_mask;
39         unsigned *ring_entries;
40         unsigned *flags;
41         unsigned *array;
42 };
43
44 struct io_cq_ring {
45         unsigned *head;
46         unsigned *tail;
47         unsigned *ring_mask;
48         unsigned *ring_entries;
49         struct io_uring_cqe *cqes;
50 };
51
52 struct ioring_mmap {
53         void *ptr;
54         size_t len;
55 };
56
57 struct ioring_data {
58         int ring_fd;
59
60         struct io_u **io_u_index;
61
62         int *fds;
63
64         struct io_sq_ring sq_ring;
65         struct io_uring_sqe *sqes;
66         struct iovec *iovecs;
67         unsigned sq_ring_mask;
68
69         struct io_cq_ring cq_ring;
70         unsigned cq_ring_mask;
71
72         int queued;
73         int cq_ring_off;
74         unsigned iodepth;
75         int prepped;
76
77         struct ioring_mmap mmap[3];
78
79         struct cmdprio cmdprio;
80 };
81
82 struct ioring_options {
83         struct thread_data *td;
84         unsigned int hipri;
85         struct cmdprio_options cmdprio_options;
86         unsigned int fixedbufs;
87         unsigned int registerfiles;
88         unsigned int sqpoll_thread;
89         unsigned int sqpoll_set;
90         unsigned int sqpoll_cpu;
91         unsigned int nonvectored;
92         unsigned int uncached;
93         unsigned int nowait;
94         unsigned int force_async;
95         enum uring_cmd_type cmd_type;
96 };
97
98 static const int ddir_to_op[2][2] = {
99         { IORING_OP_READV, IORING_OP_READ },
100         { IORING_OP_WRITEV, IORING_OP_WRITE }
101 };
102
103 static const int fixed_ddir_to_op[2] = {
104         IORING_OP_READ_FIXED,
105         IORING_OP_WRITE_FIXED
106 };
107
108 static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
109 {
110         struct ioring_options *o = data;
111
112         o->sqpoll_cpu = *val;
113         o->sqpoll_set = 1;
114         return 0;
115 }
116
117 static struct fio_option options[] = {
118         {
119                 .name   = "hipri",
120                 .lname  = "High Priority",
121                 .type   = FIO_OPT_STR_SET,
122                 .off1   = offsetof(struct ioring_options, hipri),
123                 .help   = "Use polled IO completions",
124                 .category = FIO_OPT_C_ENGINE,
125                 .group  = FIO_OPT_G_IOURING,
126         },
127 #ifdef FIO_HAVE_IOPRIO_CLASS
128         {
129                 .name   = "cmdprio_percentage",
130                 .lname  = "high priority percentage",
131                 .type   = FIO_OPT_INT,
132                 .off1   = offsetof(struct ioring_options,
133                                    cmdprio_options.percentage[DDIR_READ]),
134                 .off2   = offsetof(struct ioring_options,
135                                    cmdprio_options.percentage[DDIR_WRITE]),
136                 .minval = 0,
137                 .maxval = 100,
138                 .help   = "Send high priority I/O this percentage of the time",
139                 .category = FIO_OPT_C_ENGINE,
140                 .group  = FIO_OPT_G_IOURING,
141         },
142         {
143                 .name   = "cmdprio_class",
144                 .lname  = "Asynchronous I/O priority class",
145                 .type   = FIO_OPT_INT,
146                 .off1   = offsetof(struct ioring_options,
147                                    cmdprio_options.class[DDIR_READ]),
148                 .off2   = offsetof(struct ioring_options,
149                                    cmdprio_options.class[DDIR_WRITE]),
150                 .help   = "Set asynchronous IO priority class",
151                 .minval = IOPRIO_MIN_PRIO_CLASS + 1,
152                 .maxval = IOPRIO_MAX_PRIO_CLASS,
153                 .interval = 1,
154                 .category = FIO_OPT_C_ENGINE,
155                 .group  = FIO_OPT_G_IOURING,
156         },
157         {
158                 .name   = "cmdprio",
159                 .lname  = "Asynchronous I/O priority level",
160                 .type   = FIO_OPT_INT,
161                 .off1   = offsetof(struct ioring_options,
162                                    cmdprio_options.level[DDIR_READ]),
163                 .off2   = offsetof(struct ioring_options,
164                                    cmdprio_options.level[DDIR_WRITE]),
165                 .help   = "Set asynchronous IO priority level",
166                 .minval = IOPRIO_MIN_PRIO,
167                 .maxval = IOPRIO_MAX_PRIO,
168                 .interval = 1,
169                 .category = FIO_OPT_C_ENGINE,
170                 .group  = FIO_OPT_G_IOURING,
171         },
172         {
173                 .name   = "cmdprio_bssplit",
174                 .lname  = "Priority percentage block size split",
175                 .type   = FIO_OPT_STR_STORE,
176                 .off1   = offsetof(struct ioring_options,
177                                    cmdprio_options.bssplit_str),
178                 .help   = "Set priority percentages for different block sizes",
179                 .category = FIO_OPT_C_ENGINE,
180                 .group  = FIO_OPT_G_IOURING,
181         },
182 #else
183         {
184                 .name   = "cmdprio_percentage",
185                 .lname  = "high priority percentage",
186                 .type   = FIO_OPT_UNSUPPORTED,
187                 .help   = "Your platform does not support I/O priority classes",
188         },
189         {
190                 .name   = "cmdprio_class",
191                 .lname  = "Asynchronous I/O priority class",
192                 .type   = FIO_OPT_UNSUPPORTED,
193                 .help   = "Your platform does not support I/O priority classes",
194         },
195         {
196                 .name   = "cmdprio",
197                 .lname  = "Asynchronous I/O priority level",
198                 .type   = FIO_OPT_UNSUPPORTED,
199                 .help   = "Your platform does not support I/O priority classes",
200         },
201         {
202                 .name   = "cmdprio_bssplit",
203                 .lname  = "Priority percentage block size split",
204                 .type   = FIO_OPT_UNSUPPORTED,
205                 .help   = "Your platform does not support I/O priority classes",
206         },
207 #endif
208         {
209                 .name   = "fixedbufs",
210                 .lname  = "Fixed (pre-mapped) IO buffers",
211                 .type   = FIO_OPT_STR_SET,
212                 .off1   = offsetof(struct ioring_options, fixedbufs),
213                 .help   = "Pre map IO buffers",
214                 .category = FIO_OPT_C_ENGINE,
215                 .group  = FIO_OPT_G_IOURING,
216         },
217         {
218                 .name   = "registerfiles",
219                 .lname  = "Register file set",
220                 .type   = FIO_OPT_STR_SET,
221                 .off1   = offsetof(struct ioring_options, registerfiles),
222                 .help   = "Pre-open/register files",
223                 .category = FIO_OPT_C_ENGINE,
224                 .group  = FIO_OPT_G_IOURING,
225         },
226         {
227                 .name   = "sqthread_poll",
228                 .lname  = "Kernel SQ thread polling",
229                 .type   = FIO_OPT_INT,
230                 .off1   = offsetof(struct ioring_options, sqpoll_thread),
231                 .help   = "Offload submission/completion to kernel thread",
232                 .category = FIO_OPT_C_ENGINE,
233                 .group  = FIO_OPT_G_IOURING,
234         },
235         {
236                 .name   = "sqthread_poll_cpu",
237                 .lname  = "SQ Thread Poll CPU",
238                 .type   = FIO_OPT_INT,
239                 .cb     = fio_ioring_sqpoll_cb,
240                 .help   = "What CPU to run SQ thread polling on",
241                 .category = FIO_OPT_C_ENGINE,
242                 .group  = FIO_OPT_G_IOURING,
243         },
244         {
245                 .name   = "nonvectored",
246                 .lname  = "Non-vectored",
247                 .type   = FIO_OPT_INT,
248                 .off1   = offsetof(struct ioring_options, nonvectored),
249                 .def    = "-1",
250                 .help   = "Use non-vectored read/write commands",
251                 .category = FIO_OPT_C_ENGINE,
252                 .group  = FIO_OPT_G_IOURING,
253         },
254         {
255                 .name   = "uncached",
256                 .lname  = "Uncached",
257                 .type   = FIO_OPT_INT,
258                 .off1   = offsetof(struct ioring_options, uncached),
259                 .help   = "Use RWF_UNCACHED for buffered read/writes",
260                 .category = FIO_OPT_C_ENGINE,
261                 .group  = FIO_OPT_G_IOURING,
262         },
263         {
264                 .name   = "nowait",
265                 .lname  = "RWF_NOWAIT",
266                 .type   = FIO_OPT_BOOL,
267                 .off1   = offsetof(struct ioring_options, nowait),
268                 .help   = "Use RWF_NOWAIT for reads/writes",
269                 .category = FIO_OPT_C_ENGINE,
270                 .group  = FIO_OPT_G_IOURING,
271         },
272         {
273                 .name   = "force_async",
274                 .lname  = "Force async",
275                 .type   = FIO_OPT_INT,
276                 .off1   = offsetof(struct ioring_options, force_async),
277                 .help   = "Set IOSQE_ASYNC every N requests",
278                 .category = FIO_OPT_C_ENGINE,
279                 .group  = FIO_OPT_G_IOURING,
280         },
281         {
282                 .name   = "cmd_type",
283                 .lname  = "Uring cmd type",
284                 .type   = FIO_OPT_STR,
285                 .off1   = offsetof(struct ioring_options, cmd_type),
286                 .help   = "Specify uring-cmd type",
287                 .def    = "nvme",
288                 .posval = {
289                           { .ival = "nvme",
290                             .oval = FIO_URING_CMD_NVME,
291                             .help = "Issue nvme-uring-cmd",
292                           },
293                 },
294                 .category = FIO_OPT_C_ENGINE,
295                 .group  = FIO_OPT_G_IOURING,
296         },
297         {
298                 .name   = NULL,
299         },
300 };
301
302 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
303                          unsigned int min_complete, unsigned int flags)
304 {
305 #ifdef FIO_ARCH_HAS_SYSCALL
306         return __do_syscall6(__NR_io_uring_enter, ld->ring_fd, to_submit,
307                                 min_complete, flags, NULL, 0);
308 #else
309         return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit,
310                         min_complete, flags, NULL, 0);
311 #endif
312 }
313
314 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
315 {
316         struct ioring_data *ld = td->io_ops_data;
317         struct ioring_options *o = td->eo;
318         struct fio_file *f = io_u->file;
319         struct io_uring_sqe *sqe;
320
321         sqe = &ld->sqes[io_u->index];
322
323         if (o->registerfiles) {
324                 sqe->fd = f->engine_pos;
325                 sqe->flags = IOSQE_FIXED_FILE;
326         } else {
327                 sqe->fd = f->fd;
328                 sqe->flags = 0;
329         }
330
331         if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
332                 if (o->fixedbufs) {
333                         sqe->opcode = fixed_ddir_to_op[io_u->ddir];
334                         sqe->addr = (unsigned long) io_u->xfer_buf;
335                         sqe->len = io_u->xfer_buflen;
336                         sqe->buf_index = io_u->index;
337                 } else {
338                         struct iovec *iov = &ld->iovecs[io_u->index];
339
340                         /*
341                          * Update based on actual io_u, requeue could have
342                          * adjusted these
343                          */
344                         iov->iov_base = io_u->xfer_buf;
345                         iov->iov_len = io_u->xfer_buflen;
346
347                         sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
348                         if (o->nonvectored) {
349                                 sqe->addr = (unsigned long) iov->iov_base;
350                                 sqe->len = iov->iov_len;
351                         } else {
352                                 sqe->addr = (unsigned long) iov;
353                                 sqe->len = 1;
354                         }
355                 }
356                 sqe->rw_flags = 0;
357                 if (!td->o.odirect && o->uncached)
358                         sqe->rw_flags |= RWF_UNCACHED;
359                 if (o->nowait)
360                         sqe->rw_flags |= RWF_NOWAIT;
361
362                 /*
363                  * Since io_uring can have a submission context (sqthread_poll)
364                  * that is different from the process context, we cannot rely on
365                  * the IO priority set by ioprio_set() (option prio/prioclass)
366                  * to be inherited.
367                  * td->ioprio will have the value of the "default prio", so set
368                  * this unconditionally. This value might get overridden by
369                  * fio_ioring_cmdprio_prep() if the option cmdprio_percentage or
370                  * cmdprio_bssplit is used.
371                  */
372                 sqe->ioprio = td->ioprio;
373                 sqe->off = io_u->offset;
374         } else if (ddir_sync(io_u->ddir)) {
375                 sqe->ioprio = 0;
376                 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
377                         sqe->off = f->first_write;
378                         sqe->len = f->last_write - f->first_write;
379                         sqe->sync_range_flags = td->o.sync_file_range;
380                         sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
381                 } else {
382                         sqe->off = 0;
383                         sqe->addr = 0;
384                         sqe->len = 0;
385                         if (io_u->ddir == DDIR_DATASYNC)
386                                 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
387                         sqe->opcode = IORING_OP_FSYNC;
388                 }
389         }
390
391         if (o->force_async && ++ld->prepped == o->force_async) {
392                 ld->prepped = 0;
393                 sqe->flags |= IOSQE_ASYNC;
394         }
395
396         sqe->user_data = (unsigned long) io_u;
397         return 0;
398 }
399
400 static int fio_ioring_cmd_prep(struct thread_data *td, struct io_u *io_u)
401 {
402         struct ioring_data *ld = td->io_ops_data;
403         struct ioring_options *o = td->eo;
404         struct fio_file *f = io_u->file;
405         struct nvme_uring_cmd *cmd;
406         struct io_uring_sqe *sqe;
407
408         /* only supports nvme_uring_cmd */
409         if (o->cmd_type != FIO_URING_CMD_NVME)
410                 return -EINVAL;
411
412         sqe = &ld->sqes[(io_u->index) << 1];
413
414         if (o->registerfiles) {
415                 sqe->fd = f->engine_pos;
416                 sqe->flags = IOSQE_FIXED_FILE;
417         } else {
418                 sqe->fd = f->fd;
419         }
420         sqe->rw_flags = 0;
421         if (!td->o.odirect && o->uncached)
422                 sqe->rw_flags |= RWF_UNCACHED;
423         if (o->nowait)
424                 sqe->rw_flags |= RWF_NOWAIT;
425
426         sqe->opcode = IORING_OP_URING_CMD;
427         sqe->user_data = (unsigned long) io_u;
428         if (o->nonvectored)
429                 sqe->cmd_op = NVME_URING_CMD_IO;
430         else
431                 sqe->cmd_op = NVME_URING_CMD_IO_VEC;
432         if (o->force_async && ++ld->prepped == o->force_async) {
433                 ld->prepped = 0;
434                 sqe->flags |= IOSQE_ASYNC;
435         }
436
437         cmd = (struct nvme_uring_cmd *)sqe->cmd;
438         return fio_nvme_uring_cmd_prep(cmd, io_u,
439                         o->nonvectored ? NULL : &ld->iovecs[io_u->index]);
440 }
441
442 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
443 {
444         struct ioring_data *ld = td->io_ops_data;
445         struct io_uring_cqe *cqe;
446         struct io_u *io_u;
447         unsigned index;
448
449         index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
450
451         cqe = &ld->cq_ring.cqes[index];
452         io_u = (struct io_u *) (uintptr_t) cqe->user_data;
453
454         if (cqe->res != io_u->xfer_buflen) {
455                 if (cqe->res > io_u->xfer_buflen)
456                         io_u->error = -cqe->res;
457                 else
458                         io_u->resid = io_u->xfer_buflen - cqe->res;
459         } else
460                 io_u->error = 0;
461
462         return io_u;
463 }
464
465 static struct io_u *fio_ioring_cmd_event(struct thread_data *td, int event)
466 {
467         struct ioring_data *ld = td->io_ops_data;
468         struct ioring_options *o = td->eo;
469         struct io_uring_cqe *cqe;
470         struct io_u *io_u;
471         unsigned index;
472
473         index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
474         if (o->cmd_type == FIO_URING_CMD_NVME)
475                 index <<= 1;
476
477         cqe = &ld->cq_ring.cqes[index];
478         io_u = (struct io_u *) (uintptr_t) cqe->user_data;
479
480         if (cqe->res != 0)
481                 io_u->error = -cqe->res;
482         else
483                 io_u->error = 0;
484
485         return io_u;
486 }
487
488 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
489                                    unsigned int max)
490 {
491         struct ioring_data *ld = td->io_ops_data;
492         struct io_cq_ring *ring = &ld->cq_ring;
493         unsigned head, reaped = 0;
494
495         head = *ring->head;
496         do {
497                 if (head == atomic_load_acquire(ring->tail))
498                         break;
499                 reaped++;
500                 head++;
501         } while (reaped + events < max);
502
503         if (reaped)
504                 atomic_store_release(ring->head, head);
505
506         return reaped;
507 }
508
509 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
510                                 unsigned int max, const struct timespec *t)
511 {
512         struct ioring_data *ld = td->io_ops_data;
513         unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
514         struct ioring_options *o = td->eo;
515         struct io_cq_ring *ring = &ld->cq_ring;
516         unsigned events = 0;
517         int r;
518
519         ld->cq_ring_off = *ring->head;
520         do {
521                 r = fio_ioring_cqring_reap(td, events, max);
522                 if (r) {
523                         events += r;
524                         if (actual_min != 0)
525                                 actual_min -= r;
526                         continue;
527                 }
528
529                 if (!o->sqpoll_thread) {
530                         r = io_uring_enter(ld, 0, actual_min,
531                                                 IORING_ENTER_GETEVENTS);
532                         if (r < 0) {
533                                 if (errno == EAGAIN || errno == EINTR)
534                                         continue;
535                                 td_verror(td, errno, "io_uring_enter");
536                                 break;
537                         }
538                 }
539         } while (events < min);
540
541         return r < 0 ? r : events;
542 }
543
544 static inline void fio_ioring_cmdprio_prep(struct thread_data *td,
545                                            struct io_u *io_u)
546 {
547         struct ioring_data *ld = td->io_ops_data;
548         struct cmdprio *cmdprio = &ld->cmdprio;
549
550         if (fio_cmdprio_set_ioprio(td, cmdprio, io_u))
551                 ld->sqes[io_u->index].ioprio = io_u->ioprio;
552 }
553
554 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
555                                           struct io_u *io_u)
556 {
557         struct ioring_data *ld = td->io_ops_data;
558         struct io_sq_ring *ring = &ld->sq_ring;
559         unsigned tail, next_tail;
560
561         fio_ro_check(td, io_u);
562
563         if (ld->queued == ld->iodepth)
564                 return FIO_Q_BUSY;
565
566         if (io_u->ddir == DDIR_TRIM) {
567                 if (ld->queued)
568                         return FIO_Q_BUSY;
569
570                 do_io_u_trim(td, io_u);
571                 io_u_mark_submit(td, 1);
572                 io_u_mark_complete(td, 1);
573                 return FIO_Q_COMPLETED;
574         }
575
576         tail = *ring->tail;
577         next_tail = tail + 1;
578         if (next_tail == atomic_load_acquire(ring->head))
579                 return FIO_Q_BUSY;
580
581         if (ld->cmdprio.mode != CMDPRIO_MODE_NONE)
582                 fio_ioring_cmdprio_prep(td, io_u);
583
584         ring->array[tail & ld->sq_ring_mask] = io_u->index;
585         atomic_store_release(ring->tail, next_tail);
586
587         ld->queued++;
588         return FIO_Q_QUEUED;
589 }
590
591 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
592 {
593         struct ioring_data *ld = td->io_ops_data;
594         struct timespec now;
595
596         if (!fio_fill_issue_time(td))
597                 return;
598
599         fio_gettime(&now, NULL);
600
601         while (nr--) {
602                 struct io_sq_ring *ring = &ld->sq_ring;
603                 int index = ring->array[start & ld->sq_ring_mask];
604                 struct io_u *io_u = ld->io_u_index[index];
605
606                 memcpy(&io_u->issue_time, &now, sizeof(now));
607                 io_u_queued(td, io_u);
608
609                 start++;
610         }
611 }
612
613 static int fio_ioring_commit(struct thread_data *td)
614 {
615         struct ioring_data *ld = td->io_ops_data;
616         struct ioring_options *o = td->eo;
617         int ret;
618
619         if (!ld->queued)
620                 return 0;
621
622         /*
623          * Kernel side does submission. just need to check if the ring is
624          * flagged as needing a kick, if so, call io_uring_enter(). This
625          * only happens if we've been idle too long.
626          */
627         if (o->sqpoll_thread) {
628                 struct io_sq_ring *ring = &ld->sq_ring;
629                 unsigned flags;
630
631                 flags = atomic_load_acquire(ring->flags);
632                 if (flags & IORING_SQ_NEED_WAKEUP)
633                         io_uring_enter(ld, ld->queued, 0,
634                                         IORING_ENTER_SQ_WAKEUP);
635                 ld->queued = 0;
636                 return 0;
637         }
638
639         do {
640                 unsigned start = *ld->sq_ring.head;
641                 long nr = ld->queued;
642
643                 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
644                 if (ret > 0) {
645                         fio_ioring_queued(td, start, ret);
646                         io_u_mark_submit(td, ret);
647
648                         ld->queued -= ret;
649                         ret = 0;
650                 } else if (!ret) {
651                         io_u_mark_submit(td, ret);
652                         continue;
653                 } else {
654                         if (errno == EAGAIN || errno == EINTR) {
655                                 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
656                                 if (ret)
657                                         continue;
658                                 /* Shouldn't happen */
659                                 usleep(1);
660                                 continue;
661                         }
662                         td_verror(td, errno, "io_uring_enter submit");
663                         break;
664                 }
665         } while (ld->queued);
666
667         return ret;
668 }
669
670 static void fio_ioring_unmap(struct ioring_data *ld)
671 {
672         int i;
673
674         for (i = 0; i < FIO_ARRAY_SIZE(ld->mmap); i++)
675                 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
676         close(ld->ring_fd);
677 }
678
679 static void fio_ioring_cleanup(struct thread_data *td)
680 {
681         struct ioring_data *ld = td->io_ops_data;
682
683         if (ld) {
684                 if (!(td->flags & TD_F_CHILD))
685                         fio_ioring_unmap(ld);
686
687                 fio_cmdprio_cleanup(&ld->cmdprio);
688                 free(ld->io_u_index);
689                 free(ld->iovecs);
690                 free(ld->fds);
691                 free(ld);
692         }
693 }
694
695 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
696 {
697         struct io_sq_ring *sring = &ld->sq_ring;
698         struct io_cq_ring *cring = &ld->cq_ring;
699         void *ptr;
700
701         ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
702         ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
703                         MAP_SHARED | MAP_POPULATE, ld->ring_fd,
704                         IORING_OFF_SQ_RING);
705         ld->mmap[0].ptr = ptr;
706         sring->head = ptr + p->sq_off.head;
707         sring->tail = ptr + p->sq_off.tail;
708         sring->ring_mask = ptr + p->sq_off.ring_mask;
709         sring->ring_entries = ptr + p->sq_off.ring_entries;
710         sring->flags = ptr + p->sq_off.flags;
711         sring->array = ptr + p->sq_off.array;
712         ld->sq_ring_mask = *sring->ring_mask;
713
714         if (p->flags & IORING_SETUP_SQE128)
715                 ld->mmap[1].len = 2 * p->sq_entries * sizeof(struct io_uring_sqe);
716         else
717                 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
718         ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
719                                 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
720                                 IORING_OFF_SQES);
721         ld->mmap[1].ptr = ld->sqes;
722
723         if (p->flags & IORING_SETUP_CQE32) {
724                 ld->mmap[2].len = p->cq_off.cqes +
725                                         2 * p->cq_entries * sizeof(struct io_uring_cqe);
726         } else {
727                 ld->mmap[2].len = p->cq_off.cqes +
728                                         p->cq_entries * sizeof(struct io_uring_cqe);
729         }
730         ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
731                         MAP_SHARED | MAP_POPULATE, ld->ring_fd,
732                         IORING_OFF_CQ_RING);
733         ld->mmap[2].ptr = ptr;
734         cring->head = ptr + p->cq_off.head;
735         cring->tail = ptr + p->cq_off.tail;
736         cring->ring_mask = ptr + p->cq_off.ring_mask;
737         cring->ring_entries = ptr + p->cq_off.ring_entries;
738         cring->cqes = ptr + p->cq_off.cqes;
739         ld->cq_ring_mask = *cring->ring_mask;
740         return 0;
741 }
742
743 static void fio_ioring_probe(struct thread_data *td)
744 {
745         struct ioring_data *ld = td->io_ops_data;
746         struct ioring_options *o = td->eo;
747         struct io_uring_probe *p;
748         int ret;
749
750         /* already set by user, don't touch */
751         if (o->nonvectored != -1)
752                 return;
753
754         /* default to off, as that's always safe */
755         o->nonvectored = 0;
756
757         p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
758         if (!p)
759                 return;
760
761         memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
762         ret = syscall(__NR_io_uring_register, ld->ring_fd,
763                         IORING_REGISTER_PROBE, p, 256);
764         if (ret < 0)
765                 goto out;
766
767         if (IORING_OP_WRITE > p->ops_len)
768                 goto out;
769
770         if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED) &&
771             (p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED))
772                 o->nonvectored = 1;
773 out:
774         free(p);
775 }
776
777 static int fio_ioring_queue_init(struct thread_data *td)
778 {
779         struct ioring_data *ld = td->io_ops_data;
780         struct ioring_options *o = td->eo;
781         int depth = td->o.iodepth;
782         struct io_uring_params p;
783         int ret;
784
785         memset(&p, 0, sizeof(p));
786
787         if (o->hipri)
788                 p.flags |= IORING_SETUP_IOPOLL;
789         if (o->sqpoll_thread) {
790                 p.flags |= IORING_SETUP_SQPOLL;
791                 if (o->sqpoll_set) {
792                         p.flags |= IORING_SETUP_SQ_AFF;
793                         p.sq_thread_cpu = o->sqpoll_cpu;
794                 }
795         }
796
797         /*
798          * Clamp CQ ring size at our SQ ring size, we don't need more entries
799          * than that.
800          */
801         p.flags |= IORING_SETUP_CQSIZE;
802         p.cq_entries = depth;
803
804 retry:
805         ret = syscall(__NR_io_uring_setup, depth, &p);
806         if (ret < 0) {
807                 if (errno == EINVAL && p.flags & IORING_SETUP_CQSIZE) {
808                         p.flags &= ~IORING_SETUP_CQSIZE;
809                         goto retry;
810                 }
811                 return ret;
812         }
813
814         ld->ring_fd = ret;
815
816         fio_ioring_probe(td);
817
818         if (o->fixedbufs) {
819                 ret = syscall(__NR_io_uring_register, ld->ring_fd,
820                                 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
821                 if (ret < 0)
822                         return ret;
823         }
824
825         return fio_ioring_mmap(ld, &p);
826 }
827
828 static int fio_ioring_cmd_queue_init(struct thread_data *td)
829 {
830         struct ioring_data *ld = td->io_ops_data;
831         struct ioring_options *o = td->eo;
832         int depth = td->o.iodepth;
833         struct io_uring_params p;
834         int ret;
835
836         memset(&p, 0, sizeof(p));
837
838         if (o->hipri)
839                 p.flags |= IORING_SETUP_IOPOLL;
840         if (o->sqpoll_thread) {
841                 p.flags |= IORING_SETUP_SQPOLL;
842                 if (o->sqpoll_set) {
843                         p.flags |= IORING_SETUP_SQ_AFF;
844                         p.sq_thread_cpu = o->sqpoll_cpu;
845                 }
846         }
847         if (o->cmd_type == FIO_URING_CMD_NVME) {
848                 p.flags |= IORING_SETUP_SQE128;
849                 p.flags |= IORING_SETUP_CQE32;
850         }
851
852         /*
853          * Clamp CQ ring size at our SQ ring size, we don't need more entries
854          * than that.
855          */
856         p.flags |= IORING_SETUP_CQSIZE;
857         p.cq_entries = depth;
858
859 retry:
860         ret = syscall(__NR_io_uring_setup, depth, &p);
861         if (ret < 0) {
862                 if (errno == EINVAL && p.flags & IORING_SETUP_CQSIZE) {
863                         p.flags &= ~IORING_SETUP_CQSIZE;
864                         goto retry;
865                 }
866                 return ret;
867         }
868
869         ld->ring_fd = ret;
870
871         fio_ioring_probe(td);
872
873         if (o->fixedbufs) {
874                 ret = syscall(__NR_io_uring_register, ld->ring_fd,
875                                 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
876                 if (ret < 0)
877                         return ret;
878         }
879
880         return fio_ioring_mmap(ld, &p);
881 }
882
883 static int fio_ioring_register_files(struct thread_data *td)
884 {
885         struct ioring_data *ld = td->io_ops_data;
886         struct fio_file *f;
887         unsigned int i;
888         int ret;
889
890         ld->fds = calloc(td->o.nr_files, sizeof(int));
891
892         for_each_file(td, f, i) {
893                 ret = generic_open_file(td, f);
894                 if (ret)
895                         goto err;
896                 ld->fds[i] = f->fd;
897                 f->engine_pos = i;
898         }
899
900         ret = syscall(__NR_io_uring_register, ld->ring_fd,
901                         IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
902         if (ret) {
903 err:
904                 free(ld->fds);
905                 ld->fds = NULL;
906         }
907
908         /*
909          * Pretend the file is closed again, and really close it if we hit
910          * an error.
911          */
912         for_each_file(td, f, i) {
913                 if (ret) {
914                         int fio_unused ret2;
915                         ret2 = generic_close_file(td, f);
916                 } else
917                         f->fd = -1;
918         }
919
920         return ret;
921 }
922
923 static int fio_ioring_post_init(struct thread_data *td)
924 {
925         struct ioring_data *ld = td->io_ops_data;
926         struct ioring_options *o = td->eo;
927         struct io_u *io_u;
928         int err, i;
929
930         for (i = 0; i < td->o.iodepth; i++) {
931                 struct iovec *iov = &ld->iovecs[i];
932
933                 io_u = ld->io_u_index[i];
934                 iov->iov_base = io_u->buf;
935                 iov->iov_len = td_max_bs(td);
936         }
937
938         err = fio_ioring_queue_init(td);
939         if (err) {
940                 int init_err = errno;
941
942                 if (init_err == ENOSYS)
943                         log_err("fio: your kernel doesn't support io_uring\n");
944                 td_verror(td, init_err, "io_queue_init");
945                 return 1;
946         }
947
948         for (i = 0; i < td->o.iodepth; i++) {
949                 struct io_uring_sqe *sqe;
950
951                 sqe = &ld->sqes[i];
952                 memset(sqe, 0, sizeof(*sqe));
953         }
954
955         if (o->registerfiles) {
956                 err = fio_ioring_register_files(td);
957                 if (err) {
958                         td_verror(td, errno, "ioring_register_files");
959                         return 1;
960                 }
961         }
962
963         return 0;
964 }
965
966 static int fio_ioring_cmd_post_init(struct thread_data *td)
967 {
968         struct ioring_data *ld = td->io_ops_data;
969         struct ioring_options *o = td->eo;
970         struct io_u *io_u;
971         int err, i;
972
973         for (i = 0; i < td->o.iodepth; i++) {
974                 struct iovec *iov = &ld->iovecs[i];
975
976                 io_u = ld->io_u_index[i];
977                 iov->iov_base = io_u->buf;
978                 iov->iov_len = td_max_bs(td);
979         }
980
981         err = fio_ioring_cmd_queue_init(td);
982         if (err) {
983                 int init_err = errno;
984
985                 td_verror(td, init_err, "io_queue_init");
986                 return 1;
987         }
988
989         for (i = 0; i < td->o.iodepth; i++) {
990                 struct io_uring_sqe *sqe;
991
992                 if (o->cmd_type == FIO_URING_CMD_NVME) {
993                         sqe = &ld->sqes[i << 1];
994                         memset(sqe, 0, 2 * sizeof(*sqe));
995                 } else {
996                         sqe = &ld->sqes[i];
997                         memset(sqe, 0, sizeof(*sqe));
998                 }
999         }
1000
1001         if (o->registerfiles) {
1002                 err = fio_ioring_register_files(td);
1003                 if (err) {
1004                         td_verror(td, errno, "ioring_register_files");
1005                         return 1;
1006                 }
1007         }
1008
1009         return 0;
1010 }
1011
1012 static int fio_ioring_init(struct thread_data *td)
1013 {
1014         struct ioring_options *o = td->eo;
1015         struct ioring_data *ld;
1016         int ret;
1017
1018         /* sqthread submission requires registered files */
1019         if (o->sqpoll_thread)
1020                 o->registerfiles = 1;
1021
1022         if (o->registerfiles && td->o.nr_files != td->o.open_files) {
1023                 log_err("fio: io_uring registered files require nr_files to "
1024                         "be identical to open_files\n");
1025                 return 1;
1026         }
1027
1028         ld = calloc(1, sizeof(*ld));
1029
1030         /* ring depth must be a power-of-2 */
1031         ld->iodepth = td->o.iodepth;
1032         td->o.iodepth = roundup_pow2(td->o.iodepth);
1033
1034         /* io_u index */
1035         ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
1036         ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
1037
1038         td->io_ops_data = ld;
1039
1040         ret = fio_cmdprio_init(td, &ld->cmdprio, &o->cmdprio_options);
1041         if (ret) {
1042                 td_verror(td, EINVAL, "fio_ioring_init");
1043                 return 1;
1044         }
1045
1046         return 0;
1047 }
1048
1049 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
1050 {
1051         struct ioring_data *ld = td->io_ops_data;
1052
1053         ld->io_u_index[io_u->index] = io_u;
1054         return 0;
1055 }
1056
1057 static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
1058 {
1059         struct ioring_data *ld = td->io_ops_data;
1060         struct ioring_options *o = td->eo;
1061
1062         if (!ld || !o->registerfiles)
1063                 return generic_open_file(td, f);
1064
1065         f->fd = ld->fds[f->engine_pos];
1066         return 0;
1067 }
1068
1069 static int fio_ioring_cmd_open_file(struct thread_data *td, struct fio_file *f)
1070 {
1071         struct ioring_data *ld = td->io_ops_data;
1072         struct ioring_options *o = td->eo;
1073
1074         if (o->cmd_type == FIO_URING_CMD_NVME) {
1075                 struct nvme_data *data = NULL;
1076                 unsigned int nsid, lba_size = 0;
1077                 unsigned long long nlba = 0;
1078                 int ret;
1079
1080                 /* Store the namespace-id and lba size. */
1081                 data = FILE_ENG_DATA(f);
1082                 if (data == NULL) {
1083                         ret = fio_nvme_get_info(f, &nsid, &lba_size, &nlba);
1084                         if (ret)
1085                                 return ret;
1086
1087                         data = calloc(1, sizeof(struct nvme_data));
1088                         data->nsid = nsid;
1089                         data->lba_shift = ilog2(lba_size);
1090
1091                         FILE_SET_ENG_DATA(f, data);
1092                 }
1093         }
1094         if (!ld || !o->registerfiles)
1095                 return generic_open_file(td, f);
1096
1097         f->fd = ld->fds[f->engine_pos];
1098         return 0;
1099 }
1100
1101 static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
1102 {
1103         struct ioring_data *ld = td->io_ops_data;
1104         struct ioring_options *o = td->eo;
1105
1106         if (!ld || !o->registerfiles)
1107                 return generic_close_file(td, f);
1108
1109         f->fd = -1;
1110         return 0;
1111 }
1112
1113 static int fio_ioring_cmd_close_file(struct thread_data *td,
1114                                      struct fio_file *f)
1115 {
1116         struct ioring_data *ld = td->io_ops_data;
1117         struct ioring_options *o = td->eo;
1118
1119         if (o->cmd_type == FIO_URING_CMD_NVME) {
1120                 struct nvme_data *data = FILE_ENG_DATA(f);
1121
1122                 FILE_SET_ENG_DATA(f, NULL);
1123                 free(data);
1124         }
1125         if (!ld || !o->registerfiles)
1126                 return generic_close_file(td, f);
1127
1128         f->fd = -1;
1129         return 0;
1130 }
1131
1132 static int fio_ioring_cmd_get_file_size(struct thread_data *td,
1133                                         struct fio_file *f)
1134 {
1135         struct ioring_options *o = td->eo;
1136
1137         if (fio_file_size_known(f))
1138                 return 0;
1139
1140         if (o->cmd_type == FIO_URING_CMD_NVME) {
1141                 struct nvme_data *data = NULL;
1142                 unsigned int nsid, lba_size = 0;
1143                 unsigned long long nlba = 0;
1144                 int ret;
1145
1146                 ret = fio_nvme_get_info(f, &nsid, &lba_size, &nlba);
1147                 if (ret)
1148                         return ret;
1149
1150                 data = calloc(1, sizeof(struct nvme_data));
1151                 data->nsid = nsid;
1152                 data->lba_shift = ilog2(lba_size);
1153
1154                 f->real_file_size = lba_size * nlba;
1155                 fio_file_set_size_known(f);
1156
1157                 FILE_SET_ENG_DATA(f, data);
1158                 return 0;
1159         }
1160         return generic_get_file_size(td, f);
1161 }
1162
1163 static int fio_ioring_cmd_get_zoned_model(struct thread_data *td,
1164                                           struct fio_file *f,
1165                                           enum zbd_zoned_model *model)
1166 {
1167         return fio_nvme_get_zoned_model(td, f, model);
1168 }
1169
1170 static int fio_ioring_cmd_report_zones(struct thread_data *td,
1171                                        struct fio_file *f, uint64_t offset,
1172                                        struct zbd_zone *zbdz,
1173                                        unsigned int nr_zones)
1174 {
1175         return fio_nvme_report_zones(td, f, offset, zbdz, nr_zones);
1176 }
1177
1178 static int fio_ioring_cmd_reset_wp(struct thread_data *td, struct fio_file *f,
1179                                    uint64_t offset, uint64_t length)
1180 {
1181         return fio_nvme_reset_wp(td, f, offset, length);
1182 }
1183
1184 static int fio_ioring_cmd_get_max_open_zones(struct thread_data *td,
1185                                              struct fio_file *f,
1186                                              unsigned int *max_open_zones)
1187 {
1188         return fio_nvme_get_max_open_zones(td, f, max_open_zones);
1189 }
1190
1191 static struct ioengine_ops ioengine_uring = {
1192         .name                   = "io_uring",
1193         .version                = FIO_IOOPS_VERSION,
1194         .flags                  = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD |
1195                                         FIO_ASYNCIO_SETS_ISSUE_TIME,
1196         .init                   = fio_ioring_init,
1197         .post_init              = fio_ioring_post_init,
1198         .io_u_init              = fio_ioring_io_u_init,
1199         .prep                   = fio_ioring_prep,
1200         .queue                  = fio_ioring_queue,
1201         .commit                 = fio_ioring_commit,
1202         .getevents              = fio_ioring_getevents,
1203         .event                  = fio_ioring_event,
1204         .cleanup                = fio_ioring_cleanup,
1205         .open_file              = fio_ioring_open_file,
1206         .close_file             = fio_ioring_close_file,
1207         .get_file_size          = generic_get_file_size,
1208         .options                = options,
1209         .option_struct_size     = sizeof(struct ioring_options),
1210 };
1211
1212 static struct ioengine_ops ioengine_uring_cmd = {
1213         .name                   = "io_uring_cmd",
1214         .version                = FIO_IOOPS_VERSION,
1215         .flags                  = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD |
1216                                         FIO_MEMALIGN | FIO_RAWIO |
1217                                         FIO_ASYNCIO_SETS_ISSUE_TIME,
1218         .init                   = fio_ioring_init,
1219         .post_init              = fio_ioring_cmd_post_init,
1220         .io_u_init              = fio_ioring_io_u_init,
1221         .prep                   = fio_ioring_cmd_prep,
1222         .queue                  = fio_ioring_queue,
1223         .commit                 = fio_ioring_commit,
1224         .getevents              = fio_ioring_getevents,
1225         .event                  = fio_ioring_cmd_event,
1226         .cleanup                = fio_ioring_cleanup,
1227         .open_file              = fio_ioring_cmd_open_file,
1228         .close_file             = fio_ioring_cmd_close_file,
1229         .get_file_size          = fio_ioring_cmd_get_file_size,
1230         .get_zoned_model        = fio_ioring_cmd_get_zoned_model,
1231         .report_zones           = fio_ioring_cmd_report_zones,
1232         .reset_wp               = fio_ioring_cmd_reset_wp,
1233         .get_max_open_zones     = fio_ioring_cmd_get_max_open_zones,
1234         .options                = options,
1235         .option_struct_size     = sizeof(struct ioring_options),
1236 };
1237
1238 static void fio_init fio_ioring_register(void)
1239 {
1240         register_ioengine(&ioengine_uring);
1241         register_ioengine(&ioengine_uring_cmd);
1242 }
1243
1244 static void fio_exit fio_ioring_unregister(void)
1245 {
1246         unregister_ioengine(&ioengine_uring);
1247         unregister_ioengine(&ioengine_uring_cmd);
1248 }
1249 #endif