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