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> |
| 59 | #include <linux/workqueue.h> |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 60 | #include <linux/kthread.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 61 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 62 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 63 | #include <linux/net.h> |
| 64 | #include <net/sock.h> |
| 65 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 66 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 67 | #include <linux/anon_inodes.h> |
| 68 | #include <linux/sched/mm.h> |
| 69 | #include <linux/uaccess.h> |
| 70 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 71 | #include <linux/sizes.h> |
| 72 | #include <linux/hugetlb.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 73 | |
| 74 | #include <uapi/linux/io_uring.h> |
| 75 | |
| 76 | #include "internal.h" |
| 77 | |
| 78 | #define IORING_MAX_ENTRIES 4096 |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 79 | #define IORING_MAX_FIXED_FILES 1024 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 80 | |
| 81 | struct io_uring { |
| 82 | u32 head ____cacheline_aligned_in_smp; |
| 83 | u32 tail ____cacheline_aligned_in_smp; |
| 84 | }; |
| 85 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 86 | /* |
| 87 | * This data is shared with the application through the mmap at offset |
| 88 | * IORING_OFF_SQ_RING. |
| 89 | * |
| 90 | * The offsets to the member fields are published through struct |
| 91 | * io_sqring_offsets when calling io_uring_setup. |
| 92 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 93 | struct io_sq_ring { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 94 | /* |
| 95 | * Head and tail offsets into the ring; the offsets need to be |
| 96 | * masked to get valid indices. |
| 97 | * |
| 98 | * The kernel controls head and the application controls tail. |
| 99 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 100 | struct io_uring r; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 101 | /* |
| 102 | * Bitmask to apply to head and tail offsets (constant, equals |
| 103 | * ring_entries - 1) |
| 104 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 105 | u32 ring_mask; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 106 | /* Ring size (constant, power of 2) */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 107 | u32 ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 108 | /* |
| 109 | * Number of invalid entries dropped by the kernel due to |
| 110 | * invalid index stored in array |
| 111 | * |
| 112 | * Written by the kernel, shouldn't be modified by the |
| 113 | * application (i.e. get number of "new events" by comparing to |
| 114 | * cached value). |
| 115 | * |
| 116 | * After a new SQ head value was read by the application this |
| 117 | * counter includes all submissions that were dropped reaching |
| 118 | * the new SQ head (and possibly more). |
| 119 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 120 | u32 dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 121 | /* |
| 122 | * Runtime flags |
| 123 | * |
| 124 | * Written by the kernel, shouldn't be modified by the |
| 125 | * application. |
| 126 | * |
| 127 | * The application needs a full memory barrier before checking |
| 128 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 129 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 130 | u32 flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 131 | /* |
| 132 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 133 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 134 | * |
| 135 | * This indirection could e.g. be used to assign fixed |
| 136 | * io_uring_sqe entries to operations and only submit them to |
| 137 | * the queue when needed. |
| 138 | * |
| 139 | * The kernel modifies neither the indices array nor the entries |
| 140 | * array. |
| 141 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 142 | u32 array[]; |
| 143 | }; |
| 144 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 145 | /* |
| 146 | * This data is shared with the application through the mmap at offset |
| 147 | * IORING_OFF_CQ_RING. |
| 148 | * |
| 149 | * The offsets to the member fields are published through struct |
| 150 | * io_cqring_offsets when calling io_uring_setup. |
| 151 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 152 | struct io_cq_ring { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 153 | /* |
| 154 | * Head and tail offsets into the ring; the offsets need to be |
| 155 | * masked to get valid indices. |
| 156 | * |
| 157 | * The application controls head and the kernel tail. |
| 158 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 159 | struct io_uring r; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 160 | /* |
| 161 | * Bitmask to apply to head and tail offsets (constant, equals |
| 162 | * ring_entries - 1) |
| 163 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 164 | u32 ring_mask; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 165 | /* Ring size (constant, power of 2) */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 166 | u32 ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 167 | /* |
| 168 | * Number of completion events lost because the queue was full; |
| 169 | * this should be avoided by the application by making sure |
| 170 | * there are not more requests pending thatn there is space in |
| 171 | * the completion queue. |
| 172 | * |
| 173 | * Written by the kernel, shouldn't be modified by the |
| 174 | * application (i.e. get number of "new events" by comparing to |
| 175 | * cached value). |
| 176 | * |
| 177 | * As completion events come in out of order this counter is not |
| 178 | * ordered with any other data. |
| 179 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 180 | u32 overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 181 | /* |
| 182 | * Ring buffer of completion events. |
| 183 | * |
| 184 | * The kernel writes completion events fresh every time they are |
| 185 | * produced, so the application is allowed to modify pending |
| 186 | * entries. |
| 187 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 188 | struct io_uring_cqe cqes[]; |
| 189 | }; |
| 190 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 191 | struct io_mapped_ubuf { |
| 192 | u64 ubuf; |
| 193 | size_t len; |
| 194 | struct bio_vec *bvec; |
| 195 | unsigned int nr_bvecs; |
| 196 | }; |
| 197 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 198 | struct async_list { |
| 199 | spinlock_t lock; |
| 200 | atomic_t cnt; |
| 201 | struct list_head list; |
| 202 | |
| 203 | struct file *file; |
| 204 | off_t io_end; |
| 205 | size_t io_pages; |
| 206 | }; |
| 207 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 208 | struct io_ring_ctx { |
| 209 | struct { |
| 210 | struct percpu_ref refs; |
| 211 | } ____cacheline_aligned_in_smp; |
| 212 | |
| 213 | struct { |
| 214 | unsigned int flags; |
| 215 | bool compat; |
| 216 | bool account_mem; |
| 217 | |
| 218 | /* SQ ring */ |
| 219 | struct io_sq_ring *sq_ring; |
| 220 | unsigned cached_sq_head; |
| 221 | unsigned sq_entries; |
| 222 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 223 | unsigned sq_thread_idle; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 224 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 225 | |
| 226 | struct list_head defer_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 227 | } ____cacheline_aligned_in_smp; |
| 228 | |
| 229 | /* IO offload */ |
| 230 | struct workqueue_struct *sqo_wq; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 231 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 232 | struct mm_struct *sqo_mm; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 233 | wait_queue_head_t sqo_wait; |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 234 | struct completion sqo_thread_started; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 235 | |
| 236 | struct { |
| 237 | /* CQ ring */ |
| 238 | struct io_cq_ring *cq_ring; |
| 239 | unsigned cached_cq_tail; |
| 240 | unsigned cq_entries; |
| 241 | unsigned cq_mask; |
| 242 | struct wait_queue_head cq_wait; |
| 243 | struct fasync_struct *cq_fasync; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 244 | struct eventfd_ctx *cq_ev_fd; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 245 | } ____cacheline_aligned_in_smp; |
| 246 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 247 | /* |
| 248 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 249 | * readers must ensure that ->refs is alive as long as the file* is |
| 250 | * used. Only updated through io_uring_register(2). |
| 251 | */ |
| 252 | struct file **user_files; |
| 253 | unsigned nr_user_files; |
| 254 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 255 | /* if used, fixed mapped user buffers */ |
| 256 | unsigned nr_user_bufs; |
| 257 | struct io_mapped_ubuf *user_bufs; |
| 258 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 259 | struct user_struct *user; |
| 260 | |
| 261 | struct completion ctx_done; |
| 262 | |
| 263 | struct { |
| 264 | struct mutex uring_lock; |
| 265 | wait_queue_head_t wait; |
| 266 | } ____cacheline_aligned_in_smp; |
| 267 | |
| 268 | struct { |
| 269 | spinlock_t completion_lock; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 270 | bool poll_multi_file; |
| 271 | /* |
| 272 | * ->poll_list is protected by the ctx->uring_lock for |
| 273 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 274 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 275 | * manipulate the list, hence no extra locking is needed there. |
| 276 | */ |
| 277 | struct list_head poll_list; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 278 | struct list_head cancel_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 279 | } ____cacheline_aligned_in_smp; |
| 280 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 281 | struct async_list pending_async[2]; |
| 282 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 283 | #if defined(CONFIG_UNIX) |
| 284 | struct socket *ring_sock; |
| 285 | #endif |
| 286 | }; |
| 287 | |
| 288 | struct sqe_submit { |
| 289 | const struct io_uring_sqe *sqe; |
| 290 | unsigned short index; |
| 291 | bool has_user; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 292 | bool needs_lock; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 293 | bool needs_fixed_file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 294 | }; |
| 295 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 296 | /* |
| 297 | * First field must be the file pointer in all the |
| 298 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 299 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 300 | struct io_poll_iocb { |
| 301 | struct file *file; |
| 302 | struct wait_queue_head *head; |
| 303 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 304 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 305 | bool canceled; |
| 306 | struct wait_queue_entry wait; |
| 307 | }; |
| 308 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 309 | /* |
| 310 | * NOTE! Each of the iocb union members has the file pointer |
| 311 | * as the first entry in their struct definition. So you can |
| 312 | * access the file pointer through any of the sub-structs, |
| 313 | * or directly as just 'ki_filp' in this struct. |
| 314 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 315 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 316 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 317 | struct file *file; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 318 | struct kiocb rw; |
| 319 | struct io_poll_iocb poll; |
| 320 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 321 | |
| 322 | struct sqe_submit submit; |
| 323 | |
| 324 | struct io_ring_ctx *ctx; |
| 325 | struct list_head list; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 326 | struct list_head link_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 327 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 328 | refcount_t refs; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 329 | #define REQ_F_NOWAIT 1 /* must not punt to workers */ |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 330 | #define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */ |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 331 | #define REQ_F_FIXED_FILE 4 /* ctx owns file */ |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 332 | #define REQ_F_SEQ_PREV 8 /* sequential with previous */ |
Stefan Bühler | e2033e3 | 2019-05-11 19:08:01 +0200 | [diff] [blame] | 333 | #define REQ_F_IO_DRAIN 16 /* drain existing IO first */ |
| 334 | #define REQ_F_IO_DRAINED 32 /* drain done */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 335 | #define REQ_F_LINK 64 /* linked sqes */ |
Zhengyuan Liu | f7b76ac | 2019-07-16 23:26:14 +0800 | [diff] [blame] | 336 | #define REQ_F_LINK_DONE 128 /* linked sqes done */ |
| 337 | #define REQ_F_FAIL_LINK 256 /* fail rest of links */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 338 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 339 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 340 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 341 | |
| 342 | struct work_struct work; |
| 343 | }; |
| 344 | |
| 345 | #define IO_PLUG_THRESHOLD 2 |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 346 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 347 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 348 | struct io_submit_state { |
| 349 | struct blk_plug plug; |
| 350 | |
| 351 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 352 | * io_kiocb alloc cache |
| 353 | */ |
| 354 | void *reqs[IO_IOPOLL_BATCH]; |
| 355 | unsigned int free_reqs; |
| 356 | unsigned int cur_req; |
| 357 | |
| 358 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 359 | * File reference cache |
| 360 | */ |
| 361 | struct file *file; |
| 362 | unsigned int fd; |
| 363 | unsigned int has_refs; |
| 364 | unsigned int used_refs; |
| 365 | unsigned int ios_left; |
| 366 | }; |
| 367 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 368 | static void io_sq_wq_submit_work(struct work_struct *work); |
| 369 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 370 | static struct kmem_cache *req_cachep; |
| 371 | |
| 372 | static const struct file_operations io_uring_fops; |
| 373 | |
| 374 | struct sock *io_uring_get_socket(struct file *file) |
| 375 | { |
| 376 | #if defined(CONFIG_UNIX) |
| 377 | if (file->f_op == &io_uring_fops) { |
| 378 | struct io_ring_ctx *ctx = file->private_data; |
| 379 | |
| 380 | return ctx->ring_sock->sk; |
| 381 | } |
| 382 | #endif |
| 383 | return NULL; |
| 384 | } |
| 385 | EXPORT_SYMBOL(io_uring_get_socket); |
| 386 | |
| 387 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 388 | { |
| 389 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 390 | |
| 391 | complete(&ctx->ctx_done); |
| 392 | } |
| 393 | |
| 394 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 395 | { |
| 396 | struct io_ring_ctx *ctx; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 397 | int i; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 398 | |
| 399 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 400 | if (!ctx) |
| 401 | return NULL; |
| 402 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 403 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
| 404 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 405 | kfree(ctx); |
| 406 | return NULL; |
| 407 | } |
| 408 | |
| 409 | ctx->flags = p->flags; |
| 410 | init_waitqueue_head(&ctx->cq_wait); |
| 411 | init_completion(&ctx->ctx_done); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 412 | init_completion(&ctx->sqo_thread_started); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 413 | mutex_init(&ctx->uring_lock); |
| 414 | init_waitqueue_head(&ctx->wait); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 415 | for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) { |
| 416 | spin_lock_init(&ctx->pending_async[i].lock); |
| 417 | INIT_LIST_HEAD(&ctx->pending_async[i].list); |
| 418 | atomic_set(&ctx->pending_async[i].cnt, 0); |
| 419 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 420 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 421 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 422 | INIT_LIST_HEAD(&ctx->cancel_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 423 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 424 | return ctx; |
| 425 | } |
| 426 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 427 | static inline bool io_sequence_defer(struct io_ring_ctx *ctx, |
| 428 | struct io_kiocb *req) |
| 429 | { |
| 430 | if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN) |
| 431 | return false; |
| 432 | |
Zhengyuan Liu | dbd0f6d | 2019-07-13 11:58:26 +0800 | [diff] [blame] | 433 | return req->sequence != ctx->cached_cq_tail + ctx->sq_ring->dropped; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx) |
| 437 | { |
| 438 | struct io_kiocb *req; |
| 439 | |
| 440 | if (list_empty(&ctx->defer_list)) |
| 441 | return NULL; |
| 442 | |
| 443 | req = list_first_entry(&ctx->defer_list, struct io_kiocb, list); |
| 444 | if (!io_sequence_defer(ctx, req)) { |
| 445 | list_del_init(&req->list); |
| 446 | return req; |
| 447 | } |
| 448 | |
| 449 | return NULL; |
| 450 | } |
| 451 | |
| 452 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 453 | { |
| 454 | struct io_cq_ring *ring = ctx->cq_ring; |
| 455 | |
| 456 | if (ctx->cached_cq_tail != READ_ONCE(ring->r.tail)) { |
| 457 | /* order cqe stores with ring update */ |
| 458 | smp_store_release(&ring->r.tail, ctx->cached_cq_tail); |
| 459 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 460 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 461 | wake_up_interruptible(&ctx->cq_wait); |
| 462 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 467 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 468 | { |
| 469 | struct io_kiocb *req; |
| 470 | |
| 471 | __io_commit_cqring(ctx); |
| 472 | |
| 473 | while ((req = io_get_deferred_req(ctx)) != NULL) { |
| 474 | req->flags |= REQ_F_IO_DRAINED; |
| 475 | queue_work(ctx->sqo_wq, &req->work); |
| 476 | } |
| 477 | } |
| 478 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 479 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 480 | { |
| 481 | struct io_cq_ring *ring = ctx->cq_ring; |
| 482 | unsigned tail; |
| 483 | |
| 484 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 485 | /* |
| 486 | * writes to the cq entry need to come after reading head; the |
| 487 | * control dependency is enough as we're using WRITE_ONCE to |
| 488 | * fill the cq entry |
| 489 | */ |
Jens Axboe | 74f464e | 2019-04-17 08:57:48 -0600 | [diff] [blame] | 490 | if (tail - READ_ONCE(ring->r.head) == ring->ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 491 | return NULL; |
| 492 | |
| 493 | ctx->cached_cq_tail++; |
| 494 | return &ring->cqes[tail & ctx->cq_mask]; |
| 495 | } |
| 496 | |
| 497 | static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data, |
Jens Axboe | c71ffb6 | 2019-05-13 20:58:29 -0600 | [diff] [blame] | 498 | long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 499 | { |
| 500 | struct io_uring_cqe *cqe; |
| 501 | |
| 502 | /* |
| 503 | * If we can't get a cq entry, userspace overflowed the |
| 504 | * submission (by quite a lot). Increment the overflow count in |
| 505 | * the ring. |
| 506 | */ |
| 507 | cqe = io_get_cqring(ctx); |
| 508 | if (cqe) { |
| 509 | WRITE_ONCE(cqe->user_data, ki_user_data); |
| 510 | WRITE_ONCE(cqe->res, res); |
Jens Axboe | c71ffb6 | 2019-05-13 20:58:29 -0600 | [diff] [blame] | 511 | WRITE_ONCE(cqe->flags, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 512 | } else { |
| 513 | unsigned overflow = READ_ONCE(ctx->cq_ring->overflow); |
| 514 | |
| 515 | WRITE_ONCE(ctx->cq_ring->overflow, overflow + 1); |
| 516 | } |
| 517 | } |
| 518 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 519 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| 520 | { |
| 521 | if (waitqueue_active(&ctx->wait)) |
| 522 | wake_up(&ctx->wait); |
| 523 | if (waitqueue_active(&ctx->sqo_wait)) |
| 524 | wake_up(&ctx->sqo_wait); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 525 | if (ctx->cq_ev_fd) |
| 526 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data, |
Jens Axboe | c71ffb6 | 2019-05-13 20:58:29 -0600 | [diff] [blame] | 530 | long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 531 | { |
| 532 | unsigned long flags; |
| 533 | |
| 534 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | c71ffb6 | 2019-05-13 20:58:29 -0600 | [diff] [blame] | 535 | io_cqring_fill_event(ctx, user_data, res); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 536 | io_commit_cqring(ctx); |
| 537 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 538 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 539 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs) |
| 543 | { |
| 544 | percpu_ref_put_many(&ctx->refs, refs); |
| 545 | |
| 546 | if (waitqueue_active(&ctx->wait)) |
| 547 | wake_up(&ctx->wait); |
| 548 | } |
| 549 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 550 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 551 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 552 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 553 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 554 | struct io_kiocb *req; |
| 555 | |
| 556 | if (!percpu_ref_tryget(&ctx->refs)) |
| 557 | return NULL; |
| 558 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 559 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 560 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 561 | if (unlikely(!req)) |
| 562 | goto out; |
| 563 | } else if (!state->free_reqs) { |
| 564 | size_t sz; |
| 565 | int ret; |
| 566 | |
| 567 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 568 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 569 | |
| 570 | /* |
| 571 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 572 | * retry single alloc to be on the safe side. |
| 573 | */ |
| 574 | if (unlikely(ret <= 0)) { |
| 575 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 576 | if (!state->reqs[0]) |
| 577 | goto out; |
| 578 | ret = 1; |
| 579 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 580 | state->free_reqs = ret - 1; |
| 581 | state->cur_req = 1; |
| 582 | req = state->reqs[0]; |
| 583 | } else { |
| 584 | req = state->reqs[state->cur_req]; |
| 585 | state->free_reqs--; |
| 586 | state->cur_req++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 589 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 590 | req->ctx = ctx; |
| 591 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 592 | /* one is dropped after submission, the other at completion */ |
| 593 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 594 | req->result = 0; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 595 | return req; |
| 596 | out: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 597 | io_ring_drop_ctx_refs(ctx, 1); |
| 598 | return NULL; |
| 599 | } |
| 600 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 601 | static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr) |
| 602 | { |
| 603 | if (*nr) { |
| 604 | kmem_cache_free_bulk(req_cachep, *nr, reqs); |
| 605 | io_ring_drop_ctx_refs(ctx, *nr); |
| 606 | *nr = 0; |
| 607 | } |
| 608 | } |
| 609 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 610 | static void __io_free_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 611 | { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 612 | if (req->file && !(req->flags & REQ_F_FIXED_FILE)) |
| 613 | fput(req->file); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 614 | io_ring_drop_ctx_refs(req->ctx, 1); |
| 615 | kmem_cache_free(req_cachep, req); |
| 616 | } |
| 617 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 618 | static void io_req_link_next(struct io_kiocb *req) |
| 619 | { |
| 620 | struct io_kiocb *nxt; |
| 621 | |
| 622 | /* |
| 623 | * The list should never be empty when we are called here. But could |
| 624 | * potentially happen if the chain is messed up, check to be on the |
| 625 | * safe side. |
| 626 | */ |
| 627 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list); |
| 628 | if (nxt) { |
| 629 | list_del(&nxt->list); |
| 630 | if (!list_empty(&req->link_list)) { |
| 631 | INIT_LIST_HEAD(&nxt->link_list); |
| 632 | list_splice(&req->link_list, &nxt->link_list); |
| 633 | nxt->flags |= REQ_F_LINK; |
| 634 | } |
| 635 | |
Zhengyuan Liu | f7b76ac | 2019-07-16 23:26:14 +0800 | [diff] [blame] | 636 | nxt->flags |= REQ_F_LINK_DONE; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 637 | INIT_WORK(&nxt->work, io_sq_wq_submit_work); |
| 638 | queue_work(req->ctx->sqo_wq, &nxt->work); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * Called if REQ_F_LINK is set, and we fail the head request |
| 644 | */ |
| 645 | static void io_fail_links(struct io_kiocb *req) |
| 646 | { |
| 647 | struct io_kiocb *link; |
| 648 | |
| 649 | while (!list_empty(&req->link_list)) { |
| 650 | link = list_first_entry(&req->link_list, struct io_kiocb, list); |
| 651 | list_del(&link->list); |
| 652 | |
| 653 | io_cqring_add_event(req->ctx, link->user_data, -ECANCELED); |
| 654 | __io_free_req(link); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | static void io_free_req(struct io_kiocb *req) |
| 659 | { |
| 660 | /* |
| 661 | * If LINK is set, we have dependent requests in this chain. If we |
| 662 | * didn't fail this request, queue the first one up, moving any other |
| 663 | * dependencies to the next request. In case of failure, fail the rest |
| 664 | * of the chain. |
| 665 | */ |
| 666 | if (req->flags & REQ_F_LINK) { |
| 667 | if (req->flags & REQ_F_FAIL_LINK) |
| 668 | io_fail_links(req); |
| 669 | else |
| 670 | io_req_link_next(req); |
| 671 | } |
| 672 | |
| 673 | __io_free_req(req); |
| 674 | } |
| 675 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 676 | static void io_put_req(struct io_kiocb *req) |
| 677 | { |
| 678 | if (refcount_dec_and_test(&req->refs)) |
| 679 | io_free_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 682 | /* |
| 683 | * Find and free completed poll iocbs |
| 684 | */ |
| 685 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 686 | struct list_head *done) |
| 687 | { |
| 688 | void *reqs[IO_IOPOLL_BATCH]; |
| 689 | struct io_kiocb *req; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 690 | int to_free; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 691 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 692 | to_free = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 693 | while (!list_empty(done)) { |
| 694 | req = list_first_entry(done, struct io_kiocb, list); |
| 695 | list_del(&req->list); |
| 696 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 697 | io_cqring_fill_event(ctx, req->user_data, req->result); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 698 | (*nr_events)++; |
| 699 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 700 | if (refcount_dec_and_test(&req->refs)) { |
| 701 | /* If we're not using fixed files, we have to pair the |
| 702 | * completion part with the file put. Use regular |
| 703 | * completions for those, only batch free for fixed |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 704 | * file and non-linked commands. |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 705 | */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 706 | if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) == |
| 707 | REQ_F_FIXED_FILE) { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 708 | reqs[to_free++] = req; |
| 709 | if (to_free == ARRAY_SIZE(reqs)) |
| 710 | io_free_req_many(ctx, reqs, &to_free); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 711 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 712 | io_free_req(req); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 713 | } |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 714 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 715 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 716 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 717 | io_commit_cqring(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 718 | io_free_req_many(ctx, reqs, &to_free); |
| 719 | } |
| 720 | |
| 721 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 722 | long min) |
| 723 | { |
| 724 | struct io_kiocb *req, *tmp; |
| 725 | LIST_HEAD(done); |
| 726 | bool spin; |
| 727 | int ret; |
| 728 | |
| 729 | /* |
| 730 | * Only spin for completions if we don't have multiple devices hanging |
| 731 | * off our complete list, and we're under the requested amount. |
| 732 | */ |
| 733 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 734 | |
| 735 | ret = 0; |
| 736 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
| 737 | struct kiocb *kiocb = &req->rw; |
| 738 | |
| 739 | /* |
| 740 | * Move completed entries to our local list. If we find a |
| 741 | * request that requires polling, break out and complete |
| 742 | * the done list first, if we have entries there. |
| 743 | */ |
| 744 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 745 | list_move_tail(&req->list, &done); |
| 746 | continue; |
| 747 | } |
| 748 | if (!list_empty(&done)) |
| 749 | break; |
| 750 | |
| 751 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 752 | if (ret < 0) |
| 753 | break; |
| 754 | |
| 755 | if (ret && spin) |
| 756 | spin = false; |
| 757 | ret = 0; |
| 758 | } |
| 759 | |
| 760 | if (!list_empty(&done)) |
| 761 | io_iopoll_complete(ctx, nr_events, &done); |
| 762 | |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a |
| 768 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 769 | * as a non-spinning completion check. |
| 770 | */ |
| 771 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 772 | long min) |
| 773 | { |
| 774 | while (!list_empty(&ctx->poll_list)) { |
| 775 | int ret; |
| 776 | |
| 777 | ret = io_do_iopoll(ctx, nr_events, min); |
| 778 | if (ret < 0) |
| 779 | return ret; |
| 780 | if (!min || *nr_events >= min) |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | return 1; |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | * We can't just wait for polled events to come to us, we have to actively |
| 789 | * find and complete them. |
| 790 | */ |
| 791 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 792 | { |
| 793 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 794 | return; |
| 795 | |
| 796 | mutex_lock(&ctx->uring_lock); |
| 797 | while (!list_empty(&ctx->poll_list)) { |
| 798 | unsigned int nr_events = 0; |
| 799 | |
| 800 | io_iopoll_getevents(ctx, &nr_events, 1); |
| 801 | } |
| 802 | mutex_unlock(&ctx->uring_lock); |
| 803 | } |
| 804 | |
| 805 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 806 | long min) |
| 807 | { |
| 808 | int ret = 0; |
| 809 | |
| 810 | do { |
| 811 | int tmin = 0; |
| 812 | |
| 813 | if (*nr_events < min) |
| 814 | tmin = min - *nr_events; |
| 815 | |
| 816 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 817 | if (ret <= 0) |
| 818 | break; |
| 819 | ret = 0; |
| 820 | } while (min && !*nr_events && !need_resched()); |
| 821 | |
| 822 | return ret; |
| 823 | } |
| 824 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 825 | static void kiocb_end_write(struct kiocb *kiocb) |
| 826 | { |
| 827 | if (kiocb->ki_flags & IOCB_WRITE) { |
| 828 | struct inode *inode = file_inode(kiocb->ki_filp); |
| 829 | |
| 830 | /* |
| 831 | * Tell lockdep we inherited freeze protection from submission |
| 832 | * thread. |
| 833 | */ |
| 834 | if (S_ISREG(inode->i_mode)) |
| 835 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
| 836 | file_end_write(kiocb->ki_filp); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 841 | { |
| 842 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); |
| 843 | |
| 844 | kiocb_end_write(kiocb); |
| 845 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 846 | if ((req->flags & REQ_F_LINK) && res != req->result) |
| 847 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | c71ffb6 | 2019-05-13 20:58:29 -0600 | [diff] [blame] | 848 | io_cqring_add_event(req->ctx, req->user_data, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 849 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 852 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 853 | { |
| 854 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); |
| 855 | |
| 856 | kiocb_end_write(kiocb); |
| 857 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 858 | if ((req->flags & REQ_F_LINK) && res != req->result) |
| 859 | req->flags |= REQ_F_FAIL_LINK; |
| 860 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 861 | if (res != -EAGAIN) |
| 862 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 863 | } |
| 864 | |
| 865 | /* |
| 866 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 867 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 868 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 869 | * accessing the kiocb cookie. |
| 870 | */ |
| 871 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 872 | { |
| 873 | struct io_ring_ctx *ctx = req->ctx; |
| 874 | |
| 875 | /* |
| 876 | * Track whether we have multiple files in our lists. This will impact |
| 877 | * how we do polling eventually, not spinning if we're on potentially |
| 878 | * different devices. |
| 879 | */ |
| 880 | if (list_empty(&ctx->poll_list)) { |
| 881 | ctx->poll_multi_file = false; |
| 882 | } else if (!ctx->poll_multi_file) { |
| 883 | struct io_kiocb *list_req; |
| 884 | |
| 885 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 886 | list); |
| 887 | if (list_req->rw.ki_filp != req->rw.ki_filp) |
| 888 | ctx->poll_multi_file = true; |
| 889 | } |
| 890 | |
| 891 | /* |
| 892 | * For fast devices, IO may have already completed. If it has, add |
| 893 | * it to the front so we find it first. |
| 894 | */ |
| 895 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 896 | list_add(&req->list, &ctx->poll_list); |
| 897 | else |
| 898 | list_add_tail(&req->list, &ctx->poll_list); |
| 899 | } |
| 900 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 901 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 902 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 903 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 904 | int diff = state->has_refs - state->used_refs; |
| 905 | |
| 906 | if (diff) |
| 907 | fput_many(state->file, diff); |
| 908 | state->file = NULL; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | /* |
| 913 | * Get as many references to a file as we have IOs left in this submission, |
| 914 | * assuming most submissions are for one file, or at least that each file |
| 915 | * has more than one submission. |
| 916 | */ |
| 917 | static struct file *io_file_get(struct io_submit_state *state, int fd) |
| 918 | { |
| 919 | if (!state) |
| 920 | return fget(fd); |
| 921 | |
| 922 | if (state->file) { |
| 923 | if (state->fd == fd) { |
| 924 | state->used_refs++; |
| 925 | state->ios_left--; |
| 926 | return state->file; |
| 927 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 928 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 929 | } |
| 930 | state->file = fget_many(fd, state->ios_left); |
| 931 | if (!state->file) |
| 932 | return NULL; |
| 933 | |
| 934 | state->fd = fd; |
| 935 | state->has_refs = state->ios_left; |
| 936 | state->used_refs = 1; |
| 937 | state->ios_left--; |
| 938 | return state->file; |
| 939 | } |
| 940 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 941 | /* |
| 942 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 943 | * any file. For now, just ensure that anything potentially problematic is done |
| 944 | * inline. |
| 945 | */ |
| 946 | static bool io_file_supports_async(struct file *file) |
| 947 | { |
| 948 | umode_t mode = file_inode(file)->i_mode; |
| 949 | |
| 950 | if (S_ISBLK(mode) || S_ISCHR(mode)) |
| 951 | return true; |
| 952 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 953 | return true; |
| 954 | |
| 955 | return false; |
| 956 | } |
| 957 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 958 | static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 959 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 960 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 961 | const struct io_uring_sqe *sqe = s->sqe; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 962 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 963 | struct kiocb *kiocb = &req->rw; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 964 | unsigned ioprio; |
| 965 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 966 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 967 | if (!req->file) |
| 968 | return -EBADF; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 969 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 970 | if (force_nonblock && !io_file_supports_async(req->file)) |
| 971 | force_nonblock = false; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 972 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 973 | kiocb->ki_pos = READ_ONCE(sqe->off); |
| 974 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 975 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
| 976 | |
| 977 | ioprio = READ_ONCE(sqe->ioprio); |
| 978 | if (ioprio) { |
| 979 | ret = ioprio_check_cap(ioprio); |
| 980 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 981 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 982 | |
| 983 | kiocb->ki_ioprio = ioprio; |
| 984 | } else |
| 985 | kiocb->ki_ioprio = get_current_ioprio(); |
| 986 | |
| 987 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 988 | if (unlikely(ret)) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 989 | return ret; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 990 | |
| 991 | /* don't allow async punt if RWF_NOWAIT was requested */ |
| 992 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 993 | req->flags |= REQ_F_NOWAIT; |
| 994 | |
| 995 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 996 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 997 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 998 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 999 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 1000 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1001 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1002 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1003 | kiocb->ki_flags |= IOCB_HIPRI; |
| 1004 | kiocb->ki_complete = io_complete_rw_iopoll; |
| 1005 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1006 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1007 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1008 | kiocb->ki_complete = io_complete_rw; |
| 1009 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1010 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 1014 | { |
| 1015 | switch (ret) { |
| 1016 | case -EIOCBQUEUED: |
| 1017 | break; |
| 1018 | case -ERESTARTSYS: |
| 1019 | case -ERESTARTNOINTR: |
| 1020 | case -ERESTARTNOHAND: |
| 1021 | case -ERESTART_RESTARTBLOCK: |
| 1022 | /* |
| 1023 | * We can't just restart the syscall, since previously |
| 1024 | * submitted sqes may already be in progress. Just fail this |
| 1025 | * IO with EINTR. |
| 1026 | */ |
| 1027 | ret = -EINTR; |
| 1028 | /* fall through */ |
| 1029 | default: |
| 1030 | kiocb->ki_complete(kiocb, ret, 0); |
| 1031 | } |
| 1032 | } |
| 1033 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1034 | static int io_import_fixed(struct io_ring_ctx *ctx, int rw, |
| 1035 | const struct io_uring_sqe *sqe, |
| 1036 | struct iov_iter *iter) |
| 1037 | { |
| 1038 | size_t len = READ_ONCE(sqe->len); |
| 1039 | struct io_mapped_ubuf *imu; |
| 1040 | unsigned index, buf_index; |
| 1041 | size_t offset; |
| 1042 | u64 buf_addr; |
| 1043 | |
| 1044 | /* attempt to use fixed buffers without having provided iovecs */ |
| 1045 | if (unlikely(!ctx->user_bufs)) |
| 1046 | return -EFAULT; |
| 1047 | |
| 1048 | buf_index = READ_ONCE(sqe->buf_index); |
| 1049 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 1050 | return -EFAULT; |
| 1051 | |
| 1052 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 1053 | imu = &ctx->user_bufs[index]; |
| 1054 | buf_addr = READ_ONCE(sqe->addr); |
| 1055 | |
| 1056 | /* overflow */ |
| 1057 | if (buf_addr + len < buf_addr) |
| 1058 | return -EFAULT; |
| 1059 | /* not inside the mapped region */ |
| 1060 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 1061 | return -EFAULT; |
| 1062 | |
| 1063 | /* |
| 1064 | * May not be a start of buffer, set size appropriately |
| 1065 | * and advance us to the beginning. |
| 1066 | */ |
| 1067 | offset = buf_addr - imu->ubuf; |
| 1068 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
| 1069 | if (offset) |
| 1070 | iov_iter_advance(iter, offset); |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
Jens Axboe | 87e5e6d | 2019-05-14 16:02:22 -0600 | [diff] [blame] | 1074 | static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw, |
| 1075 | const struct sqe_submit *s, struct iovec **iovec, |
| 1076 | struct iov_iter *iter) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1077 | { |
| 1078 | const struct io_uring_sqe *sqe = s->sqe; |
| 1079 | void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 1080 | size_t sqe_len = READ_ONCE(sqe->len); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1081 | u8 opcode; |
| 1082 | |
| 1083 | /* |
| 1084 | * We're reading ->opcode for the second time, but the first read |
| 1085 | * doesn't care whether it's _FIXED or not, so it doesn't matter |
| 1086 | * whether ->opcode changes concurrently. The first read does care |
| 1087 | * about whether it is a READ or a WRITE, so we don't trust this read |
| 1088 | * for that purpose and instead let the caller pass in the read/write |
| 1089 | * flag. |
| 1090 | */ |
| 1091 | opcode = READ_ONCE(sqe->opcode); |
| 1092 | if (opcode == IORING_OP_READ_FIXED || |
| 1093 | opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | 87e5e6d | 2019-05-14 16:02:22 -0600 | [diff] [blame] | 1094 | ssize_t ret = io_import_fixed(ctx, rw, sqe, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1095 | *iovec = NULL; |
| 1096 | return ret; |
| 1097 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1098 | |
| 1099 | if (!s->has_user) |
| 1100 | return -EFAULT; |
| 1101 | |
| 1102 | #ifdef CONFIG_COMPAT |
| 1103 | if (ctx->compat) |
| 1104 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 1105 | iovec, iter); |
| 1106 | #endif |
| 1107 | |
| 1108 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 1109 | } |
| 1110 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1111 | /* |
| 1112 | * Make a note of the last file/offset/direction we punted to async |
| 1113 | * context. We'll use this information to see if we can piggy back a |
| 1114 | * sequential request onto the previous one, if it's still hasn't been |
| 1115 | * completed by the async worker. |
| 1116 | */ |
| 1117 | static void io_async_list_note(int rw, struct io_kiocb *req, size_t len) |
| 1118 | { |
| 1119 | struct async_list *async_list = &req->ctx->pending_async[rw]; |
| 1120 | struct kiocb *kiocb = &req->rw; |
| 1121 | struct file *filp = kiocb->ki_filp; |
| 1122 | off_t io_end = kiocb->ki_pos + len; |
| 1123 | |
| 1124 | if (filp == async_list->file && kiocb->ki_pos == async_list->io_end) { |
| 1125 | unsigned long max_pages; |
| 1126 | |
| 1127 | /* Use 8x RA size as a decent limiter for both reads/writes */ |
| 1128 | max_pages = filp->f_ra.ra_pages; |
| 1129 | if (!max_pages) |
Nikolay Borisov | b542023 | 2019-03-11 23:28:13 -0700 | [diff] [blame] | 1130 | max_pages = VM_READAHEAD_PAGES; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1131 | max_pages *= 8; |
| 1132 | |
| 1133 | /* If max pages are exceeded, reset the state */ |
| 1134 | len >>= PAGE_SHIFT; |
| 1135 | if (async_list->io_pages + len <= max_pages) { |
| 1136 | req->flags |= REQ_F_SEQ_PREV; |
| 1137 | async_list->io_pages += len; |
| 1138 | } else { |
| 1139 | io_end = 0; |
| 1140 | async_list->io_pages = 0; |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | /* New file? Reset state. */ |
| 1145 | if (async_list->file != filp) { |
| 1146 | async_list->io_pages = 0; |
| 1147 | async_list->file = filp; |
| 1148 | } |
| 1149 | async_list->io_end = io_end; |
| 1150 | } |
| 1151 | |
Jens Axboe | e0c5c57 | 2019-03-12 10:18:47 -0600 | [diff] [blame] | 1152 | static int io_read(struct io_kiocb *req, const struct sqe_submit *s, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 1153 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1154 | { |
| 1155 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
| 1156 | struct kiocb *kiocb = &req->rw; |
| 1157 | struct iov_iter iter; |
| 1158 | struct file *file; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1159 | size_t iov_count; |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 1160 | ssize_t read_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1161 | |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 1162 | ret = io_prep_rw(req, s, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1163 | if (ret) |
| 1164 | return ret; |
| 1165 | file = kiocb->ki_filp; |
| 1166 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1167 | if (unlikely(!(file->f_mode & FMODE_READ))) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1168 | return -EBADF; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1169 | if (unlikely(!file->f_op->read_iter)) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1170 | return -EINVAL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1171 | |
| 1172 | ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter); |
Jens Axboe | 87e5e6d | 2019-05-14 16:02:22 -0600 | [diff] [blame] | 1173 | if (ret < 0) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1174 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1175 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 1176 | read_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1177 | if (req->flags & REQ_F_LINK) |
| 1178 | req->result = read_size; |
| 1179 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1180 | iov_count = iov_iter_count(&iter); |
| 1181 | ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1182 | if (!ret) { |
| 1183 | ssize_t ret2; |
| 1184 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1185 | ret2 = call_read_iter(file, kiocb, &iter); |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 1186 | /* |
| 1187 | * In case of a short read, punt to async. This can happen |
| 1188 | * if we have data partially cached. Alternatively we can |
| 1189 | * return the short read, in which case the application will |
| 1190 | * need to issue another SQE and wait for it. That SQE will |
| 1191 | * need async punt anyway, so it's more efficient to do it |
| 1192 | * here. |
| 1193 | */ |
| 1194 | if (force_nonblock && ret2 > 0 && ret2 < read_size) |
| 1195 | ret2 = -EAGAIN; |
| 1196 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1197 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1198 | io_rw_done(kiocb, ret2); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1199 | } else { |
| 1200 | /* |
| 1201 | * If ->needs_lock is true, we're already in async |
| 1202 | * context. |
| 1203 | */ |
| 1204 | if (!s->needs_lock) |
| 1205 | io_async_list_note(READ, req, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1206 | ret = -EAGAIN; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1207 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1208 | } |
| 1209 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1210 | return ret; |
| 1211 | } |
| 1212 | |
Jens Axboe | e0c5c57 | 2019-03-12 10:18:47 -0600 | [diff] [blame] | 1213 | static int io_write(struct io_kiocb *req, const struct sqe_submit *s, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 1214 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1215 | { |
| 1216 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
| 1217 | struct kiocb *kiocb = &req->rw; |
| 1218 | struct iov_iter iter; |
| 1219 | struct file *file; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1220 | size_t iov_count; |
Jens Axboe | 87e5e6d | 2019-05-14 16:02:22 -0600 | [diff] [blame] | 1221 | ssize_t ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1222 | |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 1223 | ret = io_prep_rw(req, s, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1224 | if (ret) |
| 1225 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1226 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1227 | file = kiocb->ki_filp; |
| 1228 | if (unlikely(!(file->f_mode & FMODE_WRITE))) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1229 | return -EBADF; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1230 | if (unlikely(!file->f_op->write_iter)) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1231 | return -EINVAL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1232 | |
| 1233 | ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter); |
Jens Axboe | 87e5e6d | 2019-05-14 16:02:22 -0600 | [diff] [blame] | 1234 | if (ret < 0) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1235 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1236 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1237 | if (req->flags & REQ_F_LINK) |
| 1238 | req->result = ret; |
| 1239 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1240 | iov_count = iov_iter_count(&iter); |
| 1241 | |
| 1242 | ret = -EAGAIN; |
| 1243 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) { |
| 1244 | /* If ->needs_lock is true, we're already in async context. */ |
| 1245 | if (!s->needs_lock) |
| 1246 | io_async_list_note(WRITE, req, iov_count); |
| 1247 | goto out_free; |
| 1248 | } |
| 1249 | |
| 1250 | ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1251 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 1252 | ssize_t ret2; |
| 1253 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1254 | /* |
| 1255 | * Open-code file_start_write here to grab freeze protection, |
| 1256 | * which will be released by another thread in |
| 1257 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 1258 | * released so that it doesn't complain about the held lock when |
| 1259 | * we return to userspace. |
| 1260 | */ |
| 1261 | if (S_ISREG(file_inode(file)->i_mode)) { |
| 1262 | __sb_start_write(file_inode(file)->i_sb, |
| 1263 | SB_FREEZE_WRITE, true); |
| 1264 | __sb_writers_release(file_inode(file)->i_sb, |
| 1265 | SB_FREEZE_WRITE); |
| 1266 | } |
| 1267 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 1268 | |
| 1269 | ret2 = call_write_iter(file, kiocb, &iter); |
| 1270 | if (!force_nonblock || ret2 != -EAGAIN) { |
| 1271 | io_rw_done(kiocb, ret2); |
| 1272 | } else { |
| 1273 | /* |
| 1274 | * If ->needs_lock is true, we're already in async |
| 1275 | * context. |
| 1276 | */ |
| 1277 | if (!s->needs_lock) |
| 1278 | io_async_list_note(WRITE, req, iov_count); |
| 1279 | ret = -EAGAIN; |
| 1280 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1281 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1282 | out_free: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1283 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1284 | return ret; |
| 1285 | } |
| 1286 | |
| 1287 | /* |
| 1288 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 1289 | */ |
| 1290 | static int io_nop(struct io_kiocb *req, u64 user_data) |
| 1291 | { |
| 1292 | struct io_ring_ctx *ctx = req->ctx; |
| 1293 | long err = 0; |
| 1294 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1295 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1296 | return -EINVAL; |
| 1297 | |
Jens Axboe | c71ffb6 | 2019-05-13 20:58:29 -0600 | [diff] [blame] | 1298 | io_cqring_add_event(ctx, user_data, err); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1299 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1300 | return 0; |
| 1301 | } |
| 1302 | |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 1303 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 1304 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 1305 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 1306 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1307 | if (!req->file) |
| 1308 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 1309 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 1310 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1311 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1312 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 1313 | return -EINVAL; |
| 1314 | |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 1315 | return 0; |
| 1316 | } |
| 1317 | |
| 1318 | static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1319 | bool force_nonblock) |
| 1320 | { |
| 1321 | loff_t sqe_off = READ_ONCE(sqe->off); |
| 1322 | loff_t sqe_len = READ_ONCE(sqe->len); |
| 1323 | loff_t end = sqe_off + sqe_len; |
| 1324 | unsigned fsync_flags; |
| 1325 | int ret; |
| 1326 | |
| 1327 | fsync_flags = READ_ONCE(sqe->fsync_flags); |
| 1328 | if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC)) |
| 1329 | return -EINVAL; |
| 1330 | |
| 1331 | ret = io_prep_fsync(req, sqe); |
| 1332 | if (ret) |
| 1333 | return ret; |
| 1334 | |
| 1335 | /* fsync always requires a blocking context */ |
| 1336 | if (force_nonblock) |
| 1337 | return -EAGAIN; |
| 1338 | |
| 1339 | ret = vfs_fsync_range(req->rw.ki_filp, sqe_off, |
| 1340 | end > 0 ? end : LLONG_MAX, |
| 1341 | fsync_flags & IORING_FSYNC_DATASYNC); |
| 1342 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1343 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
| 1344 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | c71ffb6 | 2019-05-13 20:58:29 -0600 | [diff] [blame] | 1345 | io_cqring_add_event(req->ctx, sqe->user_data, ret); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1346 | io_put_req(req); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 1347 | return 0; |
| 1348 | } |
| 1349 | |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 1350 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 1351 | { |
| 1352 | struct io_ring_ctx *ctx = req->ctx; |
| 1353 | int ret = 0; |
| 1354 | |
| 1355 | if (!req->file) |
| 1356 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 1357 | |
| 1358 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1359 | return -EINVAL; |
| 1360 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 1361 | return -EINVAL; |
| 1362 | |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 1363 | return ret; |
| 1364 | } |
| 1365 | |
| 1366 | static int io_sync_file_range(struct io_kiocb *req, |
| 1367 | const struct io_uring_sqe *sqe, |
| 1368 | bool force_nonblock) |
| 1369 | { |
| 1370 | loff_t sqe_off; |
| 1371 | loff_t sqe_len; |
| 1372 | unsigned flags; |
| 1373 | int ret; |
| 1374 | |
| 1375 | ret = io_prep_sfr(req, sqe); |
| 1376 | if (ret) |
| 1377 | return ret; |
| 1378 | |
| 1379 | /* sync_file_range always requires a blocking context */ |
| 1380 | if (force_nonblock) |
| 1381 | return -EAGAIN; |
| 1382 | |
| 1383 | sqe_off = READ_ONCE(sqe->off); |
| 1384 | sqe_len = READ_ONCE(sqe->len); |
| 1385 | flags = READ_ONCE(sqe->sync_range_flags); |
| 1386 | |
| 1387 | ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags); |
| 1388 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1389 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
| 1390 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | c71ffb6 | 2019-05-13 20:58:29 -0600 | [diff] [blame] | 1391 | io_cqring_add_event(req->ctx, sqe->user_data, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 1392 | io_put_req(req); |
| 1393 | return 0; |
| 1394 | } |
| 1395 | |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 1396 | #if defined(CONFIG_NET) |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 1397 | static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1398 | bool force_nonblock, |
| 1399 | long (*fn)(struct socket *, struct user_msghdr __user *, |
| 1400 | unsigned int)) |
| 1401 | { |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 1402 | struct socket *sock; |
| 1403 | int ret; |
| 1404 | |
| 1405 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 1406 | return -EINVAL; |
| 1407 | |
| 1408 | sock = sock_from_file(req->file, &ret); |
| 1409 | if (sock) { |
| 1410 | struct user_msghdr __user *msg; |
| 1411 | unsigned flags; |
| 1412 | |
| 1413 | flags = READ_ONCE(sqe->msg_flags); |
| 1414 | if (flags & MSG_DONTWAIT) |
| 1415 | req->flags |= REQ_F_NOWAIT; |
| 1416 | else if (force_nonblock) |
| 1417 | flags |= MSG_DONTWAIT; |
| 1418 | |
| 1419 | msg = (struct user_msghdr __user *) (unsigned long) |
| 1420 | READ_ONCE(sqe->addr); |
| 1421 | |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 1422 | ret = fn(sock, msg, flags); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 1423 | if (force_nonblock && ret == -EAGAIN) |
| 1424 | return ret; |
| 1425 | } |
| 1426 | |
| 1427 | io_cqring_add_event(req->ctx, sqe->user_data, ret); |
| 1428 | io_put_req(req); |
| 1429 | return 0; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 1430 | } |
| 1431 | #endif |
| 1432 | |
| 1433 | static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1434 | bool force_nonblock) |
| 1435 | { |
| 1436 | #if defined(CONFIG_NET) |
| 1437 | return io_send_recvmsg(req, sqe, force_nonblock, __sys_sendmsg_sock); |
| 1438 | #else |
| 1439 | return -EOPNOTSUPP; |
| 1440 | #endif |
| 1441 | } |
| 1442 | |
| 1443 | static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1444 | bool force_nonblock) |
| 1445 | { |
| 1446 | #if defined(CONFIG_NET) |
| 1447 | return io_send_recvmsg(req, sqe, force_nonblock, __sys_recvmsg_sock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 1448 | #else |
| 1449 | return -EOPNOTSUPP; |
| 1450 | #endif |
| 1451 | } |
| 1452 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1453 | static void io_poll_remove_one(struct io_kiocb *req) |
| 1454 | { |
| 1455 | struct io_poll_iocb *poll = &req->poll; |
| 1456 | |
| 1457 | spin_lock(&poll->head->lock); |
| 1458 | WRITE_ONCE(poll->canceled, true); |
| 1459 | if (!list_empty(&poll->wait.entry)) { |
| 1460 | list_del_init(&poll->wait.entry); |
| 1461 | queue_work(req->ctx->sqo_wq, &req->work); |
| 1462 | } |
| 1463 | spin_unlock(&poll->head->lock); |
| 1464 | |
| 1465 | list_del_init(&req->list); |
| 1466 | } |
| 1467 | |
| 1468 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 1469 | { |
| 1470 | struct io_kiocb *req; |
| 1471 | |
| 1472 | spin_lock_irq(&ctx->completion_lock); |
| 1473 | while (!list_empty(&ctx->cancel_list)) { |
| 1474 | req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list); |
| 1475 | io_poll_remove_one(req); |
| 1476 | } |
| 1477 | spin_unlock_irq(&ctx->completion_lock); |
| 1478 | } |
| 1479 | |
| 1480 | /* |
| 1481 | * Find a running poll command that matches one specified in sqe->addr, |
| 1482 | * and remove it if found. |
| 1483 | */ |
| 1484 | static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 1485 | { |
| 1486 | struct io_ring_ctx *ctx = req->ctx; |
| 1487 | struct io_kiocb *poll_req, *next; |
| 1488 | int ret = -ENOENT; |
| 1489 | |
| 1490 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 1491 | return -EINVAL; |
| 1492 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 1493 | sqe->poll_events) |
| 1494 | return -EINVAL; |
| 1495 | |
| 1496 | spin_lock_irq(&ctx->completion_lock); |
| 1497 | list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) { |
| 1498 | if (READ_ONCE(sqe->addr) == poll_req->user_data) { |
| 1499 | io_poll_remove_one(poll_req); |
| 1500 | ret = 0; |
| 1501 | break; |
| 1502 | } |
| 1503 | } |
| 1504 | spin_unlock_irq(&ctx->completion_lock); |
| 1505 | |
Jens Axboe | c71ffb6 | 2019-05-13 20:58:29 -0600 | [diff] [blame] | 1506 | io_cqring_add_event(req->ctx, sqe->user_data, ret); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1507 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1508 | return 0; |
| 1509 | } |
| 1510 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1511 | static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 1512 | __poll_t mask) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1513 | { |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1514 | req->poll.done = true; |
Jens Axboe | c71ffb6 | 2019-05-13 20:58:29 -0600 | [diff] [blame] | 1515 | io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1516 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | static void io_poll_complete_work(struct work_struct *work) |
| 1520 | { |
| 1521 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 1522 | struct io_poll_iocb *poll = &req->poll; |
| 1523 | struct poll_table_struct pt = { ._key = poll->events }; |
| 1524 | struct io_ring_ctx *ctx = req->ctx; |
| 1525 | __poll_t mask = 0; |
| 1526 | |
| 1527 | if (!READ_ONCE(poll->canceled)) |
| 1528 | mask = vfs_poll(poll->file, &pt) & poll->events; |
| 1529 | |
| 1530 | /* |
| 1531 | * Note that ->ki_cancel callers also delete iocb from active_reqs after |
| 1532 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to |
| 1533 | * synchronize with them. In the cancellation case the list_del_init |
| 1534 | * itself is not actually needed, but harmless so we keep it in to |
| 1535 | * avoid further branches in the fast path. |
| 1536 | */ |
| 1537 | spin_lock_irq(&ctx->completion_lock); |
| 1538 | if (!mask && !READ_ONCE(poll->canceled)) { |
| 1539 | add_wait_queue(poll->head, &poll->wait); |
| 1540 | spin_unlock_irq(&ctx->completion_lock); |
| 1541 | return; |
| 1542 | } |
| 1543 | list_del_init(&req->list); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1544 | io_poll_complete(ctx, req, mask); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1545 | spin_unlock_irq(&ctx->completion_lock); |
| 1546 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1547 | io_cqring_ev_posted(ctx); |
| 1548 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 1552 | void *key) |
| 1553 | { |
| 1554 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, |
| 1555 | wait); |
| 1556 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); |
| 1557 | struct io_ring_ctx *ctx = req->ctx; |
| 1558 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1559 | unsigned long flags; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1560 | |
| 1561 | /* for instances that support it check for an event match first: */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1562 | if (mask && !(mask & poll->events)) |
| 1563 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1564 | |
| 1565 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1566 | |
| 1567 | if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) { |
| 1568 | list_del(&req->list); |
| 1569 | io_poll_complete(ctx, req, mask); |
| 1570 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1571 | |
| 1572 | io_cqring_ev_posted(ctx); |
| 1573 | io_put_req(req); |
| 1574 | } else { |
| 1575 | queue_work(ctx->sqo_wq, &req->work); |
| 1576 | } |
| 1577 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1578 | return 1; |
| 1579 | } |
| 1580 | |
| 1581 | struct io_poll_table { |
| 1582 | struct poll_table_struct pt; |
| 1583 | struct io_kiocb *req; |
| 1584 | int error; |
| 1585 | }; |
| 1586 | |
| 1587 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 1588 | struct poll_table_struct *p) |
| 1589 | { |
| 1590 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 1591 | |
| 1592 | if (unlikely(pt->req->poll.head)) { |
| 1593 | pt->error = -EINVAL; |
| 1594 | return; |
| 1595 | } |
| 1596 | |
| 1597 | pt->error = 0; |
| 1598 | pt->req->poll.head = head; |
| 1599 | add_wait_queue(head, &pt->req->poll.wait); |
| 1600 | } |
| 1601 | |
| 1602 | static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 1603 | { |
| 1604 | struct io_poll_iocb *poll = &req->poll; |
| 1605 | struct io_ring_ctx *ctx = req->ctx; |
| 1606 | struct io_poll_table ipt; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1607 | bool cancel = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1608 | __poll_t mask; |
| 1609 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1610 | |
| 1611 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 1612 | return -EINVAL; |
| 1613 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 1614 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1615 | if (!poll->file) |
| 1616 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1617 | |
| 1618 | INIT_WORK(&req->work, io_poll_complete_work); |
| 1619 | events = READ_ONCE(sqe->poll_events); |
| 1620 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
| 1621 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1622 | poll->head = NULL; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1623 | poll->done = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1624 | poll->canceled = false; |
| 1625 | |
| 1626 | ipt.pt._qproc = io_poll_queue_proc; |
| 1627 | ipt.pt._key = poll->events; |
| 1628 | ipt.req = req; |
| 1629 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ |
| 1630 | |
| 1631 | /* initialized the list so that we can do list_empty checks */ |
| 1632 | INIT_LIST_HEAD(&poll->wait.entry); |
| 1633 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); |
| 1634 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1635 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1636 | |
| 1637 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1638 | if (likely(poll->head)) { |
| 1639 | spin_lock(&poll->head->lock); |
| 1640 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 1641 | if (ipt.error) |
| 1642 | cancel = true; |
| 1643 | ipt.error = 0; |
| 1644 | mask = 0; |
| 1645 | } |
| 1646 | if (mask || ipt.error) |
| 1647 | list_del_init(&poll->wait.entry); |
| 1648 | else if (cancel) |
| 1649 | WRITE_ONCE(poll->canceled, true); |
| 1650 | else if (!poll->done) /* actually waiting for an event */ |
| 1651 | list_add_tail(&req->list, &ctx->cancel_list); |
| 1652 | spin_unlock(&poll->head->lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1653 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1654 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1655 | ipt.error = 0; |
| 1656 | io_poll_complete(ctx, req, mask); |
| 1657 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1658 | spin_unlock_irq(&ctx->completion_lock); |
| 1659 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1660 | if (mask) { |
| 1661 | io_cqring_ev_posted(ctx); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1662 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1663 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1664 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1665 | } |
| 1666 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1667 | static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 1668 | const struct io_uring_sqe *sqe) |
| 1669 | { |
| 1670 | struct io_uring_sqe *sqe_copy; |
| 1671 | |
| 1672 | if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) |
| 1673 | return 0; |
| 1674 | |
| 1675 | sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL); |
| 1676 | if (!sqe_copy) |
| 1677 | return -EAGAIN; |
| 1678 | |
| 1679 | spin_lock_irq(&ctx->completion_lock); |
| 1680 | if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) { |
| 1681 | spin_unlock_irq(&ctx->completion_lock); |
| 1682 | kfree(sqe_copy); |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
| 1686 | memcpy(sqe_copy, sqe, sizeof(*sqe_copy)); |
| 1687 | req->submit.sqe = sqe_copy; |
| 1688 | |
| 1689 | INIT_WORK(&req->work, io_sq_wq_submit_work); |
| 1690 | list_add_tail(&req->list, &ctx->defer_list); |
| 1691 | spin_unlock_irq(&ctx->completion_lock); |
| 1692 | return -EIOCBQUEUED; |
| 1693 | } |
| 1694 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1695 | static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 1696 | const struct sqe_submit *s, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1697 | { |
Jens Axboe | e0c5c57 | 2019-03-12 10:18:47 -0600 | [diff] [blame] | 1698 | int ret, opcode; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1699 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1700 | req->user_data = READ_ONCE(s->sqe->user_data); |
| 1701 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1702 | if (unlikely(s->index >= ctx->sq_entries)) |
| 1703 | return -EINVAL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1704 | |
| 1705 | opcode = READ_ONCE(s->sqe->opcode); |
| 1706 | switch (opcode) { |
| 1707 | case IORING_OP_NOP: |
| 1708 | ret = io_nop(req, req->user_data); |
| 1709 | break; |
| 1710 | case IORING_OP_READV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1711 | if (unlikely(s->sqe->buf_index)) |
| 1712 | return -EINVAL; |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 1713 | ret = io_read(req, s, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1714 | break; |
| 1715 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1716 | if (unlikely(s->sqe->buf_index)) |
| 1717 | return -EINVAL; |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 1718 | ret = io_write(req, s, force_nonblock); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1719 | break; |
| 1720 | case IORING_OP_READ_FIXED: |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 1721 | ret = io_read(req, s, force_nonblock); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1722 | break; |
| 1723 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 1724 | ret = io_write(req, s, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1725 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 1726 | case IORING_OP_FSYNC: |
| 1727 | ret = io_fsync(req, s->sqe, force_nonblock); |
| 1728 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 1729 | case IORING_OP_POLL_ADD: |
| 1730 | ret = io_poll_add(req, s->sqe); |
| 1731 | break; |
| 1732 | case IORING_OP_POLL_REMOVE: |
| 1733 | ret = io_poll_remove(req, s->sqe); |
| 1734 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 1735 | case IORING_OP_SYNC_FILE_RANGE: |
| 1736 | ret = io_sync_file_range(req, s->sqe, force_nonblock); |
| 1737 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 1738 | case IORING_OP_SENDMSG: |
| 1739 | ret = io_sendmsg(req, s->sqe, force_nonblock); |
| 1740 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 1741 | case IORING_OP_RECVMSG: |
| 1742 | ret = io_recvmsg(req, s->sqe, force_nonblock); |
| 1743 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1744 | default: |
| 1745 | ret = -EINVAL; |
| 1746 | break; |
| 1747 | } |
| 1748 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1749 | if (ret) |
| 1750 | return ret; |
| 1751 | |
| 1752 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1753 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1754 | return -EAGAIN; |
| 1755 | |
| 1756 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 1757 | if (s->needs_lock) |
| 1758 | mutex_lock(&ctx->uring_lock); |
| 1759 | io_iopoll_req_issued(req); |
| 1760 | if (s->needs_lock) |
| 1761 | mutex_unlock(&ctx->uring_lock); |
| 1762 | } |
| 1763 | |
| 1764 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1767 | static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx, |
| 1768 | const struct io_uring_sqe *sqe) |
| 1769 | { |
| 1770 | switch (sqe->opcode) { |
| 1771 | case IORING_OP_READV: |
| 1772 | case IORING_OP_READ_FIXED: |
| 1773 | return &ctx->pending_async[READ]; |
| 1774 | case IORING_OP_WRITEV: |
| 1775 | case IORING_OP_WRITE_FIXED: |
| 1776 | return &ctx->pending_async[WRITE]; |
| 1777 | default: |
| 1778 | return NULL; |
| 1779 | } |
| 1780 | } |
| 1781 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1782 | static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe) |
| 1783 | { |
| 1784 | u8 opcode = READ_ONCE(sqe->opcode); |
| 1785 | |
| 1786 | return !(opcode == IORING_OP_READ_FIXED || |
| 1787 | opcode == IORING_OP_WRITE_FIXED); |
| 1788 | } |
| 1789 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1790 | static void io_sq_wq_submit_work(struct work_struct *work) |
| 1791 | { |
| 1792 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1793 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1794 | struct mm_struct *cur_mm = NULL; |
| 1795 | struct async_list *async_list; |
| 1796 | LIST_HEAD(req_list); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1797 | mm_segment_t old_fs; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1798 | int ret; |
| 1799 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1800 | async_list = io_async_list_from_sqe(ctx, req->submit.sqe); |
| 1801 | restart: |
| 1802 | do { |
| 1803 | struct sqe_submit *s = &req->submit; |
| 1804 | const struct io_uring_sqe *sqe = s->sqe; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1805 | |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1806 | /* Ensure we clear previously set non-block flag */ |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1807 | req->rw.ki_flags &= ~IOCB_NOWAIT; |
| 1808 | |
| 1809 | ret = 0; |
| 1810 | if (io_sqe_needs_user(sqe) && !cur_mm) { |
| 1811 | if (!mmget_not_zero(ctx->sqo_mm)) { |
| 1812 | ret = -EFAULT; |
| 1813 | } else { |
| 1814 | cur_mm = ctx->sqo_mm; |
| 1815 | use_mm(cur_mm); |
| 1816 | old_fs = get_fs(); |
| 1817 | set_fs(USER_DS); |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | if (!ret) { |
| 1822 | s->has_user = cur_mm != NULL; |
| 1823 | s->needs_lock = true; |
| 1824 | do { |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 1825 | ret = __io_submit_sqe(ctx, req, s, false); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1826 | /* |
| 1827 | * We can get EAGAIN for polled IO even though |
| 1828 | * we're forcing a sync submission from here, |
| 1829 | * since we can't wait for request slots on the |
| 1830 | * block side. |
| 1831 | */ |
| 1832 | if (ret != -EAGAIN) |
| 1833 | break; |
| 1834 | cond_resched(); |
| 1835 | } while (1); |
| 1836 | } |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 1837 | |
| 1838 | /* drop submission reference */ |
| 1839 | io_put_req(req); |
| 1840 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1841 | if (ret) { |
Jens Axboe | c71ffb6 | 2019-05-13 20:58:29 -0600 | [diff] [blame] | 1842 | io_cqring_add_event(ctx, sqe->user_data, ret); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1843 | io_put_req(req); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | /* async context always use a copy of the sqe */ |
| 1847 | kfree(sqe); |
| 1848 | |
Zhengyuan Liu | f7b76ac | 2019-07-16 23:26:14 +0800 | [diff] [blame] | 1849 | /* req from defer and link list needn't decrease async cnt */ |
| 1850 | if (req->flags & (REQ_F_IO_DRAINED | REQ_F_LINK_DONE)) |
| 1851 | goto out; |
| 1852 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1853 | if (!async_list) |
| 1854 | break; |
| 1855 | if (!list_empty(&req_list)) { |
| 1856 | req = list_first_entry(&req_list, struct io_kiocb, |
| 1857 | list); |
| 1858 | list_del(&req->list); |
| 1859 | continue; |
| 1860 | } |
| 1861 | if (list_empty(&async_list->list)) |
| 1862 | break; |
| 1863 | |
| 1864 | req = NULL; |
| 1865 | spin_lock(&async_list->lock); |
| 1866 | if (list_empty(&async_list->list)) { |
| 1867 | spin_unlock(&async_list->lock); |
| 1868 | break; |
| 1869 | } |
| 1870 | list_splice_init(&async_list->list, &req_list); |
| 1871 | spin_unlock(&async_list->lock); |
| 1872 | |
| 1873 | req = list_first_entry(&req_list, struct io_kiocb, list); |
| 1874 | list_del(&req->list); |
| 1875 | } while (req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1876 | |
| 1877 | /* |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1878 | * Rare case of racing with a submitter. If we find the count has |
| 1879 | * dropped to zero AND we have pending work items, then restart |
| 1880 | * the processing. This is a tiny race window. |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1881 | */ |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1882 | if (async_list) { |
| 1883 | ret = atomic_dec_return(&async_list->cnt); |
| 1884 | while (!ret && !list_empty(&async_list->list)) { |
| 1885 | spin_lock(&async_list->lock); |
| 1886 | atomic_inc(&async_list->cnt); |
| 1887 | list_splice_init(&async_list->list, &req_list); |
| 1888 | spin_unlock(&async_list->lock); |
| 1889 | |
| 1890 | if (!list_empty(&req_list)) { |
| 1891 | req = list_first_entry(&req_list, |
| 1892 | struct io_kiocb, list); |
| 1893 | list_del(&req->list); |
| 1894 | goto restart; |
| 1895 | } |
| 1896 | ret = atomic_dec_return(&async_list->cnt); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1897 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1898 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1899 | |
Zhengyuan Liu | f7b76ac | 2019-07-16 23:26:14 +0800 | [diff] [blame] | 1900 | out: |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1901 | if (cur_mm) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1902 | set_fs(old_fs); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1903 | unuse_mm(cur_mm); |
| 1904 | mmput(cur_mm); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1905 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1906 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1907 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1908 | /* |
| 1909 | * See if we can piggy back onto previously submitted work, that is still |
| 1910 | * running. We currently only allow this if the new request is sequential |
| 1911 | * to the previous one we punted. |
| 1912 | */ |
| 1913 | static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req) |
| 1914 | { |
| 1915 | bool ret = false; |
| 1916 | |
| 1917 | if (!list) |
| 1918 | return false; |
| 1919 | if (!(req->flags & REQ_F_SEQ_PREV)) |
| 1920 | return false; |
| 1921 | if (!atomic_read(&list->cnt)) |
| 1922 | return false; |
| 1923 | |
| 1924 | ret = true; |
| 1925 | spin_lock(&list->lock); |
| 1926 | list_add_tail(&req->list, &list->list); |
Zhengyuan Liu | c0e48f9 | 2019-07-18 20:44:00 +0800 | [diff] [blame^] | 1927 | /* |
| 1928 | * Ensure we see a simultaneous modification from io_sq_wq_submit_work() |
| 1929 | */ |
| 1930 | smp_mb(); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1931 | if (!atomic_read(&list->cnt)) { |
| 1932 | list_del_init(&req->list); |
| 1933 | ret = false; |
| 1934 | } |
| 1935 | spin_unlock(&list->lock); |
| 1936 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1937 | } |
| 1938 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1939 | static bool io_op_needs_file(const struct io_uring_sqe *sqe) |
| 1940 | { |
| 1941 | int op = READ_ONCE(sqe->opcode); |
| 1942 | |
| 1943 | switch (op) { |
| 1944 | case IORING_OP_NOP: |
| 1945 | case IORING_OP_POLL_REMOVE: |
| 1946 | return false; |
| 1947 | default: |
| 1948 | return true; |
| 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s, |
| 1953 | struct io_submit_state *state, struct io_kiocb *req) |
| 1954 | { |
| 1955 | unsigned flags; |
| 1956 | int fd; |
| 1957 | |
| 1958 | flags = READ_ONCE(s->sqe->flags); |
| 1959 | fd = READ_ONCE(s->sqe->fd); |
| 1960 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1961 | if (flags & IOSQE_IO_DRAIN) { |
| 1962 | req->flags |= REQ_F_IO_DRAIN; |
| 1963 | req->sequence = ctx->cached_sq_head - 1; |
| 1964 | } |
| 1965 | |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 1966 | if (!io_op_needs_file(s->sqe)) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1967 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1968 | |
| 1969 | if (flags & IOSQE_FIXED_FILE) { |
| 1970 | if (unlikely(!ctx->user_files || |
| 1971 | (unsigned) fd >= ctx->nr_user_files)) |
| 1972 | return -EBADF; |
| 1973 | req->file = ctx->user_files[fd]; |
| 1974 | req->flags |= REQ_F_FIXED_FILE; |
| 1975 | } else { |
| 1976 | if (s->needs_fixed_file) |
| 1977 | return -EBADF; |
| 1978 | req->file = io_file_get(state, fd); |
| 1979 | if (unlikely(!req->file)) |
| 1980 | return -EBADF; |
| 1981 | } |
| 1982 | |
| 1983 | return 0; |
| 1984 | } |
| 1985 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1986 | static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 1987 | struct sqe_submit *s) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1988 | { |
Jens Axboe | e0c5c57 | 2019-03-12 10:18:47 -0600 | [diff] [blame] | 1989 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1990 | |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 1991 | ret = __io_submit_sqe(ctx, req, s, true); |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1992 | if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1993 | struct io_uring_sqe *sqe_copy; |
| 1994 | |
| 1995 | sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL); |
| 1996 | if (sqe_copy) { |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 1997 | struct async_list *list; |
| 1998 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1999 | memcpy(sqe_copy, s->sqe, sizeof(*sqe_copy)); |
| 2000 | s->sqe = sqe_copy; |
| 2001 | |
| 2002 | memcpy(&req->submit, s, sizeof(*s)); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2003 | list = io_async_list_from_sqe(ctx, s->sqe); |
| 2004 | if (!io_add_to_prev_work(list, req)) { |
| 2005 | if (list) |
| 2006 | atomic_inc(&list->cnt); |
| 2007 | INIT_WORK(&req->work, io_sq_wq_submit_work); |
| 2008 | queue_work(ctx->sqo_wq, &req->work); |
| 2009 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2010 | |
| 2011 | /* |
| 2012 | * Queued up for async execution, worker will release |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2013 | * submit reference when the iocb is actually submitted. |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2014 | */ |
| 2015 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2016 | } |
| 2017 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2018 | |
| 2019 | /* drop submission reference */ |
| 2020 | io_put_req(req); |
| 2021 | |
| 2022 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2023 | if (ret) { |
| 2024 | io_cqring_add_event(ctx, req->user_data, ret); |
| 2025 | if (req->flags & REQ_F_LINK) |
| 2026 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2027 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2028 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2029 | |
| 2030 | return ret; |
| 2031 | } |
| 2032 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2033 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK) |
| 2034 | |
| 2035 | static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s, |
| 2036 | struct io_submit_state *state, struct io_kiocb **link) |
| 2037 | { |
| 2038 | struct io_uring_sqe *sqe_copy; |
| 2039 | struct io_kiocb *req; |
| 2040 | int ret; |
| 2041 | |
| 2042 | /* enforce forwards compatibility on users */ |
| 2043 | if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) { |
| 2044 | ret = -EINVAL; |
| 2045 | goto err; |
| 2046 | } |
| 2047 | |
| 2048 | req = io_get_req(ctx, state); |
| 2049 | if (unlikely(!req)) { |
| 2050 | ret = -EAGAIN; |
| 2051 | goto err; |
| 2052 | } |
| 2053 | |
| 2054 | ret = io_req_set_file(ctx, s, state, req); |
| 2055 | if (unlikely(ret)) { |
| 2056 | err_req: |
| 2057 | io_free_req(req); |
| 2058 | err: |
| 2059 | io_cqring_add_event(ctx, s->sqe->user_data, ret); |
| 2060 | return; |
| 2061 | } |
| 2062 | |
| 2063 | ret = io_req_defer(ctx, req, s->sqe); |
| 2064 | if (ret) { |
| 2065 | if (ret != -EIOCBQUEUED) |
| 2066 | goto err_req; |
| 2067 | return; |
| 2068 | } |
| 2069 | |
| 2070 | /* |
| 2071 | * If we already have a head request, queue this one for async |
| 2072 | * submittal once the head completes. If we don't have a head but |
| 2073 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 2074 | * submitted sync once the chain is complete. If none of those |
| 2075 | * conditions are true (normal request), then just queue it. |
| 2076 | */ |
| 2077 | if (*link) { |
| 2078 | struct io_kiocb *prev = *link; |
| 2079 | |
| 2080 | sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL); |
| 2081 | if (!sqe_copy) { |
| 2082 | ret = -EAGAIN; |
| 2083 | goto err_req; |
| 2084 | } |
| 2085 | |
| 2086 | s->sqe = sqe_copy; |
| 2087 | memcpy(&req->submit, s, sizeof(*s)); |
| 2088 | list_add_tail(&req->list, &prev->link_list); |
| 2089 | } else if (s->sqe->flags & IOSQE_IO_LINK) { |
| 2090 | req->flags |= REQ_F_LINK; |
| 2091 | |
| 2092 | memcpy(&req->submit, s, sizeof(*s)); |
| 2093 | INIT_LIST_HEAD(&req->link_list); |
| 2094 | *link = req; |
| 2095 | } else { |
| 2096 | io_queue_sqe(ctx, req, s); |
| 2097 | } |
| 2098 | } |
| 2099 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2100 | /* |
| 2101 | * Batched submission is done, ensure local IO is flushed out. |
| 2102 | */ |
| 2103 | static void io_submit_state_end(struct io_submit_state *state) |
| 2104 | { |
| 2105 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 2106 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 2107 | if (state->free_reqs) |
| 2108 | kmem_cache_free_bulk(req_cachep, state->free_reqs, |
| 2109 | &state->reqs[state->cur_req]); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | /* |
| 2113 | * Start submission side cache. |
| 2114 | */ |
| 2115 | static void io_submit_state_start(struct io_submit_state *state, |
| 2116 | struct io_ring_ctx *ctx, unsigned max_ios) |
| 2117 | { |
| 2118 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 2119 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2120 | state->file = NULL; |
| 2121 | state->ios_left = max_ios; |
| 2122 | } |
| 2123 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2124 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 2125 | { |
| 2126 | struct io_sq_ring *ring = ctx->sq_ring; |
| 2127 | |
| 2128 | if (ctx->cached_sq_head != READ_ONCE(ring->r.head)) { |
| 2129 | /* |
| 2130 | * Ensure any loads from the SQEs are done at this point, |
| 2131 | * since once we write the new head, the application could |
| 2132 | * write new data to them. |
| 2133 | */ |
| 2134 | smp_store_release(&ring->r.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | /* |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2139 | * Fetch an sqe, if one is available. Note that s->sqe will point to memory |
| 2140 | * that is mapped by userspace. This means that care needs to be taken to |
| 2141 | * ensure that reads are stable, as we cannot rely on userspace always |
| 2142 | * being a good citizen. If members of the sqe are validated and then later |
| 2143 | * used, it's important that those reads are done through READ_ONCE() to |
| 2144 | * prevent a re-load down the line. |
| 2145 | */ |
| 2146 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s) |
| 2147 | { |
| 2148 | struct io_sq_ring *ring = ctx->sq_ring; |
| 2149 | unsigned head; |
| 2150 | |
| 2151 | /* |
| 2152 | * The cached sq head (or cq tail) serves two purposes: |
| 2153 | * |
| 2154 | * 1) allows us to batch the cost of updating the user visible |
| 2155 | * head updates. |
| 2156 | * 2) allows the kernel side to track the head on its own, even |
| 2157 | * though the application is the one updating it. |
| 2158 | */ |
| 2159 | head = ctx->cached_sq_head; |
Stefan Bühler | e523a29 | 2019-04-19 11:57:44 +0200 | [diff] [blame] | 2160 | /* make sure SQ entry isn't read before tail */ |
| 2161 | if (head == smp_load_acquire(&ring->r.tail)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2162 | return false; |
| 2163 | |
| 2164 | head = READ_ONCE(ring->array[head & ctx->sq_mask]); |
| 2165 | if (head < ctx->sq_entries) { |
| 2166 | s->index = head; |
| 2167 | s->sqe = &ctx->sq_sqes[head]; |
| 2168 | ctx->cached_sq_head++; |
| 2169 | return true; |
| 2170 | } |
| 2171 | |
| 2172 | /* drop invalid entries */ |
| 2173 | ctx->cached_sq_head++; |
| 2174 | ring->dropped++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2175 | return false; |
| 2176 | } |
| 2177 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2178 | static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes, |
| 2179 | unsigned int nr, bool has_user, bool mm_fault) |
| 2180 | { |
| 2181 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2182 | struct io_kiocb *link = NULL; |
| 2183 | bool prev_was_link = false; |
| 2184 | int i, submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2185 | |
| 2186 | if (nr > IO_PLUG_THRESHOLD) { |
| 2187 | io_submit_state_start(&state, ctx, nr); |
| 2188 | statep = &state; |
| 2189 | } |
| 2190 | |
| 2191 | for (i = 0; i < nr; i++) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2192 | /* |
| 2193 | * If previous wasn't linked and we have a linked command, |
| 2194 | * that's the end of the chain. Submit the previous link. |
| 2195 | */ |
| 2196 | if (!prev_was_link && link) { |
| 2197 | io_queue_sqe(ctx, link, &link->submit); |
| 2198 | link = NULL; |
| 2199 | } |
| 2200 | prev_was_link = (sqes[i].sqe->flags & IOSQE_IO_LINK) != 0; |
| 2201 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2202 | if (unlikely(mm_fault)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2203 | io_cqring_add_event(ctx, sqes[i].sqe->user_data, |
| 2204 | -EFAULT); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2205 | } else { |
| 2206 | sqes[i].has_user = has_user; |
| 2207 | sqes[i].needs_lock = true; |
| 2208 | sqes[i].needs_fixed_file = true; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2209 | io_submit_sqe(ctx, &sqes[i], statep, &link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2210 | submitted++; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2211 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2212 | } |
| 2213 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2214 | if (link) |
| 2215 | io_queue_sqe(ctx, link, &link->submit); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2216 | if (statep) |
| 2217 | io_submit_state_end(&state); |
| 2218 | |
| 2219 | return submitted; |
| 2220 | } |
| 2221 | |
| 2222 | static int io_sq_thread(void *data) |
| 2223 | { |
| 2224 | struct sqe_submit sqes[IO_IOPOLL_BATCH]; |
| 2225 | struct io_ring_ctx *ctx = data; |
| 2226 | struct mm_struct *cur_mm = NULL; |
| 2227 | mm_segment_t old_fs; |
| 2228 | DEFINE_WAIT(wait); |
| 2229 | unsigned inflight; |
| 2230 | unsigned long timeout; |
| 2231 | |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 2232 | complete(&ctx->sqo_thread_started); |
| 2233 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2234 | old_fs = get_fs(); |
| 2235 | set_fs(USER_DS); |
| 2236 | |
| 2237 | timeout = inflight = 0; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 2238 | while (!kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2239 | bool all_fixed, mm_fault = false; |
| 2240 | int i; |
| 2241 | |
| 2242 | if (inflight) { |
| 2243 | unsigned nr_events = 0; |
| 2244 | |
| 2245 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
| 2246 | /* |
| 2247 | * We disallow the app entering submit/complete |
| 2248 | * with polling, but we still need to lock the |
| 2249 | * ring to prevent racing with polled issue |
| 2250 | * that got punted to a workqueue. |
| 2251 | */ |
| 2252 | mutex_lock(&ctx->uring_lock); |
| 2253 | io_iopoll_check(ctx, &nr_events, 0); |
| 2254 | mutex_unlock(&ctx->uring_lock); |
| 2255 | } else { |
| 2256 | /* |
| 2257 | * Normal IO, just pretend everything completed. |
| 2258 | * We don't have to poll completions for that. |
| 2259 | */ |
| 2260 | nr_events = inflight; |
| 2261 | } |
| 2262 | |
| 2263 | inflight -= nr_events; |
| 2264 | if (!inflight) |
| 2265 | timeout = jiffies + ctx->sq_thread_idle; |
| 2266 | } |
| 2267 | |
| 2268 | if (!io_get_sqring(ctx, &sqes[0])) { |
| 2269 | /* |
| 2270 | * We're polling. If we're within the defined idle |
| 2271 | * period, then let us spin without work before going |
| 2272 | * to sleep. |
| 2273 | */ |
| 2274 | if (inflight || !time_after(jiffies, timeout)) { |
| 2275 | cpu_relax(); |
| 2276 | continue; |
| 2277 | } |
| 2278 | |
| 2279 | /* |
| 2280 | * Drop cur_mm before scheduling, we can't hold it for |
| 2281 | * long periods (or over schedule()). Do this before |
| 2282 | * adding ourselves to the waitqueue, as the unuse/drop |
| 2283 | * may sleep. |
| 2284 | */ |
| 2285 | if (cur_mm) { |
| 2286 | unuse_mm(cur_mm); |
| 2287 | mmput(cur_mm); |
| 2288 | cur_mm = NULL; |
| 2289 | } |
| 2290 | |
| 2291 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 2292 | TASK_INTERRUPTIBLE); |
| 2293 | |
| 2294 | /* Tell userspace we may need a wakeup call */ |
| 2295 | ctx->sq_ring->flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 2296 | /* make sure to read SQ tail after writing flags */ |
| 2297 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2298 | |
| 2299 | if (!io_get_sqring(ctx, &sqes[0])) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 2300 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2301 | finish_wait(&ctx->sqo_wait, &wait); |
| 2302 | break; |
| 2303 | } |
| 2304 | if (signal_pending(current)) |
| 2305 | flush_signals(current); |
| 2306 | schedule(); |
| 2307 | finish_wait(&ctx->sqo_wait, &wait); |
| 2308 | |
| 2309 | ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2310 | continue; |
| 2311 | } |
| 2312 | finish_wait(&ctx->sqo_wait, &wait); |
| 2313 | |
| 2314 | ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2315 | } |
| 2316 | |
| 2317 | i = 0; |
| 2318 | all_fixed = true; |
| 2319 | do { |
| 2320 | if (all_fixed && io_sqe_needs_user(sqes[i].sqe)) |
| 2321 | all_fixed = false; |
| 2322 | |
| 2323 | i++; |
| 2324 | if (i == ARRAY_SIZE(sqes)) |
| 2325 | break; |
| 2326 | } while (io_get_sqring(ctx, &sqes[i])); |
| 2327 | |
| 2328 | /* Unless all new commands are FIXED regions, grab mm */ |
| 2329 | if (!all_fixed && !cur_mm) { |
| 2330 | mm_fault = !mmget_not_zero(ctx->sqo_mm); |
| 2331 | if (!mm_fault) { |
| 2332 | use_mm(ctx->sqo_mm); |
| 2333 | cur_mm = ctx->sqo_mm; |
| 2334 | } |
| 2335 | } |
| 2336 | |
| 2337 | inflight += io_submit_sqes(ctx, sqes, i, cur_mm != NULL, |
| 2338 | mm_fault); |
| 2339 | |
| 2340 | /* Commit SQ ring head once we've consumed all SQEs */ |
| 2341 | io_commit_sqring(ctx); |
| 2342 | } |
| 2343 | |
| 2344 | set_fs(old_fs); |
| 2345 | if (cur_mm) { |
| 2346 | unuse_mm(cur_mm); |
| 2347 | mmput(cur_mm); |
| 2348 | } |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 2349 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 2350 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 2351 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2352 | return 0; |
| 2353 | } |
| 2354 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2355 | static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit) |
| 2356 | { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2357 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2358 | struct io_kiocb *link = NULL; |
| 2359 | bool prev_was_link = false; |
Jens Axboe | 5c8b0b5 | 2019-04-30 10:16:07 -0600 | [diff] [blame] | 2360 | int i, submit = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2361 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2362 | if (to_submit > IO_PLUG_THRESHOLD) { |
| 2363 | io_submit_state_start(&state, ctx, to_submit); |
| 2364 | statep = &state; |
| 2365 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2366 | |
| 2367 | for (i = 0; i < to_submit; i++) { |
| 2368 | struct sqe_submit s; |
| 2369 | |
| 2370 | if (!io_get_sqring(ctx, &s)) |
| 2371 | break; |
| 2372 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2373 | /* |
| 2374 | * If previous wasn't linked and we have a linked command, |
| 2375 | * that's the end of the chain. Submit the previous link. |
| 2376 | */ |
| 2377 | if (!prev_was_link && link) { |
| 2378 | io_queue_sqe(ctx, link, &link->submit); |
| 2379 | link = NULL; |
| 2380 | } |
| 2381 | prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0; |
| 2382 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2383 | s.has_user = true; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2384 | s.needs_lock = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2385 | s.needs_fixed_file = false; |
Jens Axboe | 5c8b0b5 | 2019-04-30 10:16:07 -0600 | [diff] [blame] | 2386 | submit++; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2387 | io_submit_sqe(ctx, &s, statep, &link); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2388 | } |
| 2389 | io_commit_sqring(ctx); |
| 2390 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2391 | if (link) |
| 2392 | io_queue_sqe(ctx, link, &link->submit); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 2393 | if (statep) |
| 2394 | io_submit_state_end(statep); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2395 | |
Jens Axboe | 5c8b0b5 | 2019-04-30 10:16:07 -0600 | [diff] [blame] | 2396 | return submit; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | static unsigned io_cqring_events(struct io_cq_ring *ring) |
| 2400 | { |
Jackie Liu | dc6ce4b | 2019-05-16 11:46:30 +0800 | [diff] [blame] | 2401 | /* See comment at the top of this file */ |
| 2402 | smp_rmb(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2403 | return READ_ONCE(ring->r.tail) - READ_ONCE(ring->r.head); |
| 2404 | } |
| 2405 | |
| 2406 | /* |
| 2407 | * Wait until events become available, if we don't already have some. The |
| 2408 | * application must reap them itself, as they reside on the shared cq ring. |
| 2409 | */ |
| 2410 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 2411 | const sigset_t __user *sig, size_t sigsz) |
| 2412 | { |
| 2413 | struct io_cq_ring *ring = ctx->cq_ring; |
| 2414 | sigset_t ksigmask, sigsaved; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2415 | int ret; |
| 2416 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2417 | if (io_cqring_events(ring) >= min_events) |
| 2418 | return 0; |
| 2419 | |
| 2420 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 2421 | #ifdef CONFIG_COMPAT |
| 2422 | if (in_compat_syscall()) |
| 2423 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
| 2424 | &ksigmask, &sigsaved, sigsz); |
| 2425 | else |
| 2426 | #endif |
| 2427 | ret = set_user_sigmask(sig, &ksigmask, |
| 2428 | &sigsaved, sigsz); |
| 2429 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2430 | if (ret) |
| 2431 | return ret; |
| 2432 | } |
| 2433 | |
Jackie Liu | fdb288a | 2019-05-16 11:46:31 +0800 | [diff] [blame] | 2434 | ret = wait_event_interruptible(ctx->wait, io_cqring_events(ring) >= min_events); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2435 | |
| 2436 | if (sig) |
Oleg Nesterov | 97abc88 | 2019-06-28 12:06:50 -0700 | [diff] [blame] | 2437 | restore_user_sigmask(sig, &sigsaved, ret == -ERESTARTSYS); |
| 2438 | |
| 2439 | if (ret == -ERESTARTSYS) |
| 2440 | ret = -EINTR; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2441 | |
| 2442 | return READ_ONCE(ring->r.head) == READ_ONCE(ring->r.tail) ? ret : 0; |
| 2443 | } |
| 2444 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2445 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 2446 | { |
| 2447 | #if defined(CONFIG_UNIX) |
| 2448 | if (ctx->ring_sock) { |
| 2449 | struct sock *sock = ctx->ring_sock->sk; |
| 2450 | struct sk_buff *skb; |
| 2451 | |
| 2452 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 2453 | kfree_skb(skb); |
| 2454 | } |
| 2455 | #else |
| 2456 | int i; |
| 2457 | |
| 2458 | for (i = 0; i < ctx->nr_user_files; i++) |
| 2459 | fput(ctx->user_files[i]); |
| 2460 | #endif |
| 2461 | } |
| 2462 | |
| 2463 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 2464 | { |
| 2465 | if (!ctx->user_files) |
| 2466 | return -ENXIO; |
| 2467 | |
| 2468 | __io_sqe_files_unregister(ctx); |
| 2469 | kfree(ctx->user_files); |
| 2470 | ctx->user_files = NULL; |
| 2471 | ctx->nr_user_files = 0; |
| 2472 | return 0; |
| 2473 | } |
| 2474 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2475 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 2476 | { |
| 2477 | if (ctx->sqo_thread) { |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 2478 | wait_for_completion(&ctx->sqo_thread_started); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 2479 | /* |
| 2480 | * The park is a bit of a work-around, without it we get |
| 2481 | * warning spews on shutdown with SQPOLL set and affinity |
| 2482 | * set to a single CPU. |
| 2483 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 2484 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2485 | kthread_stop(ctx->sqo_thread); |
| 2486 | ctx->sqo_thread = NULL; |
| 2487 | } |
| 2488 | } |
| 2489 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2490 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 2491 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2492 | io_sq_thread_stop(ctx); |
| 2493 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2494 | if (ctx->sqo_wq) { |
| 2495 | destroy_workqueue(ctx->sqo_wq); |
| 2496 | ctx->sqo_wq = NULL; |
| 2497 | } |
| 2498 | } |
| 2499 | |
| 2500 | #if defined(CONFIG_UNIX) |
| 2501 | static void io_destruct_skb(struct sk_buff *skb) |
| 2502 | { |
| 2503 | struct io_ring_ctx *ctx = skb->sk->sk_user_data; |
| 2504 | |
| 2505 | io_finish_async(ctx); |
| 2506 | unix_destruct_scm(skb); |
| 2507 | } |
| 2508 | |
| 2509 | /* |
| 2510 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 2511 | * the io_uring can be safely unregistered on process exit, even if we have |
| 2512 | * loops in the file referencing. |
| 2513 | */ |
| 2514 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 2515 | { |
| 2516 | struct sock *sk = ctx->ring_sock->sk; |
| 2517 | struct scm_fp_list *fpl; |
| 2518 | struct sk_buff *skb; |
| 2519 | int i; |
| 2520 | |
| 2521 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 2522 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 2523 | |
| 2524 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 2525 | return -EMFILE; |
| 2526 | } |
| 2527 | |
| 2528 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 2529 | if (!fpl) |
| 2530 | return -ENOMEM; |
| 2531 | |
| 2532 | skb = alloc_skb(0, GFP_KERNEL); |
| 2533 | if (!skb) { |
| 2534 | kfree(fpl); |
| 2535 | return -ENOMEM; |
| 2536 | } |
| 2537 | |
| 2538 | skb->sk = sk; |
| 2539 | skb->destructor = io_destruct_skb; |
| 2540 | |
| 2541 | fpl->user = get_uid(ctx->user); |
| 2542 | for (i = 0; i < nr; i++) { |
| 2543 | fpl->fp[i] = get_file(ctx->user_files[i + offset]); |
| 2544 | unix_inflight(fpl->user, fpl->fp[i]); |
| 2545 | } |
| 2546 | |
| 2547 | fpl->max = fpl->count = nr; |
| 2548 | UNIXCB(skb).fp = fpl; |
| 2549 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 2550 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 2551 | |
| 2552 | for (i = 0; i < nr; i++) |
| 2553 | fput(fpl->fp[i]); |
| 2554 | |
| 2555 | return 0; |
| 2556 | } |
| 2557 | |
| 2558 | /* |
| 2559 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 2560 | * causes regular reference counting to break down. We rely on the UNIX |
| 2561 | * garbage collection to take care of this problem for us. |
| 2562 | */ |
| 2563 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 2564 | { |
| 2565 | unsigned left, total; |
| 2566 | int ret = 0; |
| 2567 | |
| 2568 | total = 0; |
| 2569 | left = ctx->nr_user_files; |
| 2570 | while (left) { |
| 2571 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2572 | |
| 2573 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 2574 | if (ret) |
| 2575 | break; |
| 2576 | left -= this_files; |
| 2577 | total += this_files; |
| 2578 | } |
| 2579 | |
| 2580 | if (!ret) |
| 2581 | return 0; |
| 2582 | |
| 2583 | while (total < ctx->nr_user_files) { |
| 2584 | fput(ctx->user_files[total]); |
| 2585 | total++; |
| 2586 | } |
| 2587 | |
| 2588 | return ret; |
| 2589 | } |
| 2590 | #else |
| 2591 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 2592 | { |
| 2593 | return 0; |
| 2594 | } |
| 2595 | #endif |
| 2596 | |
| 2597 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 2598 | unsigned nr_args) |
| 2599 | { |
| 2600 | __s32 __user *fds = (__s32 __user *) arg; |
| 2601 | int fd, ret = 0; |
| 2602 | unsigned i; |
| 2603 | |
| 2604 | if (ctx->user_files) |
| 2605 | return -EBUSY; |
| 2606 | if (!nr_args) |
| 2607 | return -EINVAL; |
| 2608 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 2609 | return -EMFILE; |
| 2610 | |
| 2611 | ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL); |
| 2612 | if (!ctx->user_files) |
| 2613 | return -ENOMEM; |
| 2614 | |
| 2615 | for (i = 0; i < nr_args; i++) { |
| 2616 | ret = -EFAULT; |
| 2617 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 2618 | break; |
| 2619 | |
| 2620 | ctx->user_files[i] = fget(fd); |
| 2621 | |
| 2622 | ret = -EBADF; |
| 2623 | if (!ctx->user_files[i]) |
| 2624 | break; |
| 2625 | /* |
| 2626 | * Don't allow io_uring instances to be registered. If UNIX |
| 2627 | * isn't enabled, then this causes a reference cycle and this |
| 2628 | * instance can never get freed. If UNIX is enabled we'll |
| 2629 | * handle it just fine, but there's still no point in allowing |
| 2630 | * a ring fd as it doesn't support regular read/write anyway. |
| 2631 | */ |
| 2632 | if (ctx->user_files[i]->f_op == &io_uring_fops) { |
| 2633 | fput(ctx->user_files[i]); |
| 2634 | break; |
| 2635 | } |
| 2636 | ctx->nr_user_files++; |
| 2637 | ret = 0; |
| 2638 | } |
| 2639 | |
| 2640 | if (ret) { |
| 2641 | for (i = 0; i < ctx->nr_user_files; i++) |
| 2642 | fput(ctx->user_files[i]); |
| 2643 | |
| 2644 | kfree(ctx->user_files); |
Jens Axboe | 25adf50 | 2019-04-03 09:52:40 -0600 | [diff] [blame] | 2645 | ctx->user_files = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2646 | ctx->nr_user_files = 0; |
| 2647 | return ret; |
| 2648 | } |
| 2649 | |
| 2650 | ret = io_sqe_files_scm(ctx); |
| 2651 | if (ret) |
| 2652 | io_sqe_files_unregister(ctx); |
| 2653 | |
| 2654 | return ret; |
| 2655 | } |
| 2656 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2657 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 2658 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2659 | { |
| 2660 | int ret; |
| 2661 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2662 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2663 | mmgrab(current->mm); |
| 2664 | ctx->sqo_mm = current->mm; |
| 2665 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2666 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 2667 | ret = -EPERM; |
| 2668 | if (!capable(CAP_SYS_ADMIN)) |
| 2669 | goto err; |
| 2670 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 2671 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 2672 | if (!ctx->sq_thread_idle) |
| 2673 | ctx->sq_thread_idle = HZ; |
| 2674 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2675 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 2676 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2677 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 2678 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 2679 | if (cpu >= nr_cpu_ids) |
| 2680 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 2681 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 2682 | goto err; |
| 2683 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2684 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 2685 | ctx, cpu, |
| 2686 | "io_uring-sq"); |
| 2687 | } else { |
| 2688 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 2689 | "io_uring-sq"); |
| 2690 | } |
| 2691 | if (IS_ERR(ctx->sqo_thread)) { |
| 2692 | ret = PTR_ERR(ctx->sqo_thread); |
| 2693 | ctx->sqo_thread = NULL; |
| 2694 | goto err; |
| 2695 | } |
| 2696 | wake_up_process(ctx->sqo_thread); |
| 2697 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 2698 | /* Can't have SQ_AFF without SQPOLL */ |
| 2699 | ret = -EINVAL; |
| 2700 | goto err; |
| 2701 | } |
| 2702 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2703 | /* Do QD, or 2 * CPUS, whatever is smallest */ |
| 2704 | ctx->sqo_wq = alloc_workqueue("io_ring-wq", WQ_UNBOUND | WQ_FREEZABLE, |
| 2705 | min(ctx->sq_entries - 1, 2 * num_online_cpus())); |
| 2706 | if (!ctx->sqo_wq) { |
| 2707 | ret = -ENOMEM; |
| 2708 | goto err; |
| 2709 | } |
| 2710 | |
| 2711 | return 0; |
| 2712 | err: |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 2713 | io_sq_thread_stop(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2714 | mmdrop(ctx->sqo_mm); |
| 2715 | ctx->sqo_mm = NULL; |
| 2716 | return ret; |
| 2717 | } |
| 2718 | |
| 2719 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 2720 | { |
| 2721 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 2722 | } |
| 2723 | |
| 2724 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 2725 | { |
| 2726 | unsigned long page_limit, cur_pages, new_pages; |
| 2727 | |
| 2728 | /* Don't allow more pages than we can safely lock */ |
| 2729 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 2730 | |
| 2731 | do { |
| 2732 | cur_pages = atomic_long_read(&user->locked_vm); |
| 2733 | new_pages = cur_pages + nr_pages; |
| 2734 | if (new_pages > page_limit) |
| 2735 | return -ENOMEM; |
| 2736 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 2737 | new_pages) != cur_pages); |
| 2738 | |
| 2739 | return 0; |
| 2740 | } |
| 2741 | |
| 2742 | static void io_mem_free(void *ptr) |
| 2743 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 2744 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2745 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 2746 | if (!ptr) |
| 2747 | return; |
| 2748 | |
| 2749 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2750 | if (put_page_testzero(page)) |
| 2751 | free_compound_page(page); |
| 2752 | } |
| 2753 | |
| 2754 | static void *io_mem_alloc(size_t size) |
| 2755 | { |
| 2756 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 2757 | __GFP_NORETRY; |
| 2758 | |
| 2759 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 2760 | } |
| 2761 | |
| 2762 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 2763 | { |
| 2764 | struct io_sq_ring *sq_ring; |
| 2765 | struct io_cq_ring *cq_ring; |
| 2766 | size_t bytes; |
| 2767 | |
| 2768 | bytes = struct_size(sq_ring, array, sq_entries); |
| 2769 | bytes += array_size(sizeof(struct io_uring_sqe), sq_entries); |
| 2770 | bytes += struct_size(cq_ring, cqes, cq_entries); |
| 2771 | |
| 2772 | return (bytes + PAGE_SIZE - 1) / PAGE_SIZE; |
| 2773 | } |
| 2774 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2775 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 2776 | { |
| 2777 | int i, j; |
| 2778 | |
| 2779 | if (!ctx->user_bufs) |
| 2780 | return -ENXIO; |
| 2781 | |
| 2782 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 2783 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 2784 | |
| 2785 | for (j = 0; j < imu->nr_bvecs; j++) |
| 2786 | put_page(imu->bvec[j].bv_page); |
| 2787 | |
| 2788 | if (ctx->account_mem) |
| 2789 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 2790 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2791 | imu->nr_bvecs = 0; |
| 2792 | } |
| 2793 | |
| 2794 | kfree(ctx->user_bufs); |
| 2795 | ctx->user_bufs = NULL; |
| 2796 | ctx->nr_user_bufs = 0; |
| 2797 | return 0; |
| 2798 | } |
| 2799 | |
| 2800 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 2801 | void __user *arg, unsigned index) |
| 2802 | { |
| 2803 | struct iovec __user *src; |
| 2804 | |
| 2805 | #ifdef CONFIG_COMPAT |
| 2806 | if (ctx->compat) { |
| 2807 | struct compat_iovec __user *ciovs; |
| 2808 | struct compat_iovec ciov; |
| 2809 | |
| 2810 | ciovs = (struct compat_iovec __user *) arg; |
| 2811 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 2812 | return -EFAULT; |
| 2813 | |
| 2814 | dst->iov_base = (void __user *) (unsigned long) ciov.iov_base; |
| 2815 | dst->iov_len = ciov.iov_len; |
| 2816 | return 0; |
| 2817 | } |
| 2818 | #endif |
| 2819 | src = (struct iovec __user *) arg; |
| 2820 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 2821 | return -EFAULT; |
| 2822 | return 0; |
| 2823 | } |
| 2824 | |
| 2825 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 2826 | unsigned nr_args) |
| 2827 | { |
| 2828 | struct vm_area_struct **vmas = NULL; |
| 2829 | struct page **pages = NULL; |
| 2830 | int i, j, got_pages = 0; |
| 2831 | int ret = -EINVAL; |
| 2832 | |
| 2833 | if (ctx->user_bufs) |
| 2834 | return -EBUSY; |
| 2835 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 2836 | return -EINVAL; |
| 2837 | |
| 2838 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 2839 | GFP_KERNEL); |
| 2840 | if (!ctx->user_bufs) |
| 2841 | return -ENOMEM; |
| 2842 | |
| 2843 | for (i = 0; i < nr_args; i++) { |
| 2844 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 2845 | unsigned long off, start, end, ubuf; |
| 2846 | int pret, nr_pages; |
| 2847 | struct iovec iov; |
| 2848 | size_t size; |
| 2849 | |
| 2850 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 2851 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 2852 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2853 | |
| 2854 | /* |
| 2855 | * Don't impose further limits on the size and buffer |
| 2856 | * constraints here, we'll -EINVAL later when IO is |
| 2857 | * submitted if they are wrong. |
| 2858 | */ |
| 2859 | ret = -EFAULT; |
| 2860 | if (!iov.iov_base || !iov.iov_len) |
| 2861 | goto err; |
| 2862 | |
| 2863 | /* arbitrary limit, but we need something */ |
| 2864 | if (iov.iov_len > SZ_1G) |
| 2865 | goto err; |
| 2866 | |
| 2867 | ubuf = (unsigned long) iov.iov_base; |
| 2868 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 2869 | start = ubuf >> PAGE_SHIFT; |
| 2870 | nr_pages = end - start; |
| 2871 | |
| 2872 | if (ctx->account_mem) { |
| 2873 | ret = io_account_mem(ctx->user, nr_pages); |
| 2874 | if (ret) |
| 2875 | goto err; |
| 2876 | } |
| 2877 | |
| 2878 | ret = 0; |
| 2879 | if (!pages || nr_pages > got_pages) { |
| 2880 | kfree(vmas); |
| 2881 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 2882 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2883 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 2884 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2885 | sizeof(struct vm_area_struct *), |
| 2886 | GFP_KERNEL); |
| 2887 | if (!pages || !vmas) { |
| 2888 | ret = -ENOMEM; |
| 2889 | if (ctx->account_mem) |
| 2890 | io_unaccount_mem(ctx->user, nr_pages); |
| 2891 | goto err; |
| 2892 | } |
| 2893 | got_pages = nr_pages; |
| 2894 | } |
| 2895 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 2896 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2897 | GFP_KERNEL); |
| 2898 | ret = -ENOMEM; |
| 2899 | if (!imu->bvec) { |
| 2900 | if (ctx->account_mem) |
| 2901 | io_unaccount_mem(ctx->user, nr_pages); |
| 2902 | goto err; |
| 2903 | } |
| 2904 | |
| 2905 | ret = 0; |
| 2906 | down_read(¤t->mm->mmap_sem); |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 2907 | pret = get_user_pages(ubuf, nr_pages, |
| 2908 | FOLL_WRITE | FOLL_LONGTERM, |
| 2909 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2910 | if (pret == nr_pages) { |
| 2911 | /* don't support file backed memory */ |
| 2912 | for (j = 0; j < nr_pages; j++) { |
| 2913 | struct vm_area_struct *vma = vmas[j]; |
| 2914 | |
| 2915 | if (vma->vm_file && |
| 2916 | !is_file_hugepages(vma->vm_file)) { |
| 2917 | ret = -EOPNOTSUPP; |
| 2918 | break; |
| 2919 | } |
| 2920 | } |
| 2921 | } else { |
| 2922 | ret = pret < 0 ? pret : -EFAULT; |
| 2923 | } |
| 2924 | up_read(¤t->mm->mmap_sem); |
| 2925 | if (ret) { |
| 2926 | /* |
| 2927 | * if we did partial map, or found file backed vmas, |
| 2928 | * release any pages we did get |
| 2929 | */ |
| 2930 | if (pret > 0) { |
| 2931 | for (j = 0; j < pret; j++) |
| 2932 | put_page(pages[j]); |
| 2933 | } |
| 2934 | if (ctx->account_mem) |
| 2935 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 2936 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2937 | goto err; |
| 2938 | } |
| 2939 | |
| 2940 | off = ubuf & ~PAGE_MASK; |
| 2941 | size = iov.iov_len; |
| 2942 | for (j = 0; j < nr_pages; j++) { |
| 2943 | size_t vec_len; |
| 2944 | |
| 2945 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 2946 | imu->bvec[j].bv_page = pages[j]; |
| 2947 | imu->bvec[j].bv_len = vec_len; |
| 2948 | imu->bvec[j].bv_offset = off; |
| 2949 | off = 0; |
| 2950 | size -= vec_len; |
| 2951 | } |
| 2952 | /* store original address for later verification */ |
| 2953 | imu->ubuf = ubuf; |
| 2954 | imu->len = iov.iov_len; |
| 2955 | imu->nr_bvecs = nr_pages; |
| 2956 | |
| 2957 | ctx->nr_user_bufs++; |
| 2958 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 2959 | kvfree(pages); |
| 2960 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2961 | return 0; |
| 2962 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 2963 | kvfree(pages); |
| 2964 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2965 | io_sqe_buffer_unregister(ctx); |
| 2966 | return ret; |
| 2967 | } |
| 2968 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 2969 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 2970 | { |
| 2971 | __s32 __user *fds = arg; |
| 2972 | int fd; |
| 2973 | |
| 2974 | if (ctx->cq_ev_fd) |
| 2975 | return -EBUSY; |
| 2976 | |
| 2977 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 2978 | return -EFAULT; |
| 2979 | |
| 2980 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 2981 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 2982 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 2983 | ctx->cq_ev_fd = NULL; |
| 2984 | return ret; |
| 2985 | } |
| 2986 | |
| 2987 | return 0; |
| 2988 | } |
| 2989 | |
| 2990 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 2991 | { |
| 2992 | if (ctx->cq_ev_fd) { |
| 2993 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 2994 | ctx->cq_ev_fd = NULL; |
| 2995 | return 0; |
| 2996 | } |
| 2997 | |
| 2998 | return -ENXIO; |
| 2999 | } |
| 3000 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3001 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 3002 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3003 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3004 | if (ctx->sqo_mm) |
| 3005 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3006 | |
| 3007 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3008 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3009 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 3010 | io_eventfd_unregister(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3011 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3012 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 3013 | if (ctx->ring_sock) { |
| 3014 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3015 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 3016 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3017 | #endif |
| 3018 | |
| 3019 | io_mem_free(ctx->sq_ring); |
| 3020 | io_mem_free(ctx->sq_sqes); |
| 3021 | io_mem_free(ctx->cq_ring); |
| 3022 | |
| 3023 | percpu_ref_exit(&ctx->refs); |
| 3024 | if (ctx->account_mem) |
| 3025 | io_unaccount_mem(ctx->user, |
| 3026 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 3027 | free_uid(ctx->user); |
| 3028 | kfree(ctx); |
| 3029 | } |
| 3030 | |
| 3031 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 3032 | { |
| 3033 | struct io_ring_ctx *ctx = file->private_data; |
| 3034 | __poll_t mask = 0; |
| 3035 | |
| 3036 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 3037 | /* |
| 3038 | * synchronizes with barrier from wq_has_sleeper call in |
| 3039 | * io_commit_cqring |
| 3040 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3041 | smp_rmb(); |
Stefan Bühler | fb775fa | 2019-04-19 11:57:46 +0200 | [diff] [blame] | 3042 | if (READ_ONCE(ctx->sq_ring->r.tail) - ctx->cached_sq_head != |
| 3043 | ctx->sq_ring->ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3044 | mask |= EPOLLOUT | EPOLLWRNORM; |
| 3045 | if (READ_ONCE(ctx->cq_ring->r.head) != ctx->cached_cq_tail) |
| 3046 | mask |= EPOLLIN | EPOLLRDNORM; |
| 3047 | |
| 3048 | return mask; |
| 3049 | } |
| 3050 | |
| 3051 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 3052 | { |
| 3053 | struct io_ring_ctx *ctx = file->private_data; |
| 3054 | |
| 3055 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 3056 | } |
| 3057 | |
| 3058 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 3059 | { |
| 3060 | mutex_lock(&ctx->uring_lock); |
| 3061 | percpu_ref_kill(&ctx->refs); |
| 3062 | mutex_unlock(&ctx->uring_lock); |
| 3063 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3064 | io_poll_remove_all(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3065 | io_iopoll_reap_events(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3066 | wait_for_completion(&ctx->ctx_done); |
| 3067 | io_ring_ctx_free(ctx); |
| 3068 | } |
| 3069 | |
| 3070 | static int io_uring_release(struct inode *inode, struct file *file) |
| 3071 | { |
| 3072 | struct io_ring_ctx *ctx = file->private_data; |
| 3073 | |
| 3074 | file->private_data = NULL; |
| 3075 | io_ring_ctx_wait_and_kill(ctx); |
| 3076 | return 0; |
| 3077 | } |
| 3078 | |
| 3079 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 3080 | { |
| 3081 | loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT; |
| 3082 | unsigned long sz = vma->vm_end - vma->vm_start; |
| 3083 | struct io_ring_ctx *ctx = file->private_data; |
| 3084 | unsigned long pfn; |
| 3085 | struct page *page; |
| 3086 | void *ptr; |
| 3087 | |
| 3088 | switch (offset) { |
| 3089 | case IORING_OFF_SQ_RING: |
| 3090 | ptr = ctx->sq_ring; |
| 3091 | break; |
| 3092 | case IORING_OFF_SQES: |
| 3093 | ptr = ctx->sq_sqes; |
| 3094 | break; |
| 3095 | case IORING_OFF_CQ_RING: |
| 3096 | ptr = ctx->cq_ring; |
| 3097 | break; |
| 3098 | default: |
| 3099 | return -EINVAL; |
| 3100 | } |
| 3101 | |
| 3102 | page = virt_to_head_page(ptr); |
| 3103 | if (sz > (PAGE_SIZE << compound_order(page))) |
| 3104 | return -EINVAL; |
| 3105 | |
| 3106 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 3107 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 3108 | } |
| 3109 | |
| 3110 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 3111 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 3112 | size_t, sigsz) |
| 3113 | { |
| 3114 | struct io_ring_ctx *ctx; |
| 3115 | long ret = -EBADF; |
| 3116 | int submitted = 0; |
| 3117 | struct fd f; |
| 3118 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3119 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3120 | return -EINVAL; |
| 3121 | |
| 3122 | f = fdget(fd); |
| 3123 | if (!f.file) |
| 3124 | return -EBADF; |
| 3125 | |
| 3126 | ret = -EOPNOTSUPP; |
| 3127 | if (f.file->f_op != &io_uring_fops) |
| 3128 | goto out_fput; |
| 3129 | |
| 3130 | ret = -ENXIO; |
| 3131 | ctx = f.file->private_data; |
| 3132 | if (!percpu_ref_tryget(&ctx->refs)) |
| 3133 | goto out_fput; |
| 3134 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3135 | /* |
| 3136 | * For SQ polling, the thread will do all submissions and completions. |
| 3137 | * Just return the requested submit count, and wake the thread if |
| 3138 | * we were asked to. |
| 3139 | */ |
| 3140 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 3141 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 3142 | wake_up(&ctx->sqo_wait); |
| 3143 | submitted = to_submit; |
| 3144 | goto out_ctx; |
| 3145 | } |
| 3146 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3147 | ret = 0; |
| 3148 | if (to_submit) { |
| 3149 | to_submit = min(to_submit, ctx->sq_entries); |
| 3150 | |
| 3151 | mutex_lock(&ctx->uring_lock); |
| 3152 | submitted = io_ring_submit(ctx, to_submit); |
| 3153 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3154 | } |
| 3155 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3156 | unsigned nr_events = 0; |
| 3157 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3158 | min_complete = min(min_complete, ctx->cq_entries); |
| 3159 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3160 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
| 3161 | mutex_lock(&ctx->uring_lock); |
| 3162 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
| 3163 | mutex_unlock(&ctx->uring_lock); |
| 3164 | } else { |
| 3165 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 3166 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | out_ctx: |
| 3170 | io_ring_drop_ctx_refs(ctx, 1); |
| 3171 | out_fput: |
| 3172 | fdput(f); |
| 3173 | return submitted ? submitted : ret; |
| 3174 | } |
| 3175 | |
| 3176 | static const struct file_operations io_uring_fops = { |
| 3177 | .release = io_uring_release, |
| 3178 | .mmap = io_uring_mmap, |
| 3179 | .poll = io_uring_poll, |
| 3180 | .fasync = io_uring_fasync, |
| 3181 | }; |
| 3182 | |
| 3183 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 3184 | struct io_uring_params *p) |
| 3185 | { |
| 3186 | struct io_sq_ring *sq_ring; |
| 3187 | struct io_cq_ring *cq_ring; |
| 3188 | size_t size; |
| 3189 | |
| 3190 | sq_ring = io_mem_alloc(struct_size(sq_ring, array, p->sq_entries)); |
| 3191 | if (!sq_ring) |
| 3192 | return -ENOMEM; |
| 3193 | |
| 3194 | ctx->sq_ring = sq_ring; |
| 3195 | sq_ring->ring_mask = p->sq_entries - 1; |
| 3196 | sq_ring->ring_entries = p->sq_entries; |
| 3197 | ctx->sq_mask = sq_ring->ring_mask; |
| 3198 | ctx->sq_entries = sq_ring->ring_entries; |
| 3199 | |
| 3200 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
| 3201 | if (size == SIZE_MAX) |
| 3202 | return -EOVERFLOW; |
| 3203 | |
| 3204 | ctx->sq_sqes = io_mem_alloc(size); |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 3205 | if (!ctx->sq_sqes) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3206 | return -ENOMEM; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3207 | |
| 3208 | cq_ring = io_mem_alloc(struct_size(cq_ring, cqes, p->cq_entries)); |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 3209 | if (!cq_ring) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3210 | return -ENOMEM; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3211 | |
| 3212 | ctx->cq_ring = cq_ring; |
| 3213 | cq_ring->ring_mask = p->cq_entries - 1; |
| 3214 | cq_ring->ring_entries = p->cq_entries; |
| 3215 | ctx->cq_mask = cq_ring->ring_mask; |
| 3216 | ctx->cq_entries = cq_ring->ring_entries; |
| 3217 | return 0; |
| 3218 | } |
| 3219 | |
| 3220 | /* |
| 3221 | * Allocate an anonymous fd, this is what constitutes the application |
| 3222 | * visible backing of an io_uring instance. The application mmaps this |
| 3223 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 3224 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 3225 | */ |
| 3226 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 3227 | { |
| 3228 | struct file *file; |
| 3229 | int ret; |
| 3230 | |
| 3231 | #if defined(CONFIG_UNIX) |
| 3232 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 3233 | &ctx->ring_sock); |
| 3234 | if (ret) |
| 3235 | return ret; |
| 3236 | #endif |
| 3237 | |
| 3238 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 3239 | if (ret < 0) |
| 3240 | goto err; |
| 3241 | |
| 3242 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 3243 | O_RDWR | O_CLOEXEC); |
| 3244 | if (IS_ERR(file)) { |
| 3245 | put_unused_fd(ret); |
| 3246 | ret = PTR_ERR(file); |
| 3247 | goto err; |
| 3248 | } |
| 3249 | |
| 3250 | #if defined(CONFIG_UNIX) |
| 3251 | ctx->ring_sock->file = file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3252 | ctx->ring_sock->sk->sk_user_data = ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3253 | #endif |
| 3254 | fd_install(ret, file); |
| 3255 | return ret; |
| 3256 | err: |
| 3257 | #if defined(CONFIG_UNIX) |
| 3258 | sock_release(ctx->ring_sock); |
| 3259 | ctx->ring_sock = NULL; |
| 3260 | #endif |
| 3261 | return ret; |
| 3262 | } |
| 3263 | |
| 3264 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 3265 | { |
| 3266 | struct user_struct *user = NULL; |
| 3267 | struct io_ring_ctx *ctx; |
| 3268 | bool account_mem; |
| 3269 | int ret; |
| 3270 | |
| 3271 | if (!entries || entries > IORING_MAX_ENTRIES) |
| 3272 | return -EINVAL; |
| 3273 | |
| 3274 | /* |
| 3275 | * Use twice as many entries for the CQ ring. It's possible for the |
| 3276 | * application to drive a higher depth than the size of the SQ ring, |
| 3277 | * since the sqes are only used at submission time. This allows for |
| 3278 | * some flexibility in overcommitting a bit. |
| 3279 | */ |
| 3280 | p->sq_entries = roundup_pow_of_two(entries); |
| 3281 | p->cq_entries = 2 * p->sq_entries; |
| 3282 | |
| 3283 | user = get_uid(current_user()); |
| 3284 | account_mem = !capable(CAP_IPC_LOCK); |
| 3285 | |
| 3286 | if (account_mem) { |
| 3287 | ret = io_account_mem(user, |
| 3288 | ring_pages(p->sq_entries, p->cq_entries)); |
| 3289 | if (ret) { |
| 3290 | free_uid(user); |
| 3291 | return ret; |
| 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | ctx = io_ring_ctx_alloc(p); |
| 3296 | if (!ctx) { |
| 3297 | if (account_mem) |
| 3298 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 3299 | p->cq_entries)); |
| 3300 | free_uid(user); |
| 3301 | return -ENOMEM; |
| 3302 | } |
| 3303 | ctx->compat = in_compat_syscall(); |
| 3304 | ctx->account_mem = account_mem; |
| 3305 | ctx->user = user; |
| 3306 | |
| 3307 | ret = io_allocate_scq_urings(ctx, p); |
| 3308 | if (ret) |
| 3309 | goto err; |
| 3310 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3311 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3312 | if (ret) |
| 3313 | goto err; |
| 3314 | |
| 3315 | ret = io_uring_get_fd(ctx); |
| 3316 | if (ret < 0) |
| 3317 | goto err; |
| 3318 | |
| 3319 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
| 3320 | p->sq_off.head = offsetof(struct io_sq_ring, r.head); |
| 3321 | p->sq_off.tail = offsetof(struct io_sq_ring, r.tail); |
| 3322 | p->sq_off.ring_mask = offsetof(struct io_sq_ring, ring_mask); |
| 3323 | p->sq_off.ring_entries = offsetof(struct io_sq_ring, ring_entries); |
| 3324 | p->sq_off.flags = offsetof(struct io_sq_ring, flags); |
| 3325 | p->sq_off.dropped = offsetof(struct io_sq_ring, dropped); |
| 3326 | p->sq_off.array = offsetof(struct io_sq_ring, array); |
| 3327 | |
| 3328 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
| 3329 | p->cq_off.head = offsetof(struct io_cq_ring, r.head); |
| 3330 | p->cq_off.tail = offsetof(struct io_cq_ring, r.tail); |
| 3331 | p->cq_off.ring_mask = offsetof(struct io_cq_ring, ring_mask); |
| 3332 | p->cq_off.ring_entries = offsetof(struct io_cq_ring, ring_entries); |
| 3333 | p->cq_off.overflow = offsetof(struct io_cq_ring, overflow); |
| 3334 | p->cq_off.cqes = offsetof(struct io_cq_ring, cqes); |
| 3335 | return ret; |
| 3336 | err: |
| 3337 | io_ring_ctx_wait_and_kill(ctx); |
| 3338 | return ret; |
| 3339 | } |
| 3340 | |
| 3341 | /* |
| 3342 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 3343 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 3344 | * params structure passed in. |
| 3345 | */ |
| 3346 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 3347 | { |
| 3348 | struct io_uring_params p; |
| 3349 | long ret; |
| 3350 | int i; |
| 3351 | |
| 3352 | if (copy_from_user(&p, params, sizeof(p))) |
| 3353 | return -EFAULT; |
| 3354 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 3355 | if (p.resv[i]) |
| 3356 | return -EINVAL; |
| 3357 | } |
| 3358 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 3359 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
| 3360 | IORING_SETUP_SQ_AFF)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3361 | return -EINVAL; |
| 3362 | |
| 3363 | ret = io_uring_create(entries, &p); |
| 3364 | if (ret < 0) |
| 3365 | return ret; |
| 3366 | |
| 3367 | if (copy_to_user(params, &p, sizeof(p))) |
| 3368 | return -EFAULT; |
| 3369 | |
| 3370 | return ret; |
| 3371 | } |
| 3372 | |
| 3373 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 3374 | struct io_uring_params __user *, params) |
| 3375 | { |
| 3376 | return io_uring_setup(entries, params); |
| 3377 | } |
| 3378 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3379 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 3380 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 3381 | __releases(ctx->uring_lock) |
| 3382 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3383 | { |
| 3384 | int ret; |
| 3385 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 3386 | /* |
| 3387 | * We're inside the ring mutex, if the ref is already dying, then |
| 3388 | * someone else killed the ctx or is already going through |
| 3389 | * io_uring_register(). |
| 3390 | */ |
| 3391 | if (percpu_ref_is_dying(&ctx->refs)) |
| 3392 | return -ENXIO; |
| 3393 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3394 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 3395 | |
| 3396 | /* |
| 3397 | * Drop uring mutex before waiting for references to exit. If another |
| 3398 | * thread is currently inside io_uring_enter() it might need to grab |
| 3399 | * the uring_lock to make progress. If we hold it here across the drain |
| 3400 | * wait, then we can deadlock. It's safe to drop the mutex here, since |
| 3401 | * no new references will come in after we've killed the percpu ref. |
| 3402 | */ |
| 3403 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3404 | wait_for_completion(&ctx->ctx_done); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 3405 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3406 | |
| 3407 | switch (opcode) { |
| 3408 | case IORING_REGISTER_BUFFERS: |
| 3409 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 3410 | break; |
| 3411 | case IORING_UNREGISTER_BUFFERS: |
| 3412 | ret = -EINVAL; |
| 3413 | if (arg || nr_args) |
| 3414 | break; |
| 3415 | ret = io_sqe_buffer_unregister(ctx); |
| 3416 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 3417 | case IORING_REGISTER_FILES: |
| 3418 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 3419 | break; |
| 3420 | case IORING_UNREGISTER_FILES: |
| 3421 | ret = -EINVAL; |
| 3422 | if (arg || nr_args) |
| 3423 | break; |
| 3424 | ret = io_sqe_files_unregister(ctx); |
| 3425 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 3426 | case IORING_REGISTER_EVENTFD: |
| 3427 | ret = -EINVAL; |
| 3428 | if (nr_args != 1) |
| 3429 | break; |
| 3430 | ret = io_eventfd_register(ctx, arg); |
| 3431 | break; |
| 3432 | case IORING_UNREGISTER_EVENTFD: |
| 3433 | ret = -EINVAL; |
| 3434 | if (arg || nr_args) |
| 3435 | break; |
| 3436 | ret = io_eventfd_unregister(ctx); |
| 3437 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3438 | default: |
| 3439 | ret = -EINVAL; |
| 3440 | break; |
| 3441 | } |
| 3442 | |
| 3443 | /* bring the ctx back to life */ |
| 3444 | reinit_completion(&ctx->ctx_done); |
| 3445 | percpu_ref_reinit(&ctx->refs); |
| 3446 | return ret; |
| 3447 | } |
| 3448 | |
| 3449 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 3450 | void __user *, arg, unsigned int, nr_args) |
| 3451 | { |
| 3452 | struct io_ring_ctx *ctx; |
| 3453 | long ret = -EBADF; |
| 3454 | struct fd f; |
| 3455 | |
| 3456 | f = fdget(fd); |
| 3457 | if (!f.file) |
| 3458 | return -EBADF; |
| 3459 | |
| 3460 | ret = -EOPNOTSUPP; |
| 3461 | if (f.file->f_op != &io_uring_fops) |
| 3462 | goto out_fput; |
| 3463 | |
| 3464 | ctx = f.file->private_data; |
| 3465 | |
| 3466 | mutex_lock(&ctx->uring_lock); |
| 3467 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 3468 | mutex_unlock(&ctx->uring_lock); |
| 3469 | out_fput: |
| 3470 | fdput(f); |
| 3471 | return ret; |
| 3472 | } |
| 3473 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3474 | static int __init io_uring_init(void) |
| 3475 | { |
| 3476 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 3477 | return 0; |
| 3478 | }; |
| 3479 | __initcall(io_uring_init); |