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