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