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