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