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