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