4 * IO engine using the new native Linux aio io_uring interface. See:
6 * http://git.kernel.dk/cgit/linux-block/log/?h=io_uring
13 #include <sys/resource.h>
16 #include "../lib/pow2.h"
17 #include "../optgroup.h"
18 #include "../lib/memalign.h"
19 #include "../lib/fls.h"
20 #include "../lib/roundup.h"
21 #include "../verify.h"
23 #ifdef ARCH_HAVE_IOURING
25 #include "../lib/types.h"
26 #include "../os/linux/io_uring.h"
34 FIO_URING_CMD_NVME = 1,
41 unsigned *ring_entries;
50 unsigned *ring_entries;
51 struct io_uring_cqe *cqes;
62 struct io_u **io_u_index;
67 struct io_sq_ring sq_ring;
68 struct io_uring_sqe *sqes;
70 unsigned sq_ring_mask;
72 struct io_cq_ring cq_ring;
73 unsigned cq_ring_mask;
80 struct ioring_mmap mmap[3];
82 struct cmdprio cmdprio;
87 struct ioring_options {
88 struct thread_data *td;
90 struct cmdprio_options cmdprio_options;
91 unsigned int fixedbufs;
92 unsigned int registerfiles;
93 unsigned int sqpoll_thread;
94 unsigned int sqpoll_set;
95 unsigned int sqpoll_cpu;
96 unsigned int nonvectored;
97 unsigned int uncached;
99 unsigned int force_async;
100 unsigned int md_per_io_size;
103 unsigned int apptag_mask;
106 enum uring_cmd_type cmd_type;
109 static const int ddir_to_op[2][2] = {
110 { IORING_OP_READV, IORING_OP_READ },
111 { IORING_OP_WRITEV, IORING_OP_WRITE }
114 static const int fixed_ddir_to_op[2] = {
115 IORING_OP_READ_FIXED,
116 IORING_OP_WRITE_FIXED
119 static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
121 struct ioring_options *o = data;
123 o->sqpoll_cpu = *val;
128 static struct fio_option options[] = {
131 .lname = "High Priority",
132 .type = FIO_OPT_STR_SET,
133 .off1 = offsetof(struct ioring_options, hipri),
134 .help = "Use polled IO completions",
135 .category = FIO_OPT_C_ENGINE,
136 .group = FIO_OPT_G_IOURING,
140 .lname = "Fixed (pre-mapped) IO buffers",
141 .type = FIO_OPT_STR_SET,
142 .off1 = offsetof(struct ioring_options, fixedbufs),
143 .help = "Pre map IO buffers",
144 .category = FIO_OPT_C_ENGINE,
145 .group = FIO_OPT_G_IOURING,
148 .name = "registerfiles",
149 .lname = "Register file set",
150 .type = FIO_OPT_STR_SET,
151 .off1 = offsetof(struct ioring_options, registerfiles),
152 .help = "Pre-open/register files",
153 .category = FIO_OPT_C_ENGINE,
154 .group = FIO_OPT_G_IOURING,
157 .name = "sqthread_poll",
158 .lname = "Kernel SQ thread polling",
159 .type = FIO_OPT_STR_SET,
160 .off1 = offsetof(struct ioring_options, sqpoll_thread),
161 .help = "Offload submission/completion to kernel thread",
162 .category = FIO_OPT_C_ENGINE,
163 .group = FIO_OPT_G_IOURING,
166 .name = "sqthread_poll_cpu",
167 .lname = "SQ Thread Poll CPU",
169 .cb = fio_ioring_sqpoll_cb,
170 .help = "What CPU to run SQ thread polling on",
171 .category = FIO_OPT_C_ENGINE,
172 .group = FIO_OPT_G_IOURING,
175 .name = "nonvectored",
176 .lname = "Non-vectored",
178 .off1 = offsetof(struct ioring_options, nonvectored),
180 .help = "Use non-vectored read/write commands",
181 .category = FIO_OPT_C_ENGINE,
182 .group = FIO_OPT_G_IOURING,
188 .off1 = offsetof(struct ioring_options, uncached),
189 .help = "Use RWF_UNCACHED for buffered read/writes",
190 .category = FIO_OPT_C_ENGINE,
191 .group = FIO_OPT_G_IOURING,
195 .lname = "RWF_NOWAIT",
196 .type = FIO_OPT_BOOL,
197 .off1 = offsetof(struct ioring_options, nowait),
198 .help = "Use RWF_NOWAIT for reads/writes",
199 .category = FIO_OPT_C_ENGINE,
200 .group = FIO_OPT_G_IOURING,
203 .name = "force_async",
204 .lname = "Force async",
206 .off1 = offsetof(struct ioring_options, force_async),
207 .help = "Set IOSQE_ASYNC every N requests",
208 .category = FIO_OPT_C_ENGINE,
209 .group = FIO_OPT_G_IOURING,
213 .lname = "Uring cmd type",
215 .off1 = offsetof(struct ioring_options, cmd_type),
216 .help = "Specify uring-cmd type",
220 .oval = FIO_URING_CMD_NVME,
221 .help = "Issue nvme-uring-cmd",
224 .category = FIO_OPT_C_ENGINE,
225 .group = FIO_OPT_G_IOURING,
227 CMDPRIO_OPTIONS(struct ioring_options, FIO_OPT_G_IOURING),
229 .name = "md_per_io_size",
230 .lname = "Separate Metadata Buffer Size per I/O",
232 .off1 = offsetof(struct ioring_options, md_per_io_size),
234 .help = "Size of separate metadata buffer per I/O (Default: 0)",
235 .category = FIO_OPT_C_ENGINE,
236 .group = FIO_OPT_G_IOURING,
240 .lname = "Protection Information Action",
241 .type = FIO_OPT_BOOL,
242 .off1 = offsetof(struct ioring_options, pi_act),
244 .help = "Protection Information Action bit (pi_act=1 or pi_act=0)",
245 .category = FIO_OPT_C_ENGINE,
246 .group = FIO_OPT_G_IOURING,
250 .lname = "Protection Information Check",
251 .type = FIO_OPT_STR_STORE,
252 .off1 = offsetof(struct ioring_options, pi_chk),
254 .help = "Control of Protection Information Checking (pi_chk=GUARD,REFTAG,APPTAG)",
255 .category = FIO_OPT_C_ENGINE,
256 .group = FIO_OPT_G_IOURING,
260 .lname = "Application Tag used in Protection Information",
262 .off1 = offsetof(struct ioring_options, apptag),
264 .help = "Application Tag used in Protection Information field (Default: 0x1234)",
265 .category = FIO_OPT_C_ENGINE,
266 .group = FIO_OPT_G_IOURING,
269 .name = "apptag_mask",
270 .lname = "Application Tag Mask",
272 .off1 = offsetof(struct ioring_options, apptag_mask),
274 .help = "Application Tag Mask used with Application Tag (Default: 0xffff)",
275 .category = FIO_OPT_C_ENGINE,
276 .group = FIO_OPT_G_IOURING,
283 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
284 unsigned int min_complete, unsigned int flags)
286 #ifdef FIO_ARCH_HAS_SYSCALL
287 return __do_syscall6(__NR_io_uring_enter, ld->ring_fd, to_submit,
288 min_complete, flags, NULL, 0);
290 return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit,
291 min_complete, flags, NULL, 0);
295 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
297 struct ioring_data *ld = td->io_ops_data;
298 struct ioring_options *o = td->eo;
299 struct fio_file *f = io_u->file;
300 struct io_uring_sqe *sqe;
302 sqe = &ld->sqes[io_u->index];
304 if (o->registerfiles) {
305 sqe->fd = f->engine_pos;
306 sqe->flags = IOSQE_FIXED_FILE;
312 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
314 sqe->opcode = fixed_ddir_to_op[io_u->ddir];
315 sqe->addr = (unsigned long) io_u->xfer_buf;
316 sqe->len = io_u->xfer_buflen;
317 sqe->buf_index = io_u->index;
319 struct iovec *iov = &ld->iovecs[io_u->index];
322 * Update based on actual io_u, requeue could have
325 iov->iov_base = io_u->xfer_buf;
326 iov->iov_len = io_u->xfer_buflen;
328 sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
329 if (o->nonvectored) {
330 sqe->addr = (unsigned long) iov->iov_base;
331 sqe->len = iov->iov_len;
333 sqe->addr = (unsigned long) iov;
338 if (!td->o.odirect && o->uncached)
339 sqe->rw_flags |= RWF_UNCACHED;
341 sqe->rw_flags |= RWF_NOWAIT;
344 * Since io_uring can have a submission context (sqthread_poll)
345 * that is different from the process context, we cannot rely on
346 * the IO priority set by ioprio_set() (options prio, prioclass,
347 * and priohint) to be inherited.
348 * td->ioprio will have the value of the "default prio", so set
349 * this unconditionally. This value might get overridden by
350 * fio_ioring_cmdprio_prep() if the option cmdprio_percentage or
351 * cmdprio_bssplit is used.
353 sqe->ioprio = td->ioprio;
354 sqe->off = io_u->offset;
355 } else if (ddir_sync(io_u->ddir)) {
357 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
358 sqe->off = f->first_write;
359 sqe->len = f->last_write - f->first_write;
360 sqe->sync_range_flags = td->o.sync_file_range;
361 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
366 if (io_u->ddir == DDIR_DATASYNC)
367 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
368 sqe->opcode = IORING_OP_FSYNC;
372 if (o->force_async && ++ld->prepped == o->force_async) {
374 sqe->flags |= IOSQE_ASYNC;
377 sqe->user_data = (unsigned long) io_u;
381 static int fio_ioring_cmd_prep(struct thread_data *td, struct io_u *io_u)
383 struct ioring_data *ld = td->io_ops_data;
384 struct ioring_options *o = td->eo;
385 struct fio_file *f = io_u->file;
386 struct nvme_uring_cmd *cmd;
387 struct io_uring_sqe *sqe;
388 struct nvme_dsm *dsm;
390 unsigned int dsm_size;
392 /* only supports nvme_uring_cmd */
393 if (o->cmd_type != FIO_URING_CMD_NVME)
396 if (io_u->ddir == DDIR_TRIM && td->io_ops->flags & FIO_ASYNCIO_SYNC_TRIM)
399 sqe = &ld->sqes[(io_u->index) << 1];
401 if (o->registerfiles) {
402 sqe->fd = f->engine_pos;
403 sqe->flags = IOSQE_FIXED_FILE;
408 if (!td->o.odirect && o->uncached)
409 sqe->rw_flags |= RWF_UNCACHED;
411 sqe->rw_flags |= RWF_NOWAIT;
413 sqe->opcode = IORING_OP_URING_CMD;
414 sqe->user_data = (unsigned long) io_u;
416 sqe->cmd_op = NVME_URING_CMD_IO;
418 sqe->cmd_op = NVME_URING_CMD_IO_VEC;
419 if (o->force_async && ++ld->prepped == o->force_async) {
421 sqe->flags |= IOSQE_ASYNC;
424 sqe->uring_cmd_flags = IORING_URING_CMD_FIXED;
425 sqe->buf_index = io_u->index;
428 cmd = (struct nvme_uring_cmd *)sqe->cmd;
429 dsm_size = sizeof(*ld->dsm) + td->o.num_range * sizeof(struct nvme_dsm_range);
430 ptr += io_u->index * dsm_size;
431 dsm = (struct nvme_dsm *)ptr;
433 return fio_nvme_uring_cmd_prep(cmd, io_u,
434 o->nonvectored ? NULL : &ld->iovecs[io_u->index],
438 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
440 struct ioring_data *ld = td->io_ops_data;
441 struct io_uring_cqe *cqe;
445 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
447 cqe = &ld->cq_ring.cqes[index];
448 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
450 if (cqe->res != io_u->xfer_buflen) {
451 if (cqe->res > io_u->xfer_buflen)
452 io_u->error = -cqe->res;
454 io_u->resid = io_u->xfer_buflen - cqe->res;
461 static struct io_u *fio_ioring_cmd_event(struct thread_data *td, int event)
463 struct ioring_data *ld = td->io_ops_data;
464 struct ioring_options *o = td->eo;
465 struct io_uring_cqe *cqe;
467 struct nvme_data *data;
471 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
472 if (o->cmd_type == FIO_URING_CMD_NVME)
475 cqe = &ld->cq_ring.cqes[index];
476 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
479 io_u->error = -cqe->res;
485 if (o->cmd_type == FIO_URING_CMD_NVME) {
486 data = FILE_ENG_DATA(io_u->file);
487 if (data->pi_type && (io_u->ddir == DDIR_READ) && !o->pi_act) {
488 ret = fio_nvme_pi_verify(data, io_u);
497 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
500 struct ioring_data *ld = td->io_ops_data;
501 struct io_cq_ring *ring = &ld->cq_ring;
502 unsigned head, reaped = 0;
506 if (head == atomic_load_acquire(ring->tail))
510 } while (reaped + events < max);
513 atomic_store_release(ring->head, head);
518 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
519 unsigned int max, const struct timespec *t)
521 struct ioring_data *ld = td->io_ops_data;
522 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
523 struct ioring_options *o = td->eo;
524 struct io_cq_ring *ring = &ld->cq_ring;
528 ld->cq_ring_off = *ring->head;
530 r = fio_ioring_cqring_reap(td, events, max);
539 if (!o->sqpoll_thread) {
540 r = io_uring_enter(ld, 0, actual_min,
541 IORING_ENTER_GETEVENTS);
543 if (errno == EAGAIN || errno == EINTR)
546 td_verror(td, errno, "io_uring_enter");
550 } while (events < min);
552 return r < 0 ? r : events;
555 static inline void fio_ioring_cmd_nvme_pi(struct thread_data *td,
558 struct ioring_data *ld = td->io_ops_data;
559 struct ioring_options *o = td->eo;
560 struct nvme_uring_cmd *cmd;
561 struct io_uring_sqe *sqe;
562 struct nvme_cmd_ext_io_opts ext_opts = {0};
563 struct nvme_data *data = FILE_ENG_DATA(io_u->file);
565 if (io_u->ddir == DDIR_TRIM)
568 sqe = &ld->sqes[(io_u->index) << 1];
569 cmd = (struct nvme_uring_cmd *)sqe->cmd;
573 ext_opts.io_flags |= NVME_IO_PRINFO_PRACT;
574 ext_opts.io_flags |= o->prchk;
575 ext_opts.apptag = o->apptag;
576 ext_opts.apptag_mask = o->apptag_mask;
579 fio_nvme_pi_fill(cmd, io_u, &ext_opts);
582 static inline void fio_ioring_cmdprio_prep(struct thread_data *td,
585 struct ioring_data *ld = td->io_ops_data;
586 struct cmdprio *cmdprio = &ld->cmdprio;
588 if (fio_cmdprio_set_ioprio(td, cmdprio, io_u))
589 ld->sqes[io_u->index].ioprio = io_u->ioprio;
592 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
595 struct ioring_data *ld = td->io_ops_data;
596 struct ioring_options *o = td->eo;
597 struct io_sq_ring *ring = &ld->sq_ring;
598 unsigned tail, next_tail;
600 fio_ro_check(td, io_u);
602 if (ld->queued == ld->iodepth)
605 if (io_u->ddir == DDIR_TRIM && td->io_ops->flags & FIO_ASYNCIO_SYNC_TRIM) {
609 do_io_u_trim(td, io_u);
611 io_u_mark_submit(td, 1);
612 io_u_mark_complete(td, 1);
613 return FIO_Q_COMPLETED;
617 next_tail = tail + 1;
618 if (next_tail == atomic_load_relaxed(ring->head))
621 if (ld->cmdprio.mode != CMDPRIO_MODE_NONE)
622 fio_ioring_cmdprio_prep(td, io_u);
624 if (!strcmp(td->io_ops->name, "io_uring_cmd") &&
625 o->cmd_type == FIO_URING_CMD_NVME)
626 fio_ioring_cmd_nvme_pi(td, io_u);
628 ring->array[tail & ld->sq_ring_mask] = io_u->index;
629 atomic_store_release(ring->tail, next_tail);
635 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
637 struct ioring_data *ld = td->io_ops_data;
640 if (!fio_fill_issue_time(td))
643 fio_gettime(&now, NULL);
646 struct io_sq_ring *ring = &ld->sq_ring;
647 int index = ring->array[start & ld->sq_ring_mask];
648 struct io_u *io_u = ld->io_u_index[index];
650 memcpy(&io_u->issue_time, &now, sizeof(now));
651 io_u_queued(td, io_u);
657 * only used for iolog
659 if (td->o.read_iolog_file)
660 memcpy(&td->last_issue, &now, sizeof(now));
663 static int fio_ioring_commit(struct thread_data *td)
665 struct ioring_data *ld = td->io_ops_data;
666 struct ioring_options *o = td->eo;
673 * Kernel side does submission. just need to check if the ring is
674 * flagged as needing a kick, if so, call io_uring_enter(). This
675 * only happens if we've been idle too long.
677 if (o->sqpoll_thread) {
678 struct io_sq_ring *ring = &ld->sq_ring;
679 unsigned start = *ld->sq_ring.tail - ld->queued;
682 flags = atomic_load_relaxed(ring->flags);
683 if (flags & IORING_SQ_NEED_WAKEUP)
684 io_uring_enter(ld, ld->queued, 0,
685 IORING_ENTER_SQ_WAKEUP);
686 fio_ioring_queued(td, start, ld->queued);
687 io_u_mark_submit(td, ld->queued);
694 unsigned start = *ld->sq_ring.head;
695 long nr = ld->queued;
697 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
699 fio_ioring_queued(td, start, ret);
700 io_u_mark_submit(td, ret);
705 io_u_mark_submit(td, ret);
708 if (errno == EAGAIN || errno == EINTR) {
709 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
712 /* Shouldn't happen */
717 td_verror(td, errno, "io_uring_enter submit");
720 } while (ld->queued);
725 static void fio_ioring_unmap(struct ioring_data *ld)
729 for (i = 0; i < FIO_ARRAY_SIZE(ld->mmap); i++)
730 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
734 static void fio_ioring_cleanup(struct thread_data *td)
736 struct ioring_data *ld = td->io_ops_data;
739 if (!(td->flags & TD_F_CHILD))
740 fio_ioring_unmap(ld);
742 fio_cmdprio_cleanup(&ld->cmdprio);
743 free(ld->io_u_index);
752 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
754 struct io_sq_ring *sring = &ld->sq_ring;
755 struct io_cq_ring *cring = &ld->cq_ring;
758 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
759 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
760 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
762 ld->mmap[0].ptr = ptr;
763 sring->head = ptr + p->sq_off.head;
764 sring->tail = ptr + p->sq_off.tail;
765 sring->ring_mask = ptr + p->sq_off.ring_mask;
766 sring->ring_entries = ptr + p->sq_off.ring_entries;
767 sring->flags = ptr + p->sq_off.flags;
768 sring->array = ptr + p->sq_off.array;
769 ld->sq_ring_mask = *sring->ring_mask;
771 if (p->flags & IORING_SETUP_SQE128)
772 ld->mmap[1].len = 2 * p->sq_entries * sizeof(struct io_uring_sqe);
774 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
775 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
776 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
778 ld->mmap[1].ptr = ld->sqes;
780 if (p->flags & IORING_SETUP_CQE32) {
781 ld->mmap[2].len = p->cq_off.cqes +
782 2 * p->cq_entries * sizeof(struct io_uring_cqe);
784 ld->mmap[2].len = p->cq_off.cqes +
785 p->cq_entries * sizeof(struct io_uring_cqe);
787 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
788 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
790 ld->mmap[2].ptr = ptr;
791 cring->head = ptr + p->cq_off.head;
792 cring->tail = ptr + p->cq_off.tail;
793 cring->ring_mask = ptr + p->cq_off.ring_mask;
794 cring->ring_entries = ptr + p->cq_off.ring_entries;
795 cring->cqes = ptr + p->cq_off.cqes;
796 ld->cq_ring_mask = *cring->ring_mask;
800 static void fio_ioring_probe(struct thread_data *td)
802 struct ioring_data *ld = td->io_ops_data;
803 struct ioring_options *o = td->eo;
804 struct io_uring_probe *p;
807 /* already set by user, don't touch */
808 if (o->nonvectored != -1)
811 /* default to off, as that's always safe */
814 p = calloc(1, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
818 ret = syscall(__NR_io_uring_register, ld->ring_fd,
819 IORING_REGISTER_PROBE, p, 256);
823 if (IORING_OP_WRITE > p->ops_len)
826 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED) &&
827 (p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED))
833 static int fio_ioring_queue_init(struct thread_data *td)
835 struct ioring_data *ld = td->io_ops_data;
836 struct ioring_options *o = td->eo;
837 int depth = td->o.iodepth;
838 struct io_uring_params p;
841 memset(&p, 0, sizeof(p));
844 p.flags |= IORING_SETUP_IOPOLL;
845 if (o->sqpoll_thread) {
846 p.flags |= IORING_SETUP_SQPOLL;
848 p.flags |= IORING_SETUP_SQ_AFF;
849 p.sq_thread_cpu = o->sqpoll_cpu;
853 * Submission latency for sqpoll_thread is just the time it
854 * takes to fill in the SQ ring entries, and any syscall if
855 * IORING_SQ_NEED_WAKEUP is set, we don't need to log that time
858 td->o.disable_slat = 1;
862 * Clamp CQ ring size at our SQ ring size, we don't need more entries
865 p.flags |= IORING_SETUP_CQSIZE;
866 p.cq_entries = depth;
869 * Setup COOP_TASKRUN as we don't need to get IPI interrupted for
870 * completing IO operations.
872 p.flags |= IORING_SETUP_COOP_TASKRUN;
875 * io_uring is always a single issuer, and we can defer task_work
876 * runs until we reap events.
878 p.flags |= IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN;
881 ret = syscall(__NR_io_uring_setup, depth, &p);
883 if (errno == EINVAL && p.flags & IORING_SETUP_DEFER_TASKRUN) {
884 p.flags &= ~IORING_SETUP_DEFER_TASKRUN;
885 p.flags &= ~IORING_SETUP_SINGLE_ISSUER;
888 if (errno == EINVAL && p.flags & IORING_SETUP_COOP_TASKRUN) {
889 p.flags &= ~IORING_SETUP_COOP_TASKRUN;
892 if (errno == EINVAL && p.flags & IORING_SETUP_CQSIZE) {
893 p.flags &= ~IORING_SETUP_CQSIZE;
901 fio_ioring_probe(td);
904 ret = syscall(__NR_io_uring_register, ld->ring_fd,
905 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
910 return fio_ioring_mmap(ld, &p);
913 static int fio_ioring_cmd_queue_init(struct thread_data *td)
915 struct ioring_data *ld = td->io_ops_data;
916 struct ioring_options *o = td->eo;
917 int depth = td->o.iodepth;
918 struct io_uring_params p;
921 memset(&p, 0, sizeof(p));
924 p.flags |= IORING_SETUP_IOPOLL;
925 if (o->sqpoll_thread) {
926 p.flags |= IORING_SETUP_SQPOLL;
928 p.flags |= IORING_SETUP_SQ_AFF;
929 p.sq_thread_cpu = o->sqpoll_cpu;
933 * Submission latency for sqpoll_thread is just the time it
934 * takes to fill in the SQ ring entries, and any syscall if
935 * IORING_SQ_NEED_WAKEUP is set, we don't need to log that time
938 td->o.disable_slat = 1;
940 if (o->cmd_type == FIO_URING_CMD_NVME) {
941 p.flags |= IORING_SETUP_SQE128;
942 p.flags |= IORING_SETUP_CQE32;
946 * Clamp CQ ring size at our SQ ring size, we don't need more entries
949 p.flags |= IORING_SETUP_CQSIZE;
950 p.cq_entries = depth;
953 * Setup COOP_TASKRUN as we don't need to get IPI interrupted for
954 * completing IO operations.
956 p.flags |= IORING_SETUP_COOP_TASKRUN;
959 * io_uring is always a single issuer, and we can defer task_work
960 * runs until we reap events.
962 p.flags |= IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN;
965 ret = syscall(__NR_io_uring_setup, depth, &p);
967 if (errno == EINVAL && p.flags & IORING_SETUP_DEFER_TASKRUN) {
968 p.flags &= ~IORING_SETUP_DEFER_TASKRUN;
969 p.flags &= ~IORING_SETUP_SINGLE_ISSUER;
972 if (errno == EINVAL && p.flags & IORING_SETUP_COOP_TASKRUN) {
973 p.flags &= ~IORING_SETUP_COOP_TASKRUN;
976 if (errno == EINVAL && p.flags & IORING_SETUP_CQSIZE) {
977 p.flags &= ~IORING_SETUP_CQSIZE;
985 fio_ioring_probe(td);
988 ret = syscall(__NR_io_uring_register, ld->ring_fd,
989 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
994 return fio_ioring_mmap(ld, &p);
997 static int fio_ioring_register_files(struct thread_data *td)
999 struct ioring_data *ld = td->io_ops_data;
1004 ld->fds = calloc(td->o.nr_files, sizeof(int));
1006 for_each_file(td, f, i) {
1007 ret = generic_open_file(td, f);
1014 ret = syscall(__NR_io_uring_register, ld->ring_fd,
1015 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
1023 * Pretend the file is closed again, and really close it if we hit
1026 for_each_file(td, f, i) {
1028 int fio_unused ret2;
1029 ret2 = generic_close_file(td, f);
1037 static int fio_ioring_post_init(struct thread_data *td)
1039 struct ioring_data *ld = td->io_ops_data;
1040 struct ioring_options *o = td->eo;
1044 for (i = 0; i < td->o.iodepth; i++) {
1045 struct iovec *iov = &ld->iovecs[i];
1047 io_u = ld->io_u_index[i];
1048 iov->iov_base = io_u->buf;
1049 iov->iov_len = td_max_bs(td);
1052 err = fio_ioring_queue_init(td);
1054 int init_err = errno;
1056 if (init_err == ENOSYS)
1057 log_err("fio: your kernel doesn't support io_uring\n");
1058 td_verror(td, init_err, "io_queue_init");
1062 for (i = 0; i < td->o.iodepth; i++) {
1063 struct io_uring_sqe *sqe;
1066 memset(sqe, 0, sizeof(*sqe));
1069 if (o->registerfiles) {
1070 err = fio_ioring_register_files(td);
1072 td_verror(td, errno, "ioring_register_files");
1080 static int fio_ioring_cmd_post_init(struct thread_data *td)
1082 struct ioring_data *ld = td->io_ops_data;
1083 struct ioring_options *o = td->eo;
1087 for (i = 0; i < td->o.iodepth; i++) {
1088 struct iovec *iov = &ld->iovecs[i];
1090 io_u = ld->io_u_index[i];
1091 iov->iov_base = io_u->buf;
1092 iov->iov_len = td_max_bs(td);
1095 err = fio_ioring_cmd_queue_init(td);
1097 int init_err = errno;
1099 td_verror(td, init_err, "io_queue_init");
1103 for (i = 0; i < td->o.iodepth; i++) {
1104 struct io_uring_sqe *sqe;
1106 if (o->cmd_type == FIO_URING_CMD_NVME) {
1107 sqe = &ld->sqes[i << 1];
1108 memset(sqe, 0, 2 * sizeof(*sqe));
1111 memset(sqe, 0, sizeof(*sqe));
1115 if (o->registerfiles) {
1116 err = fio_ioring_register_files(td);
1118 td_verror(td, errno, "ioring_register_files");
1126 static void parse_prchk_flags(struct ioring_options *o)
1131 if (strstr(o->pi_chk, "GUARD") != NULL)
1132 o->prchk = NVME_IO_PRINFO_PRCHK_GUARD;
1133 if (strstr(o->pi_chk, "REFTAG") != NULL)
1134 o->prchk |= NVME_IO_PRINFO_PRCHK_REF;
1135 if (strstr(o->pi_chk, "APPTAG") != NULL)
1136 o->prchk |= NVME_IO_PRINFO_PRCHK_APP;
1139 static int fio_ioring_init(struct thread_data *td)
1141 struct ioring_options *o = td->eo;
1142 struct ioring_data *ld;
1143 struct nvme_dsm *dsm;
1145 unsigned int dsm_size;
1146 unsigned long long md_size;
1149 /* sqthread submission requires registered files */
1150 if (o->sqpoll_thread)
1151 o->registerfiles = 1;
1153 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
1154 log_err("fio: io_uring registered files require nr_files to "
1155 "be identical to open_files\n");
1159 ld = calloc(1, sizeof(*ld));
1161 /* ring depth must be a power-of-2 */
1162 ld->iodepth = td->o.iodepth;
1163 td->o.iodepth = roundup_pow2(td->o.iodepth);
1166 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
1169 * metadata buffer for nvme command.
1170 * We are only supporting iomem=malloc / mem=malloc as of now.
1172 if (!strcmp(td->io_ops->name, "io_uring_cmd") &&
1173 (o->cmd_type == FIO_URING_CMD_NVME) && o->md_per_io_size) {
1174 md_size = (unsigned long long) o->md_per_io_size
1175 * (unsigned long long) td->o.iodepth;
1176 md_size += page_mask + td->o.mem_align;
1177 if (td->o.mem_align && td->o.mem_align > page_size)
1178 md_size += td->o.mem_align - page_size;
1179 if (td->o.mem_type == MEM_MALLOC) {
1180 ld->md_buf = malloc(md_size);
1186 log_err("fio: Only iomem=malloc or mem=malloc is supported\n");
1191 parse_prchk_flags(o);
1193 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
1195 td->io_ops_data = ld;
1197 ret = fio_cmdprio_init(td, &ld->cmdprio, &o->cmdprio_options);
1199 td_verror(td, EINVAL, "fio_ioring_init");
1204 * For io_uring_cmd, trims are async operations unless we are operating
1205 * in zbd mode where trim means zone reset.
1207 if (!strcmp(td->io_ops->name, "io_uring_cmd") && td_trim(td) &&
1208 td->o.zone_mode == ZONE_MODE_ZBD) {
1209 td->io_ops->flags |= FIO_ASYNCIO_SYNC_TRIM;
1211 dsm_size = sizeof(*ld->dsm) +
1212 td->o.num_range * sizeof(struct nvme_dsm_range);
1213 ld->dsm = calloc(td->o.iodepth, dsm_size);
1215 for (i = 0; i < td->o.iodepth; i++) {
1216 dsm = (struct nvme_dsm *)ptr;
1217 dsm->nr_ranges = td->o.num_range;
1225 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
1227 struct ioring_data *ld = td->io_ops_data;
1228 struct ioring_options *o = td->eo;
1229 struct nvme_pi_data *pi_data;
1232 ld->io_u_index[io_u->index] = io_u;
1234 if (!strcmp(td->io_ops->name, "io_uring_cmd")) {
1235 p = PTR_ALIGN(ld->md_buf, page_mask) + td->o.mem_align;
1236 p += o->md_per_io_size * io_u->index;
1237 io_u->mmap_data = p;
1240 pi_data = calloc(1, sizeof(*pi_data));
1241 pi_data->io_flags |= o->prchk;
1242 pi_data->apptag_mask = o->apptag_mask;
1243 pi_data->apptag = o->apptag;
1244 io_u->engine_data = pi_data;
1251 static void fio_ioring_io_u_free(struct thread_data *td, struct io_u *io_u)
1253 struct ioring_options *o = td->eo;
1256 if (!strcmp(td->io_ops->name, "io_uring_cmd") &&
1257 (o->cmd_type == FIO_URING_CMD_NVME)) {
1258 pi = io_u->engine_data;
1260 io_u->engine_data = NULL;
1264 static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
1266 struct ioring_data *ld = td->io_ops_data;
1267 struct ioring_options *o = td->eo;
1269 if (!ld || !o->registerfiles)
1270 return generic_open_file(td, f);
1272 f->fd = ld->fds[f->engine_pos];
1276 static int fio_ioring_cmd_open_file(struct thread_data *td, struct fio_file *f)
1278 struct ioring_data *ld = td->io_ops_data;
1279 struct ioring_options *o = td->eo;
1281 if (o->cmd_type == FIO_URING_CMD_NVME) {
1282 struct nvme_data *data = NULL;
1283 unsigned int lba_size = 0;
1287 /* Store the namespace-id and lba size. */
1288 data = FILE_ENG_DATA(f);
1290 data = calloc(1, sizeof(struct nvme_data));
1291 ret = fio_nvme_get_info(f, &nlba, o->pi_act, data);
1297 FILE_SET_ENG_DATA(f, data);
1300 lba_size = data->lba_ext ? data->lba_ext : data->lba_size;
1302 for_each_rw_ddir(ddir) {
1303 if (td->o.min_bs[ddir] % lba_size || td->o.max_bs[ddir] % lba_size) {
1304 if (data->lba_ext) {
1305 log_err("%s: block size must be a multiple of %u "
1306 "(LBA data size + Metadata size)\n", f->file_name, lba_size);
1307 if (td->o.min_bs[ddir] == td->o.max_bs[ddir] &&
1308 !(td->o.min_bs[ddir] % data->lba_size)) {
1309 /* fixed block size is actually a multiple of LBA data size */
1310 unsigned long long suggestion = lba_size *
1311 (td->o.min_bs[ddir] / data->lba_size);
1312 log_err("Did you mean to use a block size of %llu?\n", suggestion);
1315 log_err("%s: block size must be a multiple of LBA data size\n",
1318 td_verror(td, EINVAL, "fio_ioring_cmd_open_file");
1321 if (data->ms && !data->lba_ext && ddir != DDIR_TRIM &&
1322 (o->md_per_io_size < ((td->o.max_bs[ddir] / data->lba_size) *
1324 log_err("%s: md_per_io_size should be at least %llu bytes\n",
1326 ((td->o.max_bs[ddir] / data->lba_size) * data->ms));
1327 td_verror(td, EINVAL, "fio_ioring_cmd_open_file");
1333 * For extended logical block sizes we cannot use verify when
1334 * end to end data protection checks are enabled, as the PI
1335 * section of data buffer conflicts with verify.
1337 if (data->ms && data->pi_type && data->lba_ext &&
1338 td->o.verify != VERIFY_NONE) {
1339 log_err("%s: for extended LBA, verify cannot be used when E2E data protection is enabled\n",
1341 td_verror(td, EINVAL, "fio_ioring_cmd_open_file");
1345 if (!ld || !o->registerfiles)
1346 return generic_open_file(td, f);
1348 f->fd = ld->fds[f->engine_pos];
1352 static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
1354 struct ioring_data *ld = td->io_ops_data;
1355 struct ioring_options *o = td->eo;
1357 if (!ld || !o->registerfiles)
1358 return generic_close_file(td, f);
1364 static int fio_ioring_cmd_close_file(struct thread_data *td,
1367 struct ioring_data *ld = td->io_ops_data;
1368 struct ioring_options *o = td->eo;
1370 if (o->cmd_type == FIO_URING_CMD_NVME) {
1371 struct nvme_data *data = FILE_ENG_DATA(f);
1373 FILE_SET_ENG_DATA(f, NULL);
1376 if (!ld || !o->registerfiles)
1377 return generic_close_file(td, f);
1383 static int fio_ioring_cmd_get_file_size(struct thread_data *td,
1386 struct ioring_options *o = td->eo;
1388 if (fio_file_size_known(f))
1391 if (o->cmd_type == FIO_URING_CMD_NVME) {
1392 struct nvme_data *data = NULL;
1396 data = calloc(1, sizeof(struct nvme_data));
1397 ret = fio_nvme_get_info(f, &nlba, o->pi_act, data);
1403 f->real_file_size = data->lba_size * nlba;
1404 fio_file_set_size_known(f);
1406 FILE_SET_ENG_DATA(f, data);
1409 return generic_get_file_size(td, f);
1412 static int fio_ioring_cmd_get_zoned_model(struct thread_data *td,
1414 enum zbd_zoned_model *model)
1416 return fio_nvme_get_zoned_model(td, f, model);
1419 static int fio_ioring_cmd_report_zones(struct thread_data *td,
1420 struct fio_file *f, uint64_t offset,
1421 struct zbd_zone *zbdz,
1422 unsigned int nr_zones)
1424 return fio_nvme_report_zones(td, f, offset, zbdz, nr_zones);
1427 static int fio_ioring_cmd_reset_wp(struct thread_data *td, struct fio_file *f,
1428 uint64_t offset, uint64_t length)
1430 return fio_nvme_reset_wp(td, f, offset, length);
1433 static int fio_ioring_cmd_get_max_open_zones(struct thread_data *td,
1435 unsigned int *max_open_zones)
1437 return fio_nvme_get_max_open_zones(td, f, max_open_zones);
1440 static int fio_ioring_cmd_fetch_ruhs(struct thread_data *td, struct fio_file *f,
1441 struct fio_ruhs_info *fruhs_info)
1443 struct nvme_fdp_ruh_status *ruhs;
1446 bytes = sizeof(*ruhs) + FDP_MAX_RUHS * sizeof(struct nvme_fdp_ruh_status_desc);
1447 ruhs = scalloc(1, bytes);
1451 ret = fio_nvme_iomgmt_ruhs(td, f, ruhs, bytes);
1455 fruhs_info->nr_ruhs = le16_to_cpu(ruhs->nruhsd);
1456 for (i = 0; i < fruhs_info->nr_ruhs; i++)
1457 fruhs_info->plis[i] = le16_to_cpu(ruhs->ruhss[i].pid);
1463 static struct ioengine_ops ioengine_uring = {
1465 .version = FIO_IOOPS_VERSION,
1466 .flags = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD |
1467 FIO_ASYNCIO_SETS_ISSUE_TIME,
1468 .init = fio_ioring_init,
1469 .post_init = fio_ioring_post_init,
1470 .io_u_init = fio_ioring_io_u_init,
1471 .prep = fio_ioring_prep,
1472 .queue = fio_ioring_queue,
1473 .commit = fio_ioring_commit,
1474 .getevents = fio_ioring_getevents,
1475 .event = fio_ioring_event,
1476 .cleanup = fio_ioring_cleanup,
1477 .open_file = fio_ioring_open_file,
1478 .close_file = fio_ioring_close_file,
1479 .get_file_size = generic_get_file_size,
1481 .option_struct_size = sizeof(struct ioring_options),
1484 static struct ioengine_ops ioengine_uring_cmd = {
1485 .name = "io_uring_cmd",
1486 .version = FIO_IOOPS_VERSION,
1487 .flags = FIO_NO_OFFLOAD | FIO_MEMALIGN | FIO_RAWIO |
1488 FIO_ASYNCIO_SETS_ISSUE_TIME |
1489 FIO_MULTI_RANGE_TRIM,
1490 .init = fio_ioring_init,
1491 .post_init = fio_ioring_cmd_post_init,
1492 .io_u_init = fio_ioring_io_u_init,
1493 .io_u_free = fio_ioring_io_u_free,
1494 .prep = fio_ioring_cmd_prep,
1495 .queue = fio_ioring_queue,
1496 .commit = fio_ioring_commit,
1497 .getevents = fio_ioring_getevents,
1498 .event = fio_ioring_cmd_event,
1499 .cleanup = fio_ioring_cleanup,
1500 .open_file = fio_ioring_cmd_open_file,
1501 .close_file = fio_ioring_cmd_close_file,
1502 .get_file_size = fio_ioring_cmd_get_file_size,
1503 .get_zoned_model = fio_ioring_cmd_get_zoned_model,
1504 .report_zones = fio_ioring_cmd_report_zones,
1505 .reset_wp = fio_ioring_cmd_reset_wp,
1506 .get_max_open_zones = fio_ioring_cmd_get_max_open_zones,
1508 .option_struct_size = sizeof(struct ioring_options),
1509 .fdp_fetch_ruhs = fio_ioring_cmd_fetch_ruhs,
1512 static void fio_init fio_ioring_register(void)
1514 register_ioengine(&ioengine_uring);
1515 register_ioengine(&ioengine_uring_cmd);
1518 static void fio_exit fio_ioring_unregister(void)
1520 unregister_ioengine(&ioengine_uring);
1521 unregister_ioengine(&ioengine_uring_cmd);