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