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