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