t/nvmept_pi: test script for protection information
[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"
16be6037 27#include "zbd.h"
855dc4d4
AG
28#include "nvme.h"
29
30#include <sys/stat.h>
31
32enum uring_cmd_type {
33 FIO_URING_CMD_NVME = 1,
34};
9a2d78b3 35
bffad86f 36struct io_sq_ring {
e2239016
JA
37 unsigned *head;
38 unsigned *tail;
39 unsigned *ring_mask;
40 unsigned *ring_entries;
41 unsigned *flags;
42 unsigned *array;
52885fa2
JA
43};
44
bffad86f 45struct io_cq_ring {
e2239016
JA
46 unsigned *head;
47 unsigned *tail;
48 unsigned *ring_mask;
49 unsigned *ring_entries;
f0403f94 50 struct io_uring_cqe *cqes;
9a2d78b3
JA
51};
52
bffad86f 53struct ioring_mmap {
9a2d78b3
JA
54 void *ptr;
55 size_t len;
52885fa2
JA
56};
57
bffad86f 58struct ioring_data {
9a2d78b3
JA
59 int ring_fd;
60
52885fa2 61 struct io_u **io_u_index;
2d6451c9 62 char *md_buf;
52885fa2 63
5ffd5626
JA
64 int *fds;
65
bffad86f 66 struct io_sq_ring sq_ring;
f0403f94 67 struct io_uring_sqe *sqes;
9a2d78b3 68 struct iovec *iovecs;
b87aa01a 69 unsigned sq_ring_mask;
52885fa2 70
bffad86f 71 struct io_cq_ring cq_ring;
b87aa01a 72 unsigned cq_ring_mask;
52885fa2
JA
73
74 int queued;
75 int cq_ring_off;
b87aa01a 76 unsigned iodepth;
5a59a81d 77 int prepped;
96563db9 78
bffad86f 79 struct ioring_mmap mmap[3];
d6cbeab4
NC
80
81 struct cmdprio cmdprio;
4885a6eb
VF
82
83 struct nvme_dsm_range *dsm;
52885fa2
JA
84};
85
bffad86f 86struct ioring_options {
a48f0cc7 87 struct thread_data *td;
52885fa2 88 unsigned int hipri;
d6cbeab4 89 struct cmdprio_options cmdprio_options;
52885fa2 90 unsigned int fixedbufs;
5ffd5626 91 unsigned int registerfiles;
3d7d00a3 92 unsigned int sqpoll_thread;
2ea53ca3
JA
93 unsigned int sqpoll_set;
94 unsigned int sqpoll_cpu;
b10b1e70 95 unsigned int nonvectored;
4a87b584 96 unsigned int uncached;
7d42e66e 97 unsigned int nowait;
5a59a81d 98 unsigned int force_async;
2d6451c9 99 unsigned int md_per_io_size;
3ee8311a
AK
100 unsigned int pi_act;
101 unsigned int apptag;
102 unsigned int apptag_mask;
103 unsigned int prchk;
104 char *pi_chk;
855dc4d4 105 enum uring_cmd_type cmd_type;
52885fa2
JA
106};
107
b10b1e70
JA
108static const int ddir_to_op[2][2] = {
109 { IORING_OP_READV, IORING_OP_READ },
110 { IORING_OP_WRITEV, IORING_OP_WRITE }
111};
112
3f1e3af7
KB
113static const int fixed_ddir_to_op[2] = {
114 IORING_OP_READ_FIXED,
115 IORING_OP_WRITE_FIXED
116};
117
2ea53ca3 118static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
a90cd050 119{
bffad86f 120 struct ioring_options *o = data;
a90cd050 121
2ea53ca3
JA
122 o->sqpoll_cpu = *val;
123 o->sqpoll_set = 1;
a90cd050
JA
124 return 0;
125}
126
52885fa2
JA
127static struct fio_option options[] = {
128 {
129 .name = "hipri",
130 .lname = "High Priority",
131 .type = FIO_OPT_STR_SET,
bffad86f 132 .off1 = offsetof(struct ioring_options, hipri),
52885fa2
JA
133 .help = "Use polled IO completions",
134 .category = FIO_OPT_C_ENGINE,
27f436d9 135 .group = FIO_OPT_G_IOURING,
52885fa2
JA
136 },
137 {
138 .name = "fixedbufs",
139 .lname = "Fixed (pre-mapped) IO buffers",
140 .type = FIO_OPT_STR_SET,
bffad86f 141 .off1 = offsetof(struct ioring_options, fixedbufs),
52885fa2
JA
142 .help = "Pre map IO buffers",
143 .category = FIO_OPT_C_ENGINE,
27f436d9 144 .group = FIO_OPT_G_IOURING,
52885fa2 145 },
5ffd5626
JA
146 {
147 .name = "registerfiles",
148 .lname = "Register file set",
149 .type = FIO_OPT_STR_SET,
150 .off1 = offsetof(struct ioring_options, registerfiles),
151 .help = "Pre-open/register files",
152 .category = FIO_OPT_C_ENGINE,
27f436d9 153 .group = FIO_OPT_G_IOURING,
5ffd5626 154 },
771c9901
JA
155 {
156 .name = "sqthread_poll",
3d7d00a3 157 .lname = "Kernel SQ thread polling",
d6f936d1 158 .type = FIO_OPT_STR_SET,
3d7d00a3
JA
159 .off1 = offsetof(struct ioring_options, sqpoll_thread),
160 .help = "Offload submission/completion to kernel thread",
161 .category = FIO_OPT_C_ENGINE,
27f436d9 162 .group = FIO_OPT_G_IOURING,
3d7d00a3
JA
163 },
164 {
165 .name = "sqthread_poll_cpu",
166 .lname = "SQ Thread Poll CPU",
2ea53ca3
JA
167 .type = FIO_OPT_INT,
168 .cb = fio_ioring_sqpoll_cb,
3d7d00a3 169 .help = "What CPU to run SQ thread polling on",
a90cd050 170 .category = FIO_OPT_C_ENGINE,
27f436d9 171 .group = FIO_OPT_G_IOURING,
a90cd050 172 },
b10b1e70
JA
173 {
174 .name = "nonvectored",
175 .lname = "Non-vectored",
176 .type = FIO_OPT_INT,
177 .off1 = offsetof(struct ioring_options, nonvectored),
556d8415 178 .def = "-1",
b10b1e70
JA
179 .help = "Use non-vectored read/write commands",
180 .category = FIO_OPT_C_ENGINE,
181 .group = FIO_OPT_G_IOURING,
182 },
4a87b584
JA
183 {
184 .name = "uncached",
185 .lname = "Uncached",
186 .type = FIO_OPT_INT,
187 .off1 = offsetof(struct ioring_options, uncached),
188 .help = "Use RWF_UNCACHED for buffered read/writes",
189 .category = FIO_OPT_C_ENGINE,
190 .group = FIO_OPT_G_IOURING,
191 },
7d42e66e
KK
192 {
193 .name = "nowait",
194 .lname = "RWF_NOWAIT",
195 .type = FIO_OPT_BOOL,
196 .off1 = offsetof(struct ioring_options, nowait),
197 .help = "Use RWF_NOWAIT for reads/writes",
198 .category = FIO_OPT_C_ENGINE,
199 .group = FIO_OPT_G_IOURING,
200 },
5a59a81d
JA
201 {
202 .name = "force_async",
203 .lname = "Force async",
204 .type = FIO_OPT_INT,
205 .off1 = offsetof(struct ioring_options, force_async),
206 .help = "Set IOSQE_ASYNC every N requests",
207 .category = FIO_OPT_C_ENGINE,
208 .group = FIO_OPT_G_IOURING,
209 },
855dc4d4
AG
210 {
211 .name = "cmd_type",
212 .lname = "Uring cmd type",
213 .type = FIO_OPT_STR,
214 .off1 = offsetof(struct ioring_options, cmd_type),
215 .help = "Specify uring-cmd type",
216 .def = "nvme",
217 .posval = {
218 { .ival = "nvme",
219 .oval = FIO_URING_CMD_NVME,
220 .help = "Issue nvme-uring-cmd",
221 },
222 },
223 .category = FIO_OPT_C_ENGINE,
224 .group = FIO_OPT_G_IOURING,
225 },
2838f77a 226 CMDPRIO_OPTIONS(struct ioring_options, FIO_OPT_G_IOURING),
2d6451c9
AK
227 {
228 .name = "md_per_io_size",
229 .lname = "Separate Metadata Buffer Size per I/O",
230 .type = FIO_OPT_INT,
231 .off1 = offsetof(struct ioring_options, md_per_io_size),
232 .def = "0",
233 .help = "Size of separate metadata buffer per I/O (Default: 0)",
234 .category = FIO_OPT_C_ENGINE,
235 .group = FIO_OPT_G_IOURING,
236 },
3ee8311a
AK
237 {
238 .name = "pi_act",
239 .lname = "Protection Information Action",
240 .type = FIO_OPT_BOOL,
241 .off1 = offsetof(struct ioring_options, pi_act),
242 .def = "1",
243 .help = "Protection Information Action bit (pi_act=1 or pi_act=0)",
244 .category = FIO_OPT_C_ENGINE,
245 .group = FIO_OPT_G_IOURING,
246 },
247 {
248 .name = "pi_chk",
249 .lname = "Protection Information Check",
250 .type = FIO_OPT_STR_STORE,
251 .off1 = offsetof(struct ioring_options, pi_chk),
252 .def = NULL,
253 .help = "Control of Protection Information Checking (pi_chk=GUARD,REFTAG,APPTAG)",
254 .category = FIO_OPT_C_ENGINE,
255 .group = FIO_OPT_G_IOURING,
256 },
257 {
258 .name = "apptag",
259 .lname = "Application Tag used in Protection Information",
260 .type = FIO_OPT_INT,
261 .off1 = offsetof(struct ioring_options, apptag),
262 .def = "0x1234",
263 .help = "Application Tag used in Protection Information field (Default: 0x1234)",
264 .category = FIO_OPT_C_ENGINE,
265 .group = FIO_OPT_G_IOURING,
266 },
267 {
268 .name = "apptag_mask",
269 .lname = "Application Tag Mask",
270 .type = FIO_OPT_INT,
271 .off1 = offsetof(struct ioring_options, apptag_mask),
272 .def = "0xffff",
273 .help = "Application Tag Mask used with Application Tag (Default: 0xffff)",
274 .category = FIO_OPT_C_ENGINE,
275 .group = FIO_OPT_G_IOURING,
276 },
52885fa2
JA
277 {
278 .name = NULL,
279 },
280};
281
bffad86f 282static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
52885fa2
JA
283 unsigned int min_complete, unsigned int flags)
284{
c377f4f8
JA
285#ifdef FIO_ARCH_HAS_SYSCALL
286 return __do_syscall6(__NR_io_uring_enter, ld->ring_fd, to_submit,
287 min_complete, flags, NULL, 0);
288#else
bfed648c 289 return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit,
521164fa 290 min_complete, flags, NULL, 0);
c377f4f8 291#endif
52885fa2
JA
292}
293
bffad86f 294static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
52885fa2 295{
bffad86f 296 struct ioring_data *ld = td->io_ops_data;
cfcc8564 297 struct ioring_options *o = td->eo;
52885fa2 298 struct fio_file *f = io_u->file;
f0403f94 299 struct io_uring_sqe *sqe;
52885fa2 300
f0403f94 301 sqe = &ld->sqes[io_u->index];
34d6090e 302
5ffd5626
JA
303 if (o->registerfiles) {
304 sqe->fd = f->engine_pos;
305 sqe->flags = IOSQE_FIXED_FILE;
306 } else {
307 sqe->fd = f->fd;
87b69ef2 308 sqe->flags = 0;
5ffd5626 309 }
52885fa2 310
e3970057 311 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
f0403f94 312 if (o->fixedbufs) {
3f1e3af7 313 sqe->opcode = fixed_ddir_to_op[io_u->ddir];
919850d2 314 sqe->addr = (unsigned long) io_u->xfer_buf;
f0403f94 315 sqe->len = io_u->xfer_buflen;
2ea53ca3 316 sqe->buf_index = io_u->index;
cfcc8564 317 } else {
832faaaf
JA
318 struct iovec *iov = &ld->iovecs[io_u->index];
319
320 /*
321 * Update based on actual io_u, requeue could have
322 * adjusted these
323 */
324 iov->iov_base = io_u->xfer_buf;
325 iov->iov_len = io_u->xfer_buflen;
326
3f1e3af7 327 sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
b10b1e70 328 if (o->nonvectored) {
832faaaf
JA
329 sqe->addr = (unsigned long) iov->iov_base;
330 sqe->len = iov->iov_len;
b10b1e70 331 } else {
832faaaf 332 sqe->addr = (unsigned long) iov;
b10b1e70
JA
333 sqe->len = 1;
334 }
cfcc8564 335 }
fd70e361 336 sqe->rw_flags = 0;
4a87b584 337 if (!td->o.odirect && o->uncached)
fd70e361 338 sqe->rw_flags |= RWF_UNCACHED;
7d42e66e
KK
339 if (o->nowait)
340 sqe->rw_flags |= RWF_NOWAIT;
8ff6b289
NC
341
342 /*
343 * Since io_uring can have a submission context (sqthread_poll)
344 * that is different from the process context, we cannot rely on
79012fec
DLM
345 * the IO priority set by ioprio_set() (options prio, prioclass,
346 * and priohint) to be inherited.
8ff6b289
NC
347 * td->ioprio will have the value of the "default prio", so set
348 * this unconditionally. This value might get overridden by
ff00f247 349 * fio_ioring_cmdprio_prep() if the option cmdprio_percentage or
8ff6b289
NC
350 * cmdprio_bssplit is used.
351 */
352 sqe->ioprio = td->ioprio;
f0403f94 353 sqe->off = io_u->offset;
48e698fa 354 } else if (ddir_sync(io_u->ddir)) {
7c70f506 355 sqe->ioprio = 0;
01387bfe
AF
356 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
357 sqe->off = f->first_write;
358 sqe->len = f->last_write - f->first_write;
359 sqe->sync_range_flags = td->o.sync_file_range;
360 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
361 } else {
7c70f506
JA
362 sqe->off = 0;
363 sqe->addr = 0;
364 sqe->len = 0;
01387bfe
AF
365 if (io_u->ddir == DDIR_DATASYNC)
366 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
367 sqe->opcode = IORING_OP_FSYNC;
368 }
48e698fa 369 }
52885fa2 370
5a59a81d
JA
371 if (o->force_async && ++ld->prepped == o->force_async) {
372 ld->prepped = 0;
373 sqe->flags |= IOSQE_ASYNC;
374 }
375
48e698fa 376 sqe->user_data = (unsigned long) io_u;
52885fa2
JA
377 return 0;
378}
379
855dc4d4
AG
380static int fio_ioring_cmd_prep(struct thread_data *td, struct io_u *io_u)
381{
382 struct ioring_data *ld = td->io_ops_data;
383 struct ioring_options *o = td->eo;
384 struct fio_file *f = io_u->file;
3ce6a3de 385 struct nvme_uring_cmd *cmd;
855dc4d4 386 struct io_uring_sqe *sqe;
855dc4d4 387
3ce6a3de
JA
388 /* only supports nvme_uring_cmd */
389 if (o->cmd_type != FIO_URING_CMD_NVME)
390 return -EINVAL;
855dc4d4 391
4885a6eb 392 if (io_u->ddir == DDIR_TRIM && td->io_ops->flags & FIO_ASYNCIO_SYNC_TRIM)
16be6037
AK
393 return 0;
394
3ce6a3de 395 sqe = &ld->sqes[(io_u->index) << 1];
855dc4d4 396
3ce6a3de
JA
397 if (o->registerfiles) {
398 sqe->fd = f->engine_pos;
399 sqe->flags = IOSQE_FIXED_FILE;
400 } else {
401 sqe->fd = f->fd;
402 }
403 sqe->rw_flags = 0;
404 if (!td->o.odirect && o->uncached)
405 sqe->rw_flags |= RWF_UNCACHED;
406 if (o->nowait)
407 sqe->rw_flags |= RWF_NOWAIT;
855dc4d4 408
3ce6a3de
JA
409 sqe->opcode = IORING_OP_URING_CMD;
410 sqe->user_data = (unsigned long) io_u;
411 if (o->nonvectored)
412 sqe->cmd_op = NVME_URING_CMD_IO;
413 else
414 sqe->cmd_op = NVME_URING_CMD_IO_VEC;
415 if (o->force_async && ++ld->prepped == o->force_async) {
416 ld->prepped = 0;
417 sqe->flags |= IOSQE_ASYNC;
855dc4d4 418 }
0ebd3bf6
AG
419 if (o->fixedbufs) {
420 sqe->uring_cmd_flags = IORING_URING_CMD_FIXED;
421 sqe->buf_index = io_u->index;
422 }
3ce6a3de
JA
423
424 cmd = (struct nvme_uring_cmd *)sqe->cmd;
425 return fio_nvme_uring_cmd_prep(cmd, io_u,
4885a6eb
VF
426 o->nonvectored ? NULL : &ld->iovecs[io_u->index],
427 &ld->dsm[io_u->index]);
855dc4d4
AG
428}
429
bffad86f 430static struct io_u *fio_ioring_event(struct thread_data *td, int event)
52885fa2 431{
bffad86f 432 struct ioring_data *ld = td->io_ops_data;
f0403f94 433 struct io_uring_cqe *cqe;
52885fa2 434 struct io_u *io_u;
b87aa01a 435 unsigned index;
52885fa2 436
b87aa01a 437 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
52885fa2 438
f0403f94 439 cqe = &ld->cq_ring.cqes[index];
e3466352 440 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
52885fa2 441
f0403f94
JA
442 if (cqe->res != io_u->xfer_buflen) {
443 if (cqe->res > io_u->xfer_buflen)
444 io_u->error = -cqe->res;
52885fa2 445 else
f0403f94 446 io_u->resid = io_u->xfer_buflen - cqe->res;
52885fa2
JA
447 } else
448 io_u->error = 0;
449
450 return io_u;
451}
452
855dc4d4
AG
453static struct io_u *fio_ioring_cmd_event(struct thread_data *td, int event)
454{
455 struct ioring_data *ld = td->io_ops_data;
456 struct ioring_options *o = td->eo;
457 struct io_uring_cqe *cqe;
458 struct io_u *io_u;
5163f35e 459 struct nvme_data *data;
855dc4d4 460 unsigned index;
5163f35e 461 int ret;
855dc4d4
AG
462
463 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
464 if (o->cmd_type == FIO_URING_CMD_NVME)
465 index <<= 1;
466
467 cqe = &ld->cq_ring.cqes[index];
468 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
469
470 if (cqe->res != 0)
471 io_u->error = -cqe->res;
472 else
473 io_u->error = 0;
474
5163f35e
AK
475 if (o->cmd_type == FIO_URING_CMD_NVME) {
476 data = FILE_ENG_DATA(io_u->file);
477 if (data->pi_type && (io_u->ddir == DDIR_READ) && !o->pi_act) {
478 ret = fio_nvme_pi_verify(data, io_u);
479 if (ret)
480 io_u->error = ret;
481 }
482 }
483
855dc4d4
AG
484 return io_u;
485}
486
bffad86f 487static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
52885fa2
JA
488 unsigned int max)
489{
bffad86f
JA
490 struct ioring_data *ld = td->io_ops_data;
491 struct io_cq_ring *ring = &ld->cq_ring;
e2239016 492 unsigned head, reaped = 0;
52885fa2 493
9a2d78b3 494 head = *ring->head;
52885fa2 495 do {
9e26aff9 496 if (head == atomic_load_acquire(ring->tail))
52885fa2
JA
497 break;
498 reaped++;
499 head++;
52885fa2
JA
500 } while (reaped + events < max);
501
76ce63dd
AB
502 if (reaped)
503 atomic_store_release(ring->head, head);
504
52885fa2
JA
505 return reaped;
506}
507
bffad86f
JA
508static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
509 unsigned int max, const struct timespec *t)
52885fa2 510{
bffad86f 511 struct ioring_data *ld = td->io_ops_data;
52885fa2 512 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
bffad86f
JA
513 struct ioring_options *o = td->eo;
514 struct io_cq_ring *ring = &ld->cq_ring;
b87aa01a
JA
515 unsigned events = 0;
516 int r;
52885fa2 517
9a2d78b3 518 ld->cq_ring_off = *ring->head;
52885fa2 519 do {
bffad86f 520 r = fio_ioring_cqring_reap(td, events, max);
52885fa2
JA
521 if (r) {
522 events += r;
ae8646a1 523 max -= r;
f7cbbbf8
ST
524 if (actual_min != 0)
525 actual_min -= r;
52885fa2
JA
526 continue;
527 }
528
3d7d00a3 529 if (!o->sqpoll_thread) {
9a2d78b3
JA
530 r = io_uring_enter(ld, 0, actual_min,
531 IORING_ENTER_GETEVENTS);
771c9901 532 if (r < 0) {
f6abd731 533 if (errno == EAGAIN || errno == EINTR)
771c9901 534 continue;
1816895b 535 r = -errno;
9a2d78b3 536 td_verror(td, errno, "io_uring_enter");
771c9901
JA
537 break;
538 }
52885fa2
JA
539 }
540 } while (events < min);
541
542 return r < 0 ? r : events;
543}
544
3ee8311a
AK
545static inline void fio_ioring_cmd_nvme_pi(struct thread_data *td,
546 struct io_u *io_u)
547{
548 struct ioring_data *ld = td->io_ops_data;
549 struct ioring_options *o = td->eo;
550 struct nvme_uring_cmd *cmd;
551 struct io_uring_sqe *sqe;
552 struct nvme_cmd_ext_io_opts ext_opts = {0};
553 struct nvme_data *data = FILE_ENG_DATA(io_u->file);
554
555 if (io_u->ddir == DDIR_TRIM)
556 return;
557
558 sqe = &ld->sqes[(io_u->index) << 1];
559 cmd = (struct nvme_uring_cmd *)sqe->cmd;
560
561 if (data->pi_type) {
562 if (o->pi_act)
563 ext_opts.io_flags |= NVME_IO_PRINFO_PRACT;
564 ext_opts.io_flags |= o->prchk;
565 ext_opts.apptag = o->apptag;
566 ext_opts.apptag_mask = o->apptag_mask;
567 }
568
569 fio_nvme_pi_fill(cmd, io_u, &ext_opts);
570}
571
127715b6
NC
572static inline void fio_ioring_cmdprio_prep(struct thread_data *td,
573 struct io_u *io_u)
b2a432bf 574{
b2a432bf 575 struct ioring_data *ld = td->io_ops_data;
d6cbeab4 576 struct cmdprio *cmdprio = &ld->cmdprio;
127715b6
NC
577
578 if (fio_cmdprio_set_ioprio(td, cmdprio, io_u))
579 ld->sqes[io_u->index].ioprio = io_u->ioprio;
b2a432bf
PC
580}
581
bffad86f
JA
582static enum fio_q_status fio_ioring_queue(struct thread_data *td,
583 struct io_u *io_u)
52885fa2 584{
bffad86f 585 struct ioring_data *ld = td->io_ops_data;
3ee8311a 586 struct ioring_options *o = td->eo;
bffad86f 587 struct io_sq_ring *ring = &ld->sq_ring;
52885fa2
JA
588 unsigned tail, next_tail;
589
590 fio_ro_check(td, io_u);
591
b87aa01a 592 if (ld->queued == ld->iodepth)
52885fa2
JA
593 return FIO_Q_BUSY;
594
4885a6eb 595 if (io_u->ddir == DDIR_TRIM && td->io_ops->flags & FIO_ASYNCIO_SYNC_TRIM) {
52885fa2
JA
596 if (ld->queued)
597 return FIO_Q_BUSY;
598
7f57e30f 599 do_io_u_trim(td, io_u);
16be6037 600
52885fa2
JA
601 io_u_mark_submit(td, 1);
602 io_u_mark_complete(td, 1);
603 return FIO_Q_COMPLETED;
604 }
605
9a2d78b3 606 tail = *ring->tail;
52885fa2 607 next_tail = tail + 1;
5c15a911 608 if (next_tail == atomic_load_relaxed(ring->head))
52885fa2
JA
609 return FIO_Q_BUSY;
610
d6cbeab4 611 if (ld->cmdprio.mode != CMDPRIO_MODE_NONE)
ff00f247
NC
612 fio_ioring_cmdprio_prep(td, io_u);
613
3ee8311a
AK
614 if (!strcmp(td->io_ops->name, "io_uring_cmd") &&
615 o->cmd_type == FIO_URING_CMD_NVME)
616 fio_ioring_cmd_nvme_pi(td, io_u);
617
b87aa01a 618 ring->array[tail & ld->sq_ring_mask] = io_u->index;
9e26aff9 619 atomic_store_release(ring->tail, next_tail);
52885fa2
JA
620
621 ld->queued++;
622 return FIO_Q_QUEUED;
623}
624
bffad86f 625static void fio_ioring_queued(struct thread_data *td, int start, int nr)
52885fa2 626{
bffad86f 627 struct ioring_data *ld = td->io_ops_data;
52885fa2
JA
628 struct timespec now;
629
630 if (!fio_fill_issue_time(td))
631 return;
632
633 fio_gettime(&now, NULL);
634
635 while (nr--) {
bffad86f 636 struct io_sq_ring *ring = &ld->sq_ring;
9a2d78b3 637 int index = ring->array[start & ld->sq_ring_mask];
f8289afc 638 struct io_u *io_u = ld->io_u_index[index];
52885fa2
JA
639
640 memcpy(&io_u->issue_time, &now, sizeof(now));
641 io_u_queued(td, io_u);
642
643 start++;
52885fa2 644 }
39f56400
VF
645
646 /*
647 * only used for iolog
648 */
649 if (td->o.read_iolog_file)
650 memcpy(&td->last_issue, &now, sizeof(now));
52885fa2
JA
651}
652
bffad86f 653static int fio_ioring_commit(struct thread_data *td)
52885fa2 654{
bffad86f
JA
655 struct ioring_data *ld = td->io_ops_data;
656 struct ioring_options *o = td->eo;
52885fa2
JA
657 int ret;
658
659 if (!ld->queued)
660 return 0;
661
3d7d00a3
JA
662 /*
663 * Kernel side does submission. just need to check if the ring is
664 * flagged as needing a kick, if so, call io_uring_enter(). This
665 * only happens if we've been idle too long.
666 */
667 if (o->sqpoll_thread) {
bffad86f 668 struct io_sq_ring *ring = &ld->sq_ring;
10bad6b9 669 unsigned start = *ld->sq_ring.tail - ld->queued;
2dd96cc4 670 unsigned flags;
4cdbc048 671
5c15a911 672 flags = atomic_load_relaxed(ring->flags);
2dd96cc4 673 if (flags & IORING_SQ_NEED_WAKEUP)
b532dd6d
JA
674 io_uring_enter(ld, ld->queued, 0,
675 IORING_ENTER_SQ_WAKEUP);
c011bf12
AK
676 fio_ioring_queued(td, start, ld->queued);
677 io_u_mark_submit(td, ld->queued);
678
771c9901
JA
679 ld->queued = 0;
680 return 0;
681 }
682
52885fa2 683 do {
9a2d78b3 684 unsigned start = *ld->sq_ring.head;
52885fa2
JA
685 long nr = ld->queued;
686
9a2d78b3 687 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
52885fa2 688 if (ret > 0) {
bffad86f 689 fio_ioring_queued(td, start, ret);
52885fa2
JA
690 io_u_mark_submit(td, ret);
691
692 ld->queued -= ret;
693 ret = 0;
a90cd050
JA
694 } else if (!ret) {
695 io_u_mark_submit(td, ret);
52885fa2 696 continue;
a90cd050 697 } else {
f6abd731 698 if (errno == EAGAIN || errno == EINTR) {
bffad86f 699 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
a90cd050
JA
700 if (ret)
701 continue;
702 /* Shouldn't happen */
703 usleep(1);
704 continue;
52885fa2 705 }
1816895b 706 ret = -errno;
9a2d78b3 707 td_verror(td, errno, "io_uring_enter submit");
52885fa2 708 break;
a90cd050 709 }
52885fa2
JA
710 } while (ld->queued);
711
712 return ret;
713}
714
bffad86f 715static void fio_ioring_unmap(struct ioring_data *ld)
52885fa2 716{
9a2d78b3 717 int i;
52885fa2 718
59f94d26 719 for (i = 0; i < FIO_ARRAY_SIZE(ld->mmap); i++)
9a2d78b3
JA
720 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
721 close(ld->ring_fd);
b87aa01a
JA
722}
723
bffad86f 724static void fio_ioring_cleanup(struct thread_data *td)
52885fa2 725{
bffad86f 726 struct ioring_data *ld = td->io_ops_data;
52885fa2
JA
727
728 if (ld) {
52885fa2 729 if (!(td->flags & TD_F_CHILD))
bffad86f 730 fio_ioring_unmap(ld);
9a2d78b3 731
d6cbeab4 732 fio_cmdprio_cleanup(&ld->cmdprio);
52885fa2 733 free(ld->io_u_index);
2d6451c9 734 free(ld->md_buf);
9a2d78b3 735 free(ld->iovecs);
5ffd5626 736 free(ld->fds);
4885a6eb 737 free(ld->dsm);
52885fa2
JA
738 free(ld);
739 }
740}
741
bffad86f 742static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
9a2d78b3 743{
bffad86f
JA
744 struct io_sq_ring *sring = &ld->sq_ring;
745 struct io_cq_ring *cring = &ld->cq_ring;
9a2d78b3
JA
746 void *ptr;
747
e2239016 748 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
9a2d78b3
JA
749 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
750 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
751 IORING_OFF_SQ_RING);
752 ld->mmap[0].ptr = ptr;
753 sring->head = ptr + p->sq_off.head;
754 sring->tail = ptr + p->sq_off.tail;
755 sring->ring_mask = ptr + p->sq_off.ring_mask;
756 sring->ring_entries = ptr + p->sq_off.ring_entries;
757 sring->flags = ptr + p->sq_off.flags;
ac122fea 758 sring->array = ptr + p->sq_off.array;
9a2d78b3
JA
759 ld->sq_ring_mask = *sring->ring_mask;
760
855dc4d4
AG
761 if (p->flags & IORING_SETUP_SQE128)
762 ld->mmap[1].len = 2 * p->sq_entries * sizeof(struct io_uring_sqe);
763 else
764 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
f0403f94 765 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
9a2d78b3 766 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
f0403f94
JA
767 IORING_OFF_SQES);
768 ld->mmap[1].ptr = ld->sqes;
9a2d78b3 769
855dc4d4
AG
770 if (p->flags & IORING_SETUP_CQE32) {
771 ld->mmap[2].len = p->cq_off.cqes +
772 2 * p->cq_entries * sizeof(struct io_uring_cqe);
773 } else {
774 ld->mmap[2].len = p->cq_off.cqes +
775 p->cq_entries * sizeof(struct io_uring_cqe);
776 }
9a2d78b3
JA
777 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
778 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
779 IORING_OFF_CQ_RING);
780 ld->mmap[2].ptr = ptr;
781 cring->head = ptr + p->cq_off.head;
782 cring->tail = ptr + p->cq_off.tail;
783 cring->ring_mask = ptr + p->cq_off.ring_mask;
784 cring->ring_entries = ptr + p->cq_off.ring_entries;
f0403f94 785 cring->cqes = ptr + p->cq_off.cqes;
9a2d78b3
JA
786 ld->cq_ring_mask = *cring->ring_mask;
787 return 0;
788}
789
556d8415
JA
790static void fio_ioring_probe(struct thread_data *td)
791{
792 struct ioring_data *ld = td->io_ops_data;
793 struct ioring_options *o = td->eo;
794 struct io_uring_probe *p;
795 int ret;
796
797 /* already set by user, don't touch */
798 if (o->nonvectored != -1)
799 return;
800
801 /* default to off, as that's always safe */
802 o->nonvectored = 0;
803
223decdd 804 p = calloc(1, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
556d8415
JA
805 if (!p)
806 return;
807
556d8415
JA
808 ret = syscall(__NR_io_uring_register, ld->ring_fd,
809 IORING_REGISTER_PROBE, p, 256);
810 if (ret < 0)
811 goto out;
812
813 if (IORING_OP_WRITE > p->ops_len)
814 goto out;
815
816 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED) &&
817 (p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED))
818 o->nonvectored = 1;
819out:
820 free(p);
821}
822
bffad86f 823static int fio_ioring_queue_init(struct thread_data *td)
52885fa2 824{
bffad86f
JA
825 struct ioring_data *ld = td->io_ops_data;
826 struct ioring_options *o = td->eo;
52885fa2 827 int depth = td->o.iodepth;
bffad86f 828 struct io_uring_params p;
9a2d78b3
JA
829 int ret;
830
831 memset(&p, 0, sizeof(p));
52885fa2
JA
832
833 if (o->hipri)
bffad86f 834 p.flags |= IORING_SETUP_IOPOLL;
3d7d00a3
JA
835 if (o->sqpoll_thread) {
836 p.flags |= IORING_SETUP_SQPOLL;
837 if (o->sqpoll_set) {
838 p.flags |= IORING_SETUP_SQ_AFF;
839 p.sq_thread_cpu = o->sqpoll_cpu;
840 }
c011bf12
AK
841
842 /*
843 * Submission latency for sqpoll_thread is just the time it
844 * takes to fill in the SQ ring entries, and any syscall if
845 * IORING_SQ_NEED_WAKEUP is set, we don't need to log that time
846 * separately.
847 */
848 td->o.disable_slat = 1;
f635f1fb 849 }
a90cd050 850
1db268db
JA
851 /*
852 * Clamp CQ ring size at our SQ ring size, we don't need more entries
853 * than that.
854 */
855 p.flags |= IORING_SETUP_CQSIZE;
856 p.cq_entries = depth;
857
4d22c103
JA
858 /*
859 * Setup COOP_TASKRUN as we don't need to get IPI interrupted for
860 * completing IO operations.
861 */
862 p.flags |= IORING_SETUP_COOP_TASKRUN;
863
e453f369
JA
864 /*
865 * io_uring is always a single issuer, and we can defer task_work
866 * runs until we reap events.
867 */
868 p.flags |= IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN;
869
b5e99df6 870retry:
bfed648c 871 ret = syscall(__NR_io_uring_setup, depth, &p);
b5e99df6 872 if (ret < 0) {
e453f369
JA
873 if (errno == EINVAL && p.flags & IORING_SETUP_DEFER_TASKRUN) {
874 p.flags &= ~IORING_SETUP_DEFER_TASKRUN;
875 p.flags &= ~IORING_SETUP_SINGLE_ISSUER;
876 goto retry;
877 }
4d22c103
JA
878 if (errno == EINVAL && p.flags & IORING_SETUP_COOP_TASKRUN) {
879 p.flags &= ~IORING_SETUP_COOP_TASKRUN;
880 goto retry;
881 }
b5e99df6
JA
882 if (errno == EINVAL && p.flags & IORING_SETUP_CQSIZE) {
883 p.flags &= ~IORING_SETUP_CQSIZE;
884 goto retry;
885 }
9a2d78b3 886 return ret;
b5e99df6 887 }
9a2d78b3
JA
888
889 ld->ring_fd = ret;
2ea53ca3 890
556d8415
JA
891 fio_ioring_probe(td);
892
2ea53ca3 893 if (o->fixedbufs) {
bfed648c 894 ret = syscall(__NR_io_uring_register, ld->ring_fd,
919850d2 895 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
2ea53ca3
JA
896 if (ret < 0)
897 return ret;
898 }
899
bffad86f 900 return fio_ioring_mmap(ld, &p);
52885fa2
JA
901}
902
855dc4d4
AG
903static int fio_ioring_cmd_queue_init(struct thread_data *td)
904{
905 struct ioring_data *ld = td->io_ops_data;
906 struct ioring_options *o = td->eo;
907 int depth = td->o.iodepth;
908 struct io_uring_params p;
909 int ret;
910
911 memset(&p, 0, sizeof(p));
912
913 if (o->hipri)
914 p.flags |= IORING_SETUP_IOPOLL;
915 if (o->sqpoll_thread) {
916 p.flags |= IORING_SETUP_SQPOLL;
917 if (o->sqpoll_set) {
918 p.flags |= IORING_SETUP_SQ_AFF;
919 p.sq_thread_cpu = o->sqpoll_cpu;
920 }
c011bf12
AK
921
922 /*
923 * Submission latency for sqpoll_thread is just the time it
924 * takes to fill in the SQ ring entries, and any syscall if
925 * IORING_SQ_NEED_WAKEUP is set, we don't need to log that time
926 * separately.
927 */
928 td->o.disable_slat = 1;
855dc4d4
AG
929 }
930 if (o->cmd_type == FIO_URING_CMD_NVME) {
931 p.flags |= IORING_SETUP_SQE128;
932 p.flags |= IORING_SETUP_CQE32;
933 }
934
935 /*
936 * Clamp CQ ring size at our SQ ring size, we don't need more entries
937 * than that.
938 */
939 p.flags |= IORING_SETUP_CQSIZE;
940 p.cq_entries = depth;
941
07f78c37
AK
942 /*
943 * Setup COOP_TASKRUN as we don't need to get IPI interrupted for
944 * completing IO operations.
945 */
946 p.flags |= IORING_SETUP_COOP_TASKRUN;
947
948 /*
949 * io_uring is always a single issuer, and we can defer task_work
950 * runs until we reap events.
951 */
952 p.flags |= IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN;
953
855dc4d4
AG
954retry:
955 ret = syscall(__NR_io_uring_setup, depth, &p);
956 if (ret < 0) {
07f78c37
AK
957 if (errno == EINVAL && p.flags & IORING_SETUP_DEFER_TASKRUN) {
958 p.flags &= ~IORING_SETUP_DEFER_TASKRUN;
959 p.flags &= ~IORING_SETUP_SINGLE_ISSUER;
960 goto retry;
961 }
962 if (errno == EINVAL && p.flags & IORING_SETUP_COOP_TASKRUN) {
963 p.flags &= ~IORING_SETUP_COOP_TASKRUN;
964 goto retry;
965 }
855dc4d4
AG
966 if (errno == EINVAL && p.flags & IORING_SETUP_CQSIZE) {
967 p.flags &= ~IORING_SETUP_CQSIZE;
968 goto retry;
969 }
970 return ret;
971 }
972
973 ld->ring_fd = ret;
974
975 fio_ioring_probe(td);
976
977 if (o->fixedbufs) {
978 ret = syscall(__NR_io_uring_register, ld->ring_fd,
979 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
980 if (ret < 0)
981 return ret;
982 }
983
984 return fio_ioring_mmap(ld, &p);
985}
986
5ffd5626
JA
987static int fio_ioring_register_files(struct thread_data *td)
988{
989 struct ioring_data *ld = td->io_ops_data;
990 struct fio_file *f;
991 unsigned int i;
992 int ret;
993
994 ld->fds = calloc(td->o.nr_files, sizeof(int));
995
996 for_each_file(td, f, i) {
997 ret = generic_open_file(td, f);
998 if (ret)
999 goto err;
1000 ld->fds[i] = f->fd;
1001 f->engine_pos = i;
1002 }
1003
bfed648c 1004 ret = syscall(__NR_io_uring_register, ld->ring_fd,
5ffd5626
JA
1005 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
1006 if (ret) {
1007err:
1008 free(ld->fds);
1009 ld->fds = NULL;
1010 }
1011
1012 /*
1013 * Pretend the file is closed again, and really close it if we hit
1014 * an error.
1015 */
1016 for_each_file(td, f, i) {
1017 if (ret) {
1018 int fio_unused ret2;
1019 ret2 = generic_close_file(td, f);
1020 } else
1021 f->fd = -1;
1022 }
1023
1024 return ret;
1025}
1026
bffad86f 1027static int fio_ioring_post_init(struct thread_data *td)
52885fa2 1028{
bffad86f 1029 struct ioring_data *ld = td->io_ops_data;
5ffd5626 1030 struct ioring_options *o = td->eo;
52885fa2 1031 struct io_u *io_u;
650346e1 1032 int err, i;
52885fa2 1033
650346e1
JA
1034 for (i = 0; i < td->o.iodepth; i++) {
1035 struct iovec *iov = &ld->iovecs[i];
9a2d78b3 1036
650346e1
JA
1037 io_u = ld->io_u_index[i];
1038 iov->iov_base = io_u->buf;
1039 iov->iov_len = td_max_bs(td);
52885fa2
JA
1040 }
1041
bffad86f 1042 err = fio_ioring_queue_init(td);
52885fa2 1043 if (err) {
0442b53f 1044 int init_err = errno;
c4f5c92f 1045
0442b53f 1046 if (init_err == ENOSYS)
c4f5c92f 1047 log_err("fio: your kernel doesn't support io_uring\n");
0442b53f 1048 td_verror(td, init_err, "io_queue_init");
52885fa2
JA
1049 return 1;
1050 }
1051
7c70f506
JA
1052 for (i = 0; i < td->o.iodepth; i++) {
1053 struct io_uring_sqe *sqe;
1054
1055 sqe = &ld->sqes[i];
1056 memset(sqe, 0, sizeof(*sqe));
1057 }
1058
5ffd5626
JA
1059 if (o->registerfiles) {
1060 err = fio_ioring_register_files(td);
1061 if (err) {
1062 td_verror(td, errno, "ioring_register_files");
1063 return 1;
1064 }
1065 }
1066
52885fa2
JA
1067 return 0;
1068}
1069
855dc4d4
AG
1070static int fio_ioring_cmd_post_init(struct thread_data *td)
1071{
1072 struct ioring_data *ld = td->io_ops_data;
1073 struct ioring_options *o = td->eo;
1074 struct io_u *io_u;
1075 int err, i;
1076
1077 for (i = 0; i < td->o.iodepth; i++) {
1078 struct iovec *iov = &ld->iovecs[i];
1079
1080 io_u = ld->io_u_index[i];
1081 iov->iov_base = io_u->buf;
1082 iov->iov_len = td_max_bs(td);
1083 }
1084
1085 err = fio_ioring_cmd_queue_init(td);
1086 if (err) {
1087 int init_err = errno;
1088
1089 td_verror(td, init_err, "io_queue_init");
1090 return 1;
1091 }
1092
1093 for (i = 0; i < td->o.iodepth; i++) {
1094 struct io_uring_sqe *sqe;
1095
1096 if (o->cmd_type == FIO_URING_CMD_NVME) {
1097 sqe = &ld->sqes[i << 1];
1098 memset(sqe, 0, 2 * sizeof(*sqe));
1099 } else {
1100 sqe = &ld->sqes[i];
1101 memset(sqe, 0, sizeof(*sqe));
1102 }
1103 }
1104
1105 if (o->registerfiles) {
1106 err = fio_ioring_register_files(td);
1107 if (err) {
1108 td_verror(td, errno, "ioring_register_files");
1109 return 1;
1110 }
1111 }
1112
1113 return 0;
1114}
1115
3ee8311a
AK
1116static void parse_prchk_flags(struct ioring_options *o)
1117{
1118 if (!o->pi_chk)
1119 return;
1120
1121 if (strstr(o->pi_chk, "GUARD") != NULL)
1122 o->prchk = NVME_IO_PRINFO_PRCHK_GUARD;
1123 if (strstr(o->pi_chk, "REFTAG") != NULL)
1124 o->prchk |= NVME_IO_PRINFO_PRCHK_REF;
1125 if (strstr(o->pi_chk, "APPTAG") != NULL)
1126 o->prchk |= NVME_IO_PRINFO_PRCHK_APP;
1127}
1128
bffad86f 1129static int fio_ioring_init(struct thread_data *td)
52885fa2 1130{
5ffd5626 1131 struct ioring_options *o = td->eo;
bffad86f 1132 struct ioring_data *ld;
2d6451c9 1133 unsigned long long md_size;
e9f6567a 1134 int ret;
52885fa2 1135
5ffd5626
JA
1136 /* sqthread submission requires registered files */
1137 if (o->sqpoll_thread)
1138 o->registerfiles = 1;
1139
1140 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
1141 log_err("fio: io_uring registered files require nr_files to "
1142 "be identical to open_files\n");
1143 return 1;
1144 }
1145
52885fa2
JA
1146 ld = calloc(1, sizeof(*ld));
1147
b87aa01a
JA
1148 /* ring depth must be a power-of-2 */
1149 ld->iodepth = td->o.iodepth;
1150 td->o.iodepth = roundup_pow2(td->o.iodepth);
1151
52885fa2
JA
1152 /* io_u index */
1153 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
2d6451c9
AK
1154
1155 /*
1156 * metadata buffer for nvme command.
1157 * We are only supporting iomem=malloc / mem=malloc as of now.
1158 */
1159 if (!strcmp(td->io_ops->name, "io_uring_cmd") &&
1160 (o->cmd_type == FIO_URING_CMD_NVME) && o->md_per_io_size) {
1161 md_size = (unsigned long long) o->md_per_io_size
1162 * (unsigned long long) td->o.iodepth;
1163 md_size += page_mask + td->o.mem_align;
1164 if (td->o.mem_align && td->o.mem_align > page_size)
1165 md_size += td->o.mem_align - page_size;
1166 if (td->o.mem_type == MEM_MALLOC) {
1167 ld->md_buf = malloc(md_size);
1168 if (!ld->md_buf)
1169 return 1;
1170 } else {
1171 log_err("fio: Only iomem=malloc or mem=malloc is supported\n");
1172 return 1;
1173 }
1174 }
3ee8311a 1175 parse_prchk_flags(o);
2d6451c9 1176
650346e1 1177 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
52885fa2
JA
1178
1179 td->io_ops_data = ld;
b2a432bf 1180
d6cbeab4 1181 ret = fio_cmdprio_init(td, &ld->cmdprio, &o->cmdprio_options);
e9f6567a
DLM
1182 if (ret) {
1183 td_verror(td, EINVAL, "fio_ioring_init");
b2a432bf
PC
1184 return 1;
1185 }
1af44196 1186
4885a6eb
VF
1187 /*
1188 * For io_uring_cmd, trims are async operations unless we are operating
1189 * in zbd mode where trim means zone reset.
1190 */
1191 if (!strcmp(td->io_ops->name, "io_uring_cmd") && td_trim(td) &&
1192 td->o.zone_mode == ZONE_MODE_ZBD)
1193 td->io_ops->flags |= FIO_ASYNCIO_SYNC_TRIM;
1194 else
1195 ld->dsm = calloc(ld->iodepth, sizeof(*ld->dsm));
1196
52885fa2
JA
1197 return 0;
1198}
1199
bffad86f 1200static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
52885fa2 1201{
bffad86f 1202 struct ioring_data *ld = td->io_ops_data;
2d6451c9 1203 struct ioring_options *o = td->eo;
5163f35e 1204 struct nvme_pi_data *pi_data;
2d6451c9 1205 char *p;
52885fa2
JA
1206
1207 ld->io_u_index[io_u->index] = io_u;
2d6451c9
AK
1208
1209 if (!strcmp(td->io_ops->name, "io_uring_cmd")) {
1210 p = PTR_ALIGN(ld->md_buf, page_mask) + td->o.mem_align;
1211 p += o->md_per_io_size * io_u->index;
1212 io_u->mmap_data = p;
5163f35e
AK
1213
1214 if (!o->pi_act) {
1215 pi_data = calloc(1, sizeof(*pi_data));
1216 pi_data->io_flags |= o->prchk;
1217 pi_data->apptag_mask = o->apptag_mask;
1218 pi_data->apptag = o->apptag;
1219 io_u->engine_data = pi_data;
1220 }
2d6451c9
AK
1221 }
1222
52885fa2
JA
1223 return 0;
1224}
1225
5163f35e
AK
1226static void fio_ioring_io_u_free(struct thread_data *td, struct io_u *io_u)
1227{
1228 struct ioring_options *o = td->eo;
1229 struct nvme_pi *pi;
1230
1231 if (!strcmp(td->io_ops->name, "io_uring_cmd") &&
1232 (o->cmd_type == FIO_URING_CMD_NVME)) {
1233 pi = io_u->engine_data;
1234 free(pi);
1235 io_u->engine_data = NULL;
1236 }
1237}
1238
5ffd5626
JA
1239static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
1240{
1241 struct ioring_data *ld = td->io_ops_data;
1242 struct ioring_options *o = td->eo;
1243
17318cf6 1244 if (!ld || !o->registerfiles)
5ffd5626
JA
1245 return generic_open_file(td, f);
1246
1247 f->fd = ld->fds[f->engine_pos];
1248 return 0;
1249}
1250
855dc4d4
AG
1251static int fio_ioring_cmd_open_file(struct thread_data *td, struct fio_file *f)
1252{
1253 struct ioring_data *ld = td->io_ops_data;
1254 struct ioring_options *o = td->eo;
1255
1256 if (o->cmd_type == FIO_URING_CMD_NVME) {
1257 struct nvme_data *data = NULL;
e7e5023b 1258 unsigned int lba_size = 0;
671aa9f5 1259 __u64 nlba = 0;
855dc4d4
AG
1260 int ret;
1261
1262 /* Store the namespace-id and lba size. */
1263 data = FILE_ENG_DATA(f);
1264 if (data == NULL) {
855dc4d4 1265 data = calloc(1, sizeof(struct nvme_data));
3ee8311a 1266 ret = fio_nvme_get_info(f, &nlba, o->pi_act, data);
e7e5023b
AK
1267 if (ret) {
1268 free(data);
1269 return ret;
1270 }
855dc4d4
AG
1271
1272 FILE_SET_ENG_DATA(f, data);
1273 }
345fa8fd 1274
e7e5023b 1275 lba_size = data->lba_ext ? data->lba_ext : data->lba_size;
345fa8fd
AK
1276
1277 for_each_rw_ddir(ddir) {
1278 if (td->o.min_bs[ddir] % lba_size ||
1279 td->o.max_bs[ddir] % lba_size) {
1280 if (data->lba_ext)
a904d182
AK
1281 log_err("%s: block size must be a multiple of (LBA data size + Metadata size)\n",
1282 f->file_name);
345fa8fd 1283 else
a904d182
AK
1284 log_err("%s: block size must be a multiple of LBA data size\n",
1285 f->file_name);
1286 td_verror(td, EINVAL, "fio_ioring_cmd_open_file");
345fa8fd
AK
1287 return 1;
1288 }
2d6451c9
AK
1289 if (data->ms && !data->lba_ext && ddir != DDIR_TRIM &&
1290 (o->md_per_io_size < ((td->o.max_bs[ddir] / data->lba_size) *
1291 data->ms))) {
1292 log_err("%s: md_per_io_size should be at least %llu bytes\n",
1293 f->file_name,
1294 ((td->o.max_bs[ddir] / data->lba_size) * data->ms));
1295 td_verror(td, EINVAL, "fio_ioring_cmd_open_file");
1296 return 1;
1297 }
345fa8fd 1298 }
855dc4d4
AG
1299 }
1300 if (!ld || !o->registerfiles)
1301 return generic_open_file(td, f);
1302
1303 f->fd = ld->fds[f->engine_pos];
1304 return 0;
1305}
1306
5ffd5626
JA
1307static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
1308{
17318cf6 1309 struct ioring_data *ld = td->io_ops_data;
5ffd5626
JA
1310 struct ioring_options *o = td->eo;
1311
17318cf6 1312 if (!ld || !o->registerfiles)
5ffd5626
JA
1313 return generic_close_file(td, f);
1314
1315 f->fd = -1;
1316 return 0;
1317}
1318
855dc4d4
AG
1319static int fio_ioring_cmd_close_file(struct thread_data *td,
1320 struct fio_file *f)
1321{
1322 struct ioring_data *ld = td->io_ops_data;
1323 struct ioring_options *o = td->eo;
1324
1325 if (o->cmd_type == FIO_URING_CMD_NVME) {
1326 struct nvme_data *data = FILE_ENG_DATA(f);
1327
1328 FILE_SET_ENG_DATA(f, NULL);
1329 free(data);
1330 }
1331 if (!ld || !o->registerfiles)
1332 return generic_close_file(td, f);
1333
1334 f->fd = -1;
1335 return 0;
1336}
1337
1338static int fio_ioring_cmd_get_file_size(struct thread_data *td,
1339 struct fio_file *f)
1340{
1341 struct ioring_options *o = td->eo;
1342
1343 if (fio_file_size_known(f))
1344 return 0;
1345
1346 if (o->cmd_type == FIO_URING_CMD_NVME) {
1347 struct nvme_data *data = NULL;
671aa9f5 1348 __u64 nlba = 0;
855dc4d4
AG
1349 int ret;
1350
855dc4d4 1351 data = calloc(1, sizeof(struct nvme_data));
3ee8311a 1352 ret = fio_nvme_get_info(f, &nlba, o->pi_act, data);
e7e5023b
AK
1353 if (ret) {
1354 free(data);
1355 return ret;
1356 }
855dc4d4 1357
e7e5023b 1358 f->real_file_size = data->lba_size * nlba;
855dc4d4
AG
1359 fio_file_set_size_known(f);
1360
1361 FILE_SET_ENG_DATA(f, data);
1362 return 0;
1363 }
1364 return generic_get_file_size(td, f);
1365}
1366
3d05e0ff
AK
1367static int fio_ioring_cmd_get_zoned_model(struct thread_data *td,
1368 struct fio_file *f,
1369 enum zbd_zoned_model *model)
1370{
1371 return fio_nvme_get_zoned_model(td, f, model);
1372}
1373
1374static int fio_ioring_cmd_report_zones(struct thread_data *td,
1375 struct fio_file *f, uint64_t offset,
1376 struct zbd_zone *zbdz,
1377 unsigned int nr_zones)
1378{
1379 return fio_nvme_report_zones(td, f, offset, zbdz, nr_zones);
1380}
1381
1382static int fio_ioring_cmd_reset_wp(struct thread_data *td, struct fio_file *f,
1383 uint64_t offset, uint64_t length)
1384{
1385 return fio_nvme_reset_wp(td, f, offset, length);
1386}
1387
1388static int fio_ioring_cmd_get_max_open_zones(struct thread_data *td,
1389 struct fio_file *f,
1390 unsigned int *max_open_zones)
1391{
1392 return fio_nvme_get_max_open_zones(td, f, max_open_zones);
1393}
1394
a7e8aae0
KB
1395static int fio_ioring_cmd_fetch_ruhs(struct thread_data *td, struct fio_file *f,
1396 struct fio_ruhs_info *fruhs_info)
1397{
1398 struct nvme_fdp_ruh_status *ruhs;
1399 int bytes, ret, i;
1400
98cd3c0e 1401 bytes = sizeof(*ruhs) + FDP_MAX_RUHS * sizeof(struct nvme_fdp_ruh_status_desc);
a7e8aae0
KB
1402 ruhs = scalloc(1, bytes);
1403 if (!ruhs)
1404 return -ENOMEM;
1405
1406 ret = fio_nvme_iomgmt_ruhs(td, f, ruhs, bytes);
1407 if (ret)
1408 goto free;
1409
1410 fruhs_info->nr_ruhs = le16_to_cpu(ruhs->nruhsd);
1411 for (i = 0; i < fruhs_info->nr_ruhs; i++)
1412 fruhs_info->plis[i] = le16_to_cpu(ruhs->ruhss[i].pid);
1413free:
1414 sfree(ruhs);
1415 return ret;
1416}
1417
855dc4d4 1418static struct ioengine_ops ioengine_uring = {
bffad86f 1419 .name = "io_uring",
52885fa2 1420 .version = FIO_IOOPS_VERSION,
4e7e7898
VF
1421 .flags = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD |
1422 FIO_ASYNCIO_SETS_ISSUE_TIME,
bffad86f
JA
1423 .init = fio_ioring_init,
1424 .post_init = fio_ioring_post_init,
1425 .io_u_init = fio_ioring_io_u_init,
1426 .prep = fio_ioring_prep,
1427 .queue = fio_ioring_queue,
1428 .commit = fio_ioring_commit,
1429 .getevents = fio_ioring_getevents,
1430 .event = fio_ioring_event,
1431 .cleanup = fio_ioring_cleanup,
5ffd5626
JA
1432 .open_file = fio_ioring_open_file,
1433 .close_file = fio_ioring_close_file,
52885fa2
JA
1434 .get_file_size = generic_get_file_size,
1435 .options = options,
bffad86f 1436 .option_struct_size = sizeof(struct ioring_options),
52885fa2
JA
1437};
1438
855dc4d4
AG
1439static struct ioengine_ops ioengine_uring_cmd = {
1440 .name = "io_uring_cmd",
1441 .version = FIO_IOOPS_VERSION,
4885a6eb 1442 .flags = FIO_NO_OFFLOAD | FIO_MEMALIGN | FIO_RAWIO |
4e7e7898 1443 FIO_ASYNCIO_SETS_ISSUE_TIME,
855dc4d4
AG
1444 .init = fio_ioring_init,
1445 .post_init = fio_ioring_cmd_post_init,
1446 .io_u_init = fio_ioring_io_u_init,
5163f35e 1447 .io_u_free = fio_ioring_io_u_free,
855dc4d4
AG
1448 .prep = fio_ioring_cmd_prep,
1449 .queue = fio_ioring_queue,
1450 .commit = fio_ioring_commit,
1451 .getevents = fio_ioring_getevents,
1452 .event = fio_ioring_cmd_event,
1453 .cleanup = fio_ioring_cleanup,
1454 .open_file = fio_ioring_cmd_open_file,
1455 .close_file = fio_ioring_cmd_close_file,
1456 .get_file_size = fio_ioring_cmd_get_file_size,
3d05e0ff
AK
1457 .get_zoned_model = fio_ioring_cmd_get_zoned_model,
1458 .report_zones = fio_ioring_cmd_report_zones,
1459 .reset_wp = fio_ioring_cmd_reset_wp,
1460 .get_max_open_zones = fio_ioring_cmd_get_max_open_zones,
855dc4d4
AG
1461 .options = options,
1462 .option_struct_size = sizeof(struct ioring_options),
a7e8aae0 1463 .fdp_fetch_ruhs = fio_ioring_cmd_fetch_ruhs,
855dc4d4
AG
1464};
1465
bffad86f 1466static void fio_init fio_ioring_register(void)
52885fa2 1467{
855dc4d4
AG
1468 register_ioengine(&ioengine_uring);
1469 register_ioengine(&ioengine_uring_cmd);
52885fa2
JA
1470}
1471
bffad86f 1472static void fio_exit fio_ioring_unregister(void)
52885fa2 1473{
855dc4d4
AG
1474 unregister_ioengine(&ioengine_uring);
1475 unregister_ioengine(&ioengine_uring_cmd);
52885fa2 1476}
1f90e9bb 1477#endif