Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Shared application/kernel submission and completion ring pairs, for |
| 4 | * supporting fast/efficient IO. |
| 5 | * |
| 6 | * A note on the read/write ordering memory barriers that are matched between |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 7 | * the application and kernel side. |
| 8 | * |
| 9 | * After the application reads the CQ ring tail, it must use an |
| 10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses |
| 11 | * before writing the tail (using smp_load_acquire to read the tail will |
| 12 | * do). It also needs a smp_mb() before updating CQ head (ordering the |
| 13 | * entry load(s) with the head store), pairing with an implicit barrier |
| 14 | * through a control-dependency in io_get_cqring (smp_store_release to |
| 15 | * store head will do). Failure to do so could lead to reading invalid |
| 16 | * CQ entries. |
| 17 | * |
| 18 | * Likewise, the application must use an appropriate smp_wmb() before |
| 19 | * writing the SQ tail (ordering SQ entry stores with the tail store), |
| 20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release |
| 21 | * to store the tail will do). And it needs a barrier ordering the SQ |
| 22 | * head load before writing new SQ entries (smp_load_acquire to read |
| 23 | * head will do). |
| 24 | * |
| 25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application |
| 26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* |
| 27 | * updating the SQ tail; a full memory barrier smp_mb() is needed |
| 28 | * between. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 29 | * |
| 30 | * Also see the examples in the liburing library: |
| 31 | * |
| 32 | * git://git.kernel.dk/liburing |
| 33 | * |
| 34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens |
| 35 | * from data shared between the kernel and application. This is done both |
| 36 | * for ordering purposes, but also to ensure that once a value is loaded from |
| 37 | * data that the application could potentially modify, it remains stable. |
| 38 | * |
| 39 | * Copyright (C) 2018-2019 Jens Axboe |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 41 | */ |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/errno.h> |
| 45 | #include <linux/syscalls.h> |
| 46 | #include <linux/compat.h> |
| 47 | #include <linux/refcount.h> |
| 48 | #include <linux/uio.h> |
| 49 | |
| 50 | #include <linux/sched/signal.h> |
| 51 | #include <linux/fs.h> |
| 52 | #include <linux/file.h> |
| 53 | #include <linux/fdtable.h> |
| 54 | #include <linux/mm.h> |
| 55 | #include <linux/mman.h> |
| 56 | #include <linux/mmu_context.h> |
| 57 | #include <linux/percpu.h> |
| 58 | #include <linux/slab.h> |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 59 | #include <linux/kthread.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 60 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 61 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 62 | #include <linux/net.h> |
| 63 | #include <net/sock.h> |
| 64 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 65 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 66 | #include <linux/anon_inodes.h> |
| 67 | #include <linux/sched/mm.h> |
| 68 | #include <linux/uaccess.h> |
| 69 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 70 | #include <linux/sizes.h> |
| 71 | #include <linux/hugetlb.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 72 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 73 | #define CREATE_TRACE_POINTS |
| 74 | #include <trace/events/io_uring.h> |
| 75 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 76 | #include <uapi/linux/io_uring.h> |
| 77 | |
| 78 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 79 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 80 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 81 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 82 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 86 | */ |
| 87 | #define IORING_FILE_TABLE_SHIFT 9 |
| 88 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 89 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 90 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 91 | |
| 92 | struct io_uring { |
| 93 | u32 head ____cacheline_aligned_in_smp; |
| 94 | u32 tail ____cacheline_aligned_in_smp; |
| 95 | }; |
| 96 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 97 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 98 | * This data is shared with the application through the mmap at offsets |
| 99 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 100 | * |
| 101 | * The offsets to the member fields are published through struct |
| 102 | * io_sqring_offsets when calling io_uring_setup. |
| 103 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 104 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 105 | /* |
| 106 | * Head and tail offsets into the ring; the offsets need to be |
| 107 | * masked to get valid indices. |
| 108 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 109 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 110 | * and the application controls tail of the sq ring and the head of the |
| 111 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 112 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 113 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 114 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 115 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 116 | * ring_entries - 1) |
| 117 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 118 | u32 sq_ring_mask, cq_ring_mask; |
| 119 | /* Ring sizes (constant, power of 2) */ |
| 120 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 121 | /* |
| 122 | * Number of invalid entries dropped by the kernel due to |
| 123 | * invalid index stored in array |
| 124 | * |
| 125 | * Written by the kernel, shouldn't be modified by the |
| 126 | * application (i.e. get number of "new events" by comparing to |
| 127 | * cached value). |
| 128 | * |
| 129 | * After a new SQ head value was read by the application this |
| 130 | * counter includes all submissions that were dropped reaching |
| 131 | * the new SQ head (and possibly more). |
| 132 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 133 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 134 | /* |
| 135 | * Runtime flags |
| 136 | * |
| 137 | * Written by the kernel, shouldn't be modified by the |
| 138 | * application. |
| 139 | * |
| 140 | * The application needs a full memory barrier before checking |
| 141 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 142 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 143 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 144 | /* |
| 145 | * Number of completion events lost because the queue was full; |
| 146 | * this should be avoided by the application by making sure |
| 147 | * there are not more requests pending thatn there is space in |
| 148 | * the completion queue. |
| 149 | * |
| 150 | * Written by the kernel, shouldn't be modified by the |
| 151 | * application (i.e. get number of "new events" by comparing to |
| 152 | * cached value). |
| 153 | * |
| 154 | * As completion events come in out of order this counter is not |
| 155 | * ordered with any other data. |
| 156 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 157 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 158 | /* |
| 159 | * Ring buffer of completion events. |
| 160 | * |
| 161 | * The kernel writes completion events fresh every time they are |
| 162 | * produced, so the application is allowed to modify pending |
| 163 | * entries. |
| 164 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 165 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 166 | }; |
| 167 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 168 | struct io_mapped_ubuf { |
| 169 | u64 ubuf; |
| 170 | size_t len; |
| 171 | struct bio_vec *bvec; |
| 172 | unsigned int nr_bvecs; |
| 173 | }; |
| 174 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 175 | struct fixed_file_table { |
| 176 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 179 | struct io_ring_ctx { |
| 180 | struct { |
| 181 | struct percpu_ref refs; |
| 182 | } ____cacheline_aligned_in_smp; |
| 183 | |
| 184 | struct { |
| 185 | unsigned int flags; |
| 186 | bool compat; |
| 187 | bool account_mem; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 188 | bool cq_overflow_flushed; |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 189 | bool drain_next; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 190 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 191 | /* |
| 192 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 193 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 194 | * |
| 195 | * This indirection could e.g. be used to assign fixed |
| 196 | * io_uring_sqe entries to operations and only submit them to |
| 197 | * the queue when needed. |
| 198 | * |
| 199 | * The kernel modifies neither the indices array nor the entries |
| 200 | * array. |
| 201 | */ |
| 202 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 203 | unsigned cached_sq_head; |
| 204 | unsigned sq_entries; |
| 205 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 206 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 207 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 208 | atomic_t cached_cq_overflow; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 209 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 210 | |
| 211 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 212 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 213 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 214 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 215 | wait_queue_head_t inflight_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 216 | } ____cacheline_aligned_in_smp; |
| 217 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 218 | struct io_rings *rings; |
| 219 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 220 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 221 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 222 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 223 | struct mm_struct *sqo_mm; |
| 224 | wait_queue_head_t sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 225 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 226 | /* |
| 227 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 228 | * readers must ensure that ->refs is alive as long as the file* is |
| 229 | * used. Only updated through io_uring_register(2). |
| 230 | */ |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 231 | struct fixed_file_table *file_table; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 232 | unsigned nr_user_files; |
| 233 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 234 | /* if used, fixed mapped user buffers */ |
| 235 | unsigned nr_user_bufs; |
| 236 | struct io_mapped_ubuf *user_bufs; |
| 237 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 238 | struct user_struct *user; |
| 239 | |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 240 | struct cred *creds; |
| 241 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 242 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
| 243 | struct completion *completions; |
| 244 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 245 | /* if all else fails... */ |
| 246 | struct io_kiocb *fallback_req; |
| 247 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 248 | #if defined(CONFIG_UNIX) |
| 249 | struct socket *ring_sock; |
| 250 | #endif |
| 251 | |
| 252 | struct { |
| 253 | unsigned cached_cq_tail; |
| 254 | unsigned cq_entries; |
| 255 | unsigned cq_mask; |
| 256 | atomic_t cq_timeouts; |
| 257 | struct wait_queue_head cq_wait; |
| 258 | struct fasync_struct *cq_fasync; |
| 259 | struct eventfd_ctx *cq_ev_fd; |
| 260 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 261 | |
| 262 | struct { |
| 263 | struct mutex uring_lock; |
| 264 | wait_queue_head_t wait; |
| 265 | } ____cacheline_aligned_in_smp; |
| 266 | |
| 267 | struct { |
| 268 | spinlock_t completion_lock; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 269 | bool poll_multi_file; |
| 270 | /* |
| 271 | * ->poll_list is protected by the ctx->uring_lock for |
| 272 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 273 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 274 | * manipulate the list, hence no extra locking is needed there. |
| 275 | */ |
| 276 | struct list_head poll_list; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 277 | struct rb_root cancel_tree; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 278 | |
| 279 | spinlock_t inflight_lock; |
| 280 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 281 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 282 | }; |
| 283 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 284 | /* |
| 285 | * First field must be the file pointer in all the |
| 286 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 287 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 288 | struct io_poll_iocb { |
| 289 | struct file *file; |
| 290 | struct wait_queue_head *head; |
| 291 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 292 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 293 | bool canceled; |
| 294 | struct wait_queue_entry wait; |
| 295 | }; |
| 296 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 297 | struct io_timeout_data { |
| 298 | struct io_kiocb *req; |
| 299 | struct hrtimer timer; |
| 300 | struct timespec64 ts; |
| 301 | enum hrtimer_mode mode; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 302 | u32 seq_offset; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 303 | }; |
| 304 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 305 | struct io_timeout { |
| 306 | struct file *file; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 307 | struct io_timeout_data *data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 308 | }; |
| 309 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 310 | /* |
| 311 | * NOTE! Each of the iocb union members has the file pointer |
| 312 | * as the first entry in their struct definition. So you can |
| 313 | * access the file pointer through any of the sub-structs, |
| 314 | * or directly as just 'ki_filp' in this struct. |
| 315 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 316 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 317 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 318 | struct file *file; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 319 | struct kiocb rw; |
| 320 | struct io_poll_iocb poll; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 321 | struct io_timeout timeout; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 322 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 323 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 324 | const struct io_uring_sqe *sqe; |
| 325 | struct file *ring_file; |
| 326 | int ring_fd; |
| 327 | bool has_user; |
| 328 | bool in_async; |
| 329 | bool needs_fixed_file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 330 | |
| 331 | struct io_ring_ctx *ctx; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 332 | union { |
| 333 | struct list_head list; |
| 334 | struct rb_node rb_node; |
| 335 | }; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 336 | struct list_head link_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 337 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 338 | refcount_t refs; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 339 | #define REQ_F_NOWAIT 1 /* must not punt to workers */ |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 340 | #define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */ |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 341 | #define REQ_F_FIXED_FILE 4 /* ctx owns file */ |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 342 | #define REQ_F_LINK_NEXT 8 /* already grabbed next link */ |
Stefan Bühler | e2033e3 | 2019-05-11 19:08:01 +0200 | [diff] [blame] | 343 | #define REQ_F_IO_DRAIN 16 /* drain existing IO first */ |
| 344 | #define REQ_F_IO_DRAINED 32 /* drain done */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 345 | #define REQ_F_LINK 64 /* linked sqes */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 346 | #define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */ |
Zhengyuan Liu | f7b76ac | 2019-07-16 23:26:14 +0800 | [diff] [blame] | 347 | #define REQ_F_FAIL_LINK 256 /* fail rest of links */ |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 348 | #define REQ_F_DRAIN_LINK 512 /* link should be fully drained */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 349 | #define REQ_F_TIMEOUT 1024 /* timeout request */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 350 | #define REQ_F_ISREG 2048 /* regular file */ |
| 351 | #define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */ |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 352 | #define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */ |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 353 | #define REQ_F_INFLIGHT 16384 /* on inflight list */ |
| 354 | #define REQ_F_COMP_LOCKED 32768 /* completion under lock */ |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 355 | #define REQ_F_FREE_SQE 65536 /* free sqe if not async queued */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 356 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 357 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 358 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 359 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 360 | struct list_head inflight_entry; |
| 361 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 362 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 363 | }; |
| 364 | |
| 365 | #define IO_PLUG_THRESHOLD 2 |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 366 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 367 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 368 | struct io_submit_state { |
| 369 | struct blk_plug plug; |
| 370 | |
| 371 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 372 | * io_kiocb alloc cache |
| 373 | */ |
| 374 | void *reqs[IO_IOPOLL_BATCH]; |
| 375 | unsigned int free_reqs; |
| 376 | unsigned int cur_req; |
| 377 | |
| 378 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 379 | * File reference cache |
| 380 | */ |
| 381 | struct file *file; |
| 382 | unsigned int fd; |
| 383 | unsigned int has_refs; |
| 384 | unsigned int used_refs; |
| 385 | unsigned int ios_left; |
| 386 | }; |
| 387 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 388 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 389 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 390 | static void __io_free_req(struct io_kiocb *req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 391 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 392 | static void io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 393 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 394 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 395 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 396 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 397 | static struct kmem_cache *req_cachep; |
| 398 | |
| 399 | static const struct file_operations io_uring_fops; |
| 400 | |
| 401 | struct sock *io_uring_get_socket(struct file *file) |
| 402 | { |
| 403 | #if defined(CONFIG_UNIX) |
| 404 | if (file->f_op == &io_uring_fops) { |
| 405 | struct io_ring_ctx *ctx = file->private_data; |
| 406 | |
| 407 | return ctx->ring_sock->sk; |
| 408 | } |
| 409 | #endif |
| 410 | return NULL; |
| 411 | } |
| 412 | EXPORT_SYMBOL(io_uring_get_socket); |
| 413 | |
| 414 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 415 | { |
| 416 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 417 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 418 | complete(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 422 | { |
| 423 | struct io_ring_ctx *ctx; |
| 424 | |
| 425 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 426 | if (!ctx) |
| 427 | return NULL; |
| 428 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 429 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 430 | if (!ctx->fallback_req) |
| 431 | goto err; |
| 432 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 433 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
| 434 | if (!ctx->completions) |
| 435 | goto err; |
| 436 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 437 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 438 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 439 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 440 | |
| 441 | ctx->flags = p->flags; |
| 442 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 443 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 444 | init_completion(&ctx->completions[0]); |
| 445 | init_completion(&ctx->completions[1]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 446 | mutex_init(&ctx->uring_lock); |
| 447 | init_waitqueue_head(&ctx->wait); |
| 448 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 449 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 450 | ctx->cancel_tree = RB_ROOT; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 451 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 452 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 453 | init_waitqueue_head(&ctx->inflight_wait); |
| 454 | spin_lock_init(&ctx->inflight_lock); |
| 455 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 456 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 457 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 458 | if (ctx->fallback_req) |
| 459 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 460 | kfree(ctx->completions); |
| 461 | kfree(ctx); |
| 462 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 465 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 466 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 467 | struct io_ring_ctx *ctx = req->ctx; |
| 468 | |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 469 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 470 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 471 | } |
| 472 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 473 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 474 | { |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 475 | if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN) |
| 476 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 477 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 478 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 482 | { |
| 483 | struct io_kiocb *req; |
| 484 | |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 485 | req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 486 | if (req && !req_need_defer(req)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 487 | list_del_init(&req->list); |
| 488 | return req; |
| 489 | } |
| 490 | |
| 491 | return NULL; |
| 492 | } |
| 493 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 494 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 495 | { |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 496 | struct io_kiocb *req; |
| 497 | |
| 498 | req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 499 | if (req) { |
| 500 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 501 | return NULL; |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 502 | if (!__req_need_defer(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 503 | list_del_init(&req->list); |
| 504 | return req; |
| 505 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | return NULL; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 509 | } |
| 510 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 511 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 512 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 513 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 514 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 515 | if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 516 | /* order cqe stores with ring update */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 517 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 518 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 519 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 520 | wake_up_interruptible(&ctx->cq_wait); |
| 521 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 526 | static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe) |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 527 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 528 | u8 opcode = READ_ONCE(sqe->opcode); |
| 529 | |
| 530 | return !(opcode == IORING_OP_READ_FIXED || |
| 531 | opcode == IORING_OP_WRITE_FIXED); |
| 532 | } |
| 533 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 534 | static inline bool io_prep_async_work(struct io_kiocb *req, |
| 535 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 536 | { |
| 537 | bool do_hashed = false; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 538 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 539 | if (req->sqe) { |
| 540 | switch (req->sqe->opcode) { |
Jens Axboe | 6cc47d1 | 2019-09-18 11:18:23 -0600 | [diff] [blame] | 541 | case IORING_OP_WRITEV: |
| 542 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 543 | do_hashed = true; |
Jens Axboe | 5f8fd2d | 2019-11-07 10:57:36 -0700 | [diff] [blame] | 544 | /* fall-through */ |
| 545 | case IORING_OP_READV: |
| 546 | case IORING_OP_READ_FIXED: |
| 547 | case IORING_OP_SENDMSG: |
| 548 | case IORING_OP_RECVMSG: |
| 549 | case IORING_OP_ACCEPT: |
| 550 | case IORING_OP_POLL_ADD: |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 551 | case IORING_OP_CONNECT: |
Jens Axboe | 5f8fd2d | 2019-11-07 10:57:36 -0700 | [diff] [blame] | 552 | /* |
| 553 | * We know REQ_F_ISREG is not set on some of these |
| 554 | * opcodes, but this enables us to keep the check in |
| 555 | * just one place. |
| 556 | */ |
| 557 | if (!(req->flags & REQ_F_ISREG)) |
| 558 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 6cc47d1 | 2019-09-18 11:18:23 -0600 | [diff] [blame] | 559 | break; |
| 560 | } |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 561 | if (io_sqe_needs_user(req->sqe)) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 562 | req->work.flags |= IO_WQ_WORK_NEEDS_USER; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 563 | } |
| 564 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 565 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 566 | return do_hashed; |
| 567 | } |
| 568 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 569 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 570 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 571 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 572 | struct io_kiocb *link; |
| 573 | bool do_hashed; |
| 574 | |
| 575 | do_hashed = io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 576 | |
| 577 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, |
| 578 | req->flags); |
| 579 | if (!do_hashed) { |
| 580 | io_wq_enqueue(ctx->io_wq, &req->work); |
| 581 | } else { |
| 582 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, |
| 583 | file_inode(req->file)); |
| 584 | } |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 585 | |
| 586 | if (link) |
| 587 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 588 | } |
| 589 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 590 | static void io_kill_timeout(struct io_kiocb *req) |
| 591 | { |
| 592 | int ret; |
| 593 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 594 | ret = hrtimer_try_to_cancel(&req->timeout.data->timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 595 | if (ret != -1) { |
| 596 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 597 | list_del_init(&req->list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 598 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 599 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 604 | { |
| 605 | struct io_kiocb *req, *tmp; |
| 606 | |
| 607 | spin_lock_irq(&ctx->completion_lock); |
| 608 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 609 | io_kill_timeout(req); |
| 610 | spin_unlock_irq(&ctx->completion_lock); |
| 611 | } |
| 612 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 613 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 614 | { |
| 615 | struct io_kiocb *req; |
| 616 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 617 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 618 | io_kill_timeout(req); |
| 619 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 620 | __io_commit_cqring(ctx); |
| 621 | |
| 622 | while ((req = io_get_deferred_req(ctx)) != NULL) { |
| 623 | req->flags |= REQ_F_IO_DRAINED; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 624 | io_queue_async_work(req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 628 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 629 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 630 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 631 | unsigned tail; |
| 632 | |
| 633 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 634 | /* |
| 635 | * writes to the cq entry need to come after reading head; the |
| 636 | * control dependency is enough as we're using WRITE_ONCE to |
| 637 | * fill the cq entry |
| 638 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 639 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 640 | return NULL; |
| 641 | |
| 642 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 643 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 646 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| 647 | { |
| 648 | if (waitqueue_active(&ctx->wait)) |
| 649 | wake_up(&ctx->wait); |
| 650 | if (waitqueue_active(&ctx->sqo_wait)) |
| 651 | wake_up(&ctx->sqo_wait); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 652 | if (ctx->cq_ev_fd) |
| 653 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 654 | } |
| 655 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 656 | /* Returns true if there are no backlogged entries after the flush */ |
| 657 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 658 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 659 | struct io_rings *rings = ctx->rings; |
| 660 | struct io_uring_cqe *cqe; |
| 661 | struct io_kiocb *req; |
| 662 | unsigned long flags; |
| 663 | LIST_HEAD(list); |
| 664 | |
| 665 | if (!force) { |
| 666 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 667 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 668 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 669 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 670 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 674 | |
| 675 | /* if force is set, the ring is going away. always drop after that */ |
| 676 | if (force) |
| 677 | ctx->cq_overflow_flushed = true; |
| 678 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 679 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 680 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 681 | cqe = io_get_cqring(ctx); |
| 682 | if (!cqe && !force) |
| 683 | break; |
| 684 | |
| 685 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 686 | list); |
| 687 | list_move(&req->list, &list); |
| 688 | if (cqe) { |
| 689 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 690 | WRITE_ONCE(cqe->res, req->result); |
| 691 | WRITE_ONCE(cqe->flags, 0); |
| 692 | } else { |
| 693 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 694 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | io_commit_cqring(ctx); |
| 699 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 700 | io_cqring_ev_posted(ctx); |
| 701 | |
| 702 | while (!list_empty(&list)) { |
| 703 | req = list_first_entry(&list, struct io_kiocb, list); |
| 704 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 705 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 706 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 707 | |
| 708 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 711 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 712 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 713 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 714 | struct io_uring_cqe *cqe; |
| 715 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 716 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 717 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 718 | /* |
| 719 | * If we can't get a cq entry, userspace overflowed the |
| 720 | * submission (by quite a lot). Increment the overflow count in |
| 721 | * the ring. |
| 722 | */ |
| 723 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 724 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 725 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 726 | WRITE_ONCE(cqe->res, res); |
| 727 | WRITE_ONCE(cqe->flags, 0); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 728 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 729 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 730 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 731 | } else { |
| 732 | refcount_inc(&req->refs); |
| 733 | req->result = res; |
| 734 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 738 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 739 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 740 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 741 | unsigned long flags; |
| 742 | |
| 743 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 744 | io_cqring_fill_event(req, res); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 745 | io_commit_cqring(ctx); |
| 746 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 747 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 748 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 751 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 752 | { |
| 753 | return req == (struct io_kiocb *) |
| 754 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 755 | } |
| 756 | |
| 757 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 758 | { |
| 759 | struct io_kiocb *req; |
| 760 | |
| 761 | req = ctx->fallback_req; |
| 762 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) |
| 763 | return req; |
| 764 | |
| 765 | return NULL; |
| 766 | } |
| 767 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 768 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 769 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 770 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 771 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 772 | struct io_kiocb *req; |
| 773 | |
| 774 | if (!percpu_ref_tryget(&ctx->refs)) |
| 775 | return NULL; |
| 776 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 777 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 778 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 779 | if (unlikely(!req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 780 | goto fallback; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 781 | } else if (!state->free_reqs) { |
| 782 | size_t sz; |
| 783 | int ret; |
| 784 | |
| 785 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 786 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 787 | |
| 788 | /* |
| 789 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 790 | * retry single alloc to be on the safe side. |
| 791 | */ |
| 792 | if (unlikely(ret <= 0)) { |
| 793 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 794 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 795 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 796 | ret = 1; |
| 797 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 798 | state->free_reqs = ret - 1; |
| 799 | state->cur_req = 1; |
| 800 | req = state->reqs[0]; |
| 801 | } else { |
| 802 | req = state->reqs[state->cur_req]; |
| 803 | state->free_reqs--; |
| 804 | state->cur_req++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 807 | got_it: |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 808 | req->ring_file = NULL; |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 809 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 810 | req->ctx = ctx; |
| 811 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 812 | /* one is dropped after submission, the other at completion */ |
| 813 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 814 | req->result = 0; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 815 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 816 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 817 | fallback: |
| 818 | req = io_get_fallback_req(ctx); |
| 819 | if (req) |
| 820 | goto got_it; |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 821 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 822 | return NULL; |
| 823 | } |
| 824 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 825 | static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr) |
| 826 | { |
| 827 | if (*nr) { |
| 828 | kmem_cache_free_bulk(req_cachep, *nr, reqs); |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 829 | percpu_ref_put_many(&ctx->refs, *nr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 830 | *nr = 0; |
| 831 | } |
| 832 | } |
| 833 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 834 | static void __io_free_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 835 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 836 | struct io_ring_ctx *ctx = req->ctx; |
| 837 | |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 838 | if (req->flags & REQ_F_FREE_SQE) |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 839 | kfree(req->sqe); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 840 | if (req->file && !(req->flags & REQ_F_FIXED_FILE)) |
| 841 | fput(req->file); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 842 | if (req->flags & REQ_F_INFLIGHT) { |
| 843 | unsigned long flags; |
| 844 | |
| 845 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 846 | list_del(&req->inflight_entry); |
| 847 | if (waitqueue_active(&ctx->inflight_wait)) |
| 848 | wake_up(&ctx->inflight_wait); |
| 849 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 850 | } |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 851 | if (req->flags & REQ_F_TIMEOUT) |
| 852 | kfree(req->timeout.data); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 853 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 854 | if (likely(!io_is_fallback_req(req))) |
| 855 | kmem_cache_free(req_cachep, req); |
| 856 | else |
| 857 | clear_bit_unlock(0, (unsigned long *) ctx->fallback_req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 858 | } |
| 859 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 860 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 861 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 862 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 863 | int ret; |
| 864 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 865 | ret = hrtimer_try_to_cancel(&req->timeout.data->timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 866 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 867 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 868 | io_commit_cqring(ctx); |
| 869 | req->flags &= ~REQ_F_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 870 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 871 | return true; |
| 872 | } |
| 873 | |
| 874 | return false; |
| 875 | } |
| 876 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 877 | static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 878 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 879 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 880 | struct io_kiocb *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 881 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 882 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 883 | /* Already got next link */ |
| 884 | if (req->flags & REQ_F_LINK_NEXT) |
| 885 | return; |
| 886 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 887 | /* |
| 888 | * The list should never be empty when we are called here. But could |
| 889 | * potentially happen if the chain is messed up, check to be on the |
| 890 | * safe side. |
| 891 | */ |
| 892 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 893 | while (nxt) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 894 | list_del_init(&nxt->list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 895 | |
| 896 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
| 897 | (nxt->flags & REQ_F_TIMEOUT)) { |
| 898 | wake_ev |= io_link_cancel_timeout(nxt); |
| 899 | nxt = list_first_entry_or_null(&req->link_list, |
| 900 | struct io_kiocb, list); |
| 901 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 902 | continue; |
| 903 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 904 | if (!list_empty(&req->link_list)) { |
| 905 | INIT_LIST_HEAD(&nxt->link_list); |
| 906 | list_splice(&req->link_list, &nxt->link_list); |
| 907 | nxt->flags |= REQ_F_LINK; |
| 908 | } |
| 909 | |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 910 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 911 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 912 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 913 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 914 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 915 | if (wake_ev) |
| 916 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | /* |
| 920 | * Called if REQ_F_LINK is set, and we fail the head request |
| 921 | */ |
| 922 | static void io_fail_links(struct io_kiocb *req) |
| 923 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 924 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 925 | struct io_kiocb *link; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 926 | unsigned long flags; |
| 927 | |
| 928 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 929 | |
| 930 | while (!list_empty(&req->link_list)) { |
| 931 | link = list_first_entry(&req->link_list, struct io_kiocb, list); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 932 | list_del_init(&link->list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 933 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 934 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 935 | |
| 936 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 937 | link->sqe->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 938 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 939 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 940 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 941 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 942 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 943 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 944 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 945 | |
| 946 | io_commit_cqring(ctx); |
| 947 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 948 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 949 | } |
| 950 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 951 | static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 952 | { |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 953 | if (likely(!(req->flags & REQ_F_LINK))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 954 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 955 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 956 | /* |
| 957 | * If LINK is set, we have dependent requests in this chain. If we |
| 958 | * didn't fail this request, queue the first one up, moving any other |
| 959 | * dependencies to the next request. In case of failure, fail the rest |
| 960 | * of the chain. |
| 961 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 962 | if (req->flags & REQ_F_FAIL_LINK) { |
| 963 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 964 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 965 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 966 | struct io_ring_ctx *ctx = req->ctx; |
| 967 | unsigned long flags; |
| 968 | |
| 969 | /* |
| 970 | * If this is a timeout link, we could be racing with the |
| 971 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 972 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 973 | */ |
| 974 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 975 | io_req_link_next(req, nxt); |
| 976 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 977 | } else { |
| 978 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 979 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 980 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 981 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 982 | static void io_free_req(struct io_kiocb *req) |
| 983 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 984 | struct io_kiocb *nxt = NULL; |
| 985 | |
| 986 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 987 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 988 | |
| 989 | if (nxt) |
| 990 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 991 | } |
| 992 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 993 | /* |
| 994 | * Drop reference to request, return next in chain (if there is one) if this |
| 995 | * was the last reference to this request. |
| 996 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 997 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 998 | static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr) |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 999 | { |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1000 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1001 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1002 | if (refcount_dec_and_test(&req->refs)) |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1003 | __io_free_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1006 | static void io_put_req(struct io_kiocb *req) |
| 1007 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1008 | if (refcount_dec_and_test(&req->refs)) |
| 1009 | io_free_req(req); |
| 1010 | } |
| 1011 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1012 | /* |
| 1013 | * Must only be used if we don't need to care about links, usually from |
| 1014 | * within the completion handling itself. |
| 1015 | */ |
| 1016 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1017 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1018 | /* drop both submit and complete references */ |
| 1019 | if (refcount_sub_and_test(2, &req->refs)) |
| 1020 | __io_free_req(req); |
| 1021 | } |
| 1022 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1023 | static void io_double_put_req(struct io_kiocb *req) |
| 1024 | { |
| 1025 | /* drop both submit and complete references */ |
| 1026 | if (refcount_sub_and_test(2, &req->refs)) |
| 1027 | io_free_req(req); |
| 1028 | } |
| 1029 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1030 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1031 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1032 | struct io_rings *rings = ctx->rings; |
| 1033 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1034 | /* |
| 1035 | * noflush == true is from the waitqueue handler, just ensure we wake |
| 1036 | * up the task, and the next invocation will flush the entries. We |
| 1037 | * cannot safely to it from here. |
| 1038 | */ |
| 1039 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1040 | return -1U; |
| 1041 | |
| 1042 | io_cqring_overflow_flush(ctx, false); |
| 1043 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1044 | /* See comment at the top of this file */ |
| 1045 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1046 | return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1047 | } |
| 1048 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1049 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1050 | { |
| 1051 | struct io_rings *rings = ctx->rings; |
| 1052 | |
| 1053 | /* make sure SQ entry isn't read before tail */ |
| 1054 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1055 | } |
| 1056 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1057 | /* |
| 1058 | * Find and free completed poll iocbs |
| 1059 | */ |
| 1060 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1061 | struct list_head *done) |
| 1062 | { |
| 1063 | void *reqs[IO_IOPOLL_BATCH]; |
| 1064 | struct io_kiocb *req; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1065 | int to_free; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1066 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1067 | to_free = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1068 | while (!list_empty(done)) { |
| 1069 | req = list_first_entry(done, struct io_kiocb, list); |
| 1070 | list_del(&req->list); |
| 1071 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1072 | io_cqring_fill_event(req, req->result); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1073 | (*nr_events)++; |
| 1074 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1075 | if (refcount_dec_and_test(&req->refs)) { |
| 1076 | /* If we're not using fixed files, we have to pair the |
| 1077 | * completion part with the file put. Use regular |
| 1078 | * completions for those, only batch free for fixed |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1079 | * file and non-linked commands. |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1080 | */ |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 1081 | if (((req->flags & |
| 1082 | (REQ_F_FIXED_FILE|REQ_F_LINK|REQ_F_FREE_SQE)) == |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1083 | REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1084 | reqs[to_free++] = req; |
| 1085 | if (to_free == ARRAY_SIZE(reqs)) |
| 1086 | io_free_req_many(ctx, reqs, &to_free); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 1087 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1088 | io_free_req(req); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 1089 | } |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1090 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1091 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1092 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1093 | io_commit_cqring(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1094 | io_free_req_many(ctx, reqs, &to_free); |
| 1095 | } |
| 1096 | |
| 1097 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1098 | long min) |
| 1099 | { |
| 1100 | struct io_kiocb *req, *tmp; |
| 1101 | LIST_HEAD(done); |
| 1102 | bool spin; |
| 1103 | int ret; |
| 1104 | |
| 1105 | /* |
| 1106 | * Only spin for completions if we don't have multiple devices hanging |
| 1107 | * off our complete list, and we're under the requested amount. |
| 1108 | */ |
| 1109 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1110 | |
| 1111 | ret = 0; |
| 1112 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
| 1113 | struct kiocb *kiocb = &req->rw; |
| 1114 | |
| 1115 | /* |
| 1116 | * Move completed entries to our local list. If we find a |
| 1117 | * request that requires polling, break out and complete |
| 1118 | * the done list first, if we have entries there. |
| 1119 | */ |
| 1120 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 1121 | list_move_tail(&req->list, &done); |
| 1122 | continue; |
| 1123 | } |
| 1124 | if (!list_empty(&done)) |
| 1125 | break; |
| 1126 | |
| 1127 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1128 | if (ret < 0) |
| 1129 | break; |
| 1130 | |
| 1131 | if (ret && spin) |
| 1132 | spin = false; |
| 1133 | ret = 0; |
| 1134 | } |
| 1135 | |
| 1136 | if (!list_empty(&done)) |
| 1137 | io_iopoll_complete(ctx, nr_events, &done); |
| 1138 | |
| 1139 | return ret; |
| 1140 | } |
| 1141 | |
| 1142 | /* |
| 1143 | * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a |
| 1144 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1145 | * as a non-spinning completion check. |
| 1146 | */ |
| 1147 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1148 | long min) |
| 1149 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1150 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1151 | int ret; |
| 1152 | |
| 1153 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1154 | if (ret < 0) |
| 1155 | return ret; |
| 1156 | if (!min || *nr_events >= min) |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
| 1160 | return 1; |
| 1161 | } |
| 1162 | |
| 1163 | /* |
| 1164 | * We can't just wait for polled events to come to us, we have to actively |
| 1165 | * find and complete them. |
| 1166 | */ |
| 1167 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1168 | { |
| 1169 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1170 | return; |
| 1171 | |
| 1172 | mutex_lock(&ctx->uring_lock); |
| 1173 | while (!list_empty(&ctx->poll_list)) { |
| 1174 | unsigned int nr_events = 0; |
| 1175 | |
| 1176 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1177 | |
| 1178 | /* |
| 1179 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1180 | * in this case we need to ensure that we reap all events. |
| 1181 | */ |
| 1182 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1183 | } |
| 1184 | mutex_unlock(&ctx->uring_lock); |
| 1185 | } |
| 1186 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1187 | static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1188 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1189 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1190 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1191 | |
| 1192 | do { |
| 1193 | int tmin = 0; |
| 1194 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1195 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1196 | * Don't enter poll loop if we already have events pending. |
| 1197 | * If we do, we can potentially be spinning for commands that |
| 1198 | * already triggered a CQE (eg in error). |
| 1199 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1200 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1201 | break; |
| 1202 | |
| 1203 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1204 | * If a submit got punted to a workqueue, we can have the |
| 1205 | * application entering polling for a command before it gets |
| 1206 | * issued. That app will hold the uring_lock for the duration |
| 1207 | * of the poll right here, so we need to take a breather every |
| 1208 | * now and then to ensure that the issue has a chance to add |
| 1209 | * the poll to the issued list. Otherwise we can spin here |
| 1210 | * forever, while the workqueue is stuck trying to acquire the |
| 1211 | * very same mutex. |
| 1212 | */ |
| 1213 | if (!(++iters & 7)) { |
| 1214 | mutex_unlock(&ctx->uring_lock); |
| 1215 | mutex_lock(&ctx->uring_lock); |
| 1216 | } |
| 1217 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1218 | if (*nr_events < min) |
| 1219 | tmin = min - *nr_events; |
| 1220 | |
| 1221 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1222 | if (ret <= 0) |
| 1223 | break; |
| 1224 | ret = 0; |
| 1225 | } while (min && !*nr_events && !need_resched()); |
| 1226 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1227 | return ret; |
| 1228 | } |
| 1229 | |
| 1230 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1231 | long min) |
| 1232 | { |
| 1233 | int ret; |
| 1234 | |
| 1235 | /* |
| 1236 | * We disallow the app entering submit/complete with polling, but we |
| 1237 | * still need to lock the ring to prevent racing with polled issue |
| 1238 | * that got punted to a workqueue. |
| 1239 | */ |
| 1240 | mutex_lock(&ctx->uring_lock); |
| 1241 | ret = __io_iopoll_check(ctx, nr_events, min); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1242 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1243 | return ret; |
| 1244 | } |
| 1245 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1246 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1247 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1248 | /* |
| 1249 | * Tell lockdep we inherited freeze protection from submission |
| 1250 | * thread. |
| 1251 | */ |
| 1252 | if (req->flags & REQ_F_ISREG) { |
| 1253 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1254 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1255 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1256 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1257 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1260 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1261 | { |
| 1262 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); |
| 1263 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1264 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1265 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1266 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1267 | if ((req->flags & REQ_F_LINK) && res != req->result) |
| 1268 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1269 | io_cqring_add_event(req, res); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1273 | { |
| 1274 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); |
| 1275 | |
| 1276 | io_complete_rw_common(kiocb, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1277 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1280 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
| 1281 | { |
| 1282 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1283 | struct io_kiocb *nxt = NULL; |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1284 | |
| 1285 | io_complete_rw_common(kiocb, res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1286 | io_put_req_find_next(req, &nxt); |
| 1287 | |
| 1288 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1291 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 1292 | { |
| 1293 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); |
| 1294 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1295 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1296 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1297 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1298 | if ((req->flags & REQ_F_LINK) && res != req->result) |
| 1299 | req->flags |= REQ_F_FAIL_LINK; |
| 1300 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1301 | if (res != -EAGAIN) |
| 1302 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 1303 | } |
| 1304 | |
| 1305 | /* |
| 1306 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 1307 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 1308 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 1309 | * accessing the kiocb cookie. |
| 1310 | */ |
| 1311 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 1312 | { |
| 1313 | struct io_ring_ctx *ctx = req->ctx; |
| 1314 | |
| 1315 | /* |
| 1316 | * Track whether we have multiple files in our lists. This will impact |
| 1317 | * how we do polling eventually, not spinning if we're on potentially |
| 1318 | * different devices. |
| 1319 | */ |
| 1320 | if (list_empty(&ctx->poll_list)) { |
| 1321 | ctx->poll_multi_file = false; |
| 1322 | } else if (!ctx->poll_multi_file) { |
| 1323 | struct io_kiocb *list_req; |
| 1324 | |
| 1325 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1326 | list); |
| 1327 | if (list_req->rw.ki_filp != req->rw.ki_filp) |
| 1328 | ctx->poll_multi_file = true; |
| 1329 | } |
| 1330 | |
| 1331 | /* |
| 1332 | * For fast devices, IO may have already completed. If it has, add |
| 1333 | * it to the front so we find it first. |
| 1334 | */ |
| 1335 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1336 | list_add(&req->list, &ctx->poll_list); |
| 1337 | else |
| 1338 | list_add_tail(&req->list, &ctx->poll_list); |
| 1339 | } |
| 1340 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1341 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1342 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1343 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1344 | int diff = state->has_refs - state->used_refs; |
| 1345 | |
| 1346 | if (diff) |
| 1347 | fput_many(state->file, diff); |
| 1348 | state->file = NULL; |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | /* |
| 1353 | * Get as many references to a file as we have IOs left in this submission, |
| 1354 | * assuming most submissions are for one file, or at least that each file |
| 1355 | * has more than one submission. |
| 1356 | */ |
| 1357 | static struct file *io_file_get(struct io_submit_state *state, int fd) |
| 1358 | { |
| 1359 | if (!state) |
| 1360 | return fget(fd); |
| 1361 | |
| 1362 | if (state->file) { |
| 1363 | if (state->fd == fd) { |
| 1364 | state->used_refs++; |
| 1365 | state->ios_left--; |
| 1366 | return state->file; |
| 1367 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1368 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1369 | } |
| 1370 | state->file = fget_many(fd, state->ios_left); |
| 1371 | if (!state->file) |
| 1372 | return NULL; |
| 1373 | |
| 1374 | state->fd = fd; |
| 1375 | state->has_refs = state->ios_left; |
| 1376 | state->used_refs = 1; |
| 1377 | state->ios_left--; |
| 1378 | return state->file; |
| 1379 | } |
| 1380 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1381 | /* |
| 1382 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 1383 | * any file. For now, just ensure that anything potentially problematic is done |
| 1384 | * inline. |
| 1385 | */ |
| 1386 | static bool io_file_supports_async(struct file *file) |
| 1387 | { |
| 1388 | umode_t mode = file_inode(file)->i_mode; |
| 1389 | |
| 1390 | if (S_ISBLK(mode) || S_ISCHR(mode)) |
| 1391 | return true; |
| 1392 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 1393 | return true; |
| 1394 | |
| 1395 | return false; |
| 1396 | } |
| 1397 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 1398 | static int io_prep_rw(struct io_kiocb *req, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1399 | { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 1400 | const struct io_uring_sqe *sqe = req->sqe; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1401 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1402 | struct kiocb *kiocb = &req->rw; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1403 | unsigned ioprio; |
| 1404 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1405 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1406 | if (!req->file) |
| 1407 | return -EBADF; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1408 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1409 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 1410 | req->flags |= REQ_F_ISREG; |
| 1411 | |
| 1412 | /* |
| 1413 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 1414 | * we know to async punt it even if it was opened O_NONBLOCK |
| 1415 | */ |
| 1416 | if (force_nonblock && !io_file_supports_async(req->file)) { |
| 1417 | req->flags |= REQ_F_MUST_PUNT; |
| 1418 | return -EAGAIN; |
| 1419 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 1420 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1421 | kiocb->ki_pos = READ_ONCE(sqe->off); |
| 1422 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 1423 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
| 1424 | |
| 1425 | ioprio = READ_ONCE(sqe->ioprio); |
| 1426 | if (ioprio) { |
| 1427 | ret = ioprio_check_cap(ioprio); |
| 1428 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1429 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1430 | |
| 1431 | kiocb->ki_ioprio = ioprio; |
| 1432 | } else |
| 1433 | kiocb->ki_ioprio = get_current_ioprio(); |
| 1434 | |
| 1435 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 1436 | if (unlikely(ret)) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1437 | return ret; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1438 | |
| 1439 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1440 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 1441 | (req->file->f_flags & O_NONBLOCK)) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1442 | req->flags |= REQ_F_NOWAIT; |
| 1443 | |
| 1444 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1445 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1446 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1447 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1448 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 1449 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1450 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1451 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1452 | kiocb->ki_flags |= IOCB_HIPRI; |
| 1453 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 1454 | req->result = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1455 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1456 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1457 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1458 | kiocb->ki_complete = io_complete_rw; |
| 1459 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1460 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 1464 | { |
| 1465 | switch (ret) { |
| 1466 | case -EIOCBQUEUED: |
| 1467 | break; |
| 1468 | case -ERESTARTSYS: |
| 1469 | case -ERESTARTNOINTR: |
| 1470 | case -ERESTARTNOHAND: |
| 1471 | case -ERESTART_RESTARTBLOCK: |
| 1472 | /* |
| 1473 | * We can't just restart the syscall, since previously |
| 1474 | * submitted sqes may already be in progress. Just fail this |
| 1475 | * IO with EINTR. |
| 1476 | */ |
| 1477 | ret = -EINTR; |
| 1478 | /* fall through */ |
| 1479 | default: |
| 1480 | kiocb->ki_complete(kiocb, ret, 0); |
| 1481 | } |
| 1482 | } |
| 1483 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1484 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt, |
| 1485 | bool in_async) |
| 1486 | { |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1487 | if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1488 | *nxt = __io_complete_rw(kiocb, ret); |
| 1489 | else |
| 1490 | io_rw_done(kiocb, ret); |
| 1491 | } |
| 1492 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1493 | static int io_import_fixed(struct io_ring_ctx *ctx, int rw, |
| 1494 | const struct io_uring_sqe *sqe, |
| 1495 | struct iov_iter *iter) |
| 1496 | { |
| 1497 | size_t len = READ_ONCE(sqe->len); |
| 1498 | struct io_mapped_ubuf *imu; |
| 1499 | unsigned index, buf_index; |
| 1500 | size_t offset; |
| 1501 | u64 buf_addr; |
| 1502 | |
| 1503 | /* attempt to use fixed buffers without having provided iovecs */ |
| 1504 | if (unlikely(!ctx->user_bufs)) |
| 1505 | return -EFAULT; |
| 1506 | |
| 1507 | buf_index = READ_ONCE(sqe->buf_index); |
| 1508 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 1509 | return -EFAULT; |
| 1510 | |
| 1511 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 1512 | imu = &ctx->user_bufs[index]; |
| 1513 | buf_addr = READ_ONCE(sqe->addr); |
| 1514 | |
| 1515 | /* overflow */ |
| 1516 | if (buf_addr + len < buf_addr) |
| 1517 | return -EFAULT; |
| 1518 | /* not inside the mapped region */ |
| 1519 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 1520 | return -EFAULT; |
| 1521 | |
| 1522 | /* |
| 1523 | * May not be a start of buffer, set size appropriately |
| 1524 | * and advance us to the beginning. |
| 1525 | */ |
| 1526 | offset = buf_addr - imu->ubuf; |
| 1527 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1528 | |
| 1529 | if (offset) { |
| 1530 | /* |
| 1531 | * Don't use iov_iter_advance() here, as it's really slow for |
| 1532 | * using the latter parts of a big fixed buffer - it iterates |
| 1533 | * over each segment manually. We can cheat a bit here, because |
| 1534 | * we know that: |
| 1535 | * |
| 1536 | * 1) it's a BVEC iter, we set it up |
| 1537 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 1538 | * first and last bvec |
| 1539 | * |
| 1540 | * So just find our index, and adjust the iterator afterwards. |
| 1541 | * If the offset is within the first bvec (or the whole first |
| 1542 | * bvec, just use iov_iter_advance(). This makes it easier |
| 1543 | * since we can just skip the first segment, which may not |
| 1544 | * be PAGE_SIZE aligned. |
| 1545 | */ |
| 1546 | const struct bio_vec *bvec = imu->bvec; |
| 1547 | |
| 1548 | if (offset <= bvec->bv_len) { |
| 1549 | iov_iter_advance(iter, offset); |
| 1550 | } else { |
| 1551 | unsigned long seg_skip; |
| 1552 | |
| 1553 | /* skip first vec */ |
| 1554 | offset -= bvec->bv_len; |
| 1555 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 1556 | |
| 1557 | iter->bvec = bvec + seg_skip; |
| 1558 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 1559 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1560 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1561 | } |
| 1562 | } |
| 1563 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 1564 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1565 | } |
| 1566 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 1567 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 1568 | struct iovec **iovec, struct iov_iter *iter) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1569 | { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 1570 | const struct io_uring_sqe *sqe = req->sqe; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1571 | void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 1572 | size_t sqe_len = READ_ONCE(sqe->len); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1573 | u8 opcode; |
| 1574 | |
| 1575 | /* |
| 1576 | * We're reading ->opcode for the second time, but the first read |
| 1577 | * doesn't care whether it's _FIXED or not, so it doesn't matter |
| 1578 | * whether ->opcode changes concurrently. The first read does care |
| 1579 | * about whether it is a READ or a WRITE, so we don't trust this read |
| 1580 | * for that purpose and instead let the caller pass in the read/write |
| 1581 | * flag. |
| 1582 | */ |
| 1583 | opcode = READ_ONCE(sqe->opcode); |
| 1584 | if (opcode == IORING_OP_READ_FIXED || |
| 1585 | opcode == IORING_OP_WRITE_FIXED) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 1586 | ssize_t ret = io_import_fixed(req->ctx, rw, sqe, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1587 | *iovec = NULL; |
| 1588 | return ret; |
| 1589 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1590 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 1591 | if (!req->has_user) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1592 | return -EFAULT; |
| 1593 | |
| 1594 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 1595 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1596 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 1597 | iovec, iter); |
| 1598 | #endif |
| 1599 | |
| 1600 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 1601 | } |
| 1602 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1603 | /* |
| 1604 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 1605 | * by looping over ->read() or ->write() manually. |
| 1606 | */ |
| 1607 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 1608 | struct iov_iter *iter) |
| 1609 | { |
| 1610 | ssize_t ret = 0; |
| 1611 | |
| 1612 | /* |
| 1613 | * Don't support polled IO through this interface, and we can't |
| 1614 | * support non-blocking either. For the latter, this just causes |
| 1615 | * the kiocb to be handled from an async context. |
| 1616 | */ |
| 1617 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1618 | return -EOPNOTSUPP; |
| 1619 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 1620 | return -EAGAIN; |
| 1621 | |
| 1622 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 1623 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1624 | ssize_t nr; |
| 1625 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 1626 | if (!iov_iter_is_bvec(iter)) { |
| 1627 | iovec = iov_iter_iovec(iter); |
| 1628 | } else { |
| 1629 | /* fixed buffers import bvec */ |
| 1630 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 1631 | + iter->iov_offset; |
| 1632 | iovec.iov_len = min(iter->count, |
| 1633 | iter->bvec->bv_len - iter->iov_offset); |
| 1634 | } |
| 1635 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1636 | if (rw == READ) { |
| 1637 | nr = file->f_op->read(file, iovec.iov_base, |
| 1638 | iovec.iov_len, &kiocb->ki_pos); |
| 1639 | } else { |
| 1640 | nr = file->f_op->write(file, iovec.iov_base, |
| 1641 | iovec.iov_len, &kiocb->ki_pos); |
| 1642 | } |
| 1643 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 1644 | if (iov_iter_is_bvec(iter)) |
| 1645 | kunmap(iter->bvec->bv_page); |
| 1646 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1647 | if (nr < 0) { |
| 1648 | if (!ret) |
| 1649 | ret = nr; |
| 1650 | break; |
| 1651 | } |
| 1652 | ret += nr; |
| 1653 | if (nr != iovec.iov_len) |
| 1654 | break; |
| 1655 | iov_iter_advance(iter, nr); |
| 1656 | } |
| 1657 | |
| 1658 | return ret; |
| 1659 | } |
| 1660 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 1661 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 1662 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1663 | { |
| 1664 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
| 1665 | struct kiocb *kiocb = &req->rw; |
| 1666 | struct iov_iter iter; |
| 1667 | struct file *file; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1668 | size_t iov_count; |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 1669 | ssize_t read_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1670 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 1671 | ret = io_prep_rw(req, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1672 | if (ret) |
| 1673 | return ret; |
| 1674 | file = kiocb->ki_filp; |
| 1675 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1676 | if (unlikely(!(file->f_mode & FMODE_READ))) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1677 | return -EBADF; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1678 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 1679 | ret = io_import_iovec(READ, req, &iovec, &iter); |
Jens Axboe | 87e5e6d | 2019-05-14 16:02:22 -0600 | [diff] [blame] | 1680 | if (ret < 0) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1681 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1682 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 1683 | read_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1684 | if (req->flags & REQ_F_LINK) |
| 1685 | req->result = read_size; |
| 1686 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1687 | iov_count = iov_iter_count(&iter); |
| 1688 | ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1689 | if (!ret) { |
| 1690 | ssize_t ret2; |
| 1691 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1692 | if (file->f_op->read_iter) |
| 1693 | ret2 = call_read_iter(file, kiocb, &iter); |
| 1694 | else |
| 1695 | ret2 = loop_rw_iter(READ, file, kiocb, &iter); |
| 1696 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 1697 | /* |
| 1698 | * In case of a short read, punt to async. This can happen |
| 1699 | * if we have data partially cached. Alternatively we can |
| 1700 | * return the short read, in which case the application will |
| 1701 | * need to issue another SQE and wait for it. That SQE will |
| 1702 | * need async punt anyway, so it's more efficient to do it |
| 1703 | * here. |
| 1704 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1705 | if (force_nonblock && !(req->flags & REQ_F_NOWAIT) && |
| 1706 | (req->flags & REQ_F_ISREG) && |
| 1707 | ret2 > 0 && ret2 < read_size) |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 1708 | ret2 = -EAGAIN; |
| 1709 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1710 | if (!force_nonblock || ret2 != -EAGAIN) |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 1711 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1712 | else |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1713 | ret = -EAGAIN; |
| 1714 | } |
| 1715 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1716 | return ret; |
| 1717 | } |
| 1718 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 1719 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 1720 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1721 | { |
| 1722 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
| 1723 | struct kiocb *kiocb = &req->rw; |
| 1724 | struct iov_iter iter; |
| 1725 | struct file *file; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1726 | size_t iov_count; |
Jens Axboe | 87e5e6d | 2019-05-14 16:02:22 -0600 | [diff] [blame] | 1727 | ssize_t ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1728 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 1729 | ret = io_prep_rw(req, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1730 | if (ret) |
| 1731 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1732 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1733 | file = kiocb->ki_filp; |
| 1734 | if (unlikely(!(file->f_mode & FMODE_WRITE))) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1735 | return -EBADF; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1736 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 1737 | ret = io_import_iovec(WRITE, req, &iovec, &iter); |
Jens Axboe | 87e5e6d | 2019-05-14 16:02:22 -0600 | [diff] [blame] | 1738 | if (ret < 0) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1739 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1740 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1741 | if (req->flags & REQ_F_LINK) |
| 1742 | req->result = ret; |
| 1743 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1744 | iov_count = iov_iter_count(&iter); |
| 1745 | |
| 1746 | ret = -EAGAIN; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1747 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1748 | goto out_free; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1749 | |
| 1750 | ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1751 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 1752 | ssize_t ret2; |
| 1753 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1754 | /* |
| 1755 | * Open-code file_start_write here to grab freeze protection, |
| 1756 | * which will be released by another thread in |
| 1757 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 1758 | * released so that it doesn't complain about the held lock when |
| 1759 | * we return to userspace. |
| 1760 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1761 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1762 | __sb_start_write(file_inode(file)->i_sb, |
| 1763 | SB_FREEZE_WRITE, true); |
| 1764 | __sb_writers_release(file_inode(file)->i_sb, |
| 1765 | SB_FREEZE_WRITE); |
| 1766 | } |
| 1767 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 1768 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1769 | if (file->f_op->write_iter) |
| 1770 | ret2 = call_write_iter(file, kiocb, &iter); |
| 1771 | else |
| 1772 | ret2 = loop_rw_iter(WRITE, file, kiocb, &iter); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1773 | if (!force_nonblock || ret2 != -EAGAIN) |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 1774 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1775 | else |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 1776 | ret = -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1777 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1778 | out_free: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1779 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1780 | return ret; |
| 1781 | } |
| 1782 | |
| 1783 | /* |
| 1784 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 1785 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1786 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1787 | { |
| 1788 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1789 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1790 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1791 | return -EINVAL; |
| 1792 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1793 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1794 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1795 | return 0; |
| 1796 | } |
| 1797 | |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 1798 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 1799 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 1800 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 1801 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1802 | if (!req->file) |
| 1803 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 1804 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 1805 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1806 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1807 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 1808 | return -EINVAL; |
| 1809 | |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 1810 | return 0; |
| 1811 | } |
| 1812 | |
| 1813 | static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1814 | struct io_kiocb **nxt, bool force_nonblock) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 1815 | { |
| 1816 | loff_t sqe_off = READ_ONCE(sqe->off); |
| 1817 | loff_t sqe_len = READ_ONCE(sqe->len); |
| 1818 | loff_t end = sqe_off + sqe_len; |
| 1819 | unsigned fsync_flags; |
| 1820 | int ret; |
| 1821 | |
| 1822 | fsync_flags = READ_ONCE(sqe->fsync_flags); |
| 1823 | if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC)) |
| 1824 | return -EINVAL; |
| 1825 | |
| 1826 | ret = io_prep_fsync(req, sqe); |
| 1827 | if (ret) |
| 1828 | return ret; |
| 1829 | |
| 1830 | /* fsync always requires a blocking context */ |
| 1831 | if (force_nonblock) |
| 1832 | return -EAGAIN; |
| 1833 | |
| 1834 | ret = vfs_fsync_range(req->rw.ki_filp, sqe_off, |
| 1835 | end > 0 ? end : LLONG_MAX, |
| 1836 | fsync_flags & IORING_FSYNC_DATASYNC); |
| 1837 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1838 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
| 1839 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1840 | io_cqring_add_event(req, ret); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1841 | io_put_req_find_next(req, nxt); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 1842 | return 0; |
| 1843 | } |
| 1844 | |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 1845 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 1846 | { |
| 1847 | struct io_ring_ctx *ctx = req->ctx; |
| 1848 | int ret = 0; |
| 1849 | |
| 1850 | if (!req->file) |
| 1851 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 1852 | |
| 1853 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1854 | return -EINVAL; |
| 1855 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 1856 | return -EINVAL; |
| 1857 | |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 1858 | return ret; |
| 1859 | } |
| 1860 | |
| 1861 | static int io_sync_file_range(struct io_kiocb *req, |
| 1862 | const struct io_uring_sqe *sqe, |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1863 | struct io_kiocb **nxt, |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 1864 | bool force_nonblock) |
| 1865 | { |
| 1866 | loff_t sqe_off; |
| 1867 | loff_t sqe_len; |
| 1868 | unsigned flags; |
| 1869 | int ret; |
| 1870 | |
| 1871 | ret = io_prep_sfr(req, sqe); |
| 1872 | if (ret) |
| 1873 | return ret; |
| 1874 | |
| 1875 | /* sync_file_range always requires a blocking context */ |
| 1876 | if (force_nonblock) |
| 1877 | return -EAGAIN; |
| 1878 | |
| 1879 | sqe_off = READ_ONCE(sqe->off); |
| 1880 | sqe_len = READ_ONCE(sqe->len); |
| 1881 | flags = READ_ONCE(sqe->sync_range_flags); |
| 1882 | |
| 1883 | ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags); |
| 1884 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1885 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
| 1886 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1887 | io_cqring_add_event(req, ret); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1888 | io_put_req_find_next(req, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 1889 | return 0; |
| 1890 | } |
| 1891 | |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 1892 | #if defined(CONFIG_NET) |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 1893 | static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1894 | struct io_kiocb **nxt, bool force_nonblock, |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 1895 | long (*fn)(struct socket *, struct user_msghdr __user *, |
| 1896 | unsigned int)) |
| 1897 | { |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 1898 | struct socket *sock; |
| 1899 | int ret; |
| 1900 | |
| 1901 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 1902 | return -EINVAL; |
| 1903 | |
| 1904 | sock = sock_from_file(req->file, &ret); |
| 1905 | if (sock) { |
| 1906 | struct user_msghdr __user *msg; |
| 1907 | unsigned flags; |
| 1908 | |
| 1909 | flags = READ_ONCE(sqe->msg_flags); |
| 1910 | if (flags & MSG_DONTWAIT) |
| 1911 | req->flags |= REQ_F_NOWAIT; |
| 1912 | else if (force_nonblock) |
| 1913 | flags |= MSG_DONTWAIT; |
| 1914 | |
| 1915 | msg = (struct user_msghdr __user *) (unsigned long) |
| 1916 | READ_ONCE(sqe->addr); |
| 1917 | |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 1918 | ret = fn(sock, msg, flags); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 1919 | if (force_nonblock && ret == -EAGAIN) |
| 1920 | return ret; |
| 1921 | } |
| 1922 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1923 | io_cqring_add_event(req, ret); |
Jens Axboe | f1f4085 | 2019-11-05 20:33:16 -0700 | [diff] [blame] | 1924 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
| 1925 | req->flags |= REQ_F_FAIL_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1926 | io_put_req_find_next(req, nxt); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 1927 | return 0; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 1928 | } |
| 1929 | #endif |
| 1930 | |
| 1931 | static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1932 | struct io_kiocb **nxt, bool force_nonblock) |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 1933 | { |
| 1934 | #if defined(CONFIG_NET) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1935 | return io_send_recvmsg(req, sqe, nxt, force_nonblock, |
| 1936 | __sys_sendmsg_sock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 1937 | #else |
| 1938 | return -EOPNOTSUPP; |
| 1939 | #endif |
| 1940 | } |
| 1941 | |
| 1942 | static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1943 | struct io_kiocb **nxt, bool force_nonblock) |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 1944 | { |
| 1945 | #if defined(CONFIG_NET) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1946 | return io_send_recvmsg(req, sqe, nxt, force_nonblock, |
| 1947 | __sys_recvmsg_sock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 1948 | #else |
| 1949 | return -EOPNOTSUPP; |
| 1950 | #endif |
| 1951 | } |
| 1952 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 1953 | static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1954 | struct io_kiocb **nxt, bool force_nonblock) |
| 1955 | { |
| 1956 | #if defined(CONFIG_NET) |
| 1957 | struct sockaddr __user *addr; |
| 1958 | int __user *addr_len; |
| 1959 | unsigned file_flags; |
| 1960 | int flags, ret; |
| 1961 | |
| 1962 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 1963 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 1964 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 1965 | return -EINVAL; |
| 1966 | |
| 1967 | addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr); |
| 1968 | addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2); |
| 1969 | flags = READ_ONCE(sqe->accept_flags); |
| 1970 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 1971 | |
| 1972 | ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags); |
| 1973 | if (ret == -EAGAIN && force_nonblock) { |
| 1974 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 1975 | return -EAGAIN; |
| 1976 | } |
Jens Axboe | 8e3cca1 | 2019-11-09 19:52:33 -0700 | [diff] [blame] | 1977 | if (ret == -ERESTARTSYS) |
| 1978 | ret = -EINTR; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 1979 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
| 1980 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1981 | io_cqring_add_event(req, ret); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1982 | io_put_req_find_next(req, nxt); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 1983 | return 0; |
| 1984 | #else |
| 1985 | return -EOPNOTSUPP; |
| 1986 | #endif |
| 1987 | } |
| 1988 | |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 1989 | static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1990 | struct io_kiocb **nxt, bool force_nonblock) |
| 1991 | { |
| 1992 | #if defined(CONFIG_NET) |
| 1993 | struct sockaddr __user *addr; |
| 1994 | unsigned file_flags; |
| 1995 | int addr_len, ret; |
| 1996 | |
| 1997 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 1998 | return -EINVAL; |
| 1999 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 2000 | return -EINVAL; |
| 2001 | |
| 2002 | addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr); |
| 2003 | addr_len = READ_ONCE(sqe->addr2); |
| 2004 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 2005 | |
| 2006 | ret = __sys_connect_file(req->file, addr, addr_len, file_flags); |
| 2007 | if (ret == -EAGAIN && force_nonblock) |
| 2008 | return -EAGAIN; |
| 2009 | if (ret == -ERESTARTSYS) |
| 2010 | ret = -EINTR; |
| 2011 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
| 2012 | req->flags |= REQ_F_FAIL_LINK; |
| 2013 | io_cqring_add_event(req, ret); |
| 2014 | io_put_req_find_next(req, nxt); |
| 2015 | return 0; |
| 2016 | #else |
| 2017 | return -EOPNOTSUPP; |
| 2018 | #endif |
| 2019 | } |
| 2020 | |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 2021 | static inline void io_poll_remove_req(struct io_kiocb *req) |
| 2022 | { |
| 2023 | if (!RB_EMPTY_NODE(&req->rb_node)) { |
| 2024 | rb_erase(&req->rb_node, &req->ctx->cancel_tree); |
| 2025 | RB_CLEAR_NODE(&req->rb_node); |
| 2026 | } |
| 2027 | } |
| 2028 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2029 | static void io_poll_remove_one(struct io_kiocb *req) |
| 2030 | { |
| 2031 | struct io_poll_iocb *poll = &req->poll; |
| 2032 | |
| 2033 | spin_lock(&poll->head->lock); |
| 2034 | WRITE_ONCE(poll->canceled, true); |
| 2035 | if (!list_empty(&poll->wait.entry)) { |
| 2036 | list_del_init(&poll->wait.entry); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2037 | io_queue_async_work(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2038 | } |
| 2039 | spin_unlock(&poll->head->lock); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 2040 | io_poll_remove_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 2044 | { |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 2045 | struct rb_node *node; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2046 | struct io_kiocb *req; |
| 2047 | |
| 2048 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 2049 | while ((node = rb_first(&ctx->cancel_tree)) != NULL) { |
| 2050 | req = rb_entry(node, struct io_kiocb, rb_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2051 | io_poll_remove_one(req); |
| 2052 | } |
| 2053 | spin_unlock_irq(&ctx->completion_lock); |
| 2054 | } |
| 2055 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2056 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 2057 | { |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 2058 | struct rb_node *p, *parent = NULL; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2059 | struct io_kiocb *req; |
| 2060 | |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 2061 | p = ctx->cancel_tree.rb_node; |
| 2062 | while (p) { |
| 2063 | parent = p; |
| 2064 | req = rb_entry(parent, struct io_kiocb, rb_node); |
| 2065 | if (sqe_addr < req->user_data) { |
| 2066 | p = p->rb_left; |
| 2067 | } else if (sqe_addr > req->user_data) { |
| 2068 | p = p->rb_right; |
| 2069 | } else { |
| 2070 | io_poll_remove_one(req); |
| 2071 | return 0; |
| 2072 | } |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | return -ENOENT; |
| 2076 | } |
| 2077 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2078 | /* |
| 2079 | * Find a running poll command that matches one specified in sqe->addr, |
| 2080 | * and remove it if found. |
| 2081 | */ |
| 2082 | static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2083 | { |
| 2084 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2085 | int ret; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2086 | |
| 2087 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2088 | return -EINVAL; |
| 2089 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 2090 | sqe->poll_events) |
| 2091 | return -EINVAL; |
| 2092 | |
| 2093 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2094 | ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr)); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2095 | spin_unlock_irq(&ctx->completion_lock); |
| 2096 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2097 | io_cqring_add_event(req, ret); |
Jens Axboe | f1f4085 | 2019-11-05 20:33:16 -0700 | [diff] [blame] | 2098 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
| 2099 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2100 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2101 | return 0; |
| 2102 | } |
| 2103 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2104 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2105 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2106 | struct io_ring_ctx *ctx = req->ctx; |
| 2107 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2108 | req->poll.done = true; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2109 | if (error) |
| 2110 | io_cqring_fill_event(req, error); |
| 2111 | else |
| 2112 | io_cqring_fill_event(req, mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2113 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2114 | } |
| 2115 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2116 | static void io_poll_complete_work(struct io_wq_work **workptr) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2117 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2118 | struct io_wq_work *work = *workptr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2119 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 2120 | struct io_poll_iocb *poll = &req->poll; |
| 2121 | struct poll_table_struct pt = { ._key = poll->events }; |
| 2122 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 2123 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2124 | __poll_t mask = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2125 | int ret = 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2126 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2127 | if (work->flags & IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2128 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2129 | ret = -ECANCELED; |
| 2130 | } else if (READ_ONCE(poll->canceled)) { |
| 2131 | ret = -ECANCELED; |
| 2132 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2133 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2134 | if (ret != -ECANCELED) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2135 | mask = vfs_poll(poll->file, &pt) & poll->events; |
| 2136 | |
| 2137 | /* |
| 2138 | * Note that ->ki_cancel callers also delete iocb from active_reqs after |
| 2139 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to |
| 2140 | * synchronize with them. In the cancellation case the list_del_init |
| 2141 | * itself is not actually needed, but harmless so we keep it in to |
| 2142 | * avoid further branches in the fast path. |
| 2143 | */ |
| 2144 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2145 | if (!mask && ret != -ECANCELED) { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2146 | add_wait_queue(poll->head, &poll->wait); |
| 2147 | spin_unlock_irq(&ctx->completion_lock); |
| 2148 | return; |
| 2149 | } |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 2150 | io_poll_remove_req(req); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2151 | io_poll_complete(req, mask, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2152 | spin_unlock_irq(&ctx->completion_lock); |
| 2153 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2154 | io_cqring_ev_posted(ctx); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 2155 | |
Jens Axboe | fba38c2 | 2019-11-18 12:27:57 -0700 | [diff] [blame] | 2156 | if (ret < 0 && req->flags & REQ_F_LINK) |
| 2157 | req->flags |= REQ_F_FAIL_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 2158 | io_put_req_find_next(req, &nxt); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 2159 | if (nxt) |
| 2160 | *workptr = &nxt->work; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 2164 | void *key) |
| 2165 | { |
| 2166 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, |
| 2167 | wait); |
| 2168 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); |
| 2169 | struct io_ring_ctx *ctx = req->ctx; |
| 2170 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2171 | unsigned long flags; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2172 | |
| 2173 | /* for instances that support it check for an event match first: */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2174 | if (mask && !(mask & poll->events)) |
| 2175 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2176 | |
| 2177 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2178 | |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 2179 | /* |
| 2180 | * Run completion inline if we can. We're using trylock here because |
| 2181 | * we are violating the completion_lock -> poll wq lock ordering. |
| 2182 | * If we have a link timeout we're going to need the completion_lock |
| 2183 | * for finalizing the request, mark us as having grabbed that already. |
| 2184 | */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2185 | if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) { |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 2186 | io_poll_remove_req(req); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2187 | io_poll_complete(req, mask, 0); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 2188 | req->flags |= REQ_F_COMP_LOCKED; |
| 2189 | io_put_req(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2190 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 2191 | |
| 2192 | io_cqring_ev_posted(ctx); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2193 | } else { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2194 | io_queue_async_work(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2195 | } |
| 2196 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2197 | return 1; |
| 2198 | } |
| 2199 | |
| 2200 | struct io_poll_table { |
| 2201 | struct poll_table_struct pt; |
| 2202 | struct io_kiocb *req; |
| 2203 | int error; |
| 2204 | }; |
| 2205 | |
| 2206 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 2207 | struct poll_table_struct *p) |
| 2208 | { |
| 2209 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 2210 | |
| 2211 | if (unlikely(pt->req->poll.head)) { |
| 2212 | pt->error = -EINVAL; |
| 2213 | return; |
| 2214 | } |
| 2215 | |
| 2216 | pt->error = 0; |
| 2217 | pt->req->poll.head = head; |
| 2218 | add_wait_queue(head, &pt->req->poll.wait); |
| 2219 | } |
| 2220 | |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 2221 | static void io_poll_req_insert(struct io_kiocb *req) |
| 2222 | { |
| 2223 | struct io_ring_ctx *ctx = req->ctx; |
| 2224 | struct rb_node **p = &ctx->cancel_tree.rb_node; |
| 2225 | struct rb_node *parent = NULL; |
| 2226 | struct io_kiocb *tmp; |
| 2227 | |
| 2228 | while (*p) { |
| 2229 | parent = *p; |
| 2230 | tmp = rb_entry(parent, struct io_kiocb, rb_node); |
| 2231 | if (req->user_data < tmp->user_data) |
| 2232 | p = &(*p)->rb_left; |
| 2233 | else |
| 2234 | p = &(*p)->rb_right; |
| 2235 | } |
| 2236 | rb_link_node(&req->rb_node, parent, p); |
| 2237 | rb_insert_color(&req->rb_node, &ctx->cancel_tree); |
| 2238 | } |
| 2239 | |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 2240 | static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2241 | struct io_kiocb **nxt) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2242 | { |
| 2243 | struct io_poll_iocb *poll = &req->poll; |
| 2244 | struct io_ring_ctx *ctx = req->ctx; |
| 2245 | struct io_poll_table ipt; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2246 | bool cancel = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2247 | __poll_t mask; |
| 2248 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2249 | |
| 2250 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2251 | return -EINVAL; |
| 2252 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 2253 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2254 | if (!poll->file) |
| 2255 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2256 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2257 | req->sqe = NULL; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2258 | INIT_IO_WORK(&req->work, io_poll_complete_work); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2259 | events = READ_ONCE(sqe->poll_events); |
| 2260 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 2261 | RB_CLEAR_NODE(&req->rb_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2262 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2263 | poll->head = NULL; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2264 | poll->done = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2265 | poll->canceled = false; |
| 2266 | |
| 2267 | ipt.pt._qproc = io_poll_queue_proc; |
| 2268 | ipt.pt._key = poll->events; |
| 2269 | ipt.req = req; |
| 2270 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ |
| 2271 | |
| 2272 | /* initialized the list so that we can do list_empty checks */ |
| 2273 | INIT_LIST_HEAD(&poll->wait.entry); |
| 2274 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); |
| 2275 | |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 2276 | INIT_LIST_HEAD(&req->list); |
| 2277 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2278 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2279 | |
| 2280 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2281 | if (likely(poll->head)) { |
| 2282 | spin_lock(&poll->head->lock); |
| 2283 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 2284 | if (ipt.error) |
| 2285 | cancel = true; |
| 2286 | ipt.error = 0; |
| 2287 | mask = 0; |
| 2288 | } |
| 2289 | if (mask || ipt.error) |
| 2290 | list_del_init(&poll->wait.entry); |
| 2291 | else if (cancel) |
| 2292 | WRITE_ONCE(poll->canceled, true); |
| 2293 | else if (!poll->done) /* actually waiting for an event */ |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 2294 | io_poll_req_insert(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2295 | spin_unlock(&poll->head->lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2296 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2297 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2298 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2299 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2300 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2301 | spin_unlock_irq(&ctx->completion_lock); |
| 2302 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2303 | if (mask) { |
| 2304 | io_cqring_ev_posted(ctx); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 2305 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2306 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 2307 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2308 | } |
| 2309 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2310 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 2311 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2312 | struct io_timeout_data *data = container_of(timer, |
| 2313 | struct io_timeout_data, timer); |
| 2314 | struct io_kiocb *req = data->req; |
| 2315 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2316 | unsigned long flags; |
| 2317 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2318 | atomic_inc(&ctx->cq_timeouts); |
| 2319 | |
| 2320 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 2321 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2322 | * We could be racing with timeout deletion. If the list is empty, |
| 2323 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 2324 | */ |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 2325 | if (!list_empty(&req->list)) { |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2326 | struct io_kiocb *prev; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2327 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2328 | /* |
| 2329 | * Adjust the reqs sequence before the current one because it |
| 2330 | * will consume a slot in the cq_ring and the the cq_tail |
| 2331 | * pointer will be increased, otherwise other timeout reqs may |
| 2332 | * return in advance without waiting for enough wait_nr. |
| 2333 | */ |
| 2334 | prev = req; |
| 2335 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 2336 | prev->sequence++; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2337 | list_del_init(&req->list); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2338 | } |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 2339 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2340 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2341 | io_commit_cqring(ctx); |
| 2342 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 2343 | |
| 2344 | io_cqring_ev_posted(ctx); |
Jens Axboe | f1f4085 | 2019-11-05 20:33:16 -0700 | [diff] [blame] | 2345 | if (req->flags & REQ_F_LINK) |
| 2346 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2347 | io_put_req(req); |
| 2348 | return HRTIMER_NORESTART; |
| 2349 | } |
| 2350 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2351 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 2352 | { |
| 2353 | struct io_kiocb *req; |
| 2354 | int ret = -ENOENT; |
| 2355 | |
| 2356 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 2357 | if (user_data == req->user_data) { |
| 2358 | list_del_init(&req->list); |
| 2359 | ret = 0; |
| 2360 | break; |
| 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | if (ret == -ENOENT) |
| 2365 | return ret; |
| 2366 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2367 | ret = hrtimer_try_to_cancel(&req->timeout.data->timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2368 | if (ret == -1) |
| 2369 | return -EALREADY; |
| 2370 | |
Jens Axboe | fba38c2 | 2019-11-18 12:27:57 -0700 | [diff] [blame] | 2371 | if (req->flags & REQ_F_LINK) |
| 2372 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2373 | io_cqring_fill_event(req, -ECANCELED); |
| 2374 | io_put_req(req); |
| 2375 | return 0; |
| 2376 | } |
| 2377 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2378 | /* |
| 2379 | * Remove or update an existing timeout command |
| 2380 | */ |
| 2381 | static int io_timeout_remove(struct io_kiocb *req, |
| 2382 | const struct io_uring_sqe *sqe) |
| 2383 | { |
| 2384 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2385 | unsigned flags; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2386 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2387 | |
| 2388 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2389 | return -EINVAL; |
| 2390 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 2391 | return -EINVAL; |
| 2392 | flags = READ_ONCE(sqe->timeout_flags); |
| 2393 | if (flags) |
| 2394 | return -EINVAL; |
| 2395 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2396 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2397 | ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr)); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2398 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2399 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2400 | io_commit_cqring(ctx); |
| 2401 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2402 | io_cqring_ev_posted(ctx); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2403 | if (ret < 0 && req->flags & REQ_F_LINK) |
| 2404 | req->flags |= REQ_F_FAIL_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 2405 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2406 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2407 | } |
| 2408 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2409 | static int io_timeout_setup(struct io_kiocb *req) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2410 | { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2411 | const struct io_uring_sqe *sqe = req->sqe; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2412 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 2413 | unsigned flags; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2414 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2415 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2416 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2417 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 2418 | return -EINVAL; |
| 2419 | flags = READ_ONCE(sqe->timeout_flags); |
| 2420 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2421 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 2422 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2423 | data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL); |
| 2424 | if (!data) |
| 2425 | return -ENOMEM; |
| 2426 | data->req = req; |
| 2427 | req->timeout.data = data; |
| 2428 | req->flags |= REQ_F_TIMEOUT; |
| 2429 | |
| 2430 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2431 | return -EFAULT; |
| 2432 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2433 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2434 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2435 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2436 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2437 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2438 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 2439 | return 0; |
| 2440 | } |
| 2441 | |
| 2442 | static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2443 | { |
| 2444 | unsigned count; |
| 2445 | struct io_ring_ctx *ctx = req->ctx; |
| 2446 | struct io_timeout_data *data; |
| 2447 | struct list_head *entry; |
| 2448 | unsigned span = 0; |
| 2449 | int ret; |
| 2450 | |
| 2451 | ret = io_timeout_setup(req); |
| 2452 | /* common setup allows flags (like links) set, we don't */ |
| 2453 | if (!ret && sqe->flags) |
| 2454 | ret = -EINVAL; |
| 2455 | if (ret) |
| 2456 | return ret; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 2457 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2458 | /* |
| 2459 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 2460 | * timeout event to be satisfied. If it isn't set, then this is |
| 2461 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2462 | */ |
| 2463 | count = READ_ONCE(sqe->off); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 2464 | if (!count) { |
| 2465 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 2466 | spin_lock_irq(&ctx->completion_lock); |
| 2467 | entry = ctx->timeout_list.prev; |
| 2468 | goto add; |
| 2469 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2470 | |
| 2471 | req->sequence = ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 2472 | req->timeout.data->seq_offset = count; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2473 | |
| 2474 | /* |
| 2475 | * Insertion sort, ensuring the first entry in the list is always |
| 2476 | * the one we need first. |
| 2477 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2478 | spin_lock_irq(&ctx->completion_lock); |
| 2479 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 2480 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 2481 | unsigned nxt_sq_head; |
| 2482 | long long tmp, tmp_nxt; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 2483 | u32 nxt_offset = nxt->timeout.data->seq_offset; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2484 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 2485 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 2486 | continue; |
| 2487 | |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 2488 | /* |
| 2489 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 2490 | * long to store it. |
| 2491 | */ |
| 2492 | tmp = (long long)ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 2493 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
| 2494 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 2495 | |
| 2496 | /* |
| 2497 | * cached_sq_head may overflow, and it will never overflow twice |
| 2498 | * once there is some timeout req still be valid. |
| 2499 | */ |
| 2500 | if (ctx->cached_sq_head < nxt_sq_head) |
yangerkun | 8b07a65 | 2019-10-17 12:12:35 +0800 | [diff] [blame] | 2501 | tmp += UINT_MAX; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 2502 | |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 2503 | if (tmp > tmp_nxt) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2504 | break; |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 2505 | |
| 2506 | /* |
| 2507 | * Sequence of reqs after the insert one and itself should |
| 2508 | * be adjusted because each timeout req consumes a slot. |
| 2509 | */ |
| 2510 | span++; |
| 2511 | nxt->sequence++; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2512 | } |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 2513 | req->sequence -= span; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 2514 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2515 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2516 | data = req->timeout.data; |
| 2517 | data->timer.function = io_timeout_fn; |
| 2518 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 2519 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2520 | return 0; |
| 2521 | } |
| 2522 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 2523 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 2524 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 2525 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 2526 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 2527 | return req->user_data == (unsigned long) data; |
| 2528 | } |
| 2529 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 2530 | static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr) |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 2531 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 2532 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 2533 | int ret = 0; |
| 2534 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 2535 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
| 2536 | switch (cancel_ret) { |
| 2537 | case IO_WQ_CANCEL_OK: |
| 2538 | ret = 0; |
| 2539 | break; |
| 2540 | case IO_WQ_CANCEL_RUNNING: |
| 2541 | ret = -EALREADY; |
| 2542 | break; |
| 2543 | case IO_WQ_CANCEL_NOTFOUND: |
| 2544 | ret = -ENOENT; |
| 2545 | break; |
| 2546 | } |
| 2547 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 2548 | return ret; |
| 2549 | } |
| 2550 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2551 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 2552 | struct io_kiocb *req, __u64 sqe_addr, |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2553 | struct io_kiocb **nxt, int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2554 | { |
| 2555 | unsigned long flags; |
| 2556 | int ret; |
| 2557 | |
| 2558 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 2559 | if (ret != -ENOENT) { |
| 2560 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 2561 | goto done; |
| 2562 | } |
| 2563 | |
| 2564 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 2565 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 2566 | if (ret != -ENOENT) |
| 2567 | goto done; |
| 2568 | ret = io_poll_cancel(ctx, sqe_addr); |
| 2569 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2570 | if (!ret) |
| 2571 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2572 | io_cqring_fill_event(req, ret); |
| 2573 | io_commit_cqring(ctx); |
| 2574 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 2575 | io_cqring_ev_posted(ctx); |
| 2576 | |
| 2577 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
| 2578 | req->flags |= REQ_F_FAIL_LINK; |
| 2579 | io_put_req_find_next(req, nxt); |
| 2580 | } |
| 2581 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 2582 | static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2583 | struct io_kiocb **nxt) |
| 2584 | { |
| 2585 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 2586 | |
| 2587 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2588 | return -EINVAL; |
| 2589 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 2590 | sqe->cancel_flags) |
| 2591 | return -EINVAL; |
| 2592 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2593 | io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 2594 | return 0; |
| 2595 | } |
| 2596 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2597 | static int io_req_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 2598 | { |
| 2599 | struct io_uring_sqe *sqe_copy; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2600 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 2601 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 2602 | /* Still need defer if there is pending req in defer list. */ |
| 2603 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 2604 | return 0; |
| 2605 | |
| 2606 | sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL); |
| 2607 | if (!sqe_copy) |
| 2608 | return -EAGAIN; |
| 2609 | |
| 2610 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 2611 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 2612 | spin_unlock_irq(&ctx->completion_lock); |
| 2613 | kfree(sqe_copy); |
| 2614 | return 0; |
| 2615 | } |
| 2616 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2617 | memcpy(sqe_copy, req->sqe, sizeof(*sqe_copy)); |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 2618 | req->flags |= REQ_F_FREE_SQE; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2619 | req->sqe = sqe_copy; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 2620 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 2621 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 2622 | list_add_tail(&req->list, &ctx->defer_list); |
| 2623 | spin_unlock_irq(&ctx->completion_lock); |
| 2624 | return -EIOCBQUEUED; |
| 2625 | } |
| 2626 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 2627 | __attribute__((nonnull)) |
Pavel Begunkov | d732447 | 2019-11-21 21:24:36 +0300 | [diff] [blame] | 2628 | static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2629 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2630 | { |
Jens Axboe | e0c5c57 | 2019-03-12 10:18:47 -0600 | [diff] [blame] | 2631 | int ret, opcode; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2632 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2633 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2634 | opcode = READ_ONCE(req->sqe->opcode); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2635 | switch (opcode) { |
| 2636 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2637 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2638 | break; |
| 2639 | case IORING_OP_READV: |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2640 | if (unlikely(req->sqe->buf_index)) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2641 | return -EINVAL; |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2642 | ret = io_read(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2643 | break; |
| 2644 | case IORING_OP_WRITEV: |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2645 | if (unlikely(req->sqe->buf_index)) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2646 | return -EINVAL; |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2647 | ret = io_write(req, nxt, force_nonblock); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2648 | break; |
| 2649 | case IORING_OP_READ_FIXED: |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2650 | ret = io_read(req, nxt, force_nonblock); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2651 | break; |
| 2652 | case IORING_OP_WRITE_FIXED: |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2653 | ret = io_write(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2654 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2655 | case IORING_OP_FSYNC: |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2656 | ret = io_fsync(req, req->sqe, nxt, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2657 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2658 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2659 | ret = io_poll_add(req, req->sqe, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2660 | break; |
| 2661 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2662 | ret = io_poll_remove(req, req->sqe); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 2663 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2664 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2665 | ret = io_sync_file_range(req, req->sqe, nxt, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2666 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 2667 | case IORING_OP_SENDMSG: |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2668 | ret = io_sendmsg(req, req->sqe, nxt, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 2669 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 2670 | case IORING_OP_RECVMSG: |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2671 | ret = io_recvmsg(req, req->sqe, nxt, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 2672 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2673 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2674 | ret = io_timeout(req, req->sqe); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 2675 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2676 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2677 | ret = io_timeout_remove(req, req->sqe); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 2678 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 2679 | case IORING_OP_ACCEPT: |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2680 | ret = io_accept(req, req->sqe, nxt, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 2681 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 2682 | case IORING_OP_CONNECT: |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2683 | ret = io_connect(req, req->sqe, nxt, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 2684 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 2685 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2686 | ret = io_async_cancel(req, req->sqe, nxt); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 2687 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2688 | default: |
| 2689 | ret = -EINVAL; |
| 2690 | break; |
| 2691 | } |
| 2692 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2693 | if (ret) |
| 2694 | return ret; |
| 2695 | |
| 2696 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2697 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2698 | return -EAGAIN; |
| 2699 | |
| 2700 | /* workqueue context doesn't hold uring_lock, grab it now */ |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2701 | if (req->in_async) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2702 | mutex_lock(&ctx->uring_lock); |
| 2703 | io_iopoll_req_issued(req); |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2704 | if (req->in_async) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2705 | mutex_unlock(&ctx->uring_lock); |
| 2706 | } |
| 2707 | |
| 2708 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2709 | } |
| 2710 | |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 2711 | static void io_link_work_cb(struct io_wq_work **workptr) |
| 2712 | { |
| 2713 | struct io_wq_work *work = *workptr; |
| 2714 | struct io_kiocb *link = work->data; |
| 2715 | |
| 2716 | io_queue_linked_timeout(link); |
| 2717 | work->func = io_wq_submit_work; |
| 2718 | } |
| 2719 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2720 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2721 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2722 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2723 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2724 | struct io_kiocb *nxt = NULL; |
| 2725 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2726 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2727 | /* Ensure we clear previously set non-block flag */ |
| 2728 | req->rw.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2729 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2730 | if (work->flags & IO_WQ_WORK_CANCEL) |
| 2731 | ret = -ECANCELED; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2732 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2733 | if (!ret) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2734 | req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0; |
| 2735 | req->in_async = true; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2736 | do { |
Pavel Begunkov | d732447 | 2019-11-21 21:24:36 +0300 | [diff] [blame] | 2737 | ret = io_issue_sqe(req, &nxt, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2738 | /* |
| 2739 | * We can get EAGAIN for polled IO even though we're |
| 2740 | * forcing a sync submission from here, since we can't |
| 2741 | * wait for request slots on the block side. |
| 2742 | */ |
| 2743 | if (ret != -EAGAIN) |
| 2744 | break; |
| 2745 | cond_resched(); |
| 2746 | } while (1); |
| 2747 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2748 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2749 | /* drop submission reference */ |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 2750 | io_put_req(req); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 2751 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2752 | if (ret) { |
Jens Axboe | f1f4085 | 2019-11-05 20:33:16 -0700 | [diff] [blame] | 2753 | if (req->flags & REQ_F_LINK) |
| 2754 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2755 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 2756 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2757 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2758 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2759 | /* if a dependent link is ready, pass it back */ |
| 2760 | if (!ret && nxt) { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 2761 | struct io_kiocb *link; |
| 2762 | |
| 2763 | io_prep_async_work(nxt, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 2764 | *workptr = &nxt->work; |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 2765 | if (link) { |
| 2766 | nxt->work.flags |= IO_WQ_WORK_CB; |
| 2767 | nxt->work.func = io_link_work_cb; |
| 2768 | nxt->work.data = link; |
| 2769 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2770 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2771 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2772 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2773 | static bool io_op_needs_file(const struct io_uring_sqe *sqe) |
| 2774 | { |
| 2775 | int op = READ_ONCE(sqe->opcode); |
| 2776 | |
| 2777 | switch (op) { |
| 2778 | case IORING_OP_NOP: |
| 2779 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | 5683e54 | 2019-11-14 00:59:19 +0300 | [diff] [blame] | 2780 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | a320e9f | 2019-11-14 00:11:01 +0300 | [diff] [blame] | 2781 | case IORING_OP_TIMEOUT_REMOVE: |
| 2782 | case IORING_OP_ASYNC_CANCEL: |
| 2783 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2784 | return false; |
| 2785 | default: |
| 2786 | return true; |
| 2787 | } |
| 2788 | } |
| 2789 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 2790 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 2791 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2792 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 2793 | struct fixed_file_table *table; |
| 2794 | |
| 2795 | table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT]; |
| 2796 | return table->files[index & IORING_FILE_TABLE_MASK]; |
| 2797 | } |
| 2798 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2799 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2800 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2801 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2802 | unsigned flags; |
| 2803 | int fd; |
| 2804 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2805 | flags = READ_ONCE(req->sqe->flags); |
| 2806 | fd = READ_ONCE(req->sqe->fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2807 | |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 2808 | if (flags & IOSQE_IO_DRAIN) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 2809 | req->flags |= REQ_F_IO_DRAIN; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 2810 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2811 | if (!io_op_needs_file(req->sqe)) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2812 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2813 | |
| 2814 | if (flags & IOSQE_FIXED_FILE) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 2815 | if (unlikely(!ctx->file_table || |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2816 | (unsigned) fd >= ctx->nr_user_files)) |
| 2817 | return -EBADF; |
Jens Axboe | b762012 | 2019-10-26 07:22:55 -0600 | [diff] [blame] | 2818 | fd = array_index_nospec(fd, ctx->nr_user_files); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 2819 | req->file = io_file_from_index(ctx, fd); |
| 2820 | if (!req->file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 2821 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2822 | req->flags |= REQ_F_FIXED_FILE; |
| 2823 | } else { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2824 | if (req->needs_fixed_file) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2825 | return -EBADF; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 2826 | trace_io_uring_file_get(ctx, fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2827 | req->file = io_file_get(state, fd); |
| 2828 | if (unlikely(!req->file)) |
| 2829 | return -EBADF; |
| 2830 | } |
| 2831 | |
| 2832 | return 0; |
| 2833 | } |
| 2834 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2835 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2836 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 2837 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 2838 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 2839 | |
| 2840 | rcu_read_lock(); |
| 2841 | spin_lock_irq(&ctx->inflight_lock); |
| 2842 | /* |
| 2843 | * We use the f_ops->flush() handler to ensure that we can flush |
| 2844 | * out work accessing these files if the fd is closed. Check if |
| 2845 | * the fd has changed since we started down this path, and disallow |
| 2846 | * this operation if it has. |
| 2847 | */ |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2848 | if (fcheck(req->ring_fd) == req->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 2849 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 2850 | req->flags |= REQ_F_INFLIGHT; |
| 2851 | req->work.files = current->files; |
| 2852 | ret = 0; |
| 2853 | } |
| 2854 | spin_unlock_irq(&ctx->inflight_lock); |
| 2855 | rcu_read_unlock(); |
| 2856 | |
| 2857 | return ret; |
| 2858 | } |
| 2859 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2860 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 2861 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2862 | struct io_timeout_data *data = container_of(timer, |
| 2863 | struct io_timeout_data, timer); |
| 2864 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2865 | struct io_ring_ctx *ctx = req->ctx; |
| 2866 | struct io_kiocb *prev = NULL; |
| 2867 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2868 | |
| 2869 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 2870 | |
| 2871 | /* |
| 2872 | * We don't expect the list to be empty, that will only happen if we |
| 2873 | * race with the completion of the linked work. |
| 2874 | */ |
| 2875 | if (!list_empty(&req->list)) { |
| 2876 | prev = list_entry(req->list.prev, struct io_kiocb, link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 2877 | if (refcount_inc_not_zero(&prev->refs)) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 2878 | list_del_init(&req->list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 2879 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 2880 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 2881 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2882 | } |
| 2883 | |
| 2884 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 2885 | |
| 2886 | if (prev) { |
Jens Axboe | fba38c2 | 2019-11-18 12:27:57 -0700 | [diff] [blame] | 2887 | if (prev->flags & REQ_F_LINK) |
| 2888 | prev->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 2889 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
| 2890 | -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 2891 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 2892 | } else { |
| 2893 | io_cqring_add_event(req, -ETIME); |
| 2894 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2895 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2896 | return HRTIMER_NORESTART; |
| 2897 | } |
| 2898 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2899 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2900 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 2901 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2902 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 2903 | /* |
| 2904 | * If the list is now empty, then our linked request finished before |
| 2905 | * we got a chance to setup the timer |
| 2906 | */ |
| 2907 | spin_lock_irq(&ctx->completion_lock); |
| 2908 | if (!list_empty(&req->list)) { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 2909 | struct io_timeout_data *data = req->timeout.data; |
| 2910 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2911 | data->timer.function = io_link_timeout_fn; |
| 2912 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 2913 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2914 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 2915 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2916 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2917 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 2918 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2919 | } |
| 2920 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 2921 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2922 | { |
| 2923 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2924 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2925 | if (!(req->flags & REQ_F_LINK)) |
| 2926 | return NULL; |
| 2927 | |
| 2928 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list); |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2929 | if (!nxt || nxt->sqe->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 2930 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2931 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 2932 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 2933 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2934 | } |
| 2935 | |
Jens Axboe | 0e0702d | 2019-11-14 21:42:10 -0700 | [diff] [blame] | 2936 | static void __io_queue_sqe(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2937 | { |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 2938 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
| 2939 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2940 | int ret; |
| 2941 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 2942 | ret = io_issue_sqe(req, &nxt, true); |
| 2943 | if (nxt) |
| 2944 | io_queue_async_work(nxt); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2945 | |
| 2946 | /* |
| 2947 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 2948 | * doesn't support non-blocking read/write attempts |
| 2949 | */ |
| 2950 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 2951 | (req->flags & REQ_F_MUST_PUNT))) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2952 | struct io_uring_sqe *sqe_copy; |
| 2953 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2954 | sqe_copy = kmemdup(req->sqe, sizeof(*sqe_copy), GFP_KERNEL); |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 2955 | if (!sqe_copy) |
| 2956 | goto err; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2957 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 2958 | req->sqe = sqe_copy; |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 2959 | req->flags |= REQ_F_FREE_SQE; |
| 2960 | |
| 2961 | if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) { |
| 2962 | ret = io_grab_files(req); |
| 2963 | if (ret) |
| 2964 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2965 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 2966 | |
| 2967 | /* |
| 2968 | * Queued up for async execution, worker will release |
| 2969 | * submit reference when the iocb is actually submitted. |
| 2970 | */ |
| 2971 | io_queue_async_work(req); |
| 2972 | return; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2973 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2974 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 2975 | err: |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2976 | /* drop submission reference */ |
| 2977 | io_put_req(req); |
| 2978 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 2979 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 2980 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 2981 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 2982 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 2983 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 2984 | } |
| 2985 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2986 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2987 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2988 | io_cqring_add_event(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2989 | if (req->flags & REQ_F_LINK) |
| 2990 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2991 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2992 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2993 | } |
| 2994 | |
Jens Axboe | 0e0702d | 2019-11-14 21:42:10 -0700 | [diff] [blame] | 2995 | static void io_queue_sqe(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 2996 | { |
| 2997 | int ret; |
| 2998 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 2999 | if (unlikely(req->ctx->drain_next)) { |
| 3000 | req->flags |= REQ_F_IO_DRAIN; |
| 3001 | req->ctx->drain_next = false; |
| 3002 | } |
| 3003 | req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK); |
| 3004 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3005 | ret = io_req_defer(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 3006 | if (ret) { |
| 3007 | if (ret != -EIOCBQUEUED) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3008 | io_cqring_add_event(req, ret); |
Pavel Begunkov | d3b35796 | 2019-11-19 23:32:48 +0300 | [diff] [blame] | 3009 | if (req->flags & REQ_F_LINK) |
| 3010 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3011 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 3012 | } |
Jens Axboe | 0e0702d | 2019-11-14 21:42:10 -0700 | [diff] [blame] | 3013 | } else |
| 3014 | __io_queue_sqe(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 3015 | } |
| 3016 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 3017 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 3018 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 3019 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 3020 | io_cqring_add_event(req, -ECANCELED); |
| 3021 | io_double_put_req(req); |
| 3022 | } else |
Jens Axboe | 0e0702d | 2019-11-14 21:42:10 -0700 | [diff] [blame] | 3023 | io_queue_sqe(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 3024 | } |
| 3025 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 3026 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3027 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK) |
| 3028 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3029 | static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state, |
| 3030 | struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3031 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3032 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3033 | int ret; |
| 3034 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 3035 | req->user_data = req->sqe->user_data; |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3036 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3037 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 3038 | if (unlikely(req->sqe->flags & ~SQE_VALID_FLAGS)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3039 | ret = -EINVAL; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 3040 | goto err_req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3041 | } |
| 3042 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3043 | ret = io_req_set_file(state, req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3044 | if (unlikely(ret)) { |
| 3045 | err_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3046 | io_cqring_add_event(req, ret); |
| 3047 | io_double_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3048 | return; |
| 3049 | } |
| 3050 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3051 | /* |
| 3052 | * If we already have a head request, queue this one for async |
| 3053 | * submittal once the head completes. If we don't have a head but |
| 3054 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 3055 | * submitted sync once the chain is complete. If none of those |
| 3056 | * conditions are true (normal request), then just queue it. |
| 3057 | */ |
| 3058 | if (*link) { |
| 3059 | struct io_kiocb *prev = *link; |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 3060 | struct io_uring_sqe *sqe_copy; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3061 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 3062 | if (req->sqe->flags & IOSQE_IO_DRAIN) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 3063 | (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN; |
| 3064 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 3065 | if (READ_ONCE(req->sqe->opcode) == IORING_OP_LINK_TIMEOUT) { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 3066 | ret = io_timeout_setup(req); |
| 3067 | /* common setup allows offset being set, we don't */ |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 3068 | if (!ret && req->sqe->off) |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 3069 | ret = -EINVAL; |
| 3070 | if (ret) { |
| 3071 | prev->flags |= REQ_F_FAIL_LINK; |
| 3072 | goto err_req; |
| 3073 | } |
| 3074 | } |
| 3075 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 3076 | sqe_copy = kmemdup(req->sqe, sizeof(*sqe_copy), GFP_KERNEL); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3077 | if (!sqe_copy) { |
| 3078 | ret = -EAGAIN; |
| 3079 | goto err_req; |
| 3080 | } |
| 3081 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 3082 | req->sqe = sqe_copy; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 3083 | req->flags |= REQ_F_FREE_SQE; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 3084 | trace_io_uring_link(ctx, req, prev); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3085 | list_add_tail(&req->list, &prev->link_list); |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 3086 | } else if (req->sqe->flags & IOSQE_IO_LINK) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3087 | req->flags |= REQ_F_LINK; |
| 3088 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3089 | INIT_LIST_HEAD(&req->link_list); |
| 3090 | *link = req; |
| 3091 | } else { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3092 | io_queue_sqe(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3093 | } |
| 3094 | } |
| 3095 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 3096 | /* |
| 3097 | * Batched submission is done, ensure local IO is flushed out. |
| 3098 | */ |
| 3099 | static void io_submit_state_end(struct io_submit_state *state) |
| 3100 | { |
| 3101 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 3102 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 3103 | if (state->free_reqs) |
| 3104 | kmem_cache_free_bulk(req_cachep, state->free_reqs, |
| 3105 | &state->reqs[state->cur_req]); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | /* |
| 3109 | * Start submission side cache. |
| 3110 | */ |
| 3111 | static void io_submit_state_start(struct io_submit_state *state, |
| 3112 | struct io_ring_ctx *ctx, unsigned max_ios) |
| 3113 | { |
| 3114 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 3115 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 3116 | state->file = NULL; |
| 3117 | state->ios_left = max_ios; |
| 3118 | } |
| 3119 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3120 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 3121 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3122 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3123 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3124 | if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3125 | /* |
| 3126 | * Ensure any loads from the SQEs are done at this point, |
| 3127 | * since once we write the new head, the application could |
| 3128 | * write new data to them. |
| 3129 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3130 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3131 | } |
| 3132 | } |
| 3133 | |
| 3134 | /* |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3135 | * Fetch an sqe, if one is available. Note that s->sqe will point to memory |
| 3136 | * that is mapped by userspace. This means that care needs to be taken to |
| 3137 | * ensure that reads are stable, as we cannot rely on userspace always |
| 3138 | * being a good citizen. If members of the sqe are validated and then later |
| 3139 | * used, it's important that those reads are done through READ_ONCE() to |
| 3140 | * prevent a re-load down the line. |
| 3141 | */ |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 3142 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3143 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3144 | struct io_rings *rings = ctx->rings; |
| 3145 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3146 | unsigned head; |
| 3147 | |
| 3148 | /* |
| 3149 | * The cached sq head (or cq tail) serves two purposes: |
| 3150 | * |
| 3151 | * 1) allows us to batch the cost of updating the user visible |
| 3152 | * head updates. |
| 3153 | * 2) allows the kernel side to track the head on its own, even |
| 3154 | * though the application is the one updating it. |
| 3155 | */ |
| 3156 | head = ctx->cached_sq_head; |
Stefan Bühler | e523a29 | 2019-04-19 11:57:44 +0200 | [diff] [blame] | 3157 | /* make sure SQ entry isn't read before tail */ |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 3158 | if (unlikely(head == smp_load_acquire(&rings->sq.tail))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3159 | return false; |
| 3160 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3161 | head = READ_ONCE(sq_array[head & ctx->sq_mask]); |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 3162 | if (likely(head < ctx->sq_entries)) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 3163 | /* |
| 3164 | * All io need record the previous position, if LINK vs DARIN, |
| 3165 | * it can be used to mark the position of the first IO in the |
| 3166 | * link list. |
| 3167 | */ |
| 3168 | req->sequence = ctx->cached_sq_head; |
| 3169 | req->sqe = &ctx->sq_sqes[head]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3170 | ctx->cached_sq_head++; |
| 3171 | return true; |
| 3172 | } |
| 3173 | |
| 3174 | /* drop invalid entries */ |
| 3175 | ctx->cached_sq_head++; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 3176 | ctx->cached_sq_dropped++; |
| 3177 | WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3178 | return false; |
| 3179 | } |
| 3180 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 3181 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr, |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 3182 | struct file *ring_file, int ring_fd, |
| 3183 | struct mm_struct **mm, bool async) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3184 | { |
| 3185 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3186 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3187 | int i, submitted = 0; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 3188 | bool mm_fault = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3189 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 3190 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
| 3191 | if (!list_empty(&ctx->cq_overflow_list) && |
| 3192 | !io_cqring_overflow_flush(ctx, false)) |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 3193 | return -EBUSY; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3194 | |
| 3195 | if (nr > IO_PLUG_THRESHOLD) { |
| 3196 | io_submit_state_start(&state, ctx, nr); |
| 3197 | statep = &state; |
| 3198 | } |
| 3199 | |
| 3200 | for (i = 0; i < nr; i++) { |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 3201 | struct io_kiocb *req; |
Pavel Begunkov | 50585b9 | 2019-11-07 01:41:07 +0300 | [diff] [blame] | 3202 | unsigned int sqe_flags; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 3203 | |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 3204 | req = io_get_req(ctx, statep); |
| 3205 | if (unlikely(!req)) { |
| 3206 | if (!submitted) |
| 3207 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 3208 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3209 | } |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 3210 | if (!io_get_sqring(ctx, req)) { |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 3211 | __io_free_req(req); |
| 3212 | break; |
| 3213 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3214 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 3215 | if (io_sqe_needs_user(req->sqe) && !*mm) { |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 3216 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
| 3217 | if (!mm_fault) { |
| 3218 | use_mm(ctx->sqo_mm); |
| 3219 | *mm = ctx->sqo_mm; |
| 3220 | } |
| 3221 | } |
| 3222 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 3223 | sqe_flags = req->sqe->flags; |
Pavel Begunkov | 50585b9 | 2019-11-07 01:41:07 +0300 | [diff] [blame] | 3224 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame^] | 3225 | req->ring_file = ring_file; |
| 3226 | req->ring_fd = ring_fd; |
| 3227 | req->has_user = *mm != NULL; |
| 3228 | req->in_async = async; |
| 3229 | req->needs_fixed_file = async; |
| 3230 | trace_io_uring_submit_sqe(ctx, req->sqe->user_data, |
Pavel Begunkov | 50585b9 | 2019-11-07 01:41:07 +0300 | [diff] [blame] | 3231 | true, async); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3232 | io_submit_sqe(req, statep, &link); |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 3233 | submitted++; |
Pavel Begunkov | e5eb636 | 2019-11-06 00:22:15 +0300 | [diff] [blame] | 3234 | |
| 3235 | /* |
| 3236 | * If previous wasn't linked and we have a linked command, |
| 3237 | * that's the end of the chain. Submit the previous link. |
| 3238 | */ |
Pavel Begunkov | 50585b9 | 2019-11-07 01:41:07 +0300 | [diff] [blame] | 3239 | if (!(sqe_flags & IOSQE_IO_LINK) && link) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 3240 | io_queue_link_head(link); |
Pavel Begunkov | e5eb636 | 2019-11-06 00:22:15 +0300 | [diff] [blame] | 3241 | link = NULL; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3242 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3243 | } |
| 3244 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3245 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 3246 | io_queue_link_head(link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3247 | if (statep) |
| 3248 | io_submit_state_end(&state); |
| 3249 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 3250 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 3251 | io_commit_sqring(ctx); |
| 3252 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3253 | return submitted; |
| 3254 | } |
| 3255 | |
| 3256 | static int io_sq_thread(void *data) |
| 3257 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3258 | struct io_ring_ctx *ctx = data; |
| 3259 | struct mm_struct *cur_mm = NULL; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 3260 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3261 | mm_segment_t old_fs; |
| 3262 | DEFINE_WAIT(wait); |
| 3263 | unsigned inflight; |
| 3264 | unsigned long timeout; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 3265 | int ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3266 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 3267 | complete(&ctx->completions[1]); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 3268 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3269 | old_fs = get_fs(); |
| 3270 | set_fs(USER_DS); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 3271 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3272 | |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 3273 | ret = timeout = inflight = 0; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 3274 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 3275 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3276 | |
| 3277 | if (inflight) { |
| 3278 | unsigned nr_events = 0; |
| 3279 | |
| 3280 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 3281 | /* |
| 3282 | * inflight is the count of the maximum possible |
| 3283 | * entries we submitted, but it can be smaller |
| 3284 | * if we dropped some of them. If we don't have |
| 3285 | * poll entries available, then we know that we |
| 3286 | * have nothing left to poll for. Reset the |
| 3287 | * inflight count to zero in that case. |
| 3288 | */ |
| 3289 | mutex_lock(&ctx->uring_lock); |
| 3290 | if (!list_empty(&ctx->poll_list)) |
| 3291 | __io_iopoll_check(ctx, &nr_events, 0); |
| 3292 | else |
| 3293 | inflight = 0; |
| 3294 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3295 | } else { |
| 3296 | /* |
| 3297 | * Normal IO, just pretend everything completed. |
| 3298 | * We don't have to poll completions for that. |
| 3299 | */ |
| 3300 | nr_events = inflight; |
| 3301 | } |
| 3302 | |
| 3303 | inflight -= nr_events; |
| 3304 | if (!inflight) |
| 3305 | timeout = jiffies + ctx->sq_thread_idle; |
| 3306 | } |
| 3307 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 3308 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 3309 | |
| 3310 | /* |
| 3311 | * If submit got -EBUSY, flag us as needing the application |
| 3312 | * to enter the kernel to reap and flush events. |
| 3313 | */ |
| 3314 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3315 | /* |
| 3316 | * We're polling. If we're within the defined idle |
| 3317 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 3318 | * to sleep. The exception is if we got EBUSY doing |
| 3319 | * more IO, we should wait for the application to |
| 3320 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3321 | */ |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 3322 | if (inflight || |
| 3323 | (!time_after(jiffies, timeout) && ret != -EBUSY)) { |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 3324 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3325 | continue; |
| 3326 | } |
| 3327 | |
| 3328 | /* |
| 3329 | * Drop cur_mm before scheduling, we can't hold it for |
| 3330 | * long periods (or over schedule()). Do this before |
| 3331 | * adding ourselves to the waitqueue, as the unuse/drop |
| 3332 | * may sleep. |
| 3333 | */ |
| 3334 | if (cur_mm) { |
| 3335 | unuse_mm(cur_mm); |
| 3336 | mmput(cur_mm); |
| 3337 | cur_mm = NULL; |
| 3338 | } |
| 3339 | |
| 3340 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 3341 | TASK_INTERRUPTIBLE); |
| 3342 | |
| 3343 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3344 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 3345 | /* make sure to read SQ tail after writing flags */ |
| 3346 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3347 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 3348 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 3349 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 3350 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3351 | finish_wait(&ctx->sqo_wait, &wait); |
| 3352 | break; |
| 3353 | } |
| 3354 | if (signal_pending(current)) |
| 3355 | flush_signals(current); |
| 3356 | schedule(); |
| 3357 | finish_wait(&ctx->sqo_wait, &wait); |
| 3358 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3359 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3360 | continue; |
| 3361 | } |
| 3362 | finish_wait(&ctx->sqo_wait, &wait); |
| 3363 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3364 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3365 | } |
| 3366 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 3367 | to_submit = min(to_submit, ctx->sq_entries); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 3368 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
| 3369 | if (ret > 0) |
| 3370 | inflight += ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3371 | } |
| 3372 | |
| 3373 | set_fs(old_fs); |
| 3374 | if (cur_mm) { |
| 3375 | unuse_mm(cur_mm); |
| 3376 | mmput(cur_mm); |
| 3377 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 3378 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 3379 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 3380 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 3381 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3382 | return 0; |
| 3383 | } |
| 3384 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 3385 | struct io_wait_queue { |
| 3386 | struct wait_queue_entry wq; |
| 3387 | struct io_ring_ctx *ctx; |
| 3388 | unsigned to_wait; |
| 3389 | unsigned nr_timeouts; |
| 3390 | }; |
| 3391 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 3392 | static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 3393 | { |
| 3394 | struct io_ring_ctx *ctx = iowq->ctx; |
| 3395 | |
| 3396 | /* |
| 3397 | * Wake up if we have enough events, or if a timeout occured since we |
| 3398 | * started waiting. For timeouts, we always want to return to userspace, |
| 3399 | * regardless of event count. |
| 3400 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 3401 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 3402 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 3403 | } |
| 3404 | |
| 3405 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 3406 | int wake_flags, void *key) |
| 3407 | { |
| 3408 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 3409 | wq); |
| 3410 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 3411 | /* use noflush == true, as we can't safely rely on locking context */ |
| 3412 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 3413 | return -1; |
| 3414 | |
| 3415 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 3416 | } |
| 3417 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3418 | /* |
| 3419 | * Wait until events become available, if we don't already have some. The |
| 3420 | * application must reap them itself, as they reside on the shared cq ring. |
| 3421 | */ |
| 3422 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 3423 | const sigset_t __user *sig, size_t sigsz) |
| 3424 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 3425 | struct io_wait_queue iowq = { |
| 3426 | .wq = { |
| 3427 | .private = current, |
| 3428 | .func = io_wake_function, |
| 3429 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 3430 | }, |
| 3431 | .ctx = ctx, |
| 3432 | .to_wait = min_events, |
| 3433 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3434 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 3435 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3436 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 3437 | if (io_cqring_events(ctx, false) >= min_events) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3438 | return 0; |
| 3439 | |
| 3440 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 3441 | #ifdef CONFIG_COMPAT |
| 3442 | if (in_compat_syscall()) |
| 3443 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 3444 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 3445 | else |
| 3446 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 3447 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 3448 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3449 | if (ret) |
| 3450 | return ret; |
| 3451 | } |
| 3452 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 3453 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 3454 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 3455 | do { |
| 3456 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 3457 | TASK_INTERRUPTIBLE); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 3458 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 3459 | break; |
| 3460 | schedule(); |
| 3461 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 3462 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 3463 | break; |
| 3464 | } |
| 3465 | } while (1); |
| 3466 | finish_wait(&ctx->wait, &iowq.wq); |
| 3467 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 3468 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3469 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 3470 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3471 | } |
| 3472 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3473 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 3474 | { |
| 3475 | #if defined(CONFIG_UNIX) |
| 3476 | if (ctx->ring_sock) { |
| 3477 | struct sock *sock = ctx->ring_sock->sk; |
| 3478 | struct sk_buff *skb; |
| 3479 | |
| 3480 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 3481 | kfree_skb(skb); |
| 3482 | } |
| 3483 | #else |
| 3484 | int i; |
| 3485 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3486 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 3487 | struct file *file; |
| 3488 | |
| 3489 | file = io_file_from_index(ctx, i); |
| 3490 | if (file) |
| 3491 | fput(file); |
| 3492 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3493 | #endif |
| 3494 | } |
| 3495 | |
| 3496 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 3497 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3498 | unsigned nr_tables, i; |
| 3499 | |
| 3500 | if (!ctx->file_table) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3501 | return -ENXIO; |
| 3502 | |
| 3503 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3504 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 3505 | for (i = 0; i < nr_tables; i++) |
| 3506 | kfree(ctx->file_table[i].files); |
| 3507 | kfree(ctx->file_table); |
| 3508 | ctx->file_table = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3509 | ctx->nr_user_files = 0; |
| 3510 | return 0; |
| 3511 | } |
| 3512 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3513 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 3514 | { |
| 3515 | if (ctx->sqo_thread) { |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 3516 | wait_for_completion(&ctx->completions[1]); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 3517 | /* |
| 3518 | * The park is a bit of a work-around, without it we get |
| 3519 | * warning spews on shutdown with SQPOLL set and affinity |
| 3520 | * set to a single CPU. |
| 3521 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 3522 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3523 | kthread_stop(ctx->sqo_thread); |
| 3524 | ctx->sqo_thread = NULL; |
| 3525 | } |
| 3526 | } |
| 3527 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3528 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 3529 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3530 | io_sq_thread_stop(ctx); |
| 3531 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3532 | if (ctx->io_wq) { |
| 3533 | io_wq_destroy(ctx->io_wq); |
| 3534 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3535 | } |
| 3536 | } |
| 3537 | |
| 3538 | #if defined(CONFIG_UNIX) |
| 3539 | static void io_destruct_skb(struct sk_buff *skb) |
| 3540 | { |
| 3541 | struct io_ring_ctx *ctx = skb->sk->sk_user_data; |
| 3542 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3543 | if (ctx->io_wq) |
| 3544 | io_wq_flush(ctx->io_wq); |
Jens Axboe | 8a99734 | 2019-10-09 14:40:13 -0600 | [diff] [blame] | 3545 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3546 | unix_destruct_scm(skb); |
| 3547 | } |
| 3548 | |
| 3549 | /* |
| 3550 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 3551 | * the io_uring can be safely unregistered on process exit, even if we have |
| 3552 | * loops in the file referencing. |
| 3553 | */ |
| 3554 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 3555 | { |
| 3556 | struct sock *sk = ctx->ring_sock->sk; |
| 3557 | struct scm_fp_list *fpl; |
| 3558 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 3559 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3560 | |
| 3561 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 3562 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 3563 | |
| 3564 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 3565 | return -EMFILE; |
| 3566 | } |
| 3567 | |
| 3568 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 3569 | if (!fpl) |
| 3570 | return -ENOMEM; |
| 3571 | |
| 3572 | skb = alloc_skb(0, GFP_KERNEL); |
| 3573 | if (!skb) { |
| 3574 | kfree(fpl); |
| 3575 | return -ENOMEM; |
| 3576 | } |
| 3577 | |
| 3578 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3579 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 3580 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3581 | fpl->user = get_uid(ctx->user); |
| 3582 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3583 | struct file *file = io_file_from_index(ctx, i + offset); |
| 3584 | |
| 3585 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 3586 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3587 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 3588 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 3589 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3590 | } |
| 3591 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 3592 | if (nr_files) { |
| 3593 | fpl->max = SCM_MAX_FD; |
| 3594 | fpl->count = nr_files; |
| 3595 | UNIXCB(skb).fp = fpl; |
| 3596 | skb->destructor = io_destruct_skb; |
| 3597 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 3598 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3599 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 3600 | for (i = 0; i < nr_files; i++) |
| 3601 | fput(fpl->fp[i]); |
| 3602 | } else { |
| 3603 | kfree_skb(skb); |
| 3604 | kfree(fpl); |
| 3605 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3606 | |
| 3607 | return 0; |
| 3608 | } |
| 3609 | |
| 3610 | /* |
| 3611 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 3612 | * causes regular reference counting to break down. We rely on the UNIX |
| 3613 | * garbage collection to take care of this problem for us. |
| 3614 | */ |
| 3615 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 3616 | { |
| 3617 | unsigned left, total; |
| 3618 | int ret = 0; |
| 3619 | |
| 3620 | total = 0; |
| 3621 | left = ctx->nr_user_files; |
| 3622 | while (left) { |
| 3623 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3624 | |
| 3625 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 3626 | if (ret) |
| 3627 | break; |
| 3628 | left -= this_files; |
| 3629 | total += this_files; |
| 3630 | } |
| 3631 | |
| 3632 | if (!ret) |
| 3633 | return 0; |
| 3634 | |
| 3635 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3636 | struct file *file = io_file_from_index(ctx, total); |
| 3637 | |
| 3638 | if (file) |
| 3639 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3640 | total++; |
| 3641 | } |
| 3642 | |
| 3643 | return ret; |
| 3644 | } |
| 3645 | #else |
| 3646 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 3647 | { |
| 3648 | return 0; |
| 3649 | } |
| 3650 | #endif |
| 3651 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3652 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 3653 | unsigned nr_files) |
| 3654 | { |
| 3655 | int i; |
| 3656 | |
| 3657 | for (i = 0; i < nr_tables; i++) { |
| 3658 | struct fixed_file_table *table = &ctx->file_table[i]; |
| 3659 | unsigned this_files; |
| 3660 | |
| 3661 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 3662 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 3663 | GFP_KERNEL); |
| 3664 | if (!table->files) |
| 3665 | break; |
| 3666 | nr_files -= this_files; |
| 3667 | } |
| 3668 | |
| 3669 | if (i == nr_tables) |
| 3670 | return 0; |
| 3671 | |
| 3672 | for (i = 0; i < nr_tables; i++) { |
| 3673 | struct fixed_file_table *table = &ctx->file_table[i]; |
| 3674 | kfree(table->files); |
| 3675 | } |
| 3676 | return 1; |
| 3677 | } |
| 3678 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3679 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 3680 | unsigned nr_args) |
| 3681 | { |
| 3682 | __s32 __user *fds = (__s32 __user *) arg; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3683 | unsigned nr_tables; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3684 | int fd, ret = 0; |
| 3685 | unsigned i; |
| 3686 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3687 | if (ctx->file_table) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3688 | return -EBUSY; |
| 3689 | if (!nr_args) |
| 3690 | return -EINVAL; |
| 3691 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 3692 | return -EMFILE; |
| 3693 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3694 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 3695 | ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table), |
| 3696 | GFP_KERNEL); |
| 3697 | if (!ctx->file_table) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3698 | return -ENOMEM; |
| 3699 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3700 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 3701 | kfree(ctx->file_table); |
Jens Axboe | 46568e9 | 2019-11-10 08:40:53 -0700 | [diff] [blame] | 3702 | ctx->file_table = NULL; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3703 | return -ENOMEM; |
| 3704 | } |
| 3705 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 3706 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3707 | struct fixed_file_table *table; |
| 3708 | unsigned index; |
| 3709 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3710 | ret = -EFAULT; |
| 3711 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 3712 | break; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 3713 | /* allow sparse sets */ |
| 3714 | if (fd == -1) { |
| 3715 | ret = 0; |
| 3716 | continue; |
| 3717 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3718 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3719 | table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT]; |
| 3720 | index = i & IORING_FILE_TABLE_MASK; |
| 3721 | table->files[index] = fget(fd); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3722 | |
| 3723 | ret = -EBADF; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3724 | if (!table->files[index]) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3725 | break; |
| 3726 | /* |
| 3727 | * Don't allow io_uring instances to be registered. If UNIX |
| 3728 | * isn't enabled, then this causes a reference cycle and this |
| 3729 | * instance can never get freed. If UNIX is enabled we'll |
| 3730 | * handle it just fine, but there's still no point in allowing |
| 3731 | * a ring fd as it doesn't support regular read/write anyway. |
| 3732 | */ |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3733 | if (table->files[index]->f_op == &io_uring_fops) { |
| 3734 | fput(table->files[index]); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3735 | break; |
| 3736 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3737 | ret = 0; |
| 3738 | } |
| 3739 | |
| 3740 | if (ret) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3741 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 3742 | struct file *file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3743 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3744 | file = io_file_from_index(ctx, i); |
| 3745 | if (file) |
| 3746 | fput(file); |
| 3747 | } |
| 3748 | for (i = 0; i < nr_tables; i++) |
| 3749 | kfree(ctx->file_table[i].files); |
| 3750 | |
| 3751 | kfree(ctx->file_table); |
| 3752 | ctx->file_table = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3753 | ctx->nr_user_files = 0; |
| 3754 | return ret; |
| 3755 | } |
| 3756 | |
| 3757 | ret = io_sqe_files_scm(ctx); |
| 3758 | if (ret) |
| 3759 | io_sqe_files_unregister(ctx); |
| 3760 | |
| 3761 | return ret; |
| 3762 | } |
| 3763 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 3764 | static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index) |
| 3765 | { |
| 3766 | #if defined(CONFIG_UNIX) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3767 | struct file *file = io_file_from_index(ctx, index); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 3768 | struct sock *sock = ctx->ring_sock->sk; |
| 3769 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 3770 | struct sk_buff *skb; |
| 3771 | int i; |
| 3772 | |
| 3773 | __skb_queue_head_init(&list); |
| 3774 | |
| 3775 | /* |
| 3776 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 3777 | * remove this entry and rearrange the file array. |
| 3778 | */ |
| 3779 | skb = skb_dequeue(head); |
| 3780 | while (skb) { |
| 3781 | struct scm_fp_list *fp; |
| 3782 | |
| 3783 | fp = UNIXCB(skb).fp; |
| 3784 | for (i = 0; i < fp->count; i++) { |
| 3785 | int left; |
| 3786 | |
| 3787 | if (fp->fp[i] != file) |
| 3788 | continue; |
| 3789 | |
| 3790 | unix_notinflight(fp->user, fp->fp[i]); |
| 3791 | left = fp->count - 1 - i; |
| 3792 | if (left) { |
| 3793 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 3794 | left * sizeof(struct file *)); |
| 3795 | } |
| 3796 | fp->count--; |
| 3797 | if (!fp->count) { |
| 3798 | kfree_skb(skb); |
| 3799 | skb = NULL; |
| 3800 | } else { |
| 3801 | __skb_queue_tail(&list, skb); |
| 3802 | } |
| 3803 | fput(file); |
| 3804 | file = NULL; |
| 3805 | break; |
| 3806 | } |
| 3807 | |
| 3808 | if (!file) |
| 3809 | break; |
| 3810 | |
| 3811 | __skb_queue_tail(&list, skb); |
| 3812 | |
| 3813 | skb = skb_dequeue(head); |
| 3814 | } |
| 3815 | |
| 3816 | if (skb_peek(&list)) { |
| 3817 | spin_lock_irq(&head->lock); |
| 3818 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 3819 | __skb_queue_tail(head, skb); |
| 3820 | spin_unlock_irq(&head->lock); |
| 3821 | } |
| 3822 | #else |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3823 | fput(io_file_from_index(ctx, index)); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 3824 | #endif |
| 3825 | } |
| 3826 | |
| 3827 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 3828 | int index) |
| 3829 | { |
| 3830 | #if defined(CONFIG_UNIX) |
| 3831 | struct sock *sock = ctx->ring_sock->sk; |
| 3832 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 3833 | struct sk_buff *skb; |
| 3834 | |
| 3835 | /* |
| 3836 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 3837 | * file set. If there's no room, fall back to allocating a new skb |
| 3838 | * and filling it in. |
| 3839 | */ |
| 3840 | spin_lock_irq(&head->lock); |
| 3841 | skb = skb_peek(head); |
| 3842 | if (skb) { |
| 3843 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 3844 | |
| 3845 | if (fpl->count < SCM_MAX_FD) { |
| 3846 | __skb_unlink(skb, head); |
| 3847 | spin_unlock_irq(&head->lock); |
| 3848 | fpl->fp[fpl->count] = get_file(file); |
| 3849 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 3850 | fpl->count++; |
| 3851 | spin_lock_irq(&head->lock); |
| 3852 | __skb_queue_head(head, skb); |
| 3853 | } else { |
| 3854 | skb = NULL; |
| 3855 | } |
| 3856 | } |
| 3857 | spin_unlock_irq(&head->lock); |
| 3858 | |
| 3859 | if (skb) { |
| 3860 | fput(file); |
| 3861 | return 0; |
| 3862 | } |
| 3863 | |
| 3864 | return __io_sqe_files_scm(ctx, 1, index); |
| 3865 | #else |
| 3866 | return 0; |
| 3867 | #endif |
| 3868 | } |
| 3869 | |
| 3870 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 3871 | unsigned nr_args) |
| 3872 | { |
| 3873 | struct io_uring_files_update up; |
| 3874 | __s32 __user *fds; |
| 3875 | int fd, i, err; |
| 3876 | __u32 done; |
| 3877 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3878 | if (!ctx->file_table) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 3879 | return -ENXIO; |
| 3880 | if (!nr_args) |
| 3881 | return -EINVAL; |
| 3882 | if (copy_from_user(&up, arg, sizeof(up))) |
| 3883 | return -EFAULT; |
| 3884 | if (check_add_overflow(up.offset, nr_args, &done)) |
| 3885 | return -EOVERFLOW; |
| 3886 | if (done > ctx->nr_user_files) |
| 3887 | return -EINVAL; |
| 3888 | |
| 3889 | done = 0; |
| 3890 | fds = (__s32 __user *) up.fds; |
| 3891 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3892 | struct fixed_file_table *table; |
| 3893 | unsigned index; |
| 3894 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 3895 | err = 0; |
| 3896 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 3897 | err = -EFAULT; |
| 3898 | break; |
| 3899 | } |
| 3900 | i = array_index_nospec(up.offset, ctx->nr_user_files); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3901 | table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT]; |
| 3902 | index = i & IORING_FILE_TABLE_MASK; |
| 3903 | if (table->files[index]) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 3904 | io_sqe_file_unregister(ctx, i); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3905 | table->files[index] = NULL; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 3906 | } |
| 3907 | if (fd != -1) { |
| 3908 | struct file *file; |
| 3909 | |
| 3910 | file = fget(fd); |
| 3911 | if (!file) { |
| 3912 | err = -EBADF; |
| 3913 | break; |
| 3914 | } |
| 3915 | /* |
| 3916 | * Don't allow io_uring instances to be registered. If |
| 3917 | * UNIX isn't enabled, then this causes a reference |
| 3918 | * cycle and this instance can never get freed. If UNIX |
| 3919 | * is enabled we'll handle it just fine, but there's |
| 3920 | * still no point in allowing a ring fd as it doesn't |
| 3921 | * support regular read/write anyway. |
| 3922 | */ |
| 3923 | if (file->f_op == &io_uring_fops) { |
| 3924 | fput(file); |
| 3925 | err = -EBADF; |
| 3926 | break; |
| 3927 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 3928 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 3929 | err = io_sqe_file_register(ctx, file, i); |
| 3930 | if (err) |
| 3931 | break; |
| 3932 | } |
| 3933 | nr_args--; |
| 3934 | done++; |
| 3935 | up.offset++; |
| 3936 | } |
| 3937 | |
| 3938 | return done ? done : err; |
| 3939 | } |
| 3940 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 3941 | static void io_put_work(struct io_wq_work *work) |
| 3942 | { |
| 3943 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 3944 | |
| 3945 | io_put_req(req); |
| 3946 | } |
| 3947 | |
| 3948 | static void io_get_work(struct io_wq_work *work) |
| 3949 | { |
| 3950 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 3951 | |
| 3952 | refcount_inc(&req->refs); |
| 3953 | } |
| 3954 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3955 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 3956 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3957 | { |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 3958 | struct io_wq_data data; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3959 | unsigned concurrency; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3960 | int ret; |
| 3961 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3962 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3963 | mmgrab(current->mm); |
| 3964 | ctx->sqo_mm = current->mm; |
| 3965 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3966 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 3967 | ret = -EPERM; |
| 3968 | if (!capable(CAP_SYS_ADMIN)) |
| 3969 | goto err; |
| 3970 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 3971 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 3972 | if (!ctx->sq_thread_idle) |
| 3973 | ctx->sq_thread_idle = HZ; |
| 3974 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3975 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 3976 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3977 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 3978 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 3979 | if (cpu >= nr_cpu_ids) |
| 3980 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 3981 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 3982 | goto err; |
| 3983 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3984 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 3985 | ctx, cpu, |
| 3986 | "io_uring-sq"); |
| 3987 | } else { |
| 3988 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 3989 | "io_uring-sq"); |
| 3990 | } |
| 3991 | if (IS_ERR(ctx->sqo_thread)) { |
| 3992 | ret = PTR_ERR(ctx->sqo_thread); |
| 3993 | ctx->sqo_thread = NULL; |
| 3994 | goto err; |
| 3995 | } |
| 3996 | wake_up_process(ctx->sqo_thread); |
| 3997 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 3998 | /* Can't have SQ_AFF without SQPOLL */ |
| 3999 | ret = -EINVAL; |
| 4000 | goto err; |
| 4001 | } |
| 4002 | |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 4003 | data.mm = ctx->sqo_mm; |
| 4004 | data.user = ctx->user; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4005 | data.creds = ctx->creds; |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 4006 | data.get_work = io_get_work; |
| 4007 | data.put_work = io_put_work; |
| 4008 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4009 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 4010 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 4011 | ctx->io_wq = io_wq_create(concurrency, &data); |
Jens Axboe | 975c99a5 | 2019-10-30 08:42:56 -0600 | [diff] [blame] | 4012 | if (IS_ERR(ctx->io_wq)) { |
| 4013 | ret = PTR_ERR(ctx->io_wq); |
| 4014 | ctx->io_wq = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4015 | goto err; |
| 4016 | } |
| 4017 | |
| 4018 | return 0; |
| 4019 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 4020 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4021 | mmdrop(ctx->sqo_mm); |
| 4022 | ctx->sqo_mm = NULL; |
| 4023 | return ret; |
| 4024 | } |
| 4025 | |
| 4026 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 4027 | { |
| 4028 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 4029 | } |
| 4030 | |
| 4031 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 4032 | { |
| 4033 | unsigned long page_limit, cur_pages, new_pages; |
| 4034 | |
| 4035 | /* Don't allow more pages than we can safely lock */ |
| 4036 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 4037 | |
| 4038 | do { |
| 4039 | cur_pages = atomic_long_read(&user->locked_vm); |
| 4040 | new_pages = cur_pages + nr_pages; |
| 4041 | if (new_pages > page_limit) |
| 4042 | return -ENOMEM; |
| 4043 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 4044 | new_pages) != cur_pages); |
| 4045 | |
| 4046 | return 0; |
| 4047 | } |
| 4048 | |
| 4049 | static void io_mem_free(void *ptr) |
| 4050 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 4051 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4052 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 4053 | if (!ptr) |
| 4054 | return; |
| 4055 | |
| 4056 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4057 | if (put_page_testzero(page)) |
| 4058 | free_compound_page(page); |
| 4059 | } |
| 4060 | |
| 4061 | static void *io_mem_alloc(size_t size) |
| 4062 | { |
| 4063 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 4064 | __GFP_NORETRY; |
| 4065 | |
| 4066 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 4067 | } |
| 4068 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4069 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 4070 | size_t *sq_offset) |
| 4071 | { |
| 4072 | struct io_rings *rings; |
| 4073 | size_t off, sq_array_size; |
| 4074 | |
| 4075 | off = struct_size(rings, cqes, cq_entries); |
| 4076 | if (off == SIZE_MAX) |
| 4077 | return SIZE_MAX; |
| 4078 | |
| 4079 | #ifdef CONFIG_SMP |
| 4080 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 4081 | if (off == 0) |
| 4082 | return SIZE_MAX; |
| 4083 | #endif |
| 4084 | |
| 4085 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 4086 | if (sq_array_size == SIZE_MAX) |
| 4087 | return SIZE_MAX; |
| 4088 | |
| 4089 | if (check_add_overflow(off, sq_array_size, &off)) |
| 4090 | return SIZE_MAX; |
| 4091 | |
| 4092 | if (sq_offset) |
| 4093 | *sq_offset = off; |
| 4094 | |
| 4095 | return off; |
| 4096 | } |
| 4097 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4098 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 4099 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4100 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4101 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4102 | pages = (size_t)1 << get_order( |
| 4103 | rings_size(sq_entries, cq_entries, NULL)); |
| 4104 | pages += (size_t)1 << get_order( |
| 4105 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4106 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4107 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4108 | } |
| 4109 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4110 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 4111 | { |
| 4112 | int i, j; |
| 4113 | |
| 4114 | if (!ctx->user_bufs) |
| 4115 | return -ENXIO; |
| 4116 | |
| 4117 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 4118 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 4119 | |
| 4120 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 4121 | put_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4122 | |
| 4123 | if (ctx->account_mem) |
| 4124 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 4125 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4126 | imu->nr_bvecs = 0; |
| 4127 | } |
| 4128 | |
| 4129 | kfree(ctx->user_bufs); |
| 4130 | ctx->user_bufs = NULL; |
| 4131 | ctx->nr_user_bufs = 0; |
| 4132 | return 0; |
| 4133 | } |
| 4134 | |
| 4135 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 4136 | void __user *arg, unsigned index) |
| 4137 | { |
| 4138 | struct iovec __user *src; |
| 4139 | |
| 4140 | #ifdef CONFIG_COMPAT |
| 4141 | if (ctx->compat) { |
| 4142 | struct compat_iovec __user *ciovs; |
| 4143 | struct compat_iovec ciov; |
| 4144 | |
| 4145 | ciovs = (struct compat_iovec __user *) arg; |
| 4146 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 4147 | return -EFAULT; |
| 4148 | |
| 4149 | dst->iov_base = (void __user *) (unsigned long) ciov.iov_base; |
| 4150 | dst->iov_len = ciov.iov_len; |
| 4151 | return 0; |
| 4152 | } |
| 4153 | #endif |
| 4154 | src = (struct iovec __user *) arg; |
| 4155 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 4156 | return -EFAULT; |
| 4157 | return 0; |
| 4158 | } |
| 4159 | |
| 4160 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 4161 | unsigned nr_args) |
| 4162 | { |
| 4163 | struct vm_area_struct **vmas = NULL; |
| 4164 | struct page **pages = NULL; |
| 4165 | int i, j, got_pages = 0; |
| 4166 | int ret = -EINVAL; |
| 4167 | |
| 4168 | if (ctx->user_bufs) |
| 4169 | return -EBUSY; |
| 4170 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 4171 | return -EINVAL; |
| 4172 | |
| 4173 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 4174 | GFP_KERNEL); |
| 4175 | if (!ctx->user_bufs) |
| 4176 | return -ENOMEM; |
| 4177 | |
| 4178 | for (i = 0; i < nr_args; i++) { |
| 4179 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 4180 | unsigned long off, start, end, ubuf; |
| 4181 | int pret, nr_pages; |
| 4182 | struct iovec iov; |
| 4183 | size_t size; |
| 4184 | |
| 4185 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 4186 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 4187 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4188 | |
| 4189 | /* |
| 4190 | * Don't impose further limits on the size and buffer |
| 4191 | * constraints here, we'll -EINVAL later when IO is |
| 4192 | * submitted if they are wrong. |
| 4193 | */ |
| 4194 | ret = -EFAULT; |
| 4195 | if (!iov.iov_base || !iov.iov_len) |
| 4196 | goto err; |
| 4197 | |
| 4198 | /* arbitrary limit, but we need something */ |
| 4199 | if (iov.iov_len > SZ_1G) |
| 4200 | goto err; |
| 4201 | |
| 4202 | ubuf = (unsigned long) iov.iov_base; |
| 4203 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 4204 | start = ubuf >> PAGE_SHIFT; |
| 4205 | nr_pages = end - start; |
| 4206 | |
| 4207 | if (ctx->account_mem) { |
| 4208 | ret = io_account_mem(ctx->user, nr_pages); |
| 4209 | if (ret) |
| 4210 | goto err; |
| 4211 | } |
| 4212 | |
| 4213 | ret = 0; |
| 4214 | if (!pages || nr_pages > got_pages) { |
| 4215 | kfree(vmas); |
| 4216 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 4217 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4218 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 4219 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4220 | sizeof(struct vm_area_struct *), |
| 4221 | GFP_KERNEL); |
| 4222 | if (!pages || !vmas) { |
| 4223 | ret = -ENOMEM; |
| 4224 | if (ctx->account_mem) |
| 4225 | io_unaccount_mem(ctx->user, nr_pages); |
| 4226 | goto err; |
| 4227 | } |
| 4228 | got_pages = nr_pages; |
| 4229 | } |
| 4230 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 4231 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4232 | GFP_KERNEL); |
| 4233 | ret = -ENOMEM; |
| 4234 | if (!imu->bvec) { |
| 4235 | if (ctx->account_mem) |
| 4236 | io_unaccount_mem(ctx->user, nr_pages); |
| 4237 | goto err; |
| 4238 | } |
| 4239 | |
| 4240 | ret = 0; |
| 4241 | down_read(¤t->mm->mmap_sem); |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 4242 | pret = get_user_pages(ubuf, nr_pages, |
| 4243 | FOLL_WRITE | FOLL_LONGTERM, |
| 4244 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4245 | if (pret == nr_pages) { |
| 4246 | /* don't support file backed memory */ |
| 4247 | for (j = 0; j < nr_pages; j++) { |
| 4248 | struct vm_area_struct *vma = vmas[j]; |
| 4249 | |
| 4250 | if (vma->vm_file && |
| 4251 | !is_file_hugepages(vma->vm_file)) { |
| 4252 | ret = -EOPNOTSUPP; |
| 4253 | break; |
| 4254 | } |
| 4255 | } |
| 4256 | } else { |
| 4257 | ret = pret < 0 ? pret : -EFAULT; |
| 4258 | } |
| 4259 | up_read(¤t->mm->mmap_sem); |
| 4260 | if (ret) { |
| 4261 | /* |
| 4262 | * if we did partial map, or found file backed vmas, |
| 4263 | * release any pages we did get |
| 4264 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 4265 | if (pret > 0) |
| 4266 | put_user_pages(pages, pret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4267 | if (ctx->account_mem) |
| 4268 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 4269 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4270 | goto err; |
| 4271 | } |
| 4272 | |
| 4273 | off = ubuf & ~PAGE_MASK; |
| 4274 | size = iov.iov_len; |
| 4275 | for (j = 0; j < nr_pages; j++) { |
| 4276 | size_t vec_len; |
| 4277 | |
| 4278 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 4279 | imu->bvec[j].bv_page = pages[j]; |
| 4280 | imu->bvec[j].bv_len = vec_len; |
| 4281 | imu->bvec[j].bv_offset = off; |
| 4282 | off = 0; |
| 4283 | size -= vec_len; |
| 4284 | } |
| 4285 | /* store original address for later verification */ |
| 4286 | imu->ubuf = ubuf; |
| 4287 | imu->len = iov.iov_len; |
| 4288 | imu->nr_bvecs = nr_pages; |
| 4289 | |
| 4290 | ctx->nr_user_bufs++; |
| 4291 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 4292 | kvfree(pages); |
| 4293 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4294 | return 0; |
| 4295 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 4296 | kvfree(pages); |
| 4297 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4298 | io_sqe_buffer_unregister(ctx); |
| 4299 | return ret; |
| 4300 | } |
| 4301 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 4302 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 4303 | { |
| 4304 | __s32 __user *fds = arg; |
| 4305 | int fd; |
| 4306 | |
| 4307 | if (ctx->cq_ev_fd) |
| 4308 | return -EBUSY; |
| 4309 | |
| 4310 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 4311 | return -EFAULT; |
| 4312 | |
| 4313 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 4314 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 4315 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 4316 | ctx->cq_ev_fd = NULL; |
| 4317 | return ret; |
| 4318 | } |
| 4319 | |
| 4320 | return 0; |
| 4321 | } |
| 4322 | |
| 4323 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 4324 | { |
| 4325 | if (ctx->cq_ev_fd) { |
| 4326 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 4327 | ctx->cq_ev_fd = NULL; |
| 4328 | return 0; |
| 4329 | } |
| 4330 | |
| 4331 | return -ENXIO; |
| 4332 | } |
| 4333 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4334 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 4335 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4336 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4337 | if (ctx->sqo_mm) |
| 4338 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4339 | |
| 4340 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4341 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4342 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 4343 | io_eventfd_unregister(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4344 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4345 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 4346 | if (ctx->ring_sock) { |
| 4347 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4348 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 4349 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4350 | #endif |
| 4351 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4352 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4353 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4354 | |
| 4355 | percpu_ref_exit(&ctx->refs); |
| 4356 | if (ctx->account_mem) |
| 4357 | io_unaccount_mem(ctx->user, |
| 4358 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 4359 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4360 | put_cred(ctx->creds); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 4361 | kfree(ctx->completions); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 4362 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4363 | kfree(ctx); |
| 4364 | } |
| 4365 | |
| 4366 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 4367 | { |
| 4368 | struct io_ring_ctx *ctx = file->private_data; |
| 4369 | __poll_t mask = 0; |
| 4370 | |
| 4371 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 4372 | /* |
| 4373 | * synchronizes with barrier from wq_has_sleeper call in |
| 4374 | * io_commit_cqring |
| 4375 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4376 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4377 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 4378 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4379 | mask |= EPOLLOUT | EPOLLWRNORM; |
yangerkun | daa5de5 | 2019-09-24 20:53:34 +0800 | [diff] [blame] | 4380 | if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4381 | mask |= EPOLLIN | EPOLLRDNORM; |
| 4382 | |
| 4383 | return mask; |
| 4384 | } |
| 4385 | |
| 4386 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 4387 | { |
| 4388 | struct io_ring_ctx *ctx = file->private_data; |
| 4389 | |
| 4390 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 4391 | } |
| 4392 | |
| 4393 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 4394 | { |
| 4395 | mutex_lock(&ctx->uring_lock); |
| 4396 | percpu_ref_kill(&ctx->refs); |
| 4397 | mutex_unlock(&ctx->uring_lock); |
| 4398 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4399 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4400 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4401 | |
| 4402 | if (ctx->io_wq) |
| 4403 | io_wq_cancel_all(ctx->io_wq); |
| 4404 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4405 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 4406 | /* if we failed setting up the ctx, we might not have any rings */ |
| 4407 | if (ctx->rings) |
| 4408 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 4409 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4410 | io_ring_ctx_free(ctx); |
| 4411 | } |
| 4412 | |
| 4413 | static int io_uring_release(struct inode *inode, struct file *file) |
| 4414 | { |
| 4415 | struct io_ring_ctx *ctx = file->private_data; |
| 4416 | |
| 4417 | file->private_data = NULL; |
| 4418 | io_ring_ctx_wait_and_kill(ctx); |
| 4419 | return 0; |
| 4420 | } |
| 4421 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4422 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 4423 | struct files_struct *files) |
| 4424 | { |
| 4425 | struct io_kiocb *req; |
| 4426 | DEFINE_WAIT(wait); |
| 4427 | |
| 4428 | while (!list_empty_careful(&ctx->inflight_list)) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 4429 | struct io_kiocb *cancel_req = NULL; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4430 | |
| 4431 | spin_lock_irq(&ctx->inflight_lock); |
| 4432 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 4433 | if (req->work.files != files) |
| 4434 | continue; |
| 4435 | /* req is being completed, ignore */ |
| 4436 | if (!refcount_inc_not_zero(&req->refs)) |
| 4437 | continue; |
| 4438 | cancel_req = req; |
| 4439 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4440 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 4441 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4442 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 4443 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4444 | spin_unlock_irq(&ctx->inflight_lock); |
| 4445 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 4446 | /* We need to keep going until we don't find a matching req */ |
| 4447 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4448 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 4449 | |
| 4450 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 4451 | io_put_req(cancel_req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4452 | schedule(); |
| 4453 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 4454 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4455 | } |
| 4456 | |
| 4457 | static int io_uring_flush(struct file *file, void *data) |
| 4458 | { |
| 4459 | struct io_ring_ctx *ctx = file->private_data; |
| 4460 | |
| 4461 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4462 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
| 4463 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4464 | io_wq_cancel_all(ctx->io_wq); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4465 | } |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4466 | return 0; |
| 4467 | } |
| 4468 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4469 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 4470 | { |
| 4471 | loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT; |
| 4472 | unsigned long sz = vma->vm_end - vma->vm_start; |
| 4473 | struct io_ring_ctx *ctx = file->private_data; |
| 4474 | unsigned long pfn; |
| 4475 | struct page *page; |
| 4476 | void *ptr; |
| 4477 | |
| 4478 | switch (offset) { |
| 4479 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4480 | case IORING_OFF_CQ_RING: |
| 4481 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4482 | break; |
| 4483 | case IORING_OFF_SQES: |
| 4484 | ptr = ctx->sq_sqes; |
| 4485 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4486 | default: |
| 4487 | return -EINVAL; |
| 4488 | } |
| 4489 | |
| 4490 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 4491 | if (sz > page_size(page)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4492 | return -EINVAL; |
| 4493 | |
| 4494 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 4495 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 4496 | } |
| 4497 | |
| 4498 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 4499 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 4500 | size_t, sigsz) |
| 4501 | { |
| 4502 | struct io_ring_ctx *ctx; |
| 4503 | long ret = -EBADF; |
| 4504 | int submitted = 0; |
| 4505 | struct fd f; |
| 4506 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4507 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4508 | return -EINVAL; |
| 4509 | |
| 4510 | f = fdget(fd); |
| 4511 | if (!f.file) |
| 4512 | return -EBADF; |
| 4513 | |
| 4514 | ret = -EOPNOTSUPP; |
| 4515 | if (f.file->f_op != &io_uring_fops) |
| 4516 | goto out_fput; |
| 4517 | |
| 4518 | ret = -ENXIO; |
| 4519 | ctx = f.file->private_data; |
| 4520 | if (!percpu_ref_tryget(&ctx->refs)) |
| 4521 | goto out_fput; |
| 4522 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4523 | /* |
| 4524 | * For SQ polling, the thread will do all submissions and completions. |
| 4525 | * Just return the requested submit count, and wake the thread if |
| 4526 | * we were asked to. |
| 4527 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 4528 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4529 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4530 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 4531 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4532 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 4533 | wake_up(&ctx->sqo_wait); |
| 4534 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 4535 | } else if (to_submit) { |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 4536 | struct mm_struct *cur_mm; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4537 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 4538 | to_submit = min(to_submit, ctx->sq_entries); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4539 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 4540 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
| 4541 | cur_mm = ctx->sqo_mm; |
| 4542 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, |
| 4543 | &cur_mm, false); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4544 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4545 | } |
| 4546 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4547 | unsigned nr_events = 0; |
| 4548 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4549 | min_complete = min(min_complete, ctx->cq_entries); |
| 4550 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4551 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4552 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4553 | } else { |
| 4554 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 4555 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4556 | } |
| 4557 | |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 4558 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4559 | out_fput: |
| 4560 | fdput(f); |
| 4561 | return submitted ? submitted : ret; |
| 4562 | } |
| 4563 | |
| 4564 | static const struct file_operations io_uring_fops = { |
| 4565 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4566 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4567 | .mmap = io_uring_mmap, |
| 4568 | .poll = io_uring_poll, |
| 4569 | .fasync = io_uring_fasync, |
| 4570 | }; |
| 4571 | |
| 4572 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 4573 | struct io_uring_params *p) |
| 4574 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4575 | struct io_rings *rings; |
| 4576 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4577 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4578 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 4579 | if (size == SIZE_MAX) |
| 4580 | return -EOVERFLOW; |
| 4581 | |
| 4582 | rings = io_mem_alloc(size); |
| 4583 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4584 | return -ENOMEM; |
| 4585 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4586 | ctx->rings = rings; |
| 4587 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 4588 | rings->sq_ring_mask = p->sq_entries - 1; |
| 4589 | rings->cq_ring_mask = p->cq_entries - 1; |
| 4590 | rings->sq_ring_entries = p->sq_entries; |
| 4591 | rings->cq_ring_entries = p->cq_entries; |
| 4592 | ctx->sq_mask = rings->sq_ring_mask; |
| 4593 | ctx->cq_mask = rings->cq_ring_mask; |
| 4594 | ctx->sq_entries = rings->sq_ring_entries; |
| 4595 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4596 | |
| 4597 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 4598 | if (size == SIZE_MAX) { |
| 4599 | io_mem_free(ctx->rings); |
| 4600 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4601 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 4602 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4603 | |
| 4604 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 4605 | if (!ctx->sq_sqes) { |
| 4606 | io_mem_free(ctx->rings); |
| 4607 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4608 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 4609 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4610 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4611 | return 0; |
| 4612 | } |
| 4613 | |
| 4614 | /* |
| 4615 | * Allocate an anonymous fd, this is what constitutes the application |
| 4616 | * visible backing of an io_uring instance. The application mmaps this |
| 4617 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 4618 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 4619 | */ |
| 4620 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 4621 | { |
| 4622 | struct file *file; |
| 4623 | int ret; |
| 4624 | |
| 4625 | #if defined(CONFIG_UNIX) |
| 4626 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 4627 | &ctx->ring_sock); |
| 4628 | if (ret) |
| 4629 | return ret; |
| 4630 | #endif |
| 4631 | |
| 4632 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 4633 | if (ret < 0) |
| 4634 | goto err; |
| 4635 | |
| 4636 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 4637 | O_RDWR | O_CLOEXEC); |
| 4638 | if (IS_ERR(file)) { |
| 4639 | put_unused_fd(ret); |
| 4640 | ret = PTR_ERR(file); |
| 4641 | goto err; |
| 4642 | } |
| 4643 | |
| 4644 | #if defined(CONFIG_UNIX) |
| 4645 | ctx->ring_sock->file = file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4646 | ctx->ring_sock->sk->sk_user_data = ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4647 | #endif |
| 4648 | fd_install(ret, file); |
| 4649 | return ret; |
| 4650 | err: |
| 4651 | #if defined(CONFIG_UNIX) |
| 4652 | sock_release(ctx->ring_sock); |
| 4653 | ctx->ring_sock = NULL; |
| 4654 | #endif |
| 4655 | return ret; |
| 4656 | } |
| 4657 | |
| 4658 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 4659 | { |
| 4660 | struct user_struct *user = NULL; |
| 4661 | struct io_ring_ctx *ctx; |
| 4662 | bool account_mem; |
| 4663 | int ret; |
| 4664 | |
| 4665 | if (!entries || entries > IORING_MAX_ENTRIES) |
| 4666 | return -EINVAL; |
| 4667 | |
| 4668 | /* |
| 4669 | * Use twice as many entries for the CQ ring. It's possible for the |
| 4670 | * application to drive a higher depth than the size of the SQ ring, |
| 4671 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 4672 | * some flexibility in overcommitting a bit. If the application has |
| 4673 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 4674 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4675 | */ |
| 4676 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 4677 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 4678 | /* |
| 4679 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 4680 | * to a power-of-two, if it isn't already. We do NOT impose |
| 4681 | * any cq vs sq ring sizing. |
| 4682 | */ |
| 4683 | if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES) |
| 4684 | return -EINVAL; |
| 4685 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 4686 | } else { |
| 4687 | p->cq_entries = 2 * p->sq_entries; |
| 4688 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4689 | |
| 4690 | user = get_uid(current_user()); |
| 4691 | account_mem = !capable(CAP_IPC_LOCK); |
| 4692 | |
| 4693 | if (account_mem) { |
| 4694 | ret = io_account_mem(user, |
| 4695 | ring_pages(p->sq_entries, p->cq_entries)); |
| 4696 | if (ret) { |
| 4697 | free_uid(user); |
| 4698 | return ret; |
| 4699 | } |
| 4700 | } |
| 4701 | |
| 4702 | ctx = io_ring_ctx_alloc(p); |
| 4703 | if (!ctx) { |
| 4704 | if (account_mem) |
| 4705 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 4706 | p->cq_entries)); |
| 4707 | free_uid(user); |
| 4708 | return -ENOMEM; |
| 4709 | } |
| 4710 | ctx->compat = in_compat_syscall(); |
| 4711 | ctx->account_mem = account_mem; |
| 4712 | ctx->user = user; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4713 | ctx->creds = prepare_creds(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4714 | |
| 4715 | ret = io_allocate_scq_urings(ctx, p); |
| 4716 | if (ret) |
| 4717 | goto err; |
| 4718 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4719 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4720 | if (ret) |
| 4721 | goto err; |
| 4722 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4723 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4724 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 4725 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 4726 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 4727 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 4728 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 4729 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 4730 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4731 | |
| 4732 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4733 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 4734 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 4735 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 4736 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 4737 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 4738 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 4739 | |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 4740 | /* |
| 4741 | * Install ring fd as the very last thing, so we don't risk someone |
| 4742 | * having closed it before we finish setup |
| 4743 | */ |
| 4744 | ret = io_uring_get_fd(ctx); |
| 4745 | if (ret < 0) |
| 4746 | goto err; |
| 4747 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4748 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 4749 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4750 | return ret; |
| 4751 | err: |
| 4752 | io_ring_ctx_wait_and_kill(ctx); |
| 4753 | return ret; |
| 4754 | } |
| 4755 | |
| 4756 | /* |
| 4757 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 4758 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 4759 | * params structure passed in. |
| 4760 | */ |
| 4761 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 4762 | { |
| 4763 | struct io_uring_params p; |
| 4764 | long ret; |
| 4765 | int i; |
| 4766 | |
| 4767 | if (copy_from_user(&p, params, sizeof(p))) |
| 4768 | return -EFAULT; |
| 4769 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 4770 | if (p.resv[i]) |
| 4771 | return -EINVAL; |
| 4772 | } |
| 4773 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4774 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 4775 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4776 | return -EINVAL; |
| 4777 | |
| 4778 | ret = io_uring_create(entries, &p); |
| 4779 | if (ret < 0) |
| 4780 | return ret; |
| 4781 | |
| 4782 | if (copy_to_user(params, &p, sizeof(p))) |
| 4783 | return -EFAULT; |
| 4784 | |
| 4785 | return ret; |
| 4786 | } |
| 4787 | |
| 4788 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 4789 | struct io_uring_params __user *, params) |
| 4790 | { |
| 4791 | return io_uring_setup(entries, params); |
| 4792 | } |
| 4793 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4794 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 4795 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 4796 | __releases(ctx->uring_lock) |
| 4797 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4798 | { |
| 4799 | int ret; |
| 4800 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 4801 | /* |
| 4802 | * We're inside the ring mutex, if the ref is already dying, then |
| 4803 | * someone else killed the ctx or is already going through |
| 4804 | * io_uring_register(). |
| 4805 | */ |
| 4806 | if (percpu_ref_is_dying(&ctx->refs)) |
| 4807 | return -ENXIO; |
| 4808 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4809 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 4810 | |
| 4811 | /* |
| 4812 | * Drop uring mutex before waiting for references to exit. If another |
| 4813 | * thread is currently inside io_uring_enter() it might need to grab |
| 4814 | * the uring_lock to make progress. If we hold it here across the drain |
| 4815 | * wait, then we can deadlock. It's safe to drop the mutex here, since |
| 4816 | * no new references will come in after we've killed the percpu ref. |
| 4817 | */ |
| 4818 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 4819 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 4820 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4821 | |
| 4822 | switch (opcode) { |
| 4823 | case IORING_REGISTER_BUFFERS: |
| 4824 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 4825 | break; |
| 4826 | case IORING_UNREGISTER_BUFFERS: |
| 4827 | ret = -EINVAL; |
| 4828 | if (arg || nr_args) |
| 4829 | break; |
| 4830 | ret = io_sqe_buffer_unregister(ctx); |
| 4831 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4832 | case IORING_REGISTER_FILES: |
| 4833 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 4834 | break; |
| 4835 | case IORING_UNREGISTER_FILES: |
| 4836 | ret = -EINVAL; |
| 4837 | if (arg || nr_args) |
| 4838 | break; |
| 4839 | ret = io_sqe_files_unregister(ctx); |
| 4840 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 4841 | case IORING_REGISTER_FILES_UPDATE: |
| 4842 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 4843 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 4844 | case IORING_REGISTER_EVENTFD: |
| 4845 | ret = -EINVAL; |
| 4846 | if (nr_args != 1) |
| 4847 | break; |
| 4848 | ret = io_eventfd_register(ctx, arg); |
| 4849 | break; |
| 4850 | case IORING_UNREGISTER_EVENTFD: |
| 4851 | ret = -EINVAL; |
| 4852 | if (arg || nr_args) |
| 4853 | break; |
| 4854 | ret = io_eventfd_unregister(ctx); |
| 4855 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4856 | default: |
| 4857 | ret = -EINVAL; |
| 4858 | break; |
| 4859 | } |
| 4860 | |
| 4861 | /* bring the ctx back to life */ |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 4862 | reinit_completion(&ctx->completions[0]); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4863 | percpu_ref_reinit(&ctx->refs); |
| 4864 | return ret; |
| 4865 | } |
| 4866 | |
| 4867 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 4868 | void __user *, arg, unsigned int, nr_args) |
| 4869 | { |
| 4870 | struct io_ring_ctx *ctx; |
| 4871 | long ret = -EBADF; |
| 4872 | struct fd f; |
| 4873 | |
| 4874 | f = fdget(fd); |
| 4875 | if (!f.file) |
| 4876 | return -EBADF; |
| 4877 | |
| 4878 | ret = -EOPNOTSUPP; |
| 4879 | if (f.file->f_op != &io_uring_fops) |
| 4880 | goto out_fput; |
| 4881 | |
| 4882 | ctx = f.file->private_data; |
| 4883 | |
| 4884 | mutex_lock(&ctx->uring_lock); |
| 4885 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 4886 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 4887 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 4888 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4889 | out_fput: |
| 4890 | fdput(f); |
| 4891 | return ret; |
| 4892 | } |
| 4893 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4894 | static int __init io_uring_init(void) |
| 4895 | { |
| 4896 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 4897 | return 0; |
| 4898 | }; |
| 4899 | __initcall(io_uring_init); |