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