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