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 | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 710 | }; |
| 711 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 712 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 713 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 714 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 715 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 716 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 717 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 718 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 719 | struct io_uring_files_update *ip, |
| 720 | unsigned nr_args); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 721 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 722 | static struct kmem_cache *req_cachep; |
| 723 | |
| 724 | static const struct file_operations io_uring_fops; |
| 725 | |
| 726 | struct sock *io_uring_get_socket(struct file *file) |
| 727 | { |
| 728 | #if defined(CONFIG_UNIX) |
| 729 | if (file->f_op == &io_uring_fops) { |
| 730 | struct io_ring_ctx *ctx = file->private_data; |
| 731 | |
| 732 | return ctx->ring_sock->sk; |
| 733 | } |
| 734 | #endif |
| 735 | return NULL; |
| 736 | } |
| 737 | EXPORT_SYMBOL(io_uring_get_socket); |
| 738 | |
| 739 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 740 | { |
| 741 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 742 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 743 | complete(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 747 | { |
| 748 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 749 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 750 | |
| 751 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 752 | if (!ctx) |
| 753 | return NULL; |
| 754 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 755 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 756 | if (!ctx->fallback_req) |
| 757 | goto err; |
| 758 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 759 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
| 760 | if (!ctx->completions) |
| 761 | goto err; |
| 762 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 763 | /* |
| 764 | * Use 5 bits less than the max cq entries, that should give us around |
| 765 | * 32 entries per hash list if totally full and uniformly spread. |
| 766 | */ |
| 767 | hash_bits = ilog2(p->cq_entries); |
| 768 | hash_bits -= 5; |
| 769 | if (hash_bits <= 0) |
| 770 | hash_bits = 1; |
| 771 | ctx->cancel_hash_bits = hash_bits; |
| 772 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 773 | GFP_KERNEL); |
| 774 | if (!ctx->cancel_hash) |
| 775 | goto err; |
| 776 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 777 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 778 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 779 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 780 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 781 | |
| 782 | ctx->flags = p->flags; |
| 783 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 784 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 785 | init_completion(&ctx->completions[0]); |
| 786 | init_completion(&ctx->completions[1]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 787 | mutex_init(&ctx->uring_lock); |
| 788 | init_waitqueue_head(&ctx->wait); |
| 789 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 790 | init_llist_head(&ctx->poll_llist); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 791 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 792 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 793 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 794 | init_waitqueue_head(&ctx->inflight_wait); |
| 795 | spin_lock_init(&ctx->inflight_lock); |
| 796 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 797 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 798 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 799 | if (ctx->fallback_req) |
| 800 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 801 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 802 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 803 | kfree(ctx); |
| 804 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 807 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 808 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 809 | struct io_ring_ctx *ctx = req->ctx; |
| 810 | |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 811 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 812 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 813 | } |
| 814 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 815 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 816 | { |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 817 | if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN) |
| 818 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 819 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 820 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | 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] | 824 | { |
| 825 | struct io_kiocb *req; |
| 826 | |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 827 | 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] | 828 | if (req && !req_need_defer(req)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 829 | list_del_init(&req->list); |
| 830 | return req; |
| 831 | } |
| 832 | |
| 833 | return NULL; |
| 834 | } |
| 835 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 836 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 837 | { |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 838 | struct io_kiocb *req; |
| 839 | |
| 840 | 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] | 841 | if (req) { |
| 842 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 843 | return NULL; |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 844 | if (!__req_need_defer(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 845 | list_del_init(&req->list); |
| 846 | return req; |
| 847 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | return NULL; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 851 | } |
| 852 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 853 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 854 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 855 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 856 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 857 | if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 858 | /* order cqe stores with ring update */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 859 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 860 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 861 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 862 | wake_up_interruptible(&ctx->cq_wait); |
| 863 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 868 | static inline bool io_prep_async_work(struct io_kiocb *req, |
| 869 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 870 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 871 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 872 | bool do_hashed = false; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 873 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 874 | if (req->flags & REQ_F_ISREG) { |
| 875 | if (def->hash_reg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 876 | do_hashed = true; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 877 | } else { |
| 878 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 879 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 880 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 881 | if (def->needs_mm) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 882 | req->work.flags |= IO_WQ_WORK_NEEDS_USER; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 883 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 884 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 885 | return do_hashed; |
| 886 | } |
| 887 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 888 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 889 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 890 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 891 | struct io_kiocb *link; |
| 892 | bool do_hashed; |
| 893 | |
| 894 | do_hashed = io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 895 | |
| 896 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, |
| 897 | req->flags); |
| 898 | if (!do_hashed) { |
| 899 | io_wq_enqueue(ctx->io_wq, &req->work); |
| 900 | } else { |
| 901 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, |
| 902 | file_inode(req->file)); |
| 903 | } |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 904 | |
| 905 | if (link) |
| 906 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 907 | } |
| 908 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 909 | static void io_kill_timeout(struct io_kiocb *req) |
| 910 | { |
| 911 | int ret; |
| 912 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 913 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 914 | if (ret != -1) { |
| 915 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 916 | list_del_init(&req->list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 917 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 918 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | |
| 922 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 923 | { |
| 924 | struct io_kiocb *req, *tmp; |
| 925 | |
| 926 | spin_lock_irq(&ctx->completion_lock); |
| 927 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 928 | io_kill_timeout(req); |
| 929 | spin_unlock_irq(&ctx->completion_lock); |
| 930 | } |
| 931 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 932 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 933 | { |
| 934 | struct io_kiocb *req; |
| 935 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 936 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 937 | io_kill_timeout(req); |
| 938 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 939 | __io_commit_cqring(ctx); |
| 940 | |
| 941 | while ((req = io_get_deferred_req(ctx)) != NULL) { |
| 942 | req->flags |= REQ_F_IO_DRAINED; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 943 | io_queue_async_work(req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 947 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 948 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 949 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 950 | unsigned tail; |
| 951 | |
| 952 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 953 | /* |
| 954 | * writes to the cq entry need to come after reading head; the |
| 955 | * control dependency is enough as we're using WRITE_ONCE to |
| 956 | * fill the cq entry |
| 957 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 958 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 959 | return NULL; |
| 960 | |
| 961 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 962 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 965 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 966 | { |
| 967 | if (!ctx->eventfd_async) |
| 968 | return true; |
| 969 | return io_wq_current_is_worker() || in_interrupt(); |
| 970 | } |
| 971 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 972 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| 973 | { |
| 974 | if (waitqueue_active(&ctx->wait)) |
| 975 | wake_up(&ctx->wait); |
| 976 | if (waitqueue_active(&ctx->sqo_wait)) |
| 977 | wake_up(&ctx->sqo_wait); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 978 | if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 979 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 980 | } |
| 981 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 982 | /* Returns true if there are no backlogged entries after the flush */ |
| 983 | 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] | 984 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 985 | struct io_rings *rings = ctx->rings; |
| 986 | struct io_uring_cqe *cqe; |
| 987 | struct io_kiocb *req; |
| 988 | unsigned long flags; |
| 989 | LIST_HEAD(list); |
| 990 | |
| 991 | if (!force) { |
| 992 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 993 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 994 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 995 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 996 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1000 | |
| 1001 | /* if force is set, the ring is going away. always drop after that */ |
| 1002 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1003 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1004 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1005 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1006 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1007 | cqe = io_get_cqring(ctx); |
| 1008 | if (!cqe && !force) |
| 1009 | break; |
| 1010 | |
| 1011 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 1012 | list); |
| 1013 | list_move(&req->list, &list); |
| 1014 | if (cqe) { |
| 1015 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1016 | WRITE_ONCE(cqe->res, req->result); |
| 1017 | WRITE_ONCE(cqe->flags, 0); |
| 1018 | } else { |
| 1019 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1020 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | io_commit_cqring(ctx); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1025 | if (cqe) { |
| 1026 | clear_bit(0, &ctx->sq_check_overflow); |
| 1027 | clear_bit(0, &ctx->cq_check_overflow); |
| 1028 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1029 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1030 | io_cqring_ev_posted(ctx); |
| 1031 | |
| 1032 | while (!list_empty(&list)) { |
| 1033 | req = list_first_entry(&list, struct io_kiocb, list); |
| 1034 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1035 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1036 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1037 | |
| 1038 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1041 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1042 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1043 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1044 | struct io_uring_cqe *cqe; |
| 1045 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1046 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1047 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1048 | /* |
| 1049 | * If we can't get a cq entry, userspace overflowed the |
| 1050 | * submission (by quite a lot). Increment the overflow count in |
| 1051 | * the ring. |
| 1052 | */ |
| 1053 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1054 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1055 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1056 | WRITE_ONCE(cqe->res, res); |
| 1057 | WRITE_ONCE(cqe->flags, 0); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1058 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1059 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1060 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1061 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1062 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1063 | set_bit(0, &ctx->sq_check_overflow); |
| 1064 | set_bit(0, &ctx->cq_check_overflow); |
| 1065 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1066 | refcount_inc(&req->refs); |
| 1067 | req->result = res; |
| 1068 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1072 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1073 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1074 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1075 | unsigned long flags; |
| 1076 | |
| 1077 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1078 | io_cqring_fill_event(req, res); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1079 | io_commit_cqring(ctx); |
| 1080 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1081 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1082 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1085 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1086 | { |
| 1087 | return req == (struct io_kiocb *) |
| 1088 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1089 | } |
| 1090 | |
| 1091 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1092 | { |
| 1093 | struct io_kiocb *req; |
| 1094 | |
| 1095 | req = ctx->fallback_req; |
| 1096 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) |
| 1097 | return req; |
| 1098 | |
| 1099 | return NULL; |
| 1100 | } |
| 1101 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1102 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 1103 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1104 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1105 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1106 | struct io_kiocb *req; |
| 1107 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1108 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1109 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1110 | if (unlikely(!req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1111 | goto fallback; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1112 | } else if (!state->free_reqs) { |
| 1113 | size_t sz; |
| 1114 | int ret; |
| 1115 | |
| 1116 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1117 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1118 | |
| 1119 | /* |
| 1120 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1121 | * retry single alloc to be on the safe side. |
| 1122 | */ |
| 1123 | if (unlikely(ret <= 0)) { |
| 1124 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1125 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1126 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1127 | ret = 1; |
| 1128 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1129 | state->free_reqs = ret - 1; |
| 1130 | state->cur_req = 1; |
| 1131 | req = state->reqs[0]; |
| 1132 | } else { |
| 1133 | req = state->reqs[state->cur_req]; |
| 1134 | state->free_reqs--; |
| 1135 | state->cur_req++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1138 | got_it: |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 1139 | req->io = NULL; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 1140 | req->ring_file = NULL; |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 1141 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1142 | req->ctx = ctx; |
| 1143 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1144 | /* one is dropped after submission, the other at completion */ |
| 1145 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1146 | req->result = 0; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1147 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1148 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1149 | fallback: |
| 1150 | req = io_get_fallback_req(ctx); |
| 1151 | if (req) |
| 1152 | goto got_it; |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 1153 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1154 | return NULL; |
| 1155 | } |
| 1156 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1157 | static void __io_req_do_free(struct io_kiocb *req) |
| 1158 | { |
| 1159 | if (likely(!io_is_fallback_req(req))) |
| 1160 | kmem_cache_free(req_cachep, req); |
| 1161 | else |
| 1162 | clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req); |
| 1163 | } |
| 1164 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1165 | static void __io_req_aux_free(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1166 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1167 | struct io_ring_ctx *ctx = req->ctx; |
| 1168 | |
YueHaibing | 96fd84d | 2020-01-07 22:22:44 +0800 | [diff] [blame] | 1169 | kfree(req->io); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1170 | if (req->file) { |
| 1171 | if (req->flags & REQ_F_FIXED_FILE) |
| 1172 | percpu_ref_put(&ctx->file_data->refs); |
| 1173 | else |
| 1174 | fput(req->file); |
| 1175 | } |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | static void __io_free_req(struct io_kiocb *req) |
| 1179 | { |
| 1180 | __io_req_aux_free(req); |
| 1181 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1182 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1183 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1184 | unsigned long flags; |
| 1185 | |
| 1186 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1187 | list_del(&req->inflight_entry); |
| 1188 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1189 | wake_up(&ctx->inflight_wait); |
| 1190 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1191 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1192 | |
| 1193 | percpu_ref_put(&req->ctx->refs); |
| 1194 | __io_req_do_free(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1195 | } |
| 1196 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1197 | struct req_batch { |
| 1198 | void *reqs[IO_IOPOLL_BATCH]; |
| 1199 | int to_free; |
| 1200 | int need_iter; |
| 1201 | }; |
| 1202 | |
| 1203 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) |
| 1204 | { |
| 1205 | if (!rb->to_free) |
| 1206 | return; |
| 1207 | if (rb->need_iter) { |
| 1208 | int i, inflight = 0; |
| 1209 | unsigned long flags; |
| 1210 | |
| 1211 | for (i = 0; i < rb->to_free; i++) { |
| 1212 | struct io_kiocb *req = rb->reqs[i]; |
| 1213 | |
| 1214 | if (req->flags & REQ_F_FIXED_FILE) |
| 1215 | req->file = NULL; |
| 1216 | if (req->flags & REQ_F_INFLIGHT) |
| 1217 | inflight++; |
| 1218 | else |
| 1219 | rb->reqs[i] = NULL; |
| 1220 | __io_req_aux_free(req); |
| 1221 | } |
| 1222 | if (!inflight) |
| 1223 | goto do_free; |
| 1224 | |
| 1225 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1226 | for (i = 0; i < rb->to_free; i++) { |
| 1227 | struct io_kiocb *req = rb->reqs[i]; |
| 1228 | |
| 1229 | if (req) { |
| 1230 | list_del(&req->inflight_entry); |
| 1231 | if (!--inflight) |
| 1232 | break; |
| 1233 | } |
| 1234 | } |
| 1235 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1236 | |
| 1237 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1238 | wake_up(&ctx->inflight_wait); |
| 1239 | } |
| 1240 | do_free: |
| 1241 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
| 1242 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
| 1243 | percpu_ref_put_many(&ctx->file_data->refs, rb->to_free); |
| 1244 | rb->to_free = rb->need_iter = 0; |
| 1245 | } |
| 1246 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1247 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1248 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1249 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1250 | int ret; |
| 1251 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1252 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1253 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1254 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1255 | io_commit_cqring(ctx); |
| 1256 | req->flags &= ~REQ_F_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1257 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1258 | return true; |
| 1259 | } |
| 1260 | |
| 1261 | return false; |
| 1262 | } |
| 1263 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1264 | 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] | 1265 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1266 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1267 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1268 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1269 | /* Already got next link */ |
| 1270 | if (req->flags & REQ_F_LINK_NEXT) |
| 1271 | return; |
| 1272 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1273 | /* |
| 1274 | * The list should never be empty when we are called here. But could |
| 1275 | * potentially happen if the chain is messed up, check to be on the |
| 1276 | * safe side. |
| 1277 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1278 | while (!list_empty(&req->link_list)) { |
| 1279 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 1280 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1281 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1282 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 1283 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 1284 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1285 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1286 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1287 | continue; |
| 1288 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1289 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1290 | list_del_init(&req->link_list); |
| 1291 | if (!list_empty(&nxt->link_list)) |
| 1292 | nxt->flags |= REQ_F_LINK; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 1293 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1294 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1295 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1296 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1297 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1298 | if (wake_ev) |
| 1299 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | /* |
| 1303 | * Called if REQ_F_LINK is set, and we fail the head request |
| 1304 | */ |
| 1305 | static void io_fail_links(struct io_kiocb *req) |
| 1306 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1307 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1308 | unsigned long flags; |
| 1309 | |
| 1310 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1311 | |
| 1312 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1313 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1314 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1315 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1316 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1317 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1318 | |
| 1319 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1320 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1321 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1322 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1323 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1324 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1325 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1326 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1327 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1328 | |
| 1329 | io_commit_cqring(ctx); |
| 1330 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1331 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1332 | } |
| 1333 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1334 | 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] | 1335 | { |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1336 | if (likely(!(req->flags & REQ_F_LINK))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1337 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1338 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1339 | /* |
| 1340 | * If LINK is set, we have dependent requests in this chain. If we |
| 1341 | * didn't fail this request, queue the first one up, moving any other |
| 1342 | * dependencies to the next request. In case of failure, fail the rest |
| 1343 | * of the chain. |
| 1344 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1345 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1346 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1347 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1348 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1349 | struct io_ring_ctx *ctx = req->ctx; |
| 1350 | unsigned long flags; |
| 1351 | |
| 1352 | /* |
| 1353 | * If this is a timeout link, we could be racing with the |
| 1354 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1355 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1356 | */ |
| 1357 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1358 | io_req_link_next(req, nxt); |
| 1359 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1360 | } else { |
| 1361 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1362 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1363 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1364 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1365 | static void io_free_req(struct io_kiocb *req) |
| 1366 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1367 | struct io_kiocb *nxt = NULL; |
| 1368 | |
| 1369 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1370 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1371 | |
| 1372 | if (nxt) |
| 1373 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1374 | } |
| 1375 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1376 | /* |
| 1377 | * Drop reference to request, return next in chain (if there is one) if this |
| 1378 | * was the last reference to this request. |
| 1379 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1380 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1381 | 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] | 1382 | { |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1383 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1384 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1385 | if (refcount_dec_and_test(&req->refs)) |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1386 | __io_free_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1389 | static void io_put_req(struct io_kiocb *req) |
| 1390 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1391 | if (refcount_dec_and_test(&req->refs)) |
| 1392 | io_free_req(req); |
| 1393 | } |
| 1394 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1395 | /* |
| 1396 | * Must only be used if we don't need to care about links, usually from |
| 1397 | * within the completion handling itself. |
| 1398 | */ |
| 1399 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1400 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1401 | /* drop both submit and complete references */ |
| 1402 | if (refcount_sub_and_test(2, &req->refs)) |
| 1403 | __io_free_req(req); |
| 1404 | } |
| 1405 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1406 | static void io_double_put_req(struct io_kiocb *req) |
| 1407 | { |
| 1408 | /* drop both submit and complete references */ |
| 1409 | if (refcount_sub_and_test(2, &req->refs)) |
| 1410 | io_free_req(req); |
| 1411 | } |
| 1412 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1413 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1414 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1415 | struct io_rings *rings = ctx->rings; |
| 1416 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1417 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1418 | /* |
| 1419 | * noflush == true is from the waitqueue handler, just ensure |
| 1420 | * we wake up the task, and the next invocation will flush the |
| 1421 | * entries. We cannot safely to it from here. |
| 1422 | */ |
| 1423 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1424 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1425 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1426 | io_cqring_overflow_flush(ctx, false); |
| 1427 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1428 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1429 | /* See comment at the top of this file */ |
| 1430 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1431 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1432 | } |
| 1433 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1434 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1435 | { |
| 1436 | struct io_rings *rings = ctx->rings; |
| 1437 | |
| 1438 | /* make sure SQ entry isn't read before tail */ |
| 1439 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1440 | } |
| 1441 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1442 | 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] | 1443 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1444 | if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req)) |
| 1445 | return false; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1446 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1447 | if (!(req->flags & REQ_F_FIXED_FILE) || req->io) |
| 1448 | rb->need_iter++; |
| 1449 | |
| 1450 | rb->reqs[rb->to_free++] = req; |
| 1451 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1452 | io_free_req_many(req->ctx, rb); |
| 1453 | return true; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1456 | /* |
| 1457 | * Find and free completed poll iocbs |
| 1458 | */ |
| 1459 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1460 | struct list_head *done) |
| 1461 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1462 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1463 | struct io_kiocb *req; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1464 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1465 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1466 | while (!list_empty(done)) { |
| 1467 | req = list_first_entry(done, struct io_kiocb, list); |
| 1468 | list_del(&req->list); |
| 1469 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1470 | io_cqring_fill_event(req, req->result); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1471 | (*nr_events)++; |
| 1472 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1473 | if (refcount_dec_and_test(&req->refs) && |
| 1474 | !io_req_multi_free(&rb, req)) |
| 1475 | io_free_req(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1476 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1477 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1478 | io_commit_cqring(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1479 | io_free_req_many(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1483 | long min) |
| 1484 | { |
| 1485 | struct io_kiocb *req, *tmp; |
| 1486 | LIST_HEAD(done); |
| 1487 | bool spin; |
| 1488 | int ret; |
| 1489 | |
| 1490 | /* |
| 1491 | * Only spin for completions if we don't have multiple devices hanging |
| 1492 | * off our complete list, and we're under the requested amount. |
| 1493 | */ |
| 1494 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1495 | |
| 1496 | ret = 0; |
| 1497 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1498 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1499 | |
| 1500 | /* |
| 1501 | * Move completed entries to our local list. If we find a |
| 1502 | * request that requires polling, break out and complete |
| 1503 | * the done list first, if we have entries there. |
| 1504 | */ |
| 1505 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 1506 | list_move_tail(&req->list, &done); |
| 1507 | continue; |
| 1508 | } |
| 1509 | if (!list_empty(&done)) |
| 1510 | break; |
| 1511 | |
| 1512 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1513 | if (ret < 0) |
| 1514 | break; |
| 1515 | |
| 1516 | if (ret && spin) |
| 1517 | spin = false; |
| 1518 | ret = 0; |
| 1519 | } |
| 1520 | |
| 1521 | if (!list_empty(&done)) |
| 1522 | io_iopoll_complete(ctx, nr_events, &done); |
| 1523 | |
| 1524 | return ret; |
| 1525 | } |
| 1526 | |
| 1527 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 1528 | * 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] | 1529 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1530 | * as a non-spinning completion check. |
| 1531 | */ |
| 1532 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1533 | long min) |
| 1534 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1535 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1536 | int ret; |
| 1537 | |
| 1538 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1539 | if (ret < 0) |
| 1540 | return ret; |
| 1541 | if (!min || *nr_events >= min) |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
| 1545 | return 1; |
| 1546 | } |
| 1547 | |
| 1548 | /* |
| 1549 | * We can't just wait for polled events to come to us, we have to actively |
| 1550 | * find and complete them. |
| 1551 | */ |
| 1552 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1553 | { |
| 1554 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1555 | return; |
| 1556 | |
| 1557 | mutex_lock(&ctx->uring_lock); |
| 1558 | while (!list_empty(&ctx->poll_list)) { |
| 1559 | unsigned int nr_events = 0; |
| 1560 | |
| 1561 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1562 | |
| 1563 | /* |
| 1564 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1565 | * in this case we need to ensure that we reap all events. |
| 1566 | */ |
| 1567 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1568 | } |
| 1569 | mutex_unlock(&ctx->uring_lock); |
| 1570 | } |
| 1571 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1572 | static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1573 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1574 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1575 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1576 | |
| 1577 | do { |
| 1578 | int tmin = 0; |
| 1579 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1580 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1581 | * Don't enter poll loop if we already have events pending. |
| 1582 | * If we do, we can potentially be spinning for commands that |
| 1583 | * already triggered a CQE (eg in error). |
| 1584 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1585 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1586 | break; |
| 1587 | |
| 1588 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1589 | * If a submit got punted to a workqueue, we can have the |
| 1590 | * application entering polling for a command before it gets |
| 1591 | * issued. That app will hold the uring_lock for the duration |
| 1592 | * of the poll right here, so we need to take a breather every |
| 1593 | * now and then to ensure that the issue has a chance to add |
| 1594 | * the poll to the issued list. Otherwise we can spin here |
| 1595 | * forever, while the workqueue is stuck trying to acquire the |
| 1596 | * very same mutex. |
| 1597 | */ |
| 1598 | if (!(++iters & 7)) { |
| 1599 | mutex_unlock(&ctx->uring_lock); |
| 1600 | mutex_lock(&ctx->uring_lock); |
| 1601 | } |
| 1602 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1603 | if (*nr_events < min) |
| 1604 | tmin = min - *nr_events; |
| 1605 | |
| 1606 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1607 | if (ret <= 0) |
| 1608 | break; |
| 1609 | ret = 0; |
| 1610 | } while (min && !*nr_events && !need_resched()); |
| 1611 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1612 | return ret; |
| 1613 | } |
| 1614 | |
| 1615 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1616 | long min) |
| 1617 | { |
| 1618 | int ret; |
| 1619 | |
| 1620 | /* |
| 1621 | * We disallow the app entering submit/complete with polling, but we |
| 1622 | * still need to lock the ring to prevent racing with polled issue |
| 1623 | * that got punted to a workqueue. |
| 1624 | */ |
| 1625 | mutex_lock(&ctx->uring_lock); |
| 1626 | ret = __io_iopoll_check(ctx, nr_events, min); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1627 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1628 | return ret; |
| 1629 | } |
| 1630 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1631 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1632 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1633 | /* |
| 1634 | * Tell lockdep we inherited freeze protection from submission |
| 1635 | * thread. |
| 1636 | */ |
| 1637 | if (req->flags & REQ_F_ISREG) { |
| 1638 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1639 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1640 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1641 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1642 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1643 | } |
| 1644 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1645 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1646 | { |
| 1647 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1648 | req->flags |= REQ_F_FAIL_LINK; |
| 1649 | } |
| 1650 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1651 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1652 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1653 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1654 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1655 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1656 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1657 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1658 | if (res != req->result) |
| 1659 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1660 | io_cqring_add_event(req, res); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1664 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1665 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1666 | |
| 1667 | io_complete_rw_common(kiocb, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1668 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1671 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
| 1672 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1673 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1674 | struct io_kiocb *nxt = NULL; |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1675 | |
| 1676 | io_complete_rw_common(kiocb, res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1677 | io_put_req_find_next(req, &nxt); |
| 1678 | |
| 1679 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1682 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 1683 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1684 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1685 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1686 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1687 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1688 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1689 | if (res != req->result) |
| 1690 | req_set_fail_links(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1691 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1692 | if (res != -EAGAIN) |
| 1693 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 1694 | } |
| 1695 | |
| 1696 | /* |
| 1697 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 1698 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 1699 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 1700 | * accessing the kiocb cookie. |
| 1701 | */ |
| 1702 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 1703 | { |
| 1704 | struct io_ring_ctx *ctx = req->ctx; |
| 1705 | |
| 1706 | /* |
| 1707 | * Track whether we have multiple files in our lists. This will impact |
| 1708 | * how we do polling eventually, not spinning if we're on potentially |
| 1709 | * different devices. |
| 1710 | */ |
| 1711 | if (list_empty(&ctx->poll_list)) { |
| 1712 | ctx->poll_multi_file = false; |
| 1713 | } else if (!ctx->poll_multi_file) { |
| 1714 | struct io_kiocb *list_req; |
| 1715 | |
| 1716 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1717 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1718 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1719 | ctx->poll_multi_file = true; |
| 1720 | } |
| 1721 | |
| 1722 | /* |
| 1723 | * For fast devices, IO may have already completed. If it has, add |
| 1724 | * it to the front so we find it first. |
| 1725 | */ |
| 1726 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1727 | list_add(&req->list, &ctx->poll_list); |
| 1728 | else |
| 1729 | list_add_tail(&req->list, &ctx->poll_list); |
| 1730 | } |
| 1731 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1732 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1733 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1734 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1735 | int diff = state->has_refs - state->used_refs; |
| 1736 | |
| 1737 | if (diff) |
| 1738 | fput_many(state->file, diff); |
| 1739 | state->file = NULL; |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | /* |
| 1744 | * Get as many references to a file as we have IOs left in this submission, |
| 1745 | * assuming most submissions are for one file, or at least that each file |
| 1746 | * has more than one submission. |
| 1747 | */ |
| 1748 | static struct file *io_file_get(struct io_submit_state *state, int fd) |
| 1749 | { |
| 1750 | if (!state) |
| 1751 | return fget(fd); |
| 1752 | |
| 1753 | if (state->file) { |
| 1754 | if (state->fd == fd) { |
| 1755 | state->used_refs++; |
| 1756 | state->ios_left--; |
| 1757 | return state->file; |
| 1758 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1759 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1760 | } |
| 1761 | state->file = fget_many(fd, state->ios_left); |
| 1762 | if (!state->file) |
| 1763 | return NULL; |
| 1764 | |
| 1765 | state->fd = fd; |
| 1766 | state->has_refs = state->ios_left; |
| 1767 | state->used_refs = 1; |
| 1768 | state->ios_left--; |
| 1769 | return state->file; |
| 1770 | } |
| 1771 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1772 | /* |
| 1773 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 1774 | * any file. For now, just ensure that anything potentially problematic is done |
| 1775 | * inline. |
| 1776 | */ |
| 1777 | static bool io_file_supports_async(struct file *file) |
| 1778 | { |
| 1779 | umode_t mode = file_inode(file)->i_mode; |
| 1780 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 1781 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1782 | return true; |
| 1783 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 1784 | return true; |
| 1785 | |
| 1786 | return false; |
| 1787 | } |
| 1788 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1789 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1790 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1791 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1792 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1793 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1794 | unsigned ioprio; |
| 1795 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1796 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1797 | if (!req->file) |
| 1798 | return -EBADF; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1799 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1800 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 1801 | req->flags |= REQ_F_ISREG; |
| 1802 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1803 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1804 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 1805 | req->flags |= REQ_F_CUR_POS; |
| 1806 | kiocb->ki_pos = req->file->f_pos; |
| 1807 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1808 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 1809 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
| 1810 | |
| 1811 | ioprio = READ_ONCE(sqe->ioprio); |
| 1812 | if (ioprio) { |
| 1813 | ret = ioprio_check_cap(ioprio); |
| 1814 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1815 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1816 | |
| 1817 | kiocb->ki_ioprio = ioprio; |
| 1818 | } else |
| 1819 | kiocb->ki_ioprio = get_current_ioprio(); |
| 1820 | |
| 1821 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 1822 | if (unlikely(ret)) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1823 | return ret; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1824 | |
| 1825 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1826 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 1827 | (req->file->f_flags & O_NONBLOCK)) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1828 | req->flags |= REQ_F_NOWAIT; |
| 1829 | |
| 1830 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1831 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1832 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1833 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1834 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 1835 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1836 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1837 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1838 | kiocb->ki_flags |= IOCB_HIPRI; |
| 1839 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 1840 | req->result = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1841 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1842 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1843 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1844 | kiocb->ki_complete = io_complete_rw; |
| 1845 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1846 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1847 | req->rw.addr = READ_ONCE(sqe->addr); |
| 1848 | req->rw.len = READ_ONCE(sqe->len); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1849 | /* we own ->private, reuse it for the buffer index */ |
| 1850 | req->rw.kiocb.private = (void *) (unsigned long) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1851 | READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1852 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 1856 | { |
| 1857 | switch (ret) { |
| 1858 | case -EIOCBQUEUED: |
| 1859 | break; |
| 1860 | case -ERESTARTSYS: |
| 1861 | case -ERESTARTNOINTR: |
| 1862 | case -ERESTARTNOHAND: |
| 1863 | case -ERESTART_RESTARTBLOCK: |
| 1864 | /* |
| 1865 | * We can't just restart the syscall, since previously |
| 1866 | * submitted sqes may already be in progress. Just fail this |
| 1867 | * IO with EINTR. |
| 1868 | */ |
| 1869 | ret = -EINTR; |
| 1870 | /* fall through */ |
| 1871 | default: |
| 1872 | kiocb->ki_complete(kiocb, ret, 0); |
| 1873 | } |
| 1874 | } |
| 1875 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1876 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt, |
| 1877 | bool in_async) |
| 1878 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1879 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 1880 | |
| 1881 | if (req->flags & REQ_F_CUR_POS) |
| 1882 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1883 | if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1884 | *nxt = __io_complete_rw(kiocb, ret); |
| 1885 | else |
| 1886 | io_rw_done(kiocb, ret); |
| 1887 | } |
| 1888 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1889 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 1890 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1891 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1892 | struct io_ring_ctx *ctx = req->ctx; |
| 1893 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1894 | struct io_mapped_ubuf *imu; |
| 1895 | unsigned index, buf_index; |
| 1896 | size_t offset; |
| 1897 | u64 buf_addr; |
| 1898 | |
| 1899 | /* attempt to use fixed buffers without having provided iovecs */ |
| 1900 | if (unlikely(!ctx->user_bufs)) |
| 1901 | return -EFAULT; |
| 1902 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1903 | buf_index = (unsigned long) req->rw.kiocb.private; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1904 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 1905 | return -EFAULT; |
| 1906 | |
| 1907 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 1908 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1909 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1910 | |
| 1911 | /* overflow */ |
| 1912 | if (buf_addr + len < buf_addr) |
| 1913 | return -EFAULT; |
| 1914 | /* not inside the mapped region */ |
| 1915 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 1916 | return -EFAULT; |
| 1917 | |
| 1918 | /* |
| 1919 | * May not be a start of buffer, set size appropriately |
| 1920 | * and advance us to the beginning. |
| 1921 | */ |
| 1922 | offset = buf_addr - imu->ubuf; |
| 1923 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1924 | |
| 1925 | if (offset) { |
| 1926 | /* |
| 1927 | * Don't use iov_iter_advance() here, as it's really slow for |
| 1928 | * using the latter parts of a big fixed buffer - it iterates |
| 1929 | * over each segment manually. We can cheat a bit here, because |
| 1930 | * we know that: |
| 1931 | * |
| 1932 | * 1) it's a BVEC iter, we set it up |
| 1933 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 1934 | * first and last bvec |
| 1935 | * |
| 1936 | * So just find our index, and adjust the iterator afterwards. |
| 1937 | * If the offset is within the first bvec (or the whole first |
| 1938 | * bvec, just use iov_iter_advance(). This makes it easier |
| 1939 | * since we can just skip the first segment, which may not |
| 1940 | * be PAGE_SIZE aligned. |
| 1941 | */ |
| 1942 | const struct bio_vec *bvec = imu->bvec; |
| 1943 | |
| 1944 | if (offset <= bvec->bv_len) { |
| 1945 | iov_iter_advance(iter, offset); |
| 1946 | } else { |
| 1947 | unsigned long seg_skip; |
| 1948 | |
| 1949 | /* skip first vec */ |
| 1950 | offset -= bvec->bv_len; |
| 1951 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 1952 | |
| 1953 | iter->bvec = bvec + seg_skip; |
| 1954 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 1955 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1956 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1957 | } |
| 1958 | } |
| 1959 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 1960 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1961 | } |
| 1962 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 1963 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 1964 | struct iovec **iovec, struct iov_iter *iter) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1965 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1966 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 1967 | size_t sqe_len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1968 | u8 opcode; |
| 1969 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1970 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 1971 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1972 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1973 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1974 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1975 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1976 | /* buffer index only valid with fixed read/write */ |
| 1977 | if (req->rw.kiocb.private) |
| 1978 | return -EINVAL; |
| 1979 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1980 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
| 1981 | ssize_t ret; |
| 1982 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 1983 | *iovec = NULL; |
| 1984 | return ret; |
| 1985 | } |
| 1986 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1987 | if (req->io) { |
| 1988 | struct io_async_rw *iorw = &req->io->rw; |
| 1989 | |
| 1990 | *iovec = iorw->iov; |
| 1991 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 1992 | if (iorw->iov == iorw->fast_iov) |
| 1993 | *iovec = NULL; |
| 1994 | return iorw->size; |
| 1995 | } |
| 1996 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 1997 | if (!req->has_user) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1998 | return -EFAULT; |
| 1999 | |
| 2000 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2001 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2002 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2003 | iovec, iter); |
| 2004 | #endif |
| 2005 | |
| 2006 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2007 | } |
| 2008 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2009 | /* |
| 2010 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2011 | * by looping over ->read() or ->write() manually. |
| 2012 | */ |
| 2013 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2014 | struct iov_iter *iter) |
| 2015 | { |
| 2016 | ssize_t ret = 0; |
| 2017 | |
| 2018 | /* |
| 2019 | * Don't support polled IO through this interface, and we can't |
| 2020 | * support non-blocking either. For the latter, this just causes |
| 2021 | * the kiocb to be handled from an async context. |
| 2022 | */ |
| 2023 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2024 | return -EOPNOTSUPP; |
| 2025 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2026 | return -EAGAIN; |
| 2027 | |
| 2028 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2029 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2030 | ssize_t nr; |
| 2031 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2032 | if (!iov_iter_is_bvec(iter)) { |
| 2033 | iovec = iov_iter_iovec(iter); |
| 2034 | } else { |
| 2035 | /* fixed buffers import bvec */ |
| 2036 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2037 | + iter->iov_offset; |
| 2038 | iovec.iov_len = min(iter->count, |
| 2039 | iter->bvec->bv_len - iter->iov_offset); |
| 2040 | } |
| 2041 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2042 | if (rw == READ) { |
| 2043 | nr = file->f_op->read(file, iovec.iov_base, |
| 2044 | iovec.iov_len, &kiocb->ki_pos); |
| 2045 | } else { |
| 2046 | nr = file->f_op->write(file, iovec.iov_base, |
| 2047 | iovec.iov_len, &kiocb->ki_pos); |
| 2048 | } |
| 2049 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2050 | if (iov_iter_is_bvec(iter)) |
| 2051 | kunmap(iter->bvec->bv_page); |
| 2052 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2053 | if (nr < 0) { |
| 2054 | if (!ret) |
| 2055 | ret = nr; |
| 2056 | break; |
| 2057 | } |
| 2058 | ret += nr; |
| 2059 | if (nr != iovec.iov_len) |
| 2060 | break; |
| 2061 | iov_iter_advance(iter, nr); |
| 2062 | } |
| 2063 | |
| 2064 | return ret; |
| 2065 | } |
| 2066 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2067 | 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] | 2068 | struct iovec *iovec, struct iovec *fast_iov, |
| 2069 | struct iov_iter *iter) |
| 2070 | { |
| 2071 | req->io->rw.nr_segs = iter->nr_segs; |
| 2072 | req->io->rw.size = io_size; |
| 2073 | req->io->rw.iov = iovec; |
| 2074 | if (!req->io->rw.iov) { |
| 2075 | req->io->rw.iov = req->io->rw.fast_iov; |
| 2076 | memcpy(req->io->rw.iov, fast_iov, |
| 2077 | sizeof(struct iovec) * iter->nr_segs); |
| 2078 | } |
| 2079 | } |
| 2080 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2081 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2082 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2083 | if (!io_op_defs[req->opcode].async_ctx) |
| 2084 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2085 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2086 | return req->io == NULL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | static void io_rw_async(struct io_wq_work **workptr) |
| 2090 | { |
| 2091 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2092 | struct iovec *iov = NULL; |
| 2093 | |
| 2094 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2095 | iov = req->io->rw.iov; |
| 2096 | io_wq_submit_work(workptr); |
| 2097 | kfree(iov); |
| 2098 | } |
| 2099 | |
| 2100 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2101 | struct iovec *iovec, struct iovec *fast_iov, |
| 2102 | struct iov_iter *iter) |
| 2103 | { |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2104 | if (req->opcode == IORING_OP_READ_FIXED || |
| 2105 | req->opcode == IORING_OP_WRITE_FIXED) |
| 2106 | return 0; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2107 | if (!req->io && io_alloc_async_ctx(req)) |
| 2108 | return -ENOMEM; |
| 2109 | |
| 2110 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2111 | req->work.func = io_rw_async; |
| 2112 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2113 | } |
| 2114 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2115 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2116 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2117 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2118 | struct io_async_ctx *io; |
| 2119 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2120 | ssize_t ret; |
| 2121 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2122 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2123 | if (ret) |
| 2124 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2125 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2126 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2127 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2128 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2129 | if (!req->io) |
| 2130 | return 0; |
| 2131 | |
| 2132 | io = req->io; |
| 2133 | io->rw.iov = io->rw.fast_iov; |
| 2134 | req->io = NULL; |
| 2135 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter); |
| 2136 | req->io = io; |
| 2137 | if (ret < 0) |
| 2138 | return ret; |
| 2139 | |
| 2140 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2141 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2142 | } |
| 2143 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2144 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2145 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2146 | { |
| 2147 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2148 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2149 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2150 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2151 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2152 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2153 | ret = io_import_iovec(READ, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2154 | if (ret < 0) |
| 2155 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2156 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2157 | /* Ensure we clear previously set non-block flag */ |
| 2158 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2159 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2160 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2161 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2162 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2163 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2164 | req->result = io_size; |
| 2165 | |
| 2166 | /* |
| 2167 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2168 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2169 | */ |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2170 | if (force_nonblock && !io_file_supports_async(req->file)) { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2171 | req->flags |= REQ_F_MUST_PUNT; |
| 2172 | goto copy_iov; |
| 2173 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2174 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2175 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2176 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2177 | if (!ret) { |
| 2178 | ssize_t ret2; |
| 2179 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2180 | if (req->file->f_op->read_iter) |
| 2181 | ret2 = call_read_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2182 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2183 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2184 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 2185 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2186 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2187 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2188 | } else { |
| 2189 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2190 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2191 | inline_vecs, &iter); |
| 2192 | if (ret) |
| 2193 | goto out_free; |
| 2194 | return -EAGAIN; |
| 2195 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2196 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2197 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2198 | if (!io_wq_current_is_worker()) |
| 2199 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2200 | return ret; |
| 2201 | } |
| 2202 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2203 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2204 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2205 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2206 | struct io_async_ctx *io; |
| 2207 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2208 | ssize_t ret; |
| 2209 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2210 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2211 | if (ret) |
| 2212 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2213 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2214 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 2215 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2216 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2217 | if (!req->io) |
| 2218 | return 0; |
| 2219 | |
| 2220 | io = req->io; |
| 2221 | io->rw.iov = io->rw.fast_iov; |
| 2222 | req->io = NULL; |
| 2223 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter); |
| 2224 | req->io = io; |
| 2225 | if (ret < 0) |
| 2226 | return ret; |
| 2227 | |
| 2228 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2229 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2232 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2233 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2234 | { |
| 2235 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2236 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2237 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2238 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2239 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2240 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2241 | ret = io_import_iovec(WRITE, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2242 | if (ret < 0) |
| 2243 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2244 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2245 | /* Ensure we clear previously set non-block flag */ |
| 2246 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2247 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2248 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2249 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2250 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2251 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2252 | req->result = io_size; |
| 2253 | |
| 2254 | /* |
| 2255 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2256 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2257 | */ |
| 2258 | if (force_nonblock && !io_file_supports_async(req->file)) { |
| 2259 | req->flags |= REQ_F_MUST_PUNT; |
| 2260 | goto copy_iov; |
| 2261 | } |
| 2262 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2263 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 2264 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 2265 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2266 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2267 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2268 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2269 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2270 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2271 | ssize_t ret2; |
| 2272 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2273 | /* |
| 2274 | * Open-code file_start_write here to grab freeze protection, |
| 2275 | * which will be released by another thread in |
| 2276 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 2277 | * released so that it doesn't complain about the held lock when |
| 2278 | * we return to userspace. |
| 2279 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2280 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2281 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2282 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2283 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2284 | SB_FREEZE_WRITE); |
| 2285 | } |
| 2286 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2287 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2288 | if (req->file->f_op->write_iter) |
| 2289 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2290 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2291 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2292 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2293 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2294 | } else { |
| 2295 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2296 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2297 | inline_vecs, &iter); |
| 2298 | if (ret) |
| 2299 | goto out_free; |
| 2300 | return -EAGAIN; |
| 2301 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2302 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2303 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2304 | if (!io_wq_current_is_worker()) |
| 2305 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2306 | return ret; |
| 2307 | } |
| 2308 | |
| 2309 | /* |
| 2310 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 2311 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2312 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2313 | { |
| 2314 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2315 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2316 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2317 | return -EINVAL; |
| 2318 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2319 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2320 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2321 | return 0; |
| 2322 | } |
| 2323 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2324 | 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] | 2325 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2326 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2327 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2328 | if (!req->file) |
| 2329 | return -EBADF; |
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 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2332 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2333 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2334 | return -EINVAL; |
| 2335 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2336 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 2337 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 2338 | return -EINVAL; |
| 2339 | |
| 2340 | req->sync.off = READ_ONCE(sqe->off); |
| 2341 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2342 | return 0; |
| 2343 | } |
| 2344 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2345 | static bool io_req_cancelled(struct io_kiocb *req) |
| 2346 | { |
| 2347 | if (req->work.flags & IO_WQ_WORK_CANCEL) { |
| 2348 | req_set_fail_links(req); |
| 2349 | io_cqring_add_event(req, -ECANCELED); |
| 2350 | io_put_req(req); |
| 2351 | return true; |
| 2352 | } |
| 2353 | |
| 2354 | return false; |
| 2355 | } |
| 2356 | |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2357 | static void io_link_work_cb(struct io_wq_work **workptr) |
| 2358 | { |
| 2359 | struct io_wq_work *work = *workptr; |
| 2360 | struct io_kiocb *link = work->data; |
| 2361 | |
| 2362 | io_queue_linked_timeout(link); |
| 2363 | work->func = io_wq_submit_work; |
| 2364 | } |
| 2365 | |
| 2366 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) |
| 2367 | { |
| 2368 | struct io_kiocb *link; |
| 2369 | |
| 2370 | io_prep_async_work(nxt, &link); |
| 2371 | *workptr = &nxt->work; |
| 2372 | if (link) { |
| 2373 | nxt->work.flags |= IO_WQ_WORK_CB; |
| 2374 | nxt->work.func = io_link_work_cb; |
| 2375 | nxt->work.data = link; |
| 2376 | } |
| 2377 | } |
| 2378 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2379 | static void io_fsync_finish(struct io_wq_work **workptr) |
| 2380 | { |
| 2381 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2382 | loff_t end = req->sync.off + req->sync.len; |
| 2383 | struct io_kiocb *nxt = NULL; |
| 2384 | int ret; |
| 2385 | |
| 2386 | if (io_req_cancelled(req)) |
| 2387 | return; |
| 2388 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2389 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2390 | end > 0 ? end : LLONG_MAX, |
| 2391 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 2392 | if (ret < 0) |
| 2393 | req_set_fail_links(req); |
| 2394 | io_cqring_add_event(req, ret); |
| 2395 | io_put_req_find_next(req, &nxt); |
| 2396 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2397 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2398 | } |
| 2399 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2400 | static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2401 | bool force_nonblock) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2402 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2403 | struct io_wq_work *work, *old_work; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2404 | |
| 2405 | /* fsync always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2406 | if (force_nonblock) { |
| 2407 | io_put_req(req); |
| 2408 | req->work.func = io_fsync_finish; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2409 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2410 | } |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2411 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2412 | work = old_work = &req->work; |
| 2413 | io_fsync_finish(&work); |
| 2414 | if (work && work != old_work) |
| 2415 | *nxt = container_of(work, struct io_kiocb, work); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2416 | return 0; |
| 2417 | } |
| 2418 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2419 | static void io_fallocate_finish(struct io_wq_work **workptr) |
| 2420 | { |
| 2421 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2422 | struct io_kiocb *nxt = NULL; |
| 2423 | int ret; |
| 2424 | |
| 2425 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 2426 | req->sync.len); |
| 2427 | if (ret < 0) |
| 2428 | req_set_fail_links(req); |
| 2429 | io_cqring_add_event(req, ret); |
| 2430 | io_put_req_find_next(req, &nxt); |
| 2431 | if (nxt) |
| 2432 | io_wq_assign_next(workptr, nxt); |
| 2433 | } |
| 2434 | |
| 2435 | static int io_fallocate_prep(struct io_kiocb *req, |
| 2436 | const struct io_uring_sqe *sqe) |
| 2437 | { |
| 2438 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 2439 | return -EINVAL; |
| 2440 | |
| 2441 | req->sync.off = READ_ONCE(sqe->off); |
| 2442 | req->sync.len = READ_ONCE(sqe->addr); |
| 2443 | req->sync.mode = READ_ONCE(sqe->len); |
| 2444 | return 0; |
| 2445 | } |
| 2446 | |
| 2447 | static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2448 | bool force_nonblock) |
| 2449 | { |
| 2450 | struct io_wq_work *work, *old_work; |
| 2451 | |
| 2452 | /* fallocate always requiring blocking context */ |
| 2453 | if (force_nonblock) { |
| 2454 | io_put_req(req); |
| 2455 | req->work.func = io_fallocate_finish; |
| 2456 | return -EAGAIN; |
| 2457 | } |
| 2458 | |
| 2459 | work = old_work = &req->work; |
| 2460 | io_fallocate_finish(&work); |
| 2461 | if (work && work != old_work) |
| 2462 | *nxt = container_of(work, struct io_kiocb, work); |
| 2463 | |
| 2464 | return 0; |
| 2465 | } |
| 2466 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2467 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2468 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame^] | 2469 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2470 | int ret; |
| 2471 | |
| 2472 | if (sqe->ioprio || sqe->buf_index) |
| 2473 | return -EINVAL; |
| 2474 | |
| 2475 | req->open.dfd = READ_ONCE(sqe->fd); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2476 | req->open.how.mode = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame^] | 2477 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2478 | req->open.how.flags = READ_ONCE(sqe->open_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2479 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame^] | 2480 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2481 | if (IS_ERR(req->open.filename)) { |
| 2482 | ret = PTR_ERR(req->open.filename); |
| 2483 | req->open.filename = NULL; |
| 2484 | return ret; |
| 2485 | } |
| 2486 | |
| 2487 | return 0; |
| 2488 | } |
| 2489 | |
| 2490 | static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2491 | bool force_nonblock) |
| 2492 | { |
| 2493 | struct open_flags op; |
| 2494 | struct open_how how; |
| 2495 | struct file *file; |
| 2496 | int ret; |
| 2497 | |
| 2498 | if (force_nonblock) { |
| 2499 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 2500 | return -EAGAIN; |
| 2501 | } |
| 2502 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2503 | how = build_open_how(req->open.how.flags, req->open.how.mode); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2504 | ret = build_open_flags(&how, &op); |
| 2505 | if (ret) |
| 2506 | goto err; |
| 2507 | |
| 2508 | ret = get_unused_fd_flags(how.flags); |
| 2509 | if (ret < 0) |
| 2510 | goto err; |
| 2511 | |
| 2512 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 2513 | if (IS_ERR(file)) { |
| 2514 | put_unused_fd(ret); |
| 2515 | ret = PTR_ERR(file); |
| 2516 | } else { |
| 2517 | fsnotify_open(file); |
| 2518 | fd_install(ret, file); |
| 2519 | } |
| 2520 | err: |
| 2521 | putname(req->open.filename); |
| 2522 | if (ret < 0) |
| 2523 | req_set_fail_links(req); |
| 2524 | io_cqring_add_event(req, ret); |
| 2525 | io_put_req_find_next(req, nxt); |
| 2526 | return 0; |
| 2527 | } |
| 2528 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 2529 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2530 | { |
| 2531 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2532 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 2533 | return -EINVAL; |
| 2534 | |
| 2535 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 2536 | req->madvise.len = READ_ONCE(sqe->len); |
| 2537 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2538 | return 0; |
| 2539 | #else |
| 2540 | return -EOPNOTSUPP; |
| 2541 | #endif |
| 2542 | } |
| 2543 | |
| 2544 | static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2545 | bool force_nonblock) |
| 2546 | { |
| 2547 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2548 | struct io_madvise *ma = &req->madvise; |
| 2549 | int ret; |
| 2550 | |
| 2551 | if (force_nonblock) |
| 2552 | return -EAGAIN; |
| 2553 | |
| 2554 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 2555 | if (ret < 0) |
| 2556 | req_set_fail_links(req); |
| 2557 | io_cqring_add_event(req, ret); |
| 2558 | io_put_req_find_next(req, nxt); |
| 2559 | return 0; |
| 2560 | #else |
| 2561 | return -EOPNOTSUPP; |
| 2562 | #endif |
| 2563 | } |
| 2564 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 2565 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2566 | { |
| 2567 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 2568 | return -EINVAL; |
| 2569 | |
| 2570 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 2571 | req->fadvise.len = READ_ONCE(sqe->len); |
| 2572 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2573 | return 0; |
| 2574 | } |
| 2575 | |
| 2576 | static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2577 | bool force_nonblock) |
| 2578 | { |
| 2579 | struct io_fadvise *fa = &req->fadvise; |
| 2580 | int ret; |
| 2581 | |
| 2582 | /* DONTNEED may block, others _should_ not */ |
| 2583 | if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock) |
| 2584 | return -EAGAIN; |
| 2585 | |
| 2586 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 2587 | if (ret < 0) |
| 2588 | req_set_fail_links(req); |
| 2589 | io_cqring_add_event(req, ret); |
| 2590 | io_put_req_find_next(req, nxt); |
| 2591 | return 0; |
| 2592 | } |
| 2593 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2594 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2595 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame^] | 2596 | const char __user *fname; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2597 | unsigned lookup_flags; |
| 2598 | int ret; |
| 2599 | |
| 2600 | if (sqe->ioprio || sqe->buf_index) |
| 2601 | return -EINVAL; |
| 2602 | |
| 2603 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2604 | req->open.mask = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame^] | 2605 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2606 | req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2607 | req->open.how.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2608 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2609 | if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2610 | return -EINVAL; |
| 2611 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame^] | 2612 | req->open.filename = getname_flags(fname, lookup_flags, NULL); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2613 | if (IS_ERR(req->open.filename)) { |
| 2614 | ret = PTR_ERR(req->open.filename); |
| 2615 | req->open.filename = NULL; |
| 2616 | return ret; |
| 2617 | } |
| 2618 | |
| 2619 | return 0; |
| 2620 | } |
| 2621 | |
| 2622 | static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2623 | bool force_nonblock) |
| 2624 | { |
| 2625 | struct io_open *ctx = &req->open; |
| 2626 | unsigned lookup_flags; |
| 2627 | struct path path; |
| 2628 | struct kstat stat; |
| 2629 | int ret; |
| 2630 | |
| 2631 | if (force_nonblock) |
| 2632 | return -EAGAIN; |
| 2633 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2634 | if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2635 | return -EINVAL; |
| 2636 | |
| 2637 | retry: |
| 2638 | /* filename_lookup() drops it, keep a reference */ |
| 2639 | ctx->filename->refcnt++; |
| 2640 | |
| 2641 | ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path, |
| 2642 | NULL); |
| 2643 | if (ret) |
| 2644 | goto err; |
| 2645 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2646 | ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2647 | path_put(&path); |
| 2648 | if (retry_estale(ret, lookup_flags)) { |
| 2649 | lookup_flags |= LOOKUP_REVAL; |
| 2650 | goto retry; |
| 2651 | } |
| 2652 | if (!ret) |
| 2653 | ret = cp_statx(&stat, ctx->buffer); |
| 2654 | err: |
| 2655 | putname(ctx->filename); |
| 2656 | if (ret < 0) |
| 2657 | req_set_fail_links(req); |
| 2658 | io_cqring_add_event(req, ret); |
| 2659 | io_put_req_find_next(req, nxt); |
| 2660 | return 0; |
| 2661 | } |
| 2662 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2663 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2664 | { |
| 2665 | /* |
| 2666 | * If we queue this for async, it must not be cancellable. That would |
| 2667 | * leave the 'file' in an undeterminate state. |
| 2668 | */ |
| 2669 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 2670 | |
| 2671 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 2672 | sqe->rw_flags || sqe->buf_index) |
| 2673 | return -EINVAL; |
| 2674 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2675 | return -EINVAL; |
| 2676 | |
| 2677 | req->close.fd = READ_ONCE(sqe->fd); |
| 2678 | if (req->file->f_op == &io_uring_fops || |
| 2679 | req->close.fd == req->ring_fd) |
| 2680 | return -EBADF; |
| 2681 | |
| 2682 | return 0; |
| 2683 | } |
| 2684 | |
| 2685 | static void io_close_finish(struct io_wq_work **workptr) |
| 2686 | { |
| 2687 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2688 | struct io_kiocb *nxt = NULL; |
| 2689 | |
| 2690 | /* Invoked with files, we need to do the close */ |
| 2691 | if (req->work.files) { |
| 2692 | int ret; |
| 2693 | |
| 2694 | ret = filp_close(req->close.put_file, req->work.files); |
| 2695 | if (ret < 0) { |
| 2696 | req_set_fail_links(req); |
| 2697 | } |
| 2698 | io_cqring_add_event(req, ret); |
| 2699 | } |
| 2700 | |
| 2701 | fput(req->close.put_file); |
| 2702 | |
| 2703 | /* we bypassed the re-issue, drop the submission reference */ |
| 2704 | io_put_req(req); |
| 2705 | io_put_req_find_next(req, &nxt); |
| 2706 | if (nxt) |
| 2707 | io_wq_assign_next(workptr, nxt); |
| 2708 | } |
| 2709 | |
| 2710 | static int io_close(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2711 | bool force_nonblock) |
| 2712 | { |
| 2713 | int ret; |
| 2714 | |
| 2715 | req->close.put_file = NULL; |
| 2716 | ret = __close_fd_get_file(req->close.fd, &req->close.put_file); |
| 2717 | if (ret < 0) |
| 2718 | return ret; |
| 2719 | |
| 2720 | /* if the file has a flush method, be safe and punt to async */ |
| 2721 | if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) { |
| 2722 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 2723 | goto eagain; |
| 2724 | } |
| 2725 | |
| 2726 | /* |
| 2727 | * No ->flush(), safely close from here and just punt the |
| 2728 | * fput() to async context. |
| 2729 | */ |
| 2730 | ret = filp_close(req->close.put_file, current->files); |
| 2731 | |
| 2732 | if (ret < 0) |
| 2733 | req_set_fail_links(req); |
| 2734 | io_cqring_add_event(req, ret); |
| 2735 | |
| 2736 | if (io_wq_current_is_worker()) { |
| 2737 | struct io_wq_work *old_work, *work; |
| 2738 | |
| 2739 | old_work = work = &req->work; |
| 2740 | io_close_finish(&work); |
| 2741 | if (work && work != old_work) |
| 2742 | *nxt = container_of(work, struct io_kiocb, work); |
| 2743 | return 0; |
| 2744 | } |
| 2745 | |
| 2746 | eagain: |
| 2747 | req->work.func = io_close_finish; |
| 2748 | return -EAGAIN; |
| 2749 | } |
| 2750 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2751 | 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] | 2752 | { |
| 2753 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2754 | |
| 2755 | if (!req->file) |
| 2756 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2757 | |
| 2758 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2759 | return -EINVAL; |
| 2760 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 2761 | return -EINVAL; |
| 2762 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2763 | req->sync.off = READ_ONCE(sqe->off); |
| 2764 | req->sync.len = READ_ONCE(sqe->len); |
| 2765 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2766 | return 0; |
| 2767 | } |
| 2768 | |
| 2769 | static void io_sync_file_range_finish(struct io_wq_work **workptr) |
| 2770 | { |
| 2771 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2772 | struct io_kiocb *nxt = NULL; |
| 2773 | int ret; |
| 2774 | |
| 2775 | if (io_req_cancelled(req)) |
| 2776 | return; |
| 2777 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2778 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2779 | req->sync.flags); |
| 2780 | if (ret < 0) |
| 2781 | req_set_fail_links(req); |
| 2782 | io_cqring_add_event(req, ret); |
| 2783 | io_put_req_find_next(req, &nxt); |
| 2784 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2785 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2786 | } |
| 2787 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2788 | 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] | 2789 | bool force_nonblock) |
| 2790 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2791 | struct io_wq_work *work, *old_work; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2792 | |
| 2793 | /* sync_file_range always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2794 | if (force_nonblock) { |
| 2795 | io_put_req(req); |
| 2796 | req->work.func = io_sync_file_range_finish; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2797 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2798 | } |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2799 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2800 | work = old_work = &req->work; |
| 2801 | io_sync_file_range_finish(&work); |
| 2802 | if (work && work != old_work) |
| 2803 | *nxt = container_of(work, struct io_kiocb, work); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2804 | return 0; |
| 2805 | } |
| 2806 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2807 | #if defined(CONFIG_NET) |
| 2808 | static void io_sendrecv_async(struct io_wq_work **workptr) |
| 2809 | { |
| 2810 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2811 | struct iovec *iov = NULL; |
| 2812 | |
| 2813 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2814 | iov = req->io->msg.iov; |
| 2815 | io_wq_submit_work(workptr); |
| 2816 | kfree(iov); |
| 2817 | } |
| 2818 | #endif |
| 2819 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2820 | 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] | 2821 | { |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2822 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2823 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2824 | struct io_async_ctx *io = req->io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2825 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2826 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 2827 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2828 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2829 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2830 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2831 | return 0; |
| 2832 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 2833 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2834 | return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2835 | &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2836 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2837 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2838 | #endif |
| 2839 | } |
| 2840 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2841 | static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2842 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2843 | { |
| 2844 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2845 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2846 | struct socket *sock; |
| 2847 | int ret; |
| 2848 | |
| 2849 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2850 | return -EINVAL; |
| 2851 | |
| 2852 | sock = sock_from_file(req->file, &ret); |
| 2853 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2854 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2855 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2856 | unsigned flags; |
| 2857 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2858 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2859 | kmsg = &req->io->msg; |
| 2860 | kmsg->msg.msg_name = &addr; |
| 2861 | /* if iov is set, it's allocated already */ |
| 2862 | if (!kmsg->iov) |
| 2863 | kmsg->iov = kmsg->fast_iov; |
| 2864 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2865 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2866 | struct io_sr_msg *sr = &req->sr_msg; |
| 2867 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2868 | kmsg = &io.msg; |
| 2869 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2870 | |
| 2871 | io.msg.iov = io.msg.fast_iov; |
| 2872 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 2873 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2874 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2875 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2876 | } |
| 2877 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2878 | flags = req->sr_msg.msg_flags; |
| 2879 | if (flags & MSG_DONTWAIT) |
| 2880 | req->flags |= REQ_F_NOWAIT; |
| 2881 | else if (force_nonblock) |
| 2882 | flags |= MSG_DONTWAIT; |
| 2883 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2884 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2885 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2886 | if (req->io) |
| 2887 | return -EAGAIN; |
| 2888 | if (io_alloc_async_ctx(req)) |
| 2889 | return -ENOMEM; |
| 2890 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 2891 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2892 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2893 | } |
| 2894 | if (ret == -ERESTARTSYS) |
| 2895 | ret = -EINTR; |
| 2896 | } |
| 2897 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2898 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2899 | kfree(kmsg->iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2900 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2901 | if (ret < 0) |
| 2902 | req_set_fail_links(req); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2903 | io_put_req_find_next(req, nxt); |
| 2904 | return 0; |
| 2905 | #else |
| 2906 | return -EOPNOTSUPP; |
| 2907 | #endif |
| 2908 | } |
| 2909 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2910 | static int io_send(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2911 | bool force_nonblock) |
| 2912 | { |
| 2913 | #if defined(CONFIG_NET) |
| 2914 | struct socket *sock; |
| 2915 | int ret; |
| 2916 | |
| 2917 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2918 | return -EINVAL; |
| 2919 | |
| 2920 | sock = sock_from_file(req->file, &ret); |
| 2921 | if (sock) { |
| 2922 | struct io_sr_msg *sr = &req->sr_msg; |
| 2923 | struct msghdr msg; |
| 2924 | struct iovec iov; |
| 2925 | unsigned flags; |
| 2926 | |
| 2927 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, |
| 2928 | &msg.msg_iter); |
| 2929 | if (ret) |
| 2930 | return ret; |
| 2931 | |
| 2932 | msg.msg_name = NULL; |
| 2933 | msg.msg_control = NULL; |
| 2934 | msg.msg_controllen = 0; |
| 2935 | msg.msg_namelen = 0; |
| 2936 | |
| 2937 | flags = req->sr_msg.msg_flags; |
| 2938 | if (flags & MSG_DONTWAIT) |
| 2939 | req->flags |= REQ_F_NOWAIT; |
| 2940 | else if (force_nonblock) |
| 2941 | flags |= MSG_DONTWAIT; |
| 2942 | |
| 2943 | ret = __sys_sendmsg_sock(sock, &msg, flags); |
| 2944 | if (force_nonblock && ret == -EAGAIN) |
| 2945 | return -EAGAIN; |
| 2946 | if (ret == -ERESTARTSYS) |
| 2947 | ret = -EINTR; |
| 2948 | } |
| 2949 | |
| 2950 | io_cqring_add_event(req, ret); |
| 2951 | if (ret < 0) |
| 2952 | req_set_fail_links(req); |
| 2953 | io_put_req_find_next(req, nxt); |
| 2954 | return 0; |
| 2955 | #else |
| 2956 | return -EOPNOTSUPP; |
| 2957 | #endif |
| 2958 | } |
| 2959 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2960 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 2961 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2962 | { |
| 2963 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2964 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2965 | struct io_async_ctx *io = req->io; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2966 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2967 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 2968 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2969 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2970 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2971 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2972 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 2973 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2974 | return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2975 | &io->msg.uaddr, &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2976 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2977 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2978 | #endif |
| 2979 | } |
| 2980 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2981 | static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2982 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2983 | { |
| 2984 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2985 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 2986 | struct socket *sock; |
| 2987 | int ret; |
| 2988 | |
| 2989 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2990 | return -EINVAL; |
| 2991 | |
| 2992 | sock = sock_from_file(req->file, &ret); |
| 2993 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2994 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2995 | struct sockaddr_storage addr; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 2996 | unsigned flags; |
| 2997 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2998 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2999 | kmsg = &req->io->msg; |
| 3000 | kmsg->msg.msg_name = &addr; |
| 3001 | /* if iov is set, it's allocated already */ |
| 3002 | if (!kmsg->iov) |
| 3003 | kmsg->iov = kmsg->fast_iov; |
| 3004 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3005 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3006 | struct io_sr_msg *sr = &req->sr_msg; |
| 3007 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3008 | kmsg = &io.msg; |
| 3009 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3010 | |
| 3011 | io.msg.iov = io.msg.fast_iov; |
| 3012 | ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3013 | sr->msg_flags, &io.msg.uaddr, |
| 3014 | &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3015 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3016 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3017 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3018 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3019 | flags = req->sr_msg.msg_flags; |
| 3020 | if (flags & MSG_DONTWAIT) |
| 3021 | req->flags |= REQ_F_NOWAIT; |
| 3022 | else if (force_nonblock) |
| 3023 | flags |= MSG_DONTWAIT; |
| 3024 | |
| 3025 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 3026 | kmsg->uaddr, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3027 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3028 | if (req->io) |
| 3029 | return -EAGAIN; |
| 3030 | if (io_alloc_async_ctx(req)) |
| 3031 | return -ENOMEM; |
| 3032 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 3033 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3034 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3035 | } |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 3036 | if (ret == -ERESTARTSYS) |
| 3037 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3038 | } |
| 3039 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3040 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3041 | kfree(kmsg->iov); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3042 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3043 | if (ret < 0) |
| 3044 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3045 | io_put_req_find_next(req, nxt); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3046 | return 0; |
| 3047 | #else |
| 3048 | return -EOPNOTSUPP; |
| 3049 | #endif |
| 3050 | } |
| 3051 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3052 | static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3053 | bool force_nonblock) |
| 3054 | { |
| 3055 | #if defined(CONFIG_NET) |
| 3056 | struct socket *sock; |
| 3057 | int ret; |
| 3058 | |
| 3059 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3060 | return -EINVAL; |
| 3061 | |
| 3062 | sock = sock_from_file(req->file, &ret); |
| 3063 | if (sock) { |
| 3064 | struct io_sr_msg *sr = &req->sr_msg; |
| 3065 | struct msghdr msg; |
| 3066 | struct iovec iov; |
| 3067 | unsigned flags; |
| 3068 | |
| 3069 | ret = import_single_range(READ, sr->buf, sr->len, &iov, |
| 3070 | &msg.msg_iter); |
| 3071 | if (ret) |
| 3072 | return ret; |
| 3073 | |
| 3074 | msg.msg_name = NULL; |
| 3075 | msg.msg_control = NULL; |
| 3076 | msg.msg_controllen = 0; |
| 3077 | msg.msg_namelen = 0; |
| 3078 | msg.msg_iocb = NULL; |
| 3079 | msg.msg_flags = 0; |
| 3080 | |
| 3081 | flags = req->sr_msg.msg_flags; |
| 3082 | if (flags & MSG_DONTWAIT) |
| 3083 | req->flags |= REQ_F_NOWAIT; |
| 3084 | else if (force_nonblock) |
| 3085 | flags |= MSG_DONTWAIT; |
| 3086 | |
| 3087 | ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags); |
| 3088 | if (force_nonblock && ret == -EAGAIN) |
| 3089 | return -EAGAIN; |
| 3090 | if (ret == -ERESTARTSYS) |
| 3091 | ret = -EINTR; |
| 3092 | } |
| 3093 | |
| 3094 | io_cqring_add_event(req, ret); |
| 3095 | if (ret < 0) |
| 3096 | req_set_fail_links(req); |
| 3097 | io_put_req_find_next(req, nxt); |
| 3098 | return 0; |
| 3099 | #else |
| 3100 | return -EOPNOTSUPP; |
| 3101 | #endif |
| 3102 | } |
| 3103 | |
| 3104 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3105 | 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] | 3106 | { |
| 3107 | #if defined(CONFIG_NET) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3108 | struct io_accept *accept = &req->accept; |
| 3109 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3110 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3111 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 3112 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3113 | return -EINVAL; |
| 3114 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 3115 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3116 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3117 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3118 | return 0; |
| 3119 | #else |
| 3120 | return -EOPNOTSUPP; |
| 3121 | #endif |
| 3122 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3123 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3124 | #if defined(CONFIG_NET) |
| 3125 | static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3126 | bool force_nonblock) |
| 3127 | { |
| 3128 | struct io_accept *accept = &req->accept; |
| 3129 | unsigned file_flags; |
| 3130 | int ret; |
| 3131 | |
| 3132 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3133 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
| 3134 | accept->addr_len, accept->flags); |
| 3135 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3136 | return -EAGAIN; |
Jens Axboe | 8e3cca1 | 2019-11-09 19:52:33 -0700 | [diff] [blame] | 3137 | if (ret == -ERESTARTSYS) |
| 3138 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3139 | if (ret < 0) |
| 3140 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3141 | io_cqring_add_event(req, ret); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3142 | io_put_req_find_next(req, nxt); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3143 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3144 | } |
| 3145 | |
| 3146 | static void io_accept_finish(struct io_wq_work **workptr) |
| 3147 | { |
| 3148 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3149 | struct io_kiocb *nxt = NULL; |
| 3150 | |
| 3151 | if (io_req_cancelled(req)) |
| 3152 | return; |
| 3153 | __io_accept(req, &nxt, false); |
| 3154 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3155 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3156 | } |
| 3157 | #endif |
| 3158 | |
| 3159 | static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3160 | bool force_nonblock) |
| 3161 | { |
| 3162 | #if defined(CONFIG_NET) |
| 3163 | int ret; |
| 3164 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3165 | ret = __io_accept(req, nxt, force_nonblock); |
| 3166 | if (ret == -EAGAIN && force_nonblock) { |
| 3167 | req->work.func = io_accept_finish; |
| 3168 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 3169 | io_put_req(req); |
| 3170 | return -EAGAIN; |
| 3171 | } |
| 3172 | return 0; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3173 | #else |
| 3174 | return -EOPNOTSUPP; |
| 3175 | #endif |
| 3176 | } |
| 3177 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3178 | 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] | 3179 | { |
| 3180 | #if defined(CONFIG_NET) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3181 | struct io_connect *conn = &req->connect; |
| 3182 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3183 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3184 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3185 | return -EINVAL; |
| 3186 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 3187 | return -EINVAL; |
| 3188 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3189 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3190 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 3191 | |
| 3192 | if (!io) |
| 3193 | return 0; |
| 3194 | |
| 3195 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3196 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3197 | #else |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3198 | return -EOPNOTSUPP; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3199 | #endif |
| 3200 | } |
| 3201 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3202 | static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3203 | bool force_nonblock) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3204 | { |
| 3205 | #if defined(CONFIG_NET) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3206 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3207 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3208 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3209 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3210 | if (req->io) { |
| 3211 | io = req->io; |
| 3212 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3213 | ret = move_addr_to_kernel(req->connect.addr, |
| 3214 | req->connect.addr_len, |
| 3215 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3216 | if (ret) |
| 3217 | goto out; |
| 3218 | io = &__io; |
| 3219 | } |
| 3220 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3221 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3222 | |
| 3223 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 3224 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 3225 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3226 | if (req->io) |
| 3227 | return -EAGAIN; |
| 3228 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3229 | ret = -ENOMEM; |
| 3230 | goto out; |
| 3231 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3232 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3233 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3234 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3235 | if (ret == -ERESTARTSYS) |
| 3236 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3237 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3238 | if (ret < 0) |
| 3239 | req_set_fail_links(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3240 | io_cqring_add_event(req, ret); |
| 3241 | io_put_req_find_next(req, nxt); |
| 3242 | return 0; |
| 3243 | #else |
| 3244 | return -EOPNOTSUPP; |
| 3245 | #endif |
| 3246 | } |
| 3247 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3248 | static void io_poll_remove_one(struct io_kiocb *req) |
| 3249 | { |
| 3250 | struct io_poll_iocb *poll = &req->poll; |
| 3251 | |
| 3252 | spin_lock(&poll->head->lock); |
| 3253 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3254 | if (!list_empty(&poll->wait.entry)) { |
| 3255 | list_del_init(&poll->wait.entry); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3256 | io_queue_async_work(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3257 | } |
| 3258 | spin_unlock(&poll->head->lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3259 | hash_del(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 3263 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3264 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3265 | struct io_kiocb *req; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3266 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3267 | |
| 3268 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3269 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 3270 | struct hlist_head *list; |
| 3271 | |
| 3272 | list = &ctx->cancel_hash[i]; |
| 3273 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
| 3274 | io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3275 | } |
| 3276 | spin_unlock_irq(&ctx->completion_lock); |
| 3277 | } |
| 3278 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3279 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 3280 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3281 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3282 | struct io_kiocb *req; |
| 3283 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3284 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 3285 | hlist_for_each_entry(req, list, hash_node) { |
| 3286 | if (sqe_addr == req->user_data) { |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3287 | io_poll_remove_one(req); |
| 3288 | return 0; |
| 3289 | } |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | return -ENOENT; |
| 3293 | } |
| 3294 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3295 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 3296 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3297 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3298 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3299 | return -EINVAL; |
| 3300 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3301 | sqe->poll_events) |
| 3302 | return -EINVAL; |
| 3303 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3304 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3305 | return 0; |
| 3306 | } |
| 3307 | |
| 3308 | /* |
| 3309 | * Find a running poll command that matches one specified in sqe->addr, |
| 3310 | * and remove it if found. |
| 3311 | */ |
| 3312 | static int io_poll_remove(struct io_kiocb *req) |
| 3313 | { |
| 3314 | struct io_ring_ctx *ctx = req->ctx; |
| 3315 | u64 addr; |
| 3316 | int ret; |
| 3317 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3318 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3319 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3320 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3321 | spin_unlock_irq(&ctx->completion_lock); |
| 3322 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3323 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3324 | if (ret < 0) |
| 3325 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3326 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3327 | return 0; |
| 3328 | } |
| 3329 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3330 | 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] | 3331 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3332 | struct io_ring_ctx *ctx = req->ctx; |
| 3333 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3334 | req->poll.done = true; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3335 | if (error) |
| 3336 | io_cqring_fill_event(req, error); |
| 3337 | else |
| 3338 | io_cqring_fill_event(req, mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3339 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3340 | } |
| 3341 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3342 | static void io_poll_complete_work(struct io_wq_work **workptr) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3343 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3344 | struct io_wq_work *work = *workptr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3345 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 3346 | struct io_poll_iocb *poll = &req->poll; |
| 3347 | struct poll_table_struct pt = { ._key = poll->events }; |
| 3348 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3349 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3350 | __poll_t mask = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3351 | int ret = 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3352 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3353 | if (work->flags & IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3354 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3355 | ret = -ECANCELED; |
| 3356 | } else if (READ_ONCE(poll->canceled)) { |
| 3357 | ret = -ECANCELED; |
| 3358 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3359 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3360 | if (ret != -ECANCELED) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3361 | mask = vfs_poll(poll->file, &pt) & poll->events; |
| 3362 | |
| 3363 | /* |
| 3364 | * Note that ->ki_cancel callers also delete iocb from active_reqs after |
| 3365 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to |
| 3366 | * synchronize with them. In the cancellation case the list_del_init |
| 3367 | * itself is not actually needed, but harmless so we keep it in to |
| 3368 | * avoid further branches in the fast path. |
| 3369 | */ |
| 3370 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3371 | if (!mask && ret != -ECANCELED) { |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3372 | add_wait_queue(poll->head, &poll->wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3373 | spin_unlock_irq(&ctx->completion_lock); |
| 3374 | return; |
| 3375 | } |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3376 | hash_del(&req->hash_node); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3377 | io_poll_complete(req, mask, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3378 | spin_unlock_irq(&ctx->completion_lock); |
| 3379 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3380 | io_cqring_ev_posted(ctx); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3381 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3382 | if (ret < 0) |
| 3383 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3384 | io_put_req_find_next(req, &nxt); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3385 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3386 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3387 | } |
| 3388 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3389 | static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes) |
| 3390 | { |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3391 | struct io_kiocb *req, *tmp; |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3392 | struct req_batch rb; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3393 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 3394 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3395 | spin_lock_irq(&ctx->completion_lock); |
| 3396 | llist_for_each_entry_safe(req, tmp, nodes, llist_node) { |
| 3397 | hash_del(&req->hash_node); |
| 3398 | io_poll_complete(req, req->result, 0); |
| 3399 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3400 | if (refcount_dec_and_test(&req->refs) && |
| 3401 | !io_req_multi_free(&rb, req)) { |
| 3402 | req->flags |= REQ_F_COMP_LOCKED; |
| 3403 | io_free_req(req); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3404 | } |
| 3405 | } |
| 3406 | spin_unlock_irq(&ctx->completion_lock); |
| 3407 | |
| 3408 | io_cqring_ev_posted(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3409 | io_free_req_many(ctx, &rb); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | static void io_poll_flush(struct io_wq_work **workptr) |
| 3413 | { |
| 3414 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3415 | struct llist_node *nodes; |
| 3416 | |
| 3417 | nodes = llist_del_all(&req->ctx->poll_llist); |
| 3418 | if (nodes) |
| 3419 | __io_poll_flush(req->ctx, nodes); |
| 3420 | } |
| 3421 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3422 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 3423 | void *key) |
| 3424 | { |
Jens Axboe | e944475 | 2019-11-26 15:02:04 -0700 | [diff] [blame] | 3425 | struct io_poll_iocb *poll = wait->private; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3426 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); |
| 3427 | struct io_ring_ctx *ctx = req->ctx; |
| 3428 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3429 | |
| 3430 | /* for instances that support it check for an event match first: */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3431 | if (mask && !(mask & poll->events)) |
| 3432 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3433 | |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3434 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3435 | |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 3436 | /* |
| 3437 | * Run completion inline if we can. We're using trylock here because |
| 3438 | * we are violating the completion_lock -> poll wq lock ordering. |
| 3439 | * If we have a link timeout we're going to need the completion_lock |
| 3440 | * for finalizing the request, mark us as having grabbed that already. |
| 3441 | */ |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3442 | if (mask) { |
| 3443 | unsigned long flags; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3444 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3445 | if (llist_empty(&ctx->poll_llist) && |
| 3446 | spin_trylock_irqsave(&ctx->completion_lock, flags)) { |
| 3447 | hash_del(&req->hash_node); |
| 3448 | io_poll_complete(req, mask, 0); |
| 3449 | req->flags |= REQ_F_COMP_LOCKED; |
| 3450 | io_put_req(req); |
| 3451 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3452 | |
| 3453 | io_cqring_ev_posted(ctx); |
| 3454 | req = NULL; |
| 3455 | } else { |
| 3456 | req->result = mask; |
| 3457 | req->llist_node.next = NULL; |
| 3458 | /* if the list wasn't empty, we're done */ |
| 3459 | if (!llist_add(&req->llist_node, &ctx->poll_llist)) |
| 3460 | req = NULL; |
| 3461 | else |
| 3462 | req->work.func = io_poll_flush; |
| 3463 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3464 | } |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3465 | if (req) |
| 3466 | io_queue_async_work(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3467 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3468 | return 1; |
| 3469 | } |
| 3470 | |
| 3471 | struct io_poll_table { |
| 3472 | struct poll_table_struct pt; |
| 3473 | struct io_kiocb *req; |
| 3474 | int error; |
| 3475 | }; |
| 3476 | |
| 3477 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 3478 | struct poll_table_struct *p) |
| 3479 | { |
| 3480 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 3481 | |
| 3482 | if (unlikely(pt->req->poll.head)) { |
| 3483 | pt->error = -EINVAL; |
| 3484 | return; |
| 3485 | } |
| 3486 | |
| 3487 | pt->error = 0; |
| 3488 | pt->req->poll.head = head; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3489 | add_wait_queue(head, &pt->req->poll.wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3490 | } |
| 3491 | |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3492 | static void io_poll_req_insert(struct io_kiocb *req) |
| 3493 | { |
| 3494 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3495 | struct hlist_head *list; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3496 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3497 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 3498 | hlist_add_head(&req->hash_node, list); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3499 | } |
| 3500 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3501 | 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] | 3502 | { |
| 3503 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3504 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3505 | |
| 3506 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3507 | return -EINVAL; |
| 3508 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 3509 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3510 | if (!poll->file) |
| 3511 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3512 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3513 | events = READ_ONCE(sqe->poll_events); |
| 3514 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3515 | return 0; |
| 3516 | } |
| 3517 | |
| 3518 | static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3519 | { |
| 3520 | struct io_poll_iocb *poll = &req->poll; |
| 3521 | struct io_ring_ctx *ctx = req->ctx; |
| 3522 | struct io_poll_table ipt; |
| 3523 | bool cancel = false; |
| 3524 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3525 | |
| 3526 | INIT_IO_WORK(&req->work, io_poll_complete_work); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3527 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3528 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3529 | poll->head = NULL; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3530 | poll->done = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3531 | poll->canceled = false; |
| 3532 | |
| 3533 | ipt.pt._qproc = io_poll_queue_proc; |
| 3534 | ipt.pt._key = poll->events; |
| 3535 | ipt.req = req; |
| 3536 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ |
| 3537 | |
| 3538 | /* initialized the list so that we can do list_empty checks */ |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3539 | INIT_LIST_HEAD(&poll->wait.entry); |
| 3540 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); |
| 3541 | poll->wait.private = poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3542 | |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 3543 | INIT_LIST_HEAD(&req->list); |
| 3544 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3545 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3546 | |
| 3547 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3548 | if (likely(poll->head)) { |
| 3549 | spin_lock(&poll->head->lock); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3550 | if (unlikely(list_empty(&poll->wait.entry))) { |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3551 | if (ipt.error) |
| 3552 | cancel = true; |
| 3553 | ipt.error = 0; |
| 3554 | mask = 0; |
| 3555 | } |
| 3556 | if (mask || ipt.error) |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3557 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3558 | else if (cancel) |
| 3559 | WRITE_ONCE(poll->canceled, true); |
| 3560 | else if (!poll->done) /* actually waiting for an event */ |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3561 | io_poll_req_insert(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3562 | spin_unlock(&poll->head->lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3563 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3564 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3565 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3566 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3567 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3568 | spin_unlock_irq(&ctx->completion_lock); |
| 3569 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3570 | if (mask) { |
| 3571 | io_cqring_ev_posted(ctx); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3572 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3573 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3574 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3575 | } |
| 3576 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3577 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 3578 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3579 | struct io_timeout_data *data = container_of(timer, |
| 3580 | struct io_timeout_data, timer); |
| 3581 | struct io_kiocb *req = data->req; |
| 3582 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3583 | unsigned long flags; |
| 3584 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3585 | atomic_inc(&ctx->cq_timeouts); |
| 3586 | |
| 3587 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3588 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3589 | * We could be racing with timeout deletion. If the list is empty, |
| 3590 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3591 | */ |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3592 | if (!list_empty(&req->list)) { |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3593 | struct io_kiocb *prev; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3594 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3595 | /* |
| 3596 | * Adjust the reqs sequence before the current one because it |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 3597 | * will consume a slot in the cq_ring and the cq_tail |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3598 | * pointer will be increased, otherwise other timeout reqs may |
| 3599 | * return in advance without waiting for enough wait_nr. |
| 3600 | */ |
| 3601 | prev = req; |
| 3602 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 3603 | prev->sequence++; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3604 | list_del_init(&req->list); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3605 | } |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3606 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3607 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3608 | io_commit_cqring(ctx); |
| 3609 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3610 | |
| 3611 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3612 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3613 | io_put_req(req); |
| 3614 | return HRTIMER_NORESTART; |
| 3615 | } |
| 3616 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3617 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 3618 | { |
| 3619 | struct io_kiocb *req; |
| 3620 | int ret = -ENOENT; |
| 3621 | |
| 3622 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 3623 | if (user_data == req->user_data) { |
| 3624 | list_del_init(&req->list); |
| 3625 | ret = 0; |
| 3626 | break; |
| 3627 | } |
| 3628 | } |
| 3629 | |
| 3630 | if (ret == -ENOENT) |
| 3631 | return ret; |
| 3632 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3633 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3634 | if (ret == -1) |
| 3635 | return -EALREADY; |
| 3636 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3637 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3638 | io_cqring_fill_event(req, -ECANCELED); |
| 3639 | io_put_req(req); |
| 3640 | return 0; |
| 3641 | } |
| 3642 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3643 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 3644 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3645 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3646 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3647 | return -EINVAL; |
| 3648 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 3649 | return -EINVAL; |
| 3650 | |
| 3651 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 3652 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 3653 | if (req->timeout.flags) |
| 3654 | return -EINVAL; |
| 3655 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3656 | return 0; |
| 3657 | } |
| 3658 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3659 | /* |
| 3660 | * Remove or update an existing timeout command |
| 3661 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3662 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3663 | { |
| 3664 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3665 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3666 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3667 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3668 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3669 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3670 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3671 | io_commit_cqring(ctx); |
| 3672 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3673 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3674 | if (ret < 0) |
| 3675 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3676 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3677 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3678 | } |
| 3679 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3680 | 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] | 3681 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3682 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3683 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3684 | unsigned flags; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3685 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3686 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3687 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3688 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3689 | return -EINVAL; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3690 | if (sqe->off && is_timeout_link) |
| 3691 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3692 | flags = READ_ONCE(sqe->timeout_flags); |
| 3693 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3694 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 3695 | |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3696 | req->timeout.count = READ_ONCE(sqe->off); |
| 3697 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3698 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3699 | return -ENOMEM; |
| 3700 | |
| 3701 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3702 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3703 | req->flags |= REQ_F_TIMEOUT; |
| 3704 | |
| 3705 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3706 | return -EFAULT; |
| 3707 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3708 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3709 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3710 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3711 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3712 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3713 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 3714 | return 0; |
| 3715 | } |
| 3716 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3717 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3718 | { |
| 3719 | unsigned count; |
| 3720 | struct io_ring_ctx *ctx = req->ctx; |
| 3721 | struct io_timeout_data *data; |
| 3722 | struct list_head *entry; |
| 3723 | unsigned span = 0; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3724 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3725 | data = &req->io->timeout; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3726 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3727 | /* |
| 3728 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3729 | * timeout event to be satisfied. If it isn't set, then this is |
| 3730 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3731 | */ |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3732 | count = req->timeout.count; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3733 | if (!count) { |
| 3734 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 3735 | spin_lock_irq(&ctx->completion_lock); |
| 3736 | entry = ctx->timeout_list.prev; |
| 3737 | goto add; |
| 3738 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3739 | |
| 3740 | req->sequence = ctx->cached_sq_head + count - 1; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3741 | data->seq_offset = count; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3742 | |
| 3743 | /* |
| 3744 | * Insertion sort, ensuring the first entry in the list is always |
| 3745 | * the one we need first. |
| 3746 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3747 | spin_lock_irq(&ctx->completion_lock); |
| 3748 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 3749 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3750 | unsigned nxt_sq_head; |
| 3751 | long long tmp, tmp_nxt; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3752 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3753 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3754 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 3755 | continue; |
| 3756 | |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3757 | /* |
| 3758 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 3759 | * long to store it. |
| 3760 | */ |
| 3761 | tmp = (long long)ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 3762 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
| 3763 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3764 | |
| 3765 | /* |
| 3766 | * cached_sq_head may overflow, and it will never overflow twice |
| 3767 | * once there is some timeout req still be valid. |
| 3768 | */ |
| 3769 | if (ctx->cached_sq_head < nxt_sq_head) |
yangerkun | 8b07a65 | 2019-10-17 12:12:35 +0800 | [diff] [blame] | 3770 | tmp += UINT_MAX; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3771 | |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3772 | if (tmp > tmp_nxt) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3773 | break; |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3774 | |
| 3775 | /* |
| 3776 | * Sequence of reqs after the insert one and itself should |
| 3777 | * be adjusted because each timeout req consumes a slot. |
| 3778 | */ |
| 3779 | span++; |
| 3780 | nxt->sequence++; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3781 | } |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3782 | req->sequence -= span; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3783 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3784 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3785 | data->timer.function = io_timeout_fn; |
| 3786 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3787 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3788 | return 0; |
| 3789 | } |
| 3790 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3791 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3792 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3793 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3794 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3795 | return req->user_data == (unsigned long) data; |
| 3796 | } |
| 3797 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3798 | 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] | 3799 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3800 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3801 | int ret = 0; |
| 3802 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3803 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
| 3804 | switch (cancel_ret) { |
| 3805 | case IO_WQ_CANCEL_OK: |
| 3806 | ret = 0; |
| 3807 | break; |
| 3808 | case IO_WQ_CANCEL_RUNNING: |
| 3809 | ret = -EALREADY; |
| 3810 | break; |
| 3811 | case IO_WQ_CANCEL_NOTFOUND: |
| 3812 | ret = -ENOENT; |
| 3813 | break; |
| 3814 | } |
| 3815 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3816 | return ret; |
| 3817 | } |
| 3818 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3819 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 3820 | struct io_kiocb *req, __u64 sqe_addr, |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3821 | struct io_kiocb **nxt, int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3822 | { |
| 3823 | unsigned long flags; |
| 3824 | int ret; |
| 3825 | |
| 3826 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 3827 | if (ret != -ENOENT) { |
| 3828 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3829 | goto done; |
| 3830 | } |
| 3831 | |
| 3832 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3833 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 3834 | if (ret != -ENOENT) |
| 3835 | goto done; |
| 3836 | ret = io_poll_cancel(ctx, sqe_addr); |
| 3837 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3838 | if (!ret) |
| 3839 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3840 | io_cqring_fill_event(req, ret); |
| 3841 | io_commit_cqring(ctx); |
| 3842 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3843 | io_cqring_ev_posted(ctx); |
| 3844 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3845 | if (ret < 0) |
| 3846 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3847 | io_put_req_find_next(req, nxt); |
| 3848 | } |
| 3849 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3850 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 3851 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3852 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3853 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3854 | return -EINVAL; |
| 3855 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 3856 | sqe->cancel_flags) |
| 3857 | return -EINVAL; |
| 3858 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3859 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 3860 | return 0; |
| 3861 | } |
| 3862 | |
| 3863 | static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3864 | { |
| 3865 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3866 | |
| 3867 | io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3868 | return 0; |
| 3869 | } |
| 3870 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 3871 | static int io_files_update_prep(struct io_kiocb *req, |
| 3872 | const struct io_uring_sqe *sqe) |
| 3873 | { |
| 3874 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) |
| 3875 | return -EINVAL; |
| 3876 | |
| 3877 | req->files_update.offset = READ_ONCE(sqe->off); |
| 3878 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 3879 | if (!req->files_update.nr_args) |
| 3880 | return -EINVAL; |
| 3881 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 3882 | return 0; |
| 3883 | } |
| 3884 | |
| 3885 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) |
| 3886 | { |
| 3887 | struct io_ring_ctx *ctx = req->ctx; |
| 3888 | struct io_uring_files_update up; |
| 3889 | int ret; |
| 3890 | |
| 3891 | if (force_nonblock) { |
| 3892 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 3893 | return -EAGAIN; |
| 3894 | } |
| 3895 | |
| 3896 | up.offset = req->files_update.offset; |
| 3897 | up.fds = req->files_update.arg; |
| 3898 | |
| 3899 | mutex_lock(&ctx->uring_lock); |
| 3900 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 3901 | mutex_unlock(&ctx->uring_lock); |
| 3902 | |
| 3903 | if (ret < 0) |
| 3904 | req_set_fail_links(req); |
| 3905 | io_cqring_add_event(req, ret); |
| 3906 | io_put_req(req); |
| 3907 | return 0; |
| 3908 | } |
| 3909 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3910 | static int io_req_defer_prep(struct io_kiocb *req, |
| 3911 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3912 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3913 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3914 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3915 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3916 | case IORING_OP_NOP: |
| 3917 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3918 | case IORING_OP_READV: |
| 3919 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3920 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3921 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3922 | break; |
| 3923 | case IORING_OP_WRITEV: |
| 3924 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3925 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3926 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3927 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3928 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3929 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3930 | break; |
| 3931 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3932 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3933 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3934 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3935 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3936 | break; |
| 3937 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3938 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3939 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3940 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3941 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3942 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3943 | break; |
| 3944 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3945 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3946 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3947 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3948 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3949 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3950 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3951 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3952 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3953 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3954 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3955 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3956 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3957 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3958 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3959 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3960 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3961 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3962 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3963 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3964 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3965 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 3966 | case IORING_OP_FALLOCATE: |
| 3967 | ret = io_fallocate_prep(req, sqe); |
| 3968 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 3969 | case IORING_OP_OPENAT: |
| 3970 | ret = io_openat_prep(req, sqe); |
| 3971 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 3972 | case IORING_OP_CLOSE: |
| 3973 | ret = io_close_prep(req, sqe); |
| 3974 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 3975 | case IORING_OP_FILES_UPDATE: |
| 3976 | ret = io_files_update_prep(req, sqe); |
| 3977 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 3978 | case IORING_OP_STATX: |
| 3979 | ret = io_statx_prep(req, sqe); |
| 3980 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 3981 | case IORING_OP_FADVISE: |
| 3982 | ret = io_fadvise_prep(req, sqe); |
| 3983 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 3984 | case IORING_OP_MADVISE: |
| 3985 | ret = io_madvise_prep(req, sqe); |
| 3986 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3987 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3988 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 3989 | req->opcode); |
| 3990 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3991 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3992 | } |
| 3993 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3994 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3995 | } |
| 3996 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3997 | 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] | 3998 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3999 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4000 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4001 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4002 | /* Still need defer if there is pending req in defer list. */ |
| 4003 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4004 | return 0; |
| 4005 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4006 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4007 | return -EAGAIN; |
| 4008 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4009 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4010 | if (ret < 0) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4011 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4012 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4013 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4014 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4015 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4016 | return 0; |
| 4017 | } |
| 4018 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 4019 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4020 | list_add_tail(&req->list, &ctx->defer_list); |
| 4021 | spin_unlock_irq(&ctx->completion_lock); |
| 4022 | return -EIOCBQUEUED; |
| 4023 | } |
| 4024 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4025 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4026 | struct io_kiocb **nxt, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4027 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4028 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4029 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4030 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4031 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4032 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4033 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4034 | break; |
| 4035 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4036 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4037 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4038 | if (sqe) { |
| 4039 | ret = io_read_prep(req, sqe, force_nonblock); |
| 4040 | if (ret < 0) |
| 4041 | break; |
| 4042 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4043 | ret = io_read(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4044 | break; |
| 4045 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4046 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4047 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4048 | if (sqe) { |
| 4049 | ret = io_write_prep(req, sqe, force_nonblock); |
| 4050 | if (ret < 0) |
| 4051 | break; |
| 4052 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4053 | ret = io_write(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4054 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4055 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4056 | if (sqe) { |
| 4057 | ret = io_prep_fsync(req, sqe); |
| 4058 | if (ret < 0) |
| 4059 | break; |
| 4060 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4061 | ret = io_fsync(req, nxt, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4062 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4063 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4064 | if (sqe) { |
| 4065 | ret = io_poll_add_prep(req, sqe); |
| 4066 | if (ret) |
| 4067 | break; |
| 4068 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4069 | ret = io_poll_add(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4070 | break; |
| 4071 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4072 | if (sqe) { |
| 4073 | ret = io_poll_remove_prep(req, sqe); |
| 4074 | if (ret < 0) |
| 4075 | break; |
| 4076 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4077 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4078 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4079 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4080 | if (sqe) { |
| 4081 | ret = io_prep_sfr(req, sqe); |
| 4082 | if (ret < 0) |
| 4083 | break; |
| 4084 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4085 | ret = io_sync_file_range(req, nxt, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4086 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4087 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4088 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4089 | if (sqe) { |
| 4090 | ret = io_sendmsg_prep(req, sqe); |
| 4091 | if (ret < 0) |
| 4092 | break; |
| 4093 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4094 | if (req->opcode == IORING_OP_SENDMSG) |
| 4095 | ret = io_sendmsg(req, nxt, force_nonblock); |
| 4096 | else |
| 4097 | ret = io_send(req, nxt, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4098 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4099 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4100 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4101 | if (sqe) { |
| 4102 | ret = io_recvmsg_prep(req, sqe); |
| 4103 | if (ret) |
| 4104 | break; |
| 4105 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4106 | if (req->opcode == IORING_OP_RECVMSG) |
| 4107 | ret = io_recvmsg(req, nxt, force_nonblock); |
| 4108 | else |
| 4109 | ret = io_recv(req, nxt, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4110 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4111 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4112 | if (sqe) { |
| 4113 | ret = io_timeout_prep(req, sqe, false); |
| 4114 | if (ret) |
| 4115 | break; |
| 4116 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4117 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4118 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4119 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4120 | if (sqe) { |
| 4121 | ret = io_timeout_remove_prep(req, sqe); |
| 4122 | if (ret) |
| 4123 | break; |
| 4124 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4125 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4126 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4127 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4128 | if (sqe) { |
| 4129 | ret = io_accept_prep(req, sqe); |
| 4130 | if (ret) |
| 4131 | break; |
| 4132 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4133 | ret = io_accept(req, nxt, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4134 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4135 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4136 | if (sqe) { |
| 4137 | ret = io_connect_prep(req, sqe); |
| 4138 | if (ret) |
| 4139 | break; |
| 4140 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4141 | ret = io_connect(req, nxt, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4142 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4143 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4144 | if (sqe) { |
| 4145 | ret = io_async_cancel_prep(req, sqe); |
| 4146 | if (ret) |
| 4147 | break; |
| 4148 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4149 | ret = io_async_cancel(req, nxt); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4150 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4151 | case IORING_OP_FALLOCATE: |
| 4152 | if (sqe) { |
| 4153 | ret = io_fallocate_prep(req, sqe); |
| 4154 | if (ret) |
| 4155 | break; |
| 4156 | } |
| 4157 | ret = io_fallocate(req, nxt, force_nonblock); |
| 4158 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4159 | case IORING_OP_OPENAT: |
| 4160 | if (sqe) { |
| 4161 | ret = io_openat_prep(req, sqe); |
| 4162 | if (ret) |
| 4163 | break; |
| 4164 | } |
| 4165 | ret = io_openat(req, nxt, force_nonblock); |
| 4166 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4167 | case IORING_OP_CLOSE: |
| 4168 | if (sqe) { |
| 4169 | ret = io_close_prep(req, sqe); |
| 4170 | if (ret) |
| 4171 | break; |
| 4172 | } |
| 4173 | ret = io_close(req, nxt, force_nonblock); |
| 4174 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4175 | case IORING_OP_FILES_UPDATE: |
| 4176 | if (sqe) { |
| 4177 | ret = io_files_update_prep(req, sqe); |
| 4178 | if (ret) |
| 4179 | break; |
| 4180 | } |
| 4181 | ret = io_files_update(req, force_nonblock); |
| 4182 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4183 | case IORING_OP_STATX: |
| 4184 | if (sqe) { |
| 4185 | ret = io_statx_prep(req, sqe); |
| 4186 | if (ret) |
| 4187 | break; |
| 4188 | } |
| 4189 | ret = io_statx(req, nxt, force_nonblock); |
| 4190 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4191 | case IORING_OP_FADVISE: |
| 4192 | if (sqe) { |
| 4193 | ret = io_fadvise_prep(req, sqe); |
| 4194 | if (ret) |
| 4195 | break; |
| 4196 | } |
| 4197 | ret = io_fadvise(req, nxt, force_nonblock); |
| 4198 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4199 | case IORING_OP_MADVISE: |
| 4200 | if (sqe) { |
| 4201 | ret = io_madvise_prep(req, sqe); |
| 4202 | if (ret) |
| 4203 | break; |
| 4204 | } |
| 4205 | ret = io_madvise(req, nxt, force_nonblock); |
| 4206 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4207 | default: |
| 4208 | ret = -EINVAL; |
| 4209 | break; |
| 4210 | } |
| 4211 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4212 | if (ret) |
| 4213 | return ret; |
| 4214 | |
| 4215 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4216 | const bool in_async = io_wq_current_is_worker(); |
| 4217 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4218 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4219 | return -EAGAIN; |
| 4220 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4221 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 4222 | if (in_async) |
| 4223 | mutex_lock(&ctx->uring_lock); |
| 4224 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4225 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4226 | |
| 4227 | if (in_async) |
| 4228 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4229 | } |
| 4230 | |
| 4231 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4232 | } |
| 4233 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4234 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4235 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4236 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4237 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4238 | struct io_kiocb *nxt = NULL; |
| 4239 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4240 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4241 | /* if NO_CANCEL is set, we must still run the work */ |
| 4242 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 4243 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4244 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4245 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4246 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4247 | if (!ret) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4248 | req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0; |
| 4249 | req->in_async = true; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4250 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4251 | ret = io_issue_sqe(req, NULL, &nxt, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4252 | /* |
| 4253 | * We can get EAGAIN for polled IO even though we're |
| 4254 | * forcing a sync submission from here, since we can't |
| 4255 | * wait for request slots on the block side. |
| 4256 | */ |
| 4257 | if (ret != -EAGAIN) |
| 4258 | break; |
| 4259 | cond_resched(); |
| 4260 | } while (1); |
| 4261 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4262 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4263 | /* drop submission reference */ |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 4264 | io_put_req(req); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4265 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4266 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4267 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4268 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4269 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4270 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4271 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4272 | /* if a dependent link is ready, pass it back */ |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4273 | if (!ret && nxt) |
| 4274 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4275 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4276 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4277 | static int io_req_needs_file(struct io_kiocb *req, int fd) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4278 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4279 | if (!io_op_defs[req->opcode].needs_file) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4280 | return 0; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4281 | if (fd == -1 && io_op_defs[req->opcode].fd_non_neg) |
| 4282 | return 0; |
| 4283 | return 1; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4284 | } |
| 4285 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4286 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 4287 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4288 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4289 | struct fixed_file_table *table; |
| 4290 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4291 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
| 4292 | return table->files[index & IORING_FILE_TABLE_MASK];; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4293 | } |
| 4294 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4295 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
| 4296 | const struct io_uring_sqe *sqe) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4297 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4298 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4299 | unsigned flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4300 | int fd; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4301 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4302 | flags = READ_ONCE(sqe->flags); |
| 4303 | fd = READ_ONCE(sqe->fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4304 | |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4305 | if (flags & IOSQE_IO_DRAIN) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4306 | req->flags |= REQ_F_IO_DRAIN; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4307 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4308 | if (!io_req_needs_file(req, fd)) |
| 4309 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4310 | |
| 4311 | if (flags & IOSQE_FIXED_FILE) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4312 | if (unlikely(!ctx->file_data || |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4313 | (unsigned) fd >= ctx->nr_user_files)) |
| 4314 | return -EBADF; |
Jens Axboe | b762012 | 2019-10-26 07:22:55 -0600 | [diff] [blame] | 4315 | fd = array_index_nospec(fd, ctx->nr_user_files); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4316 | req->file = io_file_from_index(ctx, fd); |
| 4317 | if (!req->file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4318 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4319 | req->flags |= REQ_F_FIXED_FILE; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4320 | percpu_ref_get(&ctx->file_data->refs); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4321 | } else { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4322 | if (req->needs_fixed_file) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4323 | return -EBADF; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 4324 | trace_io_uring_file_get(ctx, fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4325 | req->file = io_file_get(state, fd); |
| 4326 | if (unlikely(!req->file)) |
| 4327 | return -EBADF; |
| 4328 | } |
| 4329 | |
| 4330 | return 0; |
| 4331 | } |
| 4332 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4333 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4334 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4335 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4336 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4337 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4338 | if (!req->ring_file) |
| 4339 | return -EBADF; |
| 4340 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4341 | rcu_read_lock(); |
| 4342 | spin_lock_irq(&ctx->inflight_lock); |
| 4343 | /* |
| 4344 | * We use the f_ops->flush() handler to ensure that we can flush |
| 4345 | * out work accessing these files if the fd is closed. Check if |
| 4346 | * the fd has changed since we started down this path, and disallow |
| 4347 | * this operation if it has. |
| 4348 | */ |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4349 | if (fcheck(req->ring_fd) == req->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4350 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 4351 | req->flags |= REQ_F_INFLIGHT; |
| 4352 | req->work.files = current->files; |
| 4353 | ret = 0; |
| 4354 | } |
| 4355 | spin_unlock_irq(&ctx->inflight_lock); |
| 4356 | rcu_read_unlock(); |
| 4357 | |
| 4358 | return ret; |
| 4359 | } |
| 4360 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4361 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 4362 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4363 | struct io_timeout_data *data = container_of(timer, |
| 4364 | struct io_timeout_data, timer); |
| 4365 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4366 | struct io_ring_ctx *ctx = req->ctx; |
| 4367 | struct io_kiocb *prev = NULL; |
| 4368 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4369 | |
| 4370 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4371 | |
| 4372 | /* |
| 4373 | * We don't expect the list to be empty, that will only happen if we |
| 4374 | * race with the completion of the linked work. |
| 4375 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4376 | if (!list_empty(&req->link_list)) { |
| 4377 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 4378 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4379 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4380 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4381 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 4382 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4383 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4384 | } |
| 4385 | |
| 4386 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4387 | |
| 4388 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4389 | req_set_fail_links(prev); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4390 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
| 4391 | -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4392 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4393 | } else { |
| 4394 | io_cqring_add_event(req, -ETIME); |
| 4395 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4396 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4397 | return HRTIMER_NORESTART; |
| 4398 | } |
| 4399 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4400 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4401 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4402 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4403 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4404 | /* |
| 4405 | * If the list is now empty, then our linked request finished before |
| 4406 | * we got a chance to setup the timer |
| 4407 | */ |
| 4408 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4409 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4410 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4411 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4412 | data->timer.function = io_link_timeout_fn; |
| 4413 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 4414 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4415 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4416 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4417 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4418 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4419 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4420 | } |
| 4421 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4422 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4423 | { |
| 4424 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4425 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4426 | if (!(req->flags & REQ_F_LINK)) |
| 4427 | return NULL; |
| 4428 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4429 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 4430 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4431 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4432 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4433 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4434 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4435 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4436 | } |
| 4437 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4438 | 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] | 4439 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4440 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4441 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4442 | int ret; |
| 4443 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4444 | again: |
| 4445 | linked_timeout = io_prep_linked_timeout(req); |
| 4446 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4447 | ret = io_issue_sqe(req, sqe, &nxt, true); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 4448 | |
| 4449 | /* |
| 4450 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 4451 | * doesn't support non-blocking read/write attempts |
| 4452 | */ |
| 4453 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 4454 | (req->flags & REQ_F_MUST_PUNT))) { |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4455 | if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) { |
| 4456 | ret = io_grab_files(req); |
| 4457 | if (ret) |
| 4458 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4459 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4460 | |
| 4461 | /* |
| 4462 | * Queued up for async execution, worker will release |
| 4463 | * submit reference when the iocb is actually submitted. |
| 4464 | */ |
| 4465 | io_queue_async_work(req); |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4466 | goto done_req; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4467 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4468 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4469 | err: |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4470 | /* drop submission reference */ |
| 4471 | io_put_req(req); |
| 4472 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4473 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4474 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4475 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4476 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4477 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4478 | } |
| 4479 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4480 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4481 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4482 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4483 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4484 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4485 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4486 | done_req: |
| 4487 | if (nxt) { |
| 4488 | req = nxt; |
| 4489 | nxt = NULL; |
| 4490 | goto again; |
| 4491 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4492 | } |
| 4493 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4494 | 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] | 4495 | { |
| 4496 | int ret; |
| 4497 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4498 | if (unlikely(req->ctx->drain_next)) { |
| 4499 | req->flags |= REQ_F_IO_DRAIN; |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 4500 | req->ctx->drain_next = 0; |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4501 | } |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 4502 | req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK) != 0; |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4503 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4504 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4505 | if (ret) { |
| 4506 | if (ret != -EIOCBQUEUED) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4507 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4508 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4509 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4510 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 4511 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4512 | /* |
| 4513 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 4514 | * to async execution. |
| 4515 | */ |
| 4516 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 4517 | io_queue_async_work(req); |
| 4518 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4519 | __io_queue_sqe(req, sqe); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4520 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4521 | } |
| 4522 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4523 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4524 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4525 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4526 | io_cqring_add_event(req, -ECANCELED); |
| 4527 | io_double_put_req(req); |
| 4528 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4529 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4530 | } |
| 4531 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4532 | #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] | 4533 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4534 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4535 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4536 | struct io_submit_state *state, struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4537 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4538 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4539 | unsigned int sqe_flags; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4540 | int ret; |
| 4541 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4542 | sqe_flags = READ_ONCE(sqe->flags); |
| 4543 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4544 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4545 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4546 | ret = -EINVAL; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4547 | goto err_req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4548 | } |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4549 | if (sqe_flags & IOSQE_ASYNC) |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4550 | req->flags |= REQ_F_FORCE_ASYNC; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4551 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4552 | ret = io_req_set_file(state, req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4553 | if (unlikely(ret)) { |
| 4554 | err_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4555 | io_cqring_add_event(req, ret); |
| 4556 | io_double_put_req(req); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4557 | return false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4558 | } |
| 4559 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4560 | /* |
| 4561 | * If we already have a head request, queue this one for async |
| 4562 | * submittal once the head completes. If we don't have a head but |
| 4563 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 4564 | * submitted sync once the chain is complete. If none of those |
| 4565 | * conditions are true (normal request), then just queue it. |
| 4566 | */ |
| 4567 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4568 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4569 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4570 | if (sqe_flags & IOSQE_IO_DRAIN) |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4571 | head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN; |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4572 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4573 | if (sqe_flags & IOSQE_IO_HARDLINK) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4574 | req->flags |= REQ_F_HARDLINK; |
| 4575 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4576 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4577 | ret = -EAGAIN; |
| 4578 | goto err_req; |
| 4579 | } |
| 4580 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4581 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4582 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4583 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4584 | head->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4585 | goto err_req; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4586 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4587 | trace_io_uring_link(ctx, req, head); |
| 4588 | list_add_tail(&req->link_list, &head->link_list); |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4589 | |
| 4590 | /* last request of a link, enqueue the link */ |
| 4591 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) { |
| 4592 | io_queue_link_head(head); |
| 4593 | *link = NULL; |
| 4594 | } |
| 4595 | } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4596 | req->flags |= REQ_F_LINK; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4597 | if (sqe_flags & IOSQE_IO_HARDLINK) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4598 | req->flags |= REQ_F_HARDLINK; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4599 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4600 | INIT_LIST_HEAD(&req->link_list); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4601 | ret = io_req_defer_prep(req, sqe); |
| 4602 | if (ret) |
| 4603 | req->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4604 | *link = req; |
| 4605 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4606 | io_queue_sqe(req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4607 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4608 | |
| 4609 | return true; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4610 | } |
| 4611 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4612 | /* |
| 4613 | * Batched submission is done, ensure local IO is flushed out. |
| 4614 | */ |
| 4615 | static void io_submit_state_end(struct io_submit_state *state) |
| 4616 | { |
| 4617 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 4618 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4619 | if (state->free_reqs) |
| 4620 | kmem_cache_free_bulk(req_cachep, state->free_reqs, |
| 4621 | &state->reqs[state->cur_req]); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4622 | } |
| 4623 | |
| 4624 | /* |
| 4625 | * Start submission side cache. |
| 4626 | */ |
| 4627 | static void io_submit_state_start(struct io_submit_state *state, |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4628 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4629 | { |
| 4630 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4631 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4632 | state->file = NULL; |
| 4633 | state->ios_left = max_ios; |
| 4634 | } |
| 4635 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4636 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 4637 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4638 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4639 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 4640 | /* |
| 4641 | * Ensure any loads from the SQEs are done at this point, |
| 4642 | * since once we write the new head, the application could |
| 4643 | * write new data to them. |
| 4644 | */ |
| 4645 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4646 | } |
| 4647 | |
| 4648 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4649 | * 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] | 4650 | * that is mapped by userspace. This means that care needs to be taken to |
| 4651 | * ensure that reads are stable, as we cannot rely on userspace always |
| 4652 | * being a good citizen. If members of the sqe are validated and then later |
| 4653 | * used, it's important that those reads are done through READ_ONCE() to |
| 4654 | * prevent a re-load down the line. |
| 4655 | */ |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4656 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 4657 | const struct io_uring_sqe **sqe_ptr) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4658 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4659 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4660 | unsigned head; |
| 4661 | |
| 4662 | /* |
| 4663 | * The cached sq head (or cq tail) serves two purposes: |
| 4664 | * |
| 4665 | * 1) allows us to batch the cost of updating the user visible |
| 4666 | * head updates. |
| 4667 | * 2) allows the kernel side to track the head on its own, even |
| 4668 | * though the application is the one updating it. |
| 4669 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4670 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 4671 | if (likely(head < ctx->sq_entries)) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4672 | /* |
| 4673 | * All io need record the previous position, if LINK vs DARIN, |
| 4674 | * it can be used to mark the position of the first IO in the |
| 4675 | * link list. |
| 4676 | */ |
| 4677 | req->sequence = ctx->cached_sq_head; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4678 | *sqe_ptr = &ctx->sq_sqes[head]; |
| 4679 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); |
| 4680 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4681 | ctx->cached_sq_head++; |
| 4682 | return true; |
| 4683 | } |
| 4684 | |
| 4685 | /* drop invalid entries */ |
| 4686 | ctx->cached_sq_head++; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 4687 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4688 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4689 | return false; |
| 4690 | } |
| 4691 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4692 | 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] | 4693 | struct file *ring_file, int ring_fd, |
| 4694 | struct mm_struct **mm, bool async) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4695 | { |
| 4696 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4697 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4698 | int i, submitted = 0; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4699 | bool mm_fault = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4700 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 4701 | /* 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] | 4702 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 4703 | if (!list_empty(&ctx->cq_overflow_list) && |
| 4704 | !io_cqring_overflow_flush(ctx, false)) |
| 4705 | return -EBUSY; |
| 4706 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4707 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4708 | /* make sure SQ entry isn't read before tail */ |
| 4709 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 4710 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4711 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 4712 | return -EAGAIN; |
| 4713 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4714 | if (nr > IO_PLUG_THRESHOLD) { |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4715 | io_submit_state_start(&state, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4716 | statep = &state; |
| 4717 | } |
| 4718 | |
| 4719 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4720 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4721 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4722 | |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4723 | req = io_get_req(ctx, statep); |
| 4724 | if (unlikely(!req)) { |
| 4725 | if (!submitted) |
| 4726 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4727 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4728 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4729 | if (!io_get_sqring(ctx, req, &sqe)) { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4730 | __io_req_do_free(req); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4731 | break; |
| 4732 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4733 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4734 | /* will complete beyond this point, count as submitted */ |
| 4735 | submitted++; |
| 4736 | |
| 4737 | if (unlikely(req->opcode >= IORING_OP_LAST)) { |
| 4738 | io_cqring_add_event(req, -EINVAL); |
| 4739 | io_double_put_req(req); |
| 4740 | break; |
| 4741 | } |
| 4742 | |
| 4743 | if (io_op_defs[req->opcode].needs_mm && !*mm) { |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4744 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
| 4745 | if (!mm_fault) { |
| 4746 | use_mm(ctx->sqo_mm); |
| 4747 | *mm = ctx->sqo_mm; |
| 4748 | } |
| 4749 | } |
| 4750 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4751 | req->ring_file = ring_file; |
| 4752 | req->ring_fd = ring_fd; |
| 4753 | req->has_user = *mm != NULL; |
| 4754 | req->in_async = async; |
| 4755 | req->needs_fixed_file = async; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4756 | trace_io_uring_submit_sqe(ctx, req->user_data, true, async); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4757 | if (!io_submit_sqe(req, sqe, statep, &link)) |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4758 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4759 | } |
| 4760 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4761 | if (submitted != nr) |
| 4762 | percpu_ref_put_many(&ctx->refs, nr - submitted); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4763 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4764 | io_queue_link_head(link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4765 | if (statep) |
| 4766 | io_submit_state_end(&state); |
| 4767 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 4768 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 4769 | io_commit_sqring(ctx); |
| 4770 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4771 | return submitted; |
| 4772 | } |
| 4773 | |
| 4774 | static int io_sq_thread(void *data) |
| 4775 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4776 | struct io_ring_ctx *ctx = data; |
| 4777 | struct mm_struct *cur_mm = NULL; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4778 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4779 | mm_segment_t old_fs; |
| 4780 | DEFINE_WAIT(wait); |
| 4781 | unsigned inflight; |
| 4782 | unsigned long timeout; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4783 | int ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4784 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 4785 | complete(&ctx->completions[1]); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 4786 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4787 | old_fs = get_fs(); |
| 4788 | set_fs(USER_DS); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4789 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4790 | |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4791 | ret = timeout = inflight = 0; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4792 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4793 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4794 | |
| 4795 | if (inflight) { |
| 4796 | unsigned nr_events = 0; |
| 4797 | |
| 4798 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 4799 | /* |
| 4800 | * inflight is the count of the maximum possible |
| 4801 | * entries we submitted, but it can be smaller |
| 4802 | * if we dropped some of them. If we don't have |
| 4803 | * poll entries available, then we know that we |
| 4804 | * have nothing left to poll for. Reset the |
| 4805 | * inflight count to zero in that case. |
| 4806 | */ |
| 4807 | mutex_lock(&ctx->uring_lock); |
| 4808 | if (!list_empty(&ctx->poll_list)) |
| 4809 | __io_iopoll_check(ctx, &nr_events, 0); |
| 4810 | else |
| 4811 | inflight = 0; |
| 4812 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4813 | } else { |
| 4814 | /* |
| 4815 | * Normal IO, just pretend everything completed. |
| 4816 | * We don't have to poll completions for that. |
| 4817 | */ |
| 4818 | nr_events = inflight; |
| 4819 | } |
| 4820 | |
| 4821 | inflight -= nr_events; |
| 4822 | if (!inflight) |
| 4823 | timeout = jiffies + ctx->sq_thread_idle; |
| 4824 | } |
| 4825 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4826 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4827 | |
| 4828 | /* |
| 4829 | * If submit got -EBUSY, flag us as needing the application |
| 4830 | * to enter the kernel to reap and flush events. |
| 4831 | */ |
| 4832 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4833 | /* |
| 4834 | * We're polling. If we're within the defined idle |
| 4835 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4836 | * to sleep. The exception is if we got EBUSY doing |
| 4837 | * more IO, we should wait for the application to |
| 4838 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4839 | */ |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4840 | if (inflight || |
| 4841 | (!time_after(jiffies, timeout) && ret != -EBUSY)) { |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 4842 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4843 | continue; |
| 4844 | } |
| 4845 | |
| 4846 | /* |
| 4847 | * Drop cur_mm before scheduling, we can't hold it for |
| 4848 | * long periods (or over schedule()). Do this before |
| 4849 | * adding ourselves to the waitqueue, as the unuse/drop |
| 4850 | * may sleep. |
| 4851 | */ |
| 4852 | if (cur_mm) { |
| 4853 | unuse_mm(cur_mm); |
| 4854 | mmput(cur_mm); |
| 4855 | cur_mm = NULL; |
| 4856 | } |
| 4857 | |
| 4858 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 4859 | TASK_INTERRUPTIBLE); |
| 4860 | |
| 4861 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4862 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 4863 | /* make sure to read SQ tail after writing flags */ |
| 4864 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4865 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4866 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4867 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4868 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4869 | finish_wait(&ctx->sqo_wait, &wait); |
| 4870 | break; |
| 4871 | } |
| 4872 | if (signal_pending(current)) |
| 4873 | flush_signals(current); |
| 4874 | schedule(); |
| 4875 | finish_wait(&ctx->sqo_wait, &wait); |
| 4876 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4877 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4878 | continue; |
| 4879 | } |
| 4880 | finish_wait(&ctx->sqo_wait, &wait); |
| 4881 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4882 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4883 | } |
| 4884 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 4885 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4886 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 4887 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4888 | if (ret > 0) |
| 4889 | inflight += ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4890 | } |
| 4891 | |
| 4892 | set_fs(old_fs); |
| 4893 | if (cur_mm) { |
| 4894 | unuse_mm(cur_mm); |
| 4895 | mmput(cur_mm); |
| 4896 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4897 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 4898 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4899 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 4900 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4901 | return 0; |
| 4902 | } |
| 4903 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4904 | struct io_wait_queue { |
| 4905 | struct wait_queue_entry wq; |
| 4906 | struct io_ring_ctx *ctx; |
| 4907 | unsigned to_wait; |
| 4908 | unsigned nr_timeouts; |
| 4909 | }; |
| 4910 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4911 | 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] | 4912 | { |
| 4913 | struct io_ring_ctx *ctx = iowq->ctx; |
| 4914 | |
| 4915 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 4916 | * 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] | 4917 | * started waiting. For timeouts, we always want to return to userspace, |
| 4918 | * regardless of event count. |
| 4919 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4920 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4921 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 4922 | } |
| 4923 | |
| 4924 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 4925 | int wake_flags, void *key) |
| 4926 | { |
| 4927 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 4928 | wq); |
| 4929 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4930 | /* use noflush == true, as we can't safely rely on locking context */ |
| 4931 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4932 | return -1; |
| 4933 | |
| 4934 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 4935 | } |
| 4936 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4937 | /* |
| 4938 | * Wait until events become available, if we don't already have some. The |
| 4939 | * application must reap them itself, as they reside on the shared cq ring. |
| 4940 | */ |
| 4941 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 4942 | const sigset_t __user *sig, size_t sigsz) |
| 4943 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4944 | struct io_wait_queue iowq = { |
| 4945 | .wq = { |
| 4946 | .private = current, |
| 4947 | .func = io_wake_function, |
| 4948 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 4949 | }, |
| 4950 | .ctx = ctx, |
| 4951 | .to_wait = min_events, |
| 4952 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4953 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 4954 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4955 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4956 | if (io_cqring_events(ctx, false) >= min_events) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4957 | return 0; |
| 4958 | |
| 4959 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 4960 | #ifdef CONFIG_COMPAT |
| 4961 | if (in_compat_syscall()) |
| 4962 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 4963 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 4964 | else |
| 4965 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 4966 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 4967 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4968 | if (ret) |
| 4969 | return ret; |
| 4970 | } |
| 4971 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4972 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 4973 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4974 | do { |
| 4975 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 4976 | TASK_INTERRUPTIBLE); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4977 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4978 | break; |
| 4979 | schedule(); |
| 4980 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 4981 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4982 | break; |
| 4983 | } |
| 4984 | } while (1); |
| 4985 | finish_wait(&ctx->wait, &iowq.wq); |
| 4986 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 4987 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4988 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4989 | 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] | 4990 | } |
| 4991 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4992 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 4993 | { |
| 4994 | #if defined(CONFIG_UNIX) |
| 4995 | if (ctx->ring_sock) { |
| 4996 | struct sock *sock = ctx->ring_sock->sk; |
| 4997 | struct sk_buff *skb; |
| 4998 | |
| 4999 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 5000 | kfree_skb(skb); |
| 5001 | } |
| 5002 | #else |
| 5003 | int i; |
| 5004 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5005 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5006 | struct file *file; |
| 5007 | |
| 5008 | file = io_file_from_index(ctx, i); |
| 5009 | if (file) |
| 5010 | fput(file); |
| 5011 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5012 | #endif |
| 5013 | } |
| 5014 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5015 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 5016 | { |
| 5017 | struct fixed_file_data *data; |
| 5018 | |
| 5019 | data = container_of(ref, struct fixed_file_data, refs); |
| 5020 | complete(&data->done); |
| 5021 | } |
| 5022 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5023 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5024 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5025 | struct fixed_file_data *data = ctx->file_data; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5026 | unsigned nr_tables, i; |
| 5027 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5028 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5029 | return -ENXIO; |
| 5030 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5031 | /* protect against inflight atomic switch, which drops the ref */ |
| 5032 | flush_work(&data->ref_work); |
| 5033 | percpu_ref_get(&data->refs); |
| 5034 | percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill); |
| 5035 | wait_for_completion(&data->done); |
| 5036 | percpu_ref_put(&data->refs); |
| 5037 | percpu_ref_exit(&data->refs); |
| 5038 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5039 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5040 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 5041 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5042 | kfree(data->table[i].files); |
| 5043 | kfree(data->table); |
| 5044 | kfree(data); |
| 5045 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5046 | ctx->nr_user_files = 0; |
| 5047 | return 0; |
| 5048 | } |
| 5049 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5050 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 5051 | { |
| 5052 | if (ctx->sqo_thread) { |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5053 | wait_for_completion(&ctx->completions[1]); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5054 | /* |
| 5055 | * The park is a bit of a work-around, without it we get |
| 5056 | * warning spews on shutdown with SQPOLL set and affinity |
| 5057 | * set to a single CPU. |
| 5058 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5059 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5060 | kthread_stop(ctx->sqo_thread); |
| 5061 | ctx->sqo_thread = NULL; |
| 5062 | } |
| 5063 | } |
| 5064 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5065 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 5066 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5067 | io_sq_thread_stop(ctx); |
| 5068 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5069 | if (ctx->io_wq) { |
| 5070 | io_wq_destroy(ctx->io_wq); |
| 5071 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5072 | } |
| 5073 | } |
| 5074 | |
| 5075 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5076 | /* |
| 5077 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 5078 | * the io_uring can be safely unregistered on process exit, even if we have |
| 5079 | * loops in the file referencing. |
| 5080 | */ |
| 5081 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 5082 | { |
| 5083 | struct sock *sk = ctx->ring_sock->sk; |
| 5084 | struct scm_fp_list *fpl; |
| 5085 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5086 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5087 | |
| 5088 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 5089 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 5090 | |
| 5091 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 5092 | return -EMFILE; |
| 5093 | } |
| 5094 | |
| 5095 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 5096 | if (!fpl) |
| 5097 | return -ENOMEM; |
| 5098 | |
| 5099 | skb = alloc_skb(0, GFP_KERNEL); |
| 5100 | if (!skb) { |
| 5101 | kfree(fpl); |
| 5102 | return -ENOMEM; |
| 5103 | } |
| 5104 | |
| 5105 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5106 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5107 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5108 | fpl->user = get_uid(ctx->user); |
| 5109 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5110 | struct file *file = io_file_from_index(ctx, i + offset); |
| 5111 | |
| 5112 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5113 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5114 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5115 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 5116 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5117 | } |
| 5118 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5119 | if (nr_files) { |
| 5120 | fpl->max = SCM_MAX_FD; |
| 5121 | fpl->count = nr_files; |
| 5122 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5123 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5124 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 5125 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5126 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5127 | for (i = 0; i < nr_files; i++) |
| 5128 | fput(fpl->fp[i]); |
| 5129 | } else { |
| 5130 | kfree_skb(skb); |
| 5131 | kfree(fpl); |
| 5132 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5133 | |
| 5134 | return 0; |
| 5135 | } |
| 5136 | |
| 5137 | /* |
| 5138 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 5139 | * causes regular reference counting to break down. We rely on the UNIX |
| 5140 | * garbage collection to take care of this problem for us. |
| 5141 | */ |
| 5142 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5143 | { |
| 5144 | unsigned left, total; |
| 5145 | int ret = 0; |
| 5146 | |
| 5147 | total = 0; |
| 5148 | left = ctx->nr_user_files; |
| 5149 | while (left) { |
| 5150 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5151 | |
| 5152 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 5153 | if (ret) |
| 5154 | break; |
| 5155 | left -= this_files; |
| 5156 | total += this_files; |
| 5157 | } |
| 5158 | |
| 5159 | if (!ret) |
| 5160 | return 0; |
| 5161 | |
| 5162 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5163 | struct file *file = io_file_from_index(ctx, total); |
| 5164 | |
| 5165 | if (file) |
| 5166 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5167 | total++; |
| 5168 | } |
| 5169 | |
| 5170 | return ret; |
| 5171 | } |
| 5172 | #else |
| 5173 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5174 | { |
| 5175 | return 0; |
| 5176 | } |
| 5177 | #endif |
| 5178 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5179 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 5180 | unsigned nr_files) |
| 5181 | { |
| 5182 | int i; |
| 5183 | |
| 5184 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5185 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5186 | unsigned this_files; |
| 5187 | |
| 5188 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 5189 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 5190 | GFP_KERNEL); |
| 5191 | if (!table->files) |
| 5192 | break; |
| 5193 | nr_files -= this_files; |
| 5194 | } |
| 5195 | |
| 5196 | if (i == nr_tables) |
| 5197 | return 0; |
| 5198 | |
| 5199 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5200 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5201 | kfree(table->files); |
| 5202 | } |
| 5203 | return 1; |
| 5204 | } |
| 5205 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5206 | 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] | 5207 | { |
| 5208 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5209 | struct sock *sock = ctx->ring_sock->sk; |
| 5210 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 5211 | struct sk_buff *skb; |
| 5212 | int i; |
| 5213 | |
| 5214 | __skb_queue_head_init(&list); |
| 5215 | |
| 5216 | /* |
| 5217 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 5218 | * remove this entry and rearrange the file array. |
| 5219 | */ |
| 5220 | skb = skb_dequeue(head); |
| 5221 | while (skb) { |
| 5222 | struct scm_fp_list *fp; |
| 5223 | |
| 5224 | fp = UNIXCB(skb).fp; |
| 5225 | for (i = 0; i < fp->count; i++) { |
| 5226 | int left; |
| 5227 | |
| 5228 | if (fp->fp[i] != file) |
| 5229 | continue; |
| 5230 | |
| 5231 | unix_notinflight(fp->user, fp->fp[i]); |
| 5232 | left = fp->count - 1 - i; |
| 5233 | if (left) { |
| 5234 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 5235 | left * sizeof(struct file *)); |
| 5236 | } |
| 5237 | fp->count--; |
| 5238 | if (!fp->count) { |
| 5239 | kfree_skb(skb); |
| 5240 | skb = NULL; |
| 5241 | } else { |
| 5242 | __skb_queue_tail(&list, skb); |
| 5243 | } |
| 5244 | fput(file); |
| 5245 | file = NULL; |
| 5246 | break; |
| 5247 | } |
| 5248 | |
| 5249 | if (!file) |
| 5250 | break; |
| 5251 | |
| 5252 | __skb_queue_tail(&list, skb); |
| 5253 | |
| 5254 | skb = skb_dequeue(head); |
| 5255 | } |
| 5256 | |
| 5257 | if (skb_peek(&list)) { |
| 5258 | spin_lock_irq(&head->lock); |
| 5259 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 5260 | __skb_queue_tail(head, skb); |
| 5261 | spin_unlock_irq(&head->lock); |
| 5262 | } |
| 5263 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5264 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5265 | #endif |
| 5266 | } |
| 5267 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5268 | struct io_file_put { |
| 5269 | struct llist_node llist; |
| 5270 | struct file *file; |
| 5271 | struct completion *done; |
| 5272 | }; |
| 5273 | |
| 5274 | static void io_ring_file_ref_switch(struct work_struct *work) |
| 5275 | { |
| 5276 | struct io_file_put *pfile, *tmp; |
| 5277 | struct fixed_file_data *data; |
| 5278 | struct llist_node *node; |
| 5279 | |
| 5280 | data = container_of(work, struct fixed_file_data, ref_work); |
| 5281 | |
| 5282 | while ((node = llist_del_all(&data->put_llist)) != NULL) { |
| 5283 | llist_for_each_entry_safe(pfile, tmp, node, llist) { |
| 5284 | io_ring_file_put(data->ctx, pfile->file); |
| 5285 | if (pfile->done) |
| 5286 | complete(pfile->done); |
| 5287 | else |
| 5288 | kfree(pfile); |
| 5289 | } |
| 5290 | } |
| 5291 | |
| 5292 | percpu_ref_get(&data->refs); |
| 5293 | percpu_ref_switch_to_percpu(&data->refs); |
| 5294 | } |
| 5295 | |
| 5296 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 5297 | { |
| 5298 | struct fixed_file_data *data; |
| 5299 | |
| 5300 | data = container_of(ref, struct fixed_file_data, refs); |
| 5301 | |
| 5302 | /* we can't safely switch from inside this context, punt to wq */ |
| 5303 | queue_work(system_wq, &data->ref_work); |
| 5304 | } |
| 5305 | |
| 5306 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5307 | unsigned nr_args) |
| 5308 | { |
| 5309 | __s32 __user *fds = (__s32 __user *) arg; |
| 5310 | unsigned nr_tables; |
| 5311 | struct file *file; |
| 5312 | int fd, ret = 0; |
| 5313 | unsigned i; |
| 5314 | |
| 5315 | if (ctx->file_data) |
| 5316 | return -EBUSY; |
| 5317 | if (!nr_args) |
| 5318 | return -EINVAL; |
| 5319 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 5320 | return -EMFILE; |
| 5321 | |
| 5322 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 5323 | if (!ctx->file_data) |
| 5324 | return -ENOMEM; |
| 5325 | ctx->file_data->ctx = ctx; |
| 5326 | init_completion(&ctx->file_data->done); |
| 5327 | |
| 5328 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 5329 | ctx->file_data->table = kcalloc(nr_tables, |
| 5330 | sizeof(struct fixed_file_table), |
| 5331 | GFP_KERNEL); |
| 5332 | if (!ctx->file_data->table) { |
| 5333 | kfree(ctx->file_data); |
| 5334 | ctx->file_data = NULL; |
| 5335 | return -ENOMEM; |
| 5336 | } |
| 5337 | |
| 5338 | if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero, |
| 5339 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 5340 | kfree(ctx->file_data->table); |
| 5341 | kfree(ctx->file_data); |
| 5342 | ctx->file_data = NULL; |
| 5343 | return -ENOMEM; |
| 5344 | } |
| 5345 | ctx->file_data->put_llist.first = NULL; |
| 5346 | INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch); |
| 5347 | |
| 5348 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 5349 | percpu_ref_exit(&ctx->file_data->refs); |
| 5350 | kfree(ctx->file_data->table); |
| 5351 | kfree(ctx->file_data); |
| 5352 | ctx->file_data = NULL; |
| 5353 | return -ENOMEM; |
| 5354 | } |
| 5355 | |
| 5356 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 5357 | struct fixed_file_table *table; |
| 5358 | unsigned index; |
| 5359 | |
| 5360 | ret = -EFAULT; |
| 5361 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 5362 | break; |
| 5363 | /* allow sparse sets */ |
| 5364 | if (fd == -1) { |
| 5365 | ret = 0; |
| 5366 | continue; |
| 5367 | } |
| 5368 | |
| 5369 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 5370 | index = i & IORING_FILE_TABLE_MASK; |
| 5371 | file = fget(fd); |
| 5372 | |
| 5373 | ret = -EBADF; |
| 5374 | if (!file) |
| 5375 | break; |
| 5376 | |
| 5377 | /* |
| 5378 | * Don't allow io_uring instances to be registered. If UNIX |
| 5379 | * isn't enabled, then this causes a reference cycle and this |
| 5380 | * instance can never get freed. If UNIX is enabled we'll |
| 5381 | * handle it just fine, but there's still no point in allowing |
| 5382 | * a ring fd as it doesn't support regular read/write anyway. |
| 5383 | */ |
| 5384 | if (file->f_op == &io_uring_fops) { |
| 5385 | fput(file); |
| 5386 | break; |
| 5387 | } |
| 5388 | ret = 0; |
| 5389 | table->files[index] = file; |
| 5390 | } |
| 5391 | |
| 5392 | if (ret) { |
| 5393 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5394 | file = io_file_from_index(ctx, i); |
| 5395 | if (file) |
| 5396 | fput(file); |
| 5397 | } |
| 5398 | for (i = 0; i < nr_tables; i++) |
| 5399 | kfree(ctx->file_data->table[i].files); |
| 5400 | |
| 5401 | kfree(ctx->file_data->table); |
| 5402 | kfree(ctx->file_data); |
| 5403 | ctx->file_data = NULL; |
| 5404 | ctx->nr_user_files = 0; |
| 5405 | return ret; |
| 5406 | } |
| 5407 | |
| 5408 | ret = io_sqe_files_scm(ctx); |
| 5409 | if (ret) |
| 5410 | io_sqe_files_unregister(ctx); |
| 5411 | |
| 5412 | return ret; |
| 5413 | } |
| 5414 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5415 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 5416 | int index) |
| 5417 | { |
| 5418 | #if defined(CONFIG_UNIX) |
| 5419 | struct sock *sock = ctx->ring_sock->sk; |
| 5420 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 5421 | struct sk_buff *skb; |
| 5422 | |
| 5423 | /* |
| 5424 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 5425 | * file set. If there's no room, fall back to allocating a new skb |
| 5426 | * and filling it in. |
| 5427 | */ |
| 5428 | spin_lock_irq(&head->lock); |
| 5429 | skb = skb_peek(head); |
| 5430 | if (skb) { |
| 5431 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 5432 | |
| 5433 | if (fpl->count < SCM_MAX_FD) { |
| 5434 | __skb_unlink(skb, head); |
| 5435 | spin_unlock_irq(&head->lock); |
| 5436 | fpl->fp[fpl->count] = get_file(file); |
| 5437 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 5438 | fpl->count++; |
| 5439 | spin_lock_irq(&head->lock); |
| 5440 | __skb_queue_head(head, skb); |
| 5441 | } else { |
| 5442 | skb = NULL; |
| 5443 | } |
| 5444 | } |
| 5445 | spin_unlock_irq(&head->lock); |
| 5446 | |
| 5447 | if (skb) { |
| 5448 | fput(file); |
| 5449 | return 0; |
| 5450 | } |
| 5451 | |
| 5452 | return __io_sqe_files_scm(ctx, 1, index); |
| 5453 | #else |
| 5454 | return 0; |
| 5455 | #endif |
| 5456 | } |
| 5457 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5458 | static void io_atomic_switch(struct percpu_ref *ref) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5459 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5460 | struct fixed_file_data *data; |
| 5461 | |
| 5462 | data = container_of(ref, struct fixed_file_data, refs); |
| 5463 | clear_bit(FFD_F_ATOMIC, &data->state); |
| 5464 | } |
| 5465 | |
| 5466 | static bool io_queue_file_removal(struct fixed_file_data *data, |
| 5467 | struct file *file) |
| 5468 | { |
| 5469 | struct io_file_put *pfile, pfile_stack; |
| 5470 | DECLARE_COMPLETION_ONSTACK(done); |
| 5471 | |
| 5472 | /* |
| 5473 | * If we fail allocating the struct we need for doing async reomval |
| 5474 | * of this file, just punt to sync and wait for it. |
| 5475 | */ |
| 5476 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
| 5477 | if (!pfile) { |
| 5478 | pfile = &pfile_stack; |
| 5479 | pfile->done = &done; |
| 5480 | } |
| 5481 | |
| 5482 | pfile->file = file; |
| 5483 | llist_add(&pfile->llist, &data->put_llist); |
| 5484 | |
| 5485 | if (pfile == &pfile_stack) { |
| 5486 | if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5487 | percpu_ref_put(&data->refs); |
| 5488 | percpu_ref_switch_to_atomic(&data->refs, |
| 5489 | io_atomic_switch); |
| 5490 | } |
| 5491 | wait_for_completion(&done); |
| 5492 | flush_work(&data->ref_work); |
| 5493 | return false; |
| 5494 | } |
| 5495 | |
| 5496 | return true; |
| 5497 | } |
| 5498 | |
| 5499 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 5500 | struct io_uring_files_update *up, |
| 5501 | unsigned nr_args) |
| 5502 | { |
| 5503 | struct fixed_file_data *data = ctx->file_data; |
| 5504 | bool ref_switch = false; |
| 5505 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5506 | __s32 __user *fds; |
| 5507 | int fd, i, err; |
| 5508 | __u32 done; |
| 5509 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5510 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5511 | return -EOVERFLOW; |
| 5512 | if (done > ctx->nr_user_files) |
| 5513 | return -EINVAL; |
| 5514 | |
| 5515 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5516 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5517 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5518 | struct fixed_file_table *table; |
| 5519 | unsigned index; |
| 5520 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5521 | err = 0; |
| 5522 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 5523 | err = -EFAULT; |
| 5524 | break; |
| 5525 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5526 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 5527 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5528 | index = i & IORING_FILE_TABLE_MASK; |
| 5529 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5530 | file = io_file_from_index(ctx, index); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5531 | table->files[index] = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5532 | if (io_queue_file_removal(data, file)) |
| 5533 | ref_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5534 | } |
| 5535 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5536 | file = fget(fd); |
| 5537 | if (!file) { |
| 5538 | err = -EBADF; |
| 5539 | break; |
| 5540 | } |
| 5541 | /* |
| 5542 | * Don't allow io_uring instances to be registered. If |
| 5543 | * UNIX isn't enabled, then this causes a reference |
| 5544 | * cycle and this instance can never get freed. If UNIX |
| 5545 | * is enabled we'll handle it just fine, but there's |
| 5546 | * still no point in allowing a ring fd as it doesn't |
| 5547 | * support regular read/write anyway. |
| 5548 | */ |
| 5549 | if (file->f_op == &io_uring_fops) { |
| 5550 | fput(file); |
| 5551 | err = -EBADF; |
| 5552 | break; |
| 5553 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5554 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5555 | err = io_sqe_file_register(ctx, file, i); |
| 5556 | if (err) |
| 5557 | break; |
| 5558 | } |
| 5559 | nr_args--; |
| 5560 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5561 | up->offset++; |
| 5562 | } |
| 5563 | |
| 5564 | if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5565 | percpu_ref_put(&data->refs); |
| 5566 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5567 | } |
| 5568 | |
| 5569 | return done ? done : err; |
| 5570 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5571 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 5572 | unsigned nr_args) |
| 5573 | { |
| 5574 | struct io_uring_files_update up; |
| 5575 | |
| 5576 | if (!ctx->file_data) |
| 5577 | return -ENXIO; |
| 5578 | if (!nr_args) |
| 5579 | return -EINVAL; |
| 5580 | if (copy_from_user(&up, arg, sizeof(up))) |
| 5581 | return -EFAULT; |
| 5582 | if (up.resv) |
| 5583 | return -EINVAL; |
| 5584 | |
| 5585 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 5586 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5587 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 5588 | static void io_put_work(struct io_wq_work *work) |
| 5589 | { |
| 5590 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5591 | |
| 5592 | io_put_req(req); |
| 5593 | } |
| 5594 | |
| 5595 | static void io_get_work(struct io_wq_work *work) |
| 5596 | { |
| 5597 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5598 | |
| 5599 | refcount_inc(&req->refs); |
| 5600 | } |
| 5601 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5602 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 5603 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5604 | { |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5605 | struct io_wq_data data; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5606 | unsigned concurrency; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5607 | int ret; |
| 5608 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5609 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5610 | mmgrab(current->mm); |
| 5611 | ctx->sqo_mm = current->mm; |
| 5612 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5613 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 5614 | ret = -EPERM; |
| 5615 | if (!capable(CAP_SYS_ADMIN)) |
| 5616 | goto err; |
| 5617 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5618 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 5619 | if (!ctx->sq_thread_idle) |
| 5620 | ctx->sq_thread_idle = HZ; |
| 5621 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5622 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5623 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5624 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5625 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5626 | if (cpu >= nr_cpu_ids) |
| 5627 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 5628 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5629 | goto err; |
| 5630 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5631 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 5632 | ctx, cpu, |
| 5633 | "io_uring-sq"); |
| 5634 | } else { |
| 5635 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 5636 | "io_uring-sq"); |
| 5637 | } |
| 5638 | if (IS_ERR(ctx->sqo_thread)) { |
| 5639 | ret = PTR_ERR(ctx->sqo_thread); |
| 5640 | ctx->sqo_thread = NULL; |
| 5641 | goto err; |
| 5642 | } |
| 5643 | wake_up_process(ctx->sqo_thread); |
| 5644 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 5645 | /* Can't have SQ_AFF without SQPOLL */ |
| 5646 | ret = -EINVAL; |
| 5647 | goto err; |
| 5648 | } |
| 5649 | |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5650 | data.mm = ctx->sqo_mm; |
| 5651 | data.user = ctx->user; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5652 | data.creds = ctx->creds; |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5653 | data.get_work = io_get_work; |
| 5654 | data.put_work = io_put_work; |
| 5655 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5656 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 5657 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5658 | ctx->io_wq = io_wq_create(concurrency, &data); |
Jens Axboe | 975c99a5 | 2019-10-30 08:42:56 -0600 | [diff] [blame] | 5659 | if (IS_ERR(ctx->io_wq)) { |
| 5660 | ret = PTR_ERR(ctx->io_wq); |
| 5661 | ctx->io_wq = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5662 | goto err; |
| 5663 | } |
| 5664 | |
| 5665 | return 0; |
| 5666 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 5667 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5668 | mmdrop(ctx->sqo_mm); |
| 5669 | ctx->sqo_mm = NULL; |
| 5670 | return ret; |
| 5671 | } |
| 5672 | |
| 5673 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 5674 | { |
| 5675 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 5676 | } |
| 5677 | |
| 5678 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 5679 | { |
| 5680 | unsigned long page_limit, cur_pages, new_pages; |
| 5681 | |
| 5682 | /* Don't allow more pages than we can safely lock */ |
| 5683 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 5684 | |
| 5685 | do { |
| 5686 | cur_pages = atomic_long_read(&user->locked_vm); |
| 5687 | new_pages = cur_pages + nr_pages; |
| 5688 | if (new_pages > page_limit) |
| 5689 | return -ENOMEM; |
| 5690 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 5691 | new_pages) != cur_pages); |
| 5692 | |
| 5693 | return 0; |
| 5694 | } |
| 5695 | |
| 5696 | static void io_mem_free(void *ptr) |
| 5697 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 5698 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5699 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 5700 | if (!ptr) |
| 5701 | return; |
| 5702 | |
| 5703 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5704 | if (put_page_testzero(page)) |
| 5705 | free_compound_page(page); |
| 5706 | } |
| 5707 | |
| 5708 | static void *io_mem_alloc(size_t size) |
| 5709 | { |
| 5710 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 5711 | __GFP_NORETRY; |
| 5712 | |
| 5713 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 5714 | } |
| 5715 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5716 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 5717 | size_t *sq_offset) |
| 5718 | { |
| 5719 | struct io_rings *rings; |
| 5720 | size_t off, sq_array_size; |
| 5721 | |
| 5722 | off = struct_size(rings, cqes, cq_entries); |
| 5723 | if (off == SIZE_MAX) |
| 5724 | return SIZE_MAX; |
| 5725 | |
| 5726 | #ifdef CONFIG_SMP |
| 5727 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 5728 | if (off == 0) |
| 5729 | return SIZE_MAX; |
| 5730 | #endif |
| 5731 | |
| 5732 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 5733 | if (sq_array_size == SIZE_MAX) |
| 5734 | return SIZE_MAX; |
| 5735 | |
| 5736 | if (check_add_overflow(off, sq_array_size, &off)) |
| 5737 | return SIZE_MAX; |
| 5738 | |
| 5739 | if (sq_offset) |
| 5740 | *sq_offset = off; |
| 5741 | |
| 5742 | return off; |
| 5743 | } |
| 5744 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5745 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 5746 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5747 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5748 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5749 | pages = (size_t)1 << get_order( |
| 5750 | rings_size(sq_entries, cq_entries, NULL)); |
| 5751 | pages += (size_t)1 << get_order( |
| 5752 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5753 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5754 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5755 | } |
| 5756 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5757 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 5758 | { |
| 5759 | int i, j; |
| 5760 | |
| 5761 | if (!ctx->user_bufs) |
| 5762 | return -ENXIO; |
| 5763 | |
| 5764 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 5765 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 5766 | |
| 5767 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 5768 | put_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5769 | |
| 5770 | if (ctx->account_mem) |
| 5771 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5772 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5773 | imu->nr_bvecs = 0; |
| 5774 | } |
| 5775 | |
| 5776 | kfree(ctx->user_bufs); |
| 5777 | ctx->user_bufs = NULL; |
| 5778 | ctx->nr_user_bufs = 0; |
| 5779 | return 0; |
| 5780 | } |
| 5781 | |
| 5782 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 5783 | void __user *arg, unsigned index) |
| 5784 | { |
| 5785 | struct iovec __user *src; |
| 5786 | |
| 5787 | #ifdef CONFIG_COMPAT |
| 5788 | if (ctx->compat) { |
| 5789 | struct compat_iovec __user *ciovs; |
| 5790 | struct compat_iovec ciov; |
| 5791 | |
| 5792 | ciovs = (struct compat_iovec __user *) arg; |
| 5793 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 5794 | return -EFAULT; |
| 5795 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 5796 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5797 | dst->iov_len = ciov.iov_len; |
| 5798 | return 0; |
| 5799 | } |
| 5800 | #endif |
| 5801 | src = (struct iovec __user *) arg; |
| 5802 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 5803 | return -EFAULT; |
| 5804 | return 0; |
| 5805 | } |
| 5806 | |
| 5807 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5808 | unsigned nr_args) |
| 5809 | { |
| 5810 | struct vm_area_struct **vmas = NULL; |
| 5811 | struct page **pages = NULL; |
| 5812 | int i, j, got_pages = 0; |
| 5813 | int ret = -EINVAL; |
| 5814 | |
| 5815 | if (ctx->user_bufs) |
| 5816 | return -EBUSY; |
| 5817 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 5818 | return -EINVAL; |
| 5819 | |
| 5820 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 5821 | GFP_KERNEL); |
| 5822 | if (!ctx->user_bufs) |
| 5823 | return -ENOMEM; |
| 5824 | |
| 5825 | for (i = 0; i < nr_args; i++) { |
| 5826 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 5827 | unsigned long off, start, end, ubuf; |
| 5828 | int pret, nr_pages; |
| 5829 | struct iovec iov; |
| 5830 | size_t size; |
| 5831 | |
| 5832 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 5833 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 5834 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5835 | |
| 5836 | /* |
| 5837 | * Don't impose further limits on the size and buffer |
| 5838 | * constraints here, we'll -EINVAL later when IO is |
| 5839 | * submitted if they are wrong. |
| 5840 | */ |
| 5841 | ret = -EFAULT; |
| 5842 | if (!iov.iov_base || !iov.iov_len) |
| 5843 | goto err; |
| 5844 | |
| 5845 | /* arbitrary limit, but we need something */ |
| 5846 | if (iov.iov_len > SZ_1G) |
| 5847 | goto err; |
| 5848 | |
| 5849 | ubuf = (unsigned long) iov.iov_base; |
| 5850 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 5851 | start = ubuf >> PAGE_SHIFT; |
| 5852 | nr_pages = end - start; |
| 5853 | |
| 5854 | if (ctx->account_mem) { |
| 5855 | ret = io_account_mem(ctx->user, nr_pages); |
| 5856 | if (ret) |
| 5857 | goto err; |
| 5858 | } |
| 5859 | |
| 5860 | ret = 0; |
| 5861 | if (!pages || nr_pages > got_pages) { |
| 5862 | kfree(vmas); |
| 5863 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5864 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5865 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5866 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5867 | sizeof(struct vm_area_struct *), |
| 5868 | GFP_KERNEL); |
| 5869 | if (!pages || !vmas) { |
| 5870 | ret = -ENOMEM; |
| 5871 | if (ctx->account_mem) |
| 5872 | io_unaccount_mem(ctx->user, nr_pages); |
| 5873 | goto err; |
| 5874 | } |
| 5875 | got_pages = nr_pages; |
| 5876 | } |
| 5877 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5878 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5879 | GFP_KERNEL); |
| 5880 | ret = -ENOMEM; |
| 5881 | if (!imu->bvec) { |
| 5882 | if (ctx->account_mem) |
| 5883 | io_unaccount_mem(ctx->user, nr_pages); |
| 5884 | goto err; |
| 5885 | } |
| 5886 | |
| 5887 | ret = 0; |
| 5888 | down_read(¤t->mm->mmap_sem); |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 5889 | pret = get_user_pages(ubuf, nr_pages, |
| 5890 | FOLL_WRITE | FOLL_LONGTERM, |
| 5891 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5892 | if (pret == nr_pages) { |
| 5893 | /* don't support file backed memory */ |
| 5894 | for (j = 0; j < nr_pages; j++) { |
| 5895 | struct vm_area_struct *vma = vmas[j]; |
| 5896 | |
| 5897 | if (vma->vm_file && |
| 5898 | !is_file_hugepages(vma->vm_file)) { |
| 5899 | ret = -EOPNOTSUPP; |
| 5900 | break; |
| 5901 | } |
| 5902 | } |
| 5903 | } else { |
| 5904 | ret = pret < 0 ? pret : -EFAULT; |
| 5905 | } |
| 5906 | up_read(¤t->mm->mmap_sem); |
| 5907 | if (ret) { |
| 5908 | /* |
| 5909 | * if we did partial map, or found file backed vmas, |
| 5910 | * release any pages we did get |
| 5911 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 5912 | if (pret > 0) |
| 5913 | put_user_pages(pages, pret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5914 | if (ctx->account_mem) |
| 5915 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5916 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5917 | goto err; |
| 5918 | } |
| 5919 | |
| 5920 | off = ubuf & ~PAGE_MASK; |
| 5921 | size = iov.iov_len; |
| 5922 | for (j = 0; j < nr_pages; j++) { |
| 5923 | size_t vec_len; |
| 5924 | |
| 5925 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 5926 | imu->bvec[j].bv_page = pages[j]; |
| 5927 | imu->bvec[j].bv_len = vec_len; |
| 5928 | imu->bvec[j].bv_offset = off; |
| 5929 | off = 0; |
| 5930 | size -= vec_len; |
| 5931 | } |
| 5932 | /* store original address for later verification */ |
| 5933 | imu->ubuf = ubuf; |
| 5934 | imu->len = iov.iov_len; |
| 5935 | imu->nr_bvecs = nr_pages; |
| 5936 | |
| 5937 | ctx->nr_user_bufs++; |
| 5938 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5939 | kvfree(pages); |
| 5940 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5941 | return 0; |
| 5942 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5943 | kvfree(pages); |
| 5944 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5945 | io_sqe_buffer_unregister(ctx); |
| 5946 | return ret; |
| 5947 | } |
| 5948 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 5949 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 5950 | { |
| 5951 | __s32 __user *fds = arg; |
| 5952 | int fd; |
| 5953 | |
| 5954 | if (ctx->cq_ev_fd) |
| 5955 | return -EBUSY; |
| 5956 | |
| 5957 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 5958 | return -EFAULT; |
| 5959 | |
| 5960 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 5961 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 5962 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 5963 | ctx->cq_ev_fd = NULL; |
| 5964 | return ret; |
| 5965 | } |
| 5966 | |
| 5967 | return 0; |
| 5968 | } |
| 5969 | |
| 5970 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 5971 | { |
| 5972 | if (ctx->cq_ev_fd) { |
| 5973 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 5974 | ctx->cq_ev_fd = NULL; |
| 5975 | return 0; |
| 5976 | } |
| 5977 | |
| 5978 | return -ENXIO; |
| 5979 | } |
| 5980 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5981 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 5982 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5983 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5984 | if (ctx->sqo_mm) |
| 5985 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5986 | |
| 5987 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5988 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5989 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 5990 | io_eventfd_unregister(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 5991 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5992 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 5993 | if (ctx->ring_sock) { |
| 5994 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5995 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 5996 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5997 | #endif |
| 5998 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5999 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6000 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6001 | |
| 6002 | percpu_ref_exit(&ctx->refs); |
| 6003 | if (ctx->account_mem) |
| 6004 | io_unaccount_mem(ctx->user, |
| 6005 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 6006 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6007 | put_cred(ctx->creds); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6008 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 6009 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 6010 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6011 | kfree(ctx); |
| 6012 | } |
| 6013 | |
| 6014 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 6015 | { |
| 6016 | struct io_ring_ctx *ctx = file->private_data; |
| 6017 | __poll_t mask = 0; |
| 6018 | |
| 6019 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 6020 | /* |
| 6021 | * synchronizes with barrier from wq_has_sleeper call in |
| 6022 | * io_commit_cqring |
| 6023 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6024 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6025 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 6026 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6027 | mask |= EPOLLOUT | EPOLLWRNORM; |
yangerkun | daa5de5 | 2019-09-24 20:53:34 +0800 | [diff] [blame] | 6028 | if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6029 | mask |= EPOLLIN | EPOLLRDNORM; |
| 6030 | |
| 6031 | return mask; |
| 6032 | } |
| 6033 | |
| 6034 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 6035 | { |
| 6036 | struct io_ring_ctx *ctx = file->private_data; |
| 6037 | |
| 6038 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 6039 | } |
| 6040 | |
| 6041 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 6042 | { |
| 6043 | mutex_lock(&ctx->uring_lock); |
| 6044 | percpu_ref_kill(&ctx->refs); |
| 6045 | mutex_unlock(&ctx->uring_lock); |
| 6046 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6047 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6048 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6049 | |
| 6050 | if (ctx->io_wq) |
| 6051 | io_wq_cancel_all(ctx->io_wq); |
| 6052 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6053 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 6054 | /* if we failed setting up the ctx, we might not have any rings */ |
| 6055 | if (ctx->rings) |
| 6056 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6057 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6058 | io_ring_ctx_free(ctx); |
| 6059 | } |
| 6060 | |
| 6061 | static int io_uring_release(struct inode *inode, struct file *file) |
| 6062 | { |
| 6063 | struct io_ring_ctx *ctx = file->private_data; |
| 6064 | |
| 6065 | file->private_data = NULL; |
| 6066 | io_ring_ctx_wait_and_kill(ctx); |
| 6067 | return 0; |
| 6068 | } |
| 6069 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6070 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 6071 | struct files_struct *files) |
| 6072 | { |
| 6073 | struct io_kiocb *req; |
| 6074 | DEFINE_WAIT(wait); |
| 6075 | |
| 6076 | while (!list_empty_careful(&ctx->inflight_list)) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6077 | struct io_kiocb *cancel_req = NULL; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6078 | |
| 6079 | spin_lock_irq(&ctx->inflight_lock); |
| 6080 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6081 | if (req->work.files != files) |
| 6082 | continue; |
| 6083 | /* req is being completed, ignore */ |
| 6084 | if (!refcount_inc_not_zero(&req->refs)) |
| 6085 | continue; |
| 6086 | cancel_req = req; |
| 6087 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6088 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6089 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6090 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6091 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6092 | spin_unlock_irq(&ctx->inflight_lock); |
| 6093 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6094 | /* We need to keep going until we don't find a matching req */ |
| 6095 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6096 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 6097 | |
| 6098 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 6099 | io_put_req(cancel_req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6100 | schedule(); |
| 6101 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6102 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6103 | } |
| 6104 | |
| 6105 | static int io_uring_flush(struct file *file, void *data) |
| 6106 | { |
| 6107 | struct io_ring_ctx *ctx = file->private_data; |
| 6108 | |
| 6109 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6110 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
| 6111 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6112 | io_wq_cancel_all(ctx->io_wq); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6113 | } |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6114 | return 0; |
| 6115 | } |
| 6116 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6117 | static void *io_uring_validate_mmap_request(struct file *file, |
| 6118 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6119 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6120 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6121 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6122 | struct page *page; |
| 6123 | void *ptr; |
| 6124 | |
| 6125 | switch (offset) { |
| 6126 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6127 | case IORING_OFF_CQ_RING: |
| 6128 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6129 | break; |
| 6130 | case IORING_OFF_SQES: |
| 6131 | ptr = ctx->sq_sqes; |
| 6132 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6133 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6134 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6135 | } |
| 6136 | |
| 6137 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 6138 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6139 | return ERR_PTR(-EINVAL); |
| 6140 | |
| 6141 | return ptr; |
| 6142 | } |
| 6143 | |
| 6144 | #ifdef CONFIG_MMU |
| 6145 | |
| 6146 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6147 | { |
| 6148 | size_t sz = vma->vm_end - vma->vm_start; |
| 6149 | unsigned long pfn; |
| 6150 | void *ptr; |
| 6151 | |
| 6152 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 6153 | if (IS_ERR(ptr)) |
| 6154 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6155 | |
| 6156 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 6157 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 6158 | } |
| 6159 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6160 | #else /* !CONFIG_MMU */ |
| 6161 | |
| 6162 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6163 | { |
| 6164 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 6165 | } |
| 6166 | |
| 6167 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 6168 | { |
| 6169 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 6170 | } |
| 6171 | |
| 6172 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 6173 | unsigned long addr, unsigned long len, |
| 6174 | unsigned long pgoff, unsigned long flags) |
| 6175 | { |
| 6176 | void *ptr; |
| 6177 | |
| 6178 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 6179 | if (IS_ERR(ptr)) |
| 6180 | return PTR_ERR(ptr); |
| 6181 | |
| 6182 | return (unsigned long) ptr; |
| 6183 | } |
| 6184 | |
| 6185 | #endif /* !CONFIG_MMU */ |
| 6186 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6187 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 6188 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 6189 | size_t, sigsz) |
| 6190 | { |
| 6191 | struct io_ring_ctx *ctx; |
| 6192 | long ret = -EBADF; |
| 6193 | int submitted = 0; |
| 6194 | struct fd f; |
| 6195 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6196 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6197 | return -EINVAL; |
| 6198 | |
| 6199 | f = fdget(fd); |
| 6200 | if (!f.file) |
| 6201 | return -EBADF; |
| 6202 | |
| 6203 | ret = -EOPNOTSUPP; |
| 6204 | if (f.file->f_op != &io_uring_fops) |
| 6205 | goto out_fput; |
| 6206 | |
| 6207 | ret = -ENXIO; |
| 6208 | ctx = f.file->private_data; |
| 6209 | if (!percpu_ref_tryget(&ctx->refs)) |
| 6210 | goto out_fput; |
| 6211 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6212 | /* |
| 6213 | * For SQ polling, the thread will do all submissions and completions. |
| 6214 | * Just return the requested submit count, and wake the thread if |
| 6215 | * we were asked to. |
| 6216 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6217 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6218 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6219 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 6220 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6221 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 6222 | wake_up(&ctx->sqo_wait); |
| 6223 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6224 | } else if (to_submit) { |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6225 | struct mm_struct *cur_mm; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6226 | |
Jens Axboe | 44d2827 | 2020-01-16 19:00:24 -0700 | [diff] [blame] | 6227 | if (current->mm != ctx->sqo_mm || |
| 6228 | current_cred() != ctx->creds) { |
| 6229 | ret = -EPERM; |
| 6230 | goto out; |
| 6231 | } |
| 6232 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6233 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6234 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
| 6235 | cur_mm = ctx->sqo_mm; |
| 6236 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, |
| 6237 | &cur_mm, false); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6238 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6239 | |
| 6240 | if (submitted != to_submit) |
| 6241 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6242 | } |
| 6243 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6244 | unsigned nr_events = 0; |
| 6245 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6246 | min_complete = min(min_complete, ctx->cq_entries); |
| 6247 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6248 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6249 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6250 | } else { |
| 6251 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 6252 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6253 | } |
| 6254 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6255 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 6256 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6257 | out_fput: |
| 6258 | fdput(f); |
| 6259 | return submitted ? submitted : ret; |
| 6260 | } |
| 6261 | |
| 6262 | static const struct file_operations io_uring_fops = { |
| 6263 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6264 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6265 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6266 | #ifndef CONFIG_MMU |
| 6267 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 6268 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 6269 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6270 | .poll = io_uring_poll, |
| 6271 | .fasync = io_uring_fasync, |
| 6272 | }; |
| 6273 | |
| 6274 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 6275 | struct io_uring_params *p) |
| 6276 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6277 | struct io_rings *rings; |
| 6278 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6279 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6280 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 6281 | if (size == SIZE_MAX) |
| 6282 | return -EOVERFLOW; |
| 6283 | |
| 6284 | rings = io_mem_alloc(size); |
| 6285 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6286 | return -ENOMEM; |
| 6287 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6288 | ctx->rings = rings; |
| 6289 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 6290 | rings->sq_ring_mask = p->sq_entries - 1; |
| 6291 | rings->cq_ring_mask = p->cq_entries - 1; |
| 6292 | rings->sq_ring_entries = p->sq_entries; |
| 6293 | rings->cq_ring_entries = p->cq_entries; |
| 6294 | ctx->sq_mask = rings->sq_ring_mask; |
| 6295 | ctx->cq_mask = rings->cq_ring_mask; |
| 6296 | ctx->sq_entries = rings->sq_ring_entries; |
| 6297 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6298 | |
| 6299 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6300 | if (size == SIZE_MAX) { |
| 6301 | io_mem_free(ctx->rings); |
| 6302 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6303 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6304 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6305 | |
| 6306 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6307 | if (!ctx->sq_sqes) { |
| 6308 | io_mem_free(ctx->rings); |
| 6309 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6310 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6311 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6312 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6313 | return 0; |
| 6314 | } |
| 6315 | |
| 6316 | /* |
| 6317 | * Allocate an anonymous fd, this is what constitutes the application |
| 6318 | * visible backing of an io_uring instance. The application mmaps this |
| 6319 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 6320 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 6321 | */ |
| 6322 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 6323 | { |
| 6324 | struct file *file; |
| 6325 | int ret; |
| 6326 | |
| 6327 | #if defined(CONFIG_UNIX) |
| 6328 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 6329 | &ctx->ring_sock); |
| 6330 | if (ret) |
| 6331 | return ret; |
| 6332 | #endif |
| 6333 | |
| 6334 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 6335 | if (ret < 0) |
| 6336 | goto err; |
| 6337 | |
| 6338 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 6339 | O_RDWR | O_CLOEXEC); |
| 6340 | if (IS_ERR(file)) { |
| 6341 | put_unused_fd(ret); |
| 6342 | ret = PTR_ERR(file); |
| 6343 | goto err; |
| 6344 | } |
| 6345 | |
| 6346 | #if defined(CONFIG_UNIX) |
| 6347 | ctx->ring_sock->file = file; |
| 6348 | #endif |
| 6349 | fd_install(ret, file); |
| 6350 | return ret; |
| 6351 | err: |
| 6352 | #if defined(CONFIG_UNIX) |
| 6353 | sock_release(ctx->ring_sock); |
| 6354 | ctx->ring_sock = NULL; |
| 6355 | #endif |
| 6356 | return ret; |
| 6357 | } |
| 6358 | |
| 6359 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 6360 | { |
| 6361 | struct user_struct *user = NULL; |
| 6362 | struct io_ring_ctx *ctx; |
| 6363 | bool account_mem; |
| 6364 | int ret; |
| 6365 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6366 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6367 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6368 | if (entries > IORING_MAX_ENTRIES) { |
| 6369 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6370 | return -EINVAL; |
| 6371 | entries = IORING_MAX_ENTRIES; |
| 6372 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6373 | |
| 6374 | /* |
| 6375 | * Use twice as many entries for the CQ ring. It's possible for the |
| 6376 | * application to drive a higher depth than the size of the SQ ring, |
| 6377 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6378 | * some flexibility in overcommitting a bit. If the application has |
| 6379 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 6380 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6381 | */ |
| 6382 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6383 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 6384 | /* |
| 6385 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 6386 | * to a power-of-two, if it isn't already. We do NOT impose |
| 6387 | * any cq vs sq ring sizing. |
| 6388 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6389 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6390 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6391 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 6392 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6393 | return -EINVAL; |
| 6394 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 6395 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6396 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 6397 | } else { |
| 6398 | p->cq_entries = 2 * p->sq_entries; |
| 6399 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6400 | |
| 6401 | user = get_uid(current_user()); |
| 6402 | account_mem = !capable(CAP_IPC_LOCK); |
| 6403 | |
| 6404 | if (account_mem) { |
| 6405 | ret = io_account_mem(user, |
| 6406 | ring_pages(p->sq_entries, p->cq_entries)); |
| 6407 | if (ret) { |
| 6408 | free_uid(user); |
| 6409 | return ret; |
| 6410 | } |
| 6411 | } |
| 6412 | |
| 6413 | ctx = io_ring_ctx_alloc(p); |
| 6414 | if (!ctx) { |
| 6415 | if (account_mem) |
| 6416 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 6417 | p->cq_entries)); |
| 6418 | free_uid(user); |
| 6419 | return -ENOMEM; |
| 6420 | } |
| 6421 | ctx->compat = in_compat_syscall(); |
| 6422 | ctx->account_mem = account_mem; |
| 6423 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 6424 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6425 | |
| 6426 | ret = io_allocate_scq_urings(ctx, p); |
| 6427 | if (ret) |
| 6428 | goto err; |
| 6429 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6430 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6431 | if (ret) |
| 6432 | goto err; |
| 6433 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6434 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6435 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 6436 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 6437 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 6438 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 6439 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 6440 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 6441 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6442 | |
| 6443 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6444 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 6445 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 6446 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 6447 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 6448 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 6449 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 6450 | |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 6451 | /* |
| 6452 | * Install ring fd as the very last thing, so we don't risk someone |
| 6453 | * having closed it before we finish setup |
| 6454 | */ |
| 6455 | ret = io_uring_get_fd(ctx); |
| 6456 | if (ret < 0) |
| 6457 | goto err; |
| 6458 | |
Jens Axboe | da8c969 | 2019-12-02 18:51:26 -0700 | [diff] [blame] | 6459 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 6460 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6461 | 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] | 6462 | return ret; |
| 6463 | err: |
| 6464 | io_ring_ctx_wait_and_kill(ctx); |
| 6465 | return ret; |
| 6466 | } |
| 6467 | |
| 6468 | /* |
| 6469 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 6470 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 6471 | * params structure passed in. |
| 6472 | */ |
| 6473 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 6474 | { |
| 6475 | struct io_uring_params p; |
| 6476 | long ret; |
| 6477 | int i; |
| 6478 | |
| 6479 | if (copy_from_user(&p, params, sizeof(p))) |
| 6480 | return -EFAULT; |
| 6481 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 6482 | if (p.resv[i]) |
| 6483 | return -EINVAL; |
| 6484 | } |
| 6485 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6486 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6487 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
| 6488 | IORING_SETUP_CLAMP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6489 | return -EINVAL; |
| 6490 | |
| 6491 | ret = io_uring_create(entries, &p); |
| 6492 | if (ret < 0) |
| 6493 | return ret; |
| 6494 | |
| 6495 | if (copy_to_user(params, &p, sizeof(p))) |
| 6496 | return -EFAULT; |
| 6497 | |
| 6498 | return ret; |
| 6499 | } |
| 6500 | |
| 6501 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 6502 | struct io_uring_params __user *, params) |
| 6503 | { |
| 6504 | return io_uring_setup(entries, params); |
| 6505 | } |
| 6506 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6507 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 6508 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 6509 | __releases(ctx->uring_lock) |
| 6510 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6511 | { |
| 6512 | int ret; |
| 6513 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 6514 | /* |
| 6515 | * We're inside the ring mutex, if the ref is already dying, then |
| 6516 | * someone else killed the ctx or is already going through |
| 6517 | * io_uring_register(). |
| 6518 | */ |
| 6519 | if (percpu_ref_is_dying(&ctx->refs)) |
| 6520 | return -ENXIO; |
| 6521 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6522 | if (opcode != IORING_UNREGISTER_FILES && |
| 6523 | opcode != IORING_REGISTER_FILES_UPDATE) { |
| 6524 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 6525 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6526 | /* |
| 6527 | * Drop uring mutex before waiting for references to exit. If |
| 6528 | * another thread is currently inside io_uring_enter() it might |
| 6529 | * need to grab the uring_lock to make progress. If we hold it |
| 6530 | * here across the drain wait, then we can deadlock. It's safe |
| 6531 | * to drop the mutex here, since no new references will come in |
| 6532 | * after we've killed the percpu ref. |
| 6533 | */ |
| 6534 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6535 | ret = wait_for_completion_interruptible(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6536 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6537 | if (ret) { |
| 6538 | percpu_ref_resurrect(&ctx->refs); |
| 6539 | ret = -EINTR; |
| 6540 | goto out; |
| 6541 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6542 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6543 | |
| 6544 | switch (opcode) { |
| 6545 | case IORING_REGISTER_BUFFERS: |
| 6546 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 6547 | break; |
| 6548 | case IORING_UNREGISTER_BUFFERS: |
| 6549 | ret = -EINVAL; |
| 6550 | if (arg || nr_args) |
| 6551 | break; |
| 6552 | ret = io_sqe_buffer_unregister(ctx); |
| 6553 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6554 | case IORING_REGISTER_FILES: |
| 6555 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 6556 | break; |
| 6557 | case IORING_UNREGISTER_FILES: |
| 6558 | ret = -EINVAL; |
| 6559 | if (arg || nr_args) |
| 6560 | break; |
| 6561 | ret = io_sqe_files_unregister(ctx); |
| 6562 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6563 | case IORING_REGISTER_FILES_UPDATE: |
| 6564 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 6565 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6566 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 6567 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6568 | ret = -EINVAL; |
| 6569 | if (nr_args != 1) |
| 6570 | break; |
| 6571 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 6572 | if (ret) |
| 6573 | break; |
| 6574 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 6575 | ctx->eventfd_async = 1; |
| 6576 | else |
| 6577 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6578 | break; |
| 6579 | case IORING_UNREGISTER_EVENTFD: |
| 6580 | ret = -EINVAL; |
| 6581 | if (arg || nr_args) |
| 6582 | break; |
| 6583 | ret = io_eventfd_unregister(ctx); |
| 6584 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6585 | default: |
| 6586 | ret = -EINVAL; |
| 6587 | break; |
| 6588 | } |
| 6589 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6590 | |
| 6591 | if (opcode != IORING_UNREGISTER_FILES && |
| 6592 | opcode != IORING_REGISTER_FILES_UPDATE) { |
| 6593 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6594 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6595 | out: |
| 6596 | reinit_completion(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6597 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6598 | return ret; |
| 6599 | } |
| 6600 | |
| 6601 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 6602 | void __user *, arg, unsigned int, nr_args) |
| 6603 | { |
| 6604 | struct io_ring_ctx *ctx; |
| 6605 | long ret = -EBADF; |
| 6606 | struct fd f; |
| 6607 | |
| 6608 | f = fdget(fd); |
| 6609 | if (!f.file) |
| 6610 | return -EBADF; |
| 6611 | |
| 6612 | ret = -EOPNOTSUPP; |
| 6613 | if (f.file->f_op != &io_uring_fops) |
| 6614 | goto out_fput; |
| 6615 | |
| 6616 | ctx = f.file->private_data; |
| 6617 | |
| 6618 | mutex_lock(&ctx->uring_lock); |
| 6619 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 6620 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6621 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 6622 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6623 | out_fput: |
| 6624 | fdput(f); |
| 6625 | return ret; |
| 6626 | } |
| 6627 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6628 | static int __init io_uring_init(void) |
| 6629 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6630 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6631 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 6632 | return 0; |
| 6633 | }; |
| 6634 | __initcall(io_uring_init); |