engines/io_uring: ensure to use the right opcode for fixed buffers
[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
21 #ifdef ARCH_HAVE_IOURING
22
23 #include "../lib/types.h"
24 #include "../os/io_uring.h"
25
26 struct io_sq_ring {
27         unsigned *head;
28         unsigned *tail;
29         unsigned *ring_mask;
30         unsigned *ring_entries;
31         unsigned *flags;
32         unsigned *array;
33 };
34
35 struct io_cq_ring {
36         unsigned *head;
37         unsigned *tail;
38         unsigned *ring_mask;
39         unsigned *ring_entries;
40         struct io_uring_event *events;
41 };
42
43 struct ioring_mmap {
44         void *ptr;
45         size_t len;
46 };
47
48 struct ioring_data {
49         int ring_fd;
50
51         struct io_u **io_us;
52         struct io_u **io_u_index;
53
54         struct io_sq_ring sq_ring;
55         struct io_uring_iocb *iocbs;
56         struct iovec *iovecs;
57         unsigned sq_ring_mask;
58
59         struct io_cq_ring cq_ring;
60         unsigned cq_ring_mask;
61
62         int queued;
63         int cq_ring_off;
64         unsigned iodepth;
65
66         uint64_t cachehit;
67         uint64_t cachemiss;
68
69         struct ioring_mmap mmap[3];
70 };
71
72 struct ioring_options {
73         void *pad;
74         unsigned int hipri;
75         unsigned int fixedbufs;
76         unsigned int sqthread;
77         unsigned int sqthread_set;
78         unsigned int sqthread_poll;
79         unsigned int sqwq;
80 };
81
82 static int fio_ioring_sqthread_cb(void *data, unsigned long long *val)
83 {
84         struct ioring_options *o = data;
85
86         o->sqthread = *val;
87         o->sqthread_set = 1;
88         return 0;
89 }
90
91 static struct fio_option options[] = {
92         {
93                 .name   = "hipri",
94                 .lname  = "High Priority",
95                 .type   = FIO_OPT_STR_SET,
96                 .off1   = offsetof(struct ioring_options, hipri),
97                 .help   = "Use polled IO completions",
98                 .category = FIO_OPT_C_ENGINE,
99                 .group  = FIO_OPT_G_LIBAIO,
100         },
101         {
102                 .name   = "fixedbufs",
103                 .lname  = "Fixed (pre-mapped) IO buffers",
104                 .type   = FIO_OPT_STR_SET,
105                 .off1   = offsetof(struct ioring_options, fixedbufs),
106                 .help   = "Pre map IO buffers",
107                 .category = FIO_OPT_C_ENGINE,
108                 .group  = FIO_OPT_G_LIBAIO,
109         },
110         {
111                 .name   = "sqthread",
112                 .lname  = "Use kernel SQ thread on this CPU",
113                 .type   = FIO_OPT_INT,
114                 .cb     = fio_ioring_sqthread_cb,
115                 .help   = "Offload submission to kernel thread",
116                 .category = FIO_OPT_C_ENGINE,
117                 .group  = FIO_OPT_G_LIBAIO,
118         },
119         {
120                 .name   = "sqthread_poll",
121                 .lname  = "Kernel SQ thread should poll",
122                 .type   = FIO_OPT_STR_SET,
123                 .off1   = offsetof(struct ioring_options, sqthread_poll),
124                 .help   = "Used with sqthread, enables kernel side polling",
125                 .category = FIO_OPT_C_ENGINE,
126                 .group  = FIO_OPT_G_LIBAIO,
127         },
128         {
129                 .name   = "sqwq",
130                 .lname  = "Offload submission to kernel workqueue",
131                 .type   = FIO_OPT_STR_SET,
132                 .off1   = offsetof(struct ioring_options, sqwq),
133                 .help   = "Offload submission to kernel workqueue",
134                 .category = FIO_OPT_C_ENGINE,
135                 .group  = FIO_OPT_G_LIBAIO,
136         },
137         {
138                 .name   = NULL,
139         },
140 };
141
142 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
143                          unsigned int min_complete, unsigned int flags)
144 {
145         return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
146                         min_complete, flags);
147 }
148
149 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
150 {
151         struct ioring_data *ld = td->io_ops_data;
152         struct ioring_options *o = td->eo;
153         struct fio_file *f = io_u->file;
154         struct io_uring_iocb *iocb;
155
156         iocb = &ld->iocbs[io_u->index];
157         iocb->fd = f->fd;
158         iocb->flags = 0;
159         iocb->ioprio = 0;
160
161         if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
162                 if (io_u->ddir == DDIR_READ) {
163                         if (o->fixedbufs)
164                                 iocb->opcode = IORING_OP_READ_FIXED;
165                         else
166                                 iocb->opcode = IORING_OP_READ;
167                 } else {
168                         if (o->fixedbufs)
169                                 iocb->opcode = IORING_OP_WRITE_FIXED;
170                         else
171                                 iocb->opcode = IORING_OP_WRITE;
172                 }
173                 iocb->off = io_u->offset;
174                 iocb->addr = io_u->xfer_buf;
175                 iocb->len = io_u->xfer_buflen;
176         } else if (ddir_sync(io_u->ddir))
177                 iocb->opcode = IORING_OP_FSYNC;
178
179         return 0;
180 }
181
182 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
183 {
184         struct ioring_data *ld = td->io_ops_data;
185         struct io_uring_event *ev;
186         struct io_u *io_u;
187         unsigned index;
188
189         index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
190
191         ev = &ld->cq_ring.events[index];
192         io_u = ld->io_u_index[ev->index];
193
194         if (ev->res != io_u->xfer_buflen) {
195                 if (ev->res > io_u->xfer_buflen)
196                         io_u->error = -ev->res;
197                 else
198                         io_u->resid = io_u->xfer_buflen - ev->res;
199         } else
200                 io_u->error = 0;
201
202         if (io_u->ddir == DDIR_READ) {
203                 if (ev->flags & IOEV_FLAG_CACHEHIT)
204                         ld->cachehit++;
205                 else
206                         ld->cachemiss++;
207         }
208
209         return io_u;
210 }
211
212 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
213                                    unsigned int max)
214 {
215         struct ioring_data *ld = td->io_ops_data;
216         struct io_cq_ring *ring = &ld->cq_ring;
217         unsigned head, reaped = 0;
218
219         head = *ring->head;
220         do {
221                 read_barrier();
222                 if (head == *ring->tail)
223                         break;
224                 reaped++;
225                 head++;
226         } while (reaped + events < max);
227
228         *ring->head = head;
229         write_barrier();
230         return reaped;
231 }
232
233 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
234                                 unsigned int max, const struct timespec *t)
235 {
236         struct ioring_data *ld = td->io_ops_data;
237         unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
238         struct ioring_options *o = td->eo;
239         struct io_cq_ring *ring = &ld->cq_ring;
240         unsigned events = 0;
241         int r;
242
243         ld->cq_ring_off = *ring->head;
244         do {
245                 r = fio_ioring_cqring_reap(td, events, max);
246                 if (r) {
247                         events += r;
248                         continue;
249                 }
250
251                 if (!o->sqthread_poll) {
252                         r = io_uring_enter(ld, 0, actual_min,
253                                                 IORING_ENTER_GETEVENTS);
254                         if (r < 0) {
255                                 if (errno == EAGAIN)
256                                         continue;
257                                 td_verror(td, errno, "io_uring_enter");
258                                 break;
259                         }
260                 }
261         } while (events < min);
262
263         return r < 0 ? r : events;
264 }
265
266 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
267                                           struct io_u *io_u)
268 {
269         struct ioring_data *ld = td->io_ops_data;
270         struct io_sq_ring *ring = &ld->sq_ring;
271         unsigned tail, next_tail;
272
273         fio_ro_check(td, io_u);
274
275         if (ld->queued == ld->iodepth)
276                 return FIO_Q_BUSY;
277
278         if (io_u->ddir == DDIR_TRIM) {
279                 if (ld->queued)
280                         return FIO_Q_BUSY;
281
282                 do_io_u_trim(td, io_u);
283                 io_u_mark_submit(td, 1);
284                 io_u_mark_complete(td, 1);
285                 return FIO_Q_COMPLETED;
286         }
287
288         tail = *ring->tail;
289         next_tail = tail + 1;
290         read_barrier();
291         if (next_tail == *ring->head)
292                 return FIO_Q_BUSY;
293
294         ring->array[tail & ld->sq_ring_mask] = io_u->index;
295         *ring->tail = next_tail;
296         write_barrier();
297
298         ld->queued++;
299         return FIO_Q_QUEUED;
300 }
301
302 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
303 {
304         struct ioring_data *ld = td->io_ops_data;
305         struct timespec now;
306
307         if (!fio_fill_issue_time(td))
308                 return;
309
310         fio_gettime(&now, NULL);
311
312         while (nr--) {
313                 struct io_sq_ring *ring = &ld->sq_ring;
314                 int index = ring->array[start & ld->sq_ring_mask];
315                 struct io_u *io_u = ld->io_u_index[index];
316
317                 memcpy(&io_u->issue_time, &now, sizeof(now));
318                 io_u_queued(td, io_u);
319
320                 start++;
321         }
322 }
323
324 static int fio_ioring_commit(struct thread_data *td)
325 {
326         struct ioring_data *ld = td->io_ops_data;
327         struct ioring_options *o = td->eo;
328         int ret;
329
330         if (!ld->queued)
331                 return 0;
332
333         /* Nothing to do */
334         if (o->sqthread_poll) {
335                 struct io_sq_ring *ring = &ld->sq_ring;
336
337                 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
338                         io_uring_enter(ld, ld->queued, 0, 0);
339                 ld->queued = 0;
340                 return 0;
341         }
342
343         do {
344                 unsigned start = *ld->sq_ring.head;
345                 long nr = ld->queued;
346
347                 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
348                 if (ret > 0) {
349                         fio_ioring_queued(td, start, ret);
350                         io_u_mark_submit(td, ret);
351
352                         ld->queued -= ret;
353                         ret = 0;
354                 } else if (!ret) {
355                         io_u_mark_submit(td, ret);
356                         continue;
357                 } else {
358                         if (errno == EAGAIN) {
359                                 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
360                                 if (ret)
361                                         continue;
362                                 /* Shouldn't happen */
363                                 usleep(1);
364                                 continue;
365                         }
366                         td_verror(td, errno, "io_uring_enter submit");
367                         break;
368                 }
369         } while (ld->queued);
370
371         return ret;
372 }
373
374 static void fio_ioring_unmap(struct ioring_data *ld)
375 {
376         int i;
377
378         for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
379                 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
380         close(ld->ring_fd);
381 }
382
383 static void fio_ioring_cleanup(struct thread_data *td)
384 {
385         struct ioring_data *ld = td->io_ops_data;
386
387         if (ld) {
388                 td->ts.cachehit += ld->cachehit;
389                 td->ts.cachemiss += ld->cachemiss;
390
391                 if (!(td->flags & TD_F_CHILD))
392                         fio_ioring_unmap(ld);
393
394                 free(ld->io_u_index);
395                 free(ld->io_us);
396                 free(ld->iovecs);
397                 free(ld);
398         }
399 }
400
401 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
402 {
403         struct io_sq_ring *sring = &ld->sq_ring;
404         struct io_cq_ring *cring = &ld->cq_ring;
405         void *ptr;
406
407         ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
408         ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
409                         MAP_SHARED | MAP_POPULATE, ld->ring_fd,
410                         IORING_OFF_SQ_RING);
411         ld->mmap[0].ptr = ptr;
412         sring->head = ptr + p->sq_off.head;
413         sring->tail = ptr + p->sq_off.tail;
414         sring->ring_mask = ptr + p->sq_off.ring_mask;
415         sring->ring_entries = ptr + p->sq_off.ring_entries;
416         sring->flags = ptr + p->sq_off.flags;
417         sring->array = ptr + p->sq_off.array;
418         ld->sq_ring_mask = *sring->ring_mask;
419
420         ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_iocb);
421         ld->iocbs = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
422                                 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
423                                 IORING_OFF_IOCB);
424         ld->mmap[1].ptr = ld->iocbs;
425
426         ld->mmap[2].len = p->cq_off.events +
427                                 p->cq_entries * sizeof(struct io_uring_event);
428         ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
429                         MAP_SHARED | MAP_POPULATE, ld->ring_fd,
430                         IORING_OFF_CQ_RING);
431         ld->mmap[2].ptr = ptr;
432         cring->head = ptr + p->cq_off.head;
433         cring->tail = ptr + p->cq_off.tail;
434         cring->ring_mask = ptr + p->cq_off.ring_mask;
435         cring->ring_entries = ptr + p->cq_off.ring_entries;
436         cring->events = ptr + p->cq_off.events;
437         ld->cq_ring_mask = *cring->ring_mask;
438         return 0;
439 }
440
441 static int fio_ioring_queue_init(struct thread_data *td)
442 {
443         struct ioring_data *ld = td->io_ops_data;
444         struct ioring_options *o = td->eo;
445         int depth = td->o.iodepth;
446         struct io_uring_params p;
447         int ret;
448
449         memset(&p, 0, sizeof(p));
450
451         if (o->hipri)
452                 p.flags |= IORING_SETUP_IOPOLL;
453         if (o->sqthread_set) {
454                 p.sq_thread_cpu = o->sqthread;
455                 p.flags |= IORING_SETUP_SQTHREAD;
456                 if (o->sqthread_poll)
457                         p.flags |= IORING_SETUP_SQPOLL;
458         }
459         if (o->sqwq)
460                 p.flags |= IORING_SETUP_SQWQ;
461
462         if (o->fixedbufs) {
463                 struct rlimit rlim = {
464                         .rlim_cur = RLIM_INFINITY,
465                         .rlim_max = RLIM_INFINITY,
466                 };
467
468                 setrlimit(RLIMIT_MEMLOCK, &rlim);
469                 p.flags |= IORING_SETUP_FIXEDBUFS;
470         }
471
472         ret = syscall(__NR_sys_io_uring_setup, depth, ld->iovecs, &p);
473         if (ret < 0)
474                 return ret;
475
476         ld->ring_fd = ret;
477         return fio_ioring_mmap(ld, &p);
478 }
479
480 static int fio_ioring_post_init(struct thread_data *td)
481 {
482         struct ioring_data *ld = td->io_ops_data;
483         struct ioring_options *o = td->eo;
484         struct io_u *io_u;
485         int err;
486
487         if (o->fixedbufs) {
488                 int i;
489
490                 for (i = 0; i < td->o.iodepth; i++) {
491                         struct iovec *iov = &ld->iovecs[i];
492
493                         io_u = ld->io_u_index[i];
494                         iov->iov_base = io_u->buf;
495                         iov->iov_len = td_max_bs(td);
496                 }
497         }
498
499         err = fio_ioring_queue_init(td);
500         if (err) {
501                 td_verror(td, errno, "io_queue_init");
502                 return 1;
503         }
504
505         return 0;
506 }
507
508 static unsigned roundup_pow2(unsigned depth)
509 {
510         return 1UL << __fls(depth - 1);
511 }
512
513 static int fio_ioring_init(struct thread_data *td)
514 {
515         struct ioring_data *ld;
516
517         ld = calloc(1, sizeof(*ld));
518
519         /* ring depth must be a power-of-2 */
520         ld->iodepth = td->o.iodepth;
521         td->o.iodepth = roundup_pow2(td->o.iodepth);
522
523         /* io_u index */
524         ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
525         ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
526
527         ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
528
529         td->io_ops_data = ld;
530         return 0;
531 }
532
533 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
534 {
535         struct ioring_data *ld = td->io_ops_data;
536
537         ld->io_u_index[io_u->index] = io_u;
538         return 0;
539 }
540
541 static struct ioengine_ops ioengine = {
542         .name                   = "io_uring",
543         .version                = FIO_IOOPS_VERSION,
544         .init                   = fio_ioring_init,
545         .post_init              = fio_ioring_post_init,
546         .io_u_init              = fio_ioring_io_u_init,
547         .prep                   = fio_ioring_prep,
548         .queue                  = fio_ioring_queue,
549         .commit                 = fio_ioring_commit,
550         .getevents              = fio_ioring_getevents,
551         .event                  = fio_ioring_event,
552         .cleanup                = fio_ioring_cleanup,
553         .open_file              = generic_open_file,
554         .close_file             = generic_close_file,
555         .get_file_size          = generic_get_file_size,
556         .options                = options,
557         .option_struct_size     = sizeof(struct ioring_options),
558 };
559
560 static void fio_init fio_ioring_register(void)
561 {
562         register_ioengine(&ioengine);
563 }
564
565 static void fio_exit fio_ioring_unregister(void)
566 {
567         unregister_ioengine(&ioengine);
568 }
569 #endif