libaio,io_uring: introduce cmdprio_bssplit
[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"
6d975f2c 20#include "../lib/roundup.h"
52885fa2 21
bffad86f 22#ifdef ARCH_HAVE_IOURING
52885fa2 23
57fa61f0 24#include "../lib/types.h"
f3e769a4 25#include "../os/linux/io_uring.h"
e9f6567a 26#include "cmdprio.h"
9a2d78b3 27
bffad86f 28struct io_sq_ring {
e2239016
JA
29 unsigned *head;
30 unsigned *tail;
31 unsigned *ring_mask;
32 unsigned *ring_entries;
33 unsigned *flags;
34 unsigned *array;
52885fa2
JA
35};
36
bffad86f 37struct io_cq_ring {
e2239016
JA
38 unsigned *head;
39 unsigned *tail;
40 unsigned *ring_mask;
41 unsigned *ring_entries;
f0403f94 42 struct io_uring_cqe *cqes;
9a2d78b3
JA
43};
44
bffad86f 45struct ioring_mmap {
9a2d78b3
JA
46 void *ptr;
47 size_t len;
52885fa2
JA
48};
49
bffad86f 50struct ioring_data {
9a2d78b3
JA
51 int ring_fd;
52
52885fa2
JA
53 struct io_u **io_u_index;
54
5ffd5626
JA
55 int *fds;
56
bffad86f 57 struct io_sq_ring sq_ring;
f0403f94 58 struct io_uring_sqe *sqes;
9a2d78b3 59 struct iovec *iovecs;
b87aa01a 60 unsigned sq_ring_mask;
52885fa2 61
bffad86f 62 struct io_cq_ring cq_ring;
b87aa01a 63 unsigned cq_ring_mask;
52885fa2
JA
64
65 int queued;
66 int cq_ring_off;
b87aa01a 67 unsigned iodepth;
1af44196
XW
68 bool ioprio_class_set;
69 bool ioprio_set;
5a59a81d 70 int prepped;
96563db9 71
bffad86f 72 struct ioring_mmap mmap[3];
e9f6567a
DLM
73
74 bool use_cmdprio;
52885fa2
JA
75};
76
bffad86f 77struct ioring_options {
a48f0cc7 78 struct thread_data *td;
52885fa2 79 unsigned int hipri;
e9f6567a 80 struct cmdprio cmdprio;
52885fa2 81 unsigned int fixedbufs;
5ffd5626 82 unsigned int registerfiles;
3d7d00a3 83 unsigned int sqpoll_thread;
2ea53ca3
JA
84 unsigned int sqpoll_set;
85 unsigned int sqpoll_cpu;
b10b1e70 86 unsigned int nonvectored;
4a87b584 87 unsigned int uncached;
7d42e66e 88 unsigned int nowait;
5a59a81d 89 unsigned int force_async;
52885fa2
JA
90};
91
b10b1e70
JA
92static const int ddir_to_op[2][2] = {
93 { IORING_OP_READV, IORING_OP_READ },
94 { IORING_OP_WRITEV, IORING_OP_WRITE }
95};
96
3f1e3af7
KB
97static const int fixed_ddir_to_op[2] = {
98 IORING_OP_READ_FIXED,
99 IORING_OP_WRITE_FIXED
100};
101
2ea53ca3 102static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
a90cd050 103{
bffad86f 104 struct ioring_options *o = data;
a90cd050 105
2ea53ca3
JA
106 o->sqpoll_cpu = *val;
107 o->sqpoll_set = 1;
a90cd050
JA
108 return 0;
109}
110
a48f0cc7
DLM
111static int str_cmdprio_bssplit_cb(void *data, const char *input)
112{
113 struct ioring_options *o = data;
114 struct thread_data *td = o->td;
115 struct cmdprio *cmdprio = &o->cmdprio;
116
117 return fio_cmdprio_bssplit_parse(td, input, cmdprio);
118}
119
52885fa2
JA
120static struct fio_option options[] = {
121 {
122 .name = "hipri",
123 .lname = "High Priority",
124 .type = FIO_OPT_STR_SET,
bffad86f 125 .off1 = offsetof(struct ioring_options, hipri),
52885fa2
JA
126 .help = "Use polled IO completions",
127 .category = FIO_OPT_C_ENGINE,
27f436d9 128 .group = FIO_OPT_G_IOURING,
52885fa2 129 },
b2a432bf
PC
130#ifdef FIO_HAVE_IOPRIO_CLASS
131 {
132 .name = "cmdprio_percentage",
133 .lname = "high priority percentage",
134 .type = FIO_OPT_INT,
e9f6567a
DLM
135 .off1 = offsetof(struct ioring_options,
136 cmdprio.percentage[DDIR_READ]),
137 .off2 = offsetof(struct ioring_options,
138 cmdprio.percentage[DDIR_WRITE]),
139 .minval = 0,
b2a432bf
PC
140 .maxval = 100,
141 .help = "Send high priority I/O this percentage of the time",
142 .category = FIO_OPT_C_ENGINE,
143 .group = FIO_OPT_G_IOURING,
144 },
12f9d54a
DLM
145 {
146 .name = "cmdprio_class",
147 .lname = "Asynchronous I/O priority class",
148 .type = FIO_OPT_INT,
149 .off1 = offsetof(struct ioring_options,
150 cmdprio.class[DDIR_READ]),
151 .off2 = offsetof(struct ioring_options,
152 cmdprio.class[DDIR_WRITE]),
153 .help = "Set asynchronous IO priority class",
154 .minval = IOPRIO_MIN_PRIO_CLASS + 1,
155 .maxval = IOPRIO_MAX_PRIO_CLASS,
156 .interval = 1,
157 .category = FIO_OPT_C_ENGINE,
158 .group = FIO_OPT_G_IOURING,
159 },
160 {
161 .name = "cmdprio",
162 .lname = "Asynchronous I/O priority level",
163 .type = FIO_OPT_INT,
164 .off1 = offsetof(struct ioring_options,
165 cmdprio.level[DDIR_READ]),
166 .off2 = offsetof(struct ioring_options,
167 cmdprio.level[DDIR_WRITE]),
168 .help = "Set asynchronous IO priority level",
169 .minval = IOPRIO_MIN_PRIO,
170 .maxval = IOPRIO_MAX_PRIO,
171 .interval = 1,
172 .category = FIO_OPT_C_ENGINE,
173 .group = FIO_OPT_G_IOURING,
174 },
a48f0cc7
DLM
175 {
176 .name = "cmdprio_bssplit",
177 .lname = "Priority percentage block size split",
178 .type = FIO_OPT_STR_ULL,
179 .cb = str_cmdprio_bssplit_cb,
180 .off1 = offsetof(struct ioring_options, cmdprio.bssplit),
181 .help = "Set priority percentages for different block sizes",
182 .category = FIO_OPT_C_ENGINE,
183 .group = FIO_OPT_G_IOURING,
184 },
b2a432bf
PC
185#else
186 {
187 .name = "cmdprio_percentage",
188 .lname = "high priority percentage",
189 .type = FIO_OPT_UNSUPPORTED,
190 .help = "Your platform does not support I/O priority classes",
191 },
12f9d54a
DLM
192 {
193 .name = "cmdprio_class",
194 .lname = "Asynchronous I/O priority class",
195 .type = FIO_OPT_UNSUPPORTED,
196 .help = "Your platform does not support I/O priority classes",
197 },
198 {
199 .name = "cmdprio",
200 .lname = "Asynchronous I/O priority level",
201 .type = FIO_OPT_UNSUPPORTED,
202 .help = "Your platform does not support I/O priority classes",
203 },
a48f0cc7
DLM
204 {
205 .name = "cmdprio_bssplit",
206 .lname = "Priority percentage block size split",
207 .type = FIO_OPT_UNSUPPORTED,
208 .help = "Your platform does not support I/O priority classes",
209 },
b2a432bf 210#endif
52885fa2
JA
211 {
212 .name = "fixedbufs",
213 .lname = "Fixed (pre-mapped) IO buffers",
214 .type = FIO_OPT_STR_SET,
bffad86f 215 .off1 = offsetof(struct ioring_options, fixedbufs),
52885fa2
JA
216 .help = "Pre map IO buffers",
217 .category = FIO_OPT_C_ENGINE,
27f436d9 218 .group = FIO_OPT_G_IOURING,
52885fa2 219 },
5ffd5626
JA
220 {
221 .name = "registerfiles",
222 .lname = "Register file set",
223 .type = FIO_OPT_STR_SET,
224 .off1 = offsetof(struct ioring_options, registerfiles),
225 .help = "Pre-open/register files",
226 .category = FIO_OPT_C_ENGINE,
27f436d9 227 .group = FIO_OPT_G_IOURING,
5ffd5626 228 },
771c9901
JA
229 {
230 .name = "sqthread_poll",
3d7d00a3
JA
231 .lname = "Kernel SQ thread polling",
232 .type = FIO_OPT_INT,
233 .off1 = offsetof(struct ioring_options, sqpoll_thread),
234 .help = "Offload submission/completion to kernel thread",
235 .category = FIO_OPT_C_ENGINE,
27f436d9 236 .group = FIO_OPT_G_IOURING,
3d7d00a3
JA
237 },
238 {
239 .name = "sqthread_poll_cpu",
240 .lname = "SQ Thread Poll CPU",
2ea53ca3
JA
241 .type = FIO_OPT_INT,
242 .cb = fio_ioring_sqpoll_cb,
3d7d00a3 243 .help = "What CPU to run SQ thread polling on",
a90cd050 244 .category = FIO_OPT_C_ENGINE,
27f436d9 245 .group = FIO_OPT_G_IOURING,
a90cd050 246 },
b10b1e70
JA
247 {
248 .name = "nonvectored",
249 .lname = "Non-vectored",
250 .type = FIO_OPT_INT,
251 .off1 = offsetof(struct ioring_options, nonvectored),
556d8415 252 .def = "-1",
b10b1e70
JA
253 .help = "Use non-vectored read/write commands",
254 .category = FIO_OPT_C_ENGINE,
255 .group = FIO_OPT_G_IOURING,
256 },
4a87b584
JA
257 {
258 .name = "uncached",
259 .lname = "Uncached",
260 .type = FIO_OPT_INT,
261 .off1 = offsetof(struct ioring_options, uncached),
262 .help = "Use RWF_UNCACHED for buffered read/writes",
263 .category = FIO_OPT_C_ENGINE,
264 .group = FIO_OPT_G_IOURING,
265 },
7d42e66e
KK
266 {
267 .name = "nowait",
268 .lname = "RWF_NOWAIT",
269 .type = FIO_OPT_BOOL,
270 .off1 = offsetof(struct ioring_options, nowait),
271 .help = "Use RWF_NOWAIT for reads/writes",
272 .category = FIO_OPT_C_ENGINE,
273 .group = FIO_OPT_G_IOURING,
274 },
5a59a81d
JA
275 {
276 .name = "force_async",
277 .lname = "Force async",
278 .type = FIO_OPT_INT,
279 .off1 = offsetof(struct ioring_options, force_async),
280 .help = "Set IOSQE_ASYNC every N requests",
281 .category = FIO_OPT_C_ENGINE,
282 .group = FIO_OPT_G_IOURING,
283 },
52885fa2
JA
284 {
285 .name = NULL,
286 },
287};
288
bffad86f 289static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
52885fa2
JA
290 unsigned int min_complete, unsigned int flags)
291{
bfed648c 292 return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit,
521164fa 293 min_complete, flags, NULL, 0);
52885fa2
JA
294}
295
bffad86f 296static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
52885fa2 297{
bffad86f 298 struct ioring_data *ld = td->io_ops_data;
cfcc8564 299 struct ioring_options *o = td->eo;
52885fa2 300 struct fio_file *f = io_u->file;
f0403f94 301 struct io_uring_sqe *sqe;
52885fa2 302
f0403f94 303 sqe = &ld->sqes[io_u->index];
34d6090e 304
5ffd5626
JA
305 if (o->registerfiles) {
306 sqe->fd = f->engine_pos;
307 sqe->flags = IOSQE_FIXED_FILE;
308 } else {
309 sqe->fd = f->fd;
87b69ef2 310 sqe->flags = 0;
5ffd5626 311 }
52885fa2 312
e3970057 313 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
f0403f94 314 if (o->fixedbufs) {
3f1e3af7 315 sqe->opcode = fixed_ddir_to_op[io_u->ddir];
919850d2 316 sqe->addr = (unsigned long) io_u->xfer_buf;
f0403f94 317 sqe->len = io_u->xfer_buflen;
2ea53ca3 318 sqe->buf_index = io_u->index;
cfcc8564 319 } else {
832faaaf
JA
320 struct iovec *iov = &ld->iovecs[io_u->index];
321
322 /*
323 * Update based on actual io_u, requeue could have
324 * adjusted these
325 */
326 iov->iov_base = io_u->xfer_buf;
327 iov->iov_len = io_u->xfer_buflen;
328
3f1e3af7 329 sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
b10b1e70 330 if (o->nonvectored) {
832faaaf
JA
331 sqe->addr = (unsigned long) iov->iov_base;
332 sqe->len = iov->iov_len;
b10b1e70 333 } else {
832faaaf 334 sqe->addr = (unsigned long) iov;
b10b1e70
JA
335 sqe->len = 1;
336 }
cfcc8564 337 }
fd70e361 338 sqe->rw_flags = 0;
4a87b584 339 if (!td->o.odirect && o->uncached)
fd70e361 340 sqe->rw_flags |= RWF_UNCACHED;
7d42e66e
KK
341 if (o->nowait)
342 sqe->rw_flags |= RWF_NOWAIT;
1af44196 343 if (ld->ioprio_class_set)
b7ed2a86 344 sqe->ioprio = td->o.ioprio_class << 13;
1af44196 345 if (ld->ioprio_set)
b7ed2a86 346 sqe->ioprio |= td->o.ioprio;
f0403f94 347 sqe->off = io_u->offset;
48e698fa 348 } else if (ddir_sync(io_u->ddir)) {
7c70f506 349 sqe->ioprio = 0;
01387bfe
AF
350 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
351 sqe->off = f->first_write;
352 sqe->len = f->last_write - f->first_write;
353 sqe->sync_range_flags = td->o.sync_file_range;
354 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
355 } else {
7c70f506
JA
356 sqe->off = 0;
357 sqe->addr = 0;
358 sqe->len = 0;
01387bfe
AF
359 if (io_u->ddir == DDIR_DATASYNC)
360 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
361 sqe->opcode = IORING_OP_FSYNC;
362 }
48e698fa 363 }
52885fa2 364
5a59a81d
JA
365 if (o->force_async && ++ld->prepped == o->force_async) {
366 ld->prepped = 0;
367 sqe->flags |= IOSQE_ASYNC;
368 }
369
48e698fa 370 sqe->user_data = (unsigned long) io_u;
52885fa2
JA
371 return 0;
372}
373
bffad86f 374static struct io_u *fio_ioring_event(struct thread_data *td, int event)
52885fa2 375{
bffad86f 376 struct ioring_data *ld = td->io_ops_data;
f0403f94 377 struct io_uring_cqe *cqe;
52885fa2 378 struct io_u *io_u;
b87aa01a 379 unsigned index;
52885fa2 380
b87aa01a 381 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
52885fa2 382
f0403f94 383 cqe = &ld->cq_ring.cqes[index];
e3466352 384 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
52885fa2 385
f0403f94
JA
386 if (cqe->res != io_u->xfer_buflen) {
387 if (cqe->res > io_u->xfer_buflen)
388 io_u->error = -cqe->res;
52885fa2 389 else
f0403f94 390 io_u->resid = io_u->xfer_buflen - cqe->res;
52885fa2
JA
391 } else
392 io_u->error = 0;
393
394 return io_u;
395}
396
bffad86f 397static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
52885fa2
JA
398 unsigned int max)
399{
bffad86f
JA
400 struct ioring_data *ld = td->io_ops_data;
401 struct io_cq_ring *ring = &ld->cq_ring;
e2239016 402 unsigned head, reaped = 0;
52885fa2 403
9a2d78b3 404 head = *ring->head;
52885fa2 405 do {
9e26aff9 406 if (head == atomic_load_acquire(ring->tail))
52885fa2
JA
407 break;
408 reaped++;
409 head++;
52885fa2
JA
410 } while (reaped + events < max);
411
76ce63dd
AB
412 if (reaped)
413 atomic_store_release(ring->head, head);
414
52885fa2
JA
415 return reaped;
416}
417
bffad86f
JA
418static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
419 unsigned int max, const struct timespec *t)
52885fa2 420{
bffad86f 421 struct ioring_data *ld = td->io_ops_data;
52885fa2 422 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
bffad86f
JA
423 struct ioring_options *o = td->eo;
424 struct io_cq_ring *ring = &ld->cq_ring;
b87aa01a
JA
425 unsigned events = 0;
426 int r;
52885fa2 427
9a2d78b3 428 ld->cq_ring_off = *ring->head;
52885fa2 429 do {
bffad86f 430 r = fio_ioring_cqring_reap(td, events, max);
52885fa2
JA
431 if (r) {
432 events += r;
f7cbbbf8
ST
433 if (actual_min != 0)
434 actual_min -= r;
52885fa2
JA
435 continue;
436 }
437
3d7d00a3 438 if (!o->sqpoll_thread) {
9a2d78b3
JA
439 r = io_uring_enter(ld, 0, actual_min,
440 IORING_ENTER_GETEVENTS);
771c9901 441 if (r < 0) {
f6abd731 442 if (errno == EAGAIN || errno == EINTR)
771c9901 443 continue;
9a2d78b3 444 td_verror(td, errno, "io_uring_enter");
771c9901
JA
445 break;
446 }
52885fa2
JA
447 }
448 } while (events < min);
449
450 return r < 0 ? r : events;
451}
452
b2a432bf
PC
453static void fio_ioring_prio_prep(struct thread_data *td, struct io_u *io_u)
454{
455 struct ioring_options *o = td->eo;
456 struct ioring_data *ld = td->io_ops_data;
e9f6567a
DLM
457 struct io_uring_sqe *sqe = &ld->sqes[io_u->index];
458 struct cmdprio *cmdprio = &o->cmdprio;
12f9d54a 459 enum fio_ddir ddir = io_u->ddir;
a48f0cc7 460 unsigned int p = fio_cmdprio_percentage(cmdprio, io_u);
e9f6567a
DLM
461
462 if (p && rand_between(&td->prio_state, 0, 99) < p) {
12f9d54a
DLM
463 sqe->ioprio =
464 ioprio_value(cmdprio->class[ddir], cmdprio->level[ddir]);
b2a432bf 465 io_u->flags |= IO_U_F_PRIORITY;
ff9b6876 466 } else {
e9f6567a 467 sqe->ioprio = 0;
b2a432bf 468 }
b2a432bf
PC
469}
470
bffad86f
JA
471static enum fio_q_status fio_ioring_queue(struct thread_data *td,
472 struct io_u *io_u)
52885fa2 473{
bffad86f
JA
474 struct ioring_data *ld = td->io_ops_data;
475 struct io_sq_ring *ring = &ld->sq_ring;
52885fa2
JA
476 unsigned tail, next_tail;
477
478 fio_ro_check(td, io_u);
479
b87aa01a 480 if (ld->queued == ld->iodepth)
52885fa2
JA
481 return FIO_Q_BUSY;
482
52885fa2
JA
483 if (io_u->ddir == DDIR_TRIM) {
484 if (ld->queued)
485 return FIO_Q_BUSY;
486
487 do_io_u_trim(td, io_u);
488 io_u_mark_submit(td, 1);
489 io_u_mark_complete(td, 1);
490 return FIO_Q_COMPLETED;
491 }
492
9a2d78b3 493 tail = *ring->tail;
52885fa2 494 next_tail = tail + 1;
9e26aff9 495 if (next_tail == atomic_load_acquire(ring->head))
52885fa2
JA
496 return FIO_Q_BUSY;
497
e9f6567a 498 if (ld->use_cmdprio)
b2a432bf 499 fio_ioring_prio_prep(td, io_u);
b87aa01a 500 ring->array[tail & ld->sq_ring_mask] = io_u->index;
9e26aff9 501 atomic_store_release(ring->tail, next_tail);
52885fa2
JA
502
503 ld->queued++;
504 return FIO_Q_QUEUED;
505}
506
bffad86f 507static void fio_ioring_queued(struct thread_data *td, int start, int nr)
52885fa2 508{
bffad86f 509 struct ioring_data *ld = td->io_ops_data;
52885fa2
JA
510 struct timespec now;
511
512 if (!fio_fill_issue_time(td))
513 return;
514
515 fio_gettime(&now, NULL);
516
517 while (nr--) {
bffad86f 518 struct io_sq_ring *ring = &ld->sq_ring;
9a2d78b3 519 int index = ring->array[start & ld->sq_ring_mask];
f8289afc 520 struct io_u *io_u = ld->io_u_index[index];
52885fa2
JA
521
522 memcpy(&io_u->issue_time, &now, sizeof(now));
523 io_u_queued(td, io_u);
524
525 start++;
52885fa2
JA
526 }
527}
528
bffad86f 529static int fio_ioring_commit(struct thread_data *td)
52885fa2 530{
bffad86f
JA
531 struct ioring_data *ld = td->io_ops_data;
532 struct ioring_options *o = td->eo;
52885fa2
JA
533 int ret;
534
535 if (!ld->queued)
536 return 0;
537
3d7d00a3
JA
538 /*
539 * Kernel side does submission. just need to check if the ring is
540 * flagged as needing a kick, if so, call io_uring_enter(). This
541 * only happens if we've been idle too long.
542 */
543 if (o->sqpoll_thread) {
bffad86f 544 struct io_sq_ring *ring = &ld->sq_ring;
2dd96cc4 545 unsigned flags;
4cdbc048 546
2dd96cc4
JA
547 flags = atomic_load_acquire(ring->flags);
548 if (flags & IORING_SQ_NEED_WAKEUP)
b532dd6d
JA
549 io_uring_enter(ld, ld->queued, 0,
550 IORING_ENTER_SQ_WAKEUP);
771c9901
JA
551 ld->queued = 0;
552 return 0;
553 }
554
52885fa2 555 do {
9a2d78b3 556 unsigned start = *ld->sq_ring.head;
52885fa2
JA
557 long nr = ld->queued;
558
9a2d78b3 559 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
52885fa2 560 if (ret > 0) {
bffad86f 561 fio_ioring_queued(td, start, ret);
52885fa2
JA
562 io_u_mark_submit(td, ret);
563
564 ld->queued -= ret;
565 ret = 0;
a90cd050
JA
566 } else if (!ret) {
567 io_u_mark_submit(td, ret);
52885fa2 568 continue;
a90cd050 569 } else {
f6abd731 570 if (errno == EAGAIN || errno == EINTR) {
bffad86f 571 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
a90cd050
JA
572 if (ret)
573 continue;
574 /* Shouldn't happen */
575 usleep(1);
576 continue;
52885fa2 577 }
9a2d78b3 578 td_verror(td, errno, "io_uring_enter submit");
52885fa2 579 break;
a90cd050 580 }
52885fa2
JA
581 } while (ld->queued);
582
583 return ret;
584}
585
bffad86f 586static void fio_ioring_unmap(struct ioring_data *ld)
52885fa2 587{
9a2d78b3 588 int i;
52885fa2 589
59f94d26 590 for (i = 0; i < FIO_ARRAY_SIZE(ld->mmap); i++)
9a2d78b3
JA
591 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
592 close(ld->ring_fd);
b87aa01a
JA
593}
594
bffad86f 595static void fio_ioring_cleanup(struct thread_data *td)
52885fa2 596{
bffad86f 597 struct ioring_data *ld = td->io_ops_data;
52885fa2
JA
598
599 if (ld) {
52885fa2 600 if (!(td->flags & TD_F_CHILD))
bffad86f 601 fio_ioring_unmap(ld);
9a2d78b3 602
52885fa2 603 free(ld->io_u_index);
9a2d78b3 604 free(ld->iovecs);
5ffd5626 605 free(ld->fds);
52885fa2
JA
606 free(ld);
607 }
608}
609
bffad86f 610static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
9a2d78b3 611{
bffad86f
JA
612 struct io_sq_ring *sring = &ld->sq_ring;
613 struct io_cq_ring *cring = &ld->cq_ring;
9a2d78b3
JA
614 void *ptr;
615
e2239016 616 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
9a2d78b3
JA
617 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
618 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
619 IORING_OFF_SQ_RING);
620 ld->mmap[0].ptr = ptr;
621 sring->head = ptr + p->sq_off.head;
622 sring->tail = ptr + p->sq_off.tail;
623 sring->ring_mask = ptr + p->sq_off.ring_mask;
624 sring->ring_entries = ptr + p->sq_off.ring_entries;
625 sring->flags = ptr + p->sq_off.flags;
ac122fea 626 sring->array = ptr + p->sq_off.array;
9a2d78b3
JA
627 ld->sq_ring_mask = *sring->ring_mask;
628
f0403f94
JA
629 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
630 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
9a2d78b3 631 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
f0403f94
JA
632 IORING_OFF_SQES);
633 ld->mmap[1].ptr = ld->sqes;
9a2d78b3 634
f0403f94
JA
635 ld->mmap[2].len = p->cq_off.cqes +
636 p->cq_entries * sizeof(struct io_uring_cqe);
9a2d78b3
JA
637 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
638 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
639 IORING_OFF_CQ_RING);
640 ld->mmap[2].ptr = ptr;
641 cring->head = ptr + p->cq_off.head;
642 cring->tail = ptr + p->cq_off.tail;
643 cring->ring_mask = ptr + p->cq_off.ring_mask;
644 cring->ring_entries = ptr + p->cq_off.ring_entries;
f0403f94 645 cring->cqes = ptr + p->cq_off.cqes;
9a2d78b3
JA
646 ld->cq_ring_mask = *cring->ring_mask;
647 return 0;
648}
649
556d8415
JA
650static void fio_ioring_probe(struct thread_data *td)
651{
652 struct ioring_data *ld = td->io_ops_data;
653 struct ioring_options *o = td->eo;
654 struct io_uring_probe *p;
655 int ret;
656
657 /* already set by user, don't touch */
658 if (o->nonvectored != -1)
659 return;
660
661 /* default to off, as that's always safe */
662 o->nonvectored = 0;
663
664 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
665 if (!p)
666 return;
667
668 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
669 ret = syscall(__NR_io_uring_register, ld->ring_fd,
670 IORING_REGISTER_PROBE, p, 256);
671 if (ret < 0)
672 goto out;
673
674 if (IORING_OP_WRITE > p->ops_len)
675 goto out;
676
677 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED) &&
678 (p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED))
679 o->nonvectored = 1;
680out:
681 free(p);
682}
683
bffad86f 684static int fio_ioring_queue_init(struct thread_data *td)
52885fa2 685{
bffad86f
JA
686 struct ioring_data *ld = td->io_ops_data;
687 struct ioring_options *o = td->eo;
52885fa2 688 int depth = td->o.iodepth;
bffad86f 689 struct io_uring_params p;
9a2d78b3
JA
690 int ret;
691
692 memset(&p, 0, sizeof(p));
52885fa2
JA
693
694 if (o->hipri)
bffad86f 695 p.flags |= IORING_SETUP_IOPOLL;
3d7d00a3
JA
696 if (o->sqpoll_thread) {
697 p.flags |= IORING_SETUP_SQPOLL;
698 if (o->sqpoll_set) {
699 p.flags |= IORING_SETUP_SQ_AFF;
700 p.sq_thread_cpu = o->sqpoll_cpu;
701 }
f635f1fb 702 }
a90cd050 703
bfed648c 704 ret = syscall(__NR_io_uring_setup, depth, &p);
9a2d78b3
JA
705 if (ret < 0)
706 return ret;
707
708 ld->ring_fd = ret;
2ea53ca3 709
556d8415
JA
710 fio_ioring_probe(td);
711
2ea53ca3 712 if (o->fixedbufs) {
bfed648c 713 ret = syscall(__NR_io_uring_register, ld->ring_fd,
919850d2 714 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
2ea53ca3
JA
715 if (ret < 0)
716 return ret;
717 }
718
bffad86f 719 return fio_ioring_mmap(ld, &p);
52885fa2
JA
720}
721
5ffd5626
JA
722static int fio_ioring_register_files(struct thread_data *td)
723{
724 struct ioring_data *ld = td->io_ops_data;
725 struct fio_file *f;
726 unsigned int i;
727 int ret;
728
729 ld->fds = calloc(td->o.nr_files, sizeof(int));
730
731 for_each_file(td, f, i) {
732 ret = generic_open_file(td, f);
733 if (ret)
734 goto err;
735 ld->fds[i] = f->fd;
736 f->engine_pos = i;
737 }
738
bfed648c 739 ret = syscall(__NR_io_uring_register, ld->ring_fd,
5ffd5626
JA
740 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
741 if (ret) {
742err:
743 free(ld->fds);
744 ld->fds = NULL;
745 }
746
747 /*
748 * Pretend the file is closed again, and really close it if we hit
749 * an error.
750 */
751 for_each_file(td, f, i) {
752 if (ret) {
753 int fio_unused ret2;
754 ret2 = generic_close_file(td, f);
755 } else
756 f->fd = -1;
757 }
758
759 return ret;
760}
761
bffad86f 762static int fio_ioring_post_init(struct thread_data *td)
52885fa2 763{
bffad86f 764 struct ioring_data *ld = td->io_ops_data;
5ffd5626 765 struct ioring_options *o = td->eo;
52885fa2 766 struct io_u *io_u;
650346e1 767 int err, i;
52885fa2 768
650346e1
JA
769 for (i = 0; i < td->o.iodepth; i++) {
770 struct iovec *iov = &ld->iovecs[i];
9a2d78b3 771
650346e1
JA
772 io_u = ld->io_u_index[i];
773 iov->iov_base = io_u->buf;
774 iov->iov_len = td_max_bs(td);
52885fa2
JA
775 }
776
bffad86f 777 err = fio_ioring_queue_init(td);
52885fa2 778 if (err) {
0442b53f 779 int init_err = errno;
c4f5c92f 780
0442b53f 781 if (init_err == ENOSYS)
c4f5c92f 782 log_err("fio: your kernel doesn't support io_uring\n");
0442b53f 783 td_verror(td, init_err, "io_queue_init");
52885fa2
JA
784 return 1;
785 }
786
7c70f506
JA
787 for (i = 0; i < td->o.iodepth; i++) {
788 struct io_uring_sqe *sqe;
789
790 sqe = &ld->sqes[i];
791 memset(sqe, 0, sizeof(*sqe));
792 }
793
5ffd5626
JA
794 if (o->registerfiles) {
795 err = fio_ioring_register_files(td);
796 if (err) {
797 td_verror(td, errno, "ioring_register_files");
798 return 1;
799 }
800 }
801
52885fa2
JA
802 return 0;
803}
804
bffad86f 805static int fio_ioring_init(struct thread_data *td)
52885fa2 806{
5ffd5626 807 struct ioring_options *o = td->eo;
bffad86f 808 struct ioring_data *ld;
e9f6567a
DLM
809 struct cmdprio *cmdprio = &o->cmdprio;
810 int ret;
52885fa2 811
5ffd5626
JA
812 /* sqthread submission requires registered files */
813 if (o->sqpoll_thread)
814 o->registerfiles = 1;
815
816 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
817 log_err("fio: io_uring registered files require nr_files to "
818 "be identical to open_files\n");
819 return 1;
820 }
821
52885fa2
JA
822 ld = calloc(1, sizeof(*ld));
823
b87aa01a
JA
824 /* ring depth must be a power-of-2 */
825 ld->iodepth = td->o.iodepth;
826 td->o.iodepth = roundup_pow2(td->o.iodepth);
827
52885fa2
JA
828 /* io_u index */
829 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
650346e1 830 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
52885fa2
JA
831
832 td->io_ops_data = ld;
b2a432bf 833
e9f6567a
DLM
834 ret = fio_cmdprio_init(td, cmdprio, &ld->use_cmdprio);
835 if (ret) {
836 td_verror(td, EINVAL, "fio_ioring_init");
b2a432bf
PC
837 return 1;
838 }
1af44196
XW
839
840 if (fio_option_is_set(&td->o, ioprio_class))
841 ld->ioprio_class_set = true;
842 if (fio_option_is_set(&td->o, ioprio))
843 ld->ioprio_set = true;
844
52885fa2
JA
845 return 0;
846}
847
bffad86f 848static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
52885fa2 849{
bffad86f 850 struct ioring_data *ld = td->io_ops_data;
52885fa2
JA
851
852 ld->io_u_index[io_u->index] = io_u;
853 return 0;
854}
855
5ffd5626
JA
856static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
857{
858 struct ioring_data *ld = td->io_ops_data;
859 struct ioring_options *o = td->eo;
860
17318cf6 861 if (!ld || !o->registerfiles)
5ffd5626
JA
862 return generic_open_file(td, f);
863
864 f->fd = ld->fds[f->engine_pos];
865 return 0;
866}
867
868static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
869{
17318cf6 870 struct ioring_data *ld = td->io_ops_data;
5ffd5626
JA
871 struct ioring_options *o = td->eo;
872
17318cf6 873 if (!ld || !o->registerfiles)
5ffd5626
JA
874 return generic_close_file(td, f);
875
876 f->fd = -1;
877 return 0;
878}
879
52885fa2 880static struct ioengine_ops ioengine = {
bffad86f 881 .name = "io_uring",
52885fa2 882 .version = FIO_IOOPS_VERSION,
8bfe330e 883 .flags = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD,
bffad86f
JA
884 .init = fio_ioring_init,
885 .post_init = fio_ioring_post_init,
886 .io_u_init = fio_ioring_io_u_init,
887 .prep = fio_ioring_prep,
888 .queue = fio_ioring_queue,
889 .commit = fio_ioring_commit,
890 .getevents = fio_ioring_getevents,
891 .event = fio_ioring_event,
892 .cleanup = fio_ioring_cleanup,
5ffd5626
JA
893 .open_file = fio_ioring_open_file,
894 .close_file = fio_ioring_close_file,
52885fa2
JA
895 .get_file_size = generic_get_file_size,
896 .options = options,
bffad86f 897 .option_struct_size = sizeof(struct ioring_options),
52885fa2
JA
898};
899
bffad86f 900static void fio_init fio_ioring_register(void)
52885fa2 901{
52885fa2 902 register_ioengine(&ioengine);
52885fa2
JA
903}
904
bffad86f 905static void fio_exit fio_ioring_unregister(void)
52885fa2 906{
52885fa2 907 unregister_ioengine(&ioengine);
52885fa2 908}
1f90e9bb 909#endif