Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 1 | #include <sys/types.h> |
| 2 | #include <sys/stat.h> |
| 3 | #include <sys/mman.h> |
| 4 | #include <unistd.h> |
| 5 | #include <errno.h> |
| 6 | #include <string.h> |
Jens Axboe | 043ea22 | 2019-06-17 11:41:15 -0600 | [diff] [blame] | 7 | #include <stdbool.h> |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 8 | |
Stefan Hajnoczi | c31c7ec | 2019-07-24 09:24:50 +0100 | [diff] [blame] | 9 | #include "liburing/compat.h" |
| 10 | #include "liburing/io_uring.h" |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 11 | #include "liburing.h" |
Stefan Hajnoczi | c31c7ec | 2019-07-24 09:24:50 +0100 | [diff] [blame] | 12 | #include "liburing/barrier.h" |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 13 | |
Jens Axboe | 39e0ebd | 2019-04-18 08:32:06 -0600 | [diff] [blame] | 14 | static int __io_uring_get_cqe(struct io_uring *ring, |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 15 | struct io_uring_cqe **cqe_ptr, unsigned submit, |
Jens Axboe | 2d5ee69 | 2019-09-21 14:59:30 -0600 | [diff] [blame] | 16 | unsigned wait_nr) |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 17 | { |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 18 | int ret, err = 0; |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 19 | unsigned head; |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 20 | |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 21 | do { |
Bart Van Assche | bbb3099 | 2019-07-01 14:42:31 -0700 | [diff] [blame] | 22 | io_uring_for_each_cqe(ring, head, *cqe_ptr) |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 23 | break; |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 24 | if (*cqe_ptr) { |
| 25 | if ((*cqe_ptr)->user_data == LIBURING_UDATA_TIMEOUT) { |
| 26 | if ((*cqe_ptr)->res < 0) |
| 27 | err = (*cqe_ptr)->res; |
| 28 | io_uring_cq_advance(ring, 1); |
| 29 | if (!err) |
| 30 | continue; |
| 31 | *cqe_ptr = NULL; |
| 32 | } |
Bart Van Assche | bbb3099 | 2019-07-01 14:42:31 -0700 | [diff] [blame] | 33 | break; |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 34 | } |
Jens Axboe | 2d5ee69 | 2019-09-21 14:59:30 -0600 | [diff] [blame] | 35 | if (!wait_nr) |
Jens Axboe | 681ba88 | 2019-08-28 17:59:40 -0600 | [diff] [blame] | 36 | return -EAGAIN; |
Jens Axboe | 2d5ee69 | 2019-09-21 14:59:30 -0600 | [diff] [blame] | 37 | ret = io_uring_enter(ring->ring_fd, submit, wait_nr, |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 38 | IORING_ENTER_GETEVENTS, NULL); |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 39 | if (ret < 0) |
| 40 | return -errno; |
| 41 | } while (1); |
| 42 | |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 43 | return err; |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /* |
Jens Axboe | 39e0ebd | 2019-04-18 08:32:06 -0600 | [diff] [blame] | 47 | * Return an IO completion, if one is readily available. Returns 0 with |
| 48 | * cqe_ptr filled in on success, -errno on failure. |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 49 | */ |
Jens Axboe | 39e0ebd | 2019-04-18 08:32:06 -0600 | [diff] [blame] | 50 | int io_uring_peek_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_ptr) |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 51 | { |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 52 | return __io_uring_get_cqe(ring, cqe_ptr, 0, 0); |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* |
James Rouzier | 0b88d72 | 2019-09-25 15:35:06 -0400 | [diff] [blame] | 56 | * Fill in an array of IO completions up to count, if any are available. |
| 57 | * Returns the amount of IO completions filled. |
| 58 | */ |
Jens Axboe | 6d33802 | 2019-09-26 00:41:24 -0600 | [diff] [blame^] | 59 | unsigned io_uring_peek_batch_cqe(struct io_uring *ring, |
| 60 | struct io_uring_cqe **cqes, unsigned count) |
James Rouzier | 0b88d72 | 2019-09-25 15:35:06 -0400 | [diff] [blame] | 61 | { |
Jens Axboe | 6d33802 | 2019-09-26 00:41:24 -0600 | [diff] [blame^] | 62 | unsigned ready; |
| 63 | |
| 64 | ready = io_uring_cq_ready(ring); |
James Rouzier | 0b88d72 | 2019-09-25 15:35:06 -0400 | [diff] [blame] | 65 | if (ready) { |
James Rouzier | 0b88d72 | 2019-09-25 15:35:06 -0400 | [diff] [blame] | 66 | unsigned head = *ring->cq.khead; |
James Rouzier | 0b88d72 | 2019-09-25 15:35:06 -0400 | [diff] [blame] | 67 | unsigned mask = *ring->cq.kring_mask; |
Jens Axboe | 6d33802 | 2019-09-26 00:41:24 -0600 | [diff] [blame^] | 68 | unsigned last; |
James Rouzier | 0b88d72 | 2019-09-25 15:35:06 -0400 | [diff] [blame] | 69 | int i = 0; |
Jens Axboe | 6d33802 | 2019-09-26 00:41:24 -0600 | [diff] [blame^] | 70 | |
| 71 | count = count > ready ? ready : count; |
| 72 | last = head + count; |
| 73 | for (;head != last; head++, i++) |
James Rouzier | 0b88d72 | 2019-09-25 15:35:06 -0400 | [diff] [blame] | 74 | cqes[i] = &ring->cq.cqes[head & mask]; |
James Rouzier | 0b88d72 | 2019-09-25 15:35:06 -0400 | [diff] [blame] | 75 | |
| 76 | return count; |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | /* |
Jens Axboe | 39e0ebd | 2019-04-18 08:32:06 -0600 | [diff] [blame] | 83 | * Return an IO completion, waiting for it if necessary. Returns 0 with |
| 84 | * cqe_ptr filled in on success, -errno on failure. |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 85 | */ |
Jens Axboe | 39e0ebd | 2019-04-18 08:32:06 -0600 | [diff] [blame] | 86 | int io_uring_wait_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_ptr) |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 87 | { |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 88 | return __io_uring_get_cqe(ring, cqe_ptr, 0, 1); |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Like io_uring_wait_cqe(), except it accepts a timeout value as well. Note |
| 93 | * that an sqe is used internally to handle the timeout. Applications using |
| 94 | * this function must never set sqe->user_data to LIBURING_UDATA_TIMEOUT! |
Jens Axboe | 8b93cca | 2019-09-21 14:44:57 -0600 | [diff] [blame] | 95 | * |
| 96 | * Note that the application need not call io_uring_submit() before calling |
| 97 | * this function, as we will do that on its behalf. |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 98 | */ |
Jens Axboe | 11e18b3 | 2019-09-21 15:04:52 -0600 | [diff] [blame] | 99 | int io_uring_wait_cqes_timeout(struct io_uring *ring, |
| 100 | struct io_uring_cqe **cqe_ptr, |
| 101 | unsigned wait_nr, |
| 102 | struct timespec *ts) |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 103 | { |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 104 | int ret; |
| 105 | |
Jens Axboe | 11e18b3 | 2019-09-21 15:04:52 -0600 | [diff] [blame] | 106 | if (wait_nr) { |
| 107 | struct io_uring_sqe *sqe; |
| 108 | |
| 109 | /* |
| 110 | * If the SQ ring is full, we may need to submit IO first |
| 111 | */ |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 112 | sqe = io_uring_get_sqe(ring); |
Jens Axboe | 11e18b3 | 2019-09-21 15:04:52 -0600 | [diff] [blame] | 113 | if (!sqe) { |
| 114 | ret = io_uring_submit(ring); |
| 115 | if (ret < 0) |
| 116 | return ret; |
| 117 | sqe = io_uring_get_sqe(ring); |
| 118 | } |
| 119 | io_uring_prep_timeout(sqe, ts, wait_nr); |
| 120 | sqe->user_data = LIBURING_UDATA_TIMEOUT; |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 121 | } |
Jens Axboe | 11e18b3 | 2019-09-21 15:04:52 -0600 | [diff] [blame] | 122 | |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 123 | ret = io_uring_submit(ring); |
Jens Axboe | 5652c3b | 2019-09-21 14:26:42 -0600 | [diff] [blame] | 124 | if (ret < 0) |
Jens Axboe | 76e9232 | 2019-09-20 22:15:38 -0600 | [diff] [blame] | 125 | return ret; |
| 126 | |
| 127 | return __io_uring_get_cqe(ring, cqe_ptr, 1, 1); |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* |
Jens Axboe | 11e18b3 | 2019-09-21 15:04:52 -0600 | [diff] [blame] | 131 | * See io_uring_wait_cqes_timeout() - this function is the same, it just |
| 132 | * always uses '1' as the wait_nr. |
| 133 | */ |
| 134 | int io_uring_wait_cqe_timeout(struct io_uring *ring, |
| 135 | struct io_uring_cqe **cqe_ptr, |
| 136 | struct timespec *ts) |
| 137 | { |
| 138 | return io_uring_wait_cqes_timeout(ring, cqe_ptr, 1, ts); |
| 139 | } |
| 140 | |
| 141 | /* |
Roman Penyaev | df23d2d | 2019-05-27 21:05:09 +0200 | [diff] [blame] | 142 | * Returns true if we're not using SQ thread (thus nobody submits but us) |
Jens Axboe | 043ea22 | 2019-06-17 11:41:15 -0600 | [diff] [blame] | 143 | * or if IORING_SQ_NEED_WAKEUP is set, so submit thread must be explicitly |
| 144 | * awakened. For the latter case, we set the thread wakeup flag. |
Roman Penyaev | df23d2d | 2019-05-27 21:05:09 +0200 | [diff] [blame] | 145 | */ |
Jens Axboe | 043ea22 | 2019-06-17 11:41:15 -0600 | [diff] [blame] | 146 | static inline bool sq_ring_needs_enter(struct io_uring *ring, unsigned *flags) |
Roman Penyaev | df23d2d | 2019-05-27 21:05:09 +0200 | [diff] [blame] | 147 | { |
Jens Axboe | 043ea22 | 2019-06-17 11:41:15 -0600 | [diff] [blame] | 148 | if (!(ring->flags & IORING_SETUP_SQPOLL)) |
| 149 | return true; |
| 150 | if ((*ring->sq.kflags & IORING_SQ_NEED_WAKEUP)) { |
| 151 | *flags |= IORING_ENTER_SQ_WAKEUP; |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | return false; |
Roman Penyaev | df23d2d | 2019-05-27 21:05:09 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 159 | * Submit sqes acquired from io_uring_get_sqe() to the kernel. |
| 160 | * |
| 161 | * Returns number of sqes submitted |
| 162 | */ |
Jens Axboe | 91dde5c | 2019-06-06 10:46:13 -0600 | [diff] [blame] | 163 | static int __io_uring_submit(struct io_uring *ring, unsigned wait_nr) |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 164 | { |
| 165 | struct io_uring_sq *sq = &ring->sq; |
| 166 | const unsigned mask = *sq->kring_mask; |
Bart Van Assche | ecefd79 | 2019-07-01 14:42:32 -0700 | [diff] [blame] | 167 | unsigned ktail, submitted, to_submit; |
Jens Axboe | 043ea22 | 2019-06-17 11:41:15 -0600 | [diff] [blame] | 168 | unsigned flags; |
Jens Axboe | 8260029 | 2019-03-05 20:12:48 -0700 | [diff] [blame] | 169 | int ret; |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 170 | |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 171 | if (sq->sqe_head == sq->sqe_tail) |
| 172 | return 0; |
| 173 | |
| 174 | /* |
| 175 | * Fill in sqes that we have queued up, adding them to the kernel ring |
| 176 | */ |
| 177 | submitted = 0; |
Bart Van Assche | ecefd79 | 2019-07-01 14:42:32 -0700 | [diff] [blame] | 178 | ktail = *sq->ktail; |
Jens Axboe | 829f6a9 | 2019-05-21 10:45:16 -0600 | [diff] [blame] | 179 | to_submit = sq->sqe_tail - sq->sqe_head; |
| 180 | while (to_submit--) { |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 181 | sq->array[ktail & mask] = sq->sqe_head & mask; |
Bart Van Assche | ecefd79 | 2019-07-01 14:42:32 -0700 | [diff] [blame] | 182 | ktail++; |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 183 | sq->sqe_head++; |
| 184 | submitted++; |
| 185 | } |
| 186 | |
| 187 | if (!submitted) |
| 188 | return 0; |
| 189 | |
Bart Van Assche | ecefd79 | 2019-07-01 14:42:32 -0700 | [diff] [blame] | 190 | /* |
| 191 | * Ensure that the kernel sees the SQE updates before it sees the tail |
| 192 | * update. |
| 193 | */ |
Julia Suvorova | 552c6a0 | 2019-08-19 08:45:28 -0600 | [diff] [blame] | 194 | io_uring_smp_store_release(sq->ktail, ktail); |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 195 | |
Jens Axboe | 043ea22 | 2019-06-17 11:41:15 -0600 | [diff] [blame] | 196 | flags = 0; |
| 197 | if (wait_nr || sq_ring_needs_enter(ring, &flags)) { |
Jens Axboe | 91dde5c | 2019-06-06 10:46:13 -0600 | [diff] [blame] | 198 | if (wait_nr) { |
| 199 | if (wait_nr > submitted) |
| 200 | wait_nr = submitted; |
| 201 | flags |= IORING_ENTER_GETEVENTS; |
| 202 | } |
Roman Penyaev | df23d2d | 2019-05-27 21:05:09 +0200 | [diff] [blame] | 203 | |
Jens Axboe | 91dde5c | 2019-06-06 10:46:13 -0600 | [diff] [blame] | 204 | ret = io_uring_enter(ring->ring_fd, submitted, wait_nr, flags, |
| 205 | NULL); |
Roman Penyaev | df23d2d | 2019-05-27 21:05:09 +0200 | [diff] [blame] | 206 | if (ret < 0) |
| 207 | return -errno; |
| 208 | } else |
| 209 | ret = submitted; |
Jens Axboe | 8260029 | 2019-03-05 20:12:48 -0700 | [diff] [blame] | 210 | |
Jens Axboe | a865221 | 2019-03-13 08:48:45 -0600 | [diff] [blame] | 211 | return ret; |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* |
Jens Axboe | 91dde5c | 2019-06-06 10:46:13 -0600 | [diff] [blame] | 215 | * Submit sqes acquired from io_uring_get_sqe() to the kernel. |
| 216 | * |
| 217 | * Returns number of sqes submitted |
| 218 | */ |
| 219 | int io_uring_submit(struct io_uring *ring) |
| 220 | { |
| 221 | return __io_uring_submit(ring, 0); |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Like io_uring_submit(), but allows waiting for events as well. |
| 226 | * |
| 227 | * Returns number of sqes submitted |
| 228 | */ |
| 229 | int io_uring_submit_and_wait(struct io_uring *ring, unsigned wait_nr) |
| 230 | { |
| 231 | return __io_uring_submit(ring, wait_nr); |
| 232 | } |
| 233 | |
| 234 | /* |
Jens Axboe | 213d6f3 | 2019-01-17 21:40:30 -0700 | [diff] [blame] | 235 | * Return an sqe to fill. Application must later call io_uring_submit() |
| 236 | * when it's ready to tell the kernel about it. The caller may call this |
| 237 | * function multiple times before calling io_uring_submit(). |
| 238 | * |
| 239 | * Returns a vacant sqe, or NULL if we're full. |
| 240 | */ |
| 241 | struct io_uring_sqe *io_uring_get_sqe(struct io_uring *ring) |
| 242 | { |
| 243 | struct io_uring_sq *sq = &ring->sq; |
| 244 | unsigned next = sq->sqe_tail + 1; |
| 245 | struct io_uring_sqe *sqe; |
| 246 | |
| 247 | /* |
| 248 | * All sqes are used |
| 249 | */ |
| 250 | if (next - sq->sqe_head > *sq->kring_entries) |
| 251 | return NULL; |
| 252 | |
| 253 | sqe = &sq->sqes[sq->sqe_tail & *sq->kring_mask]; |
| 254 | sq->sqe_tail = next; |
| 255 | return sqe; |
| 256 | } |