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