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