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