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