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