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