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