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" |
52885fa2 | 20 | |
bffad86f | 21 | #ifdef ARCH_HAVE_IOURING |
52885fa2 | 22 | |
57fa61f0 | 23 | #include "../lib/types.h" |
f3e769a4 | 24 | #include "../os/linux/io_uring.h" |
9a2d78b3 | 25 | |
bffad86f | 26 | struct io_sq_ring { |
e2239016 JA |
27 | unsigned *head; |
28 | unsigned *tail; | |
29 | unsigned *ring_mask; | |
30 | unsigned *ring_entries; | |
31 | unsigned *flags; | |
32 | unsigned *array; | |
52885fa2 JA |
33 | }; |
34 | ||
bffad86f | 35 | struct io_cq_ring { |
e2239016 JA |
36 | unsigned *head; |
37 | unsigned *tail; | |
38 | unsigned *ring_mask; | |
39 | unsigned *ring_entries; | |
f0403f94 | 40 | struct io_uring_cqe *cqes; |
9a2d78b3 JA |
41 | }; |
42 | ||
bffad86f | 43 | struct ioring_mmap { |
9a2d78b3 JA |
44 | void *ptr; |
45 | size_t len; | |
52885fa2 JA |
46 | }; |
47 | ||
bffad86f | 48 | struct ioring_data { |
9a2d78b3 JA |
49 | int ring_fd; |
50 | ||
52885fa2 JA |
51 | struct io_u **io_u_index; |
52 | ||
5ffd5626 JA |
53 | int *fds; |
54 | ||
bffad86f | 55 | struct io_sq_ring sq_ring; |
f0403f94 | 56 | struct io_uring_sqe *sqes; |
9a2d78b3 | 57 | struct iovec *iovecs; |
b87aa01a | 58 | unsigned sq_ring_mask; |
52885fa2 | 59 | |
bffad86f | 60 | struct io_cq_ring cq_ring; |
b87aa01a | 61 | unsigned cq_ring_mask; |
52885fa2 JA |
62 | |
63 | int queued; | |
64 | int cq_ring_off; | |
b87aa01a | 65 | unsigned iodepth; |
96563db9 | 66 | |
bffad86f | 67 | struct ioring_mmap mmap[3]; |
52885fa2 JA |
68 | }; |
69 | ||
bffad86f | 70 | struct ioring_options { |
52885fa2 JA |
71 | void *pad; |
72 | unsigned int hipri; | |
73 | unsigned int fixedbufs; | |
5ffd5626 | 74 | unsigned int registerfiles; |
3d7d00a3 | 75 | unsigned int sqpoll_thread; |
2ea53ca3 JA |
76 | unsigned int sqpoll_set; |
77 | unsigned int sqpoll_cpu; | |
b10b1e70 | 78 | unsigned int nonvectored; |
4a87b584 | 79 | unsigned int uncached; |
52885fa2 JA |
80 | }; |
81 | ||
b10b1e70 JA |
82 | static const int ddir_to_op[2][2] = { |
83 | { IORING_OP_READV, IORING_OP_READ }, | |
84 | { IORING_OP_WRITEV, IORING_OP_WRITE } | |
85 | }; | |
86 | ||
3f1e3af7 KB |
87 | static const int fixed_ddir_to_op[2] = { |
88 | IORING_OP_READ_FIXED, | |
89 | IORING_OP_WRITE_FIXED | |
90 | }; | |
91 | ||
2ea53ca3 | 92 | static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val) |
a90cd050 | 93 | { |
bffad86f | 94 | struct ioring_options *o = data; |
a90cd050 | 95 | |
2ea53ca3 JA |
96 | o->sqpoll_cpu = *val; |
97 | o->sqpoll_set = 1; | |
a90cd050 JA |
98 | return 0; |
99 | } | |
100 | ||
52885fa2 JA |
101 | static struct fio_option options[] = { |
102 | { | |
103 | .name = "hipri", | |
104 | .lname = "High Priority", | |
105 | .type = FIO_OPT_STR_SET, | |
bffad86f | 106 | .off1 = offsetof(struct ioring_options, hipri), |
52885fa2 JA |
107 | .help = "Use polled IO completions", |
108 | .category = FIO_OPT_C_ENGINE, | |
27f436d9 | 109 | .group = FIO_OPT_G_IOURING, |
52885fa2 JA |
110 | }, |
111 | { | |
112 | .name = "fixedbufs", | |
113 | .lname = "Fixed (pre-mapped) IO buffers", | |
114 | .type = FIO_OPT_STR_SET, | |
bffad86f | 115 | .off1 = offsetof(struct ioring_options, fixedbufs), |
52885fa2 JA |
116 | .help = "Pre map IO buffers", |
117 | .category = FIO_OPT_C_ENGINE, | |
27f436d9 | 118 | .group = FIO_OPT_G_IOURING, |
52885fa2 | 119 | }, |
5ffd5626 JA |
120 | { |
121 | .name = "registerfiles", | |
122 | .lname = "Register file set", | |
123 | .type = FIO_OPT_STR_SET, | |
124 | .off1 = offsetof(struct ioring_options, registerfiles), | |
125 | .help = "Pre-open/register files", | |
126 | .category = FIO_OPT_C_ENGINE, | |
27f436d9 | 127 | .group = FIO_OPT_G_IOURING, |
5ffd5626 | 128 | }, |
771c9901 JA |
129 | { |
130 | .name = "sqthread_poll", | |
3d7d00a3 JA |
131 | .lname = "Kernel SQ thread polling", |
132 | .type = FIO_OPT_INT, | |
133 | .off1 = offsetof(struct ioring_options, sqpoll_thread), | |
134 | .help = "Offload submission/completion to kernel thread", | |
135 | .category = FIO_OPT_C_ENGINE, | |
27f436d9 | 136 | .group = FIO_OPT_G_IOURING, |
3d7d00a3 JA |
137 | }, |
138 | { | |
139 | .name = "sqthread_poll_cpu", | |
140 | .lname = "SQ Thread Poll CPU", | |
2ea53ca3 JA |
141 | .type = FIO_OPT_INT, |
142 | .cb = fio_ioring_sqpoll_cb, | |
3d7d00a3 | 143 | .help = "What CPU to run SQ thread polling on", |
a90cd050 | 144 | .category = FIO_OPT_C_ENGINE, |
27f436d9 | 145 | .group = FIO_OPT_G_IOURING, |
a90cd050 | 146 | }, |
b10b1e70 JA |
147 | { |
148 | .name = "nonvectored", | |
149 | .lname = "Non-vectored", | |
150 | .type = FIO_OPT_INT, | |
151 | .off1 = offsetof(struct ioring_options, nonvectored), | |
152 | .help = "Use non-vectored read/write commands", | |
153 | .category = FIO_OPT_C_ENGINE, | |
154 | .group = FIO_OPT_G_IOURING, | |
155 | }, | |
4a87b584 JA |
156 | { |
157 | .name = "uncached", | |
158 | .lname = "Uncached", | |
159 | .type = FIO_OPT_INT, | |
160 | .off1 = offsetof(struct ioring_options, uncached), | |
161 | .help = "Use RWF_UNCACHED for buffered read/writes", | |
162 | .category = FIO_OPT_C_ENGINE, | |
163 | .group = FIO_OPT_G_IOURING, | |
164 | }, | |
52885fa2 JA |
165 | { |
166 | .name = NULL, | |
167 | }, | |
168 | }; | |
169 | ||
bffad86f | 170 | static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit, |
52885fa2 JA |
171 | unsigned int min_complete, unsigned int flags) |
172 | { | |
9a2d78b3 | 173 | return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit, |
521164fa | 174 | min_complete, flags, NULL, 0); |
52885fa2 JA |
175 | } |
176 | ||
bffad86f | 177 | static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u) |
52885fa2 | 178 | { |
bffad86f | 179 | struct ioring_data *ld = td->io_ops_data; |
cfcc8564 | 180 | struct ioring_options *o = td->eo; |
52885fa2 | 181 | struct fio_file *f = io_u->file; |
f0403f94 | 182 | struct io_uring_sqe *sqe; |
52885fa2 | 183 | |
f0403f94 | 184 | sqe = &ld->sqes[io_u->index]; |
34d6090e AF |
185 | |
186 | /* zero out fields not used in this submission */ | |
187 | memset(sqe, 0, sizeof(*sqe)); | |
188 | ||
5ffd5626 JA |
189 | if (o->registerfiles) { |
190 | sqe->fd = f->engine_pos; | |
191 | sqe->flags = IOSQE_FIXED_FILE; | |
192 | } else { | |
193 | sqe->fd = f->fd; | |
5ffd5626 | 194 | } |
52885fa2 | 195 | |
e3970057 | 196 | if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) { |
f0403f94 | 197 | if (o->fixedbufs) { |
3f1e3af7 | 198 | sqe->opcode = fixed_ddir_to_op[io_u->ddir]; |
919850d2 | 199 | sqe->addr = (unsigned long) io_u->xfer_buf; |
f0403f94 | 200 | sqe->len = io_u->xfer_buflen; |
2ea53ca3 | 201 | sqe->buf_index = io_u->index; |
cfcc8564 | 202 | } else { |
3f1e3af7 | 203 | sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored]; |
b10b1e70 JA |
204 | if (o->nonvectored) { |
205 | sqe->addr = (unsigned long) | |
206 | ld->iovecs[io_u->index].iov_base; | |
207 | sqe->len = ld->iovecs[io_u->index].iov_len; | |
208 | } else { | |
209 | sqe->addr = (unsigned long) &ld->iovecs[io_u->index]; | |
210 | sqe->len = 1; | |
211 | } | |
cfcc8564 | 212 | } |
4a87b584 JA |
213 | if (!td->o.odirect && o->uncached) |
214 | sqe->rw_flags = RWF_UNCACHED; | |
b7ed2a86 JA |
215 | if (fio_option_is_set(&td->o, ioprio_class)) |
216 | sqe->ioprio = td->o.ioprio_class << 13; | |
217 | if (fio_option_is_set(&td->o, ioprio)) | |
218 | sqe->ioprio |= td->o.ioprio; | |
f0403f94 | 219 | sqe->off = io_u->offset; |
48e698fa | 220 | } else if (ddir_sync(io_u->ddir)) { |
01387bfe AF |
221 | if (io_u->ddir == DDIR_SYNC_FILE_RANGE) { |
222 | sqe->off = f->first_write; | |
223 | sqe->len = f->last_write - f->first_write; | |
224 | sqe->sync_range_flags = td->o.sync_file_range; | |
225 | sqe->opcode = IORING_OP_SYNC_FILE_RANGE; | |
226 | } else { | |
01387bfe AF |
227 | if (io_u->ddir == DDIR_DATASYNC) |
228 | sqe->fsync_flags |= IORING_FSYNC_DATASYNC; | |
229 | sqe->opcode = IORING_OP_FSYNC; | |
230 | } | |
48e698fa | 231 | } |
52885fa2 | 232 | |
48e698fa | 233 | sqe->user_data = (unsigned long) io_u; |
52885fa2 JA |
234 | return 0; |
235 | } | |
236 | ||
bffad86f | 237 | static struct io_u *fio_ioring_event(struct thread_data *td, int event) |
52885fa2 | 238 | { |
bffad86f | 239 | struct ioring_data *ld = td->io_ops_data; |
f0403f94 | 240 | struct io_uring_cqe *cqe; |
52885fa2 | 241 | struct io_u *io_u; |
b87aa01a | 242 | unsigned index; |
52885fa2 | 243 | |
b87aa01a | 244 | index = (event + ld->cq_ring_off) & ld->cq_ring_mask; |
52885fa2 | 245 | |
f0403f94 | 246 | cqe = &ld->cq_ring.cqes[index]; |
e3466352 | 247 | io_u = (struct io_u *) (uintptr_t) cqe->user_data; |
52885fa2 | 248 | |
f0403f94 JA |
249 | if (cqe->res != io_u->xfer_buflen) { |
250 | if (cqe->res > io_u->xfer_buflen) | |
251 | io_u->error = -cqe->res; | |
52885fa2 | 252 | else |
f0403f94 | 253 | io_u->resid = io_u->xfer_buflen - cqe->res; |
52885fa2 JA |
254 | } else |
255 | io_u->error = 0; | |
256 | ||
257 | return io_u; | |
258 | } | |
259 | ||
bffad86f | 260 | static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events, |
52885fa2 JA |
261 | unsigned int max) |
262 | { | |
bffad86f JA |
263 | struct ioring_data *ld = td->io_ops_data; |
264 | struct io_cq_ring *ring = &ld->cq_ring; | |
e2239016 | 265 | unsigned head, reaped = 0; |
52885fa2 | 266 | |
9a2d78b3 | 267 | head = *ring->head; |
52885fa2 JA |
268 | do { |
269 | read_barrier(); | |
9a2d78b3 | 270 | if (head == *ring->tail) |
52885fa2 JA |
271 | break; |
272 | reaped++; | |
273 | head++; | |
52885fa2 JA |
274 | } while (reaped + events < max); |
275 | ||
9a2d78b3 | 276 | *ring->head = head; |
52885fa2 JA |
277 | write_barrier(); |
278 | return reaped; | |
279 | } | |
280 | ||
bffad86f JA |
281 | static int fio_ioring_getevents(struct thread_data *td, unsigned int min, |
282 | unsigned int max, const struct timespec *t) | |
52885fa2 | 283 | { |
bffad86f | 284 | struct ioring_data *ld = td->io_ops_data; |
52885fa2 | 285 | unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min; |
bffad86f JA |
286 | struct ioring_options *o = td->eo; |
287 | struct io_cq_ring *ring = &ld->cq_ring; | |
b87aa01a JA |
288 | unsigned events = 0; |
289 | int r; | |
52885fa2 | 290 | |
9a2d78b3 | 291 | ld->cq_ring_off = *ring->head; |
52885fa2 | 292 | do { |
bffad86f | 293 | r = fio_ioring_cqring_reap(td, events, max); |
52885fa2 JA |
294 | if (r) { |
295 | events += r; | |
f7cbbbf8 ST |
296 | if (actual_min != 0) |
297 | actual_min -= r; | |
52885fa2 JA |
298 | continue; |
299 | } | |
300 | ||
3d7d00a3 | 301 | if (!o->sqpoll_thread) { |
9a2d78b3 JA |
302 | r = io_uring_enter(ld, 0, actual_min, |
303 | IORING_ENTER_GETEVENTS); | |
771c9901 | 304 | if (r < 0) { |
f6abd731 | 305 | if (errno == EAGAIN || errno == EINTR) |
771c9901 | 306 | continue; |
9a2d78b3 | 307 | td_verror(td, errno, "io_uring_enter"); |
771c9901 JA |
308 | break; |
309 | } | |
52885fa2 JA |
310 | } |
311 | } while (events < min); | |
312 | ||
313 | return r < 0 ? r : events; | |
314 | } | |
315 | ||
bffad86f JA |
316 | static enum fio_q_status fio_ioring_queue(struct thread_data *td, |
317 | struct io_u *io_u) | |
52885fa2 | 318 | { |
bffad86f JA |
319 | struct ioring_data *ld = td->io_ops_data; |
320 | struct io_sq_ring *ring = &ld->sq_ring; | |
52885fa2 JA |
321 | unsigned tail, next_tail; |
322 | ||
323 | fio_ro_check(td, io_u); | |
324 | ||
b87aa01a | 325 | if (ld->queued == ld->iodepth) |
52885fa2 JA |
326 | return FIO_Q_BUSY; |
327 | ||
52885fa2 JA |
328 | if (io_u->ddir == DDIR_TRIM) { |
329 | if (ld->queued) | |
330 | return FIO_Q_BUSY; | |
331 | ||
332 | do_io_u_trim(td, io_u); | |
333 | io_u_mark_submit(td, 1); | |
334 | io_u_mark_complete(td, 1); | |
335 | return FIO_Q_COMPLETED; | |
336 | } | |
337 | ||
9a2d78b3 | 338 | tail = *ring->tail; |
52885fa2 | 339 | next_tail = tail + 1; |
52885fa2 | 340 | read_barrier(); |
9a2d78b3 | 341 | if (next_tail == *ring->head) |
52885fa2 JA |
342 | return FIO_Q_BUSY; |
343 | ||
2b9415dd JA |
344 | /* ensure sqe stores are ordered with tail update */ |
345 | write_barrier(); | |
b87aa01a | 346 | ring->array[tail & ld->sq_ring_mask] = io_u->index; |
9a2d78b3 | 347 | *ring->tail = next_tail; |
52885fa2 JA |
348 | write_barrier(); |
349 | ||
350 | ld->queued++; | |
351 | return FIO_Q_QUEUED; | |
352 | } | |
353 | ||
bffad86f | 354 | static void fio_ioring_queued(struct thread_data *td, int start, int nr) |
52885fa2 | 355 | { |
bffad86f | 356 | struct ioring_data *ld = td->io_ops_data; |
52885fa2 JA |
357 | struct timespec now; |
358 | ||
359 | if (!fio_fill_issue_time(td)) | |
360 | return; | |
361 | ||
362 | fio_gettime(&now, NULL); | |
363 | ||
364 | while (nr--) { | |
bffad86f | 365 | struct io_sq_ring *ring = &ld->sq_ring; |
9a2d78b3 | 366 | int index = ring->array[start & ld->sq_ring_mask]; |
f8289afc | 367 | struct io_u *io_u = ld->io_u_index[index]; |
52885fa2 JA |
368 | |
369 | memcpy(&io_u->issue_time, &now, sizeof(now)); | |
370 | io_u_queued(td, io_u); | |
371 | ||
372 | start++; | |
52885fa2 JA |
373 | } |
374 | } | |
375 | ||
bffad86f | 376 | static int fio_ioring_commit(struct thread_data *td) |
52885fa2 | 377 | { |
bffad86f JA |
378 | struct ioring_data *ld = td->io_ops_data; |
379 | struct ioring_options *o = td->eo; | |
52885fa2 JA |
380 | int ret; |
381 | ||
382 | if (!ld->queued) | |
383 | return 0; | |
384 | ||
3d7d00a3 JA |
385 | /* |
386 | * Kernel side does submission. just need to check if the ring is | |
387 | * flagged as needing a kick, if so, call io_uring_enter(). This | |
388 | * only happens if we've been idle too long. | |
389 | */ | |
390 | if (o->sqpoll_thread) { | |
bffad86f | 391 | struct io_sq_ring *ring = &ld->sq_ring; |
4cdbc048 | 392 | |
2ea53ca3 | 393 | read_barrier(); |
9a2d78b3 | 394 | if (*ring->flags & IORING_SQ_NEED_WAKEUP) |
b532dd6d JA |
395 | io_uring_enter(ld, ld->queued, 0, |
396 | IORING_ENTER_SQ_WAKEUP); | |
771c9901 JA |
397 | ld->queued = 0; |
398 | return 0; | |
399 | } | |
400 | ||
52885fa2 | 401 | do { |
9a2d78b3 | 402 | unsigned start = *ld->sq_ring.head; |
52885fa2 JA |
403 | long nr = ld->queued; |
404 | ||
9a2d78b3 | 405 | ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS); |
52885fa2 | 406 | if (ret > 0) { |
bffad86f | 407 | fio_ioring_queued(td, start, ret); |
52885fa2 JA |
408 | io_u_mark_submit(td, ret); |
409 | ||
410 | ld->queued -= ret; | |
411 | ret = 0; | |
a90cd050 JA |
412 | } else if (!ret) { |
413 | io_u_mark_submit(td, ret); | |
52885fa2 | 414 | continue; |
a90cd050 | 415 | } else { |
f6abd731 | 416 | if (errno == EAGAIN || errno == EINTR) { |
bffad86f | 417 | ret = fio_ioring_cqring_reap(td, 0, ld->queued); |
a90cd050 JA |
418 | if (ret) |
419 | continue; | |
420 | /* Shouldn't happen */ | |
421 | usleep(1); | |
422 | continue; | |
52885fa2 | 423 | } |
9a2d78b3 | 424 | td_verror(td, errno, "io_uring_enter submit"); |
52885fa2 | 425 | break; |
a90cd050 | 426 | } |
52885fa2 JA |
427 | } while (ld->queued); |
428 | ||
429 | return ret; | |
430 | } | |
431 | ||
bffad86f | 432 | static void fio_ioring_unmap(struct ioring_data *ld) |
52885fa2 | 433 | { |
9a2d78b3 | 434 | int i; |
52885fa2 | 435 | |
9a2d78b3 JA |
436 | for (i = 0; i < ARRAY_SIZE(ld->mmap); i++) |
437 | munmap(ld->mmap[i].ptr, ld->mmap[i].len); | |
438 | close(ld->ring_fd); | |
b87aa01a JA |
439 | } |
440 | ||
bffad86f | 441 | static void fio_ioring_cleanup(struct thread_data *td) |
52885fa2 | 442 | { |
bffad86f | 443 | struct ioring_data *ld = td->io_ops_data; |
52885fa2 JA |
444 | |
445 | if (ld) { | |
52885fa2 | 446 | if (!(td->flags & TD_F_CHILD)) |
bffad86f | 447 | fio_ioring_unmap(ld); |
9a2d78b3 | 448 | |
52885fa2 | 449 | free(ld->io_u_index); |
9a2d78b3 | 450 | free(ld->iovecs); |
5ffd5626 | 451 | free(ld->fds); |
52885fa2 JA |
452 | free(ld); |
453 | } | |
454 | } | |
455 | ||
bffad86f | 456 | static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p) |
9a2d78b3 | 457 | { |
bffad86f JA |
458 | struct io_sq_ring *sring = &ld->sq_ring; |
459 | struct io_cq_ring *cring = &ld->cq_ring; | |
9a2d78b3 JA |
460 | void *ptr; |
461 | ||
e2239016 | 462 | ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32); |
9a2d78b3 JA |
463 | ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE, |
464 | MAP_SHARED | MAP_POPULATE, ld->ring_fd, | |
465 | IORING_OFF_SQ_RING); | |
466 | ld->mmap[0].ptr = ptr; | |
467 | sring->head = ptr + p->sq_off.head; | |
468 | sring->tail = ptr + p->sq_off.tail; | |
469 | sring->ring_mask = ptr + p->sq_off.ring_mask; | |
470 | sring->ring_entries = ptr + p->sq_off.ring_entries; | |
471 | sring->flags = ptr + p->sq_off.flags; | |
ac122fea | 472 | sring->array = ptr + p->sq_off.array; |
9a2d78b3 JA |
473 | ld->sq_ring_mask = *sring->ring_mask; |
474 | ||
f0403f94 JA |
475 | ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe); |
476 | ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE, | |
9a2d78b3 | 477 | MAP_SHARED | MAP_POPULATE, ld->ring_fd, |
f0403f94 JA |
478 | IORING_OFF_SQES); |
479 | ld->mmap[1].ptr = ld->sqes; | |
9a2d78b3 | 480 | |
f0403f94 JA |
481 | ld->mmap[2].len = p->cq_off.cqes + |
482 | p->cq_entries * sizeof(struct io_uring_cqe); | |
9a2d78b3 JA |
483 | ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE, |
484 | MAP_SHARED | MAP_POPULATE, ld->ring_fd, | |
485 | IORING_OFF_CQ_RING); | |
486 | ld->mmap[2].ptr = ptr; | |
487 | cring->head = ptr + p->cq_off.head; | |
488 | cring->tail = ptr + p->cq_off.tail; | |
489 | cring->ring_mask = ptr + p->cq_off.ring_mask; | |
490 | cring->ring_entries = ptr + p->cq_off.ring_entries; | |
f0403f94 | 491 | cring->cqes = ptr + p->cq_off.cqes; |
9a2d78b3 JA |
492 | ld->cq_ring_mask = *cring->ring_mask; |
493 | return 0; | |
494 | } | |
495 | ||
bffad86f | 496 | static int fio_ioring_queue_init(struct thread_data *td) |
52885fa2 | 497 | { |
bffad86f JA |
498 | struct ioring_data *ld = td->io_ops_data; |
499 | struct ioring_options *o = td->eo; | |
52885fa2 | 500 | int depth = td->o.iodepth; |
bffad86f | 501 | struct io_uring_params p; |
9a2d78b3 JA |
502 | int ret; |
503 | ||
504 | memset(&p, 0, sizeof(p)); | |
52885fa2 JA |
505 | |
506 | if (o->hipri) | |
bffad86f | 507 | p.flags |= IORING_SETUP_IOPOLL; |
3d7d00a3 JA |
508 | if (o->sqpoll_thread) { |
509 | p.flags |= IORING_SETUP_SQPOLL; | |
510 | if (o->sqpoll_set) { | |
511 | p.flags |= IORING_SETUP_SQ_AFF; | |
512 | p.sq_thread_cpu = o->sqpoll_cpu; | |
513 | } | |
f635f1fb | 514 | } |
a90cd050 | 515 | |
2ea53ca3 | 516 | ret = syscall(__NR_sys_io_uring_setup, depth, &p); |
9a2d78b3 JA |
517 | if (ret < 0) |
518 | return ret; | |
519 | ||
520 | ld->ring_fd = ret; | |
2ea53ca3 JA |
521 | |
522 | if (o->fixedbufs) { | |
2d644205 JA |
523 | struct rlimit rlim = { |
524 | .rlim_cur = RLIM_INFINITY, | |
525 | .rlim_max = RLIM_INFINITY, | |
526 | }; | |
527 | ||
528 | if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) | |
529 | return -1; | |
530 | ||
2ea53ca3 | 531 | ret = syscall(__NR_sys_io_uring_register, ld->ring_fd, |
919850d2 | 532 | IORING_REGISTER_BUFFERS, ld->iovecs, depth); |
2ea53ca3 JA |
533 | if (ret < 0) |
534 | return ret; | |
535 | } | |
536 | ||
bffad86f | 537 | return fio_ioring_mmap(ld, &p); |
52885fa2 JA |
538 | } |
539 | ||
5ffd5626 JA |
540 | static int fio_ioring_register_files(struct thread_data *td) |
541 | { | |
542 | struct ioring_data *ld = td->io_ops_data; | |
543 | struct fio_file *f; | |
544 | unsigned int i; | |
545 | int ret; | |
546 | ||
547 | ld->fds = calloc(td->o.nr_files, sizeof(int)); | |
548 | ||
549 | for_each_file(td, f, i) { | |
550 | ret = generic_open_file(td, f); | |
551 | if (ret) | |
552 | goto err; | |
553 | ld->fds[i] = f->fd; | |
554 | f->engine_pos = i; | |
555 | } | |
556 | ||
557 | ret = syscall(__NR_sys_io_uring_register, ld->ring_fd, | |
558 | IORING_REGISTER_FILES, ld->fds, td->o.nr_files); | |
559 | if (ret) { | |
560 | err: | |
561 | free(ld->fds); | |
562 | ld->fds = NULL; | |
563 | } | |
564 | ||
565 | /* | |
566 | * Pretend the file is closed again, and really close it if we hit | |
567 | * an error. | |
568 | */ | |
569 | for_each_file(td, f, i) { | |
570 | if (ret) { | |
571 | int fio_unused ret2; | |
572 | ret2 = generic_close_file(td, f); | |
573 | } else | |
574 | f->fd = -1; | |
575 | } | |
576 | ||
577 | return ret; | |
578 | } | |
579 | ||
bffad86f | 580 | static int fio_ioring_post_init(struct thread_data *td) |
52885fa2 | 581 | { |
bffad86f | 582 | struct ioring_data *ld = td->io_ops_data; |
5ffd5626 | 583 | struct ioring_options *o = td->eo; |
52885fa2 | 584 | struct io_u *io_u; |
650346e1 | 585 | int err, i; |
52885fa2 | 586 | |
650346e1 JA |
587 | for (i = 0; i < td->o.iodepth; i++) { |
588 | struct iovec *iov = &ld->iovecs[i]; | |
9a2d78b3 | 589 | |
650346e1 JA |
590 | io_u = ld->io_u_index[i]; |
591 | iov->iov_base = io_u->buf; | |
592 | iov->iov_len = td_max_bs(td); | |
52885fa2 JA |
593 | } |
594 | ||
bffad86f | 595 | err = fio_ioring_queue_init(td); |
52885fa2 | 596 | if (err) { |
d63a472d | 597 | td_verror(td, errno, "io_queue_init"); |
52885fa2 JA |
598 | return 1; |
599 | } | |
600 | ||
5ffd5626 JA |
601 | if (o->registerfiles) { |
602 | err = fio_ioring_register_files(td); | |
603 | if (err) { | |
604 | td_verror(td, errno, "ioring_register_files"); | |
605 | return 1; | |
606 | } | |
607 | } | |
608 | ||
52885fa2 JA |
609 | return 0; |
610 | } | |
611 | ||
9a2d78b3 JA |
612 | static unsigned roundup_pow2(unsigned depth) |
613 | { | |
614 | return 1UL << __fls(depth - 1); | |
615 | } | |
616 | ||
bffad86f | 617 | static int fio_ioring_init(struct thread_data *td) |
52885fa2 | 618 | { |
5ffd5626 | 619 | struct ioring_options *o = td->eo; |
bffad86f | 620 | struct ioring_data *ld; |
52885fa2 | 621 | |
5ffd5626 JA |
622 | /* sqthread submission requires registered files */ |
623 | if (o->sqpoll_thread) | |
624 | o->registerfiles = 1; | |
625 | ||
626 | if (o->registerfiles && td->o.nr_files != td->o.open_files) { | |
627 | log_err("fio: io_uring registered files require nr_files to " | |
628 | "be identical to open_files\n"); | |
629 | return 1; | |
630 | } | |
631 | ||
52885fa2 JA |
632 | ld = calloc(1, sizeof(*ld)); |
633 | ||
b87aa01a JA |
634 | /* ring depth must be a power-of-2 */ |
635 | ld->iodepth = td->o.iodepth; | |
636 | td->o.iodepth = roundup_pow2(td->o.iodepth); | |
637 | ||
52885fa2 JA |
638 | /* io_u index */ |
639 | ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *)); | |
650346e1 | 640 | ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec)); |
52885fa2 JA |
641 | |
642 | td->io_ops_data = ld; | |
643 | return 0; | |
644 | } | |
645 | ||
bffad86f | 646 | static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u) |
52885fa2 | 647 | { |
bffad86f | 648 | struct ioring_data *ld = td->io_ops_data; |
52885fa2 JA |
649 | |
650 | ld->io_u_index[io_u->index] = io_u; | |
651 | return 0; | |
652 | } | |
653 | ||
5ffd5626 JA |
654 | static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f) |
655 | { | |
656 | struct ioring_data *ld = td->io_ops_data; | |
657 | struct ioring_options *o = td->eo; | |
658 | ||
17318cf6 | 659 | if (!ld || !o->registerfiles) |
5ffd5626 JA |
660 | return generic_open_file(td, f); |
661 | ||
662 | f->fd = ld->fds[f->engine_pos]; | |
663 | return 0; | |
664 | } | |
665 | ||
666 | static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f) | |
667 | { | |
17318cf6 | 668 | struct ioring_data *ld = td->io_ops_data; |
5ffd5626 JA |
669 | struct ioring_options *o = td->eo; |
670 | ||
17318cf6 | 671 | if (!ld || !o->registerfiles) |
5ffd5626 JA |
672 | return generic_close_file(td, f); |
673 | ||
674 | f->fd = -1; | |
675 | return 0; | |
676 | } | |
677 | ||
52885fa2 | 678 | static struct ioengine_ops ioengine = { |
bffad86f | 679 | .name = "io_uring", |
52885fa2 | 680 | .version = FIO_IOOPS_VERSION, |
04ba61df | 681 | .flags = FIO_ASYNCIO_SYNC_TRIM, |
bffad86f JA |
682 | .init = fio_ioring_init, |
683 | .post_init = fio_ioring_post_init, | |
684 | .io_u_init = fio_ioring_io_u_init, | |
685 | .prep = fio_ioring_prep, | |
686 | .queue = fio_ioring_queue, | |
687 | .commit = fio_ioring_commit, | |
688 | .getevents = fio_ioring_getevents, | |
689 | .event = fio_ioring_event, | |
690 | .cleanup = fio_ioring_cleanup, | |
5ffd5626 JA |
691 | .open_file = fio_ioring_open_file, |
692 | .close_file = fio_ioring_close_file, | |
52885fa2 JA |
693 | .get_file_size = generic_get_file_size, |
694 | .options = options, | |
bffad86f | 695 | .option_struct_size = sizeof(struct ioring_options), |
52885fa2 JA |
696 | }; |
697 | ||
bffad86f | 698 | static void fio_init fio_ioring_register(void) |
52885fa2 | 699 | { |
52885fa2 | 700 | register_ioengine(&ioengine); |
52885fa2 JA |
701 | } |
702 | ||
bffad86f | 703 | static void fio_exit fio_ioring_unregister(void) |
52885fa2 | 704 | { |
52885fa2 | 705 | unregister_ioengine(&ioengine); |
52885fa2 | 706 | } |
1f90e9bb | 707 | #endif |