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; |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 205 | int compat: 1; |
| 206 | int account_mem: 1; |
| 207 | int cq_overflow_flushed: 1; |
| 208 | int drain_next: 1; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 209 | int eventfd_async: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 210 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 211 | /* |
| 212 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 213 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 214 | * |
| 215 | * This indirection could e.g. be used to assign fixed |
| 216 | * io_uring_sqe entries to operations and only submit them to |
| 217 | * the queue when needed. |
| 218 | * |
| 219 | * The kernel modifies neither the indices array nor the entries |
| 220 | * array. |
| 221 | */ |
| 222 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 223 | unsigned cached_sq_head; |
| 224 | unsigned sq_entries; |
| 225 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 226 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 227 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 228 | atomic_t cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 229 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 230 | |
| 231 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 232 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 233 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 234 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 235 | wait_queue_head_t inflight_wait; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 236 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 237 | } ____cacheline_aligned_in_smp; |
| 238 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 239 | struct io_rings *rings; |
| 240 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 241 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 242 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 243 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 244 | struct mm_struct *sqo_mm; |
| 245 | wait_queue_head_t sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 246 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 247 | /* |
| 248 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 249 | * readers must ensure that ->refs is alive as long as the file* is |
| 250 | * used. Only updated through io_uring_register(2). |
| 251 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 252 | struct fixed_file_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 253 | unsigned nr_user_files; |
| 254 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 255 | /* if used, fixed mapped user buffers */ |
| 256 | unsigned nr_user_bufs; |
| 257 | struct io_mapped_ubuf *user_bufs; |
| 258 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 259 | struct user_struct *user; |
| 260 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 261 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 262 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 263 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
| 264 | struct completion *completions; |
| 265 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 266 | /* if all else fails... */ |
| 267 | struct io_kiocb *fallback_req; |
| 268 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 269 | #if defined(CONFIG_UNIX) |
| 270 | struct socket *ring_sock; |
| 271 | #endif |
| 272 | |
| 273 | struct { |
| 274 | unsigned cached_cq_tail; |
| 275 | unsigned cq_entries; |
| 276 | unsigned cq_mask; |
| 277 | atomic_t cq_timeouts; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 278 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 279 | struct wait_queue_head cq_wait; |
| 280 | struct fasync_struct *cq_fasync; |
| 281 | struct eventfd_ctx *cq_ev_fd; |
| 282 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 283 | |
| 284 | struct { |
| 285 | struct mutex uring_lock; |
| 286 | wait_queue_head_t wait; |
| 287 | } ____cacheline_aligned_in_smp; |
| 288 | |
| 289 | struct { |
| 290 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 291 | struct llist_head poll_llist; |
| 292 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 293 | /* |
| 294 | * ->poll_list is protected by the ctx->uring_lock for |
| 295 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 296 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 297 | * manipulate the list, hence no extra locking is needed there. |
| 298 | */ |
| 299 | struct list_head poll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 300 | struct hlist_head *cancel_hash; |
| 301 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 302 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 303 | |
| 304 | spinlock_t inflight_lock; |
| 305 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 306 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 307 | }; |
| 308 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 309 | /* |
| 310 | * First field must be the file pointer in all the |
| 311 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 312 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 313 | struct io_poll_iocb { |
| 314 | struct file *file; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 315 | union { |
| 316 | struct wait_queue_head *head; |
| 317 | u64 addr; |
| 318 | }; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 319 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 320 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 321 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 322 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 323 | }; |
| 324 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 325 | struct io_close { |
| 326 | struct file *file; |
| 327 | struct file *put_file; |
| 328 | int fd; |
| 329 | }; |
| 330 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 331 | struct io_timeout_data { |
| 332 | struct io_kiocb *req; |
| 333 | struct hrtimer timer; |
| 334 | struct timespec64 ts; |
| 335 | enum hrtimer_mode mode; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 336 | u32 seq_offset; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 337 | }; |
| 338 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 339 | struct io_accept { |
| 340 | struct file *file; |
| 341 | struct sockaddr __user *addr; |
| 342 | int __user *addr_len; |
| 343 | int flags; |
| 344 | }; |
| 345 | |
| 346 | struct io_sync { |
| 347 | struct file *file; |
| 348 | loff_t len; |
| 349 | loff_t off; |
| 350 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 351 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 352 | }; |
| 353 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 354 | struct io_cancel { |
| 355 | struct file *file; |
| 356 | u64 addr; |
| 357 | }; |
| 358 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 359 | struct io_timeout { |
| 360 | struct file *file; |
| 361 | u64 addr; |
| 362 | int flags; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 363 | unsigned count; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 364 | }; |
| 365 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 366 | struct io_rw { |
| 367 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 368 | struct kiocb kiocb; |
| 369 | u64 addr; |
| 370 | u64 len; |
| 371 | }; |
| 372 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 373 | struct io_connect { |
| 374 | struct file *file; |
| 375 | struct sockaddr __user *addr; |
| 376 | int addr_len; |
| 377 | }; |
| 378 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 379 | struct io_sr_msg { |
| 380 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 381 | union { |
| 382 | struct user_msghdr __user *msg; |
| 383 | void __user *buf; |
| 384 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 385 | int msg_flags; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 386 | size_t len; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 387 | }; |
| 388 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 389 | struct io_open { |
| 390 | struct file *file; |
| 391 | int dfd; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 392 | union { |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 393 | unsigned mask; |
| 394 | }; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 395 | struct filename *filename; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 396 | struct statx __user *buffer; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 397 | struct open_how how; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 398 | }; |
| 399 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 400 | struct io_files_update { |
| 401 | struct file *file; |
| 402 | u64 arg; |
| 403 | u32 nr_args; |
| 404 | u32 offset; |
| 405 | }; |
| 406 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 407 | struct io_fadvise { |
| 408 | struct file *file; |
| 409 | u64 offset; |
| 410 | u32 len; |
| 411 | u32 advice; |
| 412 | }; |
| 413 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 414 | struct io_madvise { |
| 415 | struct file *file; |
| 416 | u64 addr; |
| 417 | u32 len; |
| 418 | u32 advice; |
| 419 | }; |
| 420 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 421 | struct io_async_connect { |
| 422 | struct sockaddr_storage address; |
| 423 | }; |
| 424 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 425 | struct io_async_msghdr { |
| 426 | struct iovec fast_iov[UIO_FASTIOV]; |
| 427 | struct iovec *iov; |
| 428 | struct sockaddr __user *uaddr; |
| 429 | struct msghdr msg; |
| 430 | }; |
| 431 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 432 | struct io_async_rw { |
| 433 | struct iovec fast_iov[UIO_FASTIOV]; |
| 434 | struct iovec *iov; |
| 435 | ssize_t nr_segs; |
| 436 | ssize_t size; |
| 437 | }; |
| 438 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 439 | struct io_async_open { |
| 440 | struct filename *filename; |
| 441 | }; |
| 442 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 443 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 444 | union { |
| 445 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 446 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 447 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 448 | struct io_timeout_data timeout; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 449 | struct io_async_open open; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 450 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 451 | }; |
| 452 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 453 | /* |
| 454 | * NOTE! Each of the iocb union members has the file pointer |
| 455 | * as the first entry in their struct definition. So you can |
| 456 | * access the file pointer through any of the sub-structs, |
| 457 | * or directly as just 'ki_filp' in this struct. |
| 458 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 459 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 460 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 461 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 462 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 463 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 464 | struct io_accept accept; |
| 465 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 466 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 467 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 468 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 469 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 470 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 471 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 472 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 473 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 474 | struct io_madvise madvise; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 475 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 476 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 477 | struct io_async_ctx *io; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 478 | union { |
| 479 | /* |
| 480 | * ring_file is only used in the submission path, and |
| 481 | * llist_node is only used for poll deferred completions |
| 482 | */ |
| 483 | struct file *ring_file; |
| 484 | struct llist_node llist_node; |
| 485 | }; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 486 | int ring_fd; |
| 487 | bool has_user; |
| 488 | bool in_async; |
| 489 | bool needs_fixed_file; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 490 | u8 opcode; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 491 | |
| 492 | struct io_ring_ctx *ctx; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 493 | union { |
| 494 | struct list_head list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 495 | struct hlist_node hash_node; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 496 | }; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 497 | struct list_head link_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 498 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 499 | refcount_t refs; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 500 | #define REQ_F_NOWAIT 1 /* must not punt to workers */ |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 501 | #define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */ |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 502 | #define REQ_F_FIXED_FILE 4 /* ctx owns file */ |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 503 | #define REQ_F_LINK_NEXT 8 /* already grabbed next link */ |
Stefan Bühler | e2033e3 | 2019-05-11 19:08:01 +0200 | [diff] [blame] | 504 | #define REQ_F_IO_DRAIN 16 /* drain existing IO first */ |
| 505 | #define REQ_F_IO_DRAINED 32 /* drain done */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 506 | #define REQ_F_LINK 64 /* linked sqes */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 507 | #define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */ |
Zhengyuan Liu | f7b76ac | 2019-07-16 23:26:14 +0800 | [diff] [blame] | 508 | #define REQ_F_FAIL_LINK 256 /* fail rest of links */ |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 509 | #define REQ_F_DRAIN_LINK 512 /* link should be fully drained */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 510 | #define REQ_F_TIMEOUT 1024 /* timeout request */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 511 | #define REQ_F_ISREG 2048 /* regular file */ |
| 512 | #define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */ |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 513 | #define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */ |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 514 | #define REQ_F_INFLIGHT 16384 /* on inflight list */ |
| 515 | #define REQ_F_COMP_LOCKED 32768 /* completion under lock */ |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 516 | #define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */ |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 517 | #define REQ_F_FORCE_ASYNC 131072 /* IOSQE_ASYNC */ |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 518 | #define REQ_F_CUR_POS 262144 /* read/write uses file position */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 519 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 520 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 521 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 522 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 523 | struct list_head inflight_entry; |
| 524 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 525 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 526 | }; |
| 527 | |
| 528 | #define IO_PLUG_THRESHOLD 2 |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 529 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 530 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 531 | struct io_submit_state { |
| 532 | struct blk_plug plug; |
| 533 | |
| 534 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 535 | * io_kiocb alloc cache |
| 536 | */ |
| 537 | void *reqs[IO_IOPOLL_BATCH]; |
| 538 | unsigned int free_reqs; |
| 539 | unsigned int cur_req; |
| 540 | |
| 541 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 542 | * File reference cache |
| 543 | */ |
| 544 | struct file *file; |
| 545 | unsigned int fd; |
| 546 | unsigned int has_refs; |
| 547 | unsigned int used_refs; |
| 548 | unsigned int ios_left; |
| 549 | }; |
| 550 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 551 | struct io_op_def { |
| 552 | /* needs req->io allocated for deferral/async */ |
| 553 | unsigned async_ctx : 1; |
| 554 | /* needs current->mm setup, does mm access */ |
| 555 | unsigned needs_mm : 1; |
| 556 | /* needs req->file assigned */ |
| 557 | unsigned needs_file : 1; |
| 558 | /* needs req->file assigned IFF fd is >= 0 */ |
| 559 | unsigned fd_non_neg : 1; |
| 560 | /* hash wq insertion if file is a regular file */ |
| 561 | unsigned hash_reg_file : 1; |
| 562 | /* unbound wq insertion if file is a non-regular file */ |
| 563 | unsigned unbound_nonreg_file : 1; |
| 564 | }; |
| 565 | |
| 566 | static const struct io_op_def io_op_defs[] = { |
| 567 | { |
| 568 | /* IORING_OP_NOP */ |
| 569 | }, |
| 570 | { |
| 571 | /* IORING_OP_READV */ |
| 572 | .async_ctx = 1, |
| 573 | .needs_mm = 1, |
| 574 | .needs_file = 1, |
| 575 | .unbound_nonreg_file = 1, |
| 576 | }, |
| 577 | { |
| 578 | /* IORING_OP_WRITEV */ |
| 579 | .async_ctx = 1, |
| 580 | .needs_mm = 1, |
| 581 | .needs_file = 1, |
| 582 | .hash_reg_file = 1, |
| 583 | .unbound_nonreg_file = 1, |
| 584 | }, |
| 585 | { |
| 586 | /* IORING_OP_FSYNC */ |
| 587 | .needs_file = 1, |
| 588 | }, |
| 589 | { |
| 590 | /* IORING_OP_READ_FIXED */ |
| 591 | .needs_file = 1, |
| 592 | .unbound_nonreg_file = 1, |
| 593 | }, |
| 594 | { |
| 595 | /* IORING_OP_WRITE_FIXED */ |
| 596 | .needs_file = 1, |
| 597 | .hash_reg_file = 1, |
| 598 | .unbound_nonreg_file = 1, |
| 599 | }, |
| 600 | { |
| 601 | /* IORING_OP_POLL_ADD */ |
| 602 | .needs_file = 1, |
| 603 | .unbound_nonreg_file = 1, |
| 604 | }, |
| 605 | { |
| 606 | /* IORING_OP_POLL_REMOVE */ |
| 607 | }, |
| 608 | { |
| 609 | /* IORING_OP_SYNC_FILE_RANGE */ |
| 610 | .needs_file = 1, |
| 611 | }, |
| 612 | { |
| 613 | /* IORING_OP_SENDMSG */ |
| 614 | .async_ctx = 1, |
| 615 | .needs_mm = 1, |
| 616 | .needs_file = 1, |
| 617 | .unbound_nonreg_file = 1, |
| 618 | }, |
| 619 | { |
| 620 | /* IORING_OP_RECVMSG */ |
| 621 | .async_ctx = 1, |
| 622 | .needs_mm = 1, |
| 623 | .needs_file = 1, |
| 624 | .unbound_nonreg_file = 1, |
| 625 | }, |
| 626 | { |
| 627 | /* IORING_OP_TIMEOUT */ |
| 628 | .async_ctx = 1, |
| 629 | .needs_mm = 1, |
| 630 | }, |
| 631 | { |
| 632 | /* IORING_OP_TIMEOUT_REMOVE */ |
| 633 | }, |
| 634 | { |
| 635 | /* IORING_OP_ACCEPT */ |
| 636 | .needs_mm = 1, |
| 637 | .needs_file = 1, |
| 638 | .unbound_nonreg_file = 1, |
| 639 | }, |
| 640 | { |
| 641 | /* IORING_OP_ASYNC_CANCEL */ |
| 642 | }, |
| 643 | { |
| 644 | /* IORING_OP_LINK_TIMEOUT */ |
| 645 | .async_ctx = 1, |
| 646 | .needs_mm = 1, |
| 647 | }, |
| 648 | { |
| 649 | /* IORING_OP_CONNECT */ |
| 650 | .async_ctx = 1, |
| 651 | .needs_mm = 1, |
| 652 | .needs_file = 1, |
| 653 | .unbound_nonreg_file = 1, |
| 654 | }, |
| 655 | { |
| 656 | /* IORING_OP_FALLOCATE */ |
| 657 | .needs_file = 1, |
| 658 | }, |
| 659 | { |
| 660 | /* IORING_OP_OPENAT */ |
| 661 | .needs_file = 1, |
| 662 | .fd_non_neg = 1, |
| 663 | }, |
| 664 | { |
| 665 | /* IORING_OP_CLOSE */ |
| 666 | .needs_file = 1, |
| 667 | }, |
| 668 | { |
| 669 | /* IORING_OP_FILES_UPDATE */ |
| 670 | .needs_mm = 1, |
| 671 | }, |
| 672 | { |
| 673 | /* IORING_OP_STATX */ |
| 674 | .needs_mm = 1, |
| 675 | .needs_file = 1, |
| 676 | .fd_non_neg = 1, |
| 677 | }, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 678 | { |
| 679 | /* IORING_OP_READ */ |
| 680 | .needs_mm = 1, |
| 681 | .needs_file = 1, |
| 682 | .unbound_nonreg_file = 1, |
| 683 | }, |
| 684 | { |
| 685 | /* IORING_OP_WRITE */ |
| 686 | .needs_mm = 1, |
| 687 | .needs_file = 1, |
| 688 | .unbound_nonreg_file = 1, |
| 689 | }, |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 690 | { |
| 691 | /* IORING_OP_FADVISE */ |
| 692 | .needs_file = 1, |
| 693 | }, |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 694 | { |
| 695 | /* IORING_OP_MADVISE */ |
| 696 | .needs_mm = 1, |
| 697 | }, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 698 | { |
| 699 | /* IORING_OP_SEND */ |
| 700 | .needs_mm = 1, |
| 701 | .needs_file = 1, |
| 702 | .unbound_nonreg_file = 1, |
| 703 | }, |
| 704 | { |
| 705 | /* IORING_OP_RECV */ |
| 706 | .needs_mm = 1, |
| 707 | .needs_file = 1, |
| 708 | .unbound_nonreg_file = 1, |
| 709 | }, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 710 | { |
| 711 | /* IORING_OP_OPENAT2 */ |
| 712 | .needs_file = 1, |
| 713 | .fd_non_neg = 1, |
| 714 | }, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 715 | }; |
| 716 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 717 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 718 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 719 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 720 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 721 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 722 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 723 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 724 | struct io_uring_files_update *ip, |
| 725 | unsigned nr_args); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 726 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 727 | static struct kmem_cache *req_cachep; |
| 728 | |
| 729 | static const struct file_operations io_uring_fops; |
| 730 | |
| 731 | struct sock *io_uring_get_socket(struct file *file) |
| 732 | { |
| 733 | #if defined(CONFIG_UNIX) |
| 734 | if (file->f_op == &io_uring_fops) { |
| 735 | struct io_ring_ctx *ctx = file->private_data; |
| 736 | |
| 737 | return ctx->ring_sock->sk; |
| 738 | } |
| 739 | #endif |
| 740 | return NULL; |
| 741 | } |
| 742 | EXPORT_SYMBOL(io_uring_get_socket); |
| 743 | |
| 744 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 745 | { |
| 746 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 747 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 748 | complete(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 752 | { |
| 753 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 754 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 755 | |
| 756 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 757 | if (!ctx) |
| 758 | return NULL; |
| 759 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 760 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 761 | if (!ctx->fallback_req) |
| 762 | goto err; |
| 763 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 764 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
| 765 | if (!ctx->completions) |
| 766 | goto err; |
| 767 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 768 | /* |
| 769 | * Use 5 bits less than the max cq entries, that should give us around |
| 770 | * 32 entries per hash list if totally full and uniformly spread. |
| 771 | */ |
| 772 | hash_bits = ilog2(p->cq_entries); |
| 773 | hash_bits -= 5; |
| 774 | if (hash_bits <= 0) |
| 775 | hash_bits = 1; |
| 776 | ctx->cancel_hash_bits = hash_bits; |
| 777 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 778 | GFP_KERNEL); |
| 779 | if (!ctx->cancel_hash) |
| 780 | goto err; |
| 781 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 782 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 783 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 784 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 785 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 786 | |
| 787 | ctx->flags = p->flags; |
| 788 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 789 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 790 | init_completion(&ctx->completions[0]); |
| 791 | init_completion(&ctx->completions[1]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 792 | mutex_init(&ctx->uring_lock); |
| 793 | init_waitqueue_head(&ctx->wait); |
| 794 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 795 | init_llist_head(&ctx->poll_llist); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 796 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 797 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 798 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 799 | init_waitqueue_head(&ctx->inflight_wait); |
| 800 | spin_lock_init(&ctx->inflight_lock); |
| 801 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 802 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 803 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 804 | if (ctx->fallback_req) |
| 805 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 806 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 807 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 808 | kfree(ctx); |
| 809 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 810 | } |
| 811 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 812 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 813 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 814 | struct io_ring_ctx *ctx = req->ctx; |
| 815 | |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 816 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 817 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 818 | } |
| 819 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 820 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 821 | { |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 822 | if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN) |
| 823 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 824 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 825 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | 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] | 829 | { |
| 830 | struct io_kiocb *req; |
| 831 | |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 832 | 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] | 833 | if (req && !req_need_defer(req)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 834 | list_del_init(&req->list); |
| 835 | return req; |
| 836 | } |
| 837 | |
| 838 | return NULL; |
| 839 | } |
| 840 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 841 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 842 | { |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 843 | struct io_kiocb *req; |
| 844 | |
| 845 | 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] | 846 | if (req) { |
| 847 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 848 | return NULL; |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 849 | if (!__req_need_defer(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 850 | list_del_init(&req->list); |
| 851 | return req; |
| 852 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | return NULL; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 856 | } |
| 857 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 858 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 859 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 860 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 861 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 862 | if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 863 | /* order cqe stores with ring update */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 864 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 865 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 866 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 867 | wake_up_interruptible(&ctx->cq_wait); |
| 868 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 873 | static inline bool io_prep_async_work(struct io_kiocb *req, |
| 874 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 875 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 876 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 877 | bool do_hashed = false; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 878 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 879 | if (req->flags & REQ_F_ISREG) { |
| 880 | if (def->hash_reg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 881 | do_hashed = true; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 882 | } else { |
| 883 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 884 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 885 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 886 | if (def->needs_mm) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 887 | req->work.flags |= IO_WQ_WORK_NEEDS_USER; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 888 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 889 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 890 | return do_hashed; |
| 891 | } |
| 892 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 893 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 894 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 895 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 896 | struct io_kiocb *link; |
| 897 | bool do_hashed; |
| 898 | |
| 899 | do_hashed = io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 900 | |
| 901 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, |
| 902 | req->flags); |
| 903 | if (!do_hashed) { |
| 904 | io_wq_enqueue(ctx->io_wq, &req->work); |
| 905 | } else { |
| 906 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, |
| 907 | file_inode(req->file)); |
| 908 | } |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 909 | |
| 910 | if (link) |
| 911 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 912 | } |
| 913 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 914 | static void io_kill_timeout(struct io_kiocb *req) |
| 915 | { |
| 916 | int ret; |
| 917 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 918 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 919 | if (ret != -1) { |
| 920 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 921 | list_del_init(&req->list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 922 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 923 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
| 927 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 928 | { |
| 929 | struct io_kiocb *req, *tmp; |
| 930 | |
| 931 | spin_lock_irq(&ctx->completion_lock); |
| 932 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 933 | io_kill_timeout(req); |
| 934 | spin_unlock_irq(&ctx->completion_lock); |
| 935 | } |
| 936 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 937 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 938 | { |
| 939 | struct io_kiocb *req; |
| 940 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 941 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 942 | io_kill_timeout(req); |
| 943 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 944 | __io_commit_cqring(ctx); |
| 945 | |
| 946 | while ((req = io_get_deferred_req(ctx)) != NULL) { |
| 947 | req->flags |= REQ_F_IO_DRAINED; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 948 | io_queue_async_work(req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 952 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 953 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 954 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 955 | unsigned tail; |
| 956 | |
| 957 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 958 | /* |
| 959 | * writes to the cq entry need to come after reading head; the |
| 960 | * control dependency is enough as we're using WRITE_ONCE to |
| 961 | * fill the cq entry |
| 962 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 963 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 964 | return NULL; |
| 965 | |
| 966 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 967 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 968 | } |
| 969 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 970 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 971 | { |
| 972 | if (!ctx->eventfd_async) |
| 973 | return true; |
| 974 | return io_wq_current_is_worker() || in_interrupt(); |
| 975 | } |
| 976 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 977 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| 978 | { |
| 979 | if (waitqueue_active(&ctx->wait)) |
| 980 | wake_up(&ctx->wait); |
| 981 | if (waitqueue_active(&ctx->sqo_wait)) |
| 982 | wake_up(&ctx->sqo_wait); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 983 | if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 984 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 985 | } |
| 986 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 987 | /* Returns true if there are no backlogged entries after the flush */ |
| 988 | 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] | 989 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 990 | struct io_rings *rings = ctx->rings; |
| 991 | struct io_uring_cqe *cqe; |
| 992 | struct io_kiocb *req; |
| 993 | unsigned long flags; |
| 994 | LIST_HEAD(list); |
| 995 | |
| 996 | if (!force) { |
| 997 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 998 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 999 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 1000 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1001 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1005 | |
| 1006 | /* if force is set, the ring is going away. always drop after that */ |
| 1007 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1008 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1009 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1010 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1011 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1012 | cqe = io_get_cqring(ctx); |
| 1013 | if (!cqe && !force) |
| 1014 | break; |
| 1015 | |
| 1016 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 1017 | list); |
| 1018 | list_move(&req->list, &list); |
| 1019 | if (cqe) { |
| 1020 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1021 | WRITE_ONCE(cqe->res, req->result); |
| 1022 | WRITE_ONCE(cqe->flags, 0); |
| 1023 | } else { |
| 1024 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1025 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | io_commit_cqring(ctx); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1030 | if (cqe) { |
| 1031 | clear_bit(0, &ctx->sq_check_overflow); |
| 1032 | clear_bit(0, &ctx->cq_check_overflow); |
| 1033 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1034 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1035 | io_cqring_ev_posted(ctx); |
| 1036 | |
| 1037 | while (!list_empty(&list)) { |
| 1038 | req = list_first_entry(&list, struct io_kiocb, list); |
| 1039 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1040 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1041 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1042 | |
| 1043 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1046 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1047 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1048 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1049 | struct io_uring_cqe *cqe; |
| 1050 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1051 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1052 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1053 | /* |
| 1054 | * If we can't get a cq entry, userspace overflowed the |
| 1055 | * submission (by quite a lot). Increment the overflow count in |
| 1056 | * the ring. |
| 1057 | */ |
| 1058 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1059 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1060 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1061 | WRITE_ONCE(cqe->res, res); |
| 1062 | WRITE_ONCE(cqe->flags, 0); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1063 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1064 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1065 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1066 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1067 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1068 | set_bit(0, &ctx->sq_check_overflow); |
| 1069 | set_bit(0, &ctx->cq_check_overflow); |
| 1070 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1071 | refcount_inc(&req->refs); |
| 1072 | req->result = res; |
| 1073 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1077 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1078 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1079 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1080 | unsigned long flags; |
| 1081 | |
| 1082 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1083 | io_cqring_fill_event(req, res); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1084 | io_commit_cqring(ctx); |
| 1085 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1086 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1087 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1090 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1091 | { |
| 1092 | return req == (struct io_kiocb *) |
| 1093 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1094 | } |
| 1095 | |
| 1096 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1097 | { |
| 1098 | struct io_kiocb *req; |
| 1099 | |
| 1100 | req = ctx->fallback_req; |
| 1101 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) |
| 1102 | return req; |
| 1103 | |
| 1104 | return NULL; |
| 1105 | } |
| 1106 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1107 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 1108 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1109 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1110 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1111 | struct io_kiocb *req; |
| 1112 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1113 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1114 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1115 | if (unlikely(!req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1116 | goto fallback; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1117 | } else if (!state->free_reqs) { |
| 1118 | size_t sz; |
| 1119 | int ret; |
| 1120 | |
| 1121 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1122 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1123 | |
| 1124 | /* |
| 1125 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1126 | * retry single alloc to be on the safe side. |
| 1127 | */ |
| 1128 | if (unlikely(ret <= 0)) { |
| 1129 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1130 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1131 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1132 | ret = 1; |
| 1133 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1134 | state->free_reqs = ret - 1; |
| 1135 | state->cur_req = 1; |
| 1136 | req = state->reqs[0]; |
| 1137 | } else { |
| 1138 | req = state->reqs[state->cur_req]; |
| 1139 | state->free_reqs--; |
| 1140 | state->cur_req++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1143 | got_it: |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 1144 | req->io = NULL; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 1145 | req->ring_file = NULL; |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 1146 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1147 | req->ctx = ctx; |
| 1148 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1149 | /* one is dropped after submission, the other at completion */ |
| 1150 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1151 | req->result = 0; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1152 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1153 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1154 | fallback: |
| 1155 | req = io_get_fallback_req(ctx); |
| 1156 | if (req) |
| 1157 | goto got_it; |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 1158 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1159 | return NULL; |
| 1160 | } |
| 1161 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1162 | static void __io_req_do_free(struct io_kiocb *req) |
| 1163 | { |
| 1164 | if (likely(!io_is_fallback_req(req))) |
| 1165 | kmem_cache_free(req_cachep, req); |
| 1166 | else |
| 1167 | clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req); |
| 1168 | } |
| 1169 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1170 | static void __io_req_aux_free(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1171 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1172 | struct io_ring_ctx *ctx = req->ctx; |
| 1173 | |
YueHaibing | 96fd84d | 2020-01-07 22:22:44 +0800 | [diff] [blame] | 1174 | kfree(req->io); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1175 | if (req->file) { |
| 1176 | if (req->flags & REQ_F_FIXED_FILE) |
| 1177 | percpu_ref_put(&ctx->file_data->refs); |
| 1178 | else |
| 1179 | fput(req->file); |
| 1180 | } |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | static void __io_free_req(struct io_kiocb *req) |
| 1184 | { |
| 1185 | __io_req_aux_free(req); |
| 1186 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1187 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1188 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1189 | unsigned long flags; |
| 1190 | |
| 1191 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1192 | list_del(&req->inflight_entry); |
| 1193 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1194 | wake_up(&ctx->inflight_wait); |
| 1195 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1196 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1197 | |
| 1198 | percpu_ref_put(&req->ctx->refs); |
| 1199 | __io_req_do_free(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1200 | } |
| 1201 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1202 | struct req_batch { |
| 1203 | void *reqs[IO_IOPOLL_BATCH]; |
| 1204 | int to_free; |
| 1205 | int need_iter; |
| 1206 | }; |
| 1207 | |
| 1208 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) |
| 1209 | { |
| 1210 | if (!rb->to_free) |
| 1211 | return; |
| 1212 | if (rb->need_iter) { |
| 1213 | int i, inflight = 0; |
| 1214 | unsigned long flags; |
| 1215 | |
| 1216 | for (i = 0; i < rb->to_free; i++) { |
| 1217 | struct io_kiocb *req = rb->reqs[i]; |
| 1218 | |
| 1219 | if (req->flags & REQ_F_FIXED_FILE) |
| 1220 | req->file = NULL; |
| 1221 | if (req->flags & REQ_F_INFLIGHT) |
| 1222 | inflight++; |
| 1223 | else |
| 1224 | rb->reqs[i] = NULL; |
| 1225 | __io_req_aux_free(req); |
| 1226 | } |
| 1227 | if (!inflight) |
| 1228 | goto do_free; |
| 1229 | |
| 1230 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1231 | for (i = 0; i < rb->to_free; i++) { |
| 1232 | struct io_kiocb *req = rb->reqs[i]; |
| 1233 | |
| 1234 | if (req) { |
| 1235 | list_del(&req->inflight_entry); |
| 1236 | if (!--inflight) |
| 1237 | break; |
| 1238 | } |
| 1239 | } |
| 1240 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1241 | |
| 1242 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1243 | wake_up(&ctx->inflight_wait); |
| 1244 | } |
| 1245 | do_free: |
| 1246 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
| 1247 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
| 1248 | percpu_ref_put_many(&ctx->file_data->refs, rb->to_free); |
| 1249 | rb->to_free = rb->need_iter = 0; |
| 1250 | } |
| 1251 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1252 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1253 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1254 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1255 | int ret; |
| 1256 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1257 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1258 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1259 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1260 | io_commit_cqring(ctx); |
| 1261 | req->flags &= ~REQ_F_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1262 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1263 | return true; |
| 1264 | } |
| 1265 | |
| 1266 | return false; |
| 1267 | } |
| 1268 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1269 | 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] | 1270 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1271 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1272 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1273 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1274 | /* Already got next link */ |
| 1275 | if (req->flags & REQ_F_LINK_NEXT) |
| 1276 | return; |
| 1277 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1278 | /* |
| 1279 | * The list should never be empty when we are called here. But could |
| 1280 | * potentially happen if the chain is messed up, check to be on the |
| 1281 | * safe side. |
| 1282 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1283 | while (!list_empty(&req->link_list)) { |
| 1284 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 1285 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1286 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1287 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 1288 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 1289 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1290 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1291 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1292 | continue; |
| 1293 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1294 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1295 | list_del_init(&req->link_list); |
| 1296 | if (!list_empty(&nxt->link_list)) |
| 1297 | nxt->flags |= REQ_F_LINK; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 1298 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1299 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1300 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1301 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1302 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1303 | if (wake_ev) |
| 1304 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | /* |
| 1308 | * Called if REQ_F_LINK is set, and we fail the head request |
| 1309 | */ |
| 1310 | static void io_fail_links(struct io_kiocb *req) |
| 1311 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1312 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1313 | unsigned long flags; |
| 1314 | |
| 1315 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1316 | |
| 1317 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1318 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1319 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1320 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1321 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1322 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1323 | |
| 1324 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1325 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1326 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1327 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1328 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1329 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1330 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1331 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1332 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1333 | |
| 1334 | io_commit_cqring(ctx); |
| 1335 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1336 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1337 | } |
| 1338 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1339 | 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] | 1340 | { |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1341 | if (likely(!(req->flags & REQ_F_LINK))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1342 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1343 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1344 | /* |
| 1345 | * If LINK is set, we have dependent requests in this chain. If we |
| 1346 | * didn't fail this request, queue the first one up, moving any other |
| 1347 | * dependencies to the next request. In case of failure, fail the rest |
| 1348 | * of the chain. |
| 1349 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1350 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1351 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1352 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1353 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1354 | struct io_ring_ctx *ctx = req->ctx; |
| 1355 | unsigned long flags; |
| 1356 | |
| 1357 | /* |
| 1358 | * If this is a timeout link, we could be racing with the |
| 1359 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1360 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1361 | */ |
| 1362 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1363 | io_req_link_next(req, nxt); |
| 1364 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1365 | } else { |
| 1366 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1367 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1368 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1369 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1370 | static void io_free_req(struct io_kiocb *req) |
| 1371 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1372 | struct io_kiocb *nxt = NULL; |
| 1373 | |
| 1374 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1375 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1376 | |
| 1377 | if (nxt) |
| 1378 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1379 | } |
| 1380 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1381 | /* |
| 1382 | * Drop reference to request, return next in chain (if there is one) if this |
| 1383 | * was the last reference to this request. |
| 1384 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1385 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1386 | 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] | 1387 | { |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1388 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1389 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1390 | if (refcount_dec_and_test(&req->refs)) |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1391 | __io_free_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1394 | static void io_put_req(struct io_kiocb *req) |
| 1395 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1396 | if (refcount_dec_and_test(&req->refs)) |
| 1397 | io_free_req(req); |
| 1398 | } |
| 1399 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1400 | /* |
| 1401 | * Must only be used if we don't need to care about links, usually from |
| 1402 | * within the completion handling itself. |
| 1403 | */ |
| 1404 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1405 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1406 | /* drop both submit and complete references */ |
| 1407 | if (refcount_sub_and_test(2, &req->refs)) |
| 1408 | __io_free_req(req); |
| 1409 | } |
| 1410 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1411 | static void io_double_put_req(struct io_kiocb *req) |
| 1412 | { |
| 1413 | /* drop both submit and complete references */ |
| 1414 | if (refcount_sub_and_test(2, &req->refs)) |
| 1415 | io_free_req(req); |
| 1416 | } |
| 1417 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1418 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1419 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1420 | struct io_rings *rings = ctx->rings; |
| 1421 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1422 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1423 | /* |
| 1424 | * noflush == true is from the waitqueue handler, just ensure |
| 1425 | * we wake up the task, and the next invocation will flush the |
| 1426 | * entries. We cannot safely to it from here. |
| 1427 | */ |
| 1428 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1429 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1430 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1431 | io_cqring_overflow_flush(ctx, false); |
| 1432 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1433 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1434 | /* See comment at the top of this file */ |
| 1435 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1436 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1437 | } |
| 1438 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1439 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1440 | { |
| 1441 | struct io_rings *rings = ctx->rings; |
| 1442 | |
| 1443 | /* make sure SQ entry isn't read before tail */ |
| 1444 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1445 | } |
| 1446 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1447 | static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req) |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1448 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1449 | if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req)) |
| 1450 | return false; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1451 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1452 | if (!(req->flags & REQ_F_FIXED_FILE) || req->io) |
| 1453 | rb->need_iter++; |
| 1454 | |
| 1455 | rb->reqs[rb->to_free++] = req; |
| 1456 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1457 | io_free_req_many(req->ctx, rb); |
| 1458 | return true; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1461 | /* |
| 1462 | * Find and free completed poll iocbs |
| 1463 | */ |
| 1464 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1465 | struct list_head *done) |
| 1466 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1467 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1468 | struct io_kiocb *req; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1469 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1470 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1471 | while (!list_empty(done)) { |
| 1472 | req = list_first_entry(done, struct io_kiocb, list); |
| 1473 | list_del(&req->list); |
| 1474 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1475 | io_cqring_fill_event(req, req->result); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1476 | (*nr_events)++; |
| 1477 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1478 | if (refcount_dec_and_test(&req->refs) && |
| 1479 | !io_req_multi_free(&rb, req)) |
| 1480 | io_free_req(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1481 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1482 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1483 | io_commit_cqring(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1484 | io_free_req_many(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1488 | long min) |
| 1489 | { |
| 1490 | struct io_kiocb *req, *tmp; |
| 1491 | LIST_HEAD(done); |
| 1492 | bool spin; |
| 1493 | int ret; |
| 1494 | |
| 1495 | /* |
| 1496 | * Only spin for completions if we don't have multiple devices hanging |
| 1497 | * off our complete list, and we're under the requested amount. |
| 1498 | */ |
| 1499 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1500 | |
| 1501 | ret = 0; |
| 1502 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1503 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1504 | |
| 1505 | /* |
| 1506 | * Move completed entries to our local list. If we find a |
| 1507 | * request that requires polling, break out and complete |
| 1508 | * the done list first, if we have entries there. |
| 1509 | */ |
| 1510 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 1511 | list_move_tail(&req->list, &done); |
| 1512 | continue; |
| 1513 | } |
| 1514 | if (!list_empty(&done)) |
| 1515 | break; |
| 1516 | |
| 1517 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1518 | if (ret < 0) |
| 1519 | break; |
| 1520 | |
| 1521 | if (ret && spin) |
| 1522 | spin = false; |
| 1523 | ret = 0; |
| 1524 | } |
| 1525 | |
| 1526 | if (!list_empty(&done)) |
| 1527 | io_iopoll_complete(ctx, nr_events, &done); |
| 1528 | |
| 1529 | return ret; |
| 1530 | } |
| 1531 | |
| 1532 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 1533 | * 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] | 1534 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1535 | * as a non-spinning completion check. |
| 1536 | */ |
| 1537 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1538 | long min) |
| 1539 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1540 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1541 | int ret; |
| 1542 | |
| 1543 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1544 | if (ret < 0) |
| 1545 | return ret; |
| 1546 | if (!min || *nr_events >= min) |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
| 1550 | return 1; |
| 1551 | } |
| 1552 | |
| 1553 | /* |
| 1554 | * We can't just wait for polled events to come to us, we have to actively |
| 1555 | * find and complete them. |
| 1556 | */ |
| 1557 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1558 | { |
| 1559 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1560 | return; |
| 1561 | |
| 1562 | mutex_lock(&ctx->uring_lock); |
| 1563 | while (!list_empty(&ctx->poll_list)) { |
| 1564 | unsigned int nr_events = 0; |
| 1565 | |
| 1566 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1567 | |
| 1568 | /* |
| 1569 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1570 | * in this case we need to ensure that we reap all events. |
| 1571 | */ |
| 1572 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1573 | } |
| 1574 | mutex_unlock(&ctx->uring_lock); |
| 1575 | } |
| 1576 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1577 | static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1578 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1579 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1580 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1581 | |
| 1582 | do { |
| 1583 | int tmin = 0; |
| 1584 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1585 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1586 | * Don't enter poll loop if we already have events pending. |
| 1587 | * If we do, we can potentially be spinning for commands that |
| 1588 | * already triggered a CQE (eg in error). |
| 1589 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1590 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1591 | break; |
| 1592 | |
| 1593 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1594 | * If a submit got punted to a workqueue, we can have the |
| 1595 | * application entering polling for a command before it gets |
| 1596 | * issued. That app will hold the uring_lock for the duration |
| 1597 | * of the poll right here, so we need to take a breather every |
| 1598 | * now and then to ensure that the issue has a chance to add |
| 1599 | * the poll to the issued list. Otherwise we can spin here |
| 1600 | * forever, while the workqueue is stuck trying to acquire the |
| 1601 | * very same mutex. |
| 1602 | */ |
| 1603 | if (!(++iters & 7)) { |
| 1604 | mutex_unlock(&ctx->uring_lock); |
| 1605 | mutex_lock(&ctx->uring_lock); |
| 1606 | } |
| 1607 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1608 | if (*nr_events < min) |
| 1609 | tmin = min - *nr_events; |
| 1610 | |
| 1611 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1612 | if (ret <= 0) |
| 1613 | break; |
| 1614 | ret = 0; |
| 1615 | } while (min && !*nr_events && !need_resched()); |
| 1616 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1617 | return ret; |
| 1618 | } |
| 1619 | |
| 1620 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1621 | long min) |
| 1622 | { |
| 1623 | int ret; |
| 1624 | |
| 1625 | /* |
| 1626 | * We disallow the app entering submit/complete with polling, but we |
| 1627 | * still need to lock the ring to prevent racing with polled issue |
| 1628 | * that got punted to a workqueue. |
| 1629 | */ |
| 1630 | mutex_lock(&ctx->uring_lock); |
| 1631 | ret = __io_iopoll_check(ctx, nr_events, min); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1632 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1633 | return ret; |
| 1634 | } |
| 1635 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1636 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1637 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1638 | /* |
| 1639 | * Tell lockdep we inherited freeze protection from submission |
| 1640 | * thread. |
| 1641 | */ |
| 1642 | if (req->flags & REQ_F_ISREG) { |
| 1643 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1644 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1645 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1646 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1647 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1648 | } |
| 1649 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1650 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1651 | { |
| 1652 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1653 | req->flags |= REQ_F_FAIL_LINK; |
| 1654 | } |
| 1655 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1656 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1657 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1658 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1659 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1660 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1661 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1662 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1663 | if (res != req->result) |
| 1664 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1665 | io_cqring_add_event(req, res); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1669 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1670 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1671 | |
| 1672 | io_complete_rw_common(kiocb, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1673 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1676 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
| 1677 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1678 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1679 | struct io_kiocb *nxt = NULL; |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1680 | |
| 1681 | io_complete_rw_common(kiocb, res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1682 | io_put_req_find_next(req, &nxt); |
| 1683 | |
| 1684 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1687 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 1688 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1689 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1690 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1691 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1692 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1693 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1694 | if (res != req->result) |
| 1695 | req_set_fail_links(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1696 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1697 | if (res != -EAGAIN) |
| 1698 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 1699 | } |
| 1700 | |
| 1701 | /* |
| 1702 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 1703 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 1704 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 1705 | * accessing the kiocb cookie. |
| 1706 | */ |
| 1707 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 1708 | { |
| 1709 | struct io_ring_ctx *ctx = req->ctx; |
| 1710 | |
| 1711 | /* |
| 1712 | * Track whether we have multiple files in our lists. This will impact |
| 1713 | * how we do polling eventually, not spinning if we're on potentially |
| 1714 | * different devices. |
| 1715 | */ |
| 1716 | if (list_empty(&ctx->poll_list)) { |
| 1717 | ctx->poll_multi_file = false; |
| 1718 | } else if (!ctx->poll_multi_file) { |
| 1719 | struct io_kiocb *list_req; |
| 1720 | |
| 1721 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1722 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1723 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1724 | ctx->poll_multi_file = true; |
| 1725 | } |
| 1726 | |
| 1727 | /* |
| 1728 | * For fast devices, IO may have already completed. If it has, add |
| 1729 | * it to the front so we find it first. |
| 1730 | */ |
| 1731 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1732 | list_add(&req->list, &ctx->poll_list); |
| 1733 | else |
| 1734 | list_add_tail(&req->list, &ctx->poll_list); |
| 1735 | } |
| 1736 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1737 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1738 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1739 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1740 | int diff = state->has_refs - state->used_refs; |
| 1741 | |
| 1742 | if (diff) |
| 1743 | fput_many(state->file, diff); |
| 1744 | state->file = NULL; |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | /* |
| 1749 | * Get as many references to a file as we have IOs left in this submission, |
| 1750 | * assuming most submissions are for one file, or at least that each file |
| 1751 | * has more than one submission. |
| 1752 | */ |
| 1753 | static struct file *io_file_get(struct io_submit_state *state, int fd) |
| 1754 | { |
| 1755 | if (!state) |
| 1756 | return fget(fd); |
| 1757 | |
| 1758 | if (state->file) { |
| 1759 | if (state->fd == fd) { |
| 1760 | state->used_refs++; |
| 1761 | state->ios_left--; |
| 1762 | return state->file; |
| 1763 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1764 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1765 | } |
| 1766 | state->file = fget_many(fd, state->ios_left); |
| 1767 | if (!state->file) |
| 1768 | return NULL; |
| 1769 | |
| 1770 | state->fd = fd; |
| 1771 | state->has_refs = state->ios_left; |
| 1772 | state->used_refs = 1; |
| 1773 | state->ios_left--; |
| 1774 | return state->file; |
| 1775 | } |
| 1776 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1777 | /* |
| 1778 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 1779 | * any file. For now, just ensure that anything potentially problematic is done |
| 1780 | * inline. |
| 1781 | */ |
| 1782 | static bool io_file_supports_async(struct file *file) |
| 1783 | { |
| 1784 | umode_t mode = file_inode(file)->i_mode; |
| 1785 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 1786 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1787 | return true; |
| 1788 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 1789 | return true; |
| 1790 | |
| 1791 | return false; |
| 1792 | } |
| 1793 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1794 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1795 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1796 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1797 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1798 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1799 | unsigned ioprio; |
| 1800 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1801 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1802 | if (!req->file) |
| 1803 | return -EBADF; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1804 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1805 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 1806 | req->flags |= REQ_F_ISREG; |
| 1807 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1808 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1809 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 1810 | req->flags |= REQ_F_CUR_POS; |
| 1811 | kiocb->ki_pos = req->file->f_pos; |
| 1812 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1813 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 1814 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
| 1815 | |
| 1816 | ioprio = READ_ONCE(sqe->ioprio); |
| 1817 | if (ioprio) { |
| 1818 | ret = ioprio_check_cap(ioprio); |
| 1819 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1820 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1821 | |
| 1822 | kiocb->ki_ioprio = ioprio; |
| 1823 | } else |
| 1824 | kiocb->ki_ioprio = get_current_ioprio(); |
| 1825 | |
| 1826 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 1827 | if (unlikely(ret)) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1828 | return ret; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1829 | |
| 1830 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1831 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 1832 | (req->file->f_flags & O_NONBLOCK)) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1833 | req->flags |= REQ_F_NOWAIT; |
| 1834 | |
| 1835 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1836 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1837 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1838 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1839 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 1840 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1841 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1842 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1843 | kiocb->ki_flags |= IOCB_HIPRI; |
| 1844 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 1845 | req->result = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1846 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1847 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1848 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1849 | kiocb->ki_complete = io_complete_rw; |
| 1850 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1851 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1852 | req->rw.addr = READ_ONCE(sqe->addr); |
| 1853 | req->rw.len = READ_ONCE(sqe->len); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1854 | /* we own ->private, reuse it for the buffer index */ |
| 1855 | req->rw.kiocb.private = (void *) (unsigned long) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1856 | READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1857 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 1861 | { |
| 1862 | switch (ret) { |
| 1863 | case -EIOCBQUEUED: |
| 1864 | break; |
| 1865 | case -ERESTARTSYS: |
| 1866 | case -ERESTARTNOINTR: |
| 1867 | case -ERESTARTNOHAND: |
| 1868 | case -ERESTART_RESTARTBLOCK: |
| 1869 | /* |
| 1870 | * We can't just restart the syscall, since previously |
| 1871 | * submitted sqes may already be in progress. Just fail this |
| 1872 | * IO with EINTR. |
| 1873 | */ |
| 1874 | ret = -EINTR; |
| 1875 | /* fall through */ |
| 1876 | default: |
| 1877 | kiocb->ki_complete(kiocb, ret, 0); |
| 1878 | } |
| 1879 | } |
| 1880 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1881 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt, |
| 1882 | bool in_async) |
| 1883 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1884 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 1885 | |
| 1886 | if (req->flags & REQ_F_CUR_POS) |
| 1887 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1888 | if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1889 | *nxt = __io_complete_rw(kiocb, ret); |
| 1890 | else |
| 1891 | io_rw_done(kiocb, ret); |
| 1892 | } |
| 1893 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1894 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 1895 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1896 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1897 | struct io_ring_ctx *ctx = req->ctx; |
| 1898 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1899 | struct io_mapped_ubuf *imu; |
| 1900 | unsigned index, buf_index; |
| 1901 | size_t offset; |
| 1902 | u64 buf_addr; |
| 1903 | |
| 1904 | /* attempt to use fixed buffers without having provided iovecs */ |
| 1905 | if (unlikely(!ctx->user_bufs)) |
| 1906 | return -EFAULT; |
| 1907 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1908 | buf_index = (unsigned long) req->rw.kiocb.private; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1909 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 1910 | return -EFAULT; |
| 1911 | |
| 1912 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 1913 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1914 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1915 | |
| 1916 | /* overflow */ |
| 1917 | if (buf_addr + len < buf_addr) |
| 1918 | return -EFAULT; |
| 1919 | /* not inside the mapped region */ |
| 1920 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 1921 | return -EFAULT; |
| 1922 | |
| 1923 | /* |
| 1924 | * May not be a start of buffer, set size appropriately |
| 1925 | * and advance us to the beginning. |
| 1926 | */ |
| 1927 | offset = buf_addr - imu->ubuf; |
| 1928 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1929 | |
| 1930 | if (offset) { |
| 1931 | /* |
| 1932 | * Don't use iov_iter_advance() here, as it's really slow for |
| 1933 | * using the latter parts of a big fixed buffer - it iterates |
| 1934 | * over each segment manually. We can cheat a bit here, because |
| 1935 | * we know that: |
| 1936 | * |
| 1937 | * 1) it's a BVEC iter, we set it up |
| 1938 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 1939 | * first and last bvec |
| 1940 | * |
| 1941 | * So just find our index, and adjust the iterator afterwards. |
| 1942 | * If the offset is within the first bvec (or the whole first |
| 1943 | * bvec, just use iov_iter_advance(). This makes it easier |
| 1944 | * since we can just skip the first segment, which may not |
| 1945 | * be PAGE_SIZE aligned. |
| 1946 | */ |
| 1947 | const struct bio_vec *bvec = imu->bvec; |
| 1948 | |
| 1949 | if (offset <= bvec->bv_len) { |
| 1950 | iov_iter_advance(iter, offset); |
| 1951 | } else { |
| 1952 | unsigned long seg_skip; |
| 1953 | |
| 1954 | /* skip first vec */ |
| 1955 | offset -= bvec->bv_len; |
| 1956 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 1957 | |
| 1958 | iter->bvec = bvec + seg_skip; |
| 1959 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 1960 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1961 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1962 | } |
| 1963 | } |
| 1964 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 1965 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1966 | } |
| 1967 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 1968 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 1969 | struct iovec **iovec, struct iov_iter *iter) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1970 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1971 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 1972 | size_t sqe_len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1973 | u8 opcode; |
| 1974 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1975 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 1976 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1977 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1978 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1979 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1980 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1981 | /* buffer index only valid with fixed read/write */ |
| 1982 | if (req->rw.kiocb.private) |
| 1983 | return -EINVAL; |
| 1984 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1985 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
| 1986 | ssize_t ret; |
| 1987 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 1988 | *iovec = NULL; |
| 1989 | return ret; |
| 1990 | } |
| 1991 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1992 | if (req->io) { |
| 1993 | struct io_async_rw *iorw = &req->io->rw; |
| 1994 | |
| 1995 | *iovec = iorw->iov; |
| 1996 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 1997 | if (iorw->iov == iorw->fast_iov) |
| 1998 | *iovec = NULL; |
| 1999 | return iorw->size; |
| 2000 | } |
| 2001 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2002 | if (!req->has_user) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2003 | return -EFAULT; |
| 2004 | |
| 2005 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2006 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2007 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2008 | iovec, iter); |
| 2009 | #endif |
| 2010 | |
| 2011 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2012 | } |
| 2013 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2014 | /* |
| 2015 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2016 | * by looping over ->read() or ->write() manually. |
| 2017 | */ |
| 2018 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2019 | struct iov_iter *iter) |
| 2020 | { |
| 2021 | ssize_t ret = 0; |
| 2022 | |
| 2023 | /* |
| 2024 | * Don't support polled IO through this interface, and we can't |
| 2025 | * support non-blocking either. For the latter, this just causes |
| 2026 | * the kiocb to be handled from an async context. |
| 2027 | */ |
| 2028 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2029 | return -EOPNOTSUPP; |
| 2030 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2031 | return -EAGAIN; |
| 2032 | |
| 2033 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2034 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2035 | ssize_t nr; |
| 2036 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2037 | if (!iov_iter_is_bvec(iter)) { |
| 2038 | iovec = iov_iter_iovec(iter); |
| 2039 | } else { |
| 2040 | /* fixed buffers import bvec */ |
| 2041 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2042 | + iter->iov_offset; |
| 2043 | iovec.iov_len = min(iter->count, |
| 2044 | iter->bvec->bv_len - iter->iov_offset); |
| 2045 | } |
| 2046 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2047 | if (rw == READ) { |
| 2048 | nr = file->f_op->read(file, iovec.iov_base, |
| 2049 | iovec.iov_len, &kiocb->ki_pos); |
| 2050 | } else { |
| 2051 | nr = file->f_op->write(file, iovec.iov_base, |
| 2052 | iovec.iov_len, &kiocb->ki_pos); |
| 2053 | } |
| 2054 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2055 | if (iov_iter_is_bvec(iter)) |
| 2056 | kunmap(iter->bvec->bv_page); |
| 2057 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2058 | if (nr < 0) { |
| 2059 | if (!ret) |
| 2060 | ret = nr; |
| 2061 | break; |
| 2062 | } |
| 2063 | ret += nr; |
| 2064 | if (nr != iovec.iov_len) |
| 2065 | break; |
| 2066 | iov_iter_advance(iter, nr); |
| 2067 | } |
| 2068 | |
| 2069 | return ret; |
| 2070 | } |
| 2071 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2072 | 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] | 2073 | struct iovec *iovec, struct iovec *fast_iov, |
| 2074 | struct iov_iter *iter) |
| 2075 | { |
| 2076 | req->io->rw.nr_segs = iter->nr_segs; |
| 2077 | req->io->rw.size = io_size; |
| 2078 | req->io->rw.iov = iovec; |
| 2079 | if (!req->io->rw.iov) { |
| 2080 | req->io->rw.iov = req->io->rw.fast_iov; |
| 2081 | memcpy(req->io->rw.iov, fast_iov, |
| 2082 | sizeof(struct iovec) * iter->nr_segs); |
| 2083 | } |
| 2084 | } |
| 2085 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2086 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2087 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2088 | if (!io_op_defs[req->opcode].async_ctx) |
| 2089 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2090 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2091 | return req->io == NULL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | static void io_rw_async(struct io_wq_work **workptr) |
| 2095 | { |
| 2096 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2097 | struct iovec *iov = NULL; |
| 2098 | |
| 2099 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2100 | iov = req->io->rw.iov; |
| 2101 | io_wq_submit_work(workptr); |
| 2102 | kfree(iov); |
| 2103 | } |
| 2104 | |
| 2105 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2106 | struct iovec *iovec, struct iovec *fast_iov, |
| 2107 | struct iov_iter *iter) |
| 2108 | { |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2109 | if (req->opcode == IORING_OP_READ_FIXED || |
| 2110 | req->opcode == IORING_OP_WRITE_FIXED) |
| 2111 | return 0; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2112 | if (!req->io && io_alloc_async_ctx(req)) |
| 2113 | return -ENOMEM; |
| 2114 | |
| 2115 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2116 | req->work.func = io_rw_async; |
| 2117 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2118 | } |
| 2119 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2120 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2121 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2122 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2123 | struct io_async_ctx *io; |
| 2124 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2125 | ssize_t ret; |
| 2126 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2127 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2128 | if (ret) |
| 2129 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2130 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2131 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2132 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2133 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2134 | if (!req->io) |
| 2135 | return 0; |
| 2136 | |
| 2137 | io = req->io; |
| 2138 | io->rw.iov = io->rw.fast_iov; |
| 2139 | req->io = NULL; |
| 2140 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter); |
| 2141 | req->io = io; |
| 2142 | if (ret < 0) |
| 2143 | return ret; |
| 2144 | |
| 2145 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2146 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2149 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2150 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2151 | { |
| 2152 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2153 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2154 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2155 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2156 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2157 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2158 | ret = io_import_iovec(READ, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2159 | if (ret < 0) |
| 2160 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2161 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2162 | /* Ensure we clear previously set non-block flag */ |
| 2163 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2164 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2165 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2166 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2167 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2168 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2169 | req->result = io_size; |
| 2170 | |
| 2171 | /* |
| 2172 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2173 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2174 | */ |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2175 | if (force_nonblock && !io_file_supports_async(req->file)) { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2176 | req->flags |= REQ_F_MUST_PUNT; |
| 2177 | goto copy_iov; |
| 2178 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2179 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2180 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2181 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2182 | if (!ret) { |
| 2183 | ssize_t ret2; |
| 2184 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2185 | if (req->file->f_op->read_iter) |
| 2186 | ret2 = call_read_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2187 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2188 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2189 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 2190 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2191 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2192 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2193 | } else { |
| 2194 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2195 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2196 | inline_vecs, &iter); |
| 2197 | if (ret) |
| 2198 | goto out_free; |
| 2199 | return -EAGAIN; |
| 2200 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2201 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2202 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2203 | if (!io_wq_current_is_worker()) |
| 2204 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2205 | return ret; |
| 2206 | } |
| 2207 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2208 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2209 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2210 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2211 | struct io_async_ctx *io; |
| 2212 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2213 | ssize_t ret; |
| 2214 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2215 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2216 | if (ret) |
| 2217 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2218 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2219 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 2220 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2221 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2222 | if (!req->io) |
| 2223 | return 0; |
| 2224 | |
| 2225 | io = req->io; |
| 2226 | io->rw.iov = io->rw.fast_iov; |
| 2227 | req->io = NULL; |
| 2228 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter); |
| 2229 | req->io = io; |
| 2230 | if (ret < 0) |
| 2231 | return ret; |
| 2232 | |
| 2233 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2234 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2235 | } |
| 2236 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2237 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2238 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2239 | { |
| 2240 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2241 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2242 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2243 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2244 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2245 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2246 | ret = io_import_iovec(WRITE, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2247 | if (ret < 0) |
| 2248 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2249 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2250 | /* Ensure we clear previously set non-block flag */ |
| 2251 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2252 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2253 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2254 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2255 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2256 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2257 | req->result = io_size; |
| 2258 | |
| 2259 | /* |
| 2260 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2261 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2262 | */ |
| 2263 | if (force_nonblock && !io_file_supports_async(req->file)) { |
| 2264 | req->flags |= REQ_F_MUST_PUNT; |
| 2265 | goto copy_iov; |
| 2266 | } |
| 2267 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2268 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 2269 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 2270 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2271 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2272 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2273 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2274 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2275 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2276 | ssize_t ret2; |
| 2277 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2278 | /* |
| 2279 | * Open-code file_start_write here to grab freeze protection, |
| 2280 | * which will be released by another thread in |
| 2281 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 2282 | * released so that it doesn't complain about the held lock when |
| 2283 | * we return to userspace. |
| 2284 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2285 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2286 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2287 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2288 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2289 | SB_FREEZE_WRITE); |
| 2290 | } |
| 2291 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2292 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2293 | if (req->file->f_op->write_iter) |
| 2294 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2295 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2296 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2297 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2298 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2299 | } else { |
| 2300 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2301 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2302 | inline_vecs, &iter); |
| 2303 | if (ret) |
| 2304 | goto out_free; |
| 2305 | return -EAGAIN; |
| 2306 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2307 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2308 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2309 | if (!io_wq_current_is_worker()) |
| 2310 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2311 | return ret; |
| 2312 | } |
| 2313 | |
| 2314 | /* |
| 2315 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 2316 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2317 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2318 | { |
| 2319 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2320 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2321 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2322 | return -EINVAL; |
| 2323 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2324 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2325 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2326 | return 0; |
| 2327 | } |
| 2328 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2329 | 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] | 2330 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2331 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2332 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2333 | if (!req->file) |
| 2334 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2335 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2336 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2337 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2338 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2339 | return -EINVAL; |
| 2340 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2341 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 2342 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 2343 | return -EINVAL; |
| 2344 | |
| 2345 | req->sync.off = READ_ONCE(sqe->off); |
| 2346 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2347 | return 0; |
| 2348 | } |
| 2349 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2350 | static bool io_req_cancelled(struct io_kiocb *req) |
| 2351 | { |
| 2352 | if (req->work.flags & IO_WQ_WORK_CANCEL) { |
| 2353 | req_set_fail_links(req); |
| 2354 | io_cqring_add_event(req, -ECANCELED); |
| 2355 | io_put_req(req); |
| 2356 | return true; |
| 2357 | } |
| 2358 | |
| 2359 | return false; |
| 2360 | } |
| 2361 | |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2362 | static void io_link_work_cb(struct io_wq_work **workptr) |
| 2363 | { |
| 2364 | struct io_wq_work *work = *workptr; |
| 2365 | struct io_kiocb *link = work->data; |
| 2366 | |
| 2367 | io_queue_linked_timeout(link); |
| 2368 | work->func = io_wq_submit_work; |
| 2369 | } |
| 2370 | |
| 2371 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) |
| 2372 | { |
| 2373 | struct io_kiocb *link; |
| 2374 | |
| 2375 | io_prep_async_work(nxt, &link); |
| 2376 | *workptr = &nxt->work; |
| 2377 | if (link) { |
| 2378 | nxt->work.flags |= IO_WQ_WORK_CB; |
| 2379 | nxt->work.func = io_link_work_cb; |
| 2380 | nxt->work.data = link; |
| 2381 | } |
| 2382 | } |
| 2383 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2384 | static void io_fsync_finish(struct io_wq_work **workptr) |
| 2385 | { |
| 2386 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2387 | loff_t end = req->sync.off + req->sync.len; |
| 2388 | struct io_kiocb *nxt = NULL; |
| 2389 | int ret; |
| 2390 | |
| 2391 | if (io_req_cancelled(req)) |
| 2392 | return; |
| 2393 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2394 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2395 | end > 0 ? end : LLONG_MAX, |
| 2396 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 2397 | if (ret < 0) |
| 2398 | req_set_fail_links(req); |
| 2399 | io_cqring_add_event(req, ret); |
| 2400 | io_put_req_find_next(req, &nxt); |
| 2401 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2402 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2403 | } |
| 2404 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2405 | static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2406 | bool force_nonblock) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2407 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2408 | struct io_wq_work *work, *old_work; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2409 | |
| 2410 | /* fsync always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2411 | if (force_nonblock) { |
| 2412 | io_put_req(req); |
| 2413 | req->work.func = io_fsync_finish; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2414 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2415 | } |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2416 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2417 | work = old_work = &req->work; |
| 2418 | io_fsync_finish(&work); |
| 2419 | if (work && work != old_work) |
| 2420 | *nxt = container_of(work, struct io_kiocb, work); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2421 | return 0; |
| 2422 | } |
| 2423 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2424 | static void io_fallocate_finish(struct io_wq_work **workptr) |
| 2425 | { |
| 2426 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2427 | struct io_kiocb *nxt = NULL; |
| 2428 | int ret; |
| 2429 | |
| 2430 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 2431 | req->sync.len); |
| 2432 | if (ret < 0) |
| 2433 | req_set_fail_links(req); |
| 2434 | io_cqring_add_event(req, ret); |
| 2435 | io_put_req_find_next(req, &nxt); |
| 2436 | if (nxt) |
| 2437 | io_wq_assign_next(workptr, nxt); |
| 2438 | } |
| 2439 | |
| 2440 | static int io_fallocate_prep(struct io_kiocb *req, |
| 2441 | const struct io_uring_sqe *sqe) |
| 2442 | { |
| 2443 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 2444 | return -EINVAL; |
| 2445 | |
| 2446 | req->sync.off = READ_ONCE(sqe->off); |
| 2447 | req->sync.len = READ_ONCE(sqe->addr); |
| 2448 | req->sync.mode = READ_ONCE(sqe->len); |
| 2449 | return 0; |
| 2450 | } |
| 2451 | |
| 2452 | static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2453 | bool force_nonblock) |
| 2454 | { |
| 2455 | struct io_wq_work *work, *old_work; |
| 2456 | |
| 2457 | /* fallocate always requiring blocking context */ |
| 2458 | if (force_nonblock) { |
| 2459 | io_put_req(req); |
| 2460 | req->work.func = io_fallocate_finish; |
| 2461 | return -EAGAIN; |
| 2462 | } |
| 2463 | |
| 2464 | work = old_work = &req->work; |
| 2465 | io_fallocate_finish(&work); |
| 2466 | if (work && work != old_work) |
| 2467 | *nxt = container_of(work, struct io_kiocb, work); |
| 2468 | |
| 2469 | return 0; |
| 2470 | } |
| 2471 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2472 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2473 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2474 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2475 | int ret; |
| 2476 | |
| 2477 | if (sqe->ioprio || sqe->buf_index) |
| 2478 | return -EINVAL; |
| 2479 | |
| 2480 | req->open.dfd = READ_ONCE(sqe->fd); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2481 | req->open.how.mode = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2482 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2483 | req->open.how.flags = READ_ONCE(sqe->open_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2484 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2485 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2486 | if (IS_ERR(req->open.filename)) { |
| 2487 | ret = PTR_ERR(req->open.filename); |
| 2488 | req->open.filename = NULL; |
| 2489 | return ret; |
| 2490 | } |
| 2491 | |
| 2492 | return 0; |
| 2493 | } |
| 2494 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2495 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2496 | { |
| 2497 | struct open_how __user *how; |
| 2498 | const char __user *fname; |
| 2499 | size_t len; |
| 2500 | int ret; |
| 2501 | |
| 2502 | if (sqe->ioprio || sqe->buf_index) |
| 2503 | return -EINVAL; |
| 2504 | |
| 2505 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2506 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2507 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 2508 | len = READ_ONCE(sqe->len); |
| 2509 | |
| 2510 | if (len < OPEN_HOW_SIZE_VER0) |
| 2511 | return -EINVAL; |
| 2512 | |
| 2513 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 2514 | len); |
| 2515 | if (ret) |
| 2516 | return ret; |
| 2517 | |
| 2518 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
| 2519 | req->open.how.flags |= O_LARGEFILE; |
| 2520 | |
| 2521 | req->open.filename = getname(fname); |
| 2522 | if (IS_ERR(req->open.filename)) { |
| 2523 | ret = PTR_ERR(req->open.filename); |
| 2524 | req->open.filename = NULL; |
| 2525 | return ret; |
| 2526 | } |
| 2527 | |
| 2528 | return 0; |
| 2529 | } |
| 2530 | |
| 2531 | static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2532 | bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2533 | { |
| 2534 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2535 | struct file *file; |
| 2536 | int ret; |
| 2537 | |
| 2538 | if (force_nonblock) { |
| 2539 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 2540 | return -EAGAIN; |
| 2541 | } |
| 2542 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2543 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2544 | if (ret) |
| 2545 | goto err; |
| 2546 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2547 | ret = get_unused_fd_flags(req->open.how.flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2548 | if (ret < 0) |
| 2549 | goto err; |
| 2550 | |
| 2551 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 2552 | if (IS_ERR(file)) { |
| 2553 | put_unused_fd(ret); |
| 2554 | ret = PTR_ERR(file); |
| 2555 | } else { |
| 2556 | fsnotify_open(file); |
| 2557 | fd_install(ret, file); |
| 2558 | } |
| 2559 | err: |
| 2560 | putname(req->open.filename); |
| 2561 | if (ret < 0) |
| 2562 | req_set_fail_links(req); |
| 2563 | io_cqring_add_event(req, ret); |
| 2564 | io_put_req_find_next(req, nxt); |
| 2565 | return 0; |
| 2566 | } |
| 2567 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2568 | static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2569 | bool force_nonblock) |
| 2570 | { |
| 2571 | req->open.how = build_open_how(req->open.how.flags, req->open.how.mode); |
| 2572 | return io_openat2(req, nxt, force_nonblock); |
| 2573 | } |
| 2574 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 2575 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2576 | { |
| 2577 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2578 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 2579 | return -EINVAL; |
| 2580 | |
| 2581 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 2582 | req->madvise.len = READ_ONCE(sqe->len); |
| 2583 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2584 | return 0; |
| 2585 | #else |
| 2586 | return -EOPNOTSUPP; |
| 2587 | #endif |
| 2588 | } |
| 2589 | |
| 2590 | static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2591 | bool force_nonblock) |
| 2592 | { |
| 2593 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2594 | struct io_madvise *ma = &req->madvise; |
| 2595 | int ret; |
| 2596 | |
| 2597 | if (force_nonblock) |
| 2598 | return -EAGAIN; |
| 2599 | |
| 2600 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 2601 | if (ret < 0) |
| 2602 | req_set_fail_links(req); |
| 2603 | io_cqring_add_event(req, ret); |
| 2604 | io_put_req_find_next(req, nxt); |
| 2605 | return 0; |
| 2606 | #else |
| 2607 | return -EOPNOTSUPP; |
| 2608 | #endif |
| 2609 | } |
| 2610 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 2611 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2612 | { |
| 2613 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 2614 | return -EINVAL; |
| 2615 | |
| 2616 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 2617 | req->fadvise.len = READ_ONCE(sqe->len); |
| 2618 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2619 | return 0; |
| 2620 | } |
| 2621 | |
| 2622 | static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2623 | bool force_nonblock) |
| 2624 | { |
| 2625 | struct io_fadvise *fa = &req->fadvise; |
| 2626 | int ret; |
| 2627 | |
| 2628 | /* DONTNEED may block, others _should_ not */ |
| 2629 | if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock) |
| 2630 | return -EAGAIN; |
| 2631 | |
| 2632 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 2633 | if (ret < 0) |
| 2634 | req_set_fail_links(req); |
| 2635 | io_cqring_add_event(req, ret); |
| 2636 | io_put_req_find_next(req, nxt); |
| 2637 | return 0; |
| 2638 | } |
| 2639 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2640 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2641 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2642 | const char __user *fname; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2643 | unsigned lookup_flags; |
| 2644 | int ret; |
| 2645 | |
| 2646 | if (sqe->ioprio || sqe->buf_index) |
| 2647 | return -EINVAL; |
| 2648 | |
| 2649 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2650 | req->open.mask = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2651 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2652 | req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2653 | req->open.how.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2654 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2655 | if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2656 | return -EINVAL; |
| 2657 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2658 | req->open.filename = getname_flags(fname, lookup_flags, NULL); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2659 | if (IS_ERR(req->open.filename)) { |
| 2660 | ret = PTR_ERR(req->open.filename); |
| 2661 | req->open.filename = NULL; |
| 2662 | return ret; |
| 2663 | } |
| 2664 | |
| 2665 | return 0; |
| 2666 | } |
| 2667 | |
| 2668 | static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2669 | bool force_nonblock) |
| 2670 | { |
| 2671 | struct io_open *ctx = &req->open; |
| 2672 | unsigned lookup_flags; |
| 2673 | struct path path; |
| 2674 | struct kstat stat; |
| 2675 | int ret; |
| 2676 | |
| 2677 | if (force_nonblock) |
| 2678 | return -EAGAIN; |
| 2679 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2680 | if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2681 | return -EINVAL; |
| 2682 | |
| 2683 | retry: |
| 2684 | /* filename_lookup() drops it, keep a reference */ |
| 2685 | ctx->filename->refcnt++; |
| 2686 | |
| 2687 | ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path, |
| 2688 | NULL); |
| 2689 | if (ret) |
| 2690 | goto err; |
| 2691 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2692 | ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2693 | path_put(&path); |
| 2694 | if (retry_estale(ret, lookup_flags)) { |
| 2695 | lookup_flags |= LOOKUP_REVAL; |
| 2696 | goto retry; |
| 2697 | } |
| 2698 | if (!ret) |
| 2699 | ret = cp_statx(&stat, ctx->buffer); |
| 2700 | err: |
| 2701 | putname(ctx->filename); |
| 2702 | if (ret < 0) |
| 2703 | req_set_fail_links(req); |
| 2704 | io_cqring_add_event(req, ret); |
| 2705 | io_put_req_find_next(req, nxt); |
| 2706 | return 0; |
| 2707 | } |
| 2708 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2709 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2710 | { |
| 2711 | /* |
| 2712 | * If we queue this for async, it must not be cancellable. That would |
| 2713 | * leave the 'file' in an undeterminate state. |
| 2714 | */ |
| 2715 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 2716 | |
| 2717 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 2718 | sqe->rw_flags || sqe->buf_index) |
| 2719 | return -EINVAL; |
| 2720 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2721 | return -EINVAL; |
| 2722 | |
| 2723 | req->close.fd = READ_ONCE(sqe->fd); |
| 2724 | if (req->file->f_op == &io_uring_fops || |
| 2725 | req->close.fd == req->ring_fd) |
| 2726 | return -EBADF; |
| 2727 | |
| 2728 | return 0; |
| 2729 | } |
| 2730 | |
| 2731 | static void io_close_finish(struct io_wq_work **workptr) |
| 2732 | { |
| 2733 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2734 | struct io_kiocb *nxt = NULL; |
| 2735 | |
| 2736 | /* Invoked with files, we need to do the close */ |
| 2737 | if (req->work.files) { |
| 2738 | int ret; |
| 2739 | |
| 2740 | ret = filp_close(req->close.put_file, req->work.files); |
| 2741 | if (ret < 0) { |
| 2742 | req_set_fail_links(req); |
| 2743 | } |
| 2744 | io_cqring_add_event(req, ret); |
| 2745 | } |
| 2746 | |
| 2747 | fput(req->close.put_file); |
| 2748 | |
| 2749 | /* we bypassed the re-issue, drop the submission reference */ |
| 2750 | io_put_req(req); |
| 2751 | io_put_req_find_next(req, &nxt); |
| 2752 | if (nxt) |
| 2753 | io_wq_assign_next(workptr, nxt); |
| 2754 | } |
| 2755 | |
| 2756 | static int io_close(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2757 | bool force_nonblock) |
| 2758 | { |
| 2759 | int ret; |
| 2760 | |
| 2761 | req->close.put_file = NULL; |
| 2762 | ret = __close_fd_get_file(req->close.fd, &req->close.put_file); |
| 2763 | if (ret < 0) |
| 2764 | return ret; |
| 2765 | |
| 2766 | /* if the file has a flush method, be safe and punt to async */ |
| 2767 | if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) { |
| 2768 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 2769 | goto eagain; |
| 2770 | } |
| 2771 | |
| 2772 | /* |
| 2773 | * No ->flush(), safely close from here and just punt the |
| 2774 | * fput() to async context. |
| 2775 | */ |
| 2776 | ret = filp_close(req->close.put_file, current->files); |
| 2777 | |
| 2778 | if (ret < 0) |
| 2779 | req_set_fail_links(req); |
| 2780 | io_cqring_add_event(req, ret); |
| 2781 | |
| 2782 | if (io_wq_current_is_worker()) { |
| 2783 | struct io_wq_work *old_work, *work; |
| 2784 | |
| 2785 | old_work = work = &req->work; |
| 2786 | io_close_finish(&work); |
| 2787 | if (work && work != old_work) |
| 2788 | *nxt = container_of(work, struct io_kiocb, work); |
| 2789 | return 0; |
| 2790 | } |
| 2791 | |
| 2792 | eagain: |
| 2793 | req->work.func = io_close_finish; |
| 2794 | return -EAGAIN; |
| 2795 | } |
| 2796 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2797 | 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] | 2798 | { |
| 2799 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2800 | |
| 2801 | if (!req->file) |
| 2802 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2803 | |
| 2804 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2805 | return -EINVAL; |
| 2806 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 2807 | return -EINVAL; |
| 2808 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2809 | req->sync.off = READ_ONCE(sqe->off); |
| 2810 | req->sync.len = READ_ONCE(sqe->len); |
| 2811 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2812 | return 0; |
| 2813 | } |
| 2814 | |
| 2815 | static void io_sync_file_range_finish(struct io_wq_work **workptr) |
| 2816 | { |
| 2817 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2818 | struct io_kiocb *nxt = NULL; |
| 2819 | int ret; |
| 2820 | |
| 2821 | if (io_req_cancelled(req)) |
| 2822 | return; |
| 2823 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2824 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2825 | req->sync.flags); |
| 2826 | if (ret < 0) |
| 2827 | req_set_fail_links(req); |
| 2828 | io_cqring_add_event(req, ret); |
| 2829 | io_put_req_find_next(req, &nxt); |
| 2830 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2831 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2832 | } |
| 2833 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2834 | 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] | 2835 | bool force_nonblock) |
| 2836 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2837 | struct io_wq_work *work, *old_work; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2838 | |
| 2839 | /* sync_file_range always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2840 | if (force_nonblock) { |
| 2841 | io_put_req(req); |
| 2842 | req->work.func = io_sync_file_range_finish; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2843 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2844 | } |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2845 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2846 | work = old_work = &req->work; |
| 2847 | io_sync_file_range_finish(&work); |
| 2848 | if (work && work != old_work) |
| 2849 | *nxt = container_of(work, struct io_kiocb, work); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2850 | return 0; |
| 2851 | } |
| 2852 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2853 | #if defined(CONFIG_NET) |
| 2854 | static void io_sendrecv_async(struct io_wq_work **workptr) |
| 2855 | { |
| 2856 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2857 | struct iovec *iov = NULL; |
| 2858 | |
| 2859 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2860 | iov = req->io->msg.iov; |
| 2861 | io_wq_submit_work(workptr); |
| 2862 | kfree(iov); |
| 2863 | } |
| 2864 | #endif |
| 2865 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2866 | 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] | 2867 | { |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2868 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2869 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2870 | struct io_async_ctx *io = req->io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2871 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2872 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 2873 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2874 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2875 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2876 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2877 | return 0; |
| 2878 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 2879 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2880 | return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2881 | &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2882 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2883 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2884 | #endif |
| 2885 | } |
| 2886 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2887 | static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2888 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2889 | { |
| 2890 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2891 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2892 | struct socket *sock; |
| 2893 | int ret; |
| 2894 | |
| 2895 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2896 | return -EINVAL; |
| 2897 | |
| 2898 | sock = sock_from_file(req->file, &ret); |
| 2899 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2900 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2901 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2902 | unsigned flags; |
| 2903 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2904 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2905 | kmsg = &req->io->msg; |
| 2906 | kmsg->msg.msg_name = &addr; |
| 2907 | /* if iov is set, it's allocated already */ |
| 2908 | if (!kmsg->iov) |
| 2909 | kmsg->iov = kmsg->fast_iov; |
| 2910 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2911 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2912 | struct io_sr_msg *sr = &req->sr_msg; |
| 2913 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2914 | kmsg = &io.msg; |
| 2915 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2916 | |
| 2917 | io.msg.iov = io.msg.fast_iov; |
| 2918 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 2919 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2920 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2921 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2922 | } |
| 2923 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2924 | flags = req->sr_msg.msg_flags; |
| 2925 | if (flags & MSG_DONTWAIT) |
| 2926 | req->flags |= REQ_F_NOWAIT; |
| 2927 | else if (force_nonblock) |
| 2928 | flags |= MSG_DONTWAIT; |
| 2929 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2930 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2931 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2932 | if (req->io) |
| 2933 | return -EAGAIN; |
| 2934 | if (io_alloc_async_ctx(req)) |
| 2935 | return -ENOMEM; |
| 2936 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 2937 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2938 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2939 | } |
| 2940 | if (ret == -ERESTARTSYS) |
| 2941 | ret = -EINTR; |
| 2942 | } |
| 2943 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2944 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2945 | kfree(kmsg->iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2946 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2947 | if (ret < 0) |
| 2948 | req_set_fail_links(req); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2949 | io_put_req_find_next(req, nxt); |
| 2950 | return 0; |
| 2951 | #else |
| 2952 | return -EOPNOTSUPP; |
| 2953 | #endif |
| 2954 | } |
| 2955 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2956 | static int io_send(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2957 | bool force_nonblock) |
| 2958 | { |
| 2959 | #if defined(CONFIG_NET) |
| 2960 | struct socket *sock; |
| 2961 | int ret; |
| 2962 | |
| 2963 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2964 | return -EINVAL; |
| 2965 | |
| 2966 | sock = sock_from_file(req->file, &ret); |
| 2967 | if (sock) { |
| 2968 | struct io_sr_msg *sr = &req->sr_msg; |
| 2969 | struct msghdr msg; |
| 2970 | struct iovec iov; |
| 2971 | unsigned flags; |
| 2972 | |
| 2973 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, |
| 2974 | &msg.msg_iter); |
| 2975 | if (ret) |
| 2976 | return ret; |
| 2977 | |
| 2978 | msg.msg_name = NULL; |
| 2979 | msg.msg_control = NULL; |
| 2980 | msg.msg_controllen = 0; |
| 2981 | msg.msg_namelen = 0; |
| 2982 | |
| 2983 | flags = req->sr_msg.msg_flags; |
| 2984 | if (flags & MSG_DONTWAIT) |
| 2985 | req->flags |= REQ_F_NOWAIT; |
| 2986 | else if (force_nonblock) |
| 2987 | flags |= MSG_DONTWAIT; |
| 2988 | |
| 2989 | ret = __sys_sendmsg_sock(sock, &msg, flags); |
| 2990 | if (force_nonblock && ret == -EAGAIN) |
| 2991 | return -EAGAIN; |
| 2992 | if (ret == -ERESTARTSYS) |
| 2993 | ret = -EINTR; |
| 2994 | } |
| 2995 | |
| 2996 | io_cqring_add_event(req, ret); |
| 2997 | if (ret < 0) |
| 2998 | req_set_fail_links(req); |
| 2999 | io_put_req_find_next(req, nxt); |
| 3000 | return 0; |
| 3001 | #else |
| 3002 | return -EOPNOTSUPP; |
| 3003 | #endif |
| 3004 | } |
| 3005 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3006 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 3007 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3008 | { |
| 3009 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3010 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3011 | struct io_async_ctx *io = req->io; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3012 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3013 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3014 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3015 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3016 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3017 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3018 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3019 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3020 | return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3021 | &io->msg.uaddr, &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3022 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3023 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3024 | #endif |
| 3025 | } |
| 3026 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3027 | static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3028 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3029 | { |
| 3030 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3031 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3032 | struct socket *sock; |
| 3033 | int ret; |
| 3034 | |
| 3035 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3036 | return -EINVAL; |
| 3037 | |
| 3038 | sock = sock_from_file(req->file, &ret); |
| 3039 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3040 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3041 | struct sockaddr_storage addr; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3042 | unsigned flags; |
| 3043 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3044 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3045 | kmsg = &req->io->msg; |
| 3046 | kmsg->msg.msg_name = &addr; |
| 3047 | /* if iov is set, it's allocated already */ |
| 3048 | if (!kmsg->iov) |
| 3049 | kmsg->iov = kmsg->fast_iov; |
| 3050 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3051 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3052 | struct io_sr_msg *sr = &req->sr_msg; |
| 3053 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3054 | kmsg = &io.msg; |
| 3055 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3056 | |
| 3057 | io.msg.iov = io.msg.fast_iov; |
| 3058 | ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3059 | sr->msg_flags, &io.msg.uaddr, |
| 3060 | &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3061 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3062 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3063 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3064 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3065 | flags = req->sr_msg.msg_flags; |
| 3066 | if (flags & MSG_DONTWAIT) |
| 3067 | req->flags |= REQ_F_NOWAIT; |
| 3068 | else if (force_nonblock) |
| 3069 | flags |= MSG_DONTWAIT; |
| 3070 | |
| 3071 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 3072 | kmsg->uaddr, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3073 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3074 | if (req->io) |
| 3075 | return -EAGAIN; |
| 3076 | if (io_alloc_async_ctx(req)) |
| 3077 | return -ENOMEM; |
| 3078 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 3079 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3080 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3081 | } |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 3082 | if (ret == -ERESTARTSYS) |
| 3083 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3084 | } |
| 3085 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3086 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3087 | kfree(kmsg->iov); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3088 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3089 | if (ret < 0) |
| 3090 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3091 | io_put_req_find_next(req, nxt); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3092 | return 0; |
| 3093 | #else |
| 3094 | return -EOPNOTSUPP; |
| 3095 | #endif |
| 3096 | } |
| 3097 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3098 | static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3099 | bool force_nonblock) |
| 3100 | { |
| 3101 | #if defined(CONFIG_NET) |
| 3102 | struct socket *sock; |
| 3103 | int ret; |
| 3104 | |
| 3105 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3106 | return -EINVAL; |
| 3107 | |
| 3108 | sock = sock_from_file(req->file, &ret); |
| 3109 | if (sock) { |
| 3110 | struct io_sr_msg *sr = &req->sr_msg; |
| 3111 | struct msghdr msg; |
| 3112 | struct iovec iov; |
| 3113 | unsigned flags; |
| 3114 | |
| 3115 | ret = import_single_range(READ, sr->buf, sr->len, &iov, |
| 3116 | &msg.msg_iter); |
| 3117 | if (ret) |
| 3118 | return ret; |
| 3119 | |
| 3120 | msg.msg_name = NULL; |
| 3121 | msg.msg_control = NULL; |
| 3122 | msg.msg_controllen = 0; |
| 3123 | msg.msg_namelen = 0; |
| 3124 | msg.msg_iocb = NULL; |
| 3125 | msg.msg_flags = 0; |
| 3126 | |
| 3127 | flags = req->sr_msg.msg_flags; |
| 3128 | if (flags & MSG_DONTWAIT) |
| 3129 | req->flags |= REQ_F_NOWAIT; |
| 3130 | else if (force_nonblock) |
| 3131 | flags |= MSG_DONTWAIT; |
| 3132 | |
| 3133 | ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags); |
| 3134 | if (force_nonblock && ret == -EAGAIN) |
| 3135 | return -EAGAIN; |
| 3136 | if (ret == -ERESTARTSYS) |
| 3137 | ret = -EINTR; |
| 3138 | } |
| 3139 | |
| 3140 | io_cqring_add_event(req, ret); |
| 3141 | if (ret < 0) |
| 3142 | req_set_fail_links(req); |
| 3143 | io_put_req_find_next(req, nxt); |
| 3144 | return 0; |
| 3145 | #else |
| 3146 | return -EOPNOTSUPP; |
| 3147 | #endif |
| 3148 | } |
| 3149 | |
| 3150 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3151 | 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] | 3152 | { |
| 3153 | #if defined(CONFIG_NET) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3154 | struct io_accept *accept = &req->accept; |
| 3155 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3156 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3157 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 3158 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3159 | return -EINVAL; |
| 3160 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 3161 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3162 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3163 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3164 | return 0; |
| 3165 | #else |
| 3166 | return -EOPNOTSUPP; |
| 3167 | #endif |
| 3168 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3169 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3170 | #if defined(CONFIG_NET) |
| 3171 | static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3172 | bool force_nonblock) |
| 3173 | { |
| 3174 | struct io_accept *accept = &req->accept; |
| 3175 | unsigned file_flags; |
| 3176 | int ret; |
| 3177 | |
| 3178 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3179 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
| 3180 | accept->addr_len, accept->flags); |
| 3181 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3182 | return -EAGAIN; |
Jens Axboe | 8e3cca1 | 2019-11-09 19:52:33 -0700 | [diff] [blame] | 3183 | if (ret == -ERESTARTSYS) |
| 3184 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3185 | if (ret < 0) |
| 3186 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3187 | io_cqring_add_event(req, ret); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3188 | io_put_req_find_next(req, nxt); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3189 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | static void io_accept_finish(struct io_wq_work **workptr) |
| 3193 | { |
| 3194 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3195 | struct io_kiocb *nxt = NULL; |
| 3196 | |
| 3197 | if (io_req_cancelled(req)) |
| 3198 | return; |
| 3199 | __io_accept(req, &nxt, false); |
| 3200 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3201 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3202 | } |
| 3203 | #endif |
| 3204 | |
| 3205 | static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3206 | bool force_nonblock) |
| 3207 | { |
| 3208 | #if defined(CONFIG_NET) |
| 3209 | int ret; |
| 3210 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3211 | ret = __io_accept(req, nxt, force_nonblock); |
| 3212 | if (ret == -EAGAIN && force_nonblock) { |
| 3213 | req->work.func = io_accept_finish; |
| 3214 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 3215 | io_put_req(req); |
| 3216 | return -EAGAIN; |
| 3217 | } |
| 3218 | return 0; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3219 | #else |
| 3220 | return -EOPNOTSUPP; |
| 3221 | #endif |
| 3222 | } |
| 3223 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3224 | 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] | 3225 | { |
| 3226 | #if defined(CONFIG_NET) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3227 | struct io_connect *conn = &req->connect; |
| 3228 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3229 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3230 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3231 | return -EINVAL; |
| 3232 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 3233 | return -EINVAL; |
| 3234 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3235 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3236 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 3237 | |
| 3238 | if (!io) |
| 3239 | return 0; |
| 3240 | |
| 3241 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3242 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3243 | #else |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3244 | return -EOPNOTSUPP; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3245 | #endif |
| 3246 | } |
| 3247 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3248 | static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3249 | bool force_nonblock) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3250 | { |
| 3251 | #if defined(CONFIG_NET) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3252 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3253 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3254 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3255 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3256 | if (req->io) { |
| 3257 | io = req->io; |
| 3258 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3259 | ret = move_addr_to_kernel(req->connect.addr, |
| 3260 | req->connect.addr_len, |
| 3261 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3262 | if (ret) |
| 3263 | goto out; |
| 3264 | io = &__io; |
| 3265 | } |
| 3266 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3267 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3268 | |
| 3269 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 3270 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 3271 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3272 | if (req->io) |
| 3273 | return -EAGAIN; |
| 3274 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3275 | ret = -ENOMEM; |
| 3276 | goto out; |
| 3277 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3278 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3279 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3280 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3281 | if (ret == -ERESTARTSYS) |
| 3282 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3283 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3284 | if (ret < 0) |
| 3285 | req_set_fail_links(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3286 | io_cqring_add_event(req, ret); |
| 3287 | io_put_req_find_next(req, nxt); |
| 3288 | return 0; |
| 3289 | #else |
| 3290 | return -EOPNOTSUPP; |
| 3291 | #endif |
| 3292 | } |
| 3293 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3294 | static void io_poll_remove_one(struct io_kiocb *req) |
| 3295 | { |
| 3296 | struct io_poll_iocb *poll = &req->poll; |
| 3297 | |
| 3298 | spin_lock(&poll->head->lock); |
| 3299 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3300 | if (!list_empty(&poll->wait.entry)) { |
| 3301 | list_del_init(&poll->wait.entry); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3302 | io_queue_async_work(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3303 | } |
| 3304 | spin_unlock(&poll->head->lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3305 | hash_del(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3306 | } |
| 3307 | |
| 3308 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 3309 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3310 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3311 | struct io_kiocb *req; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3312 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3313 | |
| 3314 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3315 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 3316 | struct hlist_head *list; |
| 3317 | |
| 3318 | list = &ctx->cancel_hash[i]; |
| 3319 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
| 3320 | io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3321 | } |
| 3322 | spin_unlock_irq(&ctx->completion_lock); |
| 3323 | } |
| 3324 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3325 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 3326 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3327 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3328 | struct io_kiocb *req; |
| 3329 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3330 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 3331 | hlist_for_each_entry(req, list, hash_node) { |
| 3332 | if (sqe_addr == req->user_data) { |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3333 | io_poll_remove_one(req); |
| 3334 | return 0; |
| 3335 | } |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3336 | } |
| 3337 | |
| 3338 | return -ENOENT; |
| 3339 | } |
| 3340 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3341 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 3342 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3343 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3344 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3345 | return -EINVAL; |
| 3346 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3347 | sqe->poll_events) |
| 3348 | return -EINVAL; |
| 3349 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3350 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3351 | return 0; |
| 3352 | } |
| 3353 | |
| 3354 | /* |
| 3355 | * Find a running poll command that matches one specified in sqe->addr, |
| 3356 | * and remove it if found. |
| 3357 | */ |
| 3358 | static int io_poll_remove(struct io_kiocb *req) |
| 3359 | { |
| 3360 | struct io_ring_ctx *ctx = req->ctx; |
| 3361 | u64 addr; |
| 3362 | int ret; |
| 3363 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3364 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3365 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3366 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3367 | spin_unlock_irq(&ctx->completion_lock); |
| 3368 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3369 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3370 | if (ret < 0) |
| 3371 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3372 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3373 | return 0; |
| 3374 | } |
| 3375 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3376 | 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] | 3377 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3378 | struct io_ring_ctx *ctx = req->ctx; |
| 3379 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3380 | req->poll.done = true; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3381 | if (error) |
| 3382 | io_cqring_fill_event(req, error); |
| 3383 | else |
| 3384 | io_cqring_fill_event(req, mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3385 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3386 | } |
| 3387 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3388 | static void io_poll_complete_work(struct io_wq_work **workptr) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3389 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3390 | struct io_wq_work *work = *workptr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3391 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 3392 | struct io_poll_iocb *poll = &req->poll; |
| 3393 | struct poll_table_struct pt = { ._key = poll->events }; |
| 3394 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3395 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3396 | __poll_t mask = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3397 | int ret = 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3398 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3399 | if (work->flags & IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3400 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3401 | ret = -ECANCELED; |
| 3402 | } else if (READ_ONCE(poll->canceled)) { |
| 3403 | ret = -ECANCELED; |
| 3404 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3405 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3406 | if (ret != -ECANCELED) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3407 | mask = vfs_poll(poll->file, &pt) & poll->events; |
| 3408 | |
| 3409 | /* |
| 3410 | * Note that ->ki_cancel callers also delete iocb from active_reqs after |
| 3411 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to |
| 3412 | * synchronize with them. In the cancellation case the list_del_init |
| 3413 | * itself is not actually needed, but harmless so we keep it in to |
| 3414 | * avoid further branches in the fast path. |
| 3415 | */ |
| 3416 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3417 | if (!mask && ret != -ECANCELED) { |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3418 | add_wait_queue(poll->head, &poll->wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3419 | spin_unlock_irq(&ctx->completion_lock); |
| 3420 | return; |
| 3421 | } |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3422 | hash_del(&req->hash_node); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3423 | io_poll_complete(req, mask, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3424 | spin_unlock_irq(&ctx->completion_lock); |
| 3425 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3426 | io_cqring_ev_posted(ctx); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3427 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3428 | if (ret < 0) |
| 3429 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3430 | io_put_req_find_next(req, &nxt); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3431 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3432 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3433 | } |
| 3434 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3435 | static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes) |
| 3436 | { |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3437 | struct io_kiocb *req, *tmp; |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3438 | struct req_batch rb; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3439 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 3440 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3441 | spin_lock_irq(&ctx->completion_lock); |
| 3442 | llist_for_each_entry_safe(req, tmp, nodes, llist_node) { |
| 3443 | hash_del(&req->hash_node); |
| 3444 | io_poll_complete(req, req->result, 0); |
| 3445 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3446 | if (refcount_dec_and_test(&req->refs) && |
| 3447 | !io_req_multi_free(&rb, req)) { |
| 3448 | req->flags |= REQ_F_COMP_LOCKED; |
| 3449 | io_free_req(req); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3450 | } |
| 3451 | } |
| 3452 | spin_unlock_irq(&ctx->completion_lock); |
| 3453 | |
| 3454 | io_cqring_ev_posted(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3455 | io_free_req_many(ctx, &rb); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3456 | } |
| 3457 | |
| 3458 | static void io_poll_flush(struct io_wq_work **workptr) |
| 3459 | { |
| 3460 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3461 | struct llist_node *nodes; |
| 3462 | |
| 3463 | nodes = llist_del_all(&req->ctx->poll_llist); |
| 3464 | if (nodes) |
| 3465 | __io_poll_flush(req->ctx, nodes); |
| 3466 | } |
| 3467 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3468 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 3469 | void *key) |
| 3470 | { |
Jens Axboe | e944475 | 2019-11-26 15:02:04 -0700 | [diff] [blame] | 3471 | struct io_poll_iocb *poll = wait->private; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3472 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); |
| 3473 | struct io_ring_ctx *ctx = req->ctx; |
| 3474 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3475 | |
| 3476 | /* for instances that support it check for an event match first: */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3477 | if (mask && !(mask & poll->events)) |
| 3478 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3479 | |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3480 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3481 | |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 3482 | /* |
| 3483 | * Run completion inline if we can. We're using trylock here because |
| 3484 | * we are violating the completion_lock -> poll wq lock ordering. |
| 3485 | * If we have a link timeout we're going to need the completion_lock |
| 3486 | * for finalizing the request, mark us as having grabbed that already. |
| 3487 | */ |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3488 | if (mask) { |
| 3489 | unsigned long flags; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3490 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3491 | if (llist_empty(&ctx->poll_llist) && |
| 3492 | spin_trylock_irqsave(&ctx->completion_lock, flags)) { |
| 3493 | hash_del(&req->hash_node); |
| 3494 | io_poll_complete(req, mask, 0); |
| 3495 | req->flags |= REQ_F_COMP_LOCKED; |
| 3496 | io_put_req(req); |
| 3497 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3498 | |
| 3499 | io_cqring_ev_posted(ctx); |
| 3500 | req = NULL; |
| 3501 | } else { |
| 3502 | req->result = mask; |
| 3503 | req->llist_node.next = NULL; |
| 3504 | /* if the list wasn't empty, we're done */ |
| 3505 | if (!llist_add(&req->llist_node, &ctx->poll_llist)) |
| 3506 | req = NULL; |
| 3507 | else |
| 3508 | req->work.func = io_poll_flush; |
| 3509 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3510 | } |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3511 | if (req) |
| 3512 | io_queue_async_work(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3513 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3514 | return 1; |
| 3515 | } |
| 3516 | |
| 3517 | struct io_poll_table { |
| 3518 | struct poll_table_struct pt; |
| 3519 | struct io_kiocb *req; |
| 3520 | int error; |
| 3521 | }; |
| 3522 | |
| 3523 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 3524 | struct poll_table_struct *p) |
| 3525 | { |
| 3526 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 3527 | |
| 3528 | if (unlikely(pt->req->poll.head)) { |
| 3529 | pt->error = -EINVAL; |
| 3530 | return; |
| 3531 | } |
| 3532 | |
| 3533 | pt->error = 0; |
| 3534 | pt->req->poll.head = head; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3535 | add_wait_queue(head, &pt->req->poll.wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3536 | } |
| 3537 | |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3538 | static void io_poll_req_insert(struct io_kiocb *req) |
| 3539 | { |
| 3540 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3541 | struct hlist_head *list; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3542 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3543 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 3544 | hlist_add_head(&req->hash_node, list); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3545 | } |
| 3546 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3547 | 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] | 3548 | { |
| 3549 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3550 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3551 | |
| 3552 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3553 | return -EINVAL; |
| 3554 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 3555 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3556 | if (!poll->file) |
| 3557 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3558 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3559 | events = READ_ONCE(sqe->poll_events); |
| 3560 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3561 | return 0; |
| 3562 | } |
| 3563 | |
| 3564 | static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3565 | { |
| 3566 | struct io_poll_iocb *poll = &req->poll; |
| 3567 | struct io_ring_ctx *ctx = req->ctx; |
| 3568 | struct io_poll_table ipt; |
| 3569 | bool cancel = false; |
| 3570 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3571 | |
| 3572 | INIT_IO_WORK(&req->work, io_poll_complete_work); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3573 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3574 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3575 | poll->head = NULL; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3576 | poll->done = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3577 | poll->canceled = false; |
| 3578 | |
| 3579 | ipt.pt._qproc = io_poll_queue_proc; |
| 3580 | ipt.pt._key = poll->events; |
| 3581 | ipt.req = req; |
| 3582 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ |
| 3583 | |
| 3584 | /* initialized the list so that we can do list_empty checks */ |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3585 | INIT_LIST_HEAD(&poll->wait.entry); |
| 3586 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); |
| 3587 | poll->wait.private = poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3588 | |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 3589 | INIT_LIST_HEAD(&req->list); |
| 3590 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3591 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3592 | |
| 3593 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3594 | if (likely(poll->head)) { |
| 3595 | spin_lock(&poll->head->lock); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3596 | if (unlikely(list_empty(&poll->wait.entry))) { |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3597 | if (ipt.error) |
| 3598 | cancel = true; |
| 3599 | ipt.error = 0; |
| 3600 | mask = 0; |
| 3601 | } |
| 3602 | if (mask || ipt.error) |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3603 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3604 | else if (cancel) |
| 3605 | WRITE_ONCE(poll->canceled, true); |
| 3606 | else if (!poll->done) /* actually waiting for an event */ |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3607 | io_poll_req_insert(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3608 | spin_unlock(&poll->head->lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3609 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3610 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3611 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3612 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3613 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3614 | spin_unlock_irq(&ctx->completion_lock); |
| 3615 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3616 | if (mask) { |
| 3617 | io_cqring_ev_posted(ctx); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3618 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3619 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3620 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3621 | } |
| 3622 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3623 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 3624 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3625 | struct io_timeout_data *data = container_of(timer, |
| 3626 | struct io_timeout_data, timer); |
| 3627 | struct io_kiocb *req = data->req; |
| 3628 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3629 | unsigned long flags; |
| 3630 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3631 | atomic_inc(&ctx->cq_timeouts); |
| 3632 | |
| 3633 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3634 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3635 | * We could be racing with timeout deletion. If the list is empty, |
| 3636 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3637 | */ |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3638 | if (!list_empty(&req->list)) { |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3639 | struct io_kiocb *prev; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3640 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3641 | /* |
| 3642 | * Adjust the reqs sequence before the current one because it |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 3643 | * will consume a slot in the cq_ring and the cq_tail |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3644 | * pointer will be increased, otherwise other timeout reqs may |
| 3645 | * return in advance without waiting for enough wait_nr. |
| 3646 | */ |
| 3647 | prev = req; |
| 3648 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 3649 | prev->sequence++; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3650 | list_del_init(&req->list); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3651 | } |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3652 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3653 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3654 | io_commit_cqring(ctx); |
| 3655 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3656 | |
| 3657 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3658 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3659 | io_put_req(req); |
| 3660 | return HRTIMER_NORESTART; |
| 3661 | } |
| 3662 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3663 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 3664 | { |
| 3665 | struct io_kiocb *req; |
| 3666 | int ret = -ENOENT; |
| 3667 | |
| 3668 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 3669 | if (user_data == req->user_data) { |
| 3670 | list_del_init(&req->list); |
| 3671 | ret = 0; |
| 3672 | break; |
| 3673 | } |
| 3674 | } |
| 3675 | |
| 3676 | if (ret == -ENOENT) |
| 3677 | return ret; |
| 3678 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3679 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3680 | if (ret == -1) |
| 3681 | return -EALREADY; |
| 3682 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3683 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3684 | io_cqring_fill_event(req, -ECANCELED); |
| 3685 | io_put_req(req); |
| 3686 | return 0; |
| 3687 | } |
| 3688 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3689 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 3690 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3691 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3692 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3693 | return -EINVAL; |
| 3694 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 3695 | return -EINVAL; |
| 3696 | |
| 3697 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 3698 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 3699 | if (req->timeout.flags) |
| 3700 | return -EINVAL; |
| 3701 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3702 | return 0; |
| 3703 | } |
| 3704 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3705 | /* |
| 3706 | * Remove or update an existing timeout command |
| 3707 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3708 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3709 | { |
| 3710 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3711 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3712 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3713 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3714 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3715 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3716 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3717 | io_commit_cqring(ctx); |
| 3718 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3719 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3720 | if (ret < 0) |
| 3721 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3722 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3723 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3724 | } |
| 3725 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3726 | 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] | 3727 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3728 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3729 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3730 | unsigned flags; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3731 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3732 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3733 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3734 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3735 | return -EINVAL; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3736 | if (sqe->off && is_timeout_link) |
| 3737 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3738 | flags = READ_ONCE(sqe->timeout_flags); |
| 3739 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3740 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 3741 | |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3742 | req->timeout.count = READ_ONCE(sqe->off); |
| 3743 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3744 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3745 | return -ENOMEM; |
| 3746 | |
| 3747 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3748 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3749 | req->flags |= REQ_F_TIMEOUT; |
| 3750 | |
| 3751 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3752 | return -EFAULT; |
| 3753 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3754 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3755 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3756 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3757 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3758 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3759 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 3760 | return 0; |
| 3761 | } |
| 3762 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3763 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3764 | { |
| 3765 | unsigned count; |
| 3766 | struct io_ring_ctx *ctx = req->ctx; |
| 3767 | struct io_timeout_data *data; |
| 3768 | struct list_head *entry; |
| 3769 | unsigned span = 0; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3770 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3771 | data = &req->io->timeout; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3772 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3773 | /* |
| 3774 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3775 | * timeout event to be satisfied. If it isn't set, then this is |
| 3776 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3777 | */ |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3778 | count = req->timeout.count; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3779 | if (!count) { |
| 3780 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 3781 | spin_lock_irq(&ctx->completion_lock); |
| 3782 | entry = ctx->timeout_list.prev; |
| 3783 | goto add; |
| 3784 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3785 | |
| 3786 | req->sequence = ctx->cached_sq_head + count - 1; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3787 | data->seq_offset = count; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3788 | |
| 3789 | /* |
| 3790 | * Insertion sort, ensuring the first entry in the list is always |
| 3791 | * the one we need first. |
| 3792 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3793 | spin_lock_irq(&ctx->completion_lock); |
| 3794 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 3795 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3796 | unsigned nxt_sq_head; |
| 3797 | long long tmp, tmp_nxt; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3798 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3799 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3800 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 3801 | continue; |
| 3802 | |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3803 | /* |
| 3804 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 3805 | * long to store it. |
| 3806 | */ |
| 3807 | tmp = (long long)ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 3808 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
| 3809 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3810 | |
| 3811 | /* |
| 3812 | * cached_sq_head may overflow, and it will never overflow twice |
| 3813 | * once there is some timeout req still be valid. |
| 3814 | */ |
| 3815 | if (ctx->cached_sq_head < nxt_sq_head) |
yangerkun | 8b07a65 | 2019-10-17 12:12:35 +0800 | [diff] [blame] | 3816 | tmp += UINT_MAX; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3817 | |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3818 | if (tmp > tmp_nxt) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3819 | break; |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3820 | |
| 3821 | /* |
| 3822 | * Sequence of reqs after the insert one and itself should |
| 3823 | * be adjusted because each timeout req consumes a slot. |
| 3824 | */ |
| 3825 | span++; |
| 3826 | nxt->sequence++; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3827 | } |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3828 | req->sequence -= span; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3829 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3830 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3831 | data->timer.function = io_timeout_fn; |
| 3832 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3833 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3834 | return 0; |
| 3835 | } |
| 3836 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3837 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3838 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3839 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3840 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3841 | return req->user_data == (unsigned long) data; |
| 3842 | } |
| 3843 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3844 | 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] | 3845 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3846 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3847 | int ret = 0; |
| 3848 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3849 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
| 3850 | switch (cancel_ret) { |
| 3851 | case IO_WQ_CANCEL_OK: |
| 3852 | ret = 0; |
| 3853 | break; |
| 3854 | case IO_WQ_CANCEL_RUNNING: |
| 3855 | ret = -EALREADY; |
| 3856 | break; |
| 3857 | case IO_WQ_CANCEL_NOTFOUND: |
| 3858 | ret = -ENOENT; |
| 3859 | break; |
| 3860 | } |
| 3861 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3862 | return ret; |
| 3863 | } |
| 3864 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3865 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 3866 | struct io_kiocb *req, __u64 sqe_addr, |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3867 | struct io_kiocb **nxt, int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3868 | { |
| 3869 | unsigned long flags; |
| 3870 | int ret; |
| 3871 | |
| 3872 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 3873 | if (ret != -ENOENT) { |
| 3874 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3875 | goto done; |
| 3876 | } |
| 3877 | |
| 3878 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3879 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 3880 | if (ret != -ENOENT) |
| 3881 | goto done; |
| 3882 | ret = io_poll_cancel(ctx, sqe_addr); |
| 3883 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3884 | if (!ret) |
| 3885 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3886 | io_cqring_fill_event(req, ret); |
| 3887 | io_commit_cqring(ctx); |
| 3888 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3889 | io_cqring_ev_posted(ctx); |
| 3890 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3891 | if (ret < 0) |
| 3892 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3893 | io_put_req_find_next(req, nxt); |
| 3894 | } |
| 3895 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3896 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 3897 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3898 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3899 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3900 | return -EINVAL; |
| 3901 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 3902 | sqe->cancel_flags) |
| 3903 | return -EINVAL; |
| 3904 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3905 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 3906 | return 0; |
| 3907 | } |
| 3908 | |
| 3909 | static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3910 | { |
| 3911 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3912 | |
| 3913 | io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3914 | return 0; |
| 3915 | } |
| 3916 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 3917 | static int io_files_update_prep(struct io_kiocb *req, |
| 3918 | const struct io_uring_sqe *sqe) |
| 3919 | { |
| 3920 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) |
| 3921 | return -EINVAL; |
| 3922 | |
| 3923 | req->files_update.offset = READ_ONCE(sqe->off); |
| 3924 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 3925 | if (!req->files_update.nr_args) |
| 3926 | return -EINVAL; |
| 3927 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 3928 | return 0; |
| 3929 | } |
| 3930 | |
| 3931 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) |
| 3932 | { |
| 3933 | struct io_ring_ctx *ctx = req->ctx; |
| 3934 | struct io_uring_files_update up; |
| 3935 | int ret; |
| 3936 | |
| 3937 | if (force_nonblock) { |
| 3938 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 3939 | return -EAGAIN; |
| 3940 | } |
| 3941 | |
| 3942 | up.offset = req->files_update.offset; |
| 3943 | up.fds = req->files_update.arg; |
| 3944 | |
| 3945 | mutex_lock(&ctx->uring_lock); |
| 3946 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 3947 | mutex_unlock(&ctx->uring_lock); |
| 3948 | |
| 3949 | if (ret < 0) |
| 3950 | req_set_fail_links(req); |
| 3951 | io_cqring_add_event(req, ret); |
| 3952 | io_put_req(req); |
| 3953 | return 0; |
| 3954 | } |
| 3955 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3956 | static int io_req_defer_prep(struct io_kiocb *req, |
| 3957 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3958 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3959 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3960 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3961 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3962 | case IORING_OP_NOP: |
| 3963 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3964 | case IORING_OP_READV: |
| 3965 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3966 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3967 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3968 | break; |
| 3969 | case IORING_OP_WRITEV: |
| 3970 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3971 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3972 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3973 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3974 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3975 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3976 | break; |
| 3977 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3978 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3979 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3980 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3981 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3982 | break; |
| 3983 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3984 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3985 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3986 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3987 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3988 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3989 | break; |
| 3990 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3991 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3992 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3993 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3994 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3995 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3996 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3997 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3998 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3999 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4000 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4001 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4002 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4003 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4004 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4005 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4006 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4007 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4008 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4009 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4010 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4011 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4012 | case IORING_OP_FALLOCATE: |
| 4013 | ret = io_fallocate_prep(req, sqe); |
| 4014 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4015 | case IORING_OP_OPENAT: |
| 4016 | ret = io_openat_prep(req, sqe); |
| 4017 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4018 | case IORING_OP_CLOSE: |
| 4019 | ret = io_close_prep(req, sqe); |
| 4020 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4021 | case IORING_OP_FILES_UPDATE: |
| 4022 | ret = io_files_update_prep(req, sqe); |
| 4023 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4024 | case IORING_OP_STATX: |
| 4025 | ret = io_statx_prep(req, sqe); |
| 4026 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4027 | case IORING_OP_FADVISE: |
| 4028 | ret = io_fadvise_prep(req, sqe); |
| 4029 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4030 | case IORING_OP_MADVISE: |
| 4031 | ret = io_madvise_prep(req, sqe); |
| 4032 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4033 | case IORING_OP_OPENAT2: |
| 4034 | ret = io_openat2_prep(req, sqe); |
| 4035 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4036 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4037 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 4038 | req->opcode); |
| 4039 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4040 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4041 | } |
| 4042 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4043 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4044 | } |
| 4045 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4046 | 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] | 4047 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4048 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4049 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4050 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4051 | /* Still need defer if there is pending req in defer list. */ |
| 4052 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4053 | return 0; |
| 4054 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4055 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4056 | return -EAGAIN; |
| 4057 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4058 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4059 | if (ret < 0) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4060 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4061 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4062 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4063 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4064 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4065 | return 0; |
| 4066 | } |
| 4067 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 4068 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4069 | list_add_tail(&req->list, &ctx->defer_list); |
| 4070 | spin_unlock_irq(&ctx->completion_lock); |
| 4071 | return -EIOCBQUEUED; |
| 4072 | } |
| 4073 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4074 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4075 | struct io_kiocb **nxt, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4076 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4077 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4078 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4079 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4080 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4081 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4082 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4083 | break; |
| 4084 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4085 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4086 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4087 | if (sqe) { |
| 4088 | ret = io_read_prep(req, sqe, force_nonblock); |
| 4089 | if (ret < 0) |
| 4090 | break; |
| 4091 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4092 | ret = io_read(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4093 | break; |
| 4094 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4095 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4096 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4097 | if (sqe) { |
| 4098 | ret = io_write_prep(req, sqe, force_nonblock); |
| 4099 | if (ret < 0) |
| 4100 | break; |
| 4101 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4102 | ret = io_write(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4103 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4104 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4105 | if (sqe) { |
| 4106 | ret = io_prep_fsync(req, sqe); |
| 4107 | if (ret < 0) |
| 4108 | break; |
| 4109 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4110 | ret = io_fsync(req, nxt, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4111 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4112 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4113 | if (sqe) { |
| 4114 | ret = io_poll_add_prep(req, sqe); |
| 4115 | if (ret) |
| 4116 | break; |
| 4117 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4118 | ret = io_poll_add(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4119 | break; |
| 4120 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4121 | if (sqe) { |
| 4122 | ret = io_poll_remove_prep(req, sqe); |
| 4123 | if (ret < 0) |
| 4124 | break; |
| 4125 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4126 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4127 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4128 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4129 | if (sqe) { |
| 4130 | ret = io_prep_sfr(req, sqe); |
| 4131 | if (ret < 0) |
| 4132 | break; |
| 4133 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4134 | ret = io_sync_file_range(req, nxt, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4135 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4136 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4137 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4138 | if (sqe) { |
| 4139 | ret = io_sendmsg_prep(req, sqe); |
| 4140 | if (ret < 0) |
| 4141 | break; |
| 4142 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4143 | if (req->opcode == IORING_OP_SENDMSG) |
| 4144 | ret = io_sendmsg(req, nxt, force_nonblock); |
| 4145 | else |
| 4146 | ret = io_send(req, nxt, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4147 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4148 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4149 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4150 | if (sqe) { |
| 4151 | ret = io_recvmsg_prep(req, sqe); |
| 4152 | if (ret) |
| 4153 | break; |
| 4154 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4155 | if (req->opcode == IORING_OP_RECVMSG) |
| 4156 | ret = io_recvmsg(req, nxt, force_nonblock); |
| 4157 | else |
| 4158 | ret = io_recv(req, nxt, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4159 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4160 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4161 | if (sqe) { |
| 4162 | ret = io_timeout_prep(req, sqe, false); |
| 4163 | if (ret) |
| 4164 | break; |
| 4165 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4166 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4167 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4168 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4169 | if (sqe) { |
| 4170 | ret = io_timeout_remove_prep(req, sqe); |
| 4171 | if (ret) |
| 4172 | break; |
| 4173 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4174 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4175 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4176 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4177 | if (sqe) { |
| 4178 | ret = io_accept_prep(req, sqe); |
| 4179 | if (ret) |
| 4180 | break; |
| 4181 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4182 | ret = io_accept(req, nxt, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4183 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4184 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4185 | if (sqe) { |
| 4186 | ret = io_connect_prep(req, sqe); |
| 4187 | if (ret) |
| 4188 | break; |
| 4189 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4190 | ret = io_connect(req, nxt, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4191 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4192 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4193 | if (sqe) { |
| 4194 | ret = io_async_cancel_prep(req, sqe); |
| 4195 | if (ret) |
| 4196 | break; |
| 4197 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4198 | ret = io_async_cancel(req, nxt); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4199 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4200 | case IORING_OP_FALLOCATE: |
| 4201 | if (sqe) { |
| 4202 | ret = io_fallocate_prep(req, sqe); |
| 4203 | if (ret) |
| 4204 | break; |
| 4205 | } |
| 4206 | ret = io_fallocate(req, nxt, force_nonblock); |
| 4207 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4208 | case IORING_OP_OPENAT: |
| 4209 | if (sqe) { |
| 4210 | ret = io_openat_prep(req, sqe); |
| 4211 | if (ret) |
| 4212 | break; |
| 4213 | } |
| 4214 | ret = io_openat(req, nxt, force_nonblock); |
| 4215 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4216 | case IORING_OP_CLOSE: |
| 4217 | if (sqe) { |
| 4218 | ret = io_close_prep(req, sqe); |
| 4219 | if (ret) |
| 4220 | break; |
| 4221 | } |
| 4222 | ret = io_close(req, nxt, force_nonblock); |
| 4223 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4224 | case IORING_OP_FILES_UPDATE: |
| 4225 | if (sqe) { |
| 4226 | ret = io_files_update_prep(req, sqe); |
| 4227 | if (ret) |
| 4228 | break; |
| 4229 | } |
| 4230 | ret = io_files_update(req, force_nonblock); |
| 4231 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4232 | case IORING_OP_STATX: |
| 4233 | if (sqe) { |
| 4234 | ret = io_statx_prep(req, sqe); |
| 4235 | if (ret) |
| 4236 | break; |
| 4237 | } |
| 4238 | ret = io_statx(req, nxt, force_nonblock); |
| 4239 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4240 | case IORING_OP_FADVISE: |
| 4241 | if (sqe) { |
| 4242 | ret = io_fadvise_prep(req, sqe); |
| 4243 | if (ret) |
| 4244 | break; |
| 4245 | } |
| 4246 | ret = io_fadvise(req, nxt, force_nonblock); |
| 4247 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4248 | case IORING_OP_MADVISE: |
| 4249 | if (sqe) { |
| 4250 | ret = io_madvise_prep(req, sqe); |
| 4251 | if (ret) |
| 4252 | break; |
| 4253 | } |
| 4254 | ret = io_madvise(req, nxt, force_nonblock); |
| 4255 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4256 | case IORING_OP_OPENAT2: |
| 4257 | if (sqe) { |
| 4258 | ret = io_openat2_prep(req, sqe); |
| 4259 | if (ret) |
| 4260 | break; |
| 4261 | } |
| 4262 | ret = io_openat2(req, nxt, force_nonblock); |
| 4263 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4264 | default: |
| 4265 | ret = -EINVAL; |
| 4266 | break; |
| 4267 | } |
| 4268 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4269 | if (ret) |
| 4270 | return ret; |
| 4271 | |
| 4272 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4273 | const bool in_async = io_wq_current_is_worker(); |
| 4274 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4275 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4276 | return -EAGAIN; |
| 4277 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4278 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 4279 | if (in_async) |
| 4280 | mutex_lock(&ctx->uring_lock); |
| 4281 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4282 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4283 | |
| 4284 | if (in_async) |
| 4285 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4286 | } |
| 4287 | |
| 4288 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4289 | } |
| 4290 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4291 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4292 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4293 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4294 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4295 | struct io_kiocb *nxt = NULL; |
| 4296 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4297 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4298 | /* if NO_CANCEL is set, we must still run the work */ |
| 4299 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 4300 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4301 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4302 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4303 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4304 | if (!ret) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4305 | req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0; |
| 4306 | req->in_async = true; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4307 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4308 | ret = io_issue_sqe(req, NULL, &nxt, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4309 | /* |
| 4310 | * We can get EAGAIN for polled IO even though we're |
| 4311 | * forcing a sync submission from here, since we can't |
| 4312 | * wait for request slots on the block side. |
| 4313 | */ |
| 4314 | if (ret != -EAGAIN) |
| 4315 | break; |
| 4316 | cond_resched(); |
| 4317 | } while (1); |
| 4318 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4319 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4320 | /* drop submission reference */ |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 4321 | io_put_req(req); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4322 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4323 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4324 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4325 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4326 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4327 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4328 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4329 | /* if a dependent link is ready, pass it back */ |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4330 | if (!ret && nxt) |
| 4331 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4332 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4333 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4334 | static int io_req_needs_file(struct io_kiocb *req, int fd) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4335 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4336 | if (!io_op_defs[req->opcode].needs_file) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4337 | return 0; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4338 | if (fd == -1 && io_op_defs[req->opcode].fd_non_neg) |
| 4339 | return 0; |
| 4340 | return 1; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4341 | } |
| 4342 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4343 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 4344 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4345 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4346 | struct fixed_file_table *table; |
| 4347 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4348 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
| 4349 | return table->files[index & IORING_FILE_TABLE_MASK];; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4350 | } |
| 4351 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4352 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
| 4353 | const struct io_uring_sqe *sqe) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4354 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4355 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4356 | unsigned flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4357 | int fd; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4358 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4359 | flags = READ_ONCE(sqe->flags); |
| 4360 | fd = READ_ONCE(sqe->fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4361 | |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4362 | if (flags & IOSQE_IO_DRAIN) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4363 | req->flags |= REQ_F_IO_DRAIN; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4364 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4365 | if (!io_req_needs_file(req, fd)) |
| 4366 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4367 | |
| 4368 | if (flags & IOSQE_FIXED_FILE) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4369 | if (unlikely(!ctx->file_data || |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4370 | (unsigned) fd >= ctx->nr_user_files)) |
| 4371 | return -EBADF; |
Jens Axboe | b762012 | 2019-10-26 07:22:55 -0600 | [diff] [blame] | 4372 | fd = array_index_nospec(fd, ctx->nr_user_files); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4373 | req->file = io_file_from_index(ctx, fd); |
| 4374 | if (!req->file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4375 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4376 | req->flags |= REQ_F_FIXED_FILE; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4377 | percpu_ref_get(&ctx->file_data->refs); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4378 | } else { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4379 | if (req->needs_fixed_file) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4380 | return -EBADF; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 4381 | trace_io_uring_file_get(ctx, fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4382 | req->file = io_file_get(state, fd); |
| 4383 | if (unlikely(!req->file)) |
| 4384 | return -EBADF; |
| 4385 | } |
| 4386 | |
| 4387 | return 0; |
| 4388 | } |
| 4389 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4390 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4391 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4392 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4393 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4394 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4395 | if (!req->ring_file) |
| 4396 | return -EBADF; |
| 4397 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4398 | rcu_read_lock(); |
| 4399 | spin_lock_irq(&ctx->inflight_lock); |
| 4400 | /* |
| 4401 | * We use the f_ops->flush() handler to ensure that we can flush |
| 4402 | * out work accessing these files if the fd is closed. Check if |
| 4403 | * the fd has changed since we started down this path, and disallow |
| 4404 | * this operation if it has. |
| 4405 | */ |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4406 | if (fcheck(req->ring_fd) == req->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4407 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 4408 | req->flags |= REQ_F_INFLIGHT; |
| 4409 | req->work.files = current->files; |
| 4410 | ret = 0; |
| 4411 | } |
| 4412 | spin_unlock_irq(&ctx->inflight_lock); |
| 4413 | rcu_read_unlock(); |
| 4414 | |
| 4415 | return ret; |
| 4416 | } |
| 4417 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4418 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 4419 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4420 | struct io_timeout_data *data = container_of(timer, |
| 4421 | struct io_timeout_data, timer); |
| 4422 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4423 | struct io_ring_ctx *ctx = req->ctx; |
| 4424 | struct io_kiocb *prev = NULL; |
| 4425 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4426 | |
| 4427 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4428 | |
| 4429 | /* |
| 4430 | * We don't expect the list to be empty, that will only happen if we |
| 4431 | * race with the completion of the linked work. |
| 4432 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4433 | if (!list_empty(&req->link_list)) { |
| 4434 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 4435 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4436 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4437 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4438 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 4439 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4440 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4441 | } |
| 4442 | |
| 4443 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4444 | |
| 4445 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4446 | req_set_fail_links(prev); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4447 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
| 4448 | -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4449 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4450 | } else { |
| 4451 | io_cqring_add_event(req, -ETIME); |
| 4452 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4453 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4454 | return HRTIMER_NORESTART; |
| 4455 | } |
| 4456 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4457 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4458 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4459 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4460 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4461 | /* |
| 4462 | * If the list is now empty, then our linked request finished before |
| 4463 | * we got a chance to setup the timer |
| 4464 | */ |
| 4465 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4466 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4467 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4468 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4469 | data->timer.function = io_link_timeout_fn; |
| 4470 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 4471 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4472 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4473 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4474 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4475 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4476 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4477 | } |
| 4478 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4479 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4480 | { |
| 4481 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4482 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4483 | if (!(req->flags & REQ_F_LINK)) |
| 4484 | return NULL; |
| 4485 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4486 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 4487 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4488 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4489 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4490 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4491 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4492 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4493 | } |
| 4494 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4495 | 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] | 4496 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4497 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4498 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4499 | int ret; |
| 4500 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4501 | again: |
| 4502 | linked_timeout = io_prep_linked_timeout(req); |
| 4503 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4504 | ret = io_issue_sqe(req, sqe, &nxt, true); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 4505 | |
| 4506 | /* |
| 4507 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 4508 | * doesn't support non-blocking read/write attempts |
| 4509 | */ |
| 4510 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 4511 | (req->flags & REQ_F_MUST_PUNT))) { |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4512 | if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) { |
| 4513 | ret = io_grab_files(req); |
| 4514 | if (ret) |
| 4515 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4516 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4517 | |
| 4518 | /* |
| 4519 | * Queued up for async execution, worker will release |
| 4520 | * submit reference when the iocb is actually submitted. |
| 4521 | */ |
| 4522 | io_queue_async_work(req); |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4523 | goto done_req; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4524 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4525 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4526 | err: |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4527 | /* drop submission reference */ |
| 4528 | io_put_req(req); |
| 4529 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4530 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4531 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4532 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4533 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4534 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4535 | } |
| 4536 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4537 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4538 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4539 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4540 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4541 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4542 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4543 | done_req: |
| 4544 | if (nxt) { |
| 4545 | req = nxt; |
| 4546 | nxt = NULL; |
| 4547 | goto again; |
| 4548 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4549 | } |
| 4550 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4551 | 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] | 4552 | { |
| 4553 | int ret; |
| 4554 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4555 | if (unlikely(req->ctx->drain_next)) { |
| 4556 | req->flags |= REQ_F_IO_DRAIN; |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 4557 | req->ctx->drain_next = 0; |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4558 | } |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 4559 | req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK) != 0; |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4560 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4561 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4562 | if (ret) { |
| 4563 | if (ret != -EIOCBQUEUED) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4564 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4565 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4566 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4567 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 4568 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4569 | /* |
| 4570 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 4571 | * to async execution. |
| 4572 | */ |
| 4573 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 4574 | io_queue_async_work(req); |
| 4575 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4576 | __io_queue_sqe(req, sqe); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4577 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4578 | } |
| 4579 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4580 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4581 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4582 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4583 | io_cqring_add_event(req, -ECANCELED); |
| 4584 | io_double_put_req(req); |
| 4585 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4586 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4587 | } |
| 4588 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4589 | #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] | 4590 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4591 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4592 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4593 | struct io_submit_state *state, struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4594 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4595 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4596 | unsigned int sqe_flags; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4597 | int ret; |
| 4598 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4599 | sqe_flags = READ_ONCE(sqe->flags); |
| 4600 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4601 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4602 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4603 | ret = -EINVAL; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4604 | goto err_req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4605 | } |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4606 | if (sqe_flags & IOSQE_ASYNC) |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4607 | req->flags |= REQ_F_FORCE_ASYNC; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4608 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4609 | ret = io_req_set_file(state, req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4610 | if (unlikely(ret)) { |
| 4611 | err_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4612 | io_cqring_add_event(req, ret); |
| 4613 | io_double_put_req(req); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4614 | return false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4615 | } |
| 4616 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4617 | /* |
| 4618 | * If we already have a head request, queue this one for async |
| 4619 | * submittal once the head completes. If we don't have a head but |
| 4620 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 4621 | * submitted sync once the chain is complete. If none of those |
| 4622 | * conditions are true (normal request), then just queue it. |
| 4623 | */ |
| 4624 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4625 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4626 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4627 | if (sqe_flags & IOSQE_IO_DRAIN) |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4628 | head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN; |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4629 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4630 | if (sqe_flags & IOSQE_IO_HARDLINK) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4631 | req->flags |= REQ_F_HARDLINK; |
| 4632 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4633 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4634 | ret = -EAGAIN; |
| 4635 | goto err_req; |
| 4636 | } |
| 4637 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4638 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4639 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4640 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4641 | head->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4642 | goto err_req; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4643 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4644 | trace_io_uring_link(ctx, req, head); |
| 4645 | list_add_tail(&req->link_list, &head->link_list); |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4646 | |
| 4647 | /* last request of a link, enqueue the link */ |
| 4648 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) { |
| 4649 | io_queue_link_head(head); |
| 4650 | *link = NULL; |
| 4651 | } |
| 4652 | } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4653 | req->flags |= REQ_F_LINK; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4654 | if (sqe_flags & IOSQE_IO_HARDLINK) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4655 | req->flags |= REQ_F_HARDLINK; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4656 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4657 | INIT_LIST_HEAD(&req->link_list); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4658 | ret = io_req_defer_prep(req, sqe); |
| 4659 | if (ret) |
| 4660 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4661 | *link = req; |
| 4662 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4663 | io_queue_sqe(req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4664 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4665 | |
| 4666 | return true; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4667 | } |
| 4668 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4669 | /* |
| 4670 | * Batched submission is done, ensure local IO is flushed out. |
| 4671 | */ |
| 4672 | static void io_submit_state_end(struct io_submit_state *state) |
| 4673 | { |
| 4674 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 4675 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4676 | if (state->free_reqs) |
| 4677 | kmem_cache_free_bulk(req_cachep, state->free_reqs, |
| 4678 | &state->reqs[state->cur_req]); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4679 | } |
| 4680 | |
| 4681 | /* |
| 4682 | * Start submission side cache. |
| 4683 | */ |
| 4684 | static void io_submit_state_start(struct io_submit_state *state, |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4685 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4686 | { |
| 4687 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4688 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4689 | state->file = NULL; |
| 4690 | state->ios_left = max_ios; |
| 4691 | } |
| 4692 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4693 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 4694 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4695 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4696 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 4697 | /* |
| 4698 | * Ensure any loads from the SQEs are done at this point, |
| 4699 | * since once we write the new head, the application could |
| 4700 | * write new data to them. |
| 4701 | */ |
| 4702 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4703 | } |
| 4704 | |
| 4705 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4706 | * 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] | 4707 | * that is mapped by userspace. This means that care needs to be taken to |
| 4708 | * ensure that reads are stable, as we cannot rely on userspace always |
| 4709 | * being a good citizen. If members of the sqe are validated and then later |
| 4710 | * used, it's important that those reads are done through READ_ONCE() to |
| 4711 | * prevent a re-load down the line. |
| 4712 | */ |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4713 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 4714 | const struct io_uring_sqe **sqe_ptr) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4715 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4716 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4717 | unsigned head; |
| 4718 | |
| 4719 | /* |
| 4720 | * The cached sq head (or cq tail) serves two purposes: |
| 4721 | * |
| 4722 | * 1) allows us to batch the cost of updating the user visible |
| 4723 | * head updates. |
| 4724 | * 2) allows the kernel side to track the head on its own, even |
| 4725 | * though the application is the one updating it. |
| 4726 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4727 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 4728 | if (likely(head < ctx->sq_entries)) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4729 | /* |
| 4730 | * All io need record the previous position, if LINK vs DARIN, |
| 4731 | * it can be used to mark the position of the first IO in the |
| 4732 | * link list. |
| 4733 | */ |
| 4734 | req->sequence = ctx->cached_sq_head; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4735 | *sqe_ptr = &ctx->sq_sqes[head]; |
| 4736 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); |
| 4737 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4738 | ctx->cached_sq_head++; |
| 4739 | return true; |
| 4740 | } |
| 4741 | |
| 4742 | /* drop invalid entries */ |
| 4743 | ctx->cached_sq_head++; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 4744 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4745 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4746 | return false; |
| 4747 | } |
| 4748 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4749 | 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] | 4750 | struct file *ring_file, int ring_fd, |
| 4751 | struct mm_struct **mm, bool async) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4752 | { |
| 4753 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4754 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4755 | int i, submitted = 0; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4756 | bool mm_fault = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4757 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 4758 | /* 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] | 4759 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 4760 | if (!list_empty(&ctx->cq_overflow_list) && |
| 4761 | !io_cqring_overflow_flush(ctx, false)) |
| 4762 | return -EBUSY; |
| 4763 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4764 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4765 | /* make sure SQ entry isn't read before tail */ |
| 4766 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 4767 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4768 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 4769 | return -EAGAIN; |
| 4770 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4771 | if (nr > IO_PLUG_THRESHOLD) { |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4772 | io_submit_state_start(&state, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4773 | statep = &state; |
| 4774 | } |
| 4775 | |
| 4776 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4777 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4778 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4779 | |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4780 | req = io_get_req(ctx, statep); |
| 4781 | if (unlikely(!req)) { |
| 4782 | if (!submitted) |
| 4783 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4784 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4785 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4786 | if (!io_get_sqring(ctx, req, &sqe)) { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4787 | __io_req_do_free(req); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4788 | break; |
| 4789 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4790 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4791 | /* will complete beyond this point, count as submitted */ |
| 4792 | submitted++; |
| 4793 | |
| 4794 | if (unlikely(req->opcode >= IORING_OP_LAST)) { |
| 4795 | io_cqring_add_event(req, -EINVAL); |
| 4796 | io_double_put_req(req); |
| 4797 | break; |
| 4798 | } |
| 4799 | |
| 4800 | if (io_op_defs[req->opcode].needs_mm && !*mm) { |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4801 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
| 4802 | if (!mm_fault) { |
| 4803 | use_mm(ctx->sqo_mm); |
| 4804 | *mm = ctx->sqo_mm; |
| 4805 | } |
| 4806 | } |
| 4807 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4808 | req->ring_file = ring_file; |
| 4809 | req->ring_fd = ring_fd; |
| 4810 | req->has_user = *mm != NULL; |
| 4811 | req->in_async = async; |
| 4812 | req->needs_fixed_file = async; |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame^] | 4813 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 4814 | true, async); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4815 | if (!io_submit_sqe(req, sqe, statep, &link)) |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4816 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4817 | } |
| 4818 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4819 | if (submitted != nr) |
| 4820 | percpu_ref_put_many(&ctx->refs, nr - submitted); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4821 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4822 | io_queue_link_head(link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4823 | if (statep) |
| 4824 | io_submit_state_end(&state); |
| 4825 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 4826 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 4827 | io_commit_sqring(ctx); |
| 4828 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4829 | return submitted; |
| 4830 | } |
| 4831 | |
| 4832 | static int io_sq_thread(void *data) |
| 4833 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4834 | struct io_ring_ctx *ctx = data; |
| 4835 | struct mm_struct *cur_mm = NULL; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4836 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4837 | mm_segment_t old_fs; |
| 4838 | DEFINE_WAIT(wait); |
| 4839 | unsigned inflight; |
| 4840 | unsigned long timeout; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4841 | int ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4842 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 4843 | complete(&ctx->completions[1]); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 4844 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4845 | old_fs = get_fs(); |
| 4846 | set_fs(USER_DS); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4847 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4848 | |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4849 | ret = timeout = inflight = 0; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4850 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4851 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4852 | |
| 4853 | if (inflight) { |
| 4854 | unsigned nr_events = 0; |
| 4855 | |
| 4856 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 4857 | /* |
| 4858 | * inflight is the count of the maximum possible |
| 4859 | * entries we submitted, but it can be smaller |
| 4860 | * if we dropped some of them. If we don't have |
| 4861 | * poll entries available, then we know that we |
| 4862 | * have nothing left to poll for. Reset the |
| 4863 | * inflight count to zero in that case. |
| 4864 | */ |
| 4865 | mutex_lock(&ctx->uring_lock); |
| 4866 | if (!list_empty(&ctx->poll_list)) |
| 4867 | __io_iopoll_check(ctx, &nr_events, 0); |
| 4868 | else |
| 4869 | inflight = 0; |
| 4870 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4871 | } else { |
| 4872 | /* |
| 4873 | * Normal IO, just pretend everything completed. |
| 4874 | * We don't have to poll completions for that. |
| 4875 | */ |
| 4876 | nr_events = inflight; |
| 4877 | } |
| 4878 | |
| 4879 | inflight -= nr_events; |
| 4880 | if (!inflight) |
| 4881 | timeout = jiffies + ctx->sq_thread_idle; |
| 4882 | } |
| 4883 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4884 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4885 | |
| 4886 | /* |
| 4887 | * If submit got -EBUSY, flag us as needing the application |
| 4888 | * to enter the kernel to reap and flush events. |
| 4889 | */ |
| 4890 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4891 | /* |
| 4892 | * We're polling. If we're within the defined idle |
| 4893 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4894 | * to sleep. The exception is if we got EBUSY doing |
| 4895 | * more IO, we should wait for the application to |
| 4896 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4897 | */ |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4898 | if (inflight || |
| 4899 | (!time_after(jiffies, timeout) && ret != -EBUSY)) { |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 4900 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4901 | continue; |
| 4902 | } |
| 4903 | |
| 4904 | /* |
| 4905 | * Drop cur_mm before scheduling, we can't hold it for |
| 4906 | * long periods (or over schedule()). Do this before |
| 4907 | * adding ourselves to the waitqueue, as the unuse/drop |
| 4908 | * may sleep. |
| 4909 | */ |
| 4910 | if (cur_mm) { |
| 4911 | unuse_mm(cur_mm); |
| 4912 | mmput(cur_mm); |
| 4913 | cur_mm = NULL; |
| 4914 | } |
| 4915 | |
| 4916 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 4917 | TASK_INTERRUPTIBLE); |
| 4918 | |
| 4919 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4920 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 4921 | /* make sure to read SQ tail after writing flags */ |
| 4922 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4923 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4924 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4925 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4926 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4927 | finish_wait(&ctx->sqo_wait, &wait); |
| 4928 | break; |
| 4929 | } |
| 4930 | if (signal_pending(current)) |
| 4931 | flush_signals(current); |
| 4932 | schedule(); |
| 4933 | finish_wait(&ctx->sqo_wait, &wait); |
| 4934 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4935 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4936 | continue; |
| 4937 | } |
| 4938 | finish_wait(&ctx->sqo_wait, &wait); |
| 4939 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4940 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4941 | } |
| 4942 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 4943 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4944 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 4945 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4946 | if (ret > 0) |
| 4947 | inflight += ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4948 | } |
| 4949 | |
| 4950 | set_fs(old_fs); |
| 4951 | if (cur_mm) { |
| 4952 | unuse_mm(cur_mm); |
| 4953 | mmput(cur_mm); |
| 4954 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4955 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 4956 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4957 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 4958 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4959 | return 0; |
| 4960 | } |
| 4961 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4962 | struct io_wait_queue { |
| 4963 | struct wait_queue_entry wq; |
| 4964 | struct io_ring_ctx *ctx; |
| 4965 | unsigned to_wait; |
| 4966 | unsigned nr_timeouts; |
| 4967 | }; |
| 4968 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4969 | 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] | 4970 | { |
| 4971 | struct io_ring_ctx *ctx = iowq->ctx; |
| 4972 | |
| 4973 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 4974 | * 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] | 4975 | * started waiting. For timeouts, we always want to return to userspace, |
| 4976 | * regardless of event count. |
| 4977 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4978 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4979 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 4980 | } |
| 4981 | |
| 4982 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 4983 | int wake_flags, void *key) |
| 4984 | { |
| 4985 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 4986 | wq); |
| 4987 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4988 | /* use noflush == true, as we can't safely rely on locking context */ |
| 4989 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4990 | return -1; |
| 4991 | |
| 4992 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 4993 | } |
| 4994 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4995 | /* |
| 4996 | * Wait until events become available, if we don't already have some. The |
| 4997 | * application must reap them itself, as they reside on the shared cq ring. |
| 4998 | */ |
| 4999 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 5000 | const sigset_t __user *sig, size_t sigsz) |
| 5001 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5002 | struct io_wait_queue iowq = { |
| 5003 | .wq = { |
| 5004 | .private = current, |
| 5005 | .func = io_wake_function, |
| 5006 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 5007 | }, |
| 5008 | .ctx = ctx, |
| 5009 | .to_wait = min_events, |
| 5010 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5011 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5012 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5013 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5014 | if (io_cqring_events(ctx, false) >= min_events) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5015 | return 0; |
| 5016 | |
| 5017 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5018 | #ifdef CONFIG_COMPAT |
| 5019 | if (in_compat_syscall()) |
| 5020 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5021 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5022 | else |
| 5023 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5024 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5025 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5026 | if (ret) |
| 5027 | return ret; |
| 5028 | } |
| 5029 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5030 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 5031 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5032 | do { |
| 5033 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 5034 | TASK_INTERRUPTIBLE); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5035 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5036 | break; |
| 5037 | schedule(); |
| 5038 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5039 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5040 | break; |
| 5041 | } |
| 5042 | } while (1); |
| 5043 | finish_wait(&ctx->wait, &iowq.wq); |
| 5044 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5045 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5046 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5047 | 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] | 5048 | } |
| 5049 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5050 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5051 | { |
| 5052 | #if defined(CONFIG_UNIX) |
| 5053 | if (ctx->ring_sock) { |
| 5054 | struct sock *sock = ctx->ring_sock->sk; |
| 5055 | struct sk_buff *skb; |
| 5056 | |
| 5057 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 5058 | kfree_skb(skb); |
| 5059 | } |
| 5060 | #else |
| 5061 | int i; |
| 5062 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5063 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5064 | struct file *file; |
| 5065 | |
| 5066 | file = io_file_from_index(ctx, i); |
| 5067 | if (file) |
| 5068 | fput(file); |
| 5069 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5070 | #endif |
| 5071 | } |
| 5072 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5073 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 5074 | { |
| 5075 | struct fixed_file_data *data; |
| 5076 | |
| 5077 | data = container_of(ref, struct fixed_file_data, refs); |
| 5078 | complete(&data->done); |
| 5079 | } |
| 5080 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5081 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5082 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5083 | struct fixed_file_data *data = ctx->file_data; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5084 | unsigned nr_tables, i; |
| 5085 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5086 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5087 | return -ENXIO; |
| 5088 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5089 | /* protect against inflight atomic switch, which drops the ref */ |
| 5090 | flush_work(&data->ref_work); |
| 5091 | percpu_ref_get(&data->refs); |
| 5092 | percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill); |
| 5093 | wait_for_completion(&data->done); |
| 5094 | percpu_ref_put(&data->refs); |
| 5095 | percpu_ref_exit(&data->refs); |
| 5096 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5097 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5098 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 5099 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5100 | kfree(data->table[i].files); |
| 5101 | kfree(data->table); |
| 5102 | kfree(data); |
| 5103 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5104 | ctx->nr_user_files = 0; |
| 5105 | return 0; |
| 5106 | } |
| 5107 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5108 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 5109 | { |
| 5110 | if (ctx->sqo_thread) { |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5111 | wait_for_completion(&ctx->completions[1]); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5112 | /* |
| 5113 | * The park is a bit of a work-around, without it we get |
| 5114 | * warning spews on shutdown with SQPOLL set and affinity |
| 5115 | * set to a single CPU. |
| 5116 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5117 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5118 | kthread_stop(ctx->sqo_thread); |
| 5119 | ctx->sqo_thread = NULL; |
| 5120 | } |
| 5121 | } |
| 5122 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5123 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 5124 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5125 | io_sq_thread_stop(ctx); |
| 5126 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5127 | if (ctx->io_wq) { |
| 5128 | io_wq_destroy(ctx->io_wq); |
| 5129 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5130 | } |
| 5131 | } |
| 5132 | |
| 5133 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5134 | /* |
| 5135 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 5136 | * the io_uring can be safely unregistered on process exit, even if we have |
| 5137 | * loops in the file referencing. |
| 5138 | */ |
| 5139 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 5140 | { |
| 5141 | struct sock *sk = ctx->ring_sock->sk; |
| 5142 | struct scm_fp_list *fpl; |
| 5143 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5144 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5145 | |
| 5146 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 5147 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 5148 | |
| 5149 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 5150 | return -EMFILE; |
| 5151 | } |
| 5152 | |
| 5153 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 5154 | if (!fpl) |
| 5155 | return -ENOMEM; |
| 5156 | |
| 5157 | skb = alloc_skb(0, GFP_KERNEL); |
| 5158 | if (!skb) { |
| 5159 | kfree(fpl); |
| 5160 | return -ENOMEM; |
| 5161 | } |
| 5162 | |
| 5163 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5164 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5165 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5166 | fpl->user = get_uid(ctx->user); |
| 5167 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5168 | struct file *file = io_file_from_index(ctx, i + offset); |
| 5169 | |
| 5170 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5171 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5172 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5173 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 5174 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5175 | } |
| 5176 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5177 | if (nr_files) { |
| 5178 | fpl->max = SCM_MAX_FD; |
| 5179 | fpl->count = nr_files; |
| 5180 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5181 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5182 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 5183 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5184 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5185 | for (i = 0; i < nr_files; i++) |
| 5186 | fput(fpl->fp[i]); |
| 5187 | } else { |
| 5188 | kfree_skb(skb); |
| 5189 | kfree(fpl); |
| 5190 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5191 | |
| 5192 | return 0; |
| 5193 | } |
| 5194 | |
| 5195 | /* |
| 5196 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 5197 | * causes regular reference counting to break down. We rely on the UNIX |
| 5198 | * garbage collection to take care of this problem for us. |
| 5199 | */ |
| 5200 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5201 | { |
| 5202 | unsigned left, total; |
| 5203 | int ret = 0; |
| 5204 | |
| 5205 | total = 0; |
| 5206 | left = ctx->nr_user_files; |
| 5207 | while (left) { |
| 5208 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5209 | |
| 5210 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 5211 | if (ret) |
| 5212 | break; |
| 5213 | left -= this_files; |
| 5214 | total += this_files; |
| 5215 | } |
| 5216 | |
| 5217 | if (!ret) |
| 5218 | return 0; |
| 5219 | |
| 5220 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5221 | struct file *file = io_file_from_index(ctx, total); |
| 5222 | |
| 5223 | if (file) |
| 5224 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5225 | total++; |
| 5226 | } |
| 5227 | |
| 5228 | return ret; |
| 5229 | } |
| 5230 | #else |
| 5231 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5232 | { |
| 5233 | return 0; |
| 5234 | } |
| 5235 | #endif |
| 5236 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5237 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 5238 | unsigned nr_files) |
| 5239 | { |
| 5240 | int i; |
| 5241 | |
| 5242 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5243 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5244 | unsigned this_files; |
| 5245 | |
| 5246 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 5247 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 5248 | GFP_KERNEL); |
| 5249 | if (!table->files) |
| 5250 | break; |
| 5251 | nr_files -= this_files; |
| 5252 | } |
| 5253 | |
| 5254 | if (i == nr_tables) |
| 5255 | return 0; |
| 5256 | |
| 5257 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5258 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5259 | kfree(table->files); |
| 5260 | } |
| 5261 | return 1; |
| 5262 | } |
| 5263 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5264 | 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] | 5265 | { |
| 5266 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5267 | struct sock *sock = ctx->ring_sock->sk; |
| 5268 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 5269 | struct sk_buff *skb; |
| 5270 | int i; |
| 5271 | |
| 5272 | __skb_queue_head_init(&list); |
| 5273 | |
| 5274 | /* |
| 5275 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 5276 | * remove this entry and rearrange the file array. |
| 5277 | */ |
| 5278 | skb = skb_dequeue(head); |
| 5279 | while (skb) { |
| 5280 | struct scm_fp_list *fp; |
| 5281 | |
| 5282 | fp = UNIXCB(skb).fp; |
| 5283 | for (i = 0; i < fp->count; i++) { |
| 5284 | int left; |
| 5285 | |
| 5286 | if (fp->fp[i] != file) |
| 5287 | continue; |
| 5288 | |
| 5289 | unix_notinflight(fp->user, fp->fp[i]); |
| 5290 | left = fp->count - 1 - i; |
| 5291 | if (left) { |
| 5292 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 5293 | left * sizeof(struct file *)); |
| 5294 | } |
| 5295 | fp->count--; |
| 5296 | if (!fp->count) { |
| 5297 | kfree_skb(skb); |
| 5298 | skb = NULL; |
| 5299 | } else { |
| 5300 | __skb_queue_tail(&list, skb); |
| 5301 | } |
| 5302 | fput(file); |
| 5303 | file = NULL; |
| 5304 | break; |
| 5305 | } |
| 5306 | |
| 5307 | if (!file) |
| 5308 | break; |
| 5309 | |
| 5310 | __skb_queue_tail(&list, skb); |
| 5311 | |
| 5312 | skb = skb_dequeue(head); |
| 5313 | } |
| 5314 | |
| 5315 | if (skb_peek(&list)) { |
| 5316 | spin_lock_irq(&head->lock); |
| 5317 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 5318 | __skb_queue_tail(head, skb); |
| 5319 | spin_unlock_irq(&head->lock); |
| 5320 | } |
| 5321 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5322 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5323 | #endif |
| 5324 | } |
| 5325 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5326 | struct io_file_put { |
| 5327 | struct llist_node llist; |
| 5328 | struct file *file; |
| 5329 | struct completion *done; |
| 5330 | }; |
| 5331 | |
| 5332 | static void io_ring_file_ref_switch(struct work_struct *work) |
| 5333 | { |
| 5334 | struct io_file_put *pfile, *tmp; |
| 5335 | struct fixed_file_data *data; |
| 5336 | struct llist_node *node; |
| 5337 | |
| 5338 | data = container_of(work, struct fixed_file_data, ref_work); |
| 5339 | |
| 5340 | while ((node = llist_del_all(&data->put_llist)) != NULL) { |
| 5341 | llist_for_each_entry_safe(pfile, tmp, node, llist) { |
| 5342 | io_ring_file_put(data->ctx, pfile->file); |
| 5343 | if (pfile->done) |
| 5344 | complete(pfile->done); |
| 5345 | else |
| 5346 | kfree(pfile); |
| 5347 | } |
| 5348 | } |
| 5349 | |
| 5350 | percpu_ref_get(&data->refs); |
| 5351 | percpu_ref_switch_to_percpu(&data->refs); |
| 5352 | } |
| 5353 | |
| 5354 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 5355 | { |
| 5356 | struct fixed_file_data *data; |
| 5357 | |
| 5358 | data = container_of(ref, struct fixed_file_data, refs); |
| 5359 | |
| 5360 | /* we can't safely switch from inside this context, punt to wq */ |
| 5361 | queue_work(system_wq, &data->ref_work); |
| 5362 | } |
| 5363 | |
| 5364 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5365 | unsigned nr_args) |
| 5366 | { |
| 5367 | __s32 __user *fds = (__s32 __user *) arg; |
| 5368 | unsigned nr_tables; |
| 5369 | struct file *file; |
| 5370 | int fd, ret = 0; |
| 5371 | unsigned i; |
| 5372 | |
| 5373 | if (ctx->file_data) |
| 5374 | return -EBUSY; |
| 5375 | if (!nr_args) |
| 5376 | return -EINVAL; |
| 5377 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 5378 | return -EMFILE; |
| 5379 | |
| 5380 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 5381 | if (!ctx->file_data) |
| 5382 | return -ENOMEM; |
| 5383 | ctx->file_data->ctx = ctx; |
| 5384 | init_completion(&ctx->file_data->done); |
| 5385 | |
| 5386 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 5387 | ctx->file_data->table = kcalloc(nr_tables, |
| 5388 | sizeof(struct fixed_file_table), |
| 5389 | GFP_KERNEL); |
| 5390 | if (!ctx->file_data->table) { |
| 5391 | kfree(ctx->file_data); |
| 5392 | ctx->file_data = NULL; |
| 5393 | return -ENOMEM; |
| 5394 | } |
| 5395 | |
| 5396 | if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero, |
| 5397 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 5398 | kfree(ctx->file_data->table); |
| 5399 | kfree(ctx->file_data); |
| 5400 | ctx->file_data = NULL; |
| 5401 | return -ENOMEM; |
| 5402 | } |
| 5403 | ctx->file_data->put_llist.first = NULL; |
| 5404 | INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch); |
| 5405 | |
| 5406 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 5407 | percpu_ref_exit(&ctx->file_data->refs); |
| 5408 | kfree(ctx->file_data->table); |
| 5409 | kfree(ctx->file_data); |
| 5410 | ctx->file_data = NULL; |
| 5411 | return -ENOMEM; |
| 5412 | } |
| 5413 | |
| 5414 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 5415 | struct fixed_file_table *table; |
| 5416 | unsigned index; |
| 5417 | |
| 5418 | ret = -EFAULT; |
| 5419 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 5420 | break; |
| 5421 | /* allow sparse sets */ |
| 5422 | if (fd == -1) { |
| 5423 | ret = 0; |
| 5424 | continue; |
| 5425 | } |
| 5426 | |
| 5427 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 5428 | index = i & IORING_FILE_TABLE_MASK; |
| 5429 | file = fget(fd); |
| 5430 | |
| 5431 | ret = -EBADF; |
| 5432 | if (!file) |
| 5433 | break; |
| 5434 | |
| 5435 | /* |
| 5436 | * Don't allow io_uring instances to be registered. If UNIX |
| 5437 | * isn't enabled, then this causes a reference cycle and this |
| 5438 | * instance can never get freed. If UNIX is enabled we'll |
| 5439 | * handle it just fine, but there's still no point in allowing |
| 5440 | * a ring fd as it doesn't support regular read/write anyway. |
| 5441 | */ |
| 5442 | if (file->f_op == &io_uring_fops) { |
| 5443 | fput(file); |
| 5444 | break; |
| 5445 | } |
| 5446 | ret = 0; |
| 5447 | table->files[index] = file; |
| 5448 | } |
| 5449 | |
| 5450 | if (ret) { |
| 5451 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5452 | file = io_file_from_index(ctx, i); |
| 5453 | if (file) |
| 5454 | fput(file); |
| 5455 | } |
| 5456 | for (i = 0; i < nr_tables; i++) |
| 5457 | kfree(ctx->file_data->table[i].files); |
| 5458 | |
| 5459 | kfree(ctx->file_data->table); |
| 5460 | kfree(ctx->file_data); |
| 5461 | ctx->file_data = NULL; |
| 5462 | ctx->nr_user_files = 0; |
| 5463 | return ret; |
| 5464 | } |
| 5465 | |
| 5466 | ret = io_sqe_files_scm(ctx); |
| 5467 | if (ret) |
| 5468 | io_sqe_files_unregister(ctx); |
| 5469 | |
| 5470 | return ret; |
| 5471 | } |
| 5472 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5473 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 5474 | int index) |
| 5475 | { |
| 5476 | #if defined(CONFIG_UNIX) |
| 5477 | struct sock *sock = ctx->ring_sock->sk; |
| 5478 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 5479 | struct sk_buff *skb; |
| 5480 | |
| 5481 | /* |
| 5482 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 5483 | * file set. If there's no room, fall back to allocating a new skb |
| 5484 | * and filling it in. |
| 5485 | */ |
| 5486 | spin_lock_irq(&head->lock); |
| 5487 | skb = skb_peek(head); |
| 5488 | if (skb) { |
| 5489 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 5490 | |
| 5491 | if (fpl->count < SCM_MAX_FD) { |
| 5492 | __skb_unlink(skb, head); |
| 5493 | spin_unlock_irq(&head->lock); |
| 5494 | fpl->fp[fpl->count] = get_file(file); |
| 5495 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 5496 | fpl->count++; |
| 5497 | spin_lock_irq(&head->lock); |
| 5498 | __skb_queue_head(head, skb); |
| 5499 | } else { |
| 5500 | skb = NULL; |
| 5501 | } |
| 5502 | } |
| 5503 | spin_unlock_irq(&head->lock); |
| 5504 | |
| 5505 | if (skb) { |
| 5506 | fput(file); |
| 5507 | return 0; |
| 5508 | } |
| 5509 | |
| 5510 | return __io_sqe_files_scm(ctx, 1, index); |
| 5511 | #else |
| 5512 | return 0; |
| 5513 | #endif |
| 5514 | } |
| 5515 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5516 | static void io_atomic_switch(struct percpu_ref *ref) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5517 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5518 | struct fixed_file_data *data; |
| 5519 | |
| 5520 | data = container_of(ref, struct fixed_file_data, refs); |
| 5521 | clear_bit(FFD_F_ATOMIC, &data->state); |
| 5522 | } |
| 5523 | |
| 5524 | static bool io_queue_file_removal(struct fixed_file_data *data, |
| 5525 | struct file *file) |
| 5526 | { |
| 5527 | struct io_file_put *pfile, pfile_stack; |
| 5528 | DECLARE_COMPLETION_ONSTACK(done); |
| 5529 | |
| 5530 | /* |
| 5531 | * If we fail allocating the struct we need for doing async reomval |
| 5532 | * of this file, just punt to sync and wait for it. |
| 5533 | */ |
| 5534 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
| 5535 | if (!pfile) { |
| 5536 | pfile = &pfile_stack; |
| 5537 | pfile->done = &done; |
| 5538 | } |
| 5539 | |
| 5540 | pfile->file = file; |
| 5541 | llist_add(&pfile->llist, &data->put_llist); |
| 5542 | |
| 5543 | if (pfile == &pfile_stack) { |
| 5544 | if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5545 | percpu_ref_put(&data->refs); |
| 5546 | percpu_ref_switch_to_atomic(&data->refs, |
| 5547 | io_atomic_switch); |
| 5548 | } |
| 5549 | wait_for_completion(&done); |
| 5550 | flush_work(&data->ref_work); |
| 5551 | return false; |
| 5552 | } |
| 5553 | |
| 5554 | return true; |
| 5555 | } |
| 5556 | |
| 5557 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 5558 | struct io_uring_files_update *up, |
| 5559 | unsigned nr_args) |
| 5560 | { |
| 5561 | struct fixed_file_data *data = ctx->file_data; |
| 5562 | bool ref_switch = false; |
| 5563 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5564 | __s32 __user *fds; |
| 5565 | int fd, i, err; |
| 5566 | __u32 done; |
| 5567 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5568 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5569 | return -EOVERFLOW; |
| 5570 | if (done > ctx->nr_user_files) |
| 5571 | return -EINVAL; |
| 5572 | |
| 5573 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5574 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5575 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5576 | struct fixed_file_table *table; |
| 5577 | unsigned index; |
| 5578 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5579 | err = 0; |
| 5580 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 5581 | err = -EFAULT; |
| 5582 | break; |
| 5583 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5584 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 5585 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5586 | index = i & IORING_FILE_TABLE_MASK; |
| 5587 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5588 | file = io_file_from_index(ctx, index); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5589 | table->files[index] = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5590 | if (io_queue_file_removal(data, file)) |
| 5591 | ref_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5592 | } |
| 5593 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5594 | file = fget(fd); |
| 5595 | if (!file) { |
| 5596 | err = -EBADF; |
| 5597 | break; |
| 5598 | } |
| 5599 | /* |
| 5600 | * Don't allow io_uring instances to be registered. If |
| 5601 | * UNIX isn't enabled, then this causes a reference |
| 5602 | * cycle and this instance can never get freed. If UNIX |
| 5603 | * is enabled we'll handle it just fine, but there's |
| 5604 | * still no point in allowing a ring fd as it doesn't |
| 5605 | * support regular read/write anyway. |
| 5606 | */ |
| 5607 | if (file->f_op == &io_uring_fops) { |
| 5608 | fput(file); |
| 5609 | err = -EBADF; |
| 5610 | break; |
| 5611 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5612 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5613 | err = io_sqe_file_register(ctx, file, i); |
| 5614 | if (err) |
| 5615 | break; |
| 5616 | } |
| 5617 | nr_args--; |
| 5618 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5619 | up->offset++; |
| 5620 | } |
| 5621 | |
| 5622 | if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5623 | percpu_ref_put(&data->refs); |
| 5624 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5625 | } |
| 5626 | |
| 5627 | return done ? done : err; |
| 5628 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5629 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 5630 | unsigned nr_args) |
| 5631 | { |
| 5632 | struct io_uring_files_update up; |
| 5633 | |
| 5634 | if (!ctx->file_data) |
| 5635 | return -ENXIO; |
| 5636 | if (!nr_args) |
| 5637 | return -EINVAL; |
| 5638 | if (copy_from_user(&up, arg, sizeof(up))) |
| 5639 | return -EFAULT; |
| 5640 | if (up.resv) |
| 5641 | return -EINVAL; |
| 5642 | |
| 5643 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 5644 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5645 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 5646 | static void io_put_work(struct io_wq_work *work) |
| 5647 | { |
| 5648 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5649 | |
| 5650 | io_put_req(req); |
| 5651 | } |
| 5652 | |
| 5653 | static void io_get_work(struct io_wq_work *work) |
| 5654 | { |
| 5655 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5656 | |
| 5657 | refcount_inc(&req->refs); |
| 5658 | } |
| 5659 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5660 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 5661 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5662 | { |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5663 | struct io_wq_data data; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5664 | unsigned concurrency; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5665 | int ret; |
| 5666 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5667 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5668 | mmgrab(current->mm); |
| 5669 | ctx->sqo_mm = current->mm; |
| 5670 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5671 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 5672 | ret = -EPERM; |
| 5673 | if (!capable(CAP_SYS_ADMIN)) |
| 5674 | goto err; |
| 5675 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5676 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 5677 | if (!ctx->sq_thread_idle) |
| 5678 | ctx->sq_thread_idle = HZ; |
| 5679 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5680 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5681 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5682 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5683 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5684 | if (cpu >= nr_cpu_ids) |
| 5685 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 5686 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5687 | goto err; |
| 5688 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5689 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 5690 | ctx, cpu, |
| 5691 | "io_uring-sq"); |
| 5692 | } else { |
| 5693 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 5694 | "io_uring-sq"); |
| 5695 | } |
| 5696 | if (IS_ERR(ctx->sqo_thread)) { |
| 5697 | ret = PTR_ERR(ctx->sqo_thread); |
| 5698 | ctx->sqo_thread = NULL; |
| 5699 | goto err; |
| 5700 | } |
| 5701 | wake_up_process(ctx->sqo_thread); |
| 5702 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 5703 | /* Can't have SQ_AFF without SQPOLL */ |
| 5704 | ret = -EINVAL; |
| 5705 | goto err; |
| 5706 | } |
| 5707 | |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5708 | data.mm = ctx->sqo_mm; |
| 5709 | data.user = ctx->user; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5710 | data.creds = ctx->creds; |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5711 | data.get_work = io_get_work; |
| 5712 | data.put_work = io_put_work; |
| 5713 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5714 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 5715 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5716 | ctx->io_wq = io_wq_create(concurrency, &data); |
Jens Axboe | 975c99a5 | 2019-10-30 08:42:56 -0600 | [diff] [blame] | 5717 | if (IS_ERR(ctx->io_wq)) { |
| 5718 | ret = PTR_ERR(ctx->io_wq); |
| 5719 | ctx->io_wq = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5720 | goto err; |
| 5721 | } |
| 5722 | |
| 5723 | return 0; |
| 5724 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 5725 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5726 | mmdrop(ctx->sqo_mm); |
| 5727 | ctx->sqo_mm = NULL; |
| 5728 | return ret; |
| 5729 | } |
| 5730 | |
| 5731 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 5732 | { |
| 5733 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 5734 | } |
| 5735 | |
| 5736 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 5737 | { |
| 5738 | unsigned long page_limit, cur_pages, new_pages; |
| 5739 | |
| 5740 | /* Don't allow more pages than we can safely lock */ |
| 5741 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 5742 | |
| 5743 | do { |
| 5744 | cur_pages = atomic_long_read(&user->locked_vm); |
| 5745 | new_pages = cur_pages + nr_pages; |
| 5746 | if (new_pages > page_limit) |
| 5747 | return -ENOMEM; |
| 5748 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 5749 | new_pages) != cur_pages); |
| 5750 | |
| 5751 | return 0; |
| 5752 | } |
| 5753 | |
| 5754 | static void io_mem_free(void *ptr) |
| 5755 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 5756 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5757 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 5758 | if (!ptr) |
| 5759 | return; |
| 5760 | |
| 5761 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5762 | if (put_page_testzero(page)) |
| 5763 | free_compound_page(page); |
| 5764 | } |
| 5765 | |
| 5766 | static void *io_mem_alloc(size_t size) |
| 5767 | { |
| 5768 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 5769 | __GFP_NORETRY; |
| 5770 | |
| 5771 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 5772 | } |
| 5773 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5774 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 5775 | size_t *sq_offset) |
| 5776 | { |
| 5777 | struct io_rings *rings; |
| 5778 | size_t off, sq_array_size; |
| 5779 | |
| 5780 | off = struct_size(rings, cqes, cq_entries); |
| 5781 | if (off == SIZE_MAX) |
| 5782 | return SIZE_MAX; |
| 5783 | |
| 5784 | #ifdef CONFIG_SMP |
| 5785 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 5786 | if (off == 0) |
| 5787 | return SIZE_MAX; |
| 5788 | #endif |
| 5789 | |
| 5790 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 5791 | if (sq_array_size == SIZE_MAX) |
| 5792 | return SIZE_MAX; |
| 5793 | |
| 5794 | if (check_add_overflow(off, sq_array_size, &off)) |
| 5795 | return SIZE_MAX; |
| 5796 | |
| 5797 | if (sq_offset) |
| 5798 | *sq_offset = off; |
| 5799 | |
| 5800 | return off; |
| 5801 | } |
| 5802 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5803 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 5804 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5805 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5806 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5807 | pages = (size_t)1 << get_order( |
| 5808 | rings_size(sq_entries, cq_entries, NULL)); |
| 5809 | pages += (size_t)1 << get_order( |
| 5810 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5811 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5812 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5813 | } |
| 5814 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5815 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 5816 | { |
| 5817 | int i, j; |
| 5818 | |
| 5819 | if (!ctx->user_bufs) |
| 5820 | return -ENXIO; |
| 5821 | |
| 5822 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 5823 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 5824 | |
| 5825 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 5826 | put_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5827 | |
| 5828 | if (ctx->account_mem) |
| 5829 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5830 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5831 | imu->nr_bvecs = 0; |
| 5832 | } |
| 5833 | |
| 5834 | kfree(ctx->user_bufs); |
| 5835 | ctx->user_bufs = NULL; |
| 5836 | ctx->nr_user_bufs = 0; |
| 5837 | return 0; |
| 5838 | } |
| 5839 | |
| 5840 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 5841 | void __user *arg, unsigned index) |
| 5842 | { |
| 5843 | struct iovec __user *src; |
| 5844 | |
| 5845 | #ifdef CONFIG_COMPAT |
| 5846 | if (ctx->compat) { |
| 5847 | struct compat_iovec __user *ciovs; |
| 5848 | struct compat_iovec ciov; |
| 5849 | |
| 5850 | ciovs = (struct compat_iovec __user *) arg; |
| 5851 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 5852 | return -EFAULT; |
| 5853 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 5854 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5855 | dst->iov_len = ciov.iov_len; |
| 5856 | return 0; |
| 5857 | } |
| 5858 | #endif |
| 5859 | src = (struct iovec __user *) arg; |
| 5860 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 5861 | return -EFAULT; |
| 5862 | return 0; |
| 5863 | } |
| 5864 | |
| 5865 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5866 | unsigned nr_args) |
| 5867 | { |
| 5868 | struct vm_area_struct **vmas = NULL; |
| 5869 | struct page **pages = NULL; |
| 5870 | int i, j, got_pages = 0; |
| 5871 | int ret = -EINVAL; |
| 5872 | |
| 5873 | if (ctx->user_bufs) |
| 5874 | return -EBUSY; |
| 5875 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 5876 | return -EINVAL; |
| 5877 | |
| 5878 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 5879 | GFP_KERNEL); |
| 5880 | if (!ctx->user_bufs) |
| 5881 | return -ENOMEM; |
| 5882 | |
| 5883 | for (i = 0; i < nr_args; i++) { |
| 5884 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 5885 | unsigned long off, start, end, ubuf; |
| 5886 | int pret, nr_pages; |
| 5887 | struct iovec iov; |
| 5888 | size_t size; |
| 5889 | |
| 5890 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 5891 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 5892 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5893 | |
| 5894 | /* |
| 5895 | * Don't impose further limits on the size and buffer |
| 5896 | * constraints here, we'll -EINVAL later when IO is |
| 5897 | * submitted if they are wrong. |
| 5898 | */ |
| 5899 | ret = -EFAULT; |
| 5900 | if (!iov.iov_base || !iov.iov_len) |
| 5901 | goto err; |
| 5902 | |
| 5903 | /* arbitrary limit, but we need something */ |
| 5904 | if (iov.iov_len > SZ_1G) |
| 5905 | goto err; |
| 5906 | |
| 5907 | ubuf = (unsigned long) iov.iov_base; |
| 5908 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 5909 | start = ubuf >> PAGE_SHIFT; |
| 5910 | nr_pages = end - start; |
| 5911 | |
| 5912 | if (ctx->account_mem) { |
| 5913 | ret = io_account_mem(ctx->user, nr_pages); |
| 5914 | if (ret) |
| 5915 | goto err; |
| 5916 | } |
| 5917 | |
| 5918 | ret = 0; |
| 5919 | if (!pages || nr_pages > got_pages) { |
| 5920 | kfree(vmas); |
| 5921 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5922 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5923 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5924 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5925 | sizeof(struct vm_area_struct *), |
| 5926 | GFP_KERNEL); |
| 5927 | if (!pages || !vmas) { |
| 5928 | ret = -ENOMEM; |
| 5929 | if (ctx->account_mem) |
| 5930 | io_unaccount_mem(ctx->user, nr_pages); |
| 5931 | goto err; |
| 5932 | } |
| 5933 | got_pages = nr_pages; |
| 5934 | } |
| 5935 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5936 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5937 | GFP_KERNEL); |
| 5938 | ret = -ENOMEM; |
| 5939 | if (!imu->bvec) { |
| 5940 | if (ctx->account_mem) |
| 5941 | io_unaccount_mem(ctx->user, nr_pages); |
| 5942 | goto err; |
| 5943 | } |
| 5944 | |
| 5945 | ret = 0; |
| 5946 | down_read(¤t->mm->mmap_sem); |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 5947 | pret = get_user_pages(ubuf, nr_pages, |
| 5948 | FOLL_WRITE | FOLL_LONGTERM, |
| 5949 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5950 | if (pret == nr_pages) { |
| 5951 | /* don't support file backed memory */ |
| 5952 | for (j = 0; j < nr_pages; j++) { |
| 5953 | struct vm_area_struct *vma = vmas[j]; |
| 5954 | |
| 5955 | if (vma->vm_file && |
| 5956 | !is_file_hugepages(vma->vm_file)) { |
| 5957 | ret = -EOPNOTSUPP; |
| 5958 | break; |
| 5959 | } |
| 5960 | } |
| 5961 | } else { |
| 5962 | ret = pret < 0 ? pret : -EFAULT; |
| 5963 | } |
| 5964 | up_read(¤t->mm->mmap_sem); |
| 5965 | if (ret) { |
| 5966 | /* |
| 5967 | * if we did partial map, or found file backed vmas, |
| 5968 | * release any pages we did get |
| 5969 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 5970 | if (pret > 0) |
| 5971 | put_user_pages(pages, pret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5972 | if (ctx->account_mem) |
| 5973 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5974 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5975 | goto err; |
| 5976 | } |
| 5977 | |
| 5978 | off = ubuf & ~PAGE_MASK; |
| 5979 | size = iov.iov_len; |
| 5980 | for (j = 0; j < nr_pages; j++) { |
| 5981 | size_t vec_len; |
| 5982 | |
| 5983 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 5984 | imu->bvec[j].bv_page = pages[j]; |
| 5985 | imu->bvec[j].bv_len = vec_len; |
| 5986 | imu->bvec[j].bv_offset = off; |
| 5987 | off = 0; |
| 5988 | size -= vec_len; |
| 5989 | } |
| 5990 | /* store original address for later verification */ |
| 5991 | imu->ubuf = ubuf; |
| 5992 | imu->len = iov.iov_len; |
| 5993 | imu->nr_bvecs = nr_pages; |
| 5994 | |
| 5995 | ctx->nr_user_bufs++; |
| 5996 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5997 | kvfree(pages); |
| 5998 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5999 | return 0; |
| 6000 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6001 | kvfree(pages); |
| 6002 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6003 | io_sqe_buffer_unregister(ctx); |
| 6004 | return ret; |
| 6005 | } |
| 6006 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6007 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 6008 | { |
| 6009 | __s32 __user *fds = arg; |
| 6010 | int fd; |
| 6011 | |
| 6012 | if (ctx->cq_ev_fd) |
| 6013 | return -EBUSY; |
| 6014 | |
| 6015 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 6016 | return -EFAULT; |
| 6017 | |
| 6018 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 6019 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 6020 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 6021 | ctx->cq_ev_fd = NULL; |
| 6022 | return ret; |
| 6023 | } |
| 6024 | |
| 6025 | return 0; |
| 6026 | } |
| 6027 | |
| 6028 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 6029 | { |
| 6030 | if (ctx->cq_ev_fd) { |
| 6031 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 6032 | ctx->cq_ev_fd = NULL; |
| 6033 | return 0; |
| 6034 | } |
| 6035 | |
| 6036 | return -ENXIO; |
| 6037 | } |
| 6038 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6039 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 6040 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6041 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6042 | if (ctx->sqo_mm) |
| 6043 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6044 | |
| 6045 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6046 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6047 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6048 | io_eventfd_unregister(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6049 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6050 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6051 | if (ctx->ring_sock) { |
| 6052 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6053 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6054 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6055 | #endif |
| 6056 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6057 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6058 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6059 | |
| 6060 | percpu_ref_exit(&ctx->refs); |
| 6061 | if (ctx->account_mem) |
| 6062 | io_unaccount_mem(ctx->user, |
| 6063 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 6064 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6065 | put_cred(ctx->creds); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6066 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 6067 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 6068 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6069 | kfree(ctx); |
| 6070 | } |
| 6071 | |
| 6072 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 6073 | { |
| 6074 | struct io_ring_ctx *ctx = file->private_data; |
| 6075 | __poll_t mask = 0; |
| 6076 | |
| 6077 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 6078 | /* |
| 6079 | * synchronizes with barrier from wq_has_sleeper call in |
| 6080 | * io_commit_cqring |
| 6081 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6082 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6083 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 6084 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6085 | mask |= EPOLLOUT | EPOLLWRNORM; |
yangerkun | daa5de5 | 2019-09-24 20:53:34 +0800 | [diff] [blame] | 6086 | if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6087 | mask |= EPOLLIN | EPOLLRDNORM; |
| 6088 | |
| 6089 | return mask; |
| 6090 | } |
| 6091 | |
| 6092 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 6093 | { |
| 6094 | struct io_ring_ctx *ctx = file->private_data; |
| 6095 | |
| 6096 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 6097 | } |
| 6098 | |
| 6099 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 6100 | { |
| 6101 | mutex_lock(&ctx->uring_lock); |
| 6102 | percpu_ref_kill(&ctx->refs); |
| 6103 | mutex_unlock(&ctx->uring_lock); |
| 6104 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6105 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6106 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6107 | |
| 6108 | if (ctx->io_wq) |
| 6109 | io_wq_cancel_all(ctx->io_wq); |
| 6110 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6111 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 6112 | /* if we failed setting up the ctx, we might not have any rings */ |
| 6113 | if (ctx->rings) |
| 6114 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6115 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6116 | io_ring_ctx_free(ctx); |
| 6117 | } |
| 6118 | |
| 6119 | static int io_uring_release(struct inode *inode, struct file *file) |
| 6120 | { |
| 6121 | struct io_ring_ctx *ctx = file->private_data; |
| 6122 | |
| 6123 | file->private_data = NULL; |
| 6124 | io_ring_ctx_wait_and_kill(ctx); |
| 6125 | return 0; |
| 6126 | } |
| 6127 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6128 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 6129 | struct files_struct *files) |
| 6130 | { |
| 6131 | struct io_kiocb *req; |
| 6132 | DEFINE_WAIT(wait); |
| 6133 | |
| 6134 | while (!list_empty_careful(&ctx->inflight_list)) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6135 | struct io_kiocb *cancel_req = NULL; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6136 | |
| 6137 | spin_lock_irq(&ctx->inflight_lock); |
| 6138 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6139 | if (req->work.files != files) |
| 6140 | continue; |
| 6141 | /* req is being completed, ignore */ |
| 6142 | if (!refcount_inc_not_zero(&req->refs)) |
| 6143 | continue; |
| 6144 | cancel_req = req; |
| 6145 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6146 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6147 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6148 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6149 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6150 | spin_unlock_irq(&ctx->inflight_lock); |
| 6151 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6152 | /* We need to keep going until we don't find a matching req */ |
| 6153 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6154 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 6155 | |
| 6156 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 6157 | io_put_req(cancel_req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6158 | schedule(); |
| 6159 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6160 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6161 | } |
| 6162 | |
| 6163 | static int io_uring_flush(struct file *file, void *data) |
| 6164 | { |
| 6165 | struct io_ring_ctx *ctx = file->private_data; |
| 6166 | |
| 6167 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6168 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
| 6169 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6170 | io_wq_cancel_all(ctx->io_wq); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6171 | } |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6172 | return 0; |
| 6173 | } |
| 6174 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6175 | static void *io_uring_validate_mmap_request(struct file *file, |
| 6176 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6177 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6178 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6179 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6180 | struct page *page; |
| 6181 | void *ptr; |
| 6182 | |
| 6183 | switch (offset) { |
| 6184 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6185 | case IORING_OFF_CQ_RING: |
| 6186 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6187 | break; |
| 6188 | case IORING_OFF_SQES: |
| 6189 | ptr = ctx->sq_sqes; |
| 6190 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6191 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6192 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6193 | } |
| 6194 | |
| 6195 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 6196 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6197 | return ERR_PTR(-EINVAL); |
| 6198 | |
| 6199 | return ptr; |
| 6200 | } |
| 6201 | |
| 6202 | #ifdef CONFIG_MMU |
| 6203 | |
| 6204 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6205 | { |
| 6206 | size_t sz = vma->vm_end - vma->vm_start; |
| 6207 | unsigned long pfn; |
| 6208 | void *ptr; |
| 6209 | |
| 6210 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 6211 | if (IS_ERR(ptr)) |
| 6212 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6213 | |
| 6214 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 6215 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 6216 | } |
| 6217 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6218 | #else /* !CONFIG_MMU */ |
| 6219 | |
| 6220 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6221 | { |
| 6222 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 6223 | } |
| 6224 | |
| 6225 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 6226 | { |
| 6227 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 6228 | } |
| 6229 | |
| 6230 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 6231 | unsigned long addr, unsigned long len, |
| 6232 | unsigned long pgoff, unsigned long flags) |
| 6233 | { |
| 6234 | void *ptr; |
| 6235 | |
| 6236 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 6237 | if (IS_ERR(ptr)) |
| 6238 | return PTR_ERR(ptr); |
| 6239 | |
| 6240 | return (unsigned long) ptr; |
| 6241 | } |
| 6242 | |
| 6243 | #endif /* !CONFIG_MMU */ |
| 6244 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6245 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 6246 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 6247 | size_t, sigsz) |
| 6248 | { |
| 6249 | struct io_ring_ctx *ctx; |
| 6250 | long ret = -EBADF; |
| 6251 | int submitted = 0; |
| 6252 | struct fd f; |
| 6253 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6254 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6255 | return -EINVAL; |
| 6256 | |
| 6257 | f = fdget(fd); |
| 6258 | if (!f.file) |
| 6259 | return -EBADF; |
| 6260 | |
| 6261 | ret = -EOPNOTSUPP; |
| 6262 | if (f.file->f_op != &io_uring_fops) |
| 6263 | goto out_fput; |
| 6264 | |
| 6265 | ret = -ENXIO; |
| 6266 | ctx = f.file->private_data; |
| 6267 | if (!percpu_ref_tryget(&ctx->refs)) |
| 6268 | goto out_fput; |
| 6269 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6270 | /* |
| 6271 | * For SQ polling, the thread will do all submissions and completions. |
| 6272 | * Just return the requested submit count, and wake the thread if |
| 6273 | * we were asked to. |
| 6274 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6275 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6276 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6277 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 6278 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6279 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 6280 | wake_up(&ctx->sqo_wait); |
| 6281 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6282 | } else if (to_submit) { |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6283 | struct mm_struct *cur_mm; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6284 | |
Jens Axboe | 44d2827 | 2020-01-16 19:00:24 -0700 | [diff] [blame] | 6285 | if (current->mm != ctx->sqo_mm || |
| 6286 | current_cred() != ctx->creds) { |
| 6287 | ret = -EPERM; |
| 6288 | goto out; |
| 6289 | } |
| 6290 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6291 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6292 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
| 6293 | cur_mm = ctx->sqo_mm; |
| 6294 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, |
| 6295 | &cur_mm, false); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6296 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6297 | |
| 6298 | if (submitted != to_submit) |
| 6299 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6300 | } |
| 6301 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6302 | unsigned nr_events = 0; |
| 6303 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6304 | min_complete = min(min_complete, ctx->cq_entries); |
| 6305 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6306 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6307 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6308 | } else { |
| 6309 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 6310 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6311 | } |
| 6312 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6313 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 6314 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6315 | out_fput: |
| 6316 | fdput(f); |
| 6317 | return submitted ? submitted : ret; |
| 6318 | } |
| 6319 | |
| 6320 | static const struct file_operations io_uring_fops = { |
| 6321 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6322 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6323 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6324 | #ifndef CONFIG_MMU |
| 6325 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 6326 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 6327 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6328 | .poll = io_uring_poll, |
| 6329 | .fasync = io_uring_fasync, |
| 6330 | }; |
| 6331 | |
| 6332 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 6333 | struct io_uring_params *p) |
| 6334 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6335 | struct io_rings *rings; |
| 6336 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6337 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6338 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 6339 | if (size == SIZE_MAX) |
| 6340 | return -EOVERFLOW; |
| 6341 | |
| 6342 | rings = io_mem_alloc(size); |
| 6343 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6344 | return -ENOMEM; |
| 6345 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6346 | ctx->rings = rings; |
| 6347 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 6348 | rings->sq_ring_mask = p->sq_entries - 1; |
| 6349 | rings->cq_ring_mask = p->cq_entries - 1; |
| 6350 | rings->sq_ring_entries = p->sq_entries; |
| 6351 | rings->cq_ring_entries = p->cq_entries; |
| 6352 | ctx->sq_mask = rings->sq_ring_mask; |
| 6353 | ctx->cq_mask = rings->cq_ring_mask; |
| 6354 | ctx->sq_entries = rings->sq_ring_entries; |
| 6355 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6356 | |
| 6357 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6358 | if (size == SIZE_MAX) { |
| 6359 | io_mem_free(ctx->rings); |
| 6360 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6361 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6362 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6363 | |
| 6364 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6365 | if (!ctx->sq_sqes) { |
| 6366 | io_mem_free(ctx->rings); |
| 6367 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6368 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6369 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6370 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6371 | return 0; |
| 6372 | } |
| 6373 | |
| 6374 | /* |
| 6375 | * Allocate an anonymous fd, this is what constitutes the application |
| 6376 | * visible backing of an io_uring instance. The application mmaps this |
| 6377 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 6378 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 6379 | */ |
| 6380 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 6381 | { |
| 6382 | struct file *file; |
| 6383 | int ret; |
| 6384 | |
| 6385 | #if defined(CONFIG_UNIX) |
| 6386 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 6387 | &ctx->ring_sock); |
| 6388 | if (ret) |
| 6389 | return ret; |
| 6390 | #endif |
| 6391 | |
| 6392 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 6393 | if (ret < 0) |
| 6394 | goto err; |
| 6395 | |
| 6396 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 6397 | O_RDWR | O_CLOEXEC); |
| 6398 | if (IS_ERR(file)) { |
| 6399 | put_unused_fd(ret); |
| 6400 | ret = PTR_ERR(file); |
| 6401 | goto err; |
| 6402 | } |
| 6403 | |
| 6404 | #if defined(CONFIG_UNIX) |
| 6405 | ctx->ring_sock->file = file; |
| 6406 | #endif |
| 6407 | fd_install(ret, file); |
| 6408 | return ret; |
| 6409 | err: |
| 6410 | #if defined(CONFIG_UNIX) |
| 6411 | sock_release(ctx->ring_sock); |
| 6412 | ctx->ring_sock = NULL; |
| 6413 | #endif |
| 6414 | return ret; |
| 6415 | } |
| 6416 | |
| 6417 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 6418 | { |
| 6419 | struct user_struct *user = NULL; |
| 6420 | struct io_ring_ctx *ctx; |
| 6421 | bool account_mem; |
| 6422 | int ret; |
| 6423 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6424 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6425 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6426 | if (entries > IORING_MAX_ENTRIES) { |
| 6427 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6428 | return -EINVAL; |
| 6429 | entries = IORING_MAX_ENTRIES; |
| 6430 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6431 | |
| 6432 | /* |
| 6433 | * Use twice as many entries for the CQ ring. It's possible for the |
| 6434 | * application to drive a higher depth than the size of the SQ ring, |
| 6435 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6436 | * some flexibility in overcommitting a bit. If the application has |
| 6437 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 6438 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6439 | */ |
| 6440 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6441 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 6442 | /* |
| 6443 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 6444 | * to a power-of-two, if it isn't already. We do NOT impose |
| 6445 | * any cq vs sq ring sizing. |
| 6446 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6447 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6448 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6449 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 6450 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6451 | return -EINVAL; |
| 6452 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 6453 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6454 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 6455 | } else { |
| 6456 | p->cq_entries = 2 * p->sq_entries; |
| 6457 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6458 | |
| 6459 | user = get_uid(current_user()); |
| 6460 | account_mem = !capable(CAP_IPC_LOCK); |
| 6461 | |
| 6462 | if (account_mem) { |
| 6463 | ret = io_account_mem(user, |
| 6464 | ring_pages(p->sq_entries, p->cq_entries)); |
| 6465 | if (ret) { |
| 6466 | free_uid(user); |
| 6467 | return ret; |
| 6468 | } |
| 6469 | } |
| 6470 | |
| 6471 | ctx = io_ring_ctx_alloc(p); |
| 6472 | if (!ctx) { |
| 6473 | if (account_mem) |
| 6474 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 6475 | p->cq_entries)); |
| 6476 | free_uid(user); |
| 6477 | return -ENOMEM; |
| 6478 | } |
| 6479 | ctx->compat = in_compat_syscall(); |
| 6480 | ctx->account_mem = account_mem; |
| 6481 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 6482 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6483 | |
| 6484 | ret = io_allocate_scq_urings(ctx, p); |
| 6485 | if (ret) |
| 6486 | goto err; |
| 6487 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6488 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6489 | if (ret) |
| 6490 | goto err; |
| 6491 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6492 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6493 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 6494 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 6495 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 6496 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 6497 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 6498 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 6499 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6500 | |
| 6501 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6502 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 6503 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 6504 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 6505 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 6506 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 6507 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 6508 | |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 6509 | /* |
| 6510 | * Install ring fd as the very last thing, so we don't risk someone |
| 6511 | * having closed it before we finish setup |
| 6512 | */ |
| 6513 | ret = io_uring_get_fd(ctx); |
| 6514 | if (ret < 0) |
| 6515 | goto err; |
| 6516 | |
Jens Axboe | da8c969 | 2019-12-02 18:51:26 -0700 | [diff] [blame] | 6517 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 6518 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6519 | 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] | 6520 | return ret; |
| 6521 | err: |
| 6522 | io_ring_ctx_wait_and_kill(ctx); |
| 6523 | return ret; |
| 6524 | } |
| 6525 | |
| 6526 | /* |
| 6527 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 6528 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 6529 | * params structure passed in. |
| 6530 | */ |
| 6531 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 6532 | { |
| 6533 | struct io_uring_params p; |
| 6534 | long ret; |
| 6535 | int i; |
| 6536 | |
| 6537 | if (copy_from_user(&p, params, sizeof(p))) |
| 6538 | return -EFAULT; |
| 6539 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 6540 | if (p.resv[i]) |
| 6541 | return -EINVAL; |
| 6542 | } |
| 6543 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6544 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6545 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
| 6546 | IORING_SETUP_CLAMP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6547 | return -EINVAL; |
| 6548 | |
| 6549 | ret = io_uring_create(entries, &p); |
| 6550 | if (ret < 0) |
| 6551 | return ret; |
| 6552 | |
| 6553 | if (copy_to_user(params, &p, sizeof(p))) |
| 6554 | return -EFAULT; |
| 6555 | |
| 6556 | return ret; |
| 6557 | } |
| 6558 | |
| 6559 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 6560 | struct io_uring_params __user *, params) |
| 6561 | { |
| 6562 | return io_uring_setup(entries, params); |
| 6563 | } |
| 6564 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6565 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 6566 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 6567 | __releases(ctx->uring_lock) |
| 6568 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6569 | { |
| 6570 | int ret; |
| 6571 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 6572 | /* |
| 6573 | * We're inside the ring mutex, if the ref is already dying, then |
| 6574 | * someone else killed the ctx or is already going through |
| 6575 | * io_uring_register(). |
| 6576 | */ |
| 6577 | if (percpu_ref_is_dying(&ctx->refs)) |
| 6578 | return -ENXIO; |
| 6579 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6580 | if (opcode != IORING_UNREGISTER_FILES && |
| 6581 | opcode != IORING_REGISTER_FILES_UPDATE) { |
| 6582 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 6583 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6584 | /* |
| 6585 | * Drop uring mutex before waiting for references to exit. If |
| 6586 | * another thread is currently inside io_uring_enter() it might |
| 6587 | * need to grab the uring_lock to make progress. If we hold it |
| 6588 | * here across the drain wait, then we can deadlock. It's safe |
| 6589 | * to drop the mutex here, since no new references will come in |
| 6590 | * after we've killed the percpu ref. |
| 6591 | */ |
| 6592 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6593 | ret = wait_for_completion_interruptible(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6594 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6595 | if (ret) { |
| 6596 | percpu_ref_resurrect(&ctx->refs); |
| 6597 | ret = -EINTR; |
| 6598 | goto out; |
| 6599 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6600 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6601 | |
| 6602 | switch (opcode) { |
| 6603 | case IORING_REGISTER_BUFFERS: |
| 6604 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 6605 | break; |
| 6606 | case IORING_UNREGISTER_BUFFERS: |
| 6607 | ret = -EINVAL; |
| 6608 | if (arg || nr_args) |
| 6609 | break; |
| 6610 | ret = io_sqe_buffer_unregister(ctx); |
| 6611 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6612 | case IORING_REGISTER_FILES: |
| 6613 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 6614 | break; |
| 6615 | case IORING_UNREGISTER_FILES: |
| 6616 | ret = -EINVAL; |
| 6617 | if (arg || nr_args) |
| 6618 | break; |
| 6619 | ret = io_sqe_files_unregister(ctx); |
| 6620 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6621 | case IORING_REGISTER_FILES_UPDATE: |
| 6622 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 6623 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6624 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 6625 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6626 | ret = -EINVAL; |
| 6627 | if (nr_args != 1) |
| 6628 | break; |
| 6629 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 6630 | if (ret) |
| 6631 | break; |
| 6632 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 6633 | ctx->eventfd_async = 1; |
| 6634 | else |
| 6635 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6636 | break; |
| 6637 | case IORING_UNREGISTER_EVENTFD: |
| 6638 | ret = -EINVAL; |
| 6639 | if (arg || nr_args) |
| 6640 | break; |
| 6641 | ret = io_eventfd_unregister(ctx); |
| 6642 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6643 | default: |
| 6644 | ret = -EINVAL; |
| 6645 | break; |
| 6646 | } |
| 6647 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6648 | |
| 6649 | if (opcode != IORING_UNREGISTER_FILES && |
| 6650 | opcode != IORING_REGISTER_FILES_UPDATE) { |
| 6651 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6652 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6653 | out: |
| 6654 | reinit_completion(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6655 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6656 | return ret; |
| 6657 | } |
| 6658 | |
| 6659 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 6660 | void __user *, arg, unsigned int, nr_args) |
| 6661 | { |
| 6662 | struct io_ring_ctx *ctx; |
| 6663 | long ret = -EBADF; |
| 6664 | struct fd f; |
| 6665 | |
| 6666 | f = fdget(fd); |
| 6667 | if (!f.file) |
| 6668 | return -EBADF; |
| 6669 | |
| 6670 | ret = -EOPNOTSUPP; |
| 6671 | if (f.file->f_op != &io_uring_fops) |
| 6672 | goto out_fput; |
| 6673 | |
| 6674 | ctx = f.file->private_data; |
| 6675 | |
| 6676 | mutex_lock(&ctx->uring_lock); |
| 6677 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 6678 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6679 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 6680 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6681 | out_fput: |
| 6682 | fdput(f); |
| 6683 | return ret; |
| 6684 | } |
| 6685 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6686 | static int __init io_uring_init(void) |
| 6687 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6688 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6689 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 6690 | return 0; |
| 6691 | }; |
| 6692 | __initcall(io_uring_init); |