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