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