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