test: fix t/run-fio-tests.py style issues identified by pylint
[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         if (o->fixedbufs) {
437                 sqe->uring_cmd_flags = IORING_URING_CMD_FIXED;
438                 sqe->buf_index = io_u->index;
439         }
440
441         cmd = (struct nvme_uring_cmd *)sqe->cmd;
442         return fio_nvme_uring_cmd_prep(cmd, io_u,
443                         o->nonvectored ? NULL : &ld->iovecs[io_u->index]);
444 }
445
446 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
447 {
448         struct ioring_data *ld = td->io_ops_data;
449         struct io_uring_cqe *cqe;
450         struct io_u *io_u;
451         unsigned index;
452
453         index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
454
455         cqe = &ld->cq_ring.cqes[index];
456         io_u = (struct io_u *) (uintptr_t) cqe->user_data;
457
458         if (cqe->res != io_u->xfer_buflen) {
459                 if (cqe->res > io_u->xfer_buflen)
460                         io_u->error = -cqe->res;
461                 else
462                         io_u->resid = io_u->xfer_buflen - cqe->res;
463         } else
464                 io_u->error = 0;
465
466         return io_u;
467 }
468
469 static struct io_u *fio_ioring_cmd_event(struct thread_data *td, int event)
470 {
471         struct ioring_data *ld = td->io_ops_data;
472         struct ioring_options *o = td->eo;
473         struct io_uring_cqe *cqe;
474         struct io_u *io_u;
475         unsigned index;
476
477         index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
478         if (o->cmd_type == FIO_URING_CMD_NVME)
479                 index <<= 1;
480
481         cqe = &ld->cq_ring.cqes[index];
482         io_u = (struct io_u *) (uintptr_t) cqe->user_data;
483
484         if (cqe->res != 0)
485                 io_u->error = -cqe->res;
486         else
487                 io_u->error = 0;
488
489         return io_u;
490 }
491
492 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
493                                    unsigned int max)
494 {
495         struct ioring_data *ld = td->io_ops_data;
496         struct io_cq_ring *ring = &ld->cq_ring;
497         unsigned head, reaped = 0;
498
499         head = *ring->head;
500         do {
501                 if (head == atomic_load_acquire(ring->tail))
502                         break;
503                 reaped++;
504                 head++;
505         } while (reaped + events < max);
506
507         if (reaped)
508                 atomic_store_release(ring->head, head);
509
510         return reaped;
511 }
512
513 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
514                                 unsigned int max, const struct timespec *t)
515 {
516         struct ioring_data *ld = td->io_ops_data;
517         unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
518         struct ioring_options *o = td->eo;
519         struct io_cq_ring *ring = &ld->cq_ring;
520         unsigned events = 0;
521         int r;
522
523         ld->cq_ring_off = *ring->head;
524         do {
525                 r = fio_ioring_cqring_reap(td, events, max);
526                 if (r) {
527                         events += r;
528                         if (actual_min != 0)
529                                 actual_min -= r;
530                         continue;
531                 }
532
533                 if (!o->sqpoll_thread) {
534                         r = io_uring_enter(ld, 0, actual_min,
535                                                 IORING_ENTER_GETEVENTS);
536                         if (r < 0) {
537                                 if (errno == EAGAIN || errno == EINTR)
538                                         continue;
539                                 r = -errno;
540                                 td_verror(td, errno, "io_uring_enter");
541                                 break;
542                         }
543                 }
544         } while (events < min);
545
546         return r < 0 ? r : events;
547 }
548
549 static inline void fio_ioring_cmdprio_prep(struct thread_data *td,
550                                            struct io_u *io_u)
551 {
552         struct ioring_data *ld = td->io_ops_data;
553         struct cmdprio *cmdprio = &ld->cmdprio;
554
555         if (fio_cmdprio_set_ioprio(td, cmdprio, io_u))
556                 ld->sqes[io_u->index].ioprio = io_u->ioprio;
557 }
558
559 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
560                                           struct io_u *io_u)
561 {
562         struct ioring_data *ld = td->io_ops_data;
563         struct io_sq_ring *ring = &ld->sq_ring;
564         unsigned tail, next_tail;
565
566         fio_ro_check(td, io_u);
567
568         if (ld->queued == ld->iodepth)
569                 return FIO_Q_BUSY;
570
571         if (io_u->ddir == DDIR_TRIM) {
572                 if (ld->queued)
573                         return FIO_Q_BUSY;
574
575                 do_io_u_trim(td, io_u);
576                 io_u_mark_submit(td, 1);
577                 io_u_mark_complete(td, 1);
578                 return FIO_Q_COMPLETED;
579         }
580
581         tail = *ring->tail;
582         next_tail = tail + 1;
583         if (next_tail == atomic_load_acquire(ring->head))
584                 return FIO_Q_BUSY;
585
586         if (ld->cmdprio.mode != CMDPRIO_MODE_NONE)
587                 fio_ioring_cmdprio_prep(td, io_u);
588
589         ring->array[tail & ld->sq_ring_mask] = io_u->index;
590         atomic_store_release(ring->tail, next_tail);
591
592         ld->queued++;
593         return FIO_Q_QUEUED;
594 }
595
596 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
597 {
598         struct ioring_data *ld = td->io_ops_data;
599         struct timespec now;
600
601         if (!fio_fill_issue_time(td))
602                 return;
603
604         fio_gettime(&now, NULL);
605
606         while (nr--) {
607                 struct io_sq_ring *ring = &ld->sq_ring;
608                 int index = ring->array[start & ld->sq_ring_mask];
609                 struct io_u *io_u = ld->io_u_index[index];
610
611                 memcpy(&io_u->issue_time, &now, sizeof(now));
612                 io_u_queued(td, io_u);
613
614                 start++;
615         }
616
617         /*
618          * only used for iolog
619          */
620         if (td->o.read_iolog_file)
621                 memcpy(&td->last_issue, &now, sizeof(now));
622 }
623
624 static int fio_ioring_commit(struct thread_data *td)
625 {
626         struct ioring_data *ld = td->io_ops_data;
627         struct ioring_options *o = td->eo;
628         int ret;
629
630         if (!ld->queued)
631                 return 0;
632
633         /*
634          * Kernel side does submission. just need to check if the ring is
635          * flagged as needing a kick, if so, call io_uring_enter(). This
636          * only happens if we've been idle too long.
637          */
638         if (o->sqpoll_thread) {
639                 struct io_sq_ring *ring = &ld->sq_ring;
640                 unsigned flags;
641
642                 flags = atomic_load_acquire(ring->flags);
643                 if (flags & IORING_SQ_NEED_WAKEUP)
644                         io_uring_enter(ld, ld->queued, 0,
645                                         IORING_ENTER_SQ_WAKEUP);
646                 ld->queued = 0;
647                 return 0;
648         }
649
650         do {
651                 unsigned start = *ld->sq_ring.head;
652                 long nr = ld->queued;
653
654                 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
655                 if (ret > 0) {
656                         fio_ioring_queued(td, start, ret);
657                         io_u_mark_submit(td, ret);
658
659                         ld->queued -= ret;
660                         ret = 0;
661                 } else if (!ret) {
662                         io_u_mark_submit(td, ret);
663                         continue;
664                 } else {
665                         if (errno == EAGAIN || errno == EINTR) {
666                                 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
667                                 if (ret)
668                                         continue;
669                                 /* Shouldn't happen */
670                                 usleep(1);
671                                 continue;
672                         }
673                         ret = -errno;
674                         td_verror(td, errno, "io_uring_enter submit");
675                         break;
676                 }
677         } while (ld->queued);
678
679         return ret;
680 }
681
682 static void fio_ioring_unmap(struct ioring_data *ld)
683 {
684         int i;
685
686         for (i = 0; i < FIO_ARRAY_SIZE(ld->mmap); i++)
687                 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
688         close(ld->ring_fd);
689 }
690
691 static void fio_ioring_cleanup(struct thread_data *td)
692 {
693         struct ioring_data *ld = td->io_ops_data;
694
695         if (ld) {
696                 if (!(td->flags & TD_F_CHILD))
697                         fio_ioring_unmap(ld);
698
699                 fio_cmdprio_cleanup(&ld->cmdprio);
700                 free(ld->io_u_index);
701                 free(ld->iovecs);
702                 free(ld->fds);
703                 free(ld);
704         }
705 }
706
707 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
708 {
709         struct io_sq_ring *sring = &ld->sq_ring;
710         struct io_cq_ring *cring = &ld->cq_ring;
711         void *ptr;
712
713         ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
714         ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
715                         MAP_SHARED | MAP_POPULATE, ld->ring_fd,
716                         IORING_OFF_SQ_RING);
717         ld->mmap[0].ptr = ptr;
718         sring->head = ptr + p->sq_off.head;
719         sring->tail = ptr + p->sq_off.tail;
720         sring->ring_mask = ptr + p->sq_off.ring_mask;
721         sring->ring_entries = ptr + p->sq_off.ring_entries;
722         sring->flags = ptr + p->sq_off.flags;
723         sring->array = ptr + p->sq_off.array;
724         ld->sq_ring_mask = *sring->ring_mask;
725
726         if (p->flags & IORING_SETUP_SQE128)
727                 ld->mmap[1].len = 2 * p->sq_entries * sizeof(struct io_uring_sqe);
728         else
729                 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
730         ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
731                                 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
732                                 IORING_OFF_SQES);
733         ld->mmap[1].ptr = ld->sqes;
734
735         if (p->flags & IORING_SETUP_CQE32) {
736                 ld->mmap[2].len = p->cq_off.cqes +
737                                         2 * p->cq_entries * sizeof(struct io_uring_cqe);
738         } else {
739                 ld->mmap[2].len = p->cq_off.cqes +
740                                         p->cq_entries * sizeof(struct io_uring_cqe);
741         }
742         ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
743                         MAP_SHARED | MAP_POPULATE, ld->ring_fd,
744                         IORING_OFF_CQ_RING);
745         ld->mmap[2].ptr = ptr;
746         cring->head = ptr + p->cq_off.head;
747         cring->tail = ptr + p->cq_off.tail;
748         cring->ring_mask = ptr + p->cq_off.ring_mask;
749         cring->ring_entries = ptr + p->cq_off.ring_entries;
750         cring->cqes = ptr + p->cq_off.cqes;
751         ld->cq_ring_mask = *cring->ring_mask;
752         return 0;
753 }
754
755 static void fio_ioring_probe(struct thread_data *td)
756 {
757         struct ioring_data *ld = td->io_ops_data;
758         struct ioring_options *o = td->eo;
759         struct io_uring_probe *p;
760         int ret;
761
762         /* already set by user, don't touch */
763         if (o->nonvectored != -1)
764                 return;
765
766         /* default to off, as that's always safe */
767         o->nonvectored = 0;
768
769         p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
770         if (!p)
771                 return;
772
773         memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
774         ret = syscall(__NR_io_uring_register, ld->ring_fd,
775                         IORING_REGISTER_PROBE, p, 256);
776         if (ret < 0)
777                 goto out;
778
779         if (IORING_OP_WRITE > p->ops_len)
780                 goto out;
781
782         if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED) &&
783             (p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED))
784                 o->nonvectored = 1;
785 out:
786         free(p);
787 }
788
789 static int fio_ioring_queue_init(struct thread_data *td)
790 {
791         struct ioring_data *ld = td->io_ops_data;
792         struct ioring_options *o = td->eo;
793         int depth = td->o.iodepth;
794         struct io_uring_params p;
795         int ret;
796
797         memset(&p, 0, sizeof(p));
798
799         if (o->hipri)
800                 p.flags |= IORING_SETUP_IOPOLL;
801         if (o->sqpoll_thread) {
802                 p.flags |= IORING_SETUP_SQPOLL;
803                 if (o->sqpoll_set) {
804                         p.flags |= IORING_SETUP_SQ_AFF;
805                         p.sq_thread_cpu = o->sqpoll_cpu;
806                 }
807         }
808
809         /*
810          * Clamp CQ ring size at our SQ ring size, we don't need more entries
811          * than that.
812          */
813         p.flags |= IORING_SETUP_CQSIZE;
814         p.cq_entries = depth;
815
816         /*
817          * Setup COOP_TASKRUN as we don't need to get IPI interrupted for
818          * completing IO operations.
819          */
820         p.flags |= IORING_SETUP_COOP_TASKRUN;
821
822         /*
823          * io_uring is always a single issuer, and we can defer task_work
824          * runs until we reap events.
825          */
826         p.flags |= IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN;
827
828 retry:
829         ret = syscall(__NR_io_uring_setup, depth, &p);
830         if (ret < 0) {
831                 if (errno == EINVAL && p.flags & IORING_SETUP_DEFER_TASKRUN) {
832                         p.flags &= ~IORING_SETUP_DEFER_TASKRUN;
833                         p.flags &= ~IORING_SETUP_SINGLE_ISSUER;
834                         goto retry;
835                 }
836                 if (errno == EINVAL && p.flags & IORING_SETUP_COOP_TASKRUN) {
837                         p.flags &= ~IORING_SETUP_COOP_TASKRUN;
838                         goto retry;
839                 }
840                 if (errno == EINVAL && p.flags & IORING_SETUP_CQSIZE) {
841                         p.flags &= ~IORING_SETUP_CQSIZE;
842                         goto retry;
843                 }
844                 return ret;
845         }
846
847         ld->ring_fd = ret;
848
849         fio_ioring_probe(td);
850
851         if (o->fixedbufs) {
852                 ret = syscall(__NR_io_uring_register, ld->ring_fd,
853                                 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
854                 if (ret < 0)
855                         return ret;
856         }
857
858         return fio_ioring_mmap(ld, &p);
859 }
860
861 static int fio_ioring_cmd_queue_init(struct thread_data *td)
862 {
863         struct ioring_data *ld = td->io_ops_data;
864         struct ioring_options *o = td->eo;
865         int depth = td->o.iodepth;
866         struct io_uring_params p;
867         int ret;
868
869         memset(&p, 0, sizeof(p));
870
871         if (o->hipri)
872                 p.flags |= IORING_SETUP_IOPOLL;
873         if (o->sqpoll_thread) {
874                 p.flags |= IORING_SETUP_SQPOLL;
875                 if (o->sqpoll_set) {
876                         p.flags |= IORING_SETUP_SQ_AFF;
877                         p.sq_thread_cpu = o->sqpoll_cpu;
878                 }
879         }
880         if (o->cmd_type == FIO_URING_CMD_NVME) {
881                 p.flags |= IORING_SETUP_SQE128;
882                 p.flags |= IORING_SETUP_CQE32;
883         }
884
885         /*
886          * Clamp CQ ring size at our SQ ring size, we don't need more entries
887          * than that.
888          */
889         p.flags |= IORING_SETUP_CQSIZE;
890         p.cq_entries = depth;
891
892 retry:
893         ret = syscall(__NR_io_uring_setup, depth, &p);
894         if (ret < 0) {
895                 if (errno == EINVAL && p.flags & IORING_SETUP_CQSIZE) {
896                         p.flags &= ~IORING_SETUP_CQSIZE;
897                         goto retry;
898                 }
899                 return ret;
900         }
901
902         ld->ring_fd = ret;
903
904         fio_ioring_probe(td);
905
906         if (o->fixedbufs) {
907                 ret = syscall(__NR_io_uring_register, ld->ring_fd,
908                                 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
909                 if (ret < 0)
910                         return ret;
911         }
912
913         return fio_ioring_mmap(ld, &p);
914 }
915
916 static int fio_ioring_register_files(struct thread_data *td)
917 {
918         struct ioring_data *ld = td->io_ops_data;
919         struct fio_file *f;
920         unsigned int i;
921         int ret;
922
923         ld->fds = calloc(td->o.nr_files, sizeof(int));
924
925         for_each_file(td, f, i) {
926                 ret = generic_open_file(td, f);
927                 if (ret)
928                         goto err;
929                 ld->fds[i] = f->fd;
930                 f->engine_pos = i;
931         }
932
933         ret = syscall(__NR_io_uring_register, ld->ring_fd,
934                         IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
935         if (ret) {
936 err:
937                 free(ld->fds);
938                 ld->fds = NULL;
939         }
940
941         /*
942          * Pretend the file is closed again, and really close it if we hit
943          * an error.
944          */
945         for_each_file(td, f, i) {
946                 if (ret) {
947                         int fio_unused ret2;
948                         ret2 = generic_close_file(td, f);
949                 } else
950                         f->fd = -1;
951         }
952
953         return ret;
954 }
955
956 static int fio_ioring_post_init(struct thread_data *td)
957 {
958         struct ioring_data *ld = td->io_ops_data;
959         struct ioring_options *o = td->eo;
960         struct io_u *io_u;
961         int err, i;
962
963         for (i = 0; i < td->o.iodepth; i++) {
964                 struct iovec *iov = &ld->iovecs[i];
965
966                 io_u = ld->io_u_index[i];
967                 iov->iov_base = io_u->buf;
968                 iov->iov_len = td_max_bs(td);
969         }
970
971         err = fio_ioring_queue_init(td);
972         if (err) {
973                 int init_err = errno;
974
975                 if (init_err == ENOSYS)
976                         log_err("fio: your kernel doesn't support io_uring\n");
977                 td_verror(td, init_err, "io_queue_init");
978                 return 1;
979         }
980
981         for (i = 0; i < td->o.iodepth; i++) {
982                 struct io_uring_sqe *sqe;
983
984                 sqe = &ld->sqes[i];
985                 memset(sqe, 0, sizeof(*sqe));
986         }
987
988         if (o->registerfiles) {
989                 err = fio_ioring_register_files(td);
990                 if (err) {
991                         td_verror(td, errno, "ioring_register_files");
992                         return 1;
993                 }
994         }
995
996         return 0;
997 }
998
999 static int fio_ioring_cmd_post_init(struct thread_data *td)
1000 {
1001         struct ioring_data *ld = td->io_ops_data;
1002         struct ioring_options *o = td->eo;
1003         struct io_u *io_u;
1004         int err, i;
1005
1006         for (i = 0; i < td->o.iodepth; i++) {
1007                 struct iovec *iov = &ld->iovecs[i];
1008
1009                 io_u = ld->io_u_index[i];
1010                 iov->iov_base = io_u->buf;
1011                 iov->iov_len = td_max_bs(td);
1012         }
1013
1014         err = fio_ioring_cmd_queue_init(td);
1015         if (err) {
1016                 int init_err = errno;
1017
1018                 td_verror(td, init_err, "io_queue_init");
1019                 return 1;
1020         }
1021
1022         for (i = 0; i < td->o.iodepth; i++) {
1023                 struct io_uring_sqe *sqe;
1024
1025                 if (o->cmd_type == FIO_URING_CMD_NVME) {
1026                         sqe = &ld->sqes[i << 1];
1027                         memset(sqe, 0, 2 * sizeof(*sqe));
1028                 } else {
1029                         sqe = &ld->sqes[i];
1030                         memset(sqe, 0, sizeof(*sqe));
1031                 }
1032         }
1033
1034         if (o->registerfiles) {
1035                 err = fio_ioring_register_files(td);
1036                 if (err) {
1037                         td_verror(td, errno, "ioring_register_files");
1038                         return 1;
1039                 }
1040         }
1041
1042         return 0;
1043 }
1044
1045 static int fio_ioring_init(struct thread_data *td)
1046 {
1047         struct ioring_options *o = td->eo;
1048         struct ioring_data *ld;
1049         int ret;
1050
1051         /* sqthread submission requires registered files */
1052         if (o->sqpoll_thread)
1053                 o->registerfiles = 1;
1054
1055         if (o->registerfiles && td->o.nr_files != td->o.open_files) {
1056                 log_err("fio: io_uring registered files require nr_files to "
1057                         "be identical to open_files\n");
1058                 return 1;
1059         }
1060
1061         ld = calloc(1, sizeof(*ld));
1062
1063         /* ring depth must be a power-of-2 */
1064         ld->iodepth = td->o.iodepth;
1065         td->o.iodepth = roundup_pow2(td->o.iodepth);
1066
1067         /* io_u index */
1068         ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
1069         ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
1070
1071         td->io_ops_data = ld;
1072
1073         ret = fio_cmdprio_init(td, &ld->cmdprio, &o->cmdprio_options);
1074         if (ret) {
1075                 td_verror(td, EINVAL, "fio_ioring_init");
1076                 return 1;
1077         }
1078
1079         return 0;
1080 }
1081
1082 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
1083 {
1084         struct ioring_data *ld = td->io_ops_data;
1085
1086         ld->io_u_index[io_u->index] = io_u;
1087         return 0;
1088 }
1089
1090 static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
1091 {
1092         struct ioring_data *ld = td->io_ops_data;
1093         struct ioring_options *o = td->eo;
1094
1095         if (!ld || !o->registerfiles)
1096                 return generic_open_file(td, f);
1097
1098         f->fd = ld->fds[f->engine_pos];
1099         return 0;
1100 }
1101
1102 static int fio_ioring_cmd_open_file(struct thread_data *td, struct fio_file *f)
1103 {
1104         struct ioring_data *ld = td->io_ops_data;
1105         struct ioring_options *o = td->eo;
1106
1107         if (o->cmd_type == FIO_URING_CMD_NVME) {
1108                 struct nvme_data *data = NULL;
1109                 unsigned int nsid, lba_size = 0;
1110                 unsigned long long nlba = 0;
1111                 int ret;
1112
1113                 /* Store the namespace-id and lba size. */
1114                 data = FILE_ENG_DATA(f);
1115                 if (data == NULL) {
1116                         ret = fio_nvme_get_info(f, &nsid, &lba_size, &nlba);
1117                         if (ret)
1118                                 return ret;
1119
1120                         data = calloc(1, sizeof(struct nvme_data));
1121                         data->nsid = nsid;
1122                         data->lba_shift = ilog2(lba_size);
1123
1124                         FILE_SET_ENG_DATA(f, data);
1125                 }
1126         }
1127         if (!ld || !o->registerfiles)
1128                 return generic_open_file(td, f);
1129
1130         f->fd = ld->fds[f->engine_pos];
1131         return 0;
1132 }
1133
1134 static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
1135 {
1136         struct ioring_data *ld = td->io_ops_data;
1137         struct ioring_options *o = td->eo;
1138
1139         if (!ld || !o->registerfiles)
1140                 return generic_close_file(td, f);
1141
1142         f->fd = -1;
1143         return 0;
1144 }
1145
1146 static int fio_ioring_cmd_close_file(struct thread_data *td,
1147                                      struct fio_file *f)
1148 {
1149         struct ioring_data *ld = td->io_ops_data;
1150         struct ioring_options *o = td->eo;
1151
1152         if (o->cmd_type == FIO_URING_CMD_NVME) {
1153                 struct nvme_data *data = FILE_ENG_DATA(f);
1154
1155                 FILE_SET_ENG_DATA(f, NULL);
1156                 free(data);
1157         }
1158         if (!ld || !o->registerfiles)
1159                 return generic_close_file(td, f);
1160
1161         f->fd = -1;
1162         return 0;
1163 }
1164
1165 static int fio_ioring_cmd_get_file_size(struct thread_data *td,
1166                                         struct fio_file *f)
1167 {
1168         struct ioring_options *o = td->eo;
1169
1170         if (fio_file_size_known(f))
1171                 return 0;
1172
1173         if (o->cmd_type == FIO_URING_CMD_NVME) {
1174                 struct nvme_data *data = NULL;
1175                 unsigned int nsid, lba_size = 0;
1176                 unsigned long long nlba = 0;
1177                 int ret;
1178
1179                 ret = fio_nvme_get_info(f, &nsid, &lba_size, &nlba);
1180                 if (ret)
1181                         return ret;
1182
1183                 data = calloc(1, sizeof(struct nvme_data));
1184                 data->nsid = nsid;
1185                 data->lba_shift = ilog2(lba_size);
1186
1187                 f->real_file_size = lba_size * nlba;
1188                 fio_file_set_size_known(f);
1189
1190                 FILE_SET_ENG_DATA(f, data);
1191                 return 0;
1192         }
1193         return generic_get_file_size(td, f);
1194 }
1195
1196 static int fio_ioring_cmd_get_zoned_model(struct thread_data *td,
1197                                           struct fio_file *f,
1198                                           enum zbd_zoned_model *model)
1199 {
1200         return fio_nvme_get_zoned_model(td, f, model);
1201 }
1202
1203 static int fio_ioring_cmd_report_zones(struct thread_data *td,
1204                                        struct fio_file *f, uint64_t offset,
1205                                        struct zbd_zone *zbdz,
1206                                        unsigned int nr_zones)
1207 {
1208         return fio_nvme_report_zones(td, f, offset, zbdz, nr_zones);
1209 }
1210
1211 static int fio_ioring_cmd_reset_wp(struct thread_data *td, struct fio_file *f,
1212                                    uint64_t offset, uint64_t length)
1213 {
1214         return fio_nvme_reset_wp(td, f, offset, length);
1215 }
1216
1217 static int fio_ioring_cmd_get_max_open_zones(struct thread_data *td,
1218                                              struct fio_file *f,
1219                                              unsigned int *max_open_zones)
1220 {
1221         return fio_nvme_get_max_open_zones(td, f, max_open_zones);
1222 }
1223
1224 static struct ioengine_ops ioengine_uring = {
1225         .name                   = "io_uring",
1226         .version                = FIO_IOOPS_VERSION,
1227         .flags                  = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD |
1228                                         FIO_ASYNCIO_SETS_ISSUE_TIME,
1229         .init                   = fio_ioring_init,
1230         .post_init              = fio_ioring_post_init,
1231         .io_u_init              = fio_ioring_io_u_init,
1232         .prep                   = fio_ioring_prep,
1233         .queue                  = fio_ioring_queue,
1234         .commit                 = fio_ioring_commit,
1235         .getevents              = fio_ioring_getevents,
1236         .event                  = fio_ioring_event,
1237         .cleanup                = fio_ioring_cleanup,
1238         .open_file              = fio_ioring_open_file,
1239         .close_file             = fio_ioring_close_file,
1240         .get_file_size          = generic_get_file_size,
1241         .options                = options,
1242         .option_struct_size     = sizeof(struct ioring_options),
1243 };
1244
1245 static struct ioengine_ops ioengine_uring_cmd = {
1246         .name                   = "io_uring_cmd",
1247         .version                = FIO_IOOPS_VERSION,
1248         .flags                  = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD |
1249                                         FIO_MEMALIGN | FIO_RAWIO |
1250                                         FIO_ASYNCIO_SETS_ISSUE_TIME,
1251         .init                   = fio_ioring_init,
1252         .post_init              = fio_ioring_cmd_post_init,
1253         .io_u_init              = fio_ioring_io_u_init,
1254         .prep                   = fio_ioring_cmd_prep,
1255         .queue                  = fio_ioring_queue,
1256         .commit                 = fio_ioring_commit,
1257         .getevents              = fio_ioring_getevents,
1258         .event                  = fio_ioring_cmd_event,
1259         .cleanup                = fio_ioring_cleanup,
1260         .open_file              = fio_ioring_cmd_open_file,
1261         .close_file             = fio_ioring_cmd_close_file,
1262         .get_file_size          = fio_ioring_cmd_get_file_size,
1263         .get_zoned_model        = fio_ioring_cmd_get_zoned_model,
1264         .report_zones           = fio_ioring_cmd_report_zones,
1265         .reset_wp               = fio_ioring_cmd_reset_wp,
1266         .get_max_open_zones     = fio_ioring_cmd_get_max_open_zones,
1267         .options                = options,
1268         .option_struct_size     = sizeof(struct ioring_options),
1269 };
1270
1271 static void fio_init fio_ioring_register(void)
1272 {
1273         register_ioengine(&ioengine_uring);
1274         register_ioengine(&ioengine_uring_cmd);
1275 }
1276
1277 static void fio_exit fio_ioring_unregister(void)
1278 {
1279         unregister_ioengine(&ioengine_uring);
1280         unregister_ioengine(&ioengine_uring_cmd);
1281 }
1282 #endif