engines/io_uring: ensure sqe stores are ordered SQ ring tail update
[fio.git] / engines / io_uring.c
... / ...
CommitLineData
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/linux/io_uring.h"
25
26struct 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
35struct io_cq_ring {
36 unsigned *head;
37 unsigned *tail;
38 unsigned *ring_mask;
39 unsigned *ring_entries;
40 struct io_uring_cqe *cqes;
41};
42
43struct ioring_mmap {
44 void *ptr;
45 size_t len;
46};
47
48struct ioring_data {
49 int ring_fd;
50
51 struct io_u **io_u_index;
52
53 struct io_sq_ring sq_ring;
54 struct io_uring_sqe *sqes;
55 struct iovec *iovecs;
56 unsigned sq_ring_mask;
57
58 struct io_cq_ring cq_ring;
59 unsigned cq_ring_mask;
60
61 int queued;
62 int cq_ring_off;
63 unsigned iodepth;
64
65 uint64_t cachehit;
66 uint64_t cachemiss;
67
68 struct ioring_mmap mmap[3];
69};
70
71struct ioring_options {
72 void *pad;
73 unsigned int hipri;
74 unsigned int fixedbufs;
75 unsigned int sqpoll_thread;
76 unsigned int sqpoll_set;
77 unsigned int sqpoll_cpu;
78};
79
80static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
81{
82 struct ioring_options *o = data;
83
84 o->sqpoll_cpu = *val;
85 o->sqpoll_set = 1;
86 return 0;
87}
88
89static struct fio_option options[] = {
90 {
91 .name = "hipri",
92 .lname = "High Priority",
93 .type = FIO_OPT_STR_SET,
94 .off1 = offsetof(struct ioring_options, hipri),
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,
103 .off1 = offsetof(struct ioring_options, fixedbufs),
104 .help = "Pre map IO buffers",
105 .category = FIO_OPT_C_ENGINE,
106 .group = FIO_OPT_G_LIBAIO,
107 },
108 {
109 .name = "sqthread_poll",
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",
120 .type = FIO_OPT_INT,
121 .cb = fio_ioring_sqpoll_cb,
122 .help = "What CPU to run SQ thread polling on",
123 .category = FIO_OPT_C_ENGINE,
124 .group = FIO_OPT_G_LIBAIO,
125 },
126 {
127 .name = NULL,
128 },
129};
130
131static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
132 unsigned int min_complete, unsigned int flags)
133{
134 return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
135 min_complete, flags);
136}
137
138static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
139{
140 struct ioring_data *ld = td->io_ops_data;
141 struct ioring_options *o = td->eo;
142 struct fio_file *f = io_u->file;
143 struct io_uring_sqe *sqe;
144
145 sqe = &ld->sqes[io_u->index];
146 sqe->fd = f->fd;
147 sqe->flags = 0;
148 sqe->ioprio = 0;
149 sqe->buf_index = 0;
150
151 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
152 if (o->fixedbufs) {
153 if (io_u->ddir == DDIR_READ)
154 sqe->opcode = IORING_OP_READ_FIXED;
155 else
156 sqe->opcode = IORING_OP_WRITE_FIXED;
157 sqe->addr = io_u->xfer_buf;
158 sqe->len = io_u->xfer_buflen;
159 sqe->buf_index = io_u->index;
160 } else {
161 if (io_u->ddir == DDIR_READ)
162 sqe->opcode = IORING_OP_READV;
163 else
164 sqe->opcode = IORING_OP_WRITEV;
165 sqe->addr = &ld->iovecs[io_u->index];
166 sqe->len = 1;
167 }
168 sqe->off = io_u->offset;
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;
173 sqe->opcode = IORING_OP_FSYNC;
174 }
175
176 sqe->user_data = (unsigned long) io_u;
177 return 0;
178}
179
180static struct io_u *fio_ioring_event(struct thread_data *td, int event)
181{
182 struct ioring_data *ld = td->io_ops_data;
183 struct io_uring_cqe *cqe;
184 struct io_u *io_u;
185 unsigned index;
186
187 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
188
189 cqe = &ld->cq_ring.cqes[index];
190 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
191
192 if (cqe->res != io_u->xfer_buflen) {
193 if (cqe->res > io_u->xfer_buflen)
194 io_u->error = -cqe->res;
195 else
196 io_u->resid = io_u->xfer_buflen - cqe->res;
197 } else
198 io_u->error = 0;
199
200 if (io_u->ddir == DDIR_READ) {
201 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
202 ld->cachehit++;
203 else
204 ld->cachemiss++;
205 }
206
207 return io_u;
208}
209
210static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
211 unsigned int max)
212{
213 struct ioring_data *ld = td->io_ops_data;
214 struct io_cq_ring *ring = &ld->cq_ring;
215 unsigned head, reaped = 0;
216
217 head = *ring->head;
218 do {
219 read_barrier();
220 if (head == *ring->tail)
221 break;
222 reaped++;
223 head++;
224 } while (reaped + events < max);
225
226 *ring->head = head;
227 write_barrier();
228 return reaped;
229}
230
231static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
232 unsigned int max, const struct timespec *t)
233{
234 struct ioring_data *ld = td->io_ops_data;
235 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
236 struct ioring_options *o = td->eo;
237 struct io_cq_ring *ring = &ld->cq_ring;
238 unsigned events = 0;
239 int r;
240
241 ld->cq_ring_off = *ring->head;
242 do {
243 r = fio_ioring_cqring_reap(td, events, max);
244 if (r) {
245 events += r;
246 continue;
247 }
248
249 if (!o->sqpoll_thread) {
250 r = io_uring_enter(ld, 0, actual_min,
251 IORING_ENTER_GETEVENTS);
252 if (r < 0) {
253 if (errno == EAGAIN)
254 continue;
255 td_verror(td, errno, "io_uring_enter");
256 break;
257 }
258 }
259 } while (events < min);
260
261 return r < 0 ? r : events;
262}
263
264static enum fio_q_status fio_ioring_queue(struct thread_data *td,
265 struct io_u *io_u)
266{
267 struct ioring_data *ld = td->io_ops_data;
268 struct io_sq_ring *ring = &ld->sq_ring;
269 unsigned tail, next_tail;
270
271 fio_ro_check(td, io_u);
272
273 if (ld->queued == ld->iodepth)
274 return FIO_Q_BUSY;
275
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
286 tail = *ring->tail;
287 next_tail = tail + 1;
288 read_barrier();
289 if (next_tail == *ring->head)
290 return FIO_Q_BUSY;
291
292 /* ensure sqe stores are ordered with tail update */
293 write_barrier();
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
302static 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
324static 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 /*
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) {
339 struct io_sq_ring *ring = &ld->sq_ring;
340
341 read_barrier();
342 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
343 io_uring_enter(ld, ld->queued, 0, 0);
344 ld->queued = 0;
345 return 0;
346 }
347
348 do {
349 unsigned start = *ld->sq_ring.head;
350 long nr = ld->queued;
351
352 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
353 if (ret > 0) {
354 fio_ioring_queued(td, start, ret);
355 io_u_mark_submit(td, ret);
356
357 ld->queued -= ret;
358 ret = 0;
359 } else if (!ret) {
360 io_u_mark_submit(td, ret);
361 continue;
362 } else {
363 if (errno == EAGAIN) {
364 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
365 if (ret)
366 continue;
367 /* Shouldn't happen */
368 usleep(1);
369 continue;
370 }
371 td_verror(td, errno, "io_uring_enter submit");
372 break;
373 }
374 } while (ld->queued);
375
376 return ret;
377}
378
379static void fio_ioring_unmap(struct ioring_data *ld)
380{
381 int i;
382
383 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
384 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
385 close(ld->ring_fd);
386}
387
388static void fio_ioring_cleanup(struct thread_data *td)
389{
390 struct ioring_data *ld = td->io_ops_data;
391
392 if (ld) {
393 td->ts.cachehit += ld->cachehit;
394 td->ts.cachemiss += ld->cachemiss;
395
396 if (!(td->flags & TD_F_CHILD))
397 fio_ioring_unmap(ld);
398
399 free(ld->io_u_index);
400 free(ld->iovecs);
401 free(ld);
402 }
403}
404
405static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
406{
407 struct io_sq_ring *sring = &ld->sq_ring;
408 struct io_cq_ring *cring = &ld->cq_ring;
409 void *ptr;
410
411 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
412 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
413 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
414 IORING_OFF_SQ_RING);
415 ld->mmap[0].ptr = ptr;
416 sring->head = ptr + p->sq_off.head;
417 sring->tail = ptr + p->sq_off.tail;
418 sring->ring_mask = ptr + p->sq_off.ring_mask;
419 sring->ring_entries = ptr + p->sq_off.ring_entries;
420 sring->flags = ptr + p->sq_off.flags;
421 sring->array = ptr + p->sq_off.array;
422 ld->sq_ring_mask = *sring->ring_mask;
423
424 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
425 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
426 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
427 IORING_OFF_SQES);
428 ld->mmap[1].ptr = ld->sqes;
429
430 ld->mmap[2].len = p->cq_off.cqes +
431 p->cq_entries * sizeof(struct io_uring_cqe);
432 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
433 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
434 IORING_OFF_CQ_RING);
435 ld->mmap[2].ptr = ptr;
436 cring->head = ptr + p->cq_off.head;
437 cring->tail = ptr + p->cq_off.tail;
438 cring->ring_mask = ptr + p->cq_off.ring_mask;
439 cring->ring_entries = ptr + p->cq_off.ring_entries;
440 cring->cqes = ptr + p->cq_off.cqes;
441 ld->cq_ring_mask = *cring->ring_mask;
442 return 0;
443}
444
445static int fio_ioring_queue_init(struct thread_data *td)
446{
447 struct ioring_data *ld = td->io_ops_data;
448 struct ioring_options *o = td->eo;
449 int depth = td->o.iodepth;
450 struct io_uring_params p;
451 int ret;
452
453 memset(&p, 0, sizeof(p));
454
455 if (o->hipri)
456 p.flags |= IORING_SETUP_IOPOLL;
457 if (o->sqpoll_thread) {
458 p.flags |= IORING_SETUP_SQPOLL;
459 if (o->sqpoll_set) {
460 p.flags |= IORING_SETUP_SQ_AFF;
461 p.sq_thread_cpu = o->sqpoll_cpu;
462 }
463 }
464
465 if (o->fixedbufs) {
466 struct rlimit rlim = {
467 .rlim_cur = RLIM_INFINITY,
468 .rlim_max = RLIM_INFINITY,
469 };
470
471 setrlimit(RLIMIT_MEMLOCK, &rlim);
472 }
473
474 ret = syscall(__NR_sys_io_uring_setup, depth, &p);
475 if (ret < 0)
476 return ret;
477
478 ld->ring_fd = ret;
479
480 if (o->fixedbufs) {
481 struct io_uring_register_buffers reg = {
482 .iovecs = ld->iovecs,
483 .nr_iovecs = depth
484 };
485
486 ret = syscall(__NR_sys_io_uring_register, ld->ring_fd,
487 IORING_REGISTER_BUFFERS, &reg);
488 if (ret < 0)
489 return ret;
490 }
491
492 return fio_ioring_mmap(ld, &p);
493}
494
495static int fio_ioring_post_init(struct thread_data *td)
496{
497 struct ioring_data *ld = td->io_ops_data;
498 struct io_u *io_u;
499 int err, i;
500
501 for (i = 0; i < td->o.iodepth; i++) {
502 struct iovec *iov = &ld->iovecs[i];
503
504 io_u = ld->io_u_index[i];
505 iov->iov_base = io_u->buf;
506 iov->iov_len = td_max_bs(td);
507 }
508
509 err = fio_ioring_queue_init(td);
510 if (err) {
511 td_verror(td, errno, "io_queue_init");
512 return 1;
513 }
514
515 return 0;
516}
517
518static unsigned roundup_pow2(unsigned depth)
519{
520 return 1UL << __fls(depth - 1);
521}
522
523static int fio_ioring_init(struct thread_data *td)
524{
525 struct ioring_data *ld;
526
527 ld = calloc(1, sizeof(*ld));
528
529 /* ring depth must be a power-of-2 */
530 ld->iodepth = td->o.iodepth;
531 td->o.iodepth = roundup_pow2(td->o.iodepth);
532
533 /* io_u index */
534 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
535 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
536
537 td->io_ops_data = ld;
538 return 0;
539}
540
541static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
542{
543 struct ioring_data *ld = td->io_ops_data;
544
545 ld->io_u_index[io_u->index] = io_u;
546 return 0;
547}
548
549static struct ioengine_ops ioengine = {
550 .name = "io_uring",
551 .version = FIO_IOOPS_VERSION,
552 .init = fio_ioring_init,
553 .post_init = fio_ioring_post_init,
554 .io_u_init = fio_ioring_io_u_init,
555 .prep = fio_ioring_prep,
556 .queue = fio_ioring_queue,
557 .commit = fio_ioring_commit,
558 .getevents = fio_ioring_getevents,
559 .event = fio_ioring_event,
560 .cleanup = fio_ioring_cleanup,
561 .open_file = generic_open_file,
562 .close_file = generic_close_file,
563 .get_file_size = generic_get_file_size,
564 .options = options,
565 .option_struct_size = sizeof(struct ioring_options),
566};
567
568static void fio_init fio_ioring_register(void)
569{
570 register_ioengine(&ioengine);
571}
572
573static void fio_exit fio_ioring_unregister(void)
574{
575 unregister_ioengine(&ioengine);
576}
577#endif