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