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