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 | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 73 | #include <linux/namei.h> |
| 74 | #include <linux/fsnotify.h> |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame^] | 75 | #include <linux/fadvise.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 76 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 77 | #define CREATE_TRACE_POINTS |
| 78 | #include <trace/events/io_uring.h> |
| 79 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 80 | #include <uapi/linux/io_uring.h> |
| 81 | |
| 82 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 83 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 84 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 85 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 86 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 90 | */ |
| 91 | #define IORING_FILE_TABLE_SHIFT 9 |
| 92 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 93 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 94 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 95 | |
| 96 | struct io_uring { |
| 97 | u32 head ____cacheline_aligned_in_smp; |
| 98 | u32 tail ____cacheline_aligned_in_smp; |
| 99 | }; |
| 100 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 101 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 102 | * This data is shared with the application through the mmap at offsets |
| 103 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 104 | * |
| 105 | * The offsets to the member fields are published through struct |
| 106 | * io_sqring_offsets when calling io_uring_setup. |
| 107 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 108 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 109 | /* |
| 110 | * Head and tail offsets into the ring; the offsets need to be |
| 111 | * masked to get valid indices. |
| 112 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 113 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 114 | * and the application controls tail of the sq ring and the head of the |
| 115 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 116 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 117 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 118 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 119 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 120 | * ring_entries - 1) |
| 121 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 122 | u32 sq_ring_mask, cq_ring_mask; |
| 123 | /* Ring sizes (constant, power of 2) */ |
| 124 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 125 | /* |
| 126 | * Number of invalid entries dropped by the kernel due to |
| 127 | * invalid index stored in array |
| 128 | * |
| 129 | * Written by the kernel, shouldn't be modified by the |
| 130 | * application (i.e. get number of "new events" by comparing to |
| 131 | * cached value). |
| 132 | * |
| 133 | * After a new SQ head value was read by the application this |
| 134 | * counter includes all submissions that were dropped reaching |
| 135 | * the new SQ head (and possibly more). |
| 136 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 137 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 138 | /* |
| 139 | * Runtime flags |
| 140 | * |
| 141 | * Written by the kernel, shouldn't be modified by the |
| 142 | * application. |
| 143 | * |
| 144 | * The application needs a full memory barrier before checking |
| 145 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 146 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 147 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 148 | /* |
| 149 | * Number of completion events lost because the queue was full; |
| 150 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 151 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 152 | * the completion queue. |
| 153 | * |
| 154 | * Written by the kernel, shouldn't be modified by the |
| 155 | * application (i.e. get number of "new events" by comparing to |
| 156 | * cached value). |
| 157 | * |
| 158 | * As completion events come in out of order this counter is not |
| 159 | * ordered with any other data. |
| 160 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 161 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 162 | /* |
| 163 | * Ring buffer of completion events. |
| 164 | * |
| 165 | * The kernel writes completion events fresh every time they are |
| 166 | * produced, so the application is allowed to modify pending |
| 167 | * entries. |
| 168 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 169 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 172 | struct io_mapped_ubuf { |
| 173 | u64 ubuf; |
| 174 | size_t len; |
| 175 | struct bio_vec *bvec; |
| 176 | unsigned int nr_bvecs; |
| 177 | }; |
| 178 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 179 | struct fixed_file_table { |
| 180 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 183 | enum { |
| 184 | FFD_F_ATOMIC, |
| 185 | }; |
| 186 | |
| 187 | struct fixed_file_data { |
| 188 | struct fixed_file_table *table; |
| 189 | struct io_ring_ctx *ctx; |
| 190 | |
| 191 | struct percpu_ref refs; |
| 192 | struct llist_head put_llist; |
| 193 | unsigned long state; |
| 194 | struct work_struct ref_work; |
| 195 | struct completion done; |
| 196 | }; |
| 197 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 198 | struct io_ring_ctx { |
| 199 | struct { |
| 200 | struct percpu_ref refs; |
| 201 | } ____cacheline_aligned_in_smp; |
| 202 | |
| 203 | struct { |
| 204 | unsigned int flags; |
| 205 | bool compat; |
| 206 | bool account_mem; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 207 | bool cq_overflow_flushed; |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 208 | bool drain_next; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 209 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 210 | /* |
| 211 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 212 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 213 | * |
| 214 | * This indirection could e.g. be used to assign fixed |
| 215 | * io_uring_sqe entries to operations and only submit them to |
| 216 | * the queue when needed. |
| 217 | * |
| 218 | * The kernel modifies neither the indices array nor the entries |
| 219 | * array. |
| 220 | */ |
| 221 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 222 | unsigned cached_sq_head; |
| 223 | unsigned sq_entries; |
| 224 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 225 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 226 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 227 | atomic_t cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 228 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 229 | |
| 230 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 231 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 232 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 233 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 234 | wait_queue_head_t inflight_wait; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 235 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 236 | } ____cacheline_aligned_in_smp; |
| 237 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 238 | struct io_rings *rings; |
| 239 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 240 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 241 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 242 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 243 | struct mm_struct *sqo_mm; |
| 244 | wait_queue_head_t sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 245 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 246 | /* |
| 247 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 248 | * readers must ensure that ->refs is alive as long as the file* is |
| 249 | * used. Only updated through io_uring_register(2). |
| 250 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 251 | struct fixed_file_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 252 | unsigned nr_user_files; |
| 253 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 254 | /* if used, fixed mapped user buffers */ |
| 255 | unsigned nr_user_bufs; |
| 256 | struct io_mapped_ubuf *user_bufs; |
| 257 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 258 | struct user_struct *user; |
| 259 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 260 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 261 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 262 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
| 263 | struct completion *completions; |
| 264 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 265 | /* if all else fails... */ |
| 266 | struct io_kiocb *fallback_req; |
| 267 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 268 | #if defined(CONFIG_UNIX) |
| 269 | struct socket *ring_sock; |
| 270 | #endif |
| 271 | |
| 272 | struct { |
| 273 | unsigned cached_cq_tail; |
| 274 | unsigned cq_entries; |
| 275 | unsigned cq_mask; |
| 276 | atomic_t cq_timeouts; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 277 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 278 | struct wait_queue_head cq_wait; |
| 279 | struct fasync_struct *cq_fasync; |
| 280 | struct eventfd_ctx *cq_ev_fd; |
| 281 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 282 | |
| 283 | struct { |
| 284 | struct mutex uring_lock; |
| 285 | wait_queue_head_t wait; |
| 286 | } ____cacheline_aligned_in_smp; |
| 287 | |
| 288 | struct { |
| 289 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 290 | struct llist_head poll_llist; |
| 291 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 292 | /* |
| 293 | * ->poll_list is protected by the ctx->uring_lock for |
| 294 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 295 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 296 | * manipulate the list, hence no extra locking is needed there. |
| 297 | */ |
| 298 | struct list_head poll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 299 | struct hlist_head *cancel_hash; |
| 300 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 301 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 302 | |
| 303 | spinlock_t inflight_lock; |
| 304 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 305 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 306 | }; |
| 307 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 308 | /* |
| 309 | * First field must be the file pointer in all the |
| 310 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 311 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 312 | struct io_poll_iocb { |
| 313 | struct file *file; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 314 | union { |
| 315 | struct wait_queue_head *head; |
| 316 | u64 addr; |
| 317 | }; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 318 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 319 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 320 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 321 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 322 | }; |
| 323 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 324 | struct io_close { |
| 325 | struct file *file; |
| 326 | struct file *put_file; |
| 327 | int fd; |
| 328 | }; |
| 329 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 330 | struct io_timeout_data { |
| 331 | struct io_kiocb *req; |
| 332 | struct hrtimer timer; |
| 333 | struct timespec64 ts; |
| 334 | enum hrtimer_mode mode; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 335 | u32 seq_offset; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 336 | }; |
| 337 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 338 | struct io_accept { |
| 339 | struct file *file; |
| 340 | struct sockaddr __user *addr; |
| 341 | int __user *addr_len; |
| 342 | int flags; |
| 343 | }; |
| 344 | |
| 345 | struct io_sync { |
| 346 | struct file *file; |
| 347 | loff_t len; |
| 348 | loff_t off; |
| 349 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 350 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 351 | }; |
| 352 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 353 | struct io_cancel { |
| 354 | struct file *file; |
| 355 | u64 addr; |
| 356 | }; |
| 357 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 358 | struct io_timeout { |
| 359 | struct file *file; |
| 360 | u64 addr; |
| 361 | int flags; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 362 | unsigned count; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 363 | }; |
| 364 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 365 | struct io_rw { |
| 366 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 367 | struct kiocb kiocb; |
| 368 | u64 addr; |
| 369 | u64 len; |
| 370 | }; |
| 371 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 372 | struct io_connect { |
| 373 | struct file *file; |
| 374 | struct sockaddr __user *addr; |
| 375 | int addr_len; |
| 376 | }; |
| 377 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 378 | struct io_sr_msg { |
| 379 | struct file *file; |
| 380 | struct user_msghdr __user *msg; |
| 381 | int msg_flags; |
| 382 | }; |
| 383 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 384 | struct io_open { |
| 385 | struct file *file; |
| 386 | int dfd; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 387 | union { |
| 388 | umode_t mode; |
| 389 | unsigned mask; |
| 390 | }; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 391 | const char __user *fname; |
| 392 | struct filename *filename; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 393 | struct statx __user *buffer; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 394 | int flags; |
| 395 | }; |
| 396 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 397 | struct io_files_update { |
| 398 | struct file *file; |
| 399 | u64 arg; |
| 400 | u32 nr_args; |
| 401 | u32 offset; |
| 402 | }; |
| 403 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame^] | 404 | struct io_fadvise { |
| 405 | struct file *file; |
| 406 | u64 offset; |
| 407 | u32 len; |
| 408 | u32 advice; |
| 409 | }; |
| 410 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 411 | struct io_async_connect { |
| 412 | struct sockaddr_storage address; |
| 413 | }; |
| 414 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 415 | struct io_async_msghdr { |
| 416 | struct iovec fast_iov[UIO_FASTIOV]; |
| 417 | struct iovec *iov; |
| 418 | struct sockaddr __user *uaddr; |
| 419 | struct msghdr msg; |
| 420 | }; |
| 421 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 422 | struct io_async_rw { |
| 423 | struct iovec fast_iov[UIO_FASTIOV]; |
| 424 | struct iovec *iov; |
| 425 | ssize_t nr_segs; |
| 426 | ssize_t size; |
| 427 | }; |
| 428 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 429 | struct io_async_open { |
| 430 | struct filename *filename; |
| 431 | }; |
| 432 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 433 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 434 | union { |
| 435 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 436 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 437 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 438 | struct io_timeout_data timeout; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 439 | struct io_async_open open; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 440 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 441 | }; |
| 442 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 443 | /* |
| 444 | * NOTE! Each of the iocb union members has the file pointer |
| 445 | * as the first entry in their struct definition. So you can |
| 446 | * access the file pointer through any of the sub-structs, |
| 447 | * or directly as just 'ki_filp' in this struct. |
| 448 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 449 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 450 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 451 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 452 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 453 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 454 | struct io_accept accept; |
| 455 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 456 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 457 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 458 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 459 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 460 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 461 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 462 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame^] | 463 | struct io_fadvise fadvise; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 464 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 465 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 466 | struct io_async_ctx *io; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 467 | union { |
| 468 | /* |
| 469 | * ring_file is only used in the submission path, and |
| 470 | * llist_node is only used for poll deferred completions |
| 471 | */ |
| 472 | struct file *ring_file; |
| 473 | struct llist_node llist_node; |
| 474 | }; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 475 | int ring_fd; |
| 476 | bool has_user; |
| 477 | bool in_async; |
| 478 | bool needs_fixed_file; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 479 | u8 opcode; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 480 | |
| 481 | struct io_ring_ctx *ctx; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 482 | union { |
| 483 | struct list_head list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 484 | struct hlist_node hash_node; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 485 | }; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 486 | struct list_head link_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 487 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 488 | refcount_t refs; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 489 | #define REQ_F_NOWAIT 1 /* must not punt to workers */ |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 490 | #define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */ |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 491 | #define REQ_F_FIXED_FILE 4 /* ctx owns file */ |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 492 | #define REQ_F_LINK_NEXT 8 /* already grabbed next link */ |
Stefan Bühler | e2033e3 | 2019-05-11 19:08:01 +0200 | [diff] [blame] | 493 | #define REQ_F_IO_DRAIN 16 /* drain existing IO first */ |
| 494 | #define REQ_F_IO_DRAINED 32 /* drain done */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 495 | #define REQ_F_LINK 64 /* linked sqes */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 496 | #define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */ |
Zhengyuan Liu | f7b76ac | 2019-07-16 23:26:14 +0800 | [diff] [blame] | 497 | #define REQ_F_FAIL_LINK 256 /* fail rest of links */ |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 498 | #define REQ_F_DRAIN_LINK 512 /* link should be fully drained */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 499 | #define REQ_F_TIMEOUT 1024 /* timeout request */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 500 | #define REQ_F_ISREG 2048 /* regular file */ |
| 501 | #define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */ |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 502 | #define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */ |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 503 | #define REQ_F_INFLIGHT 16384 /* on inflight list */ |
| 504 | #define REQ_F_COMP_LOCKED 32768 /* completion under lock */ |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 505 | #define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */ |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 506 | #define REQ_F_FORCE_ASYNC 131072 /* IOSQE_ASYNC */ |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 507 | #define REQ_F_CUR_POS 262144 /* read/write uses file position */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 508 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 509 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 510 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 511 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 512 | struct list_head inflight_entry; |
| 513 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 514 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 515 | }; |
| 516 | |
| 517 | #define IO_PLUG_THRESHOLD 2 |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 518 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 519 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 520 | struct io_submit_state { |
| 521 | struct blk_plug plug; |
| 522 | |
| 523 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 524 | * io_kiocb alloc cache |
| 525 | */ |
| 526 | void *reqs[IO_IOPOLL_BATCH]; |
| 527 | unsigned int free_reqs; |
| 528 | unsigned int cur_req; |
| 529 | |
| 530 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 531 | * File reference cache |
| 532 | */ |
| 533 | struct file *file; |
| 534 | unsigned int fd; |
| 535 | unsigned int has_refs; |
| 536 | unsigned int used_refs; |
| 537 | unsigned int ios_left; |
| 538 | }; |
| 539 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 540 | struct io_op_def { |
| 541 | /* needs req->io allocated for deferral/async */ |
| 542 | unsigned async_ctx : 1; |
| 543 | /* needs current->mm setup, does mm access */ |
| 544 | unsigned needs_mm : 1; |
| 545 | /* needs req->file assigned */ |
| 546 | unsigned needs_file : 1; |
| 547 | /* needs req->file assigned IFF fd is >= 0 */ |
| 548 | unsigned fd_non_neg : 1; |
| 549 | /* hash wq insertion if file is a regular file */ |
| 550 | unsigned hash_reg_file : 1; |
| 551 | /* unbound wq insertion if file is a non-regular file */ |
| 552 | unsigned unbound_nonreg_file : 1; |
| 553 | }; |
| 554 | |
| 555 | static const struct io_op_def io_op_defs[] = { |
| 556 | { |
| 557 | /* IORING_OP_NOP */ |
| 558 | }, |
| 559 | { |
| 560 | /* IORING_OP_READV */ |
| 561 | .async_ctx = 1, |
| 562 | .needs_mm = 1, |
| 563 | .needs_file = 1, |
| 564 | .unbound_nonreg_file = 1, |
| 565 | }, |
| 566 | { |
| 567 | /* IORING_OP_WRITEV */ |
| 568 | .async_ctx = 1, |
| 569 | .needs_mm = 1, |
| 570 | .needs_file = 1, |
| 571 | .hash_reg_file = 1, |
| 572 | .unbound_nonreg_file = 1, |
| 573 | }, |
| 574 | { |
| 575 | /* IORING_OP_FSYNC */ |
| 576 | .needs_file = 1, |
| 577 | }, |
| 578 | { |
| 579 | /* IORING_OP_READ_FIXED */ |
| 580 | .needs_file = 1, |
| 581 | .unbound_nonreg_file = 1, |
| 582 | }, |
| 583 | { |
| 584 | /* IORING_OP_WRITE_FIXED */ |
| 585 | .needs_file = 1, |
| 586 | .hash_reg_file = 1, |
| 587 | .unbound_nonreg_file = 1, |
| 588 | }, |
| 589 | { |
| 590 | /* IORING_OP_POLL_ADD */ |
| 591 | .needs_file = 1, |
| 592 | .unbound_nonreg_file = 1, |
| 593 | }, |
| 594 | { |
| 595 | /* IORING_OP_POLL_REMOVE */ |
| 596 | }, |
| 597 | { |
| 598 | /* IORING_OP_SYNC_FILE_RANGE */ |
| 599 | .needs_file = 1, |
| 600 | }, |
| 601 | { |
| 602 | /* IORING_OP_SENDMSG */ |
| 603 | .async_ctx = 1, |
| 604 | .needs_mm = 1, |
| 605 | .needs_file = 1, |
| 606 | .unbound_nonreg_file = 1, |
| 607 | }, |
| 608 | { |
| 609 | /* IORING_OP_RECVMSG */ |
| 610 | .async_ctx = 1, |
| 611 | .needs_mm = 1, |
| 612 | .needs_file = 1, |
| 613 | .unbound_nonreg_file = 1, |
| 614 | }, |
| 615 | { |
| 616 | /* IORING_OP_TIMEOUT */ |
| 617 | .async_ctx = 1, |
| 618 | .needs_mm = 1, |
| 619 | }, |
| 620 | { |
| 621 | /* IORING_OP_TIMEOUT_REMOVE */ |
| 622 | }, |
| 623 | { |
| 624 | /* IORING_OP_ACCEPT */ |
| 625 | .needs_mm = 1, |
| 626 | .needs_file = 1, |
| 627 | .unbound_nonreg_file = 1, |
| 628 | }, |
| 629 | { |
| 630 | /* IORING_OP_ASYNC_CANCEL */ |
| 631 | }, |
| 632 | { |
| 633 | /* IORING_OP_LINK_TIMEOUT */ |
| 634 | .async_ctx = 1, |
| 635 | .needs_mm = 1, |
| 636 | }, |
| 637 | { |
| 638 | /* IORING_OP_CONNECT */ |
| 639 | .async_ctx = 1, |
| 640 | .needs_mm = 1, |
| 641 | .needs_file = 1, |
| 642 | .unbound_nonreg_file = 1, |
| 643 | }, |
| 644 | { |
| 645 | /* IORING_OP_FALLOCATE */ |
| 646 | .needs_file = 1, |
| 647 | }, |
| 648 | { |
| 649 | /* IORING_OP_OPENAT */ |
| 650 | .needs_file = 1, |
| 651 | .fd_non_neg = 1, |
| 652 | }, |
| 653 | { |
| 654 | /* IORING_OP_CLOSE */ |
| 655 | .needs_file = 1, |
| 656 | }, |
| 657 | { |
| 658 | /* IORING_OP_FILES_UPDATE */ |
| 659 | .needs_mm = 1, |
| 660 | }, |
| 661 | { |
| 662 | /* IORING_OP_STATX */ |
| 663 | .needs_mm = 1, |
| 664 | .needs_file = 1, |
| 665 | .fd_non_neg = 1, |
| 666 | }, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 667 | { |
| 668 | /* IORING_OP_READ */ |
| 669 | .needs_mm = 1, |
| 670 | .needs_file = 1, |
| 671 | .unbound_nonreg_file = 1, |
| 672 | }, |
| 673 | { |
| 674 | /* IORING_OP_WRITE */ |
| 675 | .needs_mm = 1, |
| 676 | .needs_file = 1, |
| 677 | .unbound_nonreg_file = 1, |
| 678 | }, |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame^] | 679 | { |
| 680 | /* IORING_OP_FADVISE */ |
| 681 | .needs_file = 1, |
| 682 | }, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 683 | }; |
| 684 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 685 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 686 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 687 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 688 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 689 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 690 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 691 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 692 | struct io_uring_files_update *ip, |
| 693 | unsigned nr_args); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 694 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 695 | static struct kmem_cache *req_cachep; |
| 696 | |
| 697 | static const struct file_operations io_uring_fops; |
| 698 | |
| 699 | struct sock *io_uring_get_socket(struct file *file) |
| 700 | { |
| 701 | #if defined(CONFIG_UNIX) |
| 702 | if (file->f_op == &io_uring_fops) { |
| 703 | struct io_ring_ctx *ctx = file->private_data; |
| 704 | |
| 705 | return ctx->ring_sock->sk; |
| 706 | } |
| 707 | #endif |
| 708 | return NULL; |
| 709 | } |
| 710 | EXPORT_SYMBOL(io_uring_get_socket); |
| 711 | |
| 712 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 713 | { |
| 714 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 715 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 716 | complete(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 720 | { |
| 721 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 722 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 723 | |
| 724 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 725 | if (!ctx) |
| 726 | return NULL; |
| 727 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 728 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 729 | if (!ctx->fallback_req) |
| 730 | goto err; |
| 731 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 732 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
| 733 | if (!ctx->completions) |
| 734 | goto err; |
| 735 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 736 | /* |
| 737 | * Use 5 bits less than the max cq entries, that should give us around |
| 738 | * 32 entries per hash list if totally full and uniformly spread. |
| 739 | */ |
| 740 | hash_bits = ilog2(p->cq_entries); |
| 741 | hash_bits -= 5; |
| 742 | if (hash_bits <= 0) |
| 743 | hash_bits = 1; |
| 744 | ctx->cancel_hash_bits = hash_bits; |
| 745 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 746 | GFP_KERNEL); |
| 747 | if (!ctx->cancel_hash) |
| 748 | goto err; |
| 749 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 750 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 751 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 752 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 753 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 754 | |
| 755 | ctx->flags = p->flags; |
| 756 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 757 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 758 | init_completion(&ctx->completions[0]); |
| 759 | init_completion(&ctx->completions[1]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 760 | mutex_init(&ctx->uring_lock); |
| 761 | init_waitqueue_head(&ctx->wait); |
| 762 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 763 | init_llist_head(&ctx->poll_llist); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 764 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 765 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 766 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 767 | init_waitqueue_head(&ctx->inflight_wait); |
| 768 | spin_lock_init(&ctx->inflight_lock); |
| 769 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 770 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 771 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 772 | if (ctx->fallback_req) |
| 773 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 774 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 775 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 776 | kfree(ctx); |
| 777 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 780 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 781 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 782 | struct io_ring_ctx *ctx = req->ctx; |
| 783 | |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 784 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 785 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 786 | } |
| 787 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 788 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 789 | { |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 790 | if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN) |
| 791 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 792 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 793 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | 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] | 797 | { |
| 798 | struct io_kiocb *req; |
| 799 | |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 800 | 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] | 801 | if (req && !req_need_defer(req)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 802 | list_del_init(&req->list); |
| 803 | return req; |
| 804 | } |
| 805 | |
| 806 | return NULL; |
| 807 | } |
| 808 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 809 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 810 | { |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 811 | struct io_kiocb *req; |
| 812 | |
| 813 | 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] | 814 | if (req) { |
| 815 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 816 | return NULL; |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 817 | if (!__req_need_defer(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 818 | list_del_init(&req->list); |
| 819 | return req; |
| 820 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | return NULL; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 824 | } |
| 825 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 826 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 827 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 828 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 829 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 830 | if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 831 | /* order cqe stores with ring update */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 832 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 833 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 834 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 835 | wake_up_interruptible(&ctx->cq_wait); |
| 836 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 841 | static inline bool io_prep_async_work(struct io_kiocb *req, |
| 842 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 843 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 844 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 845 | bool do_hashed = false; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 846 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 847 | if (req->flags & REQ_F_ISREG) { |
| 848 | if (def->hash_reg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 849 | do_hashed = true; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 850 | } else { |
| 851 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 852 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 853 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 854 | if (def->needs_mm) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 855 | req->work.flags |= IO_WQ_WORK_NEEDS_USER; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 856 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 857 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 858 | return do_hashed; |
| 859 | } |
| 860 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 861 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 862 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 863 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 864 | struct io_kiocb *link; |
| 865 | bool do_hashed; |
| 866 | |
| 867 | do_hashed = io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 868 | |
| 869 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, |
| 870 | req->flags); |
| 871 | if (!do_hashed) { |
| 872 | io_wq_enqueue(ctx->io_wq, &req->work); |
| 873 | } else { |
| 874 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, |
| 875 | file_inode(req->file)); |
| 876 | } |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 877 | |
| 878 | if (link) |
| 879 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 880 | } |
| 881 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 882 | static void io_kill_timeout(struct io_kiocb *req) |
| 883 | { |
| 884 | int ret; |
| 885 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 886 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 887 | if (ret != -1) { |
| 888 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 889 | list_del_init(&req->list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 890 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 891 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 892 | } |
| 893 | } |
| 894 | |
| 895 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 896 | { |
| 897 | struct io_kiocb *req, *tmp; |
| 898 | |
| 899 | spin_lock_irq(&ctx->completion_lock); |
| 900 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 901 | io_kill_timeout(req); |
| 902 | spin_unlock_irq(&ctx->completion_lock); |
| 903 | } |
| 904 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 905 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 906 | { |
| 907 | struct io_kiocb *req; |
| 908 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 909 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 910 | io_kill_timeout(req); |
| 911 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 912 | __io_commit_cqring(ctx); |
| 913 | |
| 914 | while ((req = io_get_deferred_req(ctx)) != NULL) { |
| 915 | req->flags |= REQ_F_IO_DRAINED; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 916 | io_queue_async_work(req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 920 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 921 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 922 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 923 | unsigned tail; |
| 924 | |
| 925 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 926 | /* |
| 927 | * writes to the cq entry need to come after reading head; the |
| 928 | * control dependency is enough as we're using WRITE_ONCE to |
| 929 | * fill the cq entry |
| 930 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 931 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 932 | return NULL; |
| 933 | |
| 934 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 935 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 938 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| 939 | { |
| 940 | if (waitqueue_active(&ctx->wait)) |
| 941 | wake_up(&ctx->wait); |
| 942 | if (waitqueue_active(&ctx->sqo_wait)) |
| 943 | wake_up(&ctx->sqo_wait); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 944 | if (ctx->cq_ev_fd) |
| 945 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 946 | } |
| 947 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 948 | /* Returns true if there are no backlogged entries after the flush */ |
| 949 | 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] | 950 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 951 | struct io_rings *rings = ctx->rings; |
| 952 | struct io_uring_cqe *cqe; |
| 953 | struct io_kiocb *req; |
| 954 | unsigned long flags; |
| 955 | LIST_HEAD(list); |
| 956 | |
| 957 | if (!force) { |
| 958 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 959 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 960 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 961 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 962 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 966 | |
| 967 | /* if force is set, the ring is going away. always drop after that */ |
| 968 | if (force) |
| 969 | ctx->cq_overflow_flushed = true; |
| 970 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 971 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 972 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 973 | cqe = io_get_cqring(ctx); |
| 974 | if (!cqe && !force) |
| 975 | break; |
| 976 | |
| 977 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 978 | list); |
| 979 | list_move(&req->list, &list); |
| 980 | if (cqe) { |
| 981 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 982 | WRITE_ONCE(cqe->res, req->result); |
| 983 | WRITE_ONCE(cqe->flags, 0); |
| 984 | } else { |
| 985 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 986 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | io_commit_cqring(ctx); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 991 | if (cqe) { |
| 992 | clear_bit(0, &ctx->sq_check_overflow); |
| 993 | clear_bit(0, &ctx->cq_check_overflow); |
| 994 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 995 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 996 | io_cqring_ev_posted(ctx); |
| 997 | |
| 998 | while (!list_empty(&list)) { |
| 999 | req = list_first_entry(&list, struct io_kiocb, list); |
| 1000 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1001 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1002 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1003 | |
| 1004 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1007 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1008 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1009 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1010 | struct io_uring_cqe *cqe; |
| 1011 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1012 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1013 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1014 | /* |
| 1015 | * If we can't get a cq entry, userspace overflowed the |
| 1016 | * submission (by quite a lot). Increment the overflow count in |
| 1017 | * the ring. |
| 1018 | */ |
| 1019 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1020 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1021 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1022 | WRITE_ONCE(cqe->res, res); |
| 1023 | WRITE_ONCE(cqe->flags, 0); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1024 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1025 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1026 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1027 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1028 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1029 | set_bit(0, &ctx->sq_check_overflow); |
| 1030 | set_bit(0, &ctx->cq_check_overflow); |
| 1031 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1032 | refcount_inc(&req->refs); |
| 1033 | req->result = res; |
| 1034 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1038 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1039 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1040 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1041 | unsigned long flags; |
| 1042 | |
| 1043 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1044 | io_cqring_fill_event(req, res); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1045 | io_commit_cqring(ctx); |
| 1046 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1047 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1048 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1051 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1052 | { |
| 1053 | return req == (struct io_kiocb *) |
| 1054 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1055 | } |
| 1056 | |
| 1057 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1058 | { |
| 1059 | struct io_kiocb *req; |
| 1060 | |
| 1061 | req = ctx->fallback_req; |
| 1062 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) |
| 1063 | return req; |
| 1064 | |
| 1065 | return NULL; |
| 1066 | } |
| 1067 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1068 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 1069 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1070 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1071 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1072 | struct io_kiocb *req; |
| 1073 | |
| 1074 | if (!percpu_ref_tryget(&ctx->refs)) |
| 1075 | return NULL; |
| 1076 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1077 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1078 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1079 | if (unlikely(!req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1080 | goto fallback; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1081 | } else if (!state->free_reqs) { |
| 1082 | size_t sz; |
| 1083 | int ret; |
| 1084 | |
| 1085 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1086 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1087 | |
| 1088 | /* |
| 1089 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1090 | * retry single alloc to be on the safe side. |
| 1091 | */ |
| 1092 | if (unlikely(ret <= 0)) { |
| 1093 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1094 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1095 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1096 | ret = 1; |
| 1097 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1098 | state->free_reqs = ret - 1; |
| 1099 | state->cur_req = 1; |
| 1100 | req = state->reqs[0]; |
| 1101 | } else { |
| 1102 | req = state->reqs[state->cur_req]; |
| 1103 | state->free_reqs--; |
| 1104 | state->cur_req++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1107 | got_it: |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 1108 | req->io = NULL; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 1109 | req->ring_file = NULL; |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 1110 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1111 | req->ctx = ctx; |
| 1112 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1113 | /* one is dropped after submission, the other at completion */ |
| 1114 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1115 | req->result = 0; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1116 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1117 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1118 | fallback: |
| 1119 | req = io_get_fallback_req(ctx); |
| 1120 | if (req) |
| 1121 | goto got_it; |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 1122 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1123 | return NULL; |
| 1124 | } |
| 1125 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1126 | static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr) |
| 1127 | { |
| 1128 | if (*nr) { |
| 1129 | kmem_cache_free_bulk(req_cachep, *nr, reqs); |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 1130 | percpu_ref_put_many(&ctx->refs, *nr); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1131 | percpu_ref_put_many(&ctx->file_data->refs, *nr); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1132 | *nr = 0; |
| 1133 | } |
| 1134 | } |
| 1135 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1136 | static void __io_free_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1137 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1138 | struct io_ring_ctx *ctx = req->ctx; |
| 1139 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 1140 | if (req->io) |
| 1141 | kfree(req->io); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1142 | if (req->file) { |
| 1143 | if (req->flags & REQ_F_FIXED_FILE) |
| 1144 | percpu_ref_put(&ctx->file_data->refs); |
| 1145 | else |
| 1146 | fput(req->file); |
| 1147 | } |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1148 | if (req->flags & REQ_F_INFLIGHT) { |
| 1149 | unsigned long flags; |
| 1150 | |
| 1151 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1152 | list_del(&req->inflight_entry); |
| 1153 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1154 | wake_up(&ctx->inflight_wait); |
| 1155 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1156 | } |
| 1157 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1158 | if (likely(!io_is_fallback_req(req))) |
| 1159 | kmem_cache_free(req_cachep, req); |
| 1160 | else |
| 1161 | clear_bit_unlock(0, (unsigned long *) ctx->fallback_req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1162 | } |
| 1163 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1164 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1165 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1166 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1167 | int ret; |
| 1168 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1169 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1170 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1171 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1172 | io_commit_cqring(ctx); |
| 1173 | req->flags &= ~REQ_F_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1174 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1175 | return true; |
| 1176 | } |
| 1177 | |
| 1178 | return false; |
| 1179 | } |
| 1180 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1181 | 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] | 1182 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1183 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1184 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1185 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1186 | /* Already got next link */ |
| 1187 | if (req->flags & REQ_F_LINK_NEXT) |
| 1188 | return; |
| 1189 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1190 | /* |
| 1191 | * The list should never be empty when we are called here. But could |
| 1192 | * potentially happen if the chain is messed up, check to be on the |
| 1193 | * safe side. |
| 1194 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1195 | while (!list_empty(&req->link_list)) { |
| 1196 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 1197 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1198 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1199 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 1200 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 1201 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1202 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1203 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1204 | continue; |
| 1205 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1206 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1207 | list_del_init(&req->link_list); |
| 1208 | if (!list_empty(&nxt->link_list)) |
| 1209 | nxt->flags |= REQ_F_LINK; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 1210 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1211 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1212 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1213 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1214 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1215 | if (wake_ev) |
| 1216 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | /* |
| 1220 | * Called if REQ_F_LINK is set, and we fail the head request |
| 1221 | */ |
| 1222 | static void io_fail_links(struct io_kiocb *req) |
| 1223 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1224 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1225 | unsigned long flags; |
| 1226 | |
| 1227 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1228 | |
| 1229 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1230 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1231 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1232 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1233 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1234 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1235 | |
| 1236 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1237 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1238 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1239 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1240 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1241 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1242 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1243 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1244 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1245 | |
| 1246 | io_commit_cqring(ctx); |
| 1247 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1248 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1249 | } |
| 1250 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1251 | 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] | 1252 | { |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1253 | if (likely(!(req->flags & REQ_F_LINK))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1254 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1255 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1256 | /* |
| 1257 | * If LINK is set, we have dependent requests in this chain. If we |
| 1258 | * didn't fail this request, queue the first one up, moving any other |
| 1259 | * dependencies to the next request. In case of failure, fail the rest |
| 1260 | * of the chain. |
| 1261 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1262 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1263 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1264 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1265 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1266 | struct io_ring_ctx *ctx = req->ctx; |
| 1267 | unsigned long flags; |
| 1268 | |
| 1269 | /* |
| 1270 | * If this is a timeout link, we could be racing with the |
| 1271 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1272 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1273 | */ |
| 1274 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1275 | io_req_link_next(req, nxt); |
| 1276 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1277 | } else { |
| 1278 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1279 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1280 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1281 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1282 | static void io_free_req(struct io_kiocb *req) |
| 1283 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1284 | struct io_kiocb *nxt = NULL; |
| 1285 | |
| 1286 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1287 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1288 | |
| 1289 | if (nxt) |
| 1290 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1291 | } |
| 1292 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1293 | /* |
| 1294 | * Drop reference to request, return next in chain (if there is one) if this |
| 1295 | * was the last reference to this request. |
| 1296 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1297 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1298 | 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] | 1299 | { |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1300 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1301 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1302 | if (refcount_dec_and_test(&req->refs)) |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1303 | __io_free_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1306 | static void io_put_req(struct io_kiocb *req) |
| 1307 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1308 | if (refcount_dec_and_test(&req->refs)) |
| 1309 | io_free_req(req); |
| 1310 | } |
| 1311 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1312 | /* |
| 1313 | * Must only be used if we don't need to care about links, usually from |
| 1314 | * within the completion handling itself. |
| 1315 | */ |
| 1316 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1317 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1318 | /* drop both submit and complete references */ |
| 1319 | if (refcount_sub_and_test(2, &req->refs)) |
| 1320 | __io_free_req(req); |
| 1321 | } |
| 1322 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1323 | static void io_double_put_req(struct io_kiocb *req) |
| 1324 | { |
| 1325 | /* drop both submit and complete references */ |
| 1326 | if (refcount_sub_and_test(2, &req->refs)) |
| 1327 | io_free_req(req); |
| 1328 | } |
| 1329 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1330 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1331 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1332 | struct io_rings *rings = ctx->rings; |
| 1333 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1334 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1335 | /* |
| 1336 | * noflush == true is from the waitqueue handler, just ensure |
| 1337 | * we wake up the task, and the next invocation will flush the |
| 1338 | * entries. We cannot safely to it from here. |
| 1339 | */ |
| 1340 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1341 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1342 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1343 | io_cqring_overflow_flush(ctx, false); |
| 1344 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1345 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1346 | /* See comment at the top of this file */ |
| 1347 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1348 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1349 | } |
| 1350 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1351 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1352 | { |
| 1353 | struct io_rings *rings = ctx->rings; |
| 1354 | |
| 1355 | /* make sure SQ entry isn't read before tail */ |
| 1356 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1357 | } |
| 1358 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1359 | static inline bool io_req_multi_free(struct io_kiocb *req) |
| 1360 | { |
| 1361 | /* |
| 1362 | * If we're not using fixed files, we have to pair the completion part |
| 1363 | * with the file put. Use regular completions for those, only batch |
| 1364 | * free for fixed file and non-linked commands. |
| 1365 | */ |
| 1366 | if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) == REQ_F_FIXED_FILE) |
| 1367 | && !io_is_fallback_req(req) && !req->io) |
| 1368 | return true; |
| 1369 | |
| 1370 | return false; |
| 1371 | } |
| 1372 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1373 | /* |
| 1374 | * Find and free completed poll iocbs |
| 1375 | */ |
| 1376 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1377 | struct list_head *done) |
| 1378 | { |
| 1379 | void *reqs[IO_IOPOLL_BATCH]; |
| 1380 | struct io_kiocb *req; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1381 | int to_free; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1382 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1383 | to_free = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1384 | while (!list_empty(done)) { |
| 1385 | req = list_first_entry(done, struct io_kiocb, list); |
| 1386 | list_del(&req->list); |
| 1387 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1388 | io_cqring_fill_event(req, req->result); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1389 | (*nr_events)++; |
| 1390 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1391 | if (refcount_dec_and_test(&req->refs)) { |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1392 | if (io_req_multi_free(req)) { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1393 | reqs[to_free++] = req; |
| 1394 | if (to_free == ARRAY_SIZE(reqs)) |
| 1395 | io_free_req_many(ctx, reqs, &to_free); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 1396 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1397 | io_free_req(req); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 1398 | } |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1399 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1400 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1401 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1402 | io_commit_cqring(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1403 | io_free_req_many(ctx, reqs, &to_free); |
| 1404 | } |
| 1405 | |
| 1406 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1407 | long min) |
| 1408 | { |
| 1409 | struct io_kiocb *req, *tmp; |
| 1410 | LIST_HEAD(done); |
| 1411 | bool spin; |
| 1412 | int ret; |
| 1413 | |
| 1414 | /* |
| 1415 | * Only spin for completions if we don't have multiple devices hanging |
| 1416 | * off our complete list, and we're under the requested amount. |
| 1417 | */ |
| 1418 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1419 | |
| 1420 | ret = 0; |
| 1421 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1422 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1423 | |
| 1424 | /* |
| 1425 | * Move completed entries to our local list. If we find a |
| 1426 | * request that requires polling, break out and complete |
| 1427 | * the done list first, if we have entries there. |
| 1428 | */ |
| 1429 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 1430 | list_move_tail(&req->list, &done); |
| 1431 | continue; |
| 1432 | } |
| 1433 | if (!list_empty(&done)) |
| 1434 | break; |
| 1435 | |
| 1436 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1437 | if (ret < 0) |
| 1438 | break; |
| 1439 | |
| 1440 | if (ret && spin) |
| 1441 | spin = false; |
| 1442 | ret = 0; |
| 1443 | } |
| 1444 | |
| 1445 | if (!list_empty(&done)) |
| 1446 | io_iopoll_complete(ctx, nr_events, &done); |
| 1447 | |
| 1448 | return ret; |
| 1449 | } |
| 1450 | |
| 1451 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 1452 | * 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] | 1453 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1454 | * as a non-spinning completion check. |
| 1455 | */ |
| 1456 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1457 | long min) |
| 1458 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1459 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1460 | int ret; |
| 1461 | |
| 1462 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1463 | if (ret < 0) |
| 1464 | return ret; |
| 1465 | if (!min || *nr_events >= min) |
| 1466 | return 0; |
| 1467 | } |
| 1468 | |
| 1469 | return 1; |
| 1470 | } |
| 1471 | |
| 1472 | /* |
| 1473 | * We can't just wait for polled events to come to us, we have to actively |
| 1474 | * find and complete them. |
| 1475 | */ |
| 1476 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1477 | { |
| 1478 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1479 | return; |
| 1480 | |
| 1481 | mutex_lock(&ctx->uring_lock); |
| 1482 | while (!list_empty(&ctx->poll_list)) { |
| 1483 | unsigned int nr_events = 0; |
| 1484 | |
| 1485 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1486 | |
| 1487 | /* |
| 1488 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1489 | * in this case we need to ensure that we reap all events. |
| 1490 | */ |
| 1491 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1492 | } |
| 1493 | mutex_unlock(&ctx->uring_lock); |
| 1494 | } |
| 1495 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1496 | static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1497 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1498 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1499 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1500 | |
| 1501 | do { |
| 1502 | int tmin = 0; |
| 1503 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1504 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1505 | * Don't enter poll loop if we already have events pending. |
| 1506 | * If we do, we can potentially be spinning for commands that |
| 1507 | * already triggered a CQE (eg in error). |
| 1508 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1509 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1510 | break; |
| 1511 | |
| 1512 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1513 | * If a submit got punted to a workqueue, we can have the |
| 1514 | * application entering polling for a command before it gets |
| 1515 | * issued. That app will hold the uring_lock for the duration |
| 1516 | * of the poll right here, so we need to take a breather every |
| 1517 | * now and then to ensure that the issue has a chance to add |
| 1518 | * the poll to the issued list. Otherwise we can spin here |
| 1519 | * forever, while the workqueue is stuck trying to acquire the |
| 1520 | * very same mutex. |
| 1521 | */ |
| 1522 | if (!(++iters & 7)) { |
| 1523 | mutex_unlock(&ctx->uring_lock); |
| 1524 | mutex_lock(&ctx->uring_lock); |
| 1525 | } |
| 1526 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1527 | if (*nr_events < min) |
| 1528 | tmin = min - *nr_events; |
| 1529 | |
| 1530 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1531 | if (ret <= 0) |
| 1532 | break; |
| 1533 | ret = 0; |
| 1534 | } while (min && !*nr_events && !need_resched()); |
| 1535 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1536 | return ret; |
| 1537 | } |
| 1538 | |
| 1539 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1540 | long min) |
| 1541 | { |
| 1542 | int ret; |
| 1543 | |
| 1544 | /* |
| 1545 | * We disallow the app entering submit/complete with polling, but we |
| 1546 | * still need to lock the ring to prevent racing with polled issue |
| 1547 | * that got punted to a workqueue. |
| 1548 | */ |
| 1549 | mutex_lock(&ctx->uring_lock); |
| 1550 | ret = __io_iopoll_check(ctx, nr_events, min); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1551 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1552 | return ret; |
| 1553 | } |
| 1554 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1555 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1556 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1557 | /* |
| 1558 | * Tell lockdep we inherited freeze protection from submission |
| 1559 | * thread. |
| 1560 | */ |
| 1561 | if (req->flags & REQ_F_ISREG) { |
| 1562 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1563 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1564 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1565 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1566 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1567 | } |
| 1568 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1569 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1570 | { |
| 1571 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1572 | req->flags |= REQ_F_FAIL_LINK; |
| 1573 | } |
| 1574 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1575 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1576 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1577 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1578 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1579 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1580 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1581 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1582 | if (res != req->result) |
| 1583 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1584 | io_cqring_add_event(req, res); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1588 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1589 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1590 | |
| 1591 | io_complete_rw_common(kiocb, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1592 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1593 | } |
| 1594 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1595 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
| 1596 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1597 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1598 | struct io_kiocb *nxt = NULL; |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1599 | |
| 1600 | io_complete_rw_common(kiocb, res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1601 | io_put_req_find_next(req, &nxt); |
| 1602 | |
| 1603 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1604 | } |
| 1605 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1606 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 1607 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1608 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1609 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1610 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1611 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1612 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1613 | if (res != req->result) |
| 1614 | req_set_fail_links(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1615 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1616 | if (res != -EAGAIN) |
| 1617 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 1618 | } |
| 1619 | |
| 1620 | /* |
| 1621 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 1622 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 1623 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 1624 | * accessing the kiocb cookie. |
| 1625 | */ |
| 1626 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 1627 | { |
| 1628 | struct io_ring_ctx *ctx = req->ctx; |
| 1629 | |
| 1630 | /* |
| 1631 | * Track whether we have multiple files in our lists. This will impact |
| 1632 | * how we do polling eventually, not spinning if we're on potentially |
| 1633 | * different devices. |
| 1634 | */ |
| 1635 | if (list_empty(&ctx->poll_list)) { |
| 1636 | ctx->poll_multi_file = false; |
| 1637 | } else if (!ctx->poll_multi_file) { |
| 1638 | struct io_kiocb *list_req; |
| 1639 | |
| 1640 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1641 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1642 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1643 | ctx->poll_multi_file = true; |
| 1644 | } |
| 1645 | |
| 1646 | /* |
| 1647 | * For fast devices, IO may have already completed. If it has, add |
| 1648 | * it to the front so we find it first. |
| 1649 | */ |
| 1650 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1651 | list_add(&req->list, &ctx->poll_list); |
| 1652 | else |
| 1653 | list_add_tail(&req->list, &ctx->poll_list); |
| 1654 | } |
| 1655 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1656 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1657 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1658 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1659 | int diff = state->has_refs - state->used_refs; |
| 1660 | |
| 1661 | if (diff) |
| 1662 | fput_many(state->file, diff); |
| 1663 | state->file = NULL; |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | /* |
| 1668 | * Get as many references to a file as we have IOs left in this submission, |
| 1669 | * assuming most submissions are for one file, or at least that each file |
| 1670 | * has more than one submission. |
| 1671 | */ |
| 1672 | static struct file *io_file_get(struct io_submit_state *state, int fd) |
| 1673 | { |
| 1674 | if (!state) |
| 1675 | return fget(fd); |
| 1676 | |
| 1677 | if (state->file) { |
| 1678 | if (state->fd == fd) { |
| 1679 | state->used_refs++; |
| 1680 | state->ios_left--; |
| 1681 | return state->file; |
| 1682 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1683 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1684 | } |
| 1685 | state->file = fget_many(fd, state->ios_left); |
| 1686 | if (!state->file) |
| 1687 | return NULL; |
| 1688 | |
| 1689 | state->fd = fd; |
| 1690 | state->has_refs = state->ios_left; |
| 1691 | state->used_refs = 1; |
| 1692 | state->ios_left--; |
| 1693 | return state->file; |
| 1694 | } |
| 1695 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1696 | /* |
| 1697 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 1698 | * any file. For now, just ensure that anything potentially problematic is done |
| 1699 | * inline. |
| 1700 | */ |
| 1701 | static bool io_file_supports_async(struct file *file) |
| 1702 | { |
| 1703 | umode_t mode = file_inode(file)->i_mode; |
| 1704 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 1705 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1706 | return true; |
| 1707 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 1708 | return true; |
| 1709 | |
| 1710 | return false; |
| 1711 | } |
| 1712 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1713 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1714 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1715 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1716 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1717 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1718 | unsigned ioprio; |
| 1719 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1720 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1721 | if (!req->file) |
| 1722 | return -EBADF; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1723 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1724 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 1725 | req->flags |= REQ_F_ISREG; |
| 1726 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1727 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1728 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 1729 | req->flags |= REQ_F_CUR_POS; |
| 1730 | kiocb->ki_pos = req->file->f_pos; |
| 1731 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1732 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 1733 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
| 1734 | |
| 1735 | ioprio = READ_ONCE(sqe->ioprio); |
| 1736 | if (ioprio) { |
| 1737 | ret = ioprio_check_cap(ioprio); |
| 1738 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1739 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1740 | |
| 1741 | kiocb->ki_ioprio = ioprio; |
| 1742 | } else |
| 1743 | kiocb->ki_ioprio = get_current_ioprio(); |
| 1744 | |
| 1745 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 1746 | if (unlikely(ret)) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1747 | return ret; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1748 | |
| 1749 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1750 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 1751 | (req->file->f_flags & O_NONBLOCK)) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1752 | req->flags |= REQ_F_NOWAIT; |
| 1753 | |
| 1754 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1755 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1756 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1757 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1758 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 1759 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1760 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1761 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1762 | kiocb->ki_flags |= IOCB_HIPRI; |
| 1763 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 1764 | req->result = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1765 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1766 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1767 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1768 | kiocb->ki_complete = io_complete_rw; |
| 1769 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1770 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1771 | req->rw.addr = READ_ONCE(sqe->addr); |
| 1772 | req->rw.len = READ_ONCE(sqe->len); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1773 | /* we own ->private, reuse it for the buffer index */ |
| 1774 | req->rw.kiocb.private = (void *) (unsigned long) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1775 | READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1776 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 1780 | { |
| 1781 | switch (ret) { |
| 1782 | case -EIOCBQUEUED: |
| 1783 | break; |
| 1784 | case -ERESTARTSYS: |
| 1785 | case -ERESTARTNOINTR: |
| 1786 | case -ERESTARTNOHAND: |
| 1787 | case -ERESTART_RESTARTBLOCK: |
| 1788 | /* |
| 1789 | * We can't just restart the syscall, since previously |
| 1790 | * submitted sqes may already be in progress. Just fail this |
| 1791 | * IO with EINTR. |
| 1792 | */ |
| 1793 | ret = -EINTR; |
| 1794 | /* fall through */ |
| 1795 | default: |
| 1796 | kiocb->ki_complete(kiocb, ret, 0); |
| 1797 | } |
| 1798 | } |
| 1799 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1800 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt, |
| 1801 | bool in_async) |
| 1802 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1803 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 1804 | |
| 1805 | if (req->flags & REQ_F_CUR_POS) |
| 1806 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1807 | if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1808 | *nxt = __io_complete_rw(kiocb, ret); |
| 1809 | else |
| 1810 | io_rw_done(kiocb, ret); |
| 1811 | } |
| 1812 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1813 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 1814 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1815 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1816 | struct io_ring_ctx *ctx = req->ctx; |
| 1817 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1818 | struct io_mapped_ubuf *imu; |
| 1819 | unsigned index, buf_index; |
| 1820 | size_t offset; |
| 1821 | u64 buf_addr; |
| 1822 | |
| 1823 | /* attempt to use fixed buffers without having provided iovecs */ |
| 1824 | if (unlikely(!ctx->user_bufs)) |
| 1825 | return -EFAULT; |
| 1826 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1827 | buf_index = (unsigned long) req->rw.kiocb.private; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1828 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 1829 | return -EFAULT; |
| 1830 | |
| 1831 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 1832 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1833 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1834 | |
| 1835 | /* overflow */ |
| 1836 | if (buf_addr + len < buf_addr) |
| 1837 | return -EFAULT; |
| 1838 | /* not inside the mapped region */ |
| 1839 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 1840 | return -EFAULT; |
| 1841 | |
| 1842 | /* |
| 1843 | * May not be a start of buffer, set size appropriately |
| 1844 | * and advance us to the beginning. |
| 1845 | */ |
| 1846 | offset = buf_addr - imu->ubuf; |
| 1847 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1848 | |
| 1849 | if (offset) { |
| 1850 | /* |
| 1851 | * Don't use iov_iter_advance() here, as it's really slow for |
| 1852 | * using the latter parts of a big fixed buffer - it iterates |
| 1853 | * over each segment manually. We can cheat a bit here, because |
| 1854 | * we know that: |
| 1855 | * |
| 1856 | * 1) it's a BVEC iter, we set it up |
| 1857 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 1858 | * first and last bvec |
| 1859 | * |
| 1860 | * So just find our index, and adjust the iterator afterwards. |
| 1861 | * If the offset is within the first bvec (or the whole first |
| 1862 | * bvec, just use iov_iter_advance(). This makes it easier |
| 1863 | * since we can just skip the first segment, which may not |
| 1864 | * be PAGE_SIZE aligned. |
| 1865 | */ |
| 1866 | const struct bio_vec *bvec = imu->bvec; |
| 1867 | |
| 1868 | if (offset <= bvec->bv_len) { |
| 1869 | iov_iter_advance(iter, offset); |
| 1870 | } else { |
| 1871 | unsigned long seg_skip; |
| 1872 | |
| 1873 | /* skip first vec */ |
| 1874 | offset -= bvec->bv_len; |
| 1875 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 1876 | |
| 1877 | iter->bvec = bvec + seg_skip; |
| 1878 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 1879 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1880 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1881 | } |
| 1882 | } |
| 1883 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 1884 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1885 | } |
| 1886 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 1887 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 1888 | struct iovec **iovec, struct iov_iter *iter) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1889 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1890 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 1891 | size_t sqe_len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1892 | u8 opcode; |
| 1893 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1894 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 1895 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1896 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1897 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1898 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1899 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1900 | /* buffer index only valid with fixed read/write */ |
| 1901 | if (req->rw.kiocb.private) |
| 1902 | return -EINVAL; |
| 1903 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1904 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
| 1905 | ssize_t ret; |
| 1906 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 1907 | *iovec = NULL; |
| 1908 | return ret; |
| 1909 | } |
| 1910 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1911 | if (req->io) { |
| 1912 | struct io_async_rw *iorw = &req->io->rw; |
| 1913 | |
| 1914 | *iovec = iorw->iov; |
| 1915 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 1916 | if (iorw->iov == iorw->fast_iov) |
| 1917 | *iovec = NULL; |
| 1918 | return iorw->size; |
| 1919 | } |
| 1920 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 1921 | if (!req->has_user) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1922 | return -EFAULT; |
| 1923 | |
| 1924 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 1925 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1926 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 1927 | iovec, iter); |
| 1928 | #endif |
| 1929 | |
| 1930 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 1931 | } |
| 1932 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1933 | /* |
| 1934 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 1935 | * by looping over ->read() or ->write() manually. |
| 1936 | */ |
| 1937 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 1938 | struct iov_iter *iter) |
| 1939 | { |
| 1940 | ssize_t ret = 0; |
| 1941 | |
| 1942 | /* |
| 1943 | * Don't support polled IO through this interface, and we can't |
| 1944 | * support non-blocking either. For the latter, this just causes |
| 1945 | * the kiocb to be handled from an async context. |
| 1946 | */ |
| 1947 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1948 | return -EOPNOTSUPP; |
| 1949 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 1950 | return -EAGAIN; |
| 1951 | |
| 1952 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 1953 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1954 | ssize_t nr; |
| 1955 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 1956 | if (!iov_iter_is_bvec(iter)) { |
| 1957 | iovec = iov_iter_iovec(iter); |
| 1958 | } else { |
| 1959 | /* fixed buffers import bvec */ |
| 1960 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 1961 | + iter->iov_offset; |
| 1962 | iovec.iov_len = min(iter->count, |
| 1963 | iter->bvec->bv_len - iter->iov_offset); |
| 1964 | } |
| 1965 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1966 | if (rw == READ) { |
| 1967 | nr = file->f_op->read(file, iovec.iov_base, |
| 1968 | iovec.iov_len, &kiocb->ki_pos); |
| 1969 | } else { |
| 1970 | nr = file->f_op->write(file, iovec.iov_base, |
| 1971 | iovec.iov_len, &kiocb->ki_pos); |
| 1972 | } |
| 1973 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 1974 | if (iov_iter_is_bvec(iter)) |
| 1975 | kunmap(iter->bvec->bv_page); |
| 1976 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 1977 | if (nr < 0) { |
| 1978 | if (!ret) |
| 1979 | ret = nr; |
| 1980 | break; |
| 1981 | } |
| 1982 | ret += nr; |
| 1983 | if (nr != iovec.iov_len) |
| 1984 | break; |
| 1985 | iov_iter_advance(iter, nr); |
| 1986 | } |
| 1987 | |
| 1988 | return ret; |
| 1989 | } |
| 1990 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 1991 | 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] | 1992 | struct iovec *iovec, struct iovec *fast_iov, |
| 1993 | struct iov_iter *iter) |
| 1994 | { |
| 1995 | req->io->rw.nr_segs = iter->nr_segs; |
| 1996 | req->io->rw.size = io_size; |
| 1997 | req->io->rw.iov = iovec; |
| 1998 | if (!req->io->rw.iov) { |
| 1999 | req->io->rw.iov = req->io->rw.fast_iov; |
| 2000 | memcpy(req->io->rw.iov, fast_iov, |
| 2001 | sizeof(struct iovec) * iter->nr_segs); |
| 2002 | } |
| 2003 | } |
| 2004 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2005 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2006 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2007 | if (!io_op_defs[req->opcode].async_ctx) |
| 2008 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2009 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2010 | return req->io == NULL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | static void io_rw_async(struct io_wq_work **workptr) |
| 2014 | { |
| 2015 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2016 | struct iovec *iov = NULL; |
| 2017 | |
| 2018 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2019 | iov = req->io->rw.iov; |
| 2020 | io_wq_submit_work(workptr); |
| 2021 | kfree(iov); |
| 2022 | } |
| 2023 | |
| 2024 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2025 | struct iovec *iovec, struct iovec *fast_iov, |
| 2026 | struct iov_iter *iter) |
| 2027 | { |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2028 | if (req->opcode == IORING_OP_READ_FIXED || |
| 2029 | req->opcode == IORING_OP_WRITE_FIXED) |
| 2030 | return 0; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2031 | if (!req->io && io_alloc_async_ctx(req)) |
| 2032 | return -ENOMEM; |
| 2033 | |
| 2034 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2035 | req->work.func = io_rw_async; |
| 2036 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2037 | } |
| 2038 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2039 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2040 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2041 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2042 | struct io_async_ctx *io; |
| 2043 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2044 | ssize_t ret; |
| 2045 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2046 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2047 | if (ret) |
| 2048 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2049 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2050 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2051 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2052 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2053 | if (!req->io) |
| 2054 | return 0; |
| 2055 | |
| 2056 | io = req->io; |
| 2057 | io->rw.iov = io->rw.fast_iov; |
| 2058 | req->io = NULL; |
| 2059 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter); |
| 2060 | req->io = io; |
| 2061 | if (ret < 0) |
| 2062 | return ret; |
| 2063 | |
| 2064 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2065 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2068 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2069 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2070 | { |
| 2071 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2072 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2073 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2074 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2075 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2076 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2077 | ret = io_import_iovec(READ, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2078 | if (ret < 0) |
| 2079 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2080 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2081 | /* Ensure we clear previously set non-block flag */ |
| 2082 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2083 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2084 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2085 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2086 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2087 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2088 | req->result = io_size; |
| 2089 | |
| 2090 | /* |
| 2091 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2092 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2093 | */ |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2094 | if (force_nonblock && !io_file_supports_async(req->file)) { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2095 | req->flags |= REQ_F_MUST_PUNT; |
| 2096 | goto copy_iov; |
| 2097 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2098 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2099 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2100 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2101 | if (!ret) { |
| 2102 | ssize_t ret2; |
| 2103 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2104 | if (req->file->f_op->read_iter) |
| 2105 | ret2 = call_read_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2106 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2107 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2108 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 2109 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2110 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2111 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2112 | } else { |
| 2113 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2114 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2115 | inline_vecs, &iter); |
| 2116 | if (ret) |
| 2117 | goto out_free; |
| 2118 | return -EAGAIN; |
| 2119 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2120 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2121 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2122 | if (!io_wq_current_is_worker()) |
| 2123 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2124 | return ret; |
| 2125 | } |
| 2126 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2127 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2128 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2129 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2130 | struct io_async_ctx *io; |
| 2131 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2132 | ssize_t ret; |
| 2133 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2134 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2135 | if (ret) |
| 2136 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2137 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2138 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 2139 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2140 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2141 | if (!req->io) |
| 2142 | return 0; |
| 2143 | |
| 2144 | io = req->io; |
| 2145 | io->rw.iov = io->rw.fast_iov; |
| 2146 | req->io = NULL; |
| 2147 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter); |
| 2148 | req->io = io; |
| 2149 | if (ret < 0) |
| 2150 | return ret; |
| 2151 | |
| 2152 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2153 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2154 | } |
| 2155 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2156 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2157 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2158 | { |
| 2159 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2160 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2161 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2162 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2163 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2164 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2165 | ret = io_import_iovec(WRITE, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2166 | if (ret < 0) |
| 2167 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2168 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2169 | /* Ensure we clear previously set non-block flag */ |
| 2170 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2171 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2172 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2173 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2174 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2175 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2176 | req->result = io_size; |
| 2177 | |
| 2178 | /* |
| 2179 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2180 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2181 | */ |
| 2182 | if (force_nonblock && !io_file_supports_async(req->file)) { |
| 2183 | req->flags |= REQ_F_MUST_PUNT; |
| 2184 | goto copy_iov; |
| 2185 | } |
| 2186 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2187 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 2188 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 2189 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2190 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2191 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2192 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2193 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2194 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2195 | ssize_t ret2; |
| 2196 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2197 | /* |
| 2198 | * Open-code file_start_write here to grab freeze protection, |
| 2199 | * which will be released by another thread in |
| 2200 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 2201 | * released so that it doesn't complain about the held lock when |
| 2202 | * we return to userspace. |
| 2203 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2204 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2205 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2206 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2207 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2208 | SB_FREEZE_WRITE); |
| 2209 | } |
| 2210 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2211 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2212 | if (req->file->f_op->write_iter) |
| 2213 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2214 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2215 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2216 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2217 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2218 | } else { |
| 2219 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2220 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2221 | inline_vecs, &iter); |
| 2222 | if (ret) |
| 2223 | goto out_free; |
| 2224 | return -EAGAIN; |
| 2225 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2226 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2227 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2228 | if (!io_wq_current_is_worker()) |
| 2229 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2230 | return ret; |
| 2231 | } |
| 2232 | |
| 2233 | /* |
| 2234 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 2235 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2236 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2237 | { |
| 2238 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2239 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2240 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2241 | return -EINVAL; |
| 2242 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2243 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2244 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2245 | return 0; |
| 2246 | } |
| 2247 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2248 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2249 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2250 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2251 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2252 | if (!req->file) |
| 2253 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2254 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2255 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2256 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2257 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2258 | return -EINVAL; |
| 2259 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2260 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 2261 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 2262 | return -EINVAL; |
| 2263 | |
| 2264 | req->sync.off = READ_ONCE(sqe->off); |
| 2265 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2266 | return 0; |
| 2267 | } |
| 2268 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2269 | static bool io_req_cancelled(struct io_kiocb *req) |
| 2270 | { |
| 2271 | if (req->work.flags & IO_WQ_WORK_CANCEL) { |
| 2272 | req_set_fail_links(req); |
| 2273 | io_cqring_add_event(req, -ECANCELED); |
| 2274 | io_put_req(req); |
| 2275 | return true; |
| 2276 | } |
| 2277 | |
| 2278 | return false; |
| 2279 | } |
| 2280 | |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2281 | static void io_link_work_cb(struct io_wq_work **workptr) |
| 2282 | { |
| 2283 | struct io_wq_work *work = *workptr; |
| 2284 | struct io_kiocb *link = work->data; |
| 2285 | |
| 2286 | io_queue_linked_timeout(link); |
| 2287 | work->func = io_wq_submit_work; |
| 2288 | } |
| 2289 | |
| 2290 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) |
| 2291 | { |
| 2292 | struct io_kiocb *link; |
| 2293 | |
| 2294 | io_prep_async_work(nxt, &link); |
| 2295 | *workptr = &nxt->work; |
| 2296 | if (link) { |
| 2297 | nxt->work.flags |= IO_WQ_WORK_CB; |
| 2298 | nxt->work.func = io_link_work_cb; |
| 2299 | nxt->work.data = link; |
| 2300 | } |
| 2301 | } |
| 2302 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2303 | static void io_fsync_finish(struct io_wq_work **workptr) |
| 2304 | { |
| 2305 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2306 | loff_t end = req->sync.off + req->sync.len; |
| 2307 | struct io_kiocb *nxt = NULL; |
| 2308 | int ret; |
| 2309 | |
| 2310 | if (io_req_cancelled(req)) |
| 2311 | return; |
| 2312 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2313 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2314 | end > 0 ? end : LLONG_MAX, |
| 2315 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 2316 | if (ret < 0) |
| 2317 | req_set_fail_links(req); |
| 2318 | io_cqring_add_event(req, ret); |
| 2319 | io_put_req_find_next(req, &nxt); |
| 2320 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2321 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2322 | } |
| 2323 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2324 | static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2325 | bool force_nonblock) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2326 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2327 | struct io_wq_work *work, *old_work; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2328 | |
| 2329 | /* fsync always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2330 | if (force_nonblock) { |
| 2331 | io_put_req(req); |
| 2332 | req->work.func = io_fsync_finish; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2333 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2334 | } |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2335 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2336 | work = old_work = &req->work; |
| 2337 | io_fsync_finish(&work); |
| 2338 | if (work && work != old_work) |
| 2339 | *nxt = container_of(work, struct io_kiocb, work); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2340 | return 0; |
| 2341 | } |
| 2342 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2343 | static void io_fallocate_finish(struct io_wq_work **workptr) |
| 2344 | { |
| 2345 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2346 | struct io_kiocb *nxt = NULL; |
| 2347 | int ret; |
| 2348 | |
| 2349 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 2350 | req->sync.len); |
| 2351 | if (ret < 0) |
| 2352 | req_set_fail_links(req); |
| 2353 | io_cqring_add_event(req, ret); |
| 2354 | io_put_req_find_next(req, &nxt); |
| 2355 | if (nxt) |
| 2356 | io_wq_assign_next(workptr, nxt); |
| 2357 | } |
| 2358 | |
| 2359 | static int io_fallocate_prep(struct io_kiocb *req, |
| 2360 | const struct io_uring_sqe *sqe) |
| 2361 | { |
| 2362 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 2363 | return -EINVAL; |
| 2364 | |
| 2365 | req->sync.off = READ_ONCE(sqe->off); |
| 2366 | req->sync.len = READ_ONCE(sqe->addr); |
| 2367 | req->sync.mode = READ_ONCE(sqe->len); |
| 2368 | return 0; |
| 2369 | } |
| 2370 | |
| 2371 | static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2372 | bool force_nonblock) |
| 2373 | { |
| 2374 | struct io_wq_work *work, *old_work; |
| 2375 | |
| 2376 | /* fallocate always requiring blocking context */ |
| 2377 | if (force_nonblock) { |
| 2378 | io_put_req(req); |
| 2379 | req->work.func = io_fallocate_finish; |
| 2380 | return -EAGAIN; |
| 2381 | } |
| 2382 | |
| 2383 | work = old_work = &req->work; |
| 2384 | io_fallocate_finish(&work); |
| 2385 | if (work && work != old_work) |
| 2386 | *nxt = container_of(work, struct io_kiocb, work); |
| 2387 | |
| 2388 | return 0; |
| 2389 | } |
| 2390 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2391 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2392 | { |
| 2393 | int ret; |
| 2394 | |
| 2395 | if (sqe->ioprio || sqe->buf_index) |
| 2396 | return -EINVAL; |
| 2397 | |
| 2398 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2399 | req->open.mode = READ_ONCE(sqe->len); |
| 2400 | req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2401 | req->open.flags = READ_ONCE(sqe->open_flags); |
| 2402 | |
| 2403 | req->open.filename = getname(req->open.fname); |
| 2404 | if (IS_ERR(req->open.filename)) { |
| 2405 | ret = PTR_ERR(req->open.filename); |
| 2406 | req->open.filename = NULL; |
| 2407 | return ret; |
| 2408 | } |
| 2409 | |
| 2410 | return 0; |
| 2411 | } |
| 2412 | |
| 2413 | static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2414 | bool force_nonblock) |
| 2415 | { |
| 2416 | struct open_flags op; |
| 2417 | struct open_how how; |
| 2418 | struct file *file; |
| 2419 | int ret; |
| 2420 | |
| 2421 | if (force_nonblock) { |
| 2422 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 2423 | return -EAGAIN; |
| 2424 | } |
| 2425 | |
| 2426 | how = build_open_how(req->open.flags, req->open.mode); |
| 2427 | ret = build_open_flags(&how, &op); |
| 2428 | if (ret) |
| 2429 | goto err; |
| 2430 | |
| 2431 | ret = get_unused_fd_flags(how.flags); |
| 2432 | if (ret < 0) |
| 2433 | goto err; |
| 2434 | |
| 2435 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 2436 | if (IS_ERR(file)) { |
| 2437 | put_unused_fd(ret); |
| 2438 | ret = PTR_ERR(file); |
| 2439 | } else { |
| 2440 | fsnotify_open(file); |
| 2441 | fd_install(ret, file); |
| 2442 | } |
| 2443 | err: |
| 2444 | putname(req->open.filename); |
| 2445 | if (ret < 0) |
| 2446 | req_set_fail_links(req); |
| 2447 | io_cqring_add_event(req, ret); |
| 2448 | io_put_req_find_next(req, nxt); |
| 2449 | return 0; |
| 2450 | } |
| 2451 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame^] | 2452 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2453 | { |
| 2454 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 2455 | return -EINVAL; |
| 2456 | |
| 2457 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 2458 | req->fadvise.len = READ_ONCE(sqe->len); |
| 2459 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2460 | return 0; |
| 2461 | } |
| 2462 | |
| 2463 | static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2464 | bool force_nonblock) |
| 2465 | { |
| 2466 | struct io_fadvise *fa = &req->fadvise; |
| 2467 | int ret; |
| 2468 | |
| 2469 | /* DONTNEED may block, others _should_ not */ |
| 2470 | if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock) |
| 2471 | return -EAGAIN; |
| 2472 | |
| 2473 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 2474 | if (ret < 0) |
| 2475 | req_set_fail_links(req); |
| 2476 | io_cqring_add_event(req, ret); |
| 2477 | io_put_req_find_next(req, nxt); |
| 2478 | return 0; |
| 2479 | } |
| 2480 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2481 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2482 | { |
| 2483 | unsigned lookup_flags; |
| 2484 | int ret; |
| 2485 | |
| 2486 | if (sqe->ioprio || sqe->buf_index) |
| 2487 | return -EINVAL; |
| 2488 | |
| 2489 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2490 | req->open.mask = READ_ONCE(sqe->len); |
| 2491 | req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2492 | req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 2493 | req->open.flags = READ_ONCE(sqe->statx_flags); |
| 2494 | |
| 2495 | if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.flags)) |
| 2496 | return -EINVAL; |
| 2497 | |
| 2498 | req->open.filename = getname_flags(req->open.fname, lookup_flags, NULL); |
| 2499 | if (IS_ERR(req->open.filename)) { |
| 2500 | ret = PTR_ERR(req->open.filename); |
| 2501 | req->open.filename = NULL; |
| 2502 | return ret; |
| 2503 | } |
| 2504 | |
| 2505 | return 0; |
| 2506 | } |
| 2507 | |
| 2508 | static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2509 | bool force_nonblock) |
| 2510 | { |
| 2511 | struct io_open *ctx = &req->open; |
| 2512 | unsigned lookup_flags; |
| 2513 | struct path path; |
| 2514 | struct kstat stat; |
| 2515 | int ret; |
| 2516 | |
| 2517 | if (force_nonblock) |
| 2518 | return -EAGAIN; |
| 2519 | |
| 2520 | if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->flags)) |
| 2521 | return -EINVAL; |
| 2522 | |
| 2523 | retry: |
| 2524 | /* filename_lookup() drops it, keep a reference */ |
| 2525 | ctx->filename->refcnt++; |
| 2526 | |
| 2527 | ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path, |
| 2528 | NULL); |
| 2529 | if (ret) |
| 2530 | goto err; |
| 2531 | |
| 2532 | ret = vfs_getattr(&path, &stat, ctx->mask, ctx->flags); |
| 2533 | path_put(&path); |
| 2534 | if (retry_estale(ret, lookup_flags)) { |
| 2535 | lookup_flags |= LOOKUP_REVAL; |
| 2536 | goto retry; |
| 2537 | } |
| 2538 | if (!ret) |
| 2539 | ret = cp_statx(&stat, ctx->buffer); |
| 2540 | err: |
| 2541 | putname(ctx->filename); |
| 2542 | if (ret < 0) |
| 2543 | req_set_fail_links(req); |
| 2544 | io_cqring_add_event(req, ret); |
| 2545 | io_put_req_find_next(req, nxt); |
| 2546 | return 0; |
| 2547 | } |
| 2548 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2549 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2550 | { |
| 2551 | /* |
| 2552 | * If we queue this for async, it must not be cancellable. That would |
| 2553 | * leave the 'file' in an undeterminate state. |
| 2554 | */ |
| 2555 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 2556 | |
| 2557 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 2558 | sqe->rw_flags || sqe->buf_index) |
| 2559 | return -EINVAL; |
| 2560 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2561 | return -EINVAL; |
| 2562 | |
| 2563 | req->close.fd = READ_ONCE(sqe->fd); |
| 2564 | if (req->file->f_op == &io_uring_fops || |
| 2565 | req->close.fd == req->ring_fd) |
| 2566 | return -EBADF; |
| 2567 | |
| 2568 | return 0; |
| 2569 | } |
| 2570 | |
| 2571 | static void io_close_finish(struct io_wq_work **workptr) |
| 2572 | { |
| 2573 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2574 | struct io_kiocb *nxt = NULL; |
| 2575 | |
| 2576 | /* Invoked with files, we need to do the close */ |
| 2577 | if (req->work.files) { |
| 2578 | int ret; |
| 2579 | |
| 2580 | ret = filp_close(req->close.put_file, req->work.files); |
| 2581 | if (ret < 0) { |
| 2582 | req_set_fail_links(req); |
| 2583 | } |
| 2584 | io_cqring_add_event(req, ret); |
| 2585 | } |
| 2586 | |
| 2587 | fput(req->close.put_file); |
| 2588 | |
| 2589 | /* we bypassed the re-issue, drop the submission reference */ |
| 2590 | io_put_req(req); |
| 2591 | io_put_req_find_next(req, &nxt); |
| 2592 | if (nxt) |
| 2593 | io_wq_assign_next(workptr, nxt); |
| 2594 | } |
| 2595 | |
| 2596 | static int io_close(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2597 | bool force_nonblock) |
| 2598 | { |
| 2599 | int ret; |
| 2600 | |
| 2601 | req->close.put_file = NULL; |
| 2602 | ret = __close_fd_get_file(req->close.fd, &req->close.put_file); |
| 2603 | if (ret < 0) |
| 2604 | return ret; |
| 2605 | |
| 2606 | /* if the file has a flush method, be safe and punt to async */ |
| 2607 | if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) { |
| 2608 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 2609 | goto eagain; |
| 2610 | } |
| 2611 | |
| 2612 | /* |
| 2613 | * No ->flush(), safely close from here and just punt the |
| 2614 | * fput() to async context. |
| 2615 | */ |
| 2616 | ret = filp_close(req->close.put_file, current->files); |
| 2617 | |
| 2618 | if (ret < 0) |
| 2619 | req_set_fail_links(req); |
| 2620 | io_cqring_add_event(req, ret); |
| 2621 | |
| 2622 | if (io_wq_current_is_worker()) { |
| 2623 | struct io_wq_work *old_work, *work; |
| 2624 | |
| 2625 | old_work = work = &req->work; |
| 2626 | io_close_finish(&work); |
| 2627 | if (work && work != old_work) |
| 2628 | *nxt = container_of(work, struct io_kiocb, work); |
| 2629 | return 0; |
| 2630 | } |
| 2631 | |
| 2632 | eagain: |
| 2633 | req->work.func = io_close_finish; |
| 2634 | return -EAGAIN; |
| 2635 | } |
| 2636 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2637 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2638 | { |
| 2639 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2640 | |
| 2641 | if (!req->file) |
| 2642 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2643 | |
| 2644 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2645 | return -EINVAL; |
| 2646 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 2647 | return -EINVAL; |
| 2648 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2649 | req->sync.off = READ_ONCE(sqe->off); |
| 2650 | req->sync.len = READ_ONCE(sqe->len); |
| 2651 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2652 | return 0; |
| 2653 | } |
| 2654 | |
| 2655 | static void io_sync_file_range_finish(struct io_wq_work **workptr) |
| 2656 | { |
| 2657 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2658 | struct io_kiocb *nxt = NULL; |
| 2659 | int ret; |
| 2660 | |
| 2661 | if (io_req_cancelled(req)) |
| 2662 | return; |
| 2663 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2664 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2665 | req->sync.flags); |
| 2666 | if (ret < 0) |
| 2667 | req_set_fail_links(req); |
| 2668 | io_cqring_add_event(req, ret); |
| 2669 | io_put_req_find_next(req, &nxt); |
| 2670 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2671 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2672 | } |
| 2673 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2674 | 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] | 2675 | bool force_nonblock) |
| 2676 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2677 | struct io_wq_work *work, *old_work; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2678 | |
| 2679 | /* sync_file_range always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2680 | if (force_nonblock) { |
| 2681 | io_put_req(req); |
| 2682 | req->work.func = io_sync_file_range_finish; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2683 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2684 | } |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2685 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2686 | work = old_work = &req->work; |
| 2687 | io_sync_file_range_finish(&work); |
| 2688 | if (work && work != old_work) |
| 2689 | *nxt = container_of(work, struct io_kiocb, work); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2690 | return 0; |
| 2691 | } |
| 2692 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2693 | #if defined(CONFIG_NET) |
| 2694 | static void io_sendrecv_async(struct io_wq_work **workptr) |
| 2695 | { |
| 2696 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2697 | struct iovec *iov = NULL; |
| 2698 | |
| 2699 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2700 | iov = req->io->msg.iov; |
| 2701 | io_wq_submit_work(workptr); |
| 2702 | kfree(iov); |
| 2703 | } |
| 2704 | #endif |
| 2705 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2706 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 2707 | { |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2708 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2709 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2710 | struct io_async_ctx *io = req->io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2711 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2712 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 2713 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2714 | |
| 2715 | if (!io) |
| 2716 | return 0; |
| 2717 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 2718 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2719 | return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2720 | &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2721 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2722 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2723 | #endif |
| 2724 | } |
| 2725 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2726 | static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2727 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2728 | { |
| 2729 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2730 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2731 | struct socket *sock; |
| 2732 | int ret; |
| 2733 | |
| 2734 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2735 | return -EINVAL; |
| 2736 | |
| 2737 | sock = sock_from_file(req->file, &ret); |
| 2738 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2739 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2740 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2741 | unsigned flags; |
| 2742 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2743 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2744 | kmsg = &req->io->msg; |
| 2745 | kmsg->msg.msg_name = &addr; |
| 2746 | /* if iov is set, it's allocated already */ |
| 2747 | if (!kmsg->iov) |
| 2748 | kmsg->iov = kmsg->fast_iov; |
| 2749 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2750 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2751 | struct io_sr_msg *sr = &req->sr_msg; |
| 2752 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2753 | kmsg = &io.msg; |
| 2754 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2755 | |
| 2756 | io.msg.iov = io.msg.fast_iov; |
| 2757 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 2758 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2759 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2760 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2761 | } |
| 2762 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2763 | flags = req->sr_msg.msg_flags; |
| 2764 | if (flags & MSG_DONTWAIT) |
| 2765 | req->flags |= REQ_F_NOWAIT; |
| 2766 | else if (force_nonblock) |
| 2767 | flags |= MSG_DONTWAIT; |
| 2768 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2769 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2770 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2771 | if (req->io) |
| 2772 | return -EAGAIN; |
| 2773 | if (io_alloc_async_ctx(req)) |
| 2774 | return -ENOMEM; |
| 2775 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 2776 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2777 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2778 | } |
| 2779 | if (ret == -ERESTARTSYS) |
| 2780 | ret = -EINTR; |
| 2781 | } |
| 2782 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2783 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2784 | kfree(kmsg->iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2785 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2786 | if (ret < 0) |
| 2787 | req_set_fail_links(req); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2788 | io_put_req_find_next(req, nxt); |
| 2789 | return 0; |
| 2790 | #else |
| 2791 | return -EOPNOTSUPP; |
| 2792 | #endif |
| 2793 | } |
| 2794 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2795 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 2796 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2797 | { |
| 2798 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2799 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2800 | struct io_async_ctx *io = req->io; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2801 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2802 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 2803 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2804 | |
| 2805 | if (!io) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2806 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2807 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 2808 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2809 | return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2810 | &io->msg.uaddr, &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2811 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2812 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2813 | #endif |
| 2814 | } |
| 2815 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2816 | static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2817 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2818 | { |
| 2819 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2820 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 2821 | struct socket *sock; |
| 2822 | int ret; |
| 2823 | |
| 2824 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2825 | return -EINVAL; |
| 2826 | |
| 2827 | sock = sock_from_file(req->file, &ret); |
| 2828 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2829 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2830 | struct sockaddr_storage addr; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 2831 | unsigned flags; |
| 2832 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2833 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2834 | kmsg = &req->io->msg; |
| 2835 | kmsg->msg.msg_name = &addr; |
| 2836 | /* if iov is set, it's allocated already */ |
| 2837 | if (!kmsg->iov) |
| 2838 | kmsg->iov = kmsg->fast_iov; |
| 2839 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2840 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2841 | struct io_sr_msg *sr = &req->sr_msg; |
| 2842 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2843 | kmsg = &io.msg; |
| 2844 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2845 | |
| 2846 | io.msg.iov = io.msg.fast_iov; |
| 2847 | ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 2848 | sr->msg_flags, &io.msg.uaddr, |
| 2849 | &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2850 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2851 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2852 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 2853 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2854 | flags = req->sr_msg.msg_flags; |
| 2855 | if (flags & MSG_DONTWAIT) |
| 2856 | req->flags |= REQ_F_NOWAIT; |
| 2857 | else if (force_nonblock) |
| 2858 | flags |= MSG_DONTWAIT; |
| 2859 | |
| 2860 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 2861 | kmsg->uaddr, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2862 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2863 | if (req->io) |
| 2864 | return -EAGAIN; |
| 2865 | if (io_alloc_async_ctx(req)) |
| 2866 | return -ENOMEM; |
| 2867 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 2868 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2869 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2870 | } |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 2871 | if (ret == -ERESTARTSYS) |
| 2872 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 2873 | } |
| 2874 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2875 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2876 | kfree(kmsg->iov); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2877 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2878 | if (ret < 0) |
| 2879 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 2880 | io_put_req_find_next(req, nxt); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 2881 | return 0; |
| 2882 | #else |
| 2883 | return -EOPNOTSUPP; |
| 2884 | #endif |
| 2885 | } |
| 2886 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2887 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 2888 | { |
| 2889 | #if defined(CONFIG_NET) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2890 | struct io_accept *accept = &req->accept; |
| 2891 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 2892 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 2893 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 2894 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 2895 | return -EINVAL; |
| 2896 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 2897 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2898 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2899 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2900 | return 0; |
| 2901 | #else |
| 2902 | return -EOPNOTSUPP; |
| 2903 | #endif |
| 2904 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 2905 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2906 | #if defined(CONFIG_NET) |
| 2907 | static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2908 | bool force_nonblock) |
| 2909 | { |
| 2910 | struct io_accept *accept = &req->accept; |
| 2911 | unsigned file_flags; |
| 2912 | int ret; |
| 2913 | |
| 2914 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 2915 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
| 2916 | accept->addr_len, accept->flags); |
| 2917 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 2918 | return -EAGAIN; |
Jens Axboe | 8e3cca1 | 2019-11-09 19:52:33 -0700 | [diff] [blame] | 2919 | if (ret == -ERESTARTSYS) |
| 2920 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2921 | if (ret < 0) |
| 2922 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2923 | io_cqring_add_event(req, ret); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 2924 | io_put_req_find_next(req, nxt); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 2925 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2926 | } |
| 2927 | |
| 2928 | static void io_accept_finish(struct io_wq_work **workptr) |
| 2929 | { |
| 2930 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2931 | struct io_kiocb *nxt = NULL; |
| 2932 | |
| 2933 | if (io_req_cancelled(req)) |
| 2934 | return; |
| 2935 | __io_accept(req, &nxt, false); |
| 2936 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2937 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2938 | } |
| 2939 | #endif |
| 2940 | |
| 2941 | static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2942 | bool force_nonblock) |
| 2943 | { |
| 2944 | #if defined(CONFIG_NET) |
| 2945 | int ret; |
| 2946 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2947 | ret = __io_accept(req, nxt, force_nonblock); |
| 2948 | if (ret == -EAGAIN && force_nonblock) { |
| 2949 | req->work.func = io_accept_finish; |
| 2950 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 2951 | io_put_req(req); |
| 2952 | return -EAGAIN; |
| 2953 | } |
| 2954 | return 0; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 2955 | #else |
| 2956 | return -EOPNOTSUPP; |
| 2957 | #endif |
| 2958 | } |
| 2959 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2960 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 2961 | { |
| 2962 | #if defined(CONFIG_NET) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2963 | struct io_connect *conn = &req->connect; |
| 2964 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 2965 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 2966 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 2967 | return -EINVAL; |
| 2968 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 2969 | return -EINVAL; |
| 2970 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2971 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2972 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 2973 | |
| 2974 | if (!io) |
| 2975 | return 0; |
| 2976 | |
| 2977 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 2978 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 2979 | #else |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 2980 | return -EOPNOTSUPP; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 2981 | #endif |
| 2982 | } |
| 2983 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2984 | static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2985 | bool force_nonblock) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 2986 | { |
| 2987 | #if defined(CONFIG_NET) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 2988 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 2989 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 2990 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 2991 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 2992 | if (req->io) { |
| 2993 | io = req->io; |
| 2994 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2995 | ret = move_addr_to_kernel(req->connect.addr, |
| 2996 | req->connect.addr_len, |
| 2997 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 2998 | if (ret) |
| 2999 | goto out; |
| 3000 | io = &__io; |
| 3001 | } |
| 3002 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3003 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3004 | |
| 3005 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 3006 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 3007 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3008 | if (req->io) |
| 3009 | return -EAGAIN; |
| 3010 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3011 | ret = -ENOMEM; |
| 3012 | goto out; |
| 3013 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3014 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3015 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3016 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3017 | if (ret == -ERESTARTSYS) |
| 3018 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3019 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3020 | if (ret < 0) |
| 3021 | req_set_fail_links(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3022 | io_cqring_add_event(req, ret); |
| 3023 | io_put_req_find_next(req, nxt); |
| 3024 | return 0; |
| 3025 | #else |
| 3026 | return -EOPNOTSUPP; |
| 3027 | #endif |
| 3028 | } |
| 3029 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3030 | static void io_poll_remove_one(struct io_kiocb *req) |
| 3031 | { |
| 3032 | struct io_poll_iocb *poll = &req->poll; |
| 3033 | |
| 3034 | spin_lock(&poll->head->lock); |
| 3035 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3036 | if (!list_empty(&poll->wait.entry)) { |
| 3037 | list_del_init(&poll->wait.entry); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3038 | io_queue_async_work(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3039 | } |
| 3040 | spin_unlock(&poll->head->lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3041 | hash_del(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 3045 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3046 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3047 | struct io_kiocb *req; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3048 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3049 | |
| 3050 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3051 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 3052 | struct hlist_head *list; |
| 3053 | |
| 3054 | list = &ctx->cancel_hash[i]; |
| 3055 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
| 3056 | io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3057 | } |
| 3058 | spin_unlock_irq(&ctx->completion_lock); |
| 3059 | } |
| 3060 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3061 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 3062 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3063 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3064 | struct io_kiocb *req; |
| 3065 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3066 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 3067 | hlist_for_each_entry(req, list, hash_node) { |
| 3068 | if (sqe_addr == req->user_data) { |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3069 | io_poll_remove_one(req); |
| 3070 | return 0; |
| 3071 | } |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3072 | } |
| 3073 | |
| 3074 | return -ENOENT; |
| 3075 | } |
| 3076 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3077 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 3078 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3079 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3080 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3081 | return -EINVAL; |
| 3082 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3083 | sqe->poll_events) |
| 3084 | return -EINVAL; |
| 3085 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3086 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3087 | return 0; |
| 3088 | } |
| 3089 | |
| 3090 | /* |
| 3091 | * Find a running poll command that matches one specified in sqe->addr, |
| 3092 | * and remove it if found. |
| 3093 | */ |
| 3094 | static int io_poll_remove(struct io_kiocb *req) |
| 3095 | { |
| 3096 | struct io_ring_ctx *ctx = req->ctx; |
| 3097 | u64 addr; |
| 3098 | int ret; |
| 3099 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3100 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3101 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3102 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3103 | spin_unlock_irq(&ctx->completion_lock); |
| 3104 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3105 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3106 | if (ret < 0) |
| 3107 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3108 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3109 | return 0; |
| 3110 | } |
| 3111 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3112 | 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] | 3113 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3114 | struct io_ring_ctx *ctx = req->ctx; |
| 3115 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3116 | req->poll.done = true; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3117 | if (error) |
| 3118 | io_cqring_fill_event(req, error); |
| 3119 | else |
| 3120 | io_cqring_fill_event(req, mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3121 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3122 | } |
| 3123 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3124 | static void io_poll_complete_work(struct io_wq_work **workptr) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3125 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3126 | struct io_wq_work *work = *workptr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3127 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 3128 | struct io_poll_iocb *poll = &req->poll; |
| 3129 | struct poll_table_struct pt = { ._key = poll->events }; |
| 3130 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3131 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3132 | __poll_t mask = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3133 | int ret = 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3134 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3135 | if (work->flags & IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3136 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3137 | ret = -ECANCELED; |
| 3138 | } else if (READ_ONCE(poll->canceled)) { |
| 3139 | ret = -ECANCELED; |
| 3140 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3141 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3142 | if (ret != -ECANCELED) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3143 | mask = vfs_poll(poll->file, &pt) & poll->events; |
| 3144 | |
| 3145 | /* |
| 3146 | * Note that ->ki_cancel callers also delete iocb from active_reqs after |
| 3147 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to |
| 3148 | * synchronize with them. In the cancellation case the list_del_init |
| 3149 | * itself is not actually needed, but harmless so we keep it in to |
| 3150 | * avoid further branches in the fast path. |
| 3151 | */ |
| 3152 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3153 | if (!mask && ret != -ECANCELED) { |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3154 | add_wait_queue(poll->head, &poll->wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3155 | spin_unlock_irq(&ctx->completion_lock); |
| 3156 | return; |
| 3157 | } |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3158 | hash_del(&req->hash_node); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3159 | io_poll_complete(req, mask, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3160 | spin_unlock_irq(&ctx->completion_lock); |
| 3161 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3162 | io_cqring_ev_posted(ctx); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3163 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3164 | if (ret < 0) |
| 3165 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3166 | io_put_req_find_next(req, &nxt); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3167 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3168 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3169 | } |
| 3170 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3171 | static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes) |
| 3172 | { |
| 3173 | void *reqs[IO_IOPOLL_BATCH]; |
| 3174 | struct io_kiocb *req, *tmp; |
| 3175 | int to_free = 0; |
| 3176 | |
| 3177 | spin_lock_irq(&ctx->completion_lock); |
| 3178 | llist_for_each_entry_safe(req, tmp, nodes, llist_node) { |
| 3179 | hash_del(&req->hash_node); |
| 3180 | io_poll_complete(req, req->result, 0); |
| 3181 | |
| 3182 | if (refcount_dec_and_test(&req->refs)) { |
| 3183 | if (io_req_multi_free(req)) { |
| 3184 | reqs[to_free++] = req; |
| 3185 | if (to_free == ARRAY_SIZE(reqs)) |
| 3186 | io_free_req_many(ctx, reqs, &to_free); |
| 3187 | } else { |
| 3188 | req->flags |= REQ_F_COMP_LOCKED; |
| 3189 | io_free_req(req); |
| 3190 | } |
| 3191 | } |
| 3192 | } |
| 3193 | spin_unlock_irq(&ctx->completion_lock); |
| 3194 | |
| 3195 | io_cqring_ev_posted(ctx); |
| 3196 | io_free_req_many(ctx, reqs, &to_free); |
| 3197 | } |
| 3198 | |
| 3199 | static void io_poll_flush(struct io_wq_work **workptr) |
| 3200 | { |
| 3201 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3202 | struct llist_node *nodes; |
| 3203 | |
| 3204 | nodes = llist_del_all(&req->ctx->poll_llist); |
| 3205 | if (nodes) |
| 3206 | __io_poll_flush(req->ctx, nodes); |
| 3207 | } |
| 3208 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3209 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 3210 | void *key) |
| 3211 | { |
Jens Axboe | e944475 | 2019-11-26 15:02:04 -0700 | [diff] [blame] | 3212 | struct io_poll_iocb *poll = wait->private; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3213 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); |
| 3214 | struct io_ring_ctx *ctx = req->ctx; |
| 3215 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3216 | |
| 3217 | /* for instances that support it check for an event match first: */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3218 | if (mask && !(mask & poll->events)) |
| 3219 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3220 | |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3221 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3222 | |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 3223 | /* |
| 3224 | * Run completion inline if we can. We're using trylock here because |
| 3225 | * we are violating the completion_lock -> poll wq lock ordering. |
| 3226 | * If we have a link timeout we're going to need the completion_lock |
| 3227 | * for finalizing the request, mark us as having grabbed that already. |
| 3228 | */ |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3229 | if (mask) { |
| 3230 | unsigned long flags; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3231 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3232 | if (llist_empty(&ctx->poll_llist) && |
| 3233 | spin_trylock_irqsave(&ctx->completion_lock, flags)) { |
| 3234 | hash_del(&req->hash_node); |
| 3235 | io_poll_complete(req, mask, 0); |
| 3236 | req->flags |= REQ_F_COMP_LOCKED; |
| 3237 | io_put_req(req); |
| 3238 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3239 | |
| 3240 | io_cqring_ev_posted(ctx); |
| 3241 | req = NULL; |
| 3242 | } else { |
| 3243 | req->result = mask; |
| 3244 | req->llist_node.next = NULL; |
| 3245 | /* if the list wasn't empty, we're done */ |
| 3246 | if (!llist_add(&req->llist_node, &ctx->poll_llist)) |
| 3247 | req = NULL; |
| 3248 | else |
| 3249 | req->work.func = io_poll_flush; |
| 3250 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3251 | } |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3252 | if (req) |
| 3253 | io_queue_async_work(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3254 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3255 | return 1; |
| 3256 | } |
| 3257 | |
| 3258 | struct io_poll_table { |
| 3259 | struct poll_table_struct pt; |
| 3260 | struct io_kiocb *req; |
| 3261 | int error; |
| 3262 | }; |
| 3263 | |
| 3264 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 3265 | struct poll_table_struct *p) |
| 3266 | { |
| 3267 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 3268 | |
| 3269 | if (unlikely(pt->req->poll.head)) { |
| 3270 | pt->error = -EINVAL; |
| 3271 | return; |
| 3272 | } |
| 3273 | |
| 3274 | pt->error = 0; |
| 3275 | pt->req->poll.head = head; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3276 | add_wait_queue(head, &pt->req->poll.wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3277 | } |
| 3278 | |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3279 | static void io_poll_req_insert(struct io_kiocb *req) |
| 3280 | { |
| 3281 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3282 | struct hlist_head *list; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3283 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3284 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 3285 | hlist_add_head(&req->hash_node, list); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3286 | } |
| 3287 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3288 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3289 | { |
| 3290 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3291 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3292 | |
| 3293 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3294 | return -EINVAL; |
| 3295 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 3296 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3297 | if (!poll->file) |
| 3298 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3299 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3300 | events = READ_ONCE(sqe->poll_events); |
| 3301 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3302 | return 0; |
| 3303 | } |
| 3304 | |
| 3305 | static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3306 | { |
| 3307 | struct io_poll_iocb *poll = &req->poll; |
| 3308 | struct io_ring_ctx *ctx = req->ctx; |
| 3309 | struct io_poll_table ipt; |
| 3310 | bool cancel = false; |
| 3311 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3312 | |
| 3313 | INIT_IO_WORK(&req->work, io_poll_complete_work); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3314 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3315 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3316 | poll->head = NULL; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3317 | poll->done = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3318 | poll->canceled = false; |
| 3319 | |
| 3320 | ipt.pt._qproc = io_poll_queue_proc; |
| 3321 | ipt.pt._key = poll->events; |
| 3322 | ipt.req = req; |
| 3323 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ |
| 3324 | |
| 3325 | /* initialized the list so that we can do list_empty checks */ |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3326 | INIT_LIST_HEAD(&poll->wait.entry); |
| 3327 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); |
| 3328 | poll->wait.private = poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3329 | |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 3330 | INIT_LIST_HEAD(&req->list); |
| 3331 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3332 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3333 | |
| 3334 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3335 | if (likely(poll->head)) { |
| 3336 | spin_lock(&poll->head->lock); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3337 | if (unlikely(list_empty(&poll->wait.entry))) { |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3338 | if (ipt.error) |
| 3339 | cancel = true; |
| 3340 | ipt.error = 0; |
| 3341 | mask = 0; |
| 3342 | } |
| 3343 | if (mask || ipt.error) |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3344 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3345 | else if (cancel) |
| 3346 | WRITE_ONCE(poll->canceled, true); |
| 3347 | else if (!poll->done) /* actually waiting for an event */ |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3348 | io_poll_req_insert(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3349 | spin_unlock(&poll->head->lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3350 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3351 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3352 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3353 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3354 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3355 | spin_unlock_irq(&ctx->completion_lock); |
| 3356 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3357 | if (mask) { |
| 3358 | io_cqring_ev_posted(ctx); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3359 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3360 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3361 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3362 | } |
| 3363 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3364 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 3365 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3366 | struct io_timeout_data *data = container_of(timer, |
| 3367 | struct io_timeout_data, timer); |
| 3368 | struct io_kiocb *req = data->req; |
| 3369 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3370 | unsigned long flags; |
| 3371 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3372 | atomic_inc(&ctx->cq_timeouts); |
| 3373 | |
| 3374 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3375 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3376 | * We could be racing with timeout deletion. If the list is empty, |
| 3377 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3378 | */ |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3379 | if (!list_empty(&req->list)) { |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3380 | struct io_kiocb *prev; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3381 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3382 | /* |
| 3383 | * Adjust the reqs sequence before the current one because it |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 3384 | * will consume a slot in the cq_ring and the cq_tail |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3385 | * pointer will be increased, otherwise other timeout reqs may |
| 3386 | * return in advance without waiting for enough wait_nr. |
| 3387 | */ |
| 3388 | prev = req; |
| 3389 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 3390 | prev->sequence++; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3391 | list_del_init(&req->list); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3392 | } |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3393 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3394 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3395 | io_commit_cqring(ctx); |
| 3396 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3397 | |
| 3398 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3399 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3400 | io_put_req(req); |
| 3401 | return HRTIMER_NORESTART; |
| 3402 | } |
| 3403 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3404 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 3405 | { |
| 3406 | struct io_kiocb *req; |
| 3407 | int ret = -ENOENT; |
| 3408 | |
| 3409 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 3410 | if (user_data == req->user_data) { |
| 3411 | list_del_init(&req->list); |
| 3412 | ret = 0; |
| 3413 | break; |
| 3414 | } |
| 3415 | } |
| 3416 | |
| 3417 | if (ret == -ENOENT) |
| 3418 | return ret; |
| 3419 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3420 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3421 | if (ret == -1) |
| 3422 | return -EALREADY; |
| 3423 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3424 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3425 | io_cqring_fill_event(req, -ECANCELED); |
| 3426 | io_put_req(req); |
| 3427 | return 0; |
| 3428 | } |
| 3429 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3430 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 3431 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3432 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3433 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3434 | return -EINVAL; |
| 3435 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 3436 | return -EINVAL; |
| 3437 | |
| 3438 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 3439 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 3440 | if (req->timeout.flags) |
| 3441 | return -EINVAL; |
| 3442 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3443 | return 0; |
| 3444 | } |
| 3445 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3446 | /* |
| 3447 | * Remove or update an existing timeout command |
| 3448 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3449 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3450 | { |
| 3451 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3452 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3453 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3454 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3455 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3456 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3457 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3458 | io_commit_cqring(ctx); |
| 3459 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3460 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3461 | if (ret < 0) |
| 3462 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3463 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3464 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3465 | } |
| 3466 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3467 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3468 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3469 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3470 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3471 | unsigned flags; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3472 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3473 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3474 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3475 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3476 | return -EINVAL; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3477 | if (sqe->off && is_timeout_link) |
| 3478 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3479 | flags = READ_ONCE(sqe->timeout_flags); |
| 3480 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3481 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 3482 | |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3483 | req->timeout.count = READ_ONCE(sqe->off); |
| 3484 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3485 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3486 | return -ENOMEM; |
| 3487 | |
| 3488 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3489 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3490 | req->flags |= REQ_F_TIMEOUT; |
| 3491 | |
| 3492 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3493 | return -EFAULT; |
| 3494 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3495 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3496 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3497 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3498 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3499 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3500 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 3501 | return 0; |
| 3502 | } |
| 3503 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3504 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3505 | { |
| 3506 | unsigned count; |
| 3507 | struct io_ring_ctx *ctx = req->ctx; |
| 3508 | struct io_timeout_data *data; |
| 3509 | struct list_head *entry; |
| 3510 | unsigned span = 0; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3511 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3512 | data = &req->io->timeout; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3513 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3514 | /* |
| 3515 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3516 | * timeout event to be satisfied. If it isn't set, then this is |
| 3517 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3518 | */ |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3519 | count = req->timeout.count; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3520 | if (!count) { |
| 3521 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 3522 | spin_lock_irq(&ctx->completion_lock); |
| 3523 | entry = ctx->timeout_list.prev; |
| 3524 | goto add; |
| 3525 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3526 | |
| 3527 | req->sequence = ctx->cached_sq_head + count - 1; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3528 | data->seq_offset = count; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3529 | |
| 3530 | /* |
| 3531 | * Insertion sort, ensuring the first entry in the list is always |
| 3532 | * the one we need first. |
| 3533 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3534 | spin_lock_irq(&ctx->completion_lock); |
| 3535 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 3536 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3537 | unsigned nxt_sq_head; |
| 3538 | long long tmp, tmp_nxt; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3539 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3540 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3541 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 3542 | continue; |
| 3543 | |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3544 | /* |
| 3545 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 3546 | * long to store it. |
| 3547 | */ |
| 3548 | tmp = (long long)ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 3549 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
| 3550 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3551 | |
| 3552 | /* |
| 3553 | * cached_sq_head may overflow, and it will never overflow twice |
| 3554 | * once there is some timeout req still be valid. |
| 3555 | */ |
| 3556 | if (ctx->cached_sq_head < nxt_sq_head) |
yangerkun | 8b07a65 | 2019-10-17 12:12:35 +0800 | [diff] [blame] | 3557 | tmp += UINT_MAX; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3558 | |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3559 | if (tmp > tmp_nxt) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3560 | break; |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3561 | |
| 3562 | /* |
| 3563 | * Sequence of reqs after the insert one and itself should |
| 3564 | * be adjusted because each timeout req consumes a slot. |
| 3565 | */ |
| 3566 | span++; |
| 3567 | nxt->sequence++; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3568 | } |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3569 | req->sequence -= span; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3570 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3571 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3572 | data->timer.function = io_timeout_fn; |
| 3573 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3574 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3575 | return 0; |
| 3576 | } |
| 3577 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3578 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3579 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3580 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3581 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3582 | return req->user_data == (unsigned long) data; |
| 3583 | } |
| 3584 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3585 | 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] | 3586 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3587 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3588 | int ret = 0; |
| 3589 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3590 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
| 3591 | switch (cancel_ret) { |
| 3592 | case IO_WQ_CANCEL_OK: |
| 3593 | ret = 0; |
| 3594 | break; |
| 3595 | case IO_WQ_CANCEL_RUNNING: |
| 3596 | ret = -EALREADY; |
| 3597 | break; |
| 3598 | case IO_WQ_CANCEL_NOTFOUND: |
| 3599 | ret = -ENOENT; |
| 3600 | break; |
| 3601 | } |
| 3602 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3603 | return ret; |
| 3604 | } |
| 3605 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3606 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 3607 | struct io_kiocb *req, __u64 sqe_addr, |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3608 | struct io_kiocb **nxt, int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3609 | { |
| 3610 | unsigned long flags; |
| 3611 | int ret; |
| 3612 | |
| 3613 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 3614 | if (ret != -ENOENT) { |
| 3615 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3616 | goto done; |
| 3617 | } |
| 3618 | |
| 3619 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3620 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 3621 | if (ret != -ENOENT) |
| 3622 | goto done; |
| 3623 | ret = io_poll_cancel(ctx, sqe_addr); |
| 3624 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3625 | if (!ret) |
| 3626 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3627 | io_cqring_fill_event(req, ret); |
| 3628 | io_commit_cqring(ctx); |
| 3629 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3630 | io_cqring_ev_posted(ctx); |
| 3631 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3632 | if (ret < 0) |
| 3633 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3634 | io_put_req_find_next(req, nxt); |
| 3635 | } |
| 3636 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3637 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 3638 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3639 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3640 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3641 | return -EINVAL; |
| 3642 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 3643 | sqe->cancel_flags) |
| 3644 | return -EINVAL; |
| 3645 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3646 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 3647 | return 0; |
| 3648 | } |
| 3649 | |
| 3650 | static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3651 | { |
| 3652 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3653 | |
| 3654 | io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3655 | return 0; |
| 3656 | } |
| 3657 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 3658 | static int io_files_update_prep(struct io_kiocb *req, |
| 3659 | const struct io_uring_sqe *sqe) |
| 3660 | { |
| 3661 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) |
| 3662 | return -EINVAL; |
| 3663 | |
| 3664 | req->files_update.offset = READ_ONCE(sqe->off); |
| 3665 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 3666 | if (!req->files_update.nr_args) |
| 3667 | return -EINVAL; |
| 3668 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 3669 | return 0; |
| 3670 | } |
| 3671 | |
| 3672 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) |
| 3673 | { |
| 3674 | struct io_ring_ctx *ctx = req->ctx; |
| 3675 | struct io_uring_files_update up; |
| 3676 | int ret; |
| 3677 | |
| 3678 | if (force_nonblock) { |
| 3679 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 3680 | return -EAGAIN; |
| 3681 | } |
| 3682 | |
| 3683 | up.offset = req->files_update.offset; |
| 3684 | up.fds = req->files_update.arg; |
| 3685 | |
| 3686 | mutex_lock(&ctx->uring_lock); |
| 3687 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 3688 | mutex_unlock(&ctx->uring_lock); |
| 3689 | |
| 3690 | if (ret < 0) |
| 3691 | req_set_fail_links(req); |
| 3692 | io_cqring_add_event(req, ret); |
| 3693 | io_put_req(req); |
| 3694 | return 0; |
| 3695 | } |
| 3696 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3697 | static int io_req_defer_prep(struct io_kiocb *req, |
| 3698 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3699 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3700 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3701 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3702 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3703 | case IORING_OP_NOP: |
| 3704 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3705 | case IORING_OP_READV: |
| 3706 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3707 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3708 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3709 | break; |
| 3710 | case IORING_OP_WRITEV: |
| 3711 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3712 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3713 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3714 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3715 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3716 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3717 | break; |
| 3718 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3719 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3720 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3721 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3722 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3723 | break; |
| 3724 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3725 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3726 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3727 | case IORING_OP_SENDMSG: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3728 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3729 | break; |
| 3730 | case IORING_OP_RECVMSG: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3731 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3732 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3733 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3734 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3735 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3736 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3737 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3738 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3739 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3740 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3741 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3742 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3743 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3744 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3745 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3746 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3747 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3748 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3749 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3750 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3751 | case IORING_OP_FALLOCATE: |
| 3752 | ret = io_fallocate_prep(req, sqe); |
| 3753 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3754 | case IORING_OP_OPENAT: |
| 3755 | ret = io_openat_prep(req, sqe); |
| 3756 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3757 | case IORING_OP_CLOSE: |
| 3758 | ret = io_close_prep(req, sqe); |
| 3759 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 3760 | case IORING_OP_FILES_UPDATE: |
| 3761 | ret = io_files_update_prep(req, sqe); |
| 3762 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3763 | case IORING_OP_STATX: |
| 3764 | ret = io_statx_prep(req, sqe); |
| 3765 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame^] | 3766 | case IORING_OP_FADVISE: |
| 3767 | ret = io_fadvise_prep(req, sqe); |
| 3768 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3769 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3770 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 3771 | req->opcode); |
| 3772 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3773 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3774 | } |
| 3775 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3776 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3777 | } |
| 3778 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3779 | static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3780 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3781 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3782 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3783 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 3784 | /* Still need defer if there is pending req in defer list. */ |
| 3785 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3786 | return 0; |
| 3787 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3788 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3789 | return -EAGAIN; |
| 3790 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3791 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3792 | if (ret < 0) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3793 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3794 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3795 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 3796 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3797 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3798 | return 0; |
| 3799 | } |
| 3800 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 3801 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3802 | list_add_tail(&req->list, &ctx->defer_list); |
| 3803 | spin_unlock_irq(&ctx->completion_lock); |
| 3804 | return -EIOCBQUEUED; |
| 3805 | } |
| 3806 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3807 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 3808 | struct io_kiocb **nxt, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3809 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3810 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3811 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3812 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3813 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3814 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3815 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3816 | break; |
| 3817 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3818 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3819 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3820 | if (sqe) { |
| 3821 | ret = io_read_prep(req, sqe, force_nonblock); |
| 3822 | if (ret < 0) |
| 3823 | break; |
| 3824 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 3825 | ret = io_read(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3826 | break; |
| 3827 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3828 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3829 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3830 | if (sqe) { |
| 3831 | ret = io_write_prep(req, sqe, force_nonblock); |
| 3832 | if (ret < 0) |
| 3833 | break; |
| 3834 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 3835 | ret = io_write(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3836 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3837 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3838 | if (sqe) { |
| 3839 | ret = io_prep_fsync(req, sqe); |
| 3840 | if (ret < 0) |
| 3841 | break; |
| 3842 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3843 | ret = io_fsync(req, nxt, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 3844 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3845 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3846 | if (sqe) { |
| 3847 | ret = io_poll_add_prep(req, sqe); |
| 3848 | if (ret) |
| 3849 | break; |
| 3850 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3851 | ret = io_poll_add(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3852 | break; |
| 3853 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3854 | if (sqe) { |
| 3855 | ret = io_poll_remove_prep(req, sqe); |
| 3856 | if (ret < 0) |
| 3857 | break; |
| 3858 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3859 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3860 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3861 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3862 | if (sqe) { |
| 3863 | ret = io_prep_sfr(req, sqe); |
| 3864 | if (ret < 0) |
| 3865 | break; |
| 3866 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3867 | ret = io_sync_file_range(req, nxt, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 3868 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3869 | case IORING_OP_SENDMSG: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3870 | if (sqe) { |
| 3871 | ret = io_sendmsg_prep(req, sqe); |
| 3872 | if (ret < 0) |
| 3873 | break; |
| 3874 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3875 | ret = io_sendmsg(req, nxt, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3876 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 3877 | case IORING_OP_RECVMSG: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3878 | if (sqe) { |
| 3879 | ret = io_recvmsg_prep(req, sqe); |
| 3880 | if (ret) |
| 3881 | break; |
| 3882 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3883 | ret = io_recvmsg(req, nxt, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 3884 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3885 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3886 | if (sqe) { |
| 3887 | ret = io_timeout_prep(req, sqe, false); |
| 3888 | if (ret) |
| 3889 | break; |
| 3890 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3891 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3892 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3893 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3894 | if (sqe) { |
| 3895 | ret = io_timeout_remove_prep(req, sqe); |
| 3896 | if (ret) |
| 3897 | break; |
| 3898 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3899 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3900 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3901 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3902 | if (sqe) { |
| 3903 | ret = io_accept_prep(req, sqe); |
| 3904 | if (ret) |
| 3905 | break; |
| 3906 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3907 | ret = io_accept(req, nxt, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3908 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3909 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3910 | if (sqe) { |
| 3911 | ret = io_connect_prep(req, sqe); |
| 3912 | if (ret) |
| 3913 | break; |
| 3914 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3915 | ret = io_connect(req, nxt, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3916 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3917 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3918 | if (sqe) { |
| 3919 | ret = io_async_cancel_prep(req, sqe); |
| 3920 | if (ret) |
| 3921 | break; |
| 3922 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3923 | ret = io_async_cancel(req, nxt); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3924 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3925 | case IORING_OP_FALLOCATE: |
| 3926 | if (sqe) { |
| 3927 | ret = io_fallocate_prep(req, sqe); |
| 3928 | if (ret) |
| 3929 | break; |
| 3930 | } |
| 3931 | ret = io_fallocate(req, nxt, force_nonblock); |
| 3932 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3933 | case IORING_OP_OPENAT: |
| 3934 | if (sqe) { |
| 3935 | ret = io_openat_prep(req, sqe); |
| 3936 | if (ret) |
| 3937 | break; |
| 3938 | } |
| 3939 | ret = io_openat(req, nxt, force_nonblock); |
| 3940 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3941 | case IORING_OP_CLOSE: |
| 3942 | if (sqe) { |
| 3943 | ret = io_close_prep(req, sqe); |
| 3944 | if (ret) |
| 3945 | break; |
| 3946 | } |
| 3947 | ret = io_close(req, nxt, force_nonblock); |
| 3948 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 3949 | case IORING_OP_FILES_UPDATE: |
| 3950 | if (sqe) { |
| 3951 | ret = io_files_update_prep(req, sqe); |
| 3952 | if (ret) |
| 3953 | break; |
| 3954 | } |
| 3955 | ret = io_files_update(req, force_nonblock); |
| 3956 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3957 | case IORING_OP_STATX: |
| 3958 | if (sqe) { |
| 3959 | ret = io_statx_prep(req, sqe); |
| 3960 | if (ret) |
| 3961 | break; |
| 3962 | } |
| 3963 | ret = io_statx(req, nxt, force_nonblock); |
| 3964 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame^] | 3965 | case IORING_OP_FADVISE: |
| 3966 | if (sqe) { |
| 3967 | ret = io_fadvise_prep(req, sqe); |
| 3968 | if (ret) |
| 3969 | break; |
| 3970 | } |
| 3971 | ret = io_fadvise(req, nxt, force_nonblock); |
| 3972 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3973 | default: |
| 3974 | ret = -EINVAL; |
| 3975 | break; |
| 3976 | } |
| 3977 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3978 | if (ret) |
| 3979 | return ret; |
| 3980 | |
| 3981 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 3982 | const bool in_async = io_wq_current_is_worker(); |
| 3983 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3984 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3985 | return -EAGAIN; |
| 3986 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 3987 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 3988 | if (in_async) |
| 3989 | mutex_lock(&ctx->uring_lock); |
| 3990 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3991 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 3992 | |
| 3993 | if (in_async) |
| 3994 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3995 | } |
| 3996 | |
| 3997 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3998 | } |
| 3999 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4000 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4001 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4002 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4003 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4004 | struct io_kiocb *nxt = NULL; |
| 4005 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4006 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4007 | /* if NO_CANCEL is set, we must still run the work */ |
| 4008 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 4009 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4010 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4011 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4012 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4013 | if (!ret) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4014 | req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0; |
| 4015 | req->in_async = true; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4016 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4017 | ret = io_issue_sqe(req, NULL, &nxt, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4018 | /* |
| 4019 | * We can get EAGAIN for polled IO even though we're |
| 4020 | * forcing a sync submission from here, since we can't |
| 4021 | * wait for request slots on the block side. |
| 4022 | */ |
| 4023 | if (ret != -EAGAIN) |
| 4024 | break; |
| 4025 | cond_resched(); |
| 4026 | } while (1); |
| 4027 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4028 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4029 | /* drop submission reference */ |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 4030 | io_put_req(req); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4031 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4032 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4033 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4034 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4035 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4036 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4037 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4038 | /* if a dependent link is ready, pass it back */ |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4039 | if (!ret && nxt) |
| 4040 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4041 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4042 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4043 | static int io_req_needs_file(struct io_kiocb *req, int fd) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4044 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4045 | if (!io_op_defs[req->opcode].needs_file) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4046 | return 0; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4047 | if (fd == -1 && io_op_defs[req->opcode].fd_non_neg) |
| 4048 | return 0; |
| 4049 | return 1; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4050 | } |
| 4051 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4052 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 4053 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4054 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4055 | struct fixed_file_table *table; |
| 4056 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4057 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
| 4058 | return table->files[index & IORING_FILE_TABLE_MASK];; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4059 | } |
| 4060 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4061 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
| 4062 | const struct io_uring_sqe *sqe) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4063 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4064 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4065 | unsigned flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4066 | int fd; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4067 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4068 | flags = READ_ONCE(sqe->flags); |
| 4069 | fd = READ_ONCE(sqe->fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4070 | |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4071 | if (flags & IOSQE_IO_DRAIN) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4072 | req->flags |= REQ_F_IO_DRAIN; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4073 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4074 | if (!io_req_needs_file(req, fd)) |
| 4075 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4076 | |
| 4077 | if (flags & IOSQE_FIXED_FILE) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4078 | if (unlikely(!ctx->file_data || |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4079 | (unsigned) fd >= ctx->nr_user_files)) |
| 4080 | return -EBADF; |
Jens Axboe | b762012 | 2019-10-26 07:22:55 -0600 | [diff] [blame] | 4081 | fd = array_index_nospec(fd, ctx->nr_user_files); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4082 | req->file = io_file_from_index(ctx, fd); |
| 4083 | if (!req->file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4084 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4085 | req->flags |= REQ_F_FIXED_FILE; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4086 | percpu_ref_get(&ctx->file_data->refs); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4087 | } else { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4088 | if (req->needs_fixed_file) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4089 | return -EBADF; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 4090 | trace_io_uring_file_get(ctx, fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4091 | req->file = io_file_get(state, fd); |
| 4092 | if (unlikely(!req->file)) |
| 4093 | return -EBADF; |
| 4094 | } |
| 4095 | |
| 4096 | return 0; |
| 4097 | } |
| 4098 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4099 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4100 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4101 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4102 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4103 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4104 | if (!req->ring_file) |
| 4105 | return -EBADF; |
| 4106 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4107 | rcu_read_lock(); |
| 4108 | spin_lock_irq(&ctx->inflight_lock); |
| 4109 | /* |
| 4110 | * We use the f_ops->flush() handler to ensure that we can flush |
| 4111 | * out work accessing these files if the fd is closed. Check if |
| 4112 | * the fd has changed since we started down this path, and disallow |
| 4113 | * this operation if it has. |
| 4114 | */ |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4115 | if (fcheck(req->ring_fd) == req->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4116 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 4117 | req->flags |= REQ_F_INFLIGHT; |
| 4118 | req->work.files = current->files; |
| 4119 | ret = 0; |
| 4120 | } |
| 4121 | spin_unlock_irq(&ctx->inflight_lock); |
| 4122 | rcu_read_unlock(); |
| 4123 | |
| 4124 | return ret; |
| 4125 | } |
| 4126 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4127 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 4128 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4129 | struct io_timeout_data *data = container_of(timer, |
| 4130 | struct io_timeout_data, timer); |
| 4131 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4132 | struct io_ring_ctx *ctx = req->ctx; |
| 4133 | struct io_kiocb *prev = NULL; |
| 4134 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4135 | |
| 4136 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4137 | |
| 4138 | /* |
| 4139 | * We don't expect the list to be empty, that will only happen if we |
| 4140 | * race with the completion of the linked work. |
| 4141 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4142 | if (!list_empty(&req->link_list)) { |
| 4143 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 4144 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4145 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4146 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4147 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 4148 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4149 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4150 | } |
| 4151 | |
| 4152 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4153 | |
| 4154 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4155 | req_set_fail_links(prev); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4156 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
| 4157 | -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4158 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4159 | } else { |
| 4160 | io_cqring_add_event(req, -ETIME); |
| 4161 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4162 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4163 | return HRTIMER_NORESTART; |
| 4164 | } |
| 4165 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4166 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4167 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4168 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4169 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4170 | /* |
| 4171 | * If the list is now empty, then our linked request finished before |
| 4172 | * we got a chance to setup the timer |
| 4173 | */ |
| 4174 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4175 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4176 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4177 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4178 | data->timer.function = io_link_timeout_fn; |
| 4179 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 4180 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4181 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4182 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4183 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4184 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4185 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4186 | } |
| 4187 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4188 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4189 | { |
| 4190 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4191 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4192 | if (!(req->flags & REQ_F_LINK)) |
| 4193 | return NULL; |
| 4194 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4195 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 4196 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4197 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4198 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4199 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4200 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4201 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4202 | } |
| 4203 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4204 | static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4205 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4206 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4207 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4208 | int ret; |
| 4209 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4210 | again: |
| 4211 | linked_timeout = io_prep_linked_timeout(req); |
| 4212 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4213 | ret = io_issue_sqe(req, sqe, &nxt, true); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 4214 | |
| 4215 | /* |
| 4216 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 4217 | * doesn't support non-blocking read/write attempts |
| 4218 | */ |
| 4219 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 4220 | (req->flags & REQ_F_MUST_PUNT))) { |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4221 | if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) { |
| 4222 | ret = io_grab_files(req); |
| 4223 | if (ret) |
| 4224 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4225 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4226 | |
| 4227 | /* |
| 4228 | * Queued up for async execution, worker will release |
| 4229 | * submit reference when the iocb is actually submitted. |
| 4230 | */ |
| 4231 | io_queue_async_work(req); |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4232 | goto done_req; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4233 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4234 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4235 | err: |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4236 | /* drop submission reference */ |
| 4237 | io_put_req(req); |
| 4238 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4239 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4240 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4241 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4242 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4243 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4244 | } |
| 4245 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4246 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4247 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4248 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4249 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4250 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4251 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4252 | done_req: |
| 4253 | if (nxt) { |
| 4254 | req = nxt; |
| 4255 | nxt = NULL; |
| 4256 | goto again; |
| 4257 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4258 | } |
| 4259 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4260 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4261 | { |
| 4262 | int ret; |
| 4263 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4264 | if (unlikely(req->ctx->drain_next)) { |
| 4265 | req->flags |= REQ_F_IO_DRAIN; |
| 4266 | req->ctx->drain_next = false; |
| 4267 | } |
| 4268 | req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK); |
| 4269 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4270 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4271 | if (ret) { |
| 4272 | if (ret != -EIOCBQUEUED) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4273 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4274 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4275 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4276 | } |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4277 | } else if ((req->flags & REQ_F_FORCE_ASYNC) && |
| 4278 | !io_wq_current_is_worker()) { |
| 4279 | /* |
| 4280 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 4281 | * to async execution. |
| 4282 | */ |
| 4283 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 4284 | io_queue_async_work(req); |
| 4285 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4286 | __io_queue_sqe(req, sqe); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4287 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4288 | } |
| 4289 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4290 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4291 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4292 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4293 | io_cqring_add_event(req, -ECANCELED); |
| 4294 | io_double_put_req(req); |
| 4295 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4296 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4297 | } |
| 4298 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4299 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4300 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4301 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4302 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4303 | struct io_submit_state *state, struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4304 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4305 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4306 | unsigned int sqe_flags; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4307 | int ret; |
| 4308 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4309 | sqe_flags = READ_ONCE(sqe->flags); |
| 4310 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4311 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4312 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4313 | ret = -EINVAL; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4314 | goto err_req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4315 | } |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4316 | if (sqe_flags & IOSQE_ASYNC) |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4317 | req->flags |= REQ_F_FORCE_ASYNC; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4318 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4319 | ret = io_req_set_file(state, req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4320 | if (unlikely(ret)) { |
| 4321 | err_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4322 | io_cqring_add_event(req, ret); |
| 4323 | io_double_put_req(req); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4324 | return false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4325 | } |
| 4326 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4327 | /* |
| 4328 | * If we already have a head request, queue this one for async |
| 4329 | * submittal once the head completes. If we don't have a head but |
| 4330 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 4331 | * submitted sync once the chain is complete. If none of those |
| 4332 | * conditions are true (normal request), then just queue it. |
| 4333 | */ |
| 4334 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4335 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4336 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4337 | if (sqe_flags & IOSQE_IO_DRAIN) |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4338 | head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN; |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4339 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4340 | if (sqe_flags & IOSQE_IO_HARDLINK) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4341 | req->flags |= REQ_F_HARDLINK; |
| 4342 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4343 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4344 | ret = -EAGAIN; |
| 4345 | goto err_req; |
| 4346 | } |
| 4347 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4348 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4349 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4350 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4351 | head->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4352 | goto err_req; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4353 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4354 | trace_io_uring_link(ctx, req, head); |
| 4355 | list_add_tail(&req->link_list, &head->link_list); |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4356 | |
| 4357 | /* last request of a link, enqueue the link */ |
| 4358 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) { |
| 4359 | io_queue_link_head(head); |
| 4360 | *link = NULL; |
| 4361 | } |
| 4362 | } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4363 | req->flags |= REQ_F_LINK; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4364 | if (sqe_flags & IOSQE_IO_HARDLINK) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4365 | req->flags |= REQ_F_HARDLINK; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4366 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4367 | INIT_LIST_HEAD(&req->link_list); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4368 | ret = io_req_defer_prep(req, sqe); |
| 4369 | if (ret) |
| 4370 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4371 | *link = req; |
| 4372 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4373 | io_queue_sqe(req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4374 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4375 | |
| 4376 | return true; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4377 | } |
| 4378 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4379 | /* |
| 4380 | * Batched submission is done, ensure local IO is flushed out. |
| 4381 | */ |
| 4382 | static void io_submit_state_end(struct io_submit_state *state) |
| 4383 | { |
| 4384 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 4385 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4386 | if (state->free_reqs) |
| 4387 | kmem_cache_free_bulk(req_cachep, state->free_reqs, |
| 4388 | &state->reqs[state->cur_req]); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4389 | } |
| 4390 | |
| 4391 | /* |
| 4392 | * Start submission side cache. |
| 4393 | */ |
| 4394 | static void io_submit_state_start(struct io_submit_state *state, |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4395 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4396 | { |
| 4397 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4398 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4399 | state->file = NULL; |
| 4400 | state->ios_left = max_ios; |
| 4401 | } |
| 4402 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4403 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 4404 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4405 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4406 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4407 | if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4408 | /* |
| 4409 | * Ensure any loads from the SQEs are done at this point, |
| 4410 | * since once we write the new head, the application could |
| 4411 | * write new data to them. |
| 4412 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4413 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4414 | } |
| 4415 | } |
| 4416 | |
| 4417 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4418 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4419 | * that is mapped by userspace. This means that care needs to be taken to |
| 4420 | * ensure that reads are stable, as we cannot rely on userspace always |
| 4421 | * being a good citizen. If members of the sqe are validated and then later |
| 4422 | * used, it's important that those reads are done through READ_ONCE() to |
| 4423 | * prevent a re-load down the line. |
| 4424 | */ |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4425 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 4426 | const struct io_uring_sqe **sqe_ptr) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4427 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4428 | struct io_rings *rings = ctx->rings; |
| 4429 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4430 | unsigned head; |
| 4431 | |
| 4432 | /* |
| 4433 | * The cached sq head (or cq tail) serves two purposes: |
| 4434 | * |
| 4435 | * 1) allows us to batch the cost of updating the user visible |
| 4436 | * head updates. |
| 4437 | * 2) allows the kernel side to track the head on its own, even |
| 4438 | * though the application is the one updating it. |
| 4439 | */ |
| 4440 | head = ctx->cached_sq_head; |
Stefan Bühler | e523a29 | 2019-04-19 11:57:44 +0200 | [diff] [blame] | 4441 | /* make sure SQ entry isn't read before tail */ |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 4442 | if (unlikely(head == smp_load_acquire(&rings->sq.tail))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4443 | return false; |
| 4444 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4445 | head = READ_ONCE(sq_array[head & ctx->sq_mask]); |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 4446 | if (likely(head < ctx->sq_entries)) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4447 | /* |
| 4448 | * All io need record the previous position, if LINK vs DARIN, |
| 4449 | * it can be used to mark the position of the first IO in the |
| 4450 | * link list. |
| 4451 | */ |
| 4452 | req->sequence = ctx->cached_sq_head; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4453 | *sqe_ptr = &ctx->sq_sqes[head]; |
| 4454 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); |
| 4455 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4456 | ctx->cached_sq_head++; |
| 4457 | return true; |
| 4458 | } |
| 4459 | |
| 4460 | /* drop invalid entries */ |
| 4461 | ctx->cached_sq_head++; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 4462 | ctx->cached_sq_dropped++; |
| 4463 | WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4464 | return false; |
| 4465 | } |
| 4466 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4467 | 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] | 4468 | struct file *ring_file, int ring_fd, |
| 4469 | struct mm_struct **mm, bool async) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4470 | { |
| 4471 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4472 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4473 | int i, submitted = 0; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4474 | bool mm_fault = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4475 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 4476 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 4477 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 4478 | if (!list_empty(&ctx->cq_overflow_list) && |
| 4479 | !io_cqring_overflow_flush(ctx, false)) |
| 4480 | return -EBUSY; |
| 4481 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4482 | |
| 4483 | if (nr > IO_PLUG_THRESHOLD) { |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4484 | io_submit_state_start(&state, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4485 | statep = &state; |
| 4486 | } |
| 4487 | |
| 4488 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4489 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4490 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4491 | |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4492 | req = io_get_req(ctx, statep); |
| 4493 | if (unlikely(!req)) { |
| 4494 | if (!submitted) |
| 4495 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4496 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4497 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4498 | if (!io_get_sqring(ctx, req, &sqe)) { |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4499 | __io_free_req(req); |
| 4500 | break; |
| 4501 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4502 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4503 | /* will complete beyond this point, count as submitted */ |
| 4504 | submitted++; |
| 4505 | |
| 4506 | if (unlikely(req->opcode >= IORING_OP_LAST)) { |
| 4507 | io_cqring_add_event(req, -EINVAL); |
| 4508 | io_double_put_req(req); |
| 4509 | break; |
| 4510 | } |
| 4511 | |
| 4512 | if (io_op_defs[req->opcode].needs_mm && !*mm) { |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4513 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
| 4514 | if (!mm_fault) { |
| 4515 | use_mm(ctx->sqo_mm); |
| 4516 | *mm = ctx->sqo_mm; |
| 4517 | } |
| 4518 | } |
| 4519 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4520 | req->ring_file = ring_file; |
| 4521 | req->ring_fd = ring_fd; |
| 4522 | req->has_user = *mm != NULL; |
| 4523 | req->in_async = async; |
| 4524 | req->needs_fixed_file = async; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4525 | trace_io_uring_submit_sqe(ctx, req->user_data, true, async); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4526 | if (!io_submit_sqe(req, sqe, statep, &link)) |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4527 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4528 | } |
| 4529 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4530 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4531 | io_queue_link_head(link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4532 | if (statep) |
| 4533 | io_submit_state_end(&state); |
| 4534 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 4535 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 4536 | io_commit_sqring(ctx); |
| 4537 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4538 | return submitted; |
| 4539 | } |
| 4540 | |
| 4541 | static int io_sq_thread(void *data) |
| 4542 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4543 | struct io_ring_ctx *ctx = data; |
| 4544 | struct mm_struct *cur_mm = NULL; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4545 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4546 | mm_segment_t old_fs; |
| 4547 | DEFINE_WAIT(wait); |
| 4548 | unsigned inflight; |
| 4549 | unsigned long timeout; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4550 | int ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4551 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 4552 | complete(&ctx->completions[1]); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 4553 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4554 | old_fs = get_fs(); |
| 4555 | set_fs(USER_DS); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4556 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4557 | |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4558 | ret = timeout = inflight = 0; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4559 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4560 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4561 | |
| 4562 | if (inflight) { |
| 4563 | unsigned nr_events = 0; |
| 4564 | |
| 4565 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 4566 | /* |
| 4567 | * inflight is the count of the maximum possible |
| 4568 | * entries we submitted, but it can be smaller |
| 4569 | * if we dropped some of them. If we don't have |
| 4570 | * poll entries available, then we know that we |
| 4571 | * have nothing left to poll for. Reset the |
| 4572 | * inflight count to zero in that case. |
| 4573 | */ |
| 4574 | mutex_lock(&ctx->uring_lock); |
| 4575 | if (!list_empty(&ctx->poll_list)) |
| 4576 | __io_iopoll_check(ctx, &nr_events, 0); |
| 4577 | else |
| 4578 | inflight = 0; |
| 4579 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4580 | } else { |
| 4581 | /* |
| 4582 | * Normal IO, just pretend everything completed. |
| 4583 | * We don't have to poll completions for that. |
| 4584 | */ |
| 4585 | nr_events = inflight; |
| 4586 | } |
| 4587 | |
| 4588 | inflight -= nr_events; |
| 4589 | if (!inflight) |
| 4590 | timeout = jiffies + ctx->sq_thread_idle; |
| 4591 | } |
| 4592 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4593 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4594 | |
| 4595 | /* |
| 4596 | * If submit got -EBUSY, flag us as needing the application |
| 4597 | * to enter the kernel to reap and flush events. |
| 4598 | */ |
| 4599 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4600 | /* |
| 4601 | * We're polling. If we're within the defined idle |
| 4602 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4603 | * to sleep. The exception is if we got EBUSY doing |
| 4604 | * more IO, we should wait for the application to |
| 4605 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4606 | */ |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4607 | if (inflight || |
| 4608 | (!time_after(jiffies, timeout) && ret != -EBUSY)) { |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 4609 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4610 | continue; |
| 4611 | } |
| 4612 | |
| 4613 | /* |
| 4614 | * Drop cur_mm before scheduling, we can't hold it for |
| 4615 | * long periods (or over schedule()). Do this before |
| 4616 | * adding ourselves to the waitqueue, as the unuse/drop |
| 4617 | * may sleep. |
| 4618 | */ |
| 4619 | if (cur_mm) { |
| 4620 | unuse_mm(cur_mm); |
| 4621 | mmput(cur_mm); |
| 4622 | cur_mm = NULL; |
| 4623 | } |
| 4624 | |
| 4625 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 4626 | TASK_INTERRUPTIBLE); |
| 4627 | |
| 4628 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4629 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 4630 | /* make sure to read SQ tail after writing flags */ |
| 4631 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4632 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4633 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4634 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4635 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4636 | finish_wait(&ctx->sqo_wait, &wait); |
| 4637 | break; |
| 4638 | } |
| 4639 | if (signal_pending(current)) |
| 4640 | flush_signals(current); |
| 4641 | schedule(); |
| 4642 | finish_wait(&ctx->sqo_wait, &wait); |
| 4643 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4644 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4645 | continue; |
| 4646 | } |
| 4647 | finish_wait(&ctx->sqo_wait, &wait); |
| 4648 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4649 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4650 | } |
| 4651 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4652 | to_submit = min(to_submit, ctx->sq_entries); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 4653 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4654 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 4655 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4656 | if (ret > 0) |
| 4657 | inflight += ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | set_fs(old_fs); |
| 4661 | if (cur_mm) { |
| 4662 | unuse_mm(cur_mm); |
| 4663 | mmput(cur_mm); |
| 4664 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4665 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 4666 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4667 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 4668 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4669 | return 0; |
| 4670 | } |
| 4671 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4672 | struct io_wait_queue { |
| 4673 | struct wait_queue_entry wq; |
| 4674 | struct io_ring_ctx *ctx; |
| 4675 | unsigned to_wait; |
| 4676 | unsigned nr_timeouts; |
| 4677 | }; |
| 4678 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4679 | 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] | 4680 | { |
| 4681 | struct io_ring_ctx *ctx = iowq->ctx; |
| 4682 | |
| 4683 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 4684 | * 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] | 4685 | * started waiting. For timeouts, we always want to return to userspace, |
| 4686 | * regardless of event count. |
| 4687 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4688 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4689 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 4690 | } |
| 4691 | |
| 4692 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 4693 | int wake_flags, void *key) |
| 4694 | { |
| 4695 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 4696 | wq); |
| 4697 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4698 | /* use noflush == true, as we can't safely rely on locking context */ |
| 4699 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4700 | return -1; |
| 4701 | |
| 4702 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 4703 | } |
| 4704 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4705 | /* |
| 4706 | * Wait until events become available, if we don't already have some. The |
| 4707 | * application must reap them itself, as they reside on the shared cq ring. |
| 4708 | */ |
| 4709 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 4710 | const sigset_t __user *sig, size_t sigsz) |
| 4711 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4712 | struct io_wait_queue iowq = { |
| 4713 | .wq = { |
| 4714 | .private = current, |
| 4715 | .func = io_wake_function, |
| 4716 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 4717 | }, |
| 4718 | .ctx = ctx, |
| 4719 | .to_wait = min_events, |
| 4720 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4721 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 4722 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4723 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4724 | if (io_cqring_events(ctx, false) >= min_events) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4725 | return 0; |
| 4726 | |
| 4727 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 4728 | #ifdef CONFIG_COMPAT |
| 4729 | if (in_compat_syscall()) |
| 4730 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 4731 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 4732 | else |
| 4733 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 4734 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 4735 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4736 | if (ret) |
| 4737 | return ret; |
| 4738 | } |
| 4739 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4740 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 4741 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4742 | do { |
| 4743 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 4744 | TASK_INTERRUPTIBLE); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4745 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4746 | break; |
| 4747 | schedule(); |
| 4748 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 4749 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4750 | break; |
| 4751 | } |
| 4752 | } while (1); |
| 4753 | finish_wait(&ctx->wait, &iowq.wq); |
| 4754 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 4755 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4756 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4757 | 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] | 4758 | } |
| 4759 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4760 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 4761 | { |
| 4762 | #if defined(CONFIG_UNIX) |
| 4763 | if (ctx->ring_sock) { |
| 4764 | struct sock *sock = ctx->ring_sock->sk; |
| 4765 | struct sk_buff *skb; |
| 4766 | |
| 4767 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 4768 | kfree_skb(skb); |
| 4769 | } |
| 4770 | #else |
| 4771 | int i; |
| 4772 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4773 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 4774 | struct file *file; |
| 4775 | |
| 4776 | file = io_file_from_index(ctx, i); |
| 4777 | if (file) |
| 4778 | fput(file); |
| 4779 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4780 | #endif |
| 4781 | } |
| 4782 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4783 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 4784 | { |
| 4785 | struct fixed_file_data *data; |
| 4786 | |
| 4787 | data = container_of(ref, struct fixed_file_data, refs); |
| 4788 | complete(&data->done); |
| 4789 | } |
| 4790 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4791 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 4792 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4793 | struct fixed_file_data *data = ctx->file_data; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4794 | unsigned nr_tables, i; |
| 4795 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4796 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4797 | return -ENXIO; |
| 4798 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4799 | /* protect against inflight atomic switch, which drops the ref */ |
| 4800 | flush_work(&data->ref_work); |
| 4801 | percpu_ref_get(&data->refs); |
| 4802 | percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill); |
| 4803 | wait_for_completion(&data->done); |
| 4804 | percpu_ref_put(&data->refs); |
| 4805 | percpu_ref_exit(&data->refs); |
| 4806 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4807 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4808 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 4809 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4810 | kfree(data->table[i].files); |
| 4811 | kfree(data->table); |
| 4812 | kfree(data); |
| 4813 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4814 | ctx->nr_user_files = 0; |
| 4815 | return 0; |
| 4816 | } |
| 4817 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4818 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 4819 | { |
| 4820 | if (ctx->sqo_thread) { |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 4821 | wait_for_completion(&ctx->completions[1]); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4822 | /* |
| 4823 | * The park is a bit of a work-around, without it we get |
| 4824 | * warning spews on shutdown with SQPOLL set and affinity |
| 4825 | * set to a single CPU. |
| 4826 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 4827 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4828 | kthread_stop(ctx->sqo_thread); |
| 4829 | ctx->sqo_thread = NULL; |
| 4830 | } |
| 4831 | } |
| 4832 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4833 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 4834 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4835 | io_sq_thread_stop(ctx); |
| 4836 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4837 | if (ctx->io_wq) { |
| 4838 | io_wq_destroy(ctx->io_wq); |
| 4839 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4840 | } |
| 4841 | } |
| 4842 | |
| 4843 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4844 | /* |
| 4845 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 4846 | * the io_uring can be safely unregistered on process exit, even if we have |
| 4847 | * loops in the file referencing. |
| 4848 | */ |
| 4849 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 4850 | { |
| 4851 | struct sock *sk = ctx->ring_sock->sk; |
| 4852 | struct scm_fp_list *fpl; |
| 4853 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4854 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4855 | |
| 4856 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 4857 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 4858 | |
| 4859 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 4860 | return -EMFILE; |
| 4861 | } |
| 4862 | |
| 4863 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 4864 | if (!fpl) |
| 4865 | return -ENOMEM; |
| 4866 | |
| 4867 | skb = alloc_skb(0, GFP_KERNEL); |
| 4868 | if (!skb) { |
| 4869 | kfree(fpl); |
| 4870 | return -ENOMEM; |
| 4871 | } |
| 4872 | |
| 4873 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4874 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4875 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4876 | fpl->user = get_uid(ctx->user); |
| 4877 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4878 | struct file *file = io_file_from_index(ctx, i + offset); |
| 4879 | |
| 4880 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4881 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4882 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4883 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 4884 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4885 | } |
| 4886 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4887 | if (nr_files) { |
| 4888 | fpl->max = SCM_MAX_FD; |
| 4889 | fpl->count = nr_files; |
| 4890 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4891 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4892 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 4893 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4894 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4895 | for (i = 0; i < nr_files; i++) |
| 4896 | fput(fpl->fp[i]); |
| 4897 | } else { |
| 4898 | kfree_skb(skb); |
| 4899 | kfree(fpl); |
| 4900 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4901 | |
| 4902 | return 0; |
| 4903 | } |
| 4904 | |
| 4905 | /* |
| 4906 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 4907 | * causes regular reference counting to break down. We rely on the UNIX |
| 4908 | * garbage collection to take care of this problem for us. |
| 4909 | */ |
| 4910 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 4911 | { |
| 4912 | unsigned left, total; |
| 4913 | int ret = 0; |
| 4914 | |
| 4915 | total = 0; |
| 4916 | left = ctx->nr_user_files; |
| 4917 | while (left) { |
| 4918 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4919 | |
| 4920 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 4921 | if (ret) |
| 4922 | break; |
| 4923 | left -= this_files; |
| 4924 | total += this_files; |
| 4925 | } |
| 4926 | |
| 4927 | if (!ret) |
| 4928 | return 0; |
| 4929 | |
| 4930 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4931 | struct file *file = io_file_from_index(ctx, total); |
| 4932 | |
| 4933 | if (file) |
| 4934 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4935 | total++; |
| 4936 | } |
| 4937 | |
| 4938 | return ret; |
| 4939 | } |
| 4940 | #else |
| 4941 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 4942 | { |
| 4943 | return 0; |
| 4944 | } |
| 4945 | #endif |
| 4946 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4947 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 4948 | unsigned nr_files) |
| 4949 | { |
| 4950 | int i; |
| 4951 | |
| 4952 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4953 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4954 | unsigned this_files; |
| 4955 | |
| 4956 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 4957 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 4958 | GFP_KERNEL); |
| 4959 | if (!table->files) |
| 4960 | break; |
| 4961 | nr_files -= this_files; |
| 4962 | } |
| 4963 | |
| 4964 | if (i == nr_tables) |
| 4965 | return 0; |
| 4966 | |
| 4967 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4968 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4969 | kfree(table->files); |
| 4970 | } |
| 4971 | return 1; |
| 4972 | } |
| 4973 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4974 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 4975 | { |
| 4976 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 4977 | struct sock *sock = ctx->ring_sock->sk; |
| 4978 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 4979 | struct sk_buff *skb; |
| 4980 | int i; |
| 4981 | |
| 4982 | __skb_queue_head_init(&list); |
| 4983 | |
| 4984 | /* |
| 4985 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 4986 | * remove this entry and rearrange the file array. |
| 4987 | */ |
| 4988 | skb = skb_dequeue(head); |
| 4989 | while (skb) { |
| 4990 | struct scm_fp_list *fp; |
| 4991 | |
| 4992 | fp = UNIXCB(skb).fp; |
| 4993 | for (i = 0; i < fp->count; i++) { |
| 4994 | int left; |
| 4995 | |
| 4996 | if (fp->fp[i] != file) |
| 4997 | continue; |
| 4998 | |
| 4999 | unix_notinflight(fp->user, fp->fp[i]); |
| 5000 | left = fp->count - 1 - i; |
| 5001 | if (left) { |
| 5002 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 5003 | left * sizeof(struct file *)); |
| 5004 | } |
| 5005 | fp->count--; |
| 5006 | if (!fp->count) { |
| 5007 | kfree_skb(skb); |
| 5008 | skb = NULL; |
| 5009 | } else { |
| 5010 | __skb_queue_tail(&list, skb); |
| 5011 | } |
| 5012 | fput(file); |
| 5013 | file = NULL; |
| 5014 | break; |
| 5015 | } |
| 5016 | |
| 5017 | if (!file) |
| 5018 | break; |
| 5019 | |
| 5020 | __skb_queue_tail(&list, skb); |
| 5021 | |
| 5022 | skb = skb_dequeue(head); |
| 5023 | } |
| 5024 | |
| 5025 | if (skb_peek(&list)) { |
| 5026 | spin_lock_irq(&head->lock); |
| 5027 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 5028 | __skb_queue_tail(head, skb); |
| 5029 | spin_unlock_irq(&head->lock); |
| 5030 | } |
| 5031 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5032 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5033 | #endif |
| 5034 | } |
| 5035 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5036 | struct io_file_put { |
| 5037 | struct llist_node llist; |
| 5038 | struct file *file; |
| 5039 | struct completion *done; |
| 5040 | }; |
| 5041 | |
| 5042 | static void io_ring_file_ref_switch(struct work_struct *work) |
| 5043 | { |
| 5044 | struct io_file_put *pfile, *tmp; |
| 5045 | struct fixed_file_data *data; |
| 5046 | struct llist_node *node; |
| 5047 | |
| 5048 | data = container_of(work, struct fixed_file_data, ref_work); |
| 5049 | |
| 5050 | while ((node = llist_del_all(&data->put_llist)) != NULL) { |
| 5051 | llist_for_each_entry_safe(pfile, tmp, node, llist) { |
| 5052 | io_ring_file_put(data->ctx, pfile->file); |
| 5053 | if (pfile->done) |
| 5054 | complete(pfile->done); |
| 5055 | else |
| 5056 | kfree(pfile); |
| 5057 | } |
| 5058 | } |
| 5059 | |
| 5060 | percpu_ref_get(&data->refs); |
| 5061 | percpu_ref_switch_to_percpu(&data->refs); |
| 5062 | } |
| 5063 | |
| 5064 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 5065 | { |
| 5066 | struct fixed_file_data *data; |
| 5067 | |
| 5068 | data = container_of(ref, struct fixed_file_data, refs); |
| 5069 | |
| 5070 | /* we can't safely switch from inside this context, punt to wq */ |
| 5071 | queue_work(system_wq, &data->ref_work); |
| 5072 | } |
| 5073 | |
| 5074 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5075 | unsigned nr_args) |
| 5076 | { |
| 5077 | __s32 __user *fds = (__s32 __user *) arg; |
| 5078 | unsigned nr_tables; |
| 5079 | struct file *file; |
| 5080 | int fd, ret = 0; |
| 5081 | unsigned i; |
| 5082 | |
| 5083 | if (ctx->file_data) |
| 5084 | return -EBUSY; |
| 5085 | if (!nr_args) |
| 5086 | return -EINVAL; |
| 5087 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 5088 | return -EMFILE; |
| 5089 | |
| 5090 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 5091 | if (!ctx->file_data) |
| 5092 | return -ENOMEM; |
| 5093 | ctx->file_data->ctx = ctx; |
| 5094 | init_completion(&ctx->file_data->done); |
| 5095 | |
| 5096 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 5097 | ctx->file_data->table = kcalloc(nr_tables, |
| 5098 | sizeof(struct fixed_file_table), |
| 5099 | GFP_KERNEL); |
| 5100 | if (!ctx->file_data->table) { |
| 5101 | kfree(ctx->file_data); |
| 5102 | ctx->file_data = NULL; |
| 5103 | return -ENOMEM; |
| 5104 | } |
| 5105 | |
| 5106 | if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero, |
| 5107 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 5108 | kfree(ctx->file_data->table); |
| 5109 | kfree(ctx->file_data); |
| 5110 | ctx->file_data = NULL; |
| 5111 | return -ENOMEM; |
| 5112 | } |
| 5113 | ctx->file_data->put_llist.first = NULL; |
| 5114 | INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch); |
| 5115 | |
| 5116 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 5117 | percpu_ref_exit(&ctx->file_data->refs); |
| 5118 | kfree(ctx->file_data->table); |
| 5119 | kfree(ctx->file_data); |
| 5120 | ctx->file_data = NULL; |
| 5121 | return -ENOMEM; |
| 5122 | } |
| 5123 | |
| 5124 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 5125 | struct fixed_file_table *table; |
| 5126 | unsigned index; |
| 5127 | |
| 5128 | ret = -EFAULT; |
| 5129 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 5130 | break; |
| 5131 | /* allow sparse sets */ |
| 5132 | if (fd == -1) { |
| 5133 | ret = 0; |
| 5134 | continue; |
| 5135 | } |
| 5136 | |
| 5137 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 5138 | index = i & IORING_FILE_TABLE_MASK; |
| 5139 | file = fget(fd); |
| 5140 | |
| 5141 | ret = -EBADF; |
| 5142 | if (!file) |
| 5143 | break; |
| 5144 | |
| 5145 | /* |
| 5146 | * Don't allow io_uring instances to be registered. If UNIX |
| 5147 | * isn't enabled, then this causes a reference cycle and this |
| 5148 | * instance can never get freed. If UNIX is enabled we'll |
| 5149 | * handle it just fine, but there's still no point in allowing |
| 5150 | * a ring fd as it doesn't support regular read/write anyway. |
| 5151 | */ |
| 5152 | if (file->f_op == &io_uring_fops) { |
| 5153 | fput(file); |
| 5154 | break; |
| 5155 | } |
| 5156 | ret = 0; |
| 5157 | table->files[index] = file; |
| 5158 | } |
| 5159 | |
| 5160 | if (ret) { |
| 5161 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5162 | file = io_file_from_index(ctx, i); |
| 5163 | if (file) |
| 5164 | fput(file); |
| 5165 | } |
| 5166 | for (i = 0; i < nr_tables; i++) |
| 5167 | kfree(ctx->file_data->table[i].files); |
| 5168 | |
| 5169 | kfree(ctx->file_data->table); |
| 5170 | kfree(ctx->file_data); |
| 5171 | ctx->file_data = NULL; |
| 5172 | ctx->nr_user_files = 0; |
| 5173 | return ret; |
| 5174 | } |
| 5175 | |
| 5176 | ret = io_sqe_files_scm(ctx); |
| 5177 | if (ret) |
| 5178 | io_sqe_files_unregister(ctx); |
| 5179 | |
| 5180 | return ret; |
| 5181 | } |
| 5182 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5183 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 5184 | int index) |
| 5185 | { |
| 5186 | #if defined(CONFIG_UNIX) |
| 5187 | struct sock *sock = ctx->ring_sock->sk; |
| 5188 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 5189 | struct sk_buff *skb; |
| 5190 | |
| 5191 | /* |
| 5192 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 5193 | * file set. If there's no room, fall back to allocating a new skb |
| 5194 | * and filling it in. |
| 5195 | */ |
| 5196 | spin_lock_irq(&head->lock); |
| 5197 | skb = skb_peek(head); |
| 5198 | if (skb) { |
| 5199 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 5200 | |
| 5201 | if (fpl->count < SCM_MAX_FD) { |
| 5202 | __skb_unlink(skb, head); |
| 5203 | spin_unlock_irq(&head->lock); |
| 5204 | fpl->fp[fpl->count] = get_file(file); |
| 5205 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 5206 | fpl->count++; |
| 5207 | spin_lock_irq(&head->lock); |
| 5208 | __skb_queue_head(head, skb); |
| 5209 | } else { |
| 5210 | skb = NULL; |
| 5211 | } |
| 5212 | } |
| 5213 | spin_unlock_irq(&head->lock); |
| 5214 | |
| 5215 | if (skb) { |
| 5216 | fput(file); |
| 5217 | return 0; |
| 5218 | } |
| 5219 | |
| 5220 | return __io_sqe_files_scm(ctx, 1, index); |
| 5221 | #else |
| 5222 | return 0; |
| 5223 | #endif |
| 5224 | } |
| 5225 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5226 | static void io_atomic_switch(struct percpu_ref *ref) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5227 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5228 | struct fixed_file_data *data; |
| 5229 | |
| 5230 | data = container_of(ref, struct fixed_file_data, refs); |
| 5231 | clear_bit(FFD_F_ATOMIC, &data->state); |
| 5232 | } |
| 5233 | |
| 5234 | static bool io_queue_file_removal(struct fixed_file_data *data, |
| 5235 | struct file *file) |
| 5236 | { |
| 5237 | struct io_file_put *pfile, pfile_stack; |
| 5238 | DECLARE_COMPLETION_ONSTACK(done); |
| 5239 | |
| 5240 | /* |
| 5241 | * If we fail allocating the struct we need for doing async reomval |
| 5242 | * of this file, just punt to sync and wait for it. |
| 5243 | */ |
| 5244 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
| 5245 | if (!pfile) { |
| 5246 | pfile = &pfile_stack; |
| 5247 | pfile->done = &done; |
| 5248 | } |
| 5249 | |
| 5250 | pfile->file = file; |
| 5251 | llist_add(&pfile->llist, &data->put_llist); |
| 5252 | |
| 5253 | if (pfile == &pfile_stack) { |
| 5254 | if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5255 | percpu_ref_put(&data->refs); |
| 5256 | percpu_ref_switch_to_atomic(&data->refs, |
| 5257 | io_atomic_switch); |
| 5258 | } |
| 5259 | wait_for_completion(&done); |
| 5260 | flush_work(&data->ref_work); |
| 5261 | return false; |
| 5262 | } |
| 5263 | |
| 5264 | return true; |
| 5265 | } |
| 5266 | |
| 5267 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 5268 | struct io_uring_files_update *up, |
| 5269 | unsigned nr_args) |
| 5270 | { |
| 5271 | struct fixed_file_data *data = ctx->file_data; |
| 5272 | bool ref_switch = false; |
| 5273 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5274 | __s32 __user *fds; |
| 5275 | int fd, i, err; |
| 5276 | __u32 done; |
| 5277 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5278 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5279 | return -EOVERFLOW; |
| 5280 | if (done > ctx->nr_user_files) |
| 5281 | return -EINVAL; |
| 5282 | |
| 5283 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5284 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5285 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5286 | struct fixed_file_table *table; |
| 5287 | unsigned index; |
| 5288 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5289 | err = 0; |
| 5290 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 5291 | err = -EFAULT; |
| 5292 | break; |
| 5293 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5294 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 5295 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5296 | index = i & IORING_FILE_TABLE_MASK; |
| 5297 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5298 | file = io_file_from_index(ctx, index); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5299 | table->files[index] = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5300 | if (io_queue_file_removal(data, file)) |
| 5301 | ref_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5302 | } |
| 5303 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5304 | file = fget(fd); |
| 5305 | if (!file) { |
| 5306 | err = -EBADF; |
| 5307 | break; |
| 5308 | } |
| 5309 | /* |
| 5310 | * Don't allow io_uring instances to be registered. If |
| 5311 | * UNIX isn't enabled, then this causes a reference |
| 5312 | * cycle and this instance can never get freed. If UNIX |
| 5313 | * is enabled we'll handle it just fine, but there's |
| 5314 | * still no point in allowing a ring fd as it doesn't |
| 5315 | * support regular read/write anyway. |
| 5316 | */ |
| 5317 | if (file->f_op == &io_uring_fops) { |
| 5318 | fput(file); |
| 5319 | err = -EBADF; |
| 5320 | break; |
| 5321 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5322 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5323 | err = io_sqe_file_register(ctx, file, i); |
| 5324 | if (err) |
| 5325 | break; |
| 5326 | } |
| 5327 | nr_args--; |
| 5328 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5329 | up->offset++; |
| 5330 | } |
| 5331 | |
| 5332 | if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5333 | percpu_ref_put(&data->refs); |
| 5334 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5335 | } |
| 5336 | |
| 5337 | return done ? done : err; |
| 5338 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5339 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 5340 | unsigned nr_args) |
| 5341 | { |
| 5342 | struct io_uring_files_update up; |
| 5343 | |
| 5344 | if (!ctx->file_data) |
| 5345 | return -ENXIO; |
| 5346 | if (!nr_args) |
| 5347 | return -EINVAL; |
| 5348 | if (copy_from_user(&up, arg, sizeof(up))) |
| 5349 | return -EFAULT; |
| 5350 | if (up.resv) |
| 5351 | return -EINVAL; |
| 5352 | |
| 5353 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 5354 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5355 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 5356 | static void io_put_work(struct io_wq_work *work) |
| 5357 | { |
| 5358 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5359 | |
| 5360 | io_put_req(req); |
| 5361 | } |
| 5362 | |
| 5363 | static void io_get_work(struct io_wq_work *work) |
| 5364 | { |
| 5365 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5366 | |
| 5367 | refcount_inc(&req->refs); |
| 5368 | } |
| 5369 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5370 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 5371 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5372 | { |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5373 | struct io_wq_data data; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5374 | unsigned concurrency; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5375 | int ret; |
| 5376 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5377 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5378 | mmgrab(current->mm); |
| 5379 | ctx->sqo_mm = current->mm; |
| 5380 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5381 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 5382 | ret = -EPERM; |
| 5383 | if (!capable(CAP_SYS_ADMIN)) |
| 5384 | goto err; |
| 5385 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5386 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 5387 | if (!ctx->sq_thread_idle) |
| 5388 | ctx->sq_thread_idle = HZ; |
| 5389 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5390 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5391 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5392 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5393 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5394 | if (cpu >= nr_cpu_ids) |
| 5395 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 5396 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5397 | goto err; |
| 5398 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5399 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 5400 | ctx, cpu, |
| 5401 | "io_uring-sq"); |
| 5402 | } else { |
| 5403 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 5404 | "io_uring-sq"); |
| 5405 | } |
| 5406 | if (IS_ERR(ctx->sqo_thread)) { |
| 5407 | ret = PTR_ERR(ctx->sqo_thread); |
| 5408 | ctx->sqo_thread = NULL; |
| 5409 | goto err; |
| 5410 | } |
| 5411 | wake_up_process(ctx->sqo_thread); |
| 5412 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 5413 | /* Can't have SQ_AFF without SQPOLL */ |
| 5414 | ret = -EINVAL; |
| 5415 | goto err; |
| 5416 | } |
| 5417 | |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5418 | data.mm = ctx->sqo_mm; |
| 5419 | data.user = ctx->user; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5420 | data.creds = ctx->creds; |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5421 | data.get_work = io_get_work; |
| 5422 | data.put_work = io_put_work; |
| 5423 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5424 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 5425 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5426 | ctx->io_wq = io_wq_create(concurrency, &data); |
Jens Axboe | 975c99a5 | 2019-10-30 08:42:56 -0600 | [diff] [blame] | 5427 | if (IS_ERR(ctx->io_wq)) { |
| 5428 | ret = PTR_ERR(ctx->io_wq); |
| 5429 | ctx->io_wq = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5430 | goto err; |
| 5431 | } |
| 5432 | |
| 5433 | return 0; |
| 5434 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 5435 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5436 | mmdrop(ctx->sqo_mm); |
| 5437 | ctx->sqo_mm = NULL; |
| 5438 | return ret; |
| 5439 | } |
| 5440 | |
| 5441 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 5442 | { |
| 5443 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 5444 | } |
| 5445 | |
| 5446 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 5447 | { |
| 5448 | unsigned long page_limit, cur_pages, new_pages; |
| 5449 | |
| 5450 | /* Don't allow more pages than we can safely lock */ |
| 5451 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 5452 | |
| 5453 | do { |
| 5454 | cur_pages = atomic_long_read(&user->locked_vm); |
| 5455 | new_pages = cur_pages + nr_pages; |
| 5456 | if (new_pages > page_limit) |
| 5457 | return -ENOMEM; |
| 5458 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 5459 | new_pages) != cur_pages); |
| 5460 | |
| 5461 | return 0; |
| 5462 | } |
| 5463 | |
| 5464 | static void io_mem_free(void *ptr) |
| 5465 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 5466 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5467 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 5468 | if (!ptr) |
| 5469 | return; |
| 5470 | |
| 5471 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5472 | if (put_page_testzero(page)) |
| 5473 | free_compound_page(page); |
| 5474 | } |
| 5475 | |
| 5476 | static void *io_mem_alloc(size_t size) |
| 5477 | { |
| 5478 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 5479 | __GFP_NORETRY; |
| 5480 | |
| 5481 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 5482 | } |
| 5483 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5484 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 5485 | size_t *sq_offset) |
| 5486 | { |
| 5487 | struct io_rings *rings; |
| 5488 | size_t off, sq_array_size; |
| 5489 | |
| 5490 | off = struct_size(rings, cqes, cq_entries); |
| 5491 | if (off == SIZE_MAX) |
| 5492 | return SIZE_MAX; |
| 5493 | |
| 5494 | #ifdef CONFIG_SMP |
| 5495 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 5496 | if (off == 0) |
| 5497 | return SIZE_MAX; |
| 5498 | #endif |
| 5499 | |
| 5500 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 5501 | if (sq_array_size == SIZE_MAX) |
| 5502 | return SIZE_MAX; |
| 5503 | |
| 5504 | if (check_add_overflow(off, sq_array_size, &off)) |
| 5505 | return SIZE_MAX; |
| 5506 | |
| 5507 | if (sq_offset) |
| 5508 | *sq_offset = off; |
| 5509 | |
| 5510 | return off; |
| 5511 | } |
| 5512 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5513 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 5514 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5515 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5516 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5517 | pages = (size_t)1 << get_order( |
| 5518 | rings_size(sq_entries, cq_entries, NULL)); |
| 5519 | pages += (size_t)1 << get_order( |
| 5520 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5521 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5522 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5523 | } |
| 5524 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5525 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 5526 | { |
| 5527 | int i, j; |
| 5528 | |
| 5529 | if (!ctx->user_bufs) |
| 5530 | return -ENXIO; |
| 5531 | |
| 5532 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 5533 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 5534 | |
| 5535 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 5536 | put_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5537 | |
| 5538 | if (ctx->account_mem) |
| 5539 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5540 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5541 | imu->nr_bvecs = 0; |
| 5542 | } |
| 5543 | |
| 5544 | kfree(ctx->user_bufs); |
| 5545 | ctx->user_bufs = NULL; |
| 5546 | ctx->nr_user_bufs = 0; |
| 5547 | return 0; |
| 5548 | } |
| 5549 | |
| 5550 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 5551 | void __user *arg, unsigned index) |
| 5552 | { |
| 5553 | struct iovec __user *src; |
| 5554 | |
| 5555 | #ifdef CONFIG_COMPAT |
| 5556 | if (ctx->compat) { |
| 5557 | struct compat_iovec __user *ciovs; |
| 5558 | struct compat_iovec ciov; |
| 5559 | |
| 5560 | ciovs = (struct compat_iovec __user *) arg; |
| 5561 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 5562 | return -EFAULT; |
| 5563 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 5564 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5565 | dst->iov_len = ciov.iov_len; |
| 5566 | return 0; |
| 5567 | } |
| 5568 | #endif |
| 5569 | src = (struct iovec __user *) arg; |
| 5570 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 5571 | return -EFAULT; |
| 5572 | return 0; |
| 5573 | } |
| 5574 | |
| 5575 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5576 | unsigned nr_args) |
| 5577 | { |
| 5578 | struct vm_area_struct **vmas = NULL; |
| 5579 | struct page **pages = NULL; |
| 5580 | int i, j, got_pages = 0; |
| 5581 | int ret = -EINVAL; |
| 5582 | |
| 5583 | if (ctx->user_bufs) |
| 5584 | return -EBUSY; |
| 5585 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 5586 | return -EINVAL; |
| 5587 | |
| 5588 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 5589 | GFP_KERNEL); |
| 5590 | if (!ctx->user_bufs) |
| 5591 | return -ENOMEM; |
| 5592 | |
| 5593 | for (i = 0; i < nr_args; i++) { |
| 5594 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 5595 | unsigned long off, start, end, ubuf; |
| 5596 | int pret, nr_pages; |
| 5597 | struct iovec iov; |
| 5598 | size_t size; |
| 5599 | |
| 5600 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 5601 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 5602 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5603 | |
| 5604 | /* |
| 5605 | * Don't impose further limits on the size and buffer |
| 5606 | * constraints here, we'll -EINVAL later when IO is |
| 5607 | * submitted if they are wrong. |
| 5608 | */ |
| 5609 | ret = -EFAULT; |
| 5610 | if (!iov.iov_base || !iov.iov_len) |
| 5611 | goto err; |
| 5612 | |
| 5613 | /* arbitrary limit, but we need something */ |
| 5614 | if (iov.iov_len > SZ_1G) |
| 5615 | goto err; |
| 5616 | |
| 5617 | ubuf = (unsigned long) iov.iov_base; |
| 5618 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 5619 | start = ubuf >> PAGE_SHIFT; |
| 5620 | nr_pages = end - start; |
| 5621 | |
| 5622 | if (ctx->account_mem) { |
| 5623 | ret = io_account_mem(ctx->user, nr_pages); |
| 5624 | if (ret) |
| 5625 | goto err; |
| 5626 | } |
| 5627 | |
| 5628 | ret = 0; |
| 5629 | if (!pages || nr_pages > got_pages) { |
| 5630 | kfree(vmas); |
| 5631 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5632 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5633 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5634 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5635 | sizeof(struct vm_area_struct *), |
| 5636 | GFP_KERNEL); |
| 5637 | if (!pages || !vmas) { |
| 5638 | ret = -ENOMEM; |
| 5639 | if (ctx->account_mem) |
| 5640 | io_unaccount_mem(ctx->user, nr_pages); |
| 5641 | goto err; |
| 5642 | } |
| 5643 | got_pages = nr_pages; |
| 5644 | } |
| 5645 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5646 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5647 | GFP_KERNEL); |
| 5648 | ret = -ENOMEM; |
| 5649 | if (!imu->bvec) { |
| 5650 | if (ctx->account_mem) |
| 5651 | io_unaccount_mem(ctx->user, nr_pages); |
| 5652 | goto err; |
| 5653 | } |
| 5654 | |
| 5655 | ret = 0; |
| 5656 | down_read(¤t->mm->mmap_sem); |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 5657 | pret = get_user_pages(ubuf, nr_pages, |
| 5658 | FOLL_WRITE | FOLL_LONGTERM, |
| 5659 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5660 | if (pret == nr_pages) { |
| 5661 | /* don't support file backed memory */ |
| 5662 | for (j = 0; j < nr_pages; j++) { |
| 5663 | struct vm_area_struct *vma = vmas[j]; |
| 5664 | |
| 5665 | if (vma->vm_file && |
| 5666 | !is_file_hugepages(vma->vm_file)) { |
| 5667 | ret = -EOPNOTSUPP; |
| 5668 | break; |
| 5669 | } |
| 5670 | } |
| 5671 | } else { |
| 5672 | ret = pret < 0 ? pret : -EFAULT; |
| 5673 | } |
| 5674 | up_read(¤t->mm->mmap_sem); |
| 5675 | if (ret) { |
| 5676 | /* |
| 5677 | * if we did partial map, or found file backed vmas, |
| 5678 | * release any pages we did get |
| 5679 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 5680 | if (pret > 0) |
| 5681 | put_user_pages(pages, pret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5682 | if (ctx->account_mem) |
| 5683 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5684 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5685 | goto err; |
| 5686 | } |
| 5687 | |
| 5688 | off = ubuf & ~PAGE_MASK; |
| 5689 | size = iov.iov_len; |
| 5690 | for (j = 0; j < nr_pages; j++) { |
| 5691 | size_t vec_len; |
| 5692 | |
| 5693 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 5694 | imu->bvec[j].bv_page = pages[j]; |
| 5695 | imu->bvec[j].bv_len = vec_len; |
| 5696 | imu->bvec[j].bv_offset = off; |
| 5697 | off = 0; |
| 5698 | size -= vec_len; |
| 5699 | } |
| 5700 | /* store original address for later verification */ |
| 5701 | imu->ubuf = ubuf; |
| 5702 | imu->len = iov.iov_len; |
| 5703 | imu->nr_bvecs = nr_pages; |
| 5704 | |
| 5705 | ctx->nr_user_bufs++; |
| 5706 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5707 | kvfree(pages); |
| 5708 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5709 | return 0; |
| 5710 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5711 | kvfree(pages); |
| 5712 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5713 | io_sqe_buffer_unregister(ctx); |
| 5714 | return ret; |
| 5715 | } |
| 5716 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 5717 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 5718 | { |
| 5719 | __s32 __user *fds = arg; |
| 5720 | int fd; |
| 5721 | |
| 5722 | if (ctx->cq_ev_fd) |
| 5723 | return -EBUSY; |
| 5724 | |
| 5725 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 5726 | return -EFAULT; |
| 5727 | |
| 5728 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 5729 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 5730 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 5731 | ctx->cq_ev_fd = NULL; |
| 5732 | return ret; |
| 5733 | } |
| 5734 | |
| 5735 | return 0; |
| 5736 | } |
| 5737 | |
| 5738 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 5739 | { |
| 5740 | if (ctx->cq_ev_fd) { |
| 5741 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 5742 | ctx->cq_ev_fd = NULL; |
| 5743 | return 0; |
| 5744 | } |
| 5745 | |
| 5746 | return -ENXIO; |
| 5747 | } |
| 5748 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5749 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 5750 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5751 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5752 | if (ctx->sqo_mm) |
| 5753 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5754 | |
| 5755 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5756 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5757 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 5758 | io_eventfd_unregister(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5759 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5760 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 5761 | if (ctx->ring_sock) { |
| 5762 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5763 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 5764 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5765 | #endif |
| 5766 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5767 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5768 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5769 | |
| 5770 | percpu_ref_exit(&ctx->refs); |
| 5771 | if (ctx->account_mem) |
| 5772 | io_unaccount_mem(ctx->user, |
| 5773 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 5774 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5775 | put_cred(ctx->creds); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5776 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5777 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 5778 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5779 | kfree(ctx); |
| 5780 | } |
| 5781 | |
| 5782 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 5783 | { |
| 5784 | struct io_ring_ctx *ctx = file->private_data; |
| 5785 | __poll_t mask = 0; |
| 5786 | |
| 5787 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 5788 | /* |
| 5789 | * synchronizes with barrier from wq_has_sleeper call in |
| 5790 | * io_commit_cqring |
| 5791 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5792 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5793 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 5794 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5795 | mask |= EPOLLOUT | EPOLLWRNORM; |
yangerkun | daa5de5 | 2019-09-24 20:53:34 +0800 | [diff] [blame] | 5796 | if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5797 | mask |= EPOLLIN | EPOLLRDNORM; |
| 5798 | |
| 5799 | return mask; |
| 5800 | } |
| 5801 | |
| 5802 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 5803 | { |
| 5804 | struct io_ring_ctx *ctx = file->private_data; |
| 5805 | |
| 5806 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 5807 | } |
| 5808 | |
| 5809 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 5810 | { |
| 5811 | mutex_lock(&ctx->uring_lock); |
| 5812 | percpu_ref_kill(&ctx->refs); |
| 5813 | mutex_unlock(&ctx->uring_lock); |
| 5814 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5815 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5816 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5817 | |
| 5818 | if (ctx->io_wq) |
| 5819 | io_wq_cancel_all(ctx->io_wq); |
| 5820 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5821 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 5822 | /* if we failed setting up the ctx, we might not have any rings */ |
| 5823 | if (ctx->rings) |
| 5824 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5825 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5826 | io_ring_ctx_free(ctx); |
| 5827 | } |
| 5828 | |
| 5829 | static int io_uring_release(struct inode *inode, struct file *file) |
| 5830 | { |
| 5831 | struct io_ring_ctx *ctx = file->private_data; |
| 5832 | |
| 5833 | file->private_data = NULL; |
| 5834 | io_ring_ctx_wait_and_kill(ctx); |
| 5835 | return 0; |
| 5836 | } |
| 5837 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5838 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 5839 | struct files_struct *files) |
| 5840 | { |
| 5841 | struct io_kiocb *req; |
| 5842 | DEFINE_WAIT(wait); |
| 5843 | |
| 5844 | while (!list_empty_careful(&ctx->inflight_list)) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 5845 | struct io_kiocb *cancel_req = NULL; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5846 | |
| 5847 | spin_lock_irq(&ctx->inflight_lock); |
| 5848 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 5849 | if (req->work.files != files) |
| 5850 | continue; |
| 5851 | /* req is being completed, ignore */ |
| 5852 | if (!refcount_inc_not_zero(&req->refs)) |
| 5853 | continue; |
| 5854 | cancel_req = req; |
| 5855 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5856 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 5857 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5858 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 5859 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5860 | spin_unlock_irq(&ctx->inflight_lock); |
| 5861 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 5862 | /* We need to keep going until we don't find a matching req */ |
| 5863 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5864 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 5865 | |
| 5866 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 5867 | io_put_req(cancel_req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5868 | schedule(); |
| 5869 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 5870 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5871 | } |
| 5872 | |
| 5873 | static int io_uring_flush(struct file *file, void *data) |
| 5874 | { |
| 5875 | struct io_ring_ctx *ctx = file->private_data; |
| 5876 | |
| 5877 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5878 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
| 5879 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5880 | io_wq_cancel_all(ctx->io_wq); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5881 | } |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 5882 | return 0; |
| 5883 | } |
| 5884 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 5885 | static void *io_uring_validate_mmap_request(struct file *file, |
| 5886 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5887 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5888 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 5889 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5890 | struct page *page; |
| 5891 | void *ptr; |
| 5892 | |
| 5893 | switch (offset) { |
| 5894 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5895 | case IORING_OFF_CQ_RING: |
| 5896 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5897 | break; |
| 5898 | case IORING_OFF_SQES: |
| 5899 | ptr = ctx->sq_sqes; |
| 5900 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5901 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 5902 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5903 | } |
| 5904 | |
| 5905 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 5906 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 5907 | return ERR_PTR(-EINVAL); |
| 5908 | |
| 5909 | return ptr; |
| 5910 | } |
| 5911 | |
| 5912 | #ifdef CONFIG_MMU |
| 5913 | |
| 5914 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 5915 | { |
| 5916 | size_t sz = vma->vm_end - vma->vm_start; |
| 5917 | unsigned long pfn; |
| 5918 | void *ptr; |
| 5919 | |
| 5920 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 5921 | if (IS_ERR(ptr)) |
| 5922 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5923 | |
| 5924 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 5925 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 5926 | } |
| 5927 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 5928 | #else /* !CONFIG_MMU */ |
| 5929 | |
| 5930 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 5931 | { |
| 5932 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 5933 | } |
| 5934 | |
| 5935 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 5936 | { |
| 5937 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 5938 | } |
| 5939 | |
| 5940 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 5941 | unsigned long addr, unsigned long len, |
| 5942 | unsigned long pgoff, unsigned long flags) |
| 5943 | { |
| 5944 | void *ptr; |
| 5945 | |
| 5946 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 5947 | if (IS_ERR(ptr)) |
| 5948 | return PTR_ERR(ptr); |
| 5949 | |
| 5950 | return (unsigned long) ptr; |
| 5951 | } |
| 5952 | |
| 5953 | #endif /* !CONFIG_MMU */ |
| 5954 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5955 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 5956 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 5957 | size_t, sigsz) |
| 5958 | { |
| 5959 | struct io_ring_ctx *ctx; |
| 5960 | long ret = -EBADF; |
| 5961 | int submitted = 0; |
| 5962 | struct fd f; |
| 5963 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5964 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5965 | return -EINVAL; |
| 5966 | |
| 5967 | f = fdget(fd); |
| 5968 | if (!f.file) |
| 5969 | return -EBADF; |
| 5970 | |
| 5971 | ret = -EOPNOTSUPP; |
| 5972 | if (f.file->f_op != &io_uring_fops) |
| 5973 | goto out_fput; |
| 5974 | |
| 5975 | ret = -ENXIO; |
| 5976 | ctx = f.file->private_data; |
| 5977 | if (!percpu_ref_tryget(&ctx->refs)) |
| 5978 | goto out_fput; |
| 5979 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5980 | /* |
| 5981 | * For SQ polling, the thread will do all submissions and completions. |
| 5982 | * Just return the requested submit count, and wake the thread if |
| 5983 | * we were asked to. |
| 5984 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 5985 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5986 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 5987 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 5988 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5989 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 5990 | wake_up(&ctx->sqo_wait); |
| 5991 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 5992 | } else if (to_submit) { |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 5993 | struct mm_struct *cur_mm; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5994 | |
Jens Axboe | 44d2827 | 2020-01-16 19:00:24 -0700 | [diff] [blame] | 5995 | if (current->mm != ctx->sqo_mm || |
| 5996 | current_cred() != ctx->creds) { |
| 5997 | ret = -EPERM; |
| 5998 | goto out; |
| 5999 | } |
| 6000 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6001 | to_submit = min(to_submit, ctx->sq_entries); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6002 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6003 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
| 6004 | cur_mm = ctx->sqo_mm; |
| 6005 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, |
| 6006 | &cur_mm, false); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6007 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6008 | |
| 6009 | if (submitted != to_submit) |
| 6010 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6011 | } |
| 6012 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6013 | unsigned nr_events = 0; |
| 6014 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6015 | min_complete = min(min_complete, ctx->cq_entries); |
| 6016 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6017 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6018 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6019 | } else { |
| 6020 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 6021 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6022 | } |
| 6023 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6024 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 6025 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6026 | out_fput: |
| 6027 | fdput(f); |
| 6028 | return submitted ? submitted : ret; |
| 6029 | } |
| 6030 | |
| 6031 | static const struct file_operations io_uring_fops = { |
| 6032 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6033 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6034 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6035 | #ifndef CONFIG_MMU |
| 6036 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 6037 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 6038 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6039 | .poll = io_uring_poll, |
| 6040 | .fasync = io_uring_fasync, |
| 6041 | }; |
| 6042 | |
| 6043 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 6044 | struct io_uring_params *p) |
| 6045 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6046 | struct io_rings *rings; |
| 6047 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6048 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6049 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 6050 | if (size == SIZE_MAX) |
| 6051 | return -EOVERFLOW; |
| 6052 | |
| 6053 | rings = io_mem_alloc(size); |
| 6054 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6055 | return -ENOMEM; |
| 6056 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6057 | ctx->rings = rings; |
| 6058 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 6059 | rings->sq_ring_mask = p->sq_entries - 1; |
| 6060 | rings->cq_ring_mask = p->cq_entries - 1; |
| 6061 | rings->sq_ring_entries = p->sq_entries; |
| 6062 | rings->cq_ring_entries = p->cq_entries; |
| 6063 | ctx->sq_mask = rings->sq_ring_mask; |
| 6064 | ctx->cq_mask = rings->cq_ring_mask; |
| 6065 | ctx->sq_entries = rings->sq_ring_entries; |
| 6066 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6067 | |
| 6068 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6069 | if (size == SIZE_MAX) { |
| 6070 | io_mem_free(ctx->rings); |
| 6071 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6072 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6073 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6074 | |
| 6075 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6076 | if (!ctx->sq_sqes) { |
| 6077 | io_mem_free(ctx->rings); |
| 6078 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6079 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6080 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6081 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6082 | return 0; |
| 6083 | } |
| 6084 | |
| 6085 | /* |
| 6086 | * Allocate an anonymous fd, this is what constitutes the application |
| 6087 | * visible backing of an io_uring instance. The application mmaps this |
| 6088 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 6089 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 6090 | */ |
| 6091 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 6092 | { |
| 6093 | struct file *file; |
| 6094 | int ret; |
| 6095 | |
| 6096 | #if defined(CONFIG_UNIX) |
| 6097 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 6098 | &ctx->ring_sock); |
| 6099 | if (ret) |
| 6100 | return ret; |
| 6101 | #endif |
| 6102 | |
| 6103 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 6104 | if (ret < 0) |
| 6105 | goto err; |
| 6106 | |
| 6107 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 6108 | O_RDWR | O_CLOEXEC); |
| 6109 | if (IS_ERR(file)) { |
| 6110 | put_unused_fd(ret); |
| 6111 | ret = PTR_ERR(file); |
| 6112 | goto err; |
| 6113 | } |
| 6114 | |
| 6115 | #if defined(CONFIG_UNIX) |
| 6116 | ctx->ring_sock->file = file; |
| 6117 | #endif |
| 6118 | fd_install(ret, file); |
| 6119 | return ret; |
| 6120 | err: |
| 6121 | #if defined(CONFIG_UNIX) |
| 6122 | sock_release(ctx->ring_sock); |
| 6123 | ctx->ring_sock = NULL; |
| 6124 | #endif |
| 6125 | return ret; |
| 6126 | } |
| 6127 | |
| 6128 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 6129 | { |
| 6130 | struct user_struct *user = NULL; |
| 6131 | struct io_ring_ctx *ctx; |
| 6132 | bool account_mem; |
| 6133 | int ret; |
| 6134 | |
| 6135 | if (!entries || entries > IORING_MAX_ENTRIES) |
| 6136 | return -EINVAL; |
| 6137 | |
| 6138 | /* |
| 6139 | * Use twice as many entries for the CQ ring. It's possible for the |
| 6140 | * application to drive a higher depth than the size of the SQ ring, |
| 6141 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6142 | * some flexibility in overcommitting a bit. If the application has |
| 6143 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 6144 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6145 | */ |
| 6146 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6147 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 6148 | /* |
| 6149 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 6150 | * to a power-of-two, if it isn't already. We do NOT impose |
| 6151 | * any cq vs sq ring sizing. |
| 6152 | */ |
| 6153 | if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES) |
| 6154 | return -EINVAL; |
| 6155 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 6156 | } else { |
| 6157 | p->cq_entries = 2 * p->sq_entries; |
| 6158 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6159 | |
| 6160 | user = get_uid(current_user()); |
| 6161 | account_mem = !capable(CAP_IPC_LOCK); |
| 6162 | |
| 6163 | if (account_mem) { |
| 6164 | ret = io_account_mem(user, |
| 6165 | ring_pages(p->sq_entries, p->cq_entries)); |
| 6166 | if (ret) { |
| 6167 | free_uid(user); |
| 6168 | return ret; |
| 6169 | } |
| 6170 | } |
| 6171 | |
| 6172 | ctx = io_ring_ctx_alloc(p); |
| 6173 | if (!ctx) { |
| 6174 | if (account_mem) |
| 6175 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 6176 | p->cq_entries)); |
| 6177 | free_uid(user); |
| 6178 | return -ENOMEM; |
| 6179 | } |
| 6180 | ctx->compat = in_compat_syscall(); |
| 6181 | ctx->account_mem = account_mem; |
| 6182 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 6183 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6184 | |
| 6185 | ret = io_allocate_scq_urings(ctx, p); |
| 6186 | if (ret) |
| 6187 | goto err; |
| 6188 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6189 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6190 | if (ret) |
| 6191 | goto err; |
| 6192 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6193 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6194 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 6195 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 6196 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 6197 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 6198 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 6199 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 6200 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6201 | |
| 6202 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6203 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 6204 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 6205 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 6206 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 6207 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 6208 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 6209 | |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 6210 | /* |
| 6211 | * Install ring fd as the very last thing, so we don't risk someone |
| 6212 | * having closed it before we finish setup |
| 6213 | */ |
| 6214 | ret = io_uring_get_fd(ctx); |
| 6215 | if (ret < 0) |
| 6216 | goto err; |
| 6217 | |
Jens Axboe | da8c969 | 2019-12-02 18:51:26 -0700 | [diff] [blame] | 6218 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 6219 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6220 | 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] | 6221 | return ret; |
| 6222 | err: |
| 6223 | io_ring_ctx_wait_and_kill(ctx); |
| 6224 | return ret; |
| 6225 | } |
| 6226 | |
| 6227 | /* |
| 6228 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 6229 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 6230 | * params structure passed in. |
| 6231 | */ |
| 6232 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 6233 | { |
| 6234 | struct io_uring_params p; |
| 6235 | long ret; |
| 6236 | int i; |
| 6237 | |
| 6238 | if (copy_from_user(&p, params, sizeof(p))) |
| 6239 | return -EFAULT; |
| 6240 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 6241 | if (p.resv[i]) |
| 6242 | return -EINVAL; |
| 6243 | } |
| 6244 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6245 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6246 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6247 | return -EINVAL; |
| 6248 | |
| 6249 | ret = io_uring_create(entries, &p); |
| 6250 | if (ret < 0) |
| 6251 | return ret; |
| 6252 | |
| 6253 | if (copy_to_user(params, &p, sizeof(p))) |
| 6254 | return -EFAULT; |
| 6255 | |
| 6256 | return ret; |
| 6257 | } |
| 6258 | |
| 6259 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 6260 | struct io_uring_params __user *, params) |
| 6261 | { |
| 6262 | return io_uring_setup(entries, params); |
| 6263 | } |
| 6264 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6265 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 6266 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 6267 | __releases(ctx->uring_lock) |
| 6268 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6269 | { |
| 6270 | int ret; |
| 6271 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 6272 | /* |
| 6273 | * We're inside the ring mutex, if the ref is already dying, then |
| 6274 | * someone else killed the ctx or is already going through |
| 6275 | * io_uring_register(). |
| 6276 | */ |
| 6277 | if (percpu_ref_is_dying(&ctx->refs)) |
| 6278 | return -ENXIO; |
| 6279 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6280 | if (opcode != IORING_UNREGISTER_FILES && |
| 6281 | opcode != IORING_REGISTER_FILES_UPDATE) { |
| 6282 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 6283 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6284 | /* |
| 6285 | * Drop uring mutex before waiting for references to exit. If |
| 6286 | * another thread is currently inside io_uring_enter() it might |
| 6287 | * need to grab the uring_lock to make progress. If we hold it |
| 6288 | * here across the drain wait, then we can deadlock. It's safe |
| 6289 | * to drop the mutex here, since no new references will come in |
| 6290 | * after we've killed the percpu ref. |
| 6291 | */ |
| 6292 | mutex_unlock(&ctx->uring_lock); |
| 6293 | wait_for_completion(&ctx->completions[0]); |
| 6294 | mutex_lock(&ctx->uring_lock); |
| 6295 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6296 | |
| 6297 | switch (opcode) { |
| 6298 | case IORING_REGISTER_BUFFERS: |
| 6299 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 6300 | break; |
| 6301 | case IORING_UNREGISTER_BUFFERS: |
| 6302 | ret = -EINVAL; |
| 6303 | if (arg || nr_args) |
| 6304 | break; |
| 6305 | ret = io_sqe_buffer_unregister(ctx); |
| 6306 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6307 | case IORING_REGISTER_FILES: |
| 6308 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 6309 | break; |
| 6310 | case IORING_UNREGISTER_FILES: |
| 6311 | ret = -EINVAL; |
| 6312 | if (arg || nr_args) |
| 6313 | break; |
| 6314 | ret = io_sqe_files_unregister(ctx); |
| 6315 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6316 | case IORING_REGISTER_FILES_UPDATE: |
| 6317 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 6318 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6319 | case IORING_REGISTER_EVENTFD: |
| 6320 | ret = -EINVAL; |
| 6321 | if (nr_args != 1) |
| 6322 | break; |
| 6323 | ret = io_eventfd_register(ctx, arg); |
| 6324 | break; |
| 6325 | case IORING_UNREGISTER_EVENTFD: |
| 6326 | ret = -EINVAL; |
| 6327 | if (arg || nr_args) |
| 6328 | break; |
| 6329 | ret = io_eventfd_unregister(ctx); |
| 6330 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6331 | default: |
| 6332 | ret = -EINVAL; |
| 6333 | break; |
| 6334 | } |
| 6335 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6336 | |
| 6337 | if (opcode != IORING_UNREGISTER_FILES && |
| 6338 | opcode != IORING_REGISTER_FILES_UPDATE) { |
| 6339 | /* bring the ctx back to life */ |
| 6340 | reinit_completion(&ctx->completions[0]); |
| 6341 | percpu_ref_reinit(&ctx->refs); |
| 6342 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6343 | return ret; |
| 6344 | } |
| 6345 | |
| 6346 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 6347 | void __user *, arg, unsigned int, nr_args) |
| 6348 | { |
| 6349 | struct io_ring_ctx *ctx; |
| 6350 | long ret = -EBADF; |
| 6351 | struct fd f; |
| 6352 | |
| 6353 | f = fdget(fd); |
| 6354 | if (!f.file) |
| 6355 | return -EBADF; |
| 6356 | |
| 6357 | ret = -EOPNOTSUPP; |
| 6358 | if (f.file->f_op != &io_uring_fops) |
| 6359 | goto out_fput; |
| 6360 | |
| 6361 | ctx = f.file->private_data; |
| 6362 | |
| 6363 | mutex_lock(&ctx->uring_lock); |
| 6364 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 6365 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6366 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 6367 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6368 | out_fput: |
| 6369 | fdput(f); |
| 6370 | return ret; |
| 6371 | } |
| 6372 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6373 | static int __init io_uring_init(void) |
| 6374 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6375 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6376 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 6377 | return 0; |
| 6378 | }; |
| 6379 | __initcall(io_uring_init); |