configure: add __kernel_rwf_t check
[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 fio_file *f = io_u->file;
153         struct io_uring_iocb *iocb;
154
155         iocb = &ld->iocbs[io_u->index];
156         iocb->fd = f->fd;
157         iocb->flags = 0;
158         iocb->ioprio = 0;
159
160         if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
161                 if (io_u->ddir == DDIR_READ)
162                         iocb->opcode = IORING_OP_READ;
163                 else
164                         iocb->opcode = IORING_OP_WRITE;
165                 iocb->off = io_u->offset;
166                 iocb->addr = io_u->xfer_buf;
167                 iocb->len = io_u->xfer_buflen;
168         } else if (ddir_sync(io_u->ddir))
169                 iocb->opcode = IORING_OP_FSYNC;
170
171         return 0;
172 }
173
174 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
175 {
176         struct ioring_data *ld = td->io_ops_data;
177         struct io_uring_event *ev;
178         struct io_u *io_u;
179         unsigned index;
180
181         index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
182
183         ev = &ld->cq_ring.events[index];
184         io_u = ld->io_u_index[ev->index];
185
186         if (ev->res != io_u->xfer_buflen) {
187                 if (ev->res > io_u->xfer_buflen)
188                         io_u->error = -ev->res;
189                 else
190                         io_u->resid = io_u->xfer_buflen - ev->res;
191         } else
192                 io_u->error = 0;
193
194         if (io_u->ddir == DDIR_READ) {
195                 if (ev->flags & IOEV_FLAG_CACHEHIT)
196                         ld->cachehit++;
197                 else
198                         ld->cachemiss++;
199         }
200
201         return io_u;
202 }
203
204 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
205                                    unsigned int max)
206 {
207         struct ioring_data *ld = td->io_ops_data;
208         struct io_cq_ring *ring = &ld->cq_ring;
209         unsigned head, reaped = 0;
210
211         head = *ring->head;
212         do {
213                 read_barrier();
214                 if (head == *ring->tail)
215                         break;
216                 reaped++;
217                 head++;
218         } while (reaped + events < max);
219
220         *ring->head = head;
221         write_barrier();
222         return reaped;
223 }
224
225 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
226                                 unsigned int max, const struct timespec *t)
227 {
228         struct ioring_data *ld = td->io_ops_data;
229         unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
230         struct ioring_options *o = td->eo;
231         struct io_cq_ring *ring = &ld->cq_ring;
232         unsigned events = 0;
233         int r;
234
235         ld->cq_ring_off = *ring->head;
236         do {
237                 r = fio_ioring_cqring_reap(td, events, max);
238                 if (r) {
239                         events += r;
240                         continue;
241                 }
242
243                 if (!o->sqthread_poll) {
244                         r = io_uring_enter(ld, 0, actual_min,
245                                                 IORING_ENTER_GETEVENTS);
246                         if (r < 0) {
247                                 if (errno == EAGAIN)
248                                         continue;
249                                 td_verror(td, errno, "io_uring_enter");
250                                 break;
251                         }
252                 }
253         } while (events < min);
254
255         return r < 0 ? r : events;
256 }
257
258 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
259                                           struct io_u *io_u)
260 {
261         struct ioring_data *ld = td->io_ops_data;
262         struct io_sq_ring *ring = &ld->sq_ring;
263         unsigned tail, next_tail;
264
265         fio_ro_check(td, io_u);
266
267         if (ld->queued == ld->iodepth)
268                 return FIO_Q_BUSY;
269
270         if (io_u->ddir == DDIR_TRIM) {
271                 if (ld->queued)
272                         return FIO_Q_BUSY;
273
274                 do_io_u_trim(td, io_u);
275                 io_u_mark_submit(td, 1);
276                 io_u_mark_complete(td, 1);
277                 return FIO_Q_COMPLETED;
278         }
279
280         tail = *ring->tail;
281         next_tail = tail + 1;
282         read_barrier();
283         if (next_tail == *ring->head)
284                 return FIO_Q_BUSY;
285
286         ring->array[tail & ld->sq_ring_mask] = io_u->index;
287         *ring->tail = next_tail;
288         write_barrier();
289
290         ld->queued++;
291         return FIO_Q_QUEUED;
292 }
293
294 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
295 {
296         struct ioring_data *ld = td->io_ops_data;
297         struct timespec now;
298
299         if (!fio_fill_issue_time(td))
300                 return;
301
302         fio_gettime(&now, NULL);
303
304         while (nr--) {
305                 struct io_sq_ring *ring = &ld->sq_ring;
306                 int index = ring->array[start & ld->sq_ring_mask];
307                 struct io_u *io_u = ld->io_u_index[index];
308
309                 memcpy(&io_u->issue_time, &now, sizeof(now));
310                 io_u_queued(td, io_u);
311
312                 start++;
313         }
314 }
315
316 static int fio_ioring_commit(struct thread_data *td)
317 {
318         struct ioring_data *ld = td->io_ops_data;
319         struct ioring_options *o = td->eo;
320         int ret;
321
322         if (!ld->queued)
323                 return 0;
324
325         /* Nothing to do */
326         if (o->sqthread_poll) {
327                 struct io_sq_ring *ring = &ld->sq_ring;
328
329                 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
330                         io_uring_enter(ld, ld->queued, 0, 0);
331                 ld->queued = 0;
332                 return 0;
333         }
334
335         do {
336                 unsigned start = *ld->sq_ring.head;
337                 long nr = ld->queued;
338
339                 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
340                 if (ret > 0) {
341                         fio_ioring_queued(td, start, ret);
342                         io_u_mark_submit(td, ret);
343
344                         ld->queued -= ret;
345                         ret = 0;
346                 } else if (!ret) {
347                         io_u_mark_submit(td, ret);
348                         continue;
349                 } else {
350                         if (errno == EAGAIN) {
351                                 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
352                                 if (ret)
353                                         continue;
354                                 /* Shouldn't happen */
355                                 usleep(1);
356                                 continue;
357                         }
358                         td_verror(td, errno, "io_uring_enter submit");
359                         break;
360                 }
361         } while (ld->queued);
362
363         return ret;
364 }
365
366 static void fio_ioring_unmap(struct ioring_data *ld)
367 {
368         int i;
369
370         for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
371                 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
372         close(ld->ring_fd);
373 }
374
375 static void fio_ioring_cleanup(struct thread_data *td)
376 {
377         struct ioring_data *ld = td->io_ops_data;
378
379         if (ld) {
380                 td->ts.cachehit += ld->cachehit;
381                 td->ts.cachemiss += ld->cachemiss;
382
383                 if (!(td->flags & TD_F_CHILD))
384                         fio_ioring_unmap(ld);
385
386                 free(ld->io_u_index);
387                 free(ld->io_us);
388                 free(ld->iovecs);
389                 free(ld);
390         }
391 }
392
393 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
394 {
395         struct io_sq_ring *sring = &ld->sq_ring;
396         struct io_cq_ring *cring = &ld->cq_ring;
397         void *ptr;
398
399         ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
400         ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
401                         MAP_SHARED | MAP_POPULATE, ld->ring_fd,
402                         IORING_OFF_SQ_RING);
403         ld->mmap[0].ptr = ptr;
404         sring->head = ptr + p->sq_off.head;
405         sring->tail = ptr + p->sq_off.tail;
406         sring->ring_mask = ptr + p->sq_off.ring_mask;
407         sring->ring_entries = ptr + p->sq_off.ring_entries;
408         sring->flags = ptr + p->sq_off.flags;
409         sring->array = ptr + p->sq_off.array;
410         ld->sq_ring_mask = *sring->ring_mask;
411
412         ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_iocb);
413         ld->iocbs = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
414                                 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
415                                 IORING_OFF_IOCB);
416         ld->mmap[1].ptr = ld->iocbs;
417
418         ld->mmap[2].len = p->cq_off.events +
419                                 p->cq_entries * sizeof(struct io_uring_event);
420         ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
421                         MAP_SHARED | MAP_POPULATE, ld->ring_fd,
422                         IORING_OFF_CQ_RING);
423         ld->mmap[2].ptr = ptr;
424         cring->head = ptr + p->cq_off.head;
425         cring->tail = ptr + p->cq_off.tail;
426         cring->ring_mask = ptr + p->cq_off.ring_mask;
427         cring->ring_entries = ptr + p->cq_off.ring_entries;
428         cring->events = ptr + p->cq_off.events;
429         ld->cq_ring_mask = *cring->ring_mask;
430         return 0;
431 }
432
433 static int fio_ioring_queue_init(struct thread_data *td)
434 {
435         struct ioring_data *ld = td->io_ops_data;
436         struct ioring_options *o = td->eo;
437         int depth = td->o.iodepth;
438         struct io_uring_params p;
439         int ret;
440
441         memset(&p, 0, sizeof(p));
442
443         if (o->hipri)
444                 p.flags |= IORING_SETUP_IOPOLL;
445         if (o->sqthread_set) {
446                 p.sq_thread_cpu = o->sqthread;
447                 p.flags |= IORING_SETUP_SQTHREAD;
448                 if (o->sqthread_poll)
449                         p.flags |= IORING_SETUP_SQPOLL;
450         }
451         if (o->sqwq)
452                 p.flags |= IORING_SETUP_SQWQ;
453
454         if (o->fixedbufs) {
455                 struct rlimit rlim = {
456                         .rlim_cur = RLIM_INFINITY,
457                         .rlim_max = RLIM_INFINITY,
458                 };
459
460                 setrlimit(RLIMIT_MEMLOCK, &rlim);
461                 p.flags |= IORING_SETUP_FIXEDBUFS;
462         }
463
464         ret = syscall(__NR_sys_io_uring_setup, depth, ld->iovecs, &p);
465         if (ret < 0)
466                 return ret;
467
468         ld->ring_fd = ret;
469         return fio_ioring_mmap(ld, &p);
470 }
471
472 static int fio_ioring_post_init(struct thread_data *td)
473 {
474         struct ioring_data *ld = td->io_ops_data;
475         struct ioring_options *o = td->eo;
476         struct io_u *io_u;
477         int err;
478
479         if (o->fixedbufs) {
480                 int i;
481
482                 for (i = 0; i < td->o.iodepth; i++) {
483                         struct iovec *iov = &ld->iovecs[i];
484
485                         io_u = ld->io_u_index[i];
486                         iov->iov_base = io_u->buf;
487                         iov->iov_len = td_max_bs(td);
488                 }
489         }
490
491         err = fio_ioring_queue_init(td);
492         if (err) {
493                 td_verror(td, errno, "io_queue_init");
494                 return 1;
495         }
496
497         return 0;
498 }
499
500 static unsigned roundup_pow2(unsigned depth)
501 {
502         return 1UL << __fls(depth - 1);
503 }
504
505 static int fio_ioring_init(struct thread_data *td)
506 {
507         struct ioring_data *ld;
508
509         ld = calloc(1, sizeof(*ld));
510
511         /* ring depth must be a power-of-2 */
512         ld->iodepth = td->o.iodepth;
513         td->o.iodepth = roundup_pow2(td->o.iodepth);
514
515         /* io_u index */
516         ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
517         ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
518
519         ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
520
521         td->io_ops_data = ld;
522         return 0;
523 }
524
525 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
526 {
527         struct ioring_data *ld = td->io_ops_data;
528
529         ld->io_u_index[io_u->index] = io_u;
530         return 0;
531 }
532
533 static struct ioengine_ops ioengine = {
534         .name                   = "io_uring",
535         .version                = FIO_IOOPS_VERSION,
536         .init                   = fio_ioring_init,
537         .post_init              = fio_ioring_post_init,
538         .io_u_init              = fio_ioring_io_u_init,
539         .prep                   = fio_ioring_prep,
540         .queue                  = fio_ioring_queue,
541         .commit                 = fio_ioring_commit,
542         .getevents              = fio_ioring_getevents,
543         .event                  = fio_ioring_event,
544         .cleanup                = fio_ioring_cleanup,
545         .open_file              = generic_open_file,
546         .close_file             = generic_close_file,
547         .get_file_size          = generic_get_file_size,
548         .options                = options,
549         .option_struct_size     = sizeof(struct ioring_options),
550 };
551
552 static void fio_init fio_ioring_register(void)
553 {
554         register_ioengine(&ioengine);
555 }
556
557 static void fio_exit fio_ioring_unregister(void)
558 {
559         unregister_ioengine(&ioengine);
560 }
561 #endif