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