Jens Axboe | e502435 | 2020-02-11 20:34:12 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: MIT */ |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 2 | #include <sys/types.h> |
| 3 | #include <sys/stat.h> |
| 4 | #include <sys/mman.h> |
| 5 | #include <unistd.h> |
| 6 | #include <errno.h> |
| 7 | #include <string.h> |
Jens Axboe | 043ea22 | 2019-06-17 11:41:15 -0600 | [diff] [blame] | 8 | #include <stdbool.h> |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 9 | |
Stefan Hajnoczi | c31c7ec | 2019-07-24 09:24:50 +0100 | [diff] [blame] | 10 | #include "liburing/compat.h" |
| 11 | #include "liburing/io_uring.h" |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 12 | #include "liburing.h" |
Stefan Hajnoczi | c31c7ec | 2019-07-24 09:24:50 +0100 | [diff] [blame] | 13 | #include "liburing/barrier.h" |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 14 | |
Jens Axboe | 96144ea | 2019-12-01 11:21:39 -0700 | [diff] [blame] | 15 | #include "syscall.h" |
| 16 | |
Jens Axboe | 9845510 | 2019-11-27 17:21:38 -0700 | [diff] [blame] | 17 | /* |
| 18 | * Returns true if we're not using SQ thread (thus nobody submits but us) |
| 19 | * or if IORING_SQ_NEED_WAKEUP is set, so submit thread must be explicitly |
| 20 | * awakened. For the latter case, we set the thread wakeup flag. |
| 21 | */ |
| 22 | static inline bool sq_ring_needs_enter(struct io_uring *ring, unsigned *flags) |
| 23 | { |
| 24 | if (!(ring->flags & IORING_SETUP_SQPOLL)) |
| 25 | return true; |
| 26 | if (IO_URING_READ_ONCE(*ring->sq.kflags) & IORING_SQ_NEED_WAKEUP) { |
| 27 | *flags |= IORING_ENTER_SQ_WAKEUP; |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | return false; |
| 32 | } |
| 33 | |
Jens Axboe | 20c9293 | 2019-09-28 05:35:02 -0600 | [diff] [blame] | 34 | int __io_uring_get_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_ptr, |
| 35 | unsigned submit, unsigned wait_nr, sigset_t *sigmask) |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 36 | { |
Jens Axboe | 8ce3a07 | 2019-12-16 12:10:07 -0700 | [diff] [blame] | 37 | struct io_uring_cqe *cqe = NULL; |
Jens Axboe | dc14e30 | 2020-03-02 08:33:17 -0700 | [diff] [blame^] | 38 | const int to_wait = wait_nr; |
Jens Axboe | 7ad0e4b | 2019-12-01 09:11:31 -0700 | [diff] [blame] | 39 | int ret = 0, err; |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 40 | |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 41 | do { |
李通洲 | 38c82de | 2019-12-02 22:36:04 +0800 | [diff] [blame] | 42 | unsigned flags = 0; |
Jens Axboe | 9845510 | 2019-11-27 17:21:38 -0700 | [diff] [blame] | 43 | |
Jens Axboe | 8ce3a07 | 2019-12-16 12:10:07 -0700 | [diff] [blame] | 44 | err = __io_uring_peek_cqe(ring, &cqe); |
Jens Axboe | 7ad0e4b | 2019-12-01 09:11:31 -0700 | [diff] [blame] | 45 | if (err) |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 46 | break; |
Jens Axboe | dc14e30 | 2020-03-02 08:33:17 -0700 | [diff] [blame^] | 47 | if (!cqe && !to_wait && !submit) { |
Jens Axboe | 20c9293 | 2019-09-28 05:35:02 -0600 | [diff] [blame] | 48 | err = -EAGAIN; |
Bart Van Assche | bbb3099 | 2019-07-01 14:42:31 -0700 | [diff] [blame] | 49 | break; |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 50 | } |
Jens Axboe | 7ad0e4b | 2019-12-01 09:11:31 -0700 | [diff] [blame] | 51 | if (wait_nr) |
| 52 | flags = IORING_ENTER_GETEVENTS; |
Jens Axboe | 9845510 | 2019-11-27 17:21:38 -0700 | [diff] [blame] | 53 | if (submit) |
| 54 | sq_ring_needs_enter(ring, &flags); |
Jens Axboe | 7ad0e4b | 2019-12-01 09:11:31 -0700 | [diff] [blame] | 55 | if (wait_nr || submit) |
Jens Axboe | e8493b7 | 2019-12-01 11:30:40 -0700 | [diff] [blame] | 56 | ret = __sys_io_uring_enter(ring->ring_fd, submit, |
| 57 | wait_nr, flags, sigmask); |
Xiaoguang Wang | 8a03150 | 2020-03-02 12:18:11 +0800 | [diff] [blame] | 58 | if (wait_nr) |
| 59 | wait_nr = 0; |
Jens Axboe | dc14e30 | 2020-03-02 08:33:17 -0700 | [diff] [blame^] | 60 | if (ret < 0) { |
Jens Axboe | 20c9293 | 2019-09-28 05:35:02 -0600 | [diff] [blame] | 61 | err = -errno; |
Jens Axboe | dc14e30 | 2020-03-02 08:33:17 -0700 | [diff] [blame^] | 62 | } else if (ret == submit) { |
| 63 | submit = 0; |
| 64 | wait_nr = 0; |
| 65 | } else { |
Jens Axboe | 0edcef5 | 2020-03-02 08:27:03 -0700 | [diff] [blame] | 66 | submit -= ret; |
Jens Axboe | dc14e30 | 2020-03-02 08:33:17 -0700 | [diff] [blame^] | 67 | } |
Jens Axboe | 8ce3a07 | 2019-12-16 12:10:07 -0700 | [diff] [blame] | 68 | if (cqe) |
Jens Axboe | 7ad0e4b | 2019-12-01 09:11:31 -0700 | [diff] [blame] | 69 | break; |
Jens Axboe | 20c9293 | 2019-09-28 05:35:02 -0600 | [diff] [blame] | 70 | } while (!err); |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 71 | |
Jens Axboe | 8ce3a07 | 2019-12-16 12:10:07 -0700 | [diff] [blame] | 72 | *cqe_ptr = cqe; |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 73 | return err; |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* |
James Rouzier | 0b88d72 | 2019-09-25 15:35:06 -0400 | [diff] [blame] | 77 | * Fill in an array of IO completions up to count, if any are available. |
| 78 | * Returns the amount of IO completions filled. |
| 79 | */ |
Jens Axboe | 6d33802 | 2019-09-26 00:41:24 -0600 | [diff] [blame] | 80 | unsigned io_uring_peek_batch_cqe(struct io_uring *ring, |
| 81 | struct io_uring_cqe **cqes, unsigned count) |
James Rouzier | 0b88d72 | 2019-09-25 15:35:06 -0400 | [diff] [blame] | 82 | { |
Jens Axboe | 6d33802 | 2019-09-26 00:41:24 -0600 | [diff] [blame] | 83 | unsigned ready; |
| 84 | |
| 85 | ready = io_uring_cq_ready(ring); |
James Rouzier | 0b88d72 | 2019-09-25 15:35:06 -0400 | [diff] [blame] | 86 | if (ready) { |
James Rouzier | 0b88d72 | 2019-09-25 15:35:06 -0400 | [diff] [blame] | 87 | unsigned head = *ring->cq.khead; |
James Rouzier | 0b88d72 | 2019-09-25 15:35:06 -0400 | [diff] [blame] | 88 | unsigned mask = *ring->cq.kring_mask; |
Jens Axboe | 6d33802 | 2019-09-26 00:41:24 -0600 | [diff] [blame] | 89 | unsigned last; |
James Rouzier | 0b88d72 | 2019-09-25 15:35:06 -0400 | [diff] [blame] | 90 | int i = 0; |
Jens Axboe | 6d33802 | 2019-09-26 00:41:24 -0600 | [diff] [blame] | 91 | |
| 92 | count = count > ready ? ready : count; |
| 93 | last = head + count; |
| 94 | for (;head != last; head++, i++) |
James Rouzier | 0b88d72 | 2019-09-25 15:35:06 -0400 | [diff] [blame] | 95 | cqes[i] = &ring->cq.cqes[head & mask]; |
James Rouzier | 0b88d72 | 2019-09-25 15:35:06 -0400 | [diff] [blame] | 96 | |
| 97 | return count; |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* |
Jens Axboe | c39a058 | 2019-12-19 10:06:28 -0700 | [diff] [blame] | 104 | * Sync internal state with kernel ring state on the SQ side. Returns the |
| 105 | * number of pending items in the SQ ring, for the shared ring. |
Jens Axboe | 8578f0d | 2019-09-27 04:13:42 -0600 | [diff] [blame] | 106 | */ |
Jens Axboe | c39a058 | 2019-12-19 10:06:28 -0700 | [diff] [blame] | 107 | static int __io_uring_flush_sq(struct io_uring *ring) |
Jens Axboe | 8578f0d | 2019-09-27 04:13:42 -0600 | [diff] [blame] | 108 | { |
| 109 | struct io_uring_sq *sq = &ring->sq; |
| 110 | const unsigned mask = *sq->kring_mask; |
Jens Axboe | 1781f0e | 2019-12-11 09:00:43 -0700 | [diff] [blame] | 111 | unsigned ktail, to_submit; |
Jens Axboe | 8578f0d | 2019-09-27 04:13:42 -0600 | [diff] [blame] | 112 | |
Jens Axboe | c39a058 | 2019-12-19 10:06:28 -0700 | [diff] [blame] | 113 | if (sq->sqe_head == sq->sqe_tail) { |
| 114 | ktail = *sq->ktail; |
| 115 | goto out; |
| 116 | } |
Jens Axboe | 8578f0d | 2019-09-27 04:13:42 -0600 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | * Fill in sqes that we have queued up, adding them to the kernel ring |
| 120 | */ |
Jens Axboe | 8578f0d | 2019-09-27 04:13:42 -0600 | [diff] [blame] | 121 | ktail = *sq->ktail; |
| 122 | to_submit = sq->sqe_tail - sq->sqe_head; |
| 123 | while (to_submit--) { |
| 124 | sq->array[ktail & mask] = sq->sqe_head & mask; |
| 125 | ktail++; |
| 126 | sq->sqe_head++; |
Jens Axboe | 8578f0d | 2019-09-27 04:13:42 -0600 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Ensure that the kernel sees the SQE updates before it sees the tail |
| 131 | * update. |
| 132 | */ |
Kornilios Kourtis | f389745 | 2019-10-30 13:25:13 +0100 | [diff] [blame] | 133 | io_uring_smp_store_release(sq->ktail, ktail); |
Jens Axboe | c39a058 | 2019-12-19 10:06:28 -0700 | [diff] [blame] | 134 | out: |
| 135 | return ktail - *sq->khead; |
Jens Axboe | 8578f0d | 2019-09-27 04:13:42 -0600 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 139 | * Like io_uring_wait_cqe(), except it accepts a timeout value as well. Note |
| 140 | * that an sqe is used internally to handle the timeout. Applications using |
| 141 | * this function must never set sqe->user_data to LIBURING_UDATA_TIMEOUT! |
Jens Axboe | 8b93cca | 2019-09-21 14:44:57 -0600 | [diff] [blame] | 142 | * |
| 143 | * Note that the application need not call io_uring_submit() before calling |
Jens Axboe | 217756d | 2019-11-22 21:43:24 -0700 | [diff] [blame] | 144 | * this function, as we will do that on its behalf. From this it also follows |
| 145 | * that this function isn't safe to use for applications that split SQ and CQ |
| 146 | * handling between two threads and expect that to work without synchronization, |
| 147 | * as this function manipulates both the SQ and CQ side. |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 148 | */ |
Jens Axboe | ac72640 | 2019-09-27 07:26:45 -0600 | [diff] [blame] | 149 | int io_uring_wait_cqes(struct io_uring *ring, struct io_uring_cqe **cqe_ptr, |
Jens Axboe | e2934e1 | 2019-10-01 10:05:16 -0600 | [diff] [blame] | 150 | unsigned wait_nr, struct __kernel_timespec *ts, |
| 151 | sigset_t *sigmask) |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 152 | { |
Jens Axboe | e80a08c | 2019-12-01 17:19:16 -0700 | [diff] [blame] | 153 | unsigned to_submit = 0; |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 154 | |
Jens Axboe | 7ad0e4b | 2019-12-01 09:11:31 -0700 | [diff] [blame] | 155 | if (ts) { |
Jens Axboe | 11e18b3 | 2019-09-21 15:04:52 -0600 | [diff] [blame] | 156 | struct io_uring_sqe *sqe; |
Jens Axboe | 217756d | 2019-11-22 21:43:24 -0700 | [diff] [blame] | 157 | int ret; |
Jens Axboe | 11e18b3 | 2019-09-21 15:04:52 -0600 | [diff] [blame] | 158 | |
| 159 | /* |
| 160 | * If the SQ ring is full, we may need to submit IO first |
| 161 | */ |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 162 | sqe = io_uring_get_sqe(ring); |
Jens Axboe | 11e18b3 | 2019-09-21 15:04:52 -0600 | [diff] [blame] | 163 | if (!sqe) { |
| 164 | ret = io_uring_submit(ring); |
| 165 | if (ret < 0) |
| 166 | return ret; |
| 167 | sqe = io_uring_get_sqe(ring); |
Jens Axboe | e80a08c | 2019-12-01 17:19:16 -0700 | [diff] [blame] | 168 | if (!sqe) |
| 169 | return -EAGAIN; |
Jens Axboe | 11e18b3 | 2019-09-21 15:04:52 -0600 | [diff] [blame] | 170 | } |
Jens Axboe | 11a8f2b | 2019-10-15 17:31:17 -0600 | [diff] [blame] | 171 | io_uring_prep_timeout(sqe, ts, wait_nr, 0); |
Jens Axboe | 11e18b3 | 2019-09-21 15:04:52 -0600 | [diff] [blame] | 172 | sqe->user_data = LIBURING_UDATA_TIMEOUT; |
Jens Axboe | c39a058 | 2019-12-19 10:06:28 -0700 | [diff] [blame] | 173 | to_submit = __io_uring_flush_sq(ring); |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 174 | } |
Jens Axboe | 11e18b3 | 2019-09-21 15:04:52 -0600 | [diff] [blame] | 175 | |
Jens Axboe | e80a08c | 2019-12-01 17:19:16 -0700 | [diff] [blame] | 176 | return __io_uring_get_cqe(ring, cqe_ptr, to_submit, wait_nr, sigmask); |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* |
Jens Axboe | 217756d | 2019-11-22 21:43:24 -0700 | [diff] [blame] | 180 | * See io_uring_wait_cqes() - this function is the same, it just always uses |
| 181 | * '1' as the wait_nr. |
Jens Axboe | 11e18b3 | 2019-09-21 15:04:52 -0600 | [diff] [blame] | 182 | */ |
| 183 | int io_uring_wait_cqe_timeout(struct io_uring *ring, |
| 184 | struct io_uring_cqe **cqe_ptr, |
Jens Axboe | e2934e1 | 2019-10-01 10:05:16 -0600 | [diff] [blame] | 185 | struct __kernel_timespec *ts) |
Jens Axboe | 11e18b3 | 2019-09-21 15:04:52 -0600 | [diff] [blame] | 186 | { |
Jens Axboe | ac72640 | 2019-09-27 07:26:45 -0600 | [diff] [blame] | 187 | return io_uring_wait_cqes(ring, cqe_ptr, 1, ts, NULL); |
Jens Axboe | 11e18b3 | 2019-09-21 15:04:52 -0600 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* |
Jens Axboe | 40b44d2 | 2019-09-27 04:10:52 -0600 | [diff] [blame] | 191 | * Submit sqes acquired from io_uring_get_sqe() to the kernel. |
| 192 | * |
| 193 | * Returns number of sqes submitted |
| 194 | */ |
| 195 | static int __io_uring_submit(struct io_uring *ring, unsigned submitted, |
| 196 | unsigned wait_nr) |
| 197 | { |
| 198 | unsigned flags; |
| 199 | int ret; |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 200 | |
Jens Axboe | 043ea22 | 2019-06-17 11:41:15 -0600 | [diff] [blame] | 201 | flags = 0; |
Jens Axboe | d77a67e | 2019-11-27 17:02:21 -0700 | [diff] [blame] | 202 | if (sq_ring_needs_enter(ring, &flags) || wait_nr) { |
Glauber Costa | bf3aeb3 | 2019-12-19 11:15:48 -0500 | [diff] [blame] | 203 | if (wait_nr || (ring->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 91dde5c | 2019-06-06 10:46:13 -0600 | [diff] [blame] | 204 | flags |= IORING_ENTER_GETEVENTS; |
Roman Penyaev | df23d2d | 2019-05-27 21:05:09 +0200 | [diff] [blame] | 205 | |
Jens Axboe | 96144ea | 2019-12-01 11:21:39 -0700 | [diff] [blame] | 206 | ret = __sys_io_uring_enter(ring->ring_fd, submitted, wait_nr, |
| 207 | flags, NULL); |
Roman Penyaev | df23d2d | 2019-05-27 21:05:09 +0200 | [diff] [blame] | 208 | if (ret < 0) |
| 209 | return -errno; |
| 210 | } else |
| 211 | ret = submitted; |
Jens Axboe | 8260029 | 2019-03-05 20:12:48 -0700 | [diff] [blame] | 212 | |
Jens Axboe | a865221 | 2019-03-13 08:48:45 -0600 | [diff] [blame] | 213 | return ret; |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Jens Axboe | 94c9df3 | 2019-09-27 05:35:28 -0600 | [diff] [blame] | 216 | static int __io_uring_submit_and_wait(struct io_uring *ring, unsigned wait_nr) |
| 217 | { |
Jens Axboe | 94c9df3 | 2019-09-27 05:35:28 -0600 | [diff] [blame] | 218 | |
Jens Axboe | c39a058 | 2019-12-19 10:06:28 -0700 | [diff] [blame] | 219 | return __io_uring_submit(ring, __io_uring_flush_sq(ring), wait_nr); |
Jens Axboe | 94c9df3 | 2019-09-27 05:35:28 -0600 | [diff] [blame] | 220 | } |
| 221 | |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 222 | /* |
Jens Axboe | 91dde5c | 2019-06-06 10:46:13 -0600 | [diff] [blame] | 223 | * Submit sqes acquired from io_uring_get_sqe() to the kernel. |
| 224 | * |
| 225 | * Returns number of sqes submitted |
| 226 | */ |
| 227 | int io_uring_submit(struct io_uring *ring) |
| 228 | { |
Jens Axboe | 94c9df3 | 2019-09-27 05:35:28 -0600 | [diff] [blame] | 229 | return __io_uring_submit_and_wait(ring, 0); |
Jens Axboe | 91dde5c | 2019-06-06 10:46:13 -0600 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Like io_uring_submit(), but allows waiting for events as well. |
| 234 | * |
| 235 | * Returns number of sqes submitted |
| 236 | */ |
| 237 | int io_uring_submit_and_wait(struct io_uring *ring, unsigned wait_nr) |
| 238 | { |
Jens Axboe | 94c9df3 | 2019-09-27 05:35:28 -0600 | [diff] [blame] | 239 | return __io_uring_submit_and_wait(ring, wait_nr); |
Jens Axboe | 91dde5c | 2019-06-06 10:46:13 -0600 | [diff] [blame] | 240 | } |
| 241 | |
Jens Axboe | 902e446 | 2019-11-10 15:28:23 -0700 | [diff] [blame] | 242 | #define __io_uring_get_sqe(sq, __head) ({ \ |
| 243 | unsigned __next = (sq)->sqe_tail + 1; \ |
| 244 | struct io_uring_sqe *__sqe = NULL; \ |
| 245 | \ |
| 246 | if (__next - __head <= *(sq)->kring_entries) { \ |
| 247 | __sqe = &(sq)->sqes[(sq)->sqe_tail & *(sq)->kring_mask];\ |
| 248 | (sq)->sqe_tail = __next; \ |
| 249 | } \ |
| 250 | __sqe; \ |
| 251 | }) |
| 252 | |
Jens Axboe | 91dde5c | 2019-06-06 10:46:13 -0600 | [diff] [blame] | 253 | /* |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 254 | * Return an sqe to fill. Application must later call io_uring_submit() |
| 255 | * when it's ready to tell the kernel about it. The caller may call this |
| 256 | * function multiple times before calling io_uring_submit(). |
| 257 | * |
| 258 | * Returns a vacant sqe, or NULL if we're full. |
| 259 | */ |
| 260 | struct io_uring_sqe *io_uring_get_sqe(struct io_uring *ring) |
| 261 | { |
| 262 | struct io_uring_sq *sq = &ring->sq; |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 263 | |
Jens Axboe | 902e446 | 2019-11-10 15:28:23 -0700 | [diff] [blame] | 264 | if (!(ring->flags & IORING_SETUP_SQPOLL)) |
| 265 | return __io_uring_get_sqe(sq, sq->sqe_head); |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 266 | |
Jens Axboe | 902e446 | 2019-11-10 15:28:23 -0700 | [diff] [blame] | 267 | return __io_uring_get_sqe(sq, io_uring_smp_load_acquire(sq->khead)); |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 268 | } |