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> |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 49 | #include <linux/bits.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 50 | |
| 51 | #include <linux/sched/signal.h> |
| 52 | #include <linux/fs.h> |
| 53 | #include <linux/file.h> |
| 54 | #include <linux/fdtable.h> |
| 55 | #include <linux/mm.h> |
| 56 | #include <linux/mman.h> |
| 57 | #include <linux/mmu_context.h> |
| 58 | #include <linux/percpu.h> |
| 59 | #include <linux/slab.h> |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 60 | #include <linux/kthread.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 61 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 62 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 63 | #include <linux/net.h> |
| 64 | #include <net/sock.h> |
| 65 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 66 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 67 | #include <linux/anon_inodes.h> |
| 68 | #include <linux/sched/mm.h> |
| 69 | #include <linux/uaccess.h> |
| 70 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 71 | #include <linux/sizes.h> |
| 72 | #include <linux/hugetlb.h> |
Jens Axboe | aa4c396 | 2019-11-29 10:14:00 -0700 | [diff] [blame] | 73 | #include <linux/highmem.h> |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 74 | #include <linux/namei.h> |
| 75 | #include <linux/fsnotify.h> |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 76 | #include <linux/fadvise.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 77 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 78 | #define CREATE_TRACE_POINTS |
| 79 | #include <trace/events/io_uring.h> |
| 80 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 81 | #include <uapi/linux/io_uring.h> |
| 82 | |
| 83 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 84 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 85 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 86 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 87 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 91 | */ |
| 92 | #define IORING_FILE_TABLE_SHIFT 9 |
| 93 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 94 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 95 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 96 | |
| 97 | struct io_uring { |
| 98 | u32 head ____cacheline_aligned_in_smp; |
| 99 | u32 tail ____cacheline_aligned_in_smp; |
| 100 | }; |
| 101 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 102 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 103 | * This data is shared with the application through the mmap at offsets |
| 104 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 105 | * |
| 106 | * The offsets to the member fields are published through struct |
| 107 | * io_sqring_offsets when calling io_uring_setup. |
| 108 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 109 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 110 | /* |
| 111 | * Head and tail offsets into the ring; the offsets need to be |
| 112 | * masked to get valid indices. |
| 113 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 114 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 115 | * and the application controls tail of the sq ring and the head of the |
| 116 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 117 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 118 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 119 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 120 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 121 | * ring_entries - 1) |
| 122 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 123 | u32 sq_ring_mask, cq_ring_mask; |
| 124 | /* Ring sizes (constant, power of 2) */ |
| 125 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 126 | /* |
| 127 | * Number of invalid entries dropped by the kernel due to |
| 128 | * invalid index stored in array |
| 129 | * |
| 130 | * Written by the kernel, shouldn't be modified by the |
| 131 | * application (i.e. get number of "new events" by comparing to |
| 132 | * cached value). |
| 133 | * |
| 134 | * After a new SQ head value was read by the application this |
| 135 | * counter includes all submissions that were dropped reaching |
| 136 | * the new SQ head (and possibly more). |
| 137 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 138 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 139 | /* |
| 140 | * Runtime flags |
| 141 | * |
| 142 | * Written by the kernel, shouldn't be modified by the |
| 143 | * application. |
| 144 | * |
| 145 | * The application needs a full memory barrier before checking |
| 146 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 147 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 148 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 149 | /* |
| 150 | * Number of completion events lost because the queue was full; |
| 151 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 152 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 153 | * the completion queue. |
| 154 | * |
| 155 | * Written by the kernel, shouldn't be modified by the |
| 156 | * application (i.e. get number of "new events" by comparing to |
| 157 | * cached value). |
| 158 | * |
| 159 | * As completion events come in out of order this counter is not |
| 160 | * ordered with any other data. |
| 161 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 162 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 163 | /* |
| 164 | * Ring buffer of completion events. |
| 165 | * |
| 166 | * The kernel writes completion events fresh every time they are |
| 167 | * produced, so the application is allowed to modify pending |
| 168 | * entries. |
| 169 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 170 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 173 | struct io_mapped_ubuf { |
| 174 | u64 ubuf; |
| 175 | size_t len; |
| 176 | struct bio_vec *bvec; |
| 177 | unsigned int nr_bvecs; |
| 178 | }; |
| 179 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 180 | struct fixed_file_table { |
| 181 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 184 | enum { |
| 185 | FFD_F_ATOMIC, |
| 186 | }; |
| 187 | |
| 188 | struct fixed_file_data { |
| 189 | struct fixed_file_table *table; |
| 190 | struct io_ring_ctx *ctx; |
| 191 | |
| 192 | struct percpu_ref refs; |
| 193 | struct llist_head put_llist; |
| 194 | unsigned long state; |
| 195 | struct work_struct ref_work; |
| 196 | struct completion done; |
| 197 | }; |
| 198 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 199 | struct io_ring_ctx { |
| 200 | struct { |
| 201 | struct percpu_ref refs; |
| 202 | } ____cacheline_aligned_in_smp; |
| 203 | |
| 204 | struct { |
| 205 | unsigned int flags; |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 206 | int compat: 1; |
| 207 | int account_mem: 1; |
| 208 | int cq_overflow_flushed: 1; |
| 209 | int drain_next: 1; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 210 | int eventfd_async: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 211 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 212 | /* |
| 213 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 214 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 215 | * |
| 216 | * This indirection could e.g. be used to assign fixed |
| 217 | * io_uring_sqe entries to operations and only submit them to |
| 218 | * the queue when needed. |
| 219 | * |
| 220 | * The kernel modifies neither the indices array nor the entries |
| 221 | * array. |
| 222 | */ |
| 223 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 224 | unsigned cached_sq_head; |
| 225 | unsigned sq_entries; |
| 226 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 227 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 228 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 229 | atomic_t cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 230 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 231 | |
| 232 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 233 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 234 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 235 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 236 | wait_queue_head_t inflight_wait; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 237 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 238 | } ____cacheline_aligned_in_smp; |
| 239 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 240 | struct io_rings *rings; |
| 241 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 242 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 243 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 244 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 245 | struct mm_struct *sqo_mm; |
| 246 | wait_queue_head_t sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 247 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 248 | /* |
| 249 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 250 | * readers must ensure that ->refs is alive as long as the file* is |
| 251 | * used. Only updated through io_uring_register(2). |
| 252 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 253 | struct fixed_file_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 254 | unsigned nr_user_files; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 255 | int ring_fd; |
| 256 | struct file *ring_file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 257 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 258 | /* if used, fixed mapped user buffers */ |
| 259 | unsigned nr_user_bufs; |
| 260 | struct io_mapped_ubuf *user_bufs; |
| 261 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 262 | struct user_struct *user; |
| 263 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 264 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 265 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 266 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
| 267 | struct completion *completions; |
| 268 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 269 | /* if all else fails... */ |
| 270 | struct io_kiocb *fallback_req; |
| 271 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 272 | #if defined(CONFIG_UNIX) |
| 273 | struct socket *ring_sock; |
| 274 | #endif |
| 275 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame^] | 276 | struct idr personality_idr; |
| 277 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 278 | struct { |
| 279 | unsigned cached_cq_tail; |
| 280 | unsigned cq_entries; |
| 281 | unsigned cq_mask; |
| 282 | atomic_t cq_timeouts; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 283 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 284 | struct wait_queue_head cq_wait; |
| 285 | struct fasync_struct *cq_fasync; |
| 286 | struct eventfd_ctx *cq_ev_fd; |
| 287 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 288 | |
| 289 | struct { |
| 290 | struct mutex uring_lock; |
| 291 | wait_queue_head_t wait; |
| 292 | } ____cacheline_aligned_in_smp; |
| 293 | |
| 294 | struct { |
| 295 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 296 | struct llist_head poll_llist; |
| 297 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 298 | /* |
| 299 | * ->poll_list is protected by the ctx->uring_lock for |
| 300 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 301 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 302 | * manipulate the list, hence no extra locking is needed there. |
| 303 | */ |
| 304 | struct list_head poll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 305 | struct hlist_head *cancel_hash; |
| 306 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 307 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 308 | |
| 309 | spinlock_t inflight_lock; |
| 310 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 311 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 312 | }; |
| 313 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 314 | /* |
| 315 | * First field must be the file pointer in all the |
| 316 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 317 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 318 | struct io_poll_iocb { |
| 319 | struct file *file; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 320 | union { |
| 321 | struct wait_queue_head *head; |
| 322 | u64 addr; |
| 323 | }; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 324 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 325 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 326 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 327 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 328 | }; |
| 329 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 330 | struct io_close { |
| 331 | struct file *file; |
| 332 | struct file *put_file; |
| 333 | int fd; |
| 334 | }; |
| 335 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 336 | struct io_timeout_data { |
| 337 | struct io_kiocb *req; |
| 338 | struct hrtimer timer; |
| 339 | struct timespec64 ts; |
| 340 | enum hrtimer_mode mode; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 341 | u32 seq_offset; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 342 | }; |
| 343 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 344 | struct io_accept { |
| 345 | struct file *file; |
| 346 | struct sockaddr __user *addr; |
| 347 | int __user *addr_len; |
| 348 | int flags; |
| 349 | }; |
| 350 | |
| 351 | struct io_sync { |
| 352 | struct file *file; |
| 353 | loff_t len; |
| 354 | loff_t off; |
| 355 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 356 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 357 | }; |
| 358 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 359 | struct io_cancel { |
| 360 | struct file *file; |
| 361 | u64 addr; |
| 362 | }; |
| 363 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 364 | struct io_timeout { |
| 365 | struct file *file; |
| 366 | u64 addr; |
| 367 | int flags; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 368 | unsigned count; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 369 | }; |
| 370 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 371 | struct io_rw { |
| 372 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 373 | struct kiocb kiocb; |
| 374 | u64 addr; |
| 375 | u64 len; |
| 376 | }; |
| 377 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 378 | struct io_connect { |
| 379 | struct file *file; |
| 380 | struct sockaddr __user *addr; |
| 381 | int addr_len; |
| 382 | }; |
| 383 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 384 | struct io_sr_msg { |
| 385 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 386 | union { |
| 387 | struct user_msghdr __user *msg; |
| 388 | void __user *buf; |
| 389 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 390 | int msg_flags; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 391 | size_t len; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 392 | }; |
| 393 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 394 | struct io_open { |
| 395 | struct file *file; |
| 396 | int dfd; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 397 | union { |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 398 | unsigned mask; |
| 399 | }; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 400 | struct filename *filename; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 401 | struct statx __user *buffer; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 402 | struct open_how how; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 403 | }; |
| 404 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 405 | struct io_files_update { |
| 406 | struct file *file; |
| 407 | u64 arg; |
| 408 | u32 nr_args; |
| 409 | u32 offset; |
| 410 | }; |
| 411 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 412 | struct io_fadvise { |
| 413 | struct file *file; |
| 414 | u64 offset; |
| 415 | u32 len; |
| 416 | u32 advice; |
| 417 | }; |
| 418 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 419 | struct io_madvise { |
| 420 | struct file *file; |
| 421 | u64 addr; |
| 422 | u32 len; |
| 423 | u32 advice; |
| 424 | }; |
| 425 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 426 | struct io_async_connect { |
| 427 | struct sockaddr_storage address; |
| 428 | }; |
| 429 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 430 | struct io_async_msghdr { |
| 431 | struct iovec fast_iov[UIO_FASTIOV]; |
| 432 | struct iovec *iov; |
| 433 | struct sockaddr __user *uaddr; |
| 434 | struct msghdr msg; |
| 435 | }; |
| 436 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 437 | struct io_async_rw { |
| 438 | struct iovec fast_iov[UIO_FASTIOV]; |
| 439 | struct iovec *iov; |
| 440 | ssize_t nr_segs; |
| 441 | ssize_t size; |
| 442 | }; |
| 443 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 444 | struct io_async_open { |
| 445 | struct filename *filename; |
| 446 | }; |
| 447 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 448 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 449 | union { |
| 450 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 451 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 452 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 453 | struct io_timeout_data timeout; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 454 | struct io_async_open open; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 455 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 456 | }; |
| 457 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 458 | enum { |
| 459 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 460 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 461 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 462 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 463 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
| 464 | |
| 465 | REQ_F_LINK_NEXT_BIT, |
| 466 | REQ_F_FAIL_LINK_BIT, |
| 467 | REQ_F_INFLIGHT_BIT, |
| 468 | REQ_F_CUR_POS_BIT, |
| 469 | REQ_F_NOWAIT_BIT, |
| 470 | REQ_F_IOPOLL_COMPLETED_BIT, |
| 471 | REQ_F_LINK_TIMEOUT_BIT, |
| 472 | REQ_F_TIMEOUT_BIT, |
| 473 | REQ_F_ISREG_BIT, |
| 474 | REQ_F_MUST_PUNT_BIT, |
| 475 | REQ_F_TIMEOUT_NOSEQ_BIT, |
| 476 | REQ_F_COMP_LOCKED_BIT, |
| 477 | }; |
| 478 | |
| 479 | enum { |
| 480 | /* ctx owns file */ |
| 481 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 482 | /* drain existing IO first */ |
| 483 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 484 | /* linked sqes */ |
| 485 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 486 | /* doesn't sever on completion < 0 */ |
| 487 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 488 | /* IOSQE_ASYNC */ |
| 489 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
| 490 | |
| 491 | /* already grabbed next link */ |
| 492 | REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT), |
| 493 | /* fail rest of links */ |
| 494 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 495 | /* on inflight list */ |
| 496 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 497 | /* read/write uses file position */ |
| 498 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 499 | /* must not punt to workers */ |
| 500 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
| 501 | /* polled IO has completed */ |
| 502 | REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT), |
| 503 | /* has linked timeout */ |
| 504 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
| 505 | /* timeout request */ |
| 506 | REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT), |
| 507 | /* regular file */ |
| 508 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
| 509 | /* must be punted even for NONBLOCK */ |
| 510 | REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT), |
| 511 | /* no timeout sequence */ |
| 512 | REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT), |
| 513 | /* completion under lock */ |
| 514 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), |
| 515 | }; |
| 516 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 517 | /* |
| 518 | * NOTE! Each of the iocb union members has the file pointer |
| 519 | * as the first entry in their struct definition. So you can |
| 520 | * access the file pointer through any of the sub-structs, |
| 521 | * or directly as just 'ki_filp' in this struct. |
| 522 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 523 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 524 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 525 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 526 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 527 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 528 | struct io_accept accept; |
| 529 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 530 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 531 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 532 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 533 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 534 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 535 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 536 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 537 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 538 | struct io_madvise madvise; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 539 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 540 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 541 | struct io_async_ctx *io; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 542 | /* |
| 543 | * llist_node is only used for poll deferred completions |
| 544 | */ |
| 545 | struct llist_node llist_node; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 546 | bool has_user; |
| 547 | bool in_async; |
| 548 | bool needs_fixed_file; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 549 | u8 opcode; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 550 | |
| 551 | struct io_ring_ctx *ctx; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 552 | union { |
| 553 | struct list_head list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 554 | struct hlist_node hash_node; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 555 | }; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 556 | struct list_head link_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 557 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 558 | refcount_t refs; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 559 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 560 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 561 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 562 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 563 | struct list_head inflight_entry; |
| 564 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 565 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 566 | }; |
| 567 | |
| 568 | #define IO_PLUG_THRESHOLD 2 |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 569 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 570 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 571 | struct io_submit_state { |
| 572 | struct blk_plug plug; |
| 573 | |
| 574 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 575 | * io_kiocb alloc cache |
| 576 | */ |
| 577 | void *reqs[IO_IOPOLL_BATCH]; |
| 578 | unsigned int free_reqs; |
| 579 | unsigned int cur_req; |
| 580 | |
| 581 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 582 | * File reference cache |
| 583 | */ |
| 584 | struct file *file; |
| 585 | unsigned int fd; |
| 586 | unsigned int has_refs; |
| 587 | unsigned int used_refs; |
| 588 | unsigned int ios_left; |
| 589 | }; |
| 590 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 591 | struct io_op_def { |
| 592 | /* needs req->io allocated for deferral/async */ |
| 593 | unsigned async_ctx : 1; |
| 594 | /* needs current->mm setup, does mm access */ |
| 595 | unsigned needs_mm : 1; |
| 596 | /* needs req->file assigned */ |
| 597 | unsigned needs_file : 1; |
| 598 | /* needs req->file assigned IFF fd is >= 0 */ |
| 599 | unsigned fd_non_neg : 1; |
| 600 | /* hash wq insertion if file is a regular file */ |
| 601 | unsigned hash_reg_file : 1; |
| 602 | /* unbound wq insertion if file is a non-regular file */ |
| 603 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 604 | /* opcode is not supported by this kernel */ |
| 605 | unsigned not_supported : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 606 | }; |
| 607 | |
| 608 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 609 | [IORING_OP_NOP] = {}, |
| 610 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 611 | .async_ctx = 1, |
| 612 | .needs_mm = 1, |
| 613 | .needs_file = 1, |
| 614 | .unbound_nonreg_file = 1, |
| 615 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 616 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 617 | .async_ctx = 1, |
| 618 | .needs_mm = 1, |
| 619 | .needs_file = 1, |
| 620 | .hash_reg_file = 1, |
| 621 | .unbound_nonreg_file = 1, |
| 622 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 623 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 624 | .needs_file = 1, |
| 625 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 626 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 627 | .needs_file = 1, |
| 628 | .unbound_nonreg_file = 1, |
| 629 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 630 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 631 | .needs_file = 1, |
| 632 | .hash_reg_file = 1, |
| 633 | .unbound_nonreg_file = 1, |
| 634 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 635 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 636 | .needs_file = 1, |
| 637 | .unbound_nonreg_file = 1, |
| 638 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 639 | [IORING_OP_POLL_REMOVE] = {}, |
| 640 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 641 | .needs_file = 1, |
| 642 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 643 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 644 | .async_ctx = 1, |
| 645 | .needs_mm = 1, |
| 646 | .needs_file = 1, |
| 647 | .unbound_nonreg_file = 1, |
| 648 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 649 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 650 | .async_ctx = 1, |
| 651 | .needs_mm = 1, |
| 652 | .needs_file = 1, |
| 653 | .unbound_nonreg_file = 1, |
| 654 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 655 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 656 | .async_ctx = 1, |
| 657 | .needs_mm = 1, |
| 658 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 659 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
| 660 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 661 | .needs_mm = 1, |
| 662 | .needs_file = 1, |
| 663 | .unbound_nonreg_file = 1, |
| 664 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 665 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 666 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 667 | .async_ctx = 1, |
| 668 | .needs_mm = 1, |
| 669 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 670 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 671 | .async_ctx = 1, |
| 672 | .needs_mm = 1, |
| 673 | .needs_file = 1, |
| 674 | .unbound_nonreg_file = 1, |
| 675 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 676 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 677 | .needs_file = 1, |
| 678 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 679 | [IORING_OP_OPENAT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 680 | .needs_file = 1, |
| 681 | .fd_non_neg = 1, |
| 682 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 683 | [IORING_OP_CLOSE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 684 | .needs_file = 1, |
| 685 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 686 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 687 | .needs_mm = 1, |
| 688 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 689 | [IORING_OP_STATX] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 690 | .needs_mm = 1, |
| 691 | .needs_file = 1, |
| 692 | .fd_non_neg = 1, |
| 693 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 694 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 695 | .needs_mm = 1, |
| 696 | .needs_file = 1, |
| 697 | .unbound_nonreg_file = 1, |
| 698 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 699 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 700 | .needs_mm = 1, |
| 701 | .needs_file = 1, |
| 702 | .unbound_nonreg_file = 1, |
| 703 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 704 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 705 | .needs_file = 1, |
| 706 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 707 | [IORING_OP_MADVISE] = { |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 708 | .needs_mm = 1, |
| 709 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 710 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 711 | .needs_mm = 1, |
| 712 | .needs_file = 1, |
| 713 | .unbound_nonreg_file = 1, |
| 714 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 715 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 716 | .needs_mm = 1, |
| 717 | .needs_file = 1, |
| 718 | .unbound_nonreg_file = 1, |
| 719 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 720 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 721 | .needs_file = 1, |
| 722 | .fd_non_neg = 1, |
| 723 | }, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 724 | }; |
| 725 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 726 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 727 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 728 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 729 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 730 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 731 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 732 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 733 | struct io_uring_files_update *ip, |
| 734 | unsigned nr_args); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 735 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 736 | static struct kmem_cache *req_cachep; |
| 737 | |
| 738 | static const struct file_operations io_uring_fops; |
| 739 | |
| 740 | struct sock *io_uring_get_socket(struct file *file) |
| 741 | { |
| 742 | #if defined(CONFIG_UNIX) |
| 743 | if (file->f_op == &io_uring_fops) { |
| 744 | struct io_ring_ctx *ctx = file->private_data; |
| 745 | |
| 746 | return ctx->ring_sock->sk; |
| 747 | } |
| 748 | #endif |
| 749 | return NULL; |
| 750 | } |
| 751 | EXPORT_SYMBOL(io_uring_get_socket); |
| 752 | |
| 753 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 754 | { |
| 755 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 756 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 757 | complete(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 761 | { |
| 762 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 763 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 764 | |
| 765 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 766 | if (!ctx) |
| 767 | return NULL; |
| 768 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 769 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 770 | if (!ctx->fallback_req) |
| 771 | goto err; |
| 772 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 773 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
| 774 | if (!ctx->completions) |
| 775 | goto err; |
| 776 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 777 | /* |
| 778 | * Use 5 bits less than the max cq entries, that should give us around |
| 779 | * 32 entries per hash list if totally full and uniformly spread. |
| 780 | */ |
| 781 | hash_bits = ilog2(p->cq_entries); |
| 782 | hash_bits -= 5; |
| 783 | if (hash_bits <= 0) |
| 784 | hash_bits = 1; |
| 785 | ctx->cancel_hash_bits = hash_bits; |
| 786 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 787 | GFP_KERNEL); |
| 788 | if (!ctx->cancel_hash) |
| 789 | goto err; |
| 790 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 791 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 792 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 793 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 794 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 795 | |
| 796 | ctx->flags = p->flags; |
| 797 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 798 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 799 | init_completion(&ctx->completions[0]); |
| 800 | init_completion(&ctx->completions[1]); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame^] | 801 | idr_init(&ctx->personality_idr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 802 | mutex_init(&ctx->uring_lock); |
| 803 | init_waitqueue_head(&ctx->wait); |
| 804 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 805 | init_llist_head(&ctx->poll_llist); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 806 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 807 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 808 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 809 | init_waitqueue_head(&ctx->inflight_wait); |
| 810 | spin_lock_init(&ctx->inflight_lock); |
| 811 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 812 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 813 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 814 | if (ctx->fallback_req) |
| 815 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 816 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 817 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 818 | kfree(ctx); |
| 819 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 822 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 823 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 824 | struct io_ring_ctx *ctx = req->ctx; |
| 825 | |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 826 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 827 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 828 | } |
| 829 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 830 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 831 | { |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 832 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 833 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 834 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 835 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | 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] | 839 | { |
| 840 | struct io_kiocb *req; |
| 841 | |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 842 | 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] | 843 | if (req && !req_need_defer(req)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 844 | list_del_init(&req->list); |
| 845 | return req; |
| 846 | } |
| 847 | |
| 848 | return NULL; |
| 849 | } |
| 850 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 851 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 852 | { |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 853 | struct io_kiocb *req; |
| 854 | |
| 855 | 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] | 856 | if (req) { |
| 857 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 858 | return NULL; |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 859 | if (!__req_need_defer(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 860 | list_del_init(&req->list); |
| 861 | return req; |
| 862 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | return NULL; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 866 | } |
| 867 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 868 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 869 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 870 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 871 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 872 | /* order cqe stores with ring update */ |
| 873 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 874 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 875 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 876 | wake_up_interruptible(&ctx->cq_wait); |
| 877 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 881 | static inline void io_req_work_grab_env(struct io_kiocb *req, |
| 882 | const struct io_op_def *def) |
| 883 | { |
| 884 | if (!req->work.mm && def->needs_mm) { |
| 885 | mmgrab(current->mm); |
| 886 | req->work.mm = current->mm; |
| 887 | } |
| 888 | if (!req->work.creds) |
| 889 | req->work.creds = get_current_cred(); |
| 890 | } |
| 891 | |
| 892 | static inline void io_req_work_drop_env(struct io_kiocb *req) |
| 893 | { |
| 894 | if (req->work.mm) { |
| 895 | mmdrop(req->work.mm); |
| 896 | req->work.mm = NULL; |
| 897 | } |
| 898 | if (req->work.creds) { |
| 899 | put_cred(req->work.creds); |
| 900 | req->work.creds = NULL; |
| 901 | } |
| 902 | } |
| 903 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 904 | static inline bool io_prep_async_work(struct io_kiocb *req, |
| 905 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 906 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 907 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 908 | bool do_hashed = false; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 909 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 910 | if (req->flags & REQ_F_ISREG) { |
| 911 | if (def->hash_reg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 912 | do_hashed = true; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 913 | } else { |
| 914 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 915 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 916 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 917 | |
| 918 | io_req_work_grab_env(req, def); |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 919 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 920 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 921 | return do_hashed; |
| 922 | } |
| 923 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 924 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 925 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 926 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 927 | struct io_kiocb *link; |
| 928 | bool do_hashed; |
| 929 | |
| 930 | do_hashed = io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 931 | |
| 932 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, |
| 933 | req->flags); |
| 934 | if (!do_hashed) { |
| 935 | io_wq_enqueue(ctx->io_wq, &req->work); |
| 936 | } else { |
| 937 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, |
| 938 | file_inode(req->file)); |
| 939 | } |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 940 | |
| 941 | if (link) |
| 942 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 943 | } |
| 944 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 945 | static void io_kill_timeout(struct io_kiocb *req) |
| 946 | { |
| 947 | int ret; |
| 948 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 949 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 950 | if (ret != -1) { |
| 951 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 952 | list_del_init(&req->list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 953 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 954 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | |
| 958 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 959 | { |
| 960 | struct io_kiocb *req, *tmp; |
| 961 | |
| 962 | spin_lock_irq(&ctx->completion_lock); |
| 963 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 964 | io_kill_timeout(req); |
| 965 | spin_unlock_irq(&ctx->completion_lock); |
| 966 | } |
| 967 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 968 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 969 | { |
| 970 | struct io_kiocb *req; |
| 971 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 972 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 973 | io_kill_timeout(req); |
| 974 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 975 | __io_commit_cqring(ctx); |
| 976 | |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 977 | while ((req = io_get_deferred_req(ctx)) != NULL) |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 978 | io_queue_async_work(req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 979 | } |
| 980 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 981 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 982 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 983 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 984 | unsigned tail; |
| 985 | |
| 986 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 987 | /* |
| 988 | * writes to the cq entry need to come after reading head; the |
| 989 | * control dependency is enough as we're using WRITE_ONCE to |
| 990 | * fill the cq entry |
| 991 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 992 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 993 | return NULL; |
| 994 | |
| 995 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 996 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 999 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1000 | { |
| 1001 | if (!ctx->eventfd_async) |
| 1002 | return true; |
| 1003 | return io_wq_current_is_worker() || in_interrupt(); |
| 1004 | } |
| 1005 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1006 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| 1007 | { |
| 1008 | if (waitqueue_active(&ctx->wait)) |
| 1009 | wake_up(&ctx->wait); |
| 1010 | if (waitqueue_active(&ctx->sqo_wait)) |
| 1011 | wake_up(&ctx->sqo_wait); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1012 | if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1013 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1014 | } |
| 1015 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1016 | /* Returns true if there are no backlogged entries after the flush */ |
| 1017 | 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] | 1018 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1019 | struct io_rings *rings = ctx->rings; |
| 1020 | struct io_uring_cqe *cqe; |
| 1021 | struct io_kiocb *req; |
| 1022 | unsigned long flags; |
| 1023 | LIST_HEAD(list); |
| 1024 | |
| 1025 | if (!force) { |
| 1026 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1027 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1028 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 1029 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1030 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1034 | |
| 1035 | /* if force is set, the ring is going away. always drop after that */ |
| 1036 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1037 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1038 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1039 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1040 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1041 | cqe = io_get_cqring(ctx); |
| 1042 | if (!cqe && !force) |
| 1043 | break; |
| 1044 | |
| 1045 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 1046 | list); |
| 1047 | list_move(&req->list, &list); |
| 1048 | if (cqe) { |
| 1049 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1050 | WRITE_ONCE(cqe->res, req->result); |
| 1051 | WRITE_ONCE(cqe->flags, 0); |
| 1052 | } else { |
| 1053 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1054 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | io_commit_cqring(ctx); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1059 | if (cqe) { |
| 1060 | clear_bit(0, &ctx->sq_check_overflow); |
| 1061 | clear_bit(0, &ctx->cq_check_overflow); |
| 1062 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1063 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1064 | io_cqring_ev_posted(ctx); |
| 1065 | |
| 1066 | while (!list_empty(&list)) { |
| 1067 | req = list_first_entry(&list, struct io_kiocb, list); |
| 1068 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1069 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1070 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1071 | |
| 1072 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1075 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1076 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1077 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1078 | struct io_uring_cqe *cqe; |
| 1079 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1080 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1081 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1082 | /* |
| 1083 | * If we can't get a cq entry, userspace overflowed the |
| 1084 | * submission (by quite a lot). Increment the overflow count in |
| 1085 | * the ring. |
| 1086 | */ |
| 1087 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1088 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1089 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1090 | WRITE_ONCE(cqe->res, res); |
| 1091 | WRITE_ONCE(cqe->flags, 0); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1092 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1093 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1094 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1095 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1096 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1097 | set_bit(0, &ctx->sq_check_overflow); |
| 1098 | set_bit(0, &ctx->cq_check_overflow); |
| 1099 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1100 | refcount_inc(&req->refs); |
| 1101 | req->result = res; |
| 1102 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1106 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1107 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1108 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1109 | unsigned long flags; |
| 1110 | |
| 1111 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1112 | io_cqring_fill_event(req, res); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1113 | io_commit_cqring(ctx); |
| 1114 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1115 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1116 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1119 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1120 | { |
| 1121 | return req == (struct io_kiocb *) |
| 1122 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1123 | } |
| 1124 | |
| 1125 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1126 | { |
| 1127 | struct io_kiocb *req; |
| 1128 | |
| 1129 | req = ctx->fallback_req; |
| 1130 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) |
| 1131 | return req; |
| 1132 | |
| 1133 | return NULL; |
| 1134 | } |
| 1135 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1136 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 1137 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1138 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1139 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1140 | struct io_kiocb *req; |
| 1141 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1142 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1143 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1144 | if (unlikely(!req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1145 | goto fallback; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1146 | } else if (!state->free_reqs) { |
| 1147 | size_t sz; |
| 1148 | int ret; |
| 1149 | |
| 1150 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1151 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1152 | |
| 1153 | /* |
| 1154 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1155 | * retry single alloc to be on the safe side. |
| 1156 | */ |
| 1157 | if (unlikely(ret <= 0)) { |
| 1158 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1159 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1160 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1161 | ret = 1; |
| 1162 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1163 | state->free_reqs = ret - 1; |
| 1164 | state->cur_req = 1; |
| 1165 | req = state->reqs[0]; |
| 1166 | } else { |
| 1167 | req = state->reqs[state->cur_req]; |
| 1168 | state->free_reqs--; |
| 1169 | state->cur_req++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1172 | got_it: |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 1173 | req->io = NULL; |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 1174 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1175 | req->ctx = ctx; |
| 1176 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1177 | /* one is dropped after submission, the other at completion */ |
| 1178 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1179 | req->result = 0; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1180 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1181 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1182 | fallback: |
| 1183 | req = io_get_fallback_req(ctx); |
| 1184 | if (req) |
| 1185 | goto got_it; |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 1186 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1187 | return NULL; |
| 1188 | } |
| 1189 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1190 | static void __io_req_do_free(struct io_kiocb *req) |
| 1191 | { |
| 1192 | if (likely(!io_is_fallback_req(req))) |
| 1193 | kmem_cache_free(req_cachep, req); |
| 1194 | else |
| 1195 | clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req); |
| 1196 | } |
| 1197 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1198 | static void __io_req_aux_free(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1199 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1200 | struct io_ring_ctx *ctx = req->ctx; |
| 1201 | |
YueHaibing | 96fd84d | 2020-01-07 22:22:44 +0800 | [diff] [blame] | 1202 | kfree(req->io); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1203 | if (req->file) { |
| 1204 | if (req->flags & REQ_F_FIXED_FILE) |
| 1205 | percpu_ref_put(&ctx->file_data->refs); |
| 1206 | else |
| 1207 | fput(req->file); |
| 1208 | } |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 1209 | |
| 1210 | io_req_work_drop_env(req); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | static void __io_free_req(struct io_kiocb *req) |
| 1214 | { |
| 1215 | __io_req_aux_free(req); |
| 1216 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1217 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1218 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1219 | unsigned long flags; |
| 1220 | |
| 1221 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1222 | list_del(&req->inflight_entry); |
| 1223 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1224 | wake_up(&ctx->inflight_wait); |
| 1225 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1226 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1227 | |
| 1228 | percpu_ref_put(&req->ctx->refs); |
| 1229 | __io_req_do_free(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1230 | } |
| 1231 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1232 | struct req_batch { |
| 1233 | void *reqs[IO_IOPOLL_BATCH]; |
| 1234 | int to_free; |
| 1235 | int need_iter; |
| 1236 | }; |
| 1237 | |
| 1238 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) |
| 1239 | { |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1240 | int fixed_refs = rb->to_free; |
| 1241 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1242 | if (!rb->to_free) |
| 1243 | return; |
| 1244 | if (rb->need_iter) { |
| 1245 | int i, inflight = 0; |
| 1246 | unsigned long flags; |
| 1247 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1248 | fixed_refs = 0; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1249 | for (i = 0; i < rb->to_free; i++) { |
| 1250 | struct io_kiocb *req = rb->reqs[i]; |
| 1251 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1252 | if (req->flags & REQ_F_FIXED_FILE) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1253 | req->file = NULL; |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1254 | fixed_refs++; |
| 1255 | } |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1256 | if (req->flags & REQ_F_INFLIGHT) |
| 1257 | inflight++; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1258 | __io_req_aux_free(req); |
| 1259 | } |
| 1260 | if (!inflight) |
| 1261 | goto do_free; |
| 1262 | |
| 1263 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1264 | for (i = 0; i < rb->to_free; i++) { |
| 1265 | struct io_kiocb *req = rb->reqs[i]; |
| 1266 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1267 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1268 | list_del(&req->inflight_entry); |
| 1269 | if (!--inflight) |
| 1270 | break; |
| 1271 | } |
| 1272 | } |
| 1273 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1274 | |
| 1275 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1276 | wake_up(&ctx->inflight_wait); |
| 1277 | } |
| 1278 | do_free: |
| 1279 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1280 | if (fixed_refs) |
| 1281 | percpu_ref_put_many(&ctx->file_data->refs, fixed_refs); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1282 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1283 | rb->to_free = rb->need_iter = 0; |
| 1284 | } |
| 1285 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1286 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1287 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1288 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1289 | int ret; |
| 1290 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1291 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1292 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1293 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1294 | io_commit_cqring(ctx); |
| 1295 | req->flags &= ~REQ_F_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1296 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1297 | return true; |
| 1298 | } |
| 1299 | |
| 1300 | return false; |
| 1301 | } |
| 1302 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1303 | 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] | 1304 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1305 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1306 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1307 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1308 | /* Already got next link */ |
| 1309 | if (req->flags & REQ_F_LINK_NEXT) |
| 1310 | return; |
| 1311 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1312 | /* |
| 1313 | * The list should never be empty when we are called here. But could |
| 1314 | * potentially happen if the chain is messed up, check to be on the |
| 1315 | * safe side. |
| 1316 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1317 | while (!list_empty(&req->link_list)) { |
| 1318 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 1319 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1320 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1321 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 1322 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 1323 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1324 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1325 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1326 | continue; |
| 1327 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1328 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1329 | list_del_init(&req->link_list); |
| 1330 | if (!list_empty(&nxt->link_list)) |
| 1331 | nxt->flags |= REQ_F_LINK; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 1332 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1333 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1334 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1335 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1336 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1337 | if (wake_ev) |
| 1338 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | /* |
| 1342 | * Called if REQ_F_LINK is set, and we fail the head request |
| 1343 | */ |
| 1344 | static void io_fail_links(struct io_kiocb *req) |
| 1345 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1346 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1347 | unsigned long flags; |
| 1348 | |
| 1349 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1350 | |
| 1351 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1352 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1353 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1354 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1355 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1356 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1357 | |
| 1358 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1359 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1360 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1361 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1362 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1363 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1364 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1365 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1366 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1367 | |
| 1368 | io_commit_cqring(ctx); |
| 1369 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1370 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1371 | } |
| 1372 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1373 | 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] | 1374 | { |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1375 | if (likely(!(req->flags & REQ_F_LINK))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1376 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1377 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1378 | /* |
| 1379 | * If LINK is set, we have dependent requests in this chain. If we |
| 1380 | * didn't fail this request, queue the first one up, moving any other |
| 1381 | * dependencies to the next request. In case of failure, fail the rest |
| 1382 | * of the chain. |
| 1383 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1384 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1385 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1386 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1387 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1388 | struct io_ring_ctx *ctx = req->ctx; |
| 1389 | unsigned long flags; |
| 1390 | |
| 1391 | /* |
| 1392 | * If this is a timeout link, we could be racing with the |
| 1393 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1394 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1395 | */ |
| 1396 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1397 | io_req_link_next(req, nxt); |
| 1398 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1399 | } else { |
| 1400 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1401 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1402 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1403 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1404 | static void io_free_req(struct io_kiocb *req) |
| 1405 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1406 | struct io_kiocb *nxt = NULL; |
| 1407 | |
| 1408 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1409 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1410 | |
| 1411 | if (nxt) |
| 1412 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1413 | } |
| 1414 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1415 | /* |
| 1416 | * Drop reference to request, return next in chain (if there is one) if this |
| 1417 | * was the last reference to this request. |
| 1418 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1419 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1420 | 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] | 1421 | { |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1422 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1423 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1424 | if (refcount_dec_and_test(&req->refs)) |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1425 | __io_free_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1428 | static void io_put_req(struct io_kiocb *req) |
| 1429 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1430 | if (refcount_dec_and_test(&req->refs)) |
| 1431 | io_free_req(req); |
| 1432 | } |
| 1433 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1434 | /* |
| 1435 | * Must only be used if we don't need to care about links, usually from |
| 1436 | * within the completion handling itself. |
| 1437 | */ |
| 1438 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1439 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1440 | /* drop both submit and complete references */ |
| 1441 | if (refcount_sub_and_test(2, &req->refs)) |
| 1442 | __io_free_req(req); |
| 1443 | } |
| 1444 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1445 | static void io_double_put_req(struct io_kiocb *req) |
| 1446 | { |
| 1447 | /* drop both submit and complete references */ |
| 1448 | if (refcount_sub_and_test(2, &req->refs)) |
| 1449 | io_free_req(req); |
| 1450 | } |
| 1451 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1452 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1453 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1454 | struct io_rings *rings = ctx->rings; |
| 1455 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1456 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1457 | /* |
| 1458 | * noflush == true is from the waitqueue handler, just ensure |
| 1459 | * we wake up the task, and the next invocation will flush the |
| 1460 | * entries. We cannot safely to it from here. |
| 1461 | */ |
| 1462 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1463 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1464 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1465 | io_cqring_overflow_flush(ctx, false); |
| 1466 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1467 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1468 | /* See comment at the top of this file */ |
| 1469 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1470 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1471 | } |
| 1472 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1473 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1474 | { |
| 1475 | struct io_rings *rings = ctx->rings; |
| 1476 | |
| 1477 | /* make sure SQ entry isn't read before tail */ |
| 1478 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1479 | } |
| 1480 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1481 | 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] | 1482 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1483 | if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req)) |
| 1484 | return false; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1485 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1486 | if (!(req->flags & REQ_F_FIXED_FILE) || req->io) |
| 1487 | rb->need_iter++; |
| 1488 | |
| 1489 | rb->reqs[rb->to_free++] = req; |
| 1490 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1491 | io_free_req_many(req->ctx, rb); |
| 1492 | return true; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1495 | /* |
| 1496 | * Find and free completed poll iocbs |
| 1497 | */ |
| 1498 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1499 | struct list_head *done) |
| 1500 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1501 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1502 | struct io_kiocb *req; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1503 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1504 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1505 | while (!list_empty(done)) { |
| 1506 | req = list_first_entry(done, struct io_kiocb, list); |
| 1507 | list_del(&req->list); |
| 1508 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1509 | io_cqring_fill_event(req, req->result); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1510 | (*nr_events)++; |
| 1511 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1512 | if (refcount_dec_and_test(&req->refs) && |
| 1513 | !io_req_multi_free(&rb, req)) |
| 1514 | io_free_req(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1515 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1516 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1517 | io_commit_cqring(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1518 | io_free_req_many(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1522 | long min) |
| 1523 | { |
| 1524 | struct io_kiocb *req, *tmp; |
| 1525 | LIST_HEAD(done); |
| 1526 | bool spin; |
| 1527 | int ret; |
| 1528 | |
| 1529 | /* |
| 1530 | * Only spin for completions if we don't have multiple devices hanging |
| 1531 | * off our complete list, and we're under the requested amount. |
| 1532 | */ |
| 1533 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1534 | |
| 1535 | ret = 0; |
| 1536 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1537 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1538 | |
| 1539 | /* |
| 1540 | * Move completed entries to our local list. If we find a |
| 1541 | * request that requires polling, break out and complete |
| 1542 | * the done list first, if we have entries there. |
| 1543 | */ |
| 1544 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 1545 | list_move_tail(&req->list, &done); |
| 1546 | continue; |
| 1547 | } |
| 1548 | if (!list_empty(&done)) |
| 1549 | break; |
| 1550 | |
| 1551 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1552 | if (ret < 0) |
| 1553 | break; |
| 1554 | |
| 1555 | if (ret && spin) |
| 1556 | spin = false; |
| 1557 | ret = 0; |
| 1558 | } |
| 1559 | |
| 1560 | if (!list_empty(&done)) |
| 1561 | io_iopoll_complete(ctx, nr_events, &done); |
| 1562 | |
| 1563 | return ret; |
| 1564 | } |
| 1565 | |
| 1566 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 1567 | * 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] | 1568 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1569 | * as a non-spinning completion check. |
| 1570 | */ |
| 1571 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1572 | long min) |
| 1573 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1574 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1575 | int ret; |
| 1576 | |
| 1577 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1578 | if (ret < 0) |
| 1579 | return ret; |
| 1580 | if (!min || *nr_events >= min) |
| 1581 | return 0; |
| 1582 | } |
| 1583 | |
| 1584 | return 1; |
| 1585 | } |
| 1586 | |
| 1587 | /* |
| 1588 | * We can't just wait for polled events to come to us, we have to actively |
| 1589 | * find and complete them. |
| 1590 | */ |
| 1591 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1592 | { |
| 1593 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1594 | return; |
| 1595 | |
| 1596 | mutex_lock(&ctx->uring_lock); |
| 1597 | while (!list_empty(&ctx->poll_list)) { |
| 1598 | unsigned int nr_events = 0; |
| 1599 | |
| 1600 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1601 | |
| 1602 | /* |
| 1603 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1604 | * in this case we need to ensure that we reap all events. |
| 1605 | */ |
| 1606 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1607 | } |
| 1608 | mutex_unlock(&ctx->uring_lock); |
| 1609 | } |
| 1610 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1611 | static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1612 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1613 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1614 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1615 | |
| 1616 | do { |
| 1617 | int tmin = 0; |
| 1618 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1619 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1620 | * Don't enter poll loop if we already have events pending. |
| 1621 | * If we do, we can potentially be spinning for commands that |
| 1622 | * already triggered a CQE (eg in error). |
| 1623 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1624 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1625 | break; |
| 1626 | |
| 1627 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1628 | * If a submit got punted to a workqueue, we can have the |
| 1629 | * application entering polling for a command before it gets |
| 1630 | * issued. That app will hold the uring_lock for the duration |
| 1631 | * of the poll right here, so we need to take a breather every |
| 1632 | * now and then to ensure that the issue has a chance to add |
| 1633 | * the poll to the issued list. Otherwise we can spin here |
| 1634 | * forever, while the workqueue is stuck trying to acquire the |
| 1635 | * very same mutex. |
| 1636 | */ |
| 1637 | if (!(++iters & 7)) { |
| 1638 | mutex_unlock(&ctx->uring_lock); |
| 1639 | mutex_lock(&ctx->uring_lock); |
| 1640 | } |
| 1641 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1642 | if (*nr_events < min) |
| 1643 | tmin = min - *nr_events; |
| 1644 | |
| 1645 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1646 | if (ret <= 0) |
| 1647 | break; |
| 1648 | ret = 0; |
| 1649 | } while (min && !*nr_events && !need_resched()); |
| 1650 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1651 | return ret; |
| 1652 | } |
| 1653 | |
| 1654 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1655 | long min) |
| 1656 | { |
| 1657 | int ret; |
| 1658 | |
| 1659 | /* |
| 1660 | * We disallow the app entering submit/complete with polling, but we |
| 1661 | * still need to lock the ring to prevent racing with polled issue |
| 1662 | * that got punted to a workqueue. |
| 1663 | */ |
| 1664 | mutex_lock(&ctx->uring_lock); |
| 1665 | ret = __io_iopoll_check(ctx, nr_events, min); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1666 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1667 | return ret; |
| 1668 | } |
| 1669 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1670 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1671 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1672 | /* |
| 1673 | * Tell lockdep we inherited freeze protection from submission |
| 1674 | * thread. |
| 1675 | */ |
| 1676 | if (req->flags & REQ_F_ISREG) { |
| 1677 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1678 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1679 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1680 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1681 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1682 | } |
| 1683 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1684 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1685 | { |
| 1686 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1687 | req->flags |= REQ_F_FAIL_LINK; |
| 1688 | } |
| 1689 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1690 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1691 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1692 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1693 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1694 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1695 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1696 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1697 | if (res != req->result) |
| 1698 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1699 | io_cqring_add_event(req, res); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1703 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1704 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1705 | |
| 1706 | io_complete_rw_common(kiocb, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1707 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1708 | } |
| 1709 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1710 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
| 1711 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1712 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1713 | struct io_kiocb *nxt = NULL; |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1714 | |
| 1715 | io_complete_rw_common(kiocb, res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1716 | io_put_req_find_next(req, &nxt); |
| 1717 | |
| 1718 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1719 | } |
| 1720 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1721 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 1722 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1723 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1724 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1725 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1726 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1727 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1728 | if (res != req->result) |
| 1729 | req_set_fail_links(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1730 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1731 | if (res != -EAGAIN) |
| 1732 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 1733 | } |
| 1734 | |
| 1735 | /* |
| 1736 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 1737 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 1738 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 1739 | * accessing the kiocb cookie. |
| 1740 | */ |
| 1741 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 1742 | { |
| 1743 | struct io_ring_ctx *ctx = req->ctx; |
| 1744 | |
| 1745 | /* |
| 1746 | * Track whether we have multiple files in our lists. This will impact |
| 1747 | * how we do polling eventually, not spinning if we're on potentially |
| 1748 | * different devices. |
| 1749 | */ |
| 1750 | if (list_empty(&ctx->poll_list)) { |
| 1751 | ctx->poll_multi_file = false; |
| 1752 | } else if (!ctx->poll_multi_file) { |
| 1753 | struct io_kiocb *list_req; |
| 1754 | |
| 1755 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1756 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1757 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1758 | ctx->poll_multi_file = true; |
| 1759 | } |
| 1760 | |
| 1761 | /* |
| 1762 | * For fast devices, IO may have already completed. If it has, add |
| 1763 | * it to the front so we find it first. |
| 1764 | */ |
| 1765 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1766 | list_add(&req->list, &ctx->poll_list); |
| 1767 | else |
| 1768 | list_add_tail(&req->list, &ctx->poll_list); |
| 1769 | } |
| 1770 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1771 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1772 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1773 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1774 | int diff = state->has_refs - state->used_refs; |
| 1775 | |
| 1776 | if (diff) |
| 1777 | fput_many(state->file, diff); |
| 1778 | state->file = NULL; |
| 1779 | } |
| 1780 | } |
| 1781 | |
| 1782 | /* |
| 1783 | * Get as many references to a file as we have IOs left in this submission, |
| 1784 | * assuming most submissions are for one file, or at least that each file |
| 1785 | * has more than one submission. |
| 1786 | */ |
| 1787 | static struct file *io_file_get(struct io_submit_state *state, int fd) |
| 1788 | { |
| 1789 | if (!state) |
| 1790 | return fget(fd); |
| 1791 | |
| 1792 | if (state->file) { |
| 1793 | if (state->fd == fd) { |
| 1794 | state->used_refs++; |
| 1795 | state->ios_left--; |
| 1796 | return state->file; |
| 1797 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1798 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1799 | } |
| 1800 | state->file = fget_many(fd, state->ios_left); |
| 1801 | if (!state->file) |
| 1802 | return NULL; |
| 1803 | |
| 1804 | state->fd = fd; |
| 1805 | state->has_refs = state->ios_left; |
| 1806 | state->used_refs = 1; |
| 1807 | state->ios_left--; |
| 1808 | return state->file; |
| 1809 | } |
| 1810 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1811 | /* |
| 1812 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 1813 | * any file. For now, just ensure that anything potentially problematic is done |
| 1814 | * inline. |
| 1815 | */ |
| 1816 | static bool io_file_supports_async(struct file *file) |
| 1817 | { |
| 1818 | umode_t mode = file_inode(file)->i_mode; |
| 1819 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 1820 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1821 | return true; |
| 1822 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 1823 | return true; |
| 1824 | |
| 1825 | return false; |
| 1826 | } |
| 1827 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1828 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1829 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1830 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1831 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1832 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1833 | unsigned ioprio; |
| 1834 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1835 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1836 | if (!req->file) |
| 1837 | return -EBADF; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1838 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1839 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 1840 | req->flags |= REQ_F_ISREG; |
| 1841 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1842 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1843 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 1844 | req->flags |= REQ_F_CUR_POS; |
| 1845 | kiocb->ki_pos = req->file->f_pos; |
| 1846 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1847 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 1848 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
| 1849 | |
| 1850 | ioprio = READ_ONCE(sqe->ioprio); |
| 1851 | if (ioprio) { |
| 1852 | ret = ioprio_check_cap(ioprio); |
| 1853 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1854 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1855 | |
| 1856 | kiocb->ki_ioprio = ioprio; |
| 1857 | } else |
| 1858 | kiocb->ki_ioprio = get_current_ioprio(); |
| 1859 | |
| 1860 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 1861 | if (unlikely(ret)) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1862 | return ret; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1863 | |
| 1864 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1865 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 1866 | (req->file->f_flags & O_NONBLOCK)) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1867 | req->flags |= REQ_F_NOWAIT; |
| 1868 | |
| 1869 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1870 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1871 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1872 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1873 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 1874 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1875 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1876 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1877 | kiocb->ki_flags |= IOCB_HIPRI; |
| 1878 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 1879 | req->result = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1880 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1881 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1882 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1883 | kiocb->ki_complete = io_complete_rw; |
| 1884 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1885 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1886 | req->rw.addr = READ_ONCE(sqe->addr); |
| 1887 | req->rw.len = READ_ONCE(sqe->len); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1888 | /* we own ->private, reuse it for the buffer index */ |
| 1889 | req->rw.kiocb.private = (void *) (unsigned long) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1890 | READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1891 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 1895 | { |
| 1896 | switch (ret) { |
| 1897 | case -EIOCBQUEUED: |
| 1898 | break; |
| 1899 | case -ERESTARTSYS: |
| 1900 | case -ERESTARTNOINTR: |
| 1901 | case -ERESTARTNOHAND: |
| 1902 | case -ERESTART_RESTARTBLOCK: |
| 1903 | /* |
| 1904 | * We can't just restart the syscall, since previously |
| 1905 | * submitted sqes may already be in progress. Just fail this |
| 1906 | * IO with EINTR. |
| 1907 | */ |
| 1908 | ret = -EINTR; |
| 1909 | /* fall through */ |
| 1910 | default: |
| 1911 | kiocb->ki_complete(kiocb, ret, 0); |
| 1912 | } |
| 1913 | } |
| 1914 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1915 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt, |
| 1916 | bool in_async) |
| 1917 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1918 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 1919 | |
| 1920 | if (req->flags & REQ_F_CUR_POS) |
| 1921 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1922 | if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1923 | *nxt = __io_complete_rw(kiocb, ret); |
| 1924 | else |
| 1925 | io_rw_done(kiocb, ret); |
| 1926 | } |
| 1927 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1928 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 1929 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1930 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1931 | struct io_ring_ctx *ctx = req->ctx; |
| 1932 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1933 | struct io_mapped_ubuf *imu; |
| 1934 | unsigned index, buf_index; |
| 1935 | size_t offset; |
| 1936 | u64 buf_addr; |
| 1937 | |
| 1938 | /* attempt to use fixed buffers without having provided iovecs */ |
| 1939 | if (unlikely(!ctx->user_bufs)) |
| 1940 | return -EFAULT; |
| 1941 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1942 | buf_index = (unsigned long) req->rw.kiocb.private; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1943 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 1944 | return -EFAULT; |
| 1945 | |
| 1946 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 1947 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1948 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1949 | |
| 1950 | /* overflow */ |
| 1951 | if (buf_addr + len < buf_addr) |
| 1952 | return -EFAULT; |
| 1953 | /* not inside the mapped region */ |
| 1954 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 1955 | return -EFAULT; |
| 1956 | |
| 1957 | /* |
| 1958 | * May not be a start of buffer, set size appropriately |
| 1959 | * and advance us to the beginning. |
| 1960 | */ |
| 1961 | offset = buf_addr - imu->ubuf; |
| 1962 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1963 | |
| 1964 | if (offset) { |
| 1965 | /* |
| 1966 | * Don't use iov_iter_advance() here, as it's really slow for |
| 1967 | * using the latter parts of a big fixed buffer - it iterates |
| 1968 | * over each segment manually. We can cheat a bit here, because |
| 1969 | * we know that: |
| 1970 | * |
| 1971 | * 1) it's a BVEC iter, we set it up |
| 1972 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 1973 | * first and last bvec |
| 1974 | * |
| 1975 | * So just find our index, and adjust the iterator afterwards. |
| 1976 | * If the offset is within the first bvec (or the whole first |
| 1977 | * bvec, just use iov_iter_advance(). This makes it easier |
| 1978 | * since we can just skip the first segment, which may not |
| 1979 | * be PAGE_SIZE aligned. |
| 1980 | */ |
| 1981 | const struct bio_vec *bvec = imu->bvec; |
| 1982 | |
| 1983 | if (offset <= bvec->bv_len) { |
| 1984 | iov_iter_advance(iter, offset); |
| 1985 | } else { |
| 1986 | unsigned long seg_skip; |
| 1987 | |
| 1988 | /* skip first vec */ |
| 1989 | offset -= bvec->bv_len; |
| 1990 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 1991 | |
| 1992 | iter->bvec = bvec + seg_skip; |
| 1993 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 1994 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1995 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1996 | } |
| 1997 | } |
| 1998 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 1999 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2000 | } |
| 2001 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2002 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 2003 | struct iovec **iovec, struct iov_iter *iter) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2004 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2005 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 2006 | size_t sqe_len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2007 | u8 opcode; |
| 2008 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 2009 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 2010 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2011 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2012 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2013 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2014 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2015 | /* buffer index only valid with fixed read/write */ |
| 2016 | if (req->rw.kiocb.private) |
| 2017 | return -EINVAL; |
| 2018 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 2019 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
| 2020 | ssize_t ret; |
| 2021 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 2022 | *iovec = NULL; |
| 2023 | return ret; |
| 2024 | } |
| 2025 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2026 | if (req->io) { |
| 2027 | struct io_async_rw *iorw = &req->io->rw; |
| 2028 | |
| 2029 | *iovec = iorw->iov; |
| 2030 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 2031 | if (iorw->iov == iorw->fast_iov) |
| 2032 | *iovec = NULL; |
| 2033 | return iorw->size; |
| 2034 | } |
| 2035 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2036 | if (!req->has_user) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2037 | return -EFAULT; |
| 2038 | |
| 2039 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2040 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2041 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2042 | iovec, iter); |
| 2043 | #endif |
| 2044 | |
| 2045 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2046 | } |
| 2047 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2048 | /* |
| 2049 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2050 | * by looping over ->read() or ->write() manually. |
| 2051 | */ |
| 2052 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2053 | struct iov_iter *iter) |
| 2054 | { |
| 2055 | ssize_t ret = 0; |
| 2056 | |
| 2057 | /* |
| 2058 | * Don't support polled IO through this interface, and we can't |
| 2059 | * support non-blocking either. For the latter, this just causes |
| 2060 | * the kiocb to be handled from an async context. |
| 2061 | */ |
| 2062 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2063 | return -EOPNOTSUPP; |
| 2064 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2065 | return -EAGAIN; |
| 2066 | |
| 2067 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2068 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2069 | ssize_t nr; |
| 2070 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2071 | if (!iov_iter_is_bvec(iter)) { |
| 2072 | iovec = iov_iter_iovec(iter); |
| 2073 | } else { |
| 2074 | /* fixed buffers import bvec */ |
| 2075 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2076 | + iter->iov_offset; |
| 2077 | iovec.iov_len = min(iter->count, |
| 2078 | iter->bvec->bv_len - iter->iov_offset); |
| 2079 | } |
| 2080 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2081 | if (rw == READ) { |
| 2082 | nr = file->f_op->read(file, iovec.iov_base, |
| 2083 | iovec.iov_len, &kiocb->ki_pos); |
| 2084 | } else { |
| 2085 | nr = file->f_op->write(file, iovec.iov_base, |
| 2086 | iovec.iov_len, &kiocb->ki_pos); |
| 2087 | } |
| 2088 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2089 | if (iov_iter_is_bvec(iter)) |
| 2090 | kunmap(iter->bvec->bv_page); |
| 2091 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2092 | if (nr < 0) { |
| 2093 | if (!ret) |
| 2094 | ret = nr; |
| 2095 | break; |
| 2096 | } |
| 2097 | ret += nr; |
| 2098 | if (nr != iovec.iov_len) |
| 2099 | break; |
| 2100 | iov_iter_advance(iter, nr); |
| 2101 | } |
| 2102 | |
| 2103 | return ret; |
| 2104 | } |
| 2105 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2106 | 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] | 2107 | struct iovec *iovec, struct iovec *fast_iov, |
| 2108 | struct iov_iter *iter) |
| 2109 | { |
| 2110 | req->io->rw.nr_segs = iter->nr_segs; |
| 2111 | req->io->rw.size = io_size; |
| 2112 | req->io->rw.iov = iovec; |
| 2113 | if (!req->io->rw.iov) { |
| 2114 | req->io->rw.iov = req->io->rw.fast_iov; |
| 2115 | memcpy(req->io->rw.iov, fast_iov, |
| 2116 | sizeof(struct iovec) * iter->nr_segs); |
| 2117 | } |
| 2118 | } |
| 2119 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2120 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2121 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2122 | if (!io_op_defs[req->opcode].async_ctx) |
| 2123 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2124 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2125 | return req->io == NULL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | static void io_rw_async(struct io_wq_work **workptr) |
| 2129 | { |
| 2130 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2131 | struct iovec *iov = NULL; |
| 2132 | |
| 2133 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2134 | iov = req->io->rw.iov; |
| 2135 | io_wq_submit_work(workptr); |
| 2136 | kfree(iov); |
| 2137 | } |
| 2138 | |
| 2139 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2140 | struct iovec *iovec, struct iovec *fast_iov, |
| 2141 | struct iov_iter *iter) |
| 2142 | { |
Jens Axboe | 980ad26 | 2020-01-24 23:08:54 -0700 | [diff] [blame] | 2143 | if (!io_op_defs[req->opcode].async_ctx) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2144 | return 0; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2145 | if (!req->io && io_alloc_async_ctx(req)) |
| 2146 | return -ENOMEM; |
| 2147 | |
| 2148 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2149 | req->work.func = io_rw_async; |
| 2150 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2151 | } |
| 2152 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2153 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2154 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2155 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2156 | struct io_async_ctx *io; |
| 2157 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2158 | ssize_t ret; |
| 2159 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2160 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2161 | if (ret) |
| 2162 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2163 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2164 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2165 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2166 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2167 | if (!req->io) |
| 2168 | return 0; |
| 2169 | |
| 2170 | io = req->io; |
| 2171 | io->rw.iov = io->rw.fast_iov; |
| 2172 | req->io = NULL; |
| 2173 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter); |
| 2174 | req->io = io; |
| 2175 | if (ret < 0) |
| 2176 | return ret; |
| 2177 | |
| 2178 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2179 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2180 | } |
| 2181 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2182 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2183 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2184 | { |
| 2185 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2186 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2187 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2188 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2189 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2190 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2191 | ret = io_import_iovec(READ, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2192 | if (ret < 0) |
| 2193 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2194 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2195 | /* Ensure we clear previously set non-block flag */ |
| 2196 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2197 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2198 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2199 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2200 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2201 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2202 | req->result = io_size; |
| 2203 | |
| 2204 | /* |
| 2205 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2206 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2207 | */ |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2208 | if (force_nonblock && !io_file_supports_async(req->file)) { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2209 | req->flags |= REQ_F_MUST_PUNT; |
| 2210 | goto copy_iov; |
| 2211 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2212 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2213 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2214 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2215 | if (!ret) { |
| 2216 | ssize_t ret2; |
| 2217 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2218 | if (req->file->f_op->read_iter) |
| 2219 | ret2 = call_read_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2220 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2221 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2222 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 2223 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2224 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2225 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2226 | } else { |
| 2227 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2228 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2229 | inline_vecs, &iter); |
| 2230 | if (ret) |
| 2231 | goto out_free; |
| 2232 | return -EAGAIN; |
| 2233 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2234 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2235 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2236 | if (!io_wq_current_is_worker()) |
| 2237 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2238 | return ret; |
| 2239 | } |
| 2240 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2241 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2242 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2243 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2244 | struct io_async_ctx *io; |
| 2245 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2246 | ssize_t ret; |
| 2247 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2248 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2249 | if (ret) |
| 2250 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2251 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2252 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 2253 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2254 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2255 | if (!req->io) |
| 2256 | return 0; |
| 2257 | |
| 2258 | io = req->io; |
| 2259 | io->rw.iov = io->rw.fast_iov; |
| 2260 | req->io = NULL; |
| 2261 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter); |
| 2262 | req->io = io; |
| 2263 | if (ret < 0) |
| 2264 | return ret; |
| 2265 | |
| 2266 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2267 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2268 | } |
| 2269 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2270 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2271 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2272 | { |
| 2273 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2274 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2275 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2276 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2277 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2278 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2279 | ret = io_import_iovec(WRITE, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2280 | if (ret < 0) |
| 2281 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2282 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2283 | /* Ensure we clear previously set non-block flag */ |
| 2284 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2285 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2286 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2287 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2288 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2289 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2290 | req->result = io_size; |
| 2291 | |
| 2292 | /* |
| 2293 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2294 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2295 | */ |
| 2296 | if (force_nonblock && !io_file_supports_async(req->file)) { |
| 2297 | req->flags |= REQ_F_MUST_PUNT; |
| 2298 | goto copy_iov; |
| 2299 | } |
| 2300 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2301 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 2302 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 2303 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2304 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2305 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2306 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2307 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2308 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2309 | ssize_t ret2; |
| 2310 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2311 | /* |
| 2312 | * Open-code file_start_write here to grab freeze protection, |
| 2313 | * which will be released by another thread in |
| 2314 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 2315 | * released so that it doesn't complain about the held lock when |
| 2316 | * we return to userspace. |
| 2317 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2318 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2319 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2320 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2321 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2322 | SB_FREEZE_WRITE); |
| 2323 | } |
| 2324 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2325 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2326 | if (req->file->f_op->write_iter) |
| 2327 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2328 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2329 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2330 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2331 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2332 | } else { |
| 2333 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2334 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2335 | inline_vecs, &iter); |
| 2336 | if (ret) |
| 2337 | goto out_free; |
| 2338 | return -EAGAIN; |
| 2339 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2340 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2341 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2342 | if (!io_wq_current_is_worker()) |
| 2343 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2344 | return ret; |
| 2345 | } |
| 2346 | |
| 2347 | /* |
| 2348 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 2349 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2350 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2351 | { |
| 2352 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2353 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2354 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2355 | return -EINVAL; |
| 2356 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2357 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2358 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2359 | return 0; |
| 2360 | } |
| 2361 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2362 | 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] | 2363 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2364 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2365 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2366 | if (!req->file) |
| 2367 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2368 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2369 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2370 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2371 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2372 | return -EINVAL; |
| 2373 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2374 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 2375 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 2376 | return -EINVAL; |
| 2377 | |
| 2378 | req->sync.off = READ_ONCE(sqe->off); |
| 2379 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2380 | return 0; |
| 2381 | } |
| 2382 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2383 | static bool io_req_cancelled(struct io_kiocb *req) |
| 2384 | { |
| 2385 | if (req->work.flags & IO_WQ_WORK_CANCEL) { |
| 2386 | req_set_fail_links(req); |
| 2387 | io_cqring_add_event(req, -ECANCELED); |
| 2388 | io_put_req(req); |
| 2389 | return true; |
| 2390 | } |
| 2391 | |
| 2392 | return false; |
| 2393 | } |
| 2394 | |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2395 | static void io_link_work_cb(struct io_wq_work **workptr) |
| 2396 | { |
| 2397 | struct io_wq_work *work = *workptr; |
| 2398 | struct io_kiocb *link = work->data; |
| 2399 | |
| 2400 | io_queue_linked_timeout(link); |
| 2401 | work->func = io_wq_submit_work; |
| 2402 | } |
| 2403 | |
| 2404 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) |
| 2405 | { |
| 2406 | struct io_kiocb *link; |
| 2407 | |
| 2408 | io_prep_async_work(nxt, &link); |
| 2409 | *workptr = &nxt->work; |
| 2410 | if (link) { |
| 2411 | nxt->work.flags |= IO_WQ_WORK_CB; |
| 2412 | nxt->work.func = io_link_work_cb; |
| 2413 | nxt->work.data = link; |
| 2414 | } |
| 2415 | } |
| 2416 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2417 | static void io_fsync_finish(struct io_wq_work **workptr) |
| 2418 | { |
| 2419 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2420 | loff_t end = req->sync.off + req->sync.len; |
| 2421 | struct io_kiocb *nxt = NULL; |
| 2422 | int ret; |
| 2423 | |
| 2424 | if (io_req_cancelled(req)) |
| 2425 | return; |
| 2426 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2427 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2428 | end > 0 ? end : LLONG_MAX, |
| 2429 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 2430 | if (ret < 0) |
| 2431 | req_set_fail_links(req); |
| 2432 | io_cqring_add_event(req, ret); |
| 2433 | io_put_req_find_next(req, &nxt); |
| 2434 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2435 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2436 | } |
| 2437 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2438 | static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2439 | bool force_nonblock) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2440 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2441 | struct io_wq_work *work, *old_work; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2442 | |
| 2443 | /* fsync always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2444 | if (force_nonblock) { |
| 2445 | io_put_req(req); |
| 2446 | req->work.func = io_fsync_finish; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2447 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2448 | } |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2449 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2450 | work = old_work = &req->work; |
| 2451 | io_fsync_finish(&work); |
| 2452 | if (work && work != old_work) |
| 2453 | *nxt = container_of(work, struct io_kiocb, work); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2454 | return 0; |
| 2455 | } |
| 2456 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2457 | static void io_fallocate_finish(struct io_wq_work **workptr) |
| 2458 | { |
| 2459 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2460 | struct io_kiocb *nxt = NULL; |
| 2461 | int ret; |
| 2462 | |
| 2463 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 2464 | req->sync.len); |
| 2465 | if (ret < 0) |
| 2466 | req_set_fail_links(req); |
| 2467 | io_cqring_add_event(req, ret); |
| 2468 | io_put_req_find_next(req, &nxt); |
| 2469 | if (nxt) |
| 2470 | io_wq_assign_next(workptr, nxt); |
| 2471 | } |
| 2472 | |
| 2473 | static int io_fallocate_prep(struct io_kiocb *req, |
| 2474 | const struct io_uring_sqe *sqe) |
| 2475 | { |
| 2476 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 2477 | return -EINVAL; |
| 2478 | |
| 2479 | req->sync.off = READ_ONCE(sqe->off); |
| 2480 | req->sync.len = READ_ONCE(sqe->addr); |
| 2481 | req->sync.mode = READ_ONCE(sqe->len); |
| 2482 | return 0; |
| 2483 | } |
| 2484 | |
| 2485 | static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2486 | bool force_nonblock) |
| 2487 | { |
| 2488 | struct io_wq_work *work, *old_work; |
| 2489 | |
| 2490 | /* fallocate always requiring blocking context */ |
| 2491 | if (force_nonblock) { |
| 2492 | io_put_req(req); |
| 2493 | req->work.func = io_fallocate_finish; |
| 2494 | return -EAGAIN; |
| 2495 | } |
| 2496 | |
| 2497 | work = old_work = &req->work; |
| 2498 | io_fallocate_finish(&work); |
| 2499 | if (work && work != old_work) |
| 2500 | *nxt = container_of(work, struct io_kiocb, work); |
| 2501 | |
| 2502 | return 0; |
| 2503 | } |
| 2504 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2505 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2506 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2507 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2508 | int ret; |
| 2509 | |
| 2510 | if (sqe->ioprio || sqe->buf_index) |
| 2511 | return -EINVAL; |
| 2512 | |
| 2513 | req->open.dfd = READ_ONCE(sqe->fd); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2514 | req->open.how.mode = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2515 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2516 | req->open.how.flags = READ_ONCE(sqe->open_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2517 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2518 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2519 | if (IS_ERR(req->open.filename)) { |
| 2520 | ret = PTR_ERR(req->open.filename); |
| 2521 | req->open.filename = NULL; |
| 2522 | return ret; |
| 2523 | } |
| 2524 | |
| 2525 | return 0; |
| 2526 | } |
| 2527 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2528 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2529 | { |
| 2530 | struct open_how __user *how; |
| 2531 | const char __user *fname; |
| 2532 | size_t len; |
| 2533 | int ret; |
| 2534 | |
| 2535 | if (sqe->ioprio || sqe->buf_index) |
| 2536 | return -EINVAL; |
| 2537 | |
| 2538 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2539 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2540 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 2541 | len = READ_ONCE(sqe->len); |
| 2542 | |
| 2543 | if (len < OPEN_HOW_SIZE_VER0) |
| 2544 | return -EINVAL; |
| 2545 | |
| 2546 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 2547 | len); |
| 2548 | if (ret) |
| 2549 | return ret; |
| 2550 | |
| 2551 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
| 2552 | req->open.how.flags |= O_LARGEFILE; |
| 2553 | |
| 2554 | req->open.filename = getname(fname); |
| 2555 | if (IS_ERR(req->open.filename)) { |
| 2556 | ret = PTR_ERR(req->open.filename); |
| 2557 | req->open.filename = NULL; |
| 2558 | return ret; |
| 2559 | } |
| 2560 | |
| 2561 | return 0; |
| 2562 | } |
| 2563 | |
| 2564 | static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2565 | bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2566 | { |
| 2567 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2568 | struct file *file; |
| 2569 | int ret; |
| 2570 | |
| 2571 | if (force_nonblock) { |
| 2572 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 2573 | return -EAGAIN; |
| 2574 | } |
| 2575 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2576 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2577 | if (ret) |
| 2578 | goto err; |
| 2579 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2580 | ret = get_unused_fd_flags(req->open.how.flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2581 | if (ret < 0) |
| 2582 | goto err; |
| 2583 | |
| 2584 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 2585 | if (IS_ERR(file)) { |
| 2586 | put_unused_fd(ret); |
| 2587 | ret = PTR_ERR(file); |
| 2588 | } else { |
| 2589 | fsnotify_open(file); |
| 2590 | fd_install(ret, file); |
| 2591 | } |
| 2592 | err: |
| 2593 | putname(req->open.filename); |
| 2594 | if (ret < 0) |
| 2595 | req_set_fail_links(req); |
| 2596 | io_cqring_add_event(req, ret); |
| 2597 | io_put_req_find_next(req, nxt); |
| 2598 | return 0; |
| 2599 | } |
| 2600 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2601 | static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2602 | bool force_nonblock) |
| 2603 | { |
| 2604 | req->open.how = build_open_how(req->open.how.flags, req->open.how.mode); |
| 2605 | return io_openat2(req, nxt, force_nonblock); |
| 2606 | } |
| 2607 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 2608 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2609 | { |
| 2610 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2611 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 2612 | return -EINVAL; |
| 2613 | |
| 2614 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 2615 | req->madvise.len = READ_ONCE(sqe->len); |
| 2616 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2617 | return 0; |
| 2618 | #else |
| 2619 | return -EOPNOTSUPP; |
| 2620 | #endif |
| 2621 | } |
| 2622 | |
| 2623 | static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2624 | bool force_nonblock) |
| 2625 | { |
| 2626 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2627 | struct io_madvise *ma = &req->madvise; |
| 2628 | int ret; |
| 2629 | |
| 2630 | if (force_nonblock) |
| 2631 | return -EAGAIN; |
| 2632 | |
| 2633 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 2634 | if (ret < 0) |
| 2635 | req_set_fail_links(req); |
| 2636 | io_cqring_add_event(req, ret); |
| 2637 | io_put_req_find_next(req, nxt); |
| 2638 | return 0; |
| 2639 | #else |
| 2640 | return -EOPNOTSUPP; |
| 2641 | #endif |
| 2642 | } |
| 2643 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 2644 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2645 | { |
| 2646 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 2647 | return -EINVAL; |
| 2648 | |
| 2649 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 2650 | req->fadvise.len = READ_ONCE(sqe->len); |
| 2651 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2652 | return 0; |
| 2653 | } |
| 2654 | |
| 2655 | static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2656 | bool force_nonblock) |
| 2657 | { |
| 2658 | struct io_fadvise *fa = &req->fadvise; |
| 2659 | int ret; |
| 2660 | |
| 2661 | /* DONTNEED may block, others _should_ not */ |
| 2662 | if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock) |
| 2663 | return -EAGAIN; |
| 2664 | |
| 2665 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 2666 | if (ret < 0) |
| 2667 | req_set_fail_links(req); |
| 2668 | io_cqring_add_event(req, ret); |
| 2669 | io_put_req_find_next(req, nxt); |
| 2670 | return 0; |
| 2671 | } |
| 2672 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2673 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2674 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2675 | const char __user *fname; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2676 | unsigned lookup_flags; |
| 2677 | int ret; |
| 2678 | |
| 2679 | if (sqe->ioprio || sqe->buf_index) |
| 2680 | return -EINVAL; |
| 2681 | |
| 2682 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2683 | req->open.mask = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2684 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2685 | req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2686 | req->open.how.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2687 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2688 | if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2689 | return -EINVAL; |
| 2690 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2691 | req->open.filename = getname_flags(fname, lookup_flags, NULL); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2692 | if (IS_ERR(req->open.filename)) { |
| 2693 | ret = PTR_ERR(req->open.filename); |
| 2694 | req->open.filename = NULL; |
| 2695 | return ret; |
| 2696 | } |
| 2697 | |
| 2698 | return 0; |
| 2699 | } |
| 2700 | |
| 2701 | static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2702 | bool force_nonblock) |
| 2703 | { |
| 2704 | struct io_open *ctx = &req->open; |
| 2705 | unsigned lookup_flags; |
| 2706 | struct path path; |
| 2707 | struct kstat stat; |
| 2708 | int ret; |
| 2709 | |
| 2710 | if (force_nonblock) |
| 2711 | return -EAGAIN; |
| 2712 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2713 | if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2714 | return -EINVAL; |
| 2715 | |
| 2716 | retry: |
| 2717 | /* filename_lookup() drops it, keep a reference */ |
| 2718 | ctx->filename->refcnt++; |
| 2719 | |
| 2720 | ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path, |
| 2721 | NULL); |
| 2722 | if (ret) |
| 2723 | goto err; |
| 2724 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2725 | ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2726 | path_put(&path); |
| 2727 | if (retry_estale(ret, lookup_flags)) { |
| 2728 | lookup_flags |= LOOKUP_REVAL; |
| 2729 | goto retry; |
| 2730 | } |
| 2731 | if (!ret) |
| 2732 | ret = cp_statx(&stat, ctx->buffer); |
| 2733 | err: |
| 2734 | putname(ctx->filename); |
| 2735 | if (ret < 0) |
| 2736 | req_set_fail_links(req); |
| 2737 | io_cqring_add_event(req, ret); |
| 2738 | io_put_req_find_next(req, nxt); |
| 2739 | return 0; |
| 2740 | } |
| 2741 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2742 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2743 | { |
| 2744 | /* |
| 2745 | * If we queue this for async, it must not be cancellable. That would |
| 2746 | * leave the 'file' in an undeterminate state. |
| 2747 | */ |
| 2748 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 2749 | |
| 2750 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 2751 | sqe->rw_flags || sqe->buf_index) |
| 2752 | return -EINVAL; |
| 2753 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2754 | return -EINVAL; |
| 2755 | |
| 2756 | req->close.fd = READ_ONCE(sqe->fd); |
| 2757 | if (req->file->f_op == &io_uring_fops || |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 2758 | req->close.fd == req->ctx->ring_fd) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2759 | return -EBADF; |
| 2760 | |
| 2761 | return 0; |
| 2762 | } |
| 2763 | |
| 2764 | static void io_close_finish(struct io_wq_work **workptr) |
| 2765 | { |
| 2766 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2767 | struct io_kiocb *nxt = NULL; |
| 2768 | |
| 2769 | /* Invoked with files, we need to do the close */ |
| 2770 | if (req->work.files) { |
| 2771 | int ret; |
| 2772 | |
| 2773 | ret = filp_close(req->close.put_file, req->work.files); |
| 2774 | if (ret < 0) { |
| 2775 | req_set_fail_links(req); |
| 2776 | } |
| 2777 | io_cqring_add_event(req, ret); |
| 2778 | } |
| 2779 | |
| 2780 | fput(req->close.put_file); |
| 2781 | |
| 2782 | /* we bypassed the re-issue, drop the submission reference */ |
| 2783 | io_put_req(req); |
| 2784 | io_put_req_find_next(req, &nxt); |
| 2785 | if (nxt) |
| 2786 | io_wq_assign_next(workptr, nxt); |
| 2787 | } |
| 2788 | |
| 2789 | static int io_close(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2790 | bool force_nonblock) |
| 2791 | { |
| 2792 | int ret; |
| 2793 | |
| 2794 | req->close.put_file = NULL; |
| 2795 | ret = __close_fd_get_file(req->close.fd, &req->close.put_file); |
| 2796 | if (ret < 0) |
| 2797 | return ret; |
| 2798 | |
| 2799 | /* if the file has a flush method, be safe and punt to async */ |
| 2800 | if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) { |
| 2801 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 2802 | goto eagain; |
| 2803 | } |
| 2804 | |
| 2805 | /* |
| 2806 | * No ->flush(), safely close from here and just punt the |
| 2807 | * fput() to async context. |
| 2808 | */ |
| 2809 | ret = filp_close(req->close.put_file, current->files); |
| 2810 | |
| 2811 | if (ret < 0) |
| 2812 | req_set_fail_links(req); |
| 2813 | io_cqring_add_event(req, ret); |
| 2814 | |
| 2815 | if (io_wq_current_is_worker()) { |
| 2816 | struct io_wq_work *old_work, *work; |
| 2817 | |
| 2818 | old_work = work = &req->work; |
| 2819 | io_close_finish(&work); |
| 2820 | if (work && work != old_work) |
| 2821 | *nxt = container_of(work, struct io_kiocb, work); |
| 2822 | return 0; |
| 2823 | } |
| 2824 | |
| 2825 | eagain: |
| 2826 | req->work.func = io_close_finish; |
| 2827 | return -EAGAIN; |
| 2828 | } |
| 2829 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2830 | 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] | 2831 | { |
| 2832 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2833 | |
| 2834 | if (!req->file) |
| 2835 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2836 | |
| 2837 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2838 | return -EINVAL; |
| 2839 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 2840 | return -EINVAL; |
| 2841 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2842 | req->sync.off = READ_ONCE(sqe->off); |
| 2843 | req->sync.len = READ_ONCE(sqe->len); |
| 2844 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2845 | return 0; |
| 2846 | } |
| 2847 | |
| 2848 | static void io_sync_file_range_finish(struct io_wq_work **workptr) |
| 2849 | { |
| 2850 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2851 | struct io_kiocb *nxt = NULL; |
| 2852 | int ret; |
| 2853 | |
| 2854 | if (io_req_cancelled(req)) |
| 2855 | return; |
| 2856 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2857 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2858 | req->sync.flags); |
| 2859 | if (ret < 0) |
| 2860 | req_set_fail_links(req); |
| 2861 | io_cqring_add_event(req, ret); |
| 2862 | io_put_req_find_next(req, &nxt); |
| 2863 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2864 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2865 | } |
| 2866 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2867 | 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] | 2868 | bool force_nonblock) |
| 2869 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2870 | struct io_wq_work *work, *old_work; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2871 | |
| 2872 | /* sync_file_range always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2873 | if (force_nonblock) { |
| 2874 | io_put_req(req); |
| 2875 | req->work.func = io_sync_file_range_finish; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2876 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2877 | } |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2878 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2879 | work = old_work = &req->work; |
| 2880 | io_sync_file_range_finish(&work); |
| 2881 | if (work && work != old_work) |
| 2882 | *nxt = container_of(work, struct io_kiocb, work); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2883 | return 0; |
| 2884 | } |
| 2885 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2886 | #if defined(CONFIG_NET) |
| 2887 | static void io_sendrecv_async(struct io_wq_work **workptr) |
| 2888 | { |
| 2889 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2890 | struct iovec *iov = NULL; |
| 2891 | |
| 2892 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2893 | iov = req->io->msg.iov; |
| 2894 | io_wq_submit_work(workptr); |
| 2895 | kfree(iov); |
| 2896 | } |
| 2897 | #endif |
| 2898 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2899 | 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] | 2900 | { |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2901 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2902 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2903 | struct io_async_ctx *io = req->io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2904 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2905 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 2906 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2907 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2908 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2909 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2910 | return 0; |
| 2911 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 2912 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2913 | return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2914 | &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2915 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2916 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2917 | #endif |
| 2918 | } |
| 2919 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2920 | static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2921 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2922 | { |
| 2923 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2924 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2925 | struct socket *sock; |
| 2926 | int ret; |
| 2927 | |
| 2928 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2929 | return -EINVAL; |
| 2930 | |
| 2931 | sock = sock_from_file(req->file, &ret); |
| 2932 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2933 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2934 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2935 | unsigned flags; |
| 2936 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2937 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2938 | kmsg = &req->io->msg; |
| 2939 | kmsg->msg.msg_name = &addr; |
| 2940 | /* if iov is set, it's allocated already */ |
| 2941 | if (!kmsg->iov) |
| 2942 | kmsg->iov = kmsg->fast_iov; |
| 2943 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2944 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2945 | struct io_sr_msg *sr = &req->sr_msg; |
| 2946 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2947 | kmsg = &io.msg; |
| 2948 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2949 | |
| 2950 | io.msg.iov = io.msg.fast_iov; |
| 2951 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 2952 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2953 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2954 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2955 | } |
| 2956 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2957 | flags = req->sr_msg.msg_flags; |
| 2958 | if (flags & MSG_DONTWAIT) |
| 2959 | req->flags |= REQ_F_NOWAIT; |
| 2960 | else if (force_nonblock) |
| 2961 | flags |= MSG_DONTWAIT; |
| 2962 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2963 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2964 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2965 | if (req->io) |
| 2966 | return -EAGAIN; |
| 2967 | if (io_alloc_async_ctx(req)) |
| 2968 | return -ENOMEM; |
| 2969 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 2970 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2971 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2972 | } |
| 2973 | if (ret == -ERESTARTSYS) |
| 2974 | ret = -EINTR; |
| 2975 | } |
| 2976 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2977 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2978 | kfree(kmsg->iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2979 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2980 | if (ret < 0) |
| 2981 | req_set_fail_links(req); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2982 | io_put_req_find_next(req, nxt); |
| 2983 | return 0; |
| 2984 | #else |
| 2985 | return -EOPNOTSUPP; |
| 2986 | #endif |
| 2987 | } |
| 2988 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2989 | static int io_send(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2990 | bool force_nonblock) |
| 2991 | { |
| 2992 | #if defined(CONFIG_NET) |
| 2993 | struct socket *sock; |
| 2994 | int ret; |
| 2995 | |
| 2996 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2997 | return -EINVAL; |
| 2998 | |
| 2999 | sock = sock_from_file(req->file, &ret); |
| 3000 | if (sock) { |
| 3001 | struct io_sr_msg *sr = &req->sr_msg; |
| 3002 | struct msghdr msg; |
| 3003 | struct iovec iov; |
| 3004 | unsigned flags; |
| 3005 | |
| 3006 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, |
| 3007 | &msg.msg_iter); |
| 3008 | if (ret) |
| 3009 | return ret; |
| 3010 | |
| 3011 | msg.msg_name = NULL; |
| 3012 | msg.msg_control = NULL; |
| 3013 | msg.msg_controllen = 0; |
| 3014 | msg.msg_namelen = 0; |
| 3015 | |
| 3016 | flags = req->sr_msg.msg_flags; |
| 3017 | if (flags & MSG_DONTWAIT) |
| 3018 | req->flags |= REQ_F_NOWAIT; |
| 3019 | else if (force_nonblock) |
| 3020 | flags |= MSG_DONTWAIT; |
| 3021 | |
| 3022 | ret = __sys_sendmsg_sock(sock, &msg, flags); |
| 3023 | if (force_nonblock && ret == -EAGAIN) |
| 3024 | return -EAGAIN; |
| 3025 | if (ret == -ERESTARTSYS) |
| 3026 | ret = -EINTR; |
| 3027 | } |
| 3028 | |
| 3029 | io_cqring_add_event(req, ret); |
| 3030 | if (ret < 0) |
| 3031 | req_set_fail_links(req); |
| 3032 | io_put_req_find_next(req, nxt); |
| 3033 | return 0; |
| 3034 | #else |
| 3035 | return -EOPNOTSUPP; |
| 3036 | #endif |
| 3037 | } |
| 3038 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3039 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 3040 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3041 | { |
| 3042 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3043 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3044 | struct io_async_ctx *io = req->io; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3045 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3046 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3047 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3048 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3049 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3050 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3051 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3052 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3053 | return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3054 | &io->msg.uaddr, &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3055 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3056 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3057 | #endif |
| 3058 | } |
| 3059 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3060 | static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3061 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3062 | { |
| 3063 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3064 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3065 | struct socket *sock; |
| 3066 | int ret; |
| 3067 | |
| 3068 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3069 | return -EINVAL; |
| 3070 | |
| 3071 | sock = sock_from_file(req->file, &ret); |
| 3072 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3073 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3074 | struct sockaddr_storage addr; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3075 | unsigned flags; |
| 3076 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3077 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3078 | kmsg = &req->io->msg; |
| 3079 | kmsg->msg.msg_name = &addr; |
| 3080 | /* if iov is set, it's allocated already */ |
| 3081 | if (!kmsg->iov) |
| 3082 | kmsg->iov = kmsg->fast_iov; |
| 3083 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3084 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3085 | struct io_sr_msg *sr = &req->sr_msg; |
| 3086 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3087 | kmsg = &io.msg; |
| 3088 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3089 | |
| 3090 | io.msg.iov = io.msg.fast_iov; |
| 3091 | ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3092 | sr->msg_flags, &io.msg.uaddr, |
| 3093 | &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3094 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3095 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3096 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3097 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3098 | flags = req->sr_msg.msg_flags; |
| 3099 | if (flags & MSG_DONTWAIT) |
| 3100 | req->flags |= REQ_F_NOWAIT; |
| 3101 | else if (force_nonblock) |
| 3102 | flags |= MSG_DONTWAIT; |
| 3103 | |
| 3104 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 3105 | kmsg->uaddr, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3106 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3107 | if (req->io) |
| 3108 | return -EAGAIN; |
| 3109 | if (io_alloc_async_ctx(req)) |
| 3110 | return -ENOMEM; |
| 3111 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 3112 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3113 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3114 | } |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 3115 | if (ret == -ERESTARTSYS) |
| 3116 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3117 | } |
| 3118 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3119 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3120 | kfree(kmsg->iov); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3121 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3122 | if (ret < 0) |
| 3123 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3124 | io_put_req_find_next(req, nxt); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3125 | return 0; |
| 3126 | #else |
| 3127 | return -EOPNOTSUPP; |
| 3128 | #endif |
| 3129 | } |
| 3130 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3131 | static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3132 | bool force_nonblock) |
| 3133 | { |
| 3134 | #if defined(CONFIG_NET) |
| 3135 | struct socket *sock; |
| 3136 | int ret; |
| 3137 | |
| 3138 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3139 | return -EINVAL; |
| 3140 | |
| 3141 | sock = sock_from_file(req->file, &ret); |
| 3142 | if (sock) { |
| 3143 | struct io_sr_msg *sr = &req->sr_msg; |
| 3144 | struct msghdr msg; |
| 3145 | struct iovec iov; |
| 3146 | unsigned flags; |
| 3147 | |
| 3148 | ret = import_single_range(READ, sr->buf, sr->len, &iov, |
| 3149 | &msg.msg_iter); |
| 3150 | if (ret) |
| 3151 | return ret; |
| 3152 | |
| 3153 | msg.msg_name = NULL; |
| 3154 | msg.msg_control = NULL; |
| 3155 | msg.msg_controllen = 0; |
| 3156 | msg.msg_namelen = 0; |
| 3157 | msg.msg_iocb = NULL; |
| 3158 | msg.msg_flags = 0; |
| 3159 | |
| 3160 | flags = req->sr_msg.msg_flags; |
| 3161 | if (flags & MSG_DONTWAIT) |
| 3162 | req->flags |= REQ_F_NOWAIT; |
| 3163 | else if (force_nonblock) |
| 3164 | flags |= MSG_DONTWAIT; |
| 3165 | |
| 3166 | ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags); |
| 3167 | if (force_nonblock && ret == -EAGAIN) |
| 3168 | return -EAGAIN; |
| 3169 | if (ret == -ERESTARTSYS) |
| 3170 | ret = -EINTR; |
| 3171 | } |
| 3172 | |
| 3173 | io_cqring_add_event(req, ret); |
| 3174 | if (ret < 0) |
| 3175 | req_set_fail_links(req); |
| 3176 | io_put_req_find_next(req, nxt); |
| 3177 | return 0; |
| 3178 | #else |
| 3179 | return -EOPNOTSUPP; |
| 3180 | #endif |
| 3181 | } |
| 3182 | |
| 3183 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3184 | 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] | 3185 | { |
| 3186 | #if defined(CONFIG_NET) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3187 | struct io_accept *accept = &req->accept; |
| 3188 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3189 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3190 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 3191 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3192 | return -EINVAL; |
| 3193 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 3194 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3195 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3196 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3197 | return 0; |
| 3198 | #else |
| 3199 | return -EOPNOTSUPP; |
| 3200 | #endif |
| 3201 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3202 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3203 | #if defined(CONFIG_NET) |
| 3204 | static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3205 | bool force_nonblock) |
| 3206 | { |
| 3207 | struct io_accept *accept = &req->accept; |
| 3208 | unsigned file_flags; |
| 3209 | int ret; |
| 3210 | |
| 3211 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3212 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
| 3213 | accept->addr_len, accept->flags); |
| 3214 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3215 | return -EAGAIN; |
Jens Axboe | 8e3cca1 | 2019-11-09 19:52:33 -0700 | [diff] [blame] | 3216 | if (ret == -ERESTARTSYS) |
| 3217 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3218 | if (ret < 0) |
| 3219 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3220 | io_cqring_add_event(req, ret); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3221 | io_put_req_find_next(req, nxt); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3222 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3223 | } |
| 3224 | |
| 3225 | static void io_accept_finish(struct io_wq_work **workptr) |
| 3226 | { |
| 3227 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3228 | struct io_kiocb *nxt = NULL; |
| 3229 | |
| 3230 | if (io_req_cancelled(req)) |
| 3231 | return; |
| 3232 | __io_accept(req, &nxt, false); |
| 3233 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3234 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3235 | } |
| 3236 | #endif |
| 3237 | |
| 3238 | static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3239 | bool force_nonblock) |
| 3240 | { |
| 3241 | #if defined(CONFIG_NET) |
| 3242 | int ret; |
| 3243 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3244 | ret = __io_accept(req, nxt, force_nonblock); |
| 3245 | if (ret == -EAGAIN && force_nonblock) { |
| 3246 | req->work.func = io_accept_finish; |
| 3247 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 3248 | io_put_req(req); |
| 3249 | return -EAGAIN; |
| 3250 | } |
| 3251 | return 0; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3252 | #else |
| 3253 | return -EOPNOTSUPP; |
| 3254 | #endif |
| 3255 | } |
| 3256 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3257 | 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] | 3258 | { |
| 3259 | #if defined(CONFIG_NET) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3260 | struct io_connect *conn = &req->connect; |
| 3261 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3262 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3263 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3264 | return -EINVAL; |
| 3265 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 3266 | return -EINVAL; |
| 3267 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3268 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3269 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 3270 | |
| 3271 | if (!io) |
| 3272 | return 0; |
| 3273 | |
| 3274 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3275 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3276 | #else |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3277 | return -EOPNOTSUPP; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3278 | #endif |
| 3279 | } |
| 3280 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3281 | static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3282 | bool force_nonblock) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3283 | { |
| 3284 | #if defined(CONFIG_NET) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3285 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3286 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3287 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3288 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3289 | if (req->io) { |
| 3290 | io = req->io; |
| 3291 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3292 | ret = move_addr_to_kernel(req->connect.addr, |
| 3293 | req->connect.addr_len, |
| 3294 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3295 | if (ret) |
| 3296 | goto out; |
| 3297 | io = &__io; |
| 3298 | } |
| 3299 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3300 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3301 | |
| 3302 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 3303 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 3304 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3305 | if (req->io) |
| 3306 | return -EAGAIN; |
| 3307 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3308 | ret = -ENOMEM; |
| 3309 | goto out; |
| 3310 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3311 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3312 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3313 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3314 | if (ret == -ERESTARTSYS) |
| 3315 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3316 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3317 | if (ret < 0) |
| 3318 | req_set_fail_links(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3319 | io_cqring_add_event(req, ret); |
| 3320 | io_put_req_find_next(req, nxt); |
| 3321 | return 0; |
| 3322 | #else |
| 3323 | return -EOPNOTSUPP; |
| 3324 | #endif |
| 3325 | } |
| 3326 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3327 | static void io_poll_remove_one(struct io_kiocb *req) |
| 3328 | { |
| 3329 | struct io_poll_iocb *poll = &req->poll; |
| 3330 | |
| 3331 | spin_lock(&poll->head->lock); |
| 3332 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3333 | if (!list_empty(&poll->wait.entry)) { |
| 3334 | list_del_init(&poll->wait.entry); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3335 | io_queue_async_work(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3336 | } |
| 3337 | spin_unlock(&poll->head->lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3338 | hash_del(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3339 | } |
| 3340 | |
| 3341 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 3342 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3343 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3344 | struct io_kiocb *req; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3345 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3346 | |
| 3347 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3348 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 3349 | struct hlist_head *list; |
| 3350 | |
| 3351 | list = &ctx->cancel_hash[i]; |
| 3352 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
| 3353 | io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3354 | } |
| 3355 | spin_unlock_irq(&ctx->completion_lock); |
| 3356 | } |
| 3357 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3358 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 3359 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3360 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3361 | struct io_kiocb *req; |
| 3362 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3363 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 3364 | hlist_for_each_entry(req, list, hash_node) { |
| 3365 | if (sqe_addr == req->user_data) { |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3366 | io_poll_remove_one(req); |
| 3367 | return 0; |
| 3368 | } |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3369 | } |
| 3370 | |
| 3371 | return -ENOENT; |
| 3372 | } |
| 3373 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3374 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 3375 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3376 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3377 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3378 | return -EINVAL; |
| 3379 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3380 | sqe->poll_events) |
| 3381 | return -EINVAL; |
| 3382 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3383 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3384 | return 0; |
| 3385 | } |
| 3386 | |
| 3387 | /* |
| 3388 | * Find a running poll command that matches one specified in sqe->addr, |
| 3389 | * and remove it if found. |
| 3390 | */ |
| 3391 | static int io_poll_remove(struct io_kiocb *req) |
| 3392 | { |
| 3393 | struct io_ring_ctx *ctx = req->ctx; |
| 3394 | u64 addr; |
| 3395 | int ret; |
| 3396 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3397 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3398 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3399 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3400 | spin_unlock_irq(&ctx->completion_lock); |
| 3401 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3402 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3403 | if (ret < 0) |
| 3404 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3405 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3406 | return 0; |
| 3407 | } |
| 3408 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3409 | 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] | 3410 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3411 | struct io_ring_ctx *ctx = req->ctx; |
| 3412 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3413 | req->poll.done = true; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3414 | if (error) |
| 3415 | io_cqring_fill_event(req, error); |
| 3416 | else |
| 3417 | io_cqring_fill_event(req, mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3418 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3419 | } |
| 3420 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3421 | static void io_poll_complete_work(struct io_wq_work **workptr) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3422 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3423 | struct io_wq_work *work = *workptr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3424 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 3425 | struct io_poll_iocb *poll = &req->poll; |
| 3426 | struct poll_table_struct pt = { ._key = poll->events }; |
| 3427 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3428 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3429 | __poll_t mask = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3430 | int ret = 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3431 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3432 | if (work->flags & IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3433 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3434 | ret = -ECANCELED; |
| 3435 | } else if (READ_ONCE(poll->canceled)) { |
| 3436 | ret = -ECANCELED; |
| 3437 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3438 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3439 | if (ret != -ECANCELED) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3440 | mask = vfs_poll(poll->file, &pt) & poll->events; |
| 3441 | |
| 3442 | /* |
| 3443 | * Note that ->ki_cancel callers also delete iocb from active_reqs after |
| 3444 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to |
| 3445 | * synchronize with them. In the cancellation case the list_del_init |
| 3446 | * itself is not actually needed, but harmless so we keep it in to |
| 3447 | * avoid further branches in the fast path. |
| 3448 | */ |
| 3449 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3450 | if (!mask && ret != -ECANCELED) { |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3451 | add_wait_queue(poll->head, &poll->wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3452 | spin_unlock_irq(&ctx->completion_lock); |
| 3453 | return; |
| 3454 | } |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3455 | hash_del(&req->hash_node); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3456 | io_poll_complete(req, mask, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3457 | spin_unlock_irq(&ctx->completion_lock); |
| 3458 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3459 | io_cqring_ev_posted(ctx); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3460 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3461 | if (ret < 0) |
| 3462 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3463 | io_put_req_find_next(req, &nxt); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3464 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3465 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3466 | } |
| 3467 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3468 | static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes) |
| 3469 | { |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3470 | struct io_kiocb *req, *tmp; |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3471 | struct req_batch rb; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3472 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 3473 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3474 | spin_lock_irq(&ctx->completion_lock); |
| 3475 | llist_for_each_entry_safe(req, tmp, nodes, llist_node) { |
| 3476 | hash_del(&req->hash_node); |
| 3477 | io_poll_complete(req, req->result, 0); |
| 3478 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3479 | if (refcount_dec_and_test(&req->refs) && |
| 3480 | !io_req_multi_free(&rb, req)) { |
| 3481 | req->flags |= REQ_F_COMP_LOCKED; |
| 3482 | io_free_req(req); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3483 | } |
| 3484 | } |
| 3485 | spin_unlock_irq(&ctx->completion_lock); |
| 3486 | |
| 3487 | io_cqring_ev_posted(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3488 | io_free_req_many(ctx, &rb); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3489 | } |
| 3490 | |
| 3491 | static void io_poll_flush(struct io_wq_work **workptr) |
| 3492 | { |
| 3493 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3494 | struct llist_node *nodes; |
| 3495 | |
| 3496 | nodes = llist_del_all(&req->ctx->poll_llist); |
| 3497 | if (nodes) |
| 3498 | __io_poll_flush(req->ctx, nodes); |
| 3499 | } |
| 3500 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3501 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 3502 | void *key) |
| 3503 | { |
Jens Axboe | e944475 | 2019-11-26 15:02:04 -0700 | [diff] [blame] | 3504 | struct io_poll_iocb *poll = wait->private; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3505 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); |
| 3506 | struct io_ring_ctx *ctx = req->ctx; |
| 3507 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3508 | |
| 3509 | /* for instances that support it check for an event match first: */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3510 | if (mask && !(mask & poll->events)) |
| 3511 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3512 | |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3513 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3514 | |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 3515 | /* |
| 3516 | * Run completion inline if we can. We're using trylock here because |
| 3517 | * we are violating the completion_lock -> poll wq lock ordering. |
| 3518 | * If we have a link timeout we're going to need the completion_lock |
| 3519 | * for finalizing the request, mark us as having grabbed that already. |
| 3520 | */ |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3521 | if (mask) { |
| 3522 | unsigned long flags; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3523 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3524 | if (llist_empty(&ctx->poll_llist) && |
| 3525 | spin_trylock_irqsave(&ctx->completion_lock, flags)) { |
| 3526 | hash_del(&req->hash_node); |
| 3527 | io_poll_complete(req, mask, 0); |
| 3528 | req->flags |= REQ_F_COMP_LOCKED; |
| 3529 | io_put_req(req); |
| 3530 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3531 | |
| 3532 | io_cqring_ev_posted(ctx); |
| 3533 | req = NULL; |
| 3534 | } else { |
| 3535 | req->result = mask; |
| 3536 | req->llist_node.next = NULL; |
| 3537 | /* if the list wasn't empty, we're done */ |
| 3538 | if (!llist_add(&req->llist_node, &ctx->poll_llist)) |
| 3539 | req = NULL; |
| 3540 | else |
| 3541 | req->work.func = io_poll_flush; |
| 3542 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3543 | } |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3544 | if (req) |
| 3545 | io_queue_async_work(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3546 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3547 | return 1; |
| 3548 | } |
| 3549 | |
| 3550 | struct io_poll_table { |
| 3551 | struct poll_table_struct pt; |
| 3552 | struct io_kiocb *req; |
| 3553 | int error; |
| 3554 | }; |
| 3555 | |
| 3556 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 3557 | struct poll_table_struct *p) |
| 3558 | { |
| 3559 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 3560 | |
| 3561 | if (unlikely(pt->req->poll.head)) { |
| 3562 | pt->error = -EINVAL; |
| 3563 | return; |
| 3564 | } |
| 3565 | |
| 3566 | pt->error = 0; |
| 3567 | pt->req->poll.head = head; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3568 | add_wait_queue(head, &pt->req->poll.wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3569 | } |
| 3570 | |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3571 | static void io_poll_req_insert(struct io_kiocb *req) |
| 3572 | { |
| 3573 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3574 | struct hlist_head *list; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3575 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3576 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 3577 | hlist_add_head(&req->hash_node, list); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3578 | } |
| 3579 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3580 | 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] | 3581 | { |
| 3582 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3583 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3584 | |
| 3585 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3586 | return -EINVAL; |
| 3587 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 3588 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3589 | if (!poll->file) |
| 3590 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3591 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3592 | events = READ_ONCE(sqe->poll_events); |
| 3593 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3594 | return 0; |
| 3595 | } |
| 3596 | |
| 3597 | static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3598 | { |
| 3599 | struct io_poll_iocb *poll = &req->poll; |
| 3600 | struct io_ring_ctx *ctx = req->ctx; |
| 3601 | struct io_poll_table ipt; |
| 3602 | bool cancel = false; |
| 3603 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3604 | |
| 3605 | INIT_IO_WORK(&req->work, io_poll_complete_work); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3606 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3607 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3608 | poll->head = NULL; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3609 | poll->done = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3610 | poll->canceled = false; |
| 3611 | |
| 3612 | ipt.pt._qproc = io_poll_queue_proc; |
| 3613 | ipt.pt._key = poll->events; |
| 3614 | ipt.req = req; |
| 3615 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ |
| 3616 | |
| 3617 | /* initialized the list so that we can do list_empty checks */ |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3618 | INIT_LIST_HEAD(&poll->wait.entry); |
| 3619 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); |
| 3620 | poll->wait.private = poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3621 | |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 3622 | INIT_LIST_HEAD(&req->list); |
| 3623 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3624 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3625 | |
| 3626 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3627 | if (likely(poll->head)) { |
| 3628 | spin_lock(&poll->head->lock); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3629 | if (unlikely(list_empty(&poll->wait.entry))) { |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3630 | if (ipt.error) |
| 3631 | cancel = true; |
| 3632 | ipt.error = 0; |
| 3633 | mask = 0; |
| 3634 | } |
| 3635 | if (mask || ipt.error) |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3636 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3637 | else if (cancel) |
| 3638 | WRITE_ONCE(poll->canceled, true); |
| 3639 | else if (!poll->done) /* actually waiting for an event */ |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3640 | io_poll_req_insert(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3641 | spin_unlock(&poll->head->lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3642 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3643 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3644 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3645 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3646 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3647 | spin_unlock_irq(&ctx->completion_lock); |
| 3648 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3649 | if (mask) { |
| 3650 | io_cqring_ev_posted(ctx); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3651 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3652 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3653 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3654 | } |
| 3655 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3656 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 3657 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3658 | struct io_timeout_data *data = container_of(timer, |
| 3659 | struct io_timeout_data, timer); |
| 3660 | struct io_kiocb *req = data->req; |
| 3661 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3662 | unsigned long flags; |
| 3663 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3664 | atomic_inc(&ctx->cq_timeouts); |
| 3665 | |
| 3666 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3667 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3668 | * We could be racing with timeout deletion. If the list is empty, |
| 3669 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3670 | */ |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3671 | if (!list_empty(&req->list)) { |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3672 | struct io_kiocb *prev; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3673 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3674 | /* |
| 3675 | * Adjust the reqs sequence before the current one because it |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 3676 | * will consume a slot in the cq_ring and the cq_tail |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3677 | * pointer will be increased, otherwise other timeout reqs may |
| 3678 | * return in advance without waiting for enough wait_nr. |
| 3679 | */ |
| 3680 | prev = req; |
| 3681 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 3682 | prev->sequence++; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3683 | list_del_init(&req->list); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3684 | } |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3685 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3686 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3687 | io_commit_cqring(ctx); |
| 3688 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3689 | |
| 3690 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3691 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3692 | io_put_req(req); |
| 3693 | return HRTIMER_NORESTART; |
| 3694 | } |
| 3695 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3696 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 3697 | { |
| 3698 | struct io_kiocb *req; |
| 3699 | int ret = -ENOENT; |
| 3700 | |
| 3701 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 3702 | if (user_data == req->user_data) { |
| 3703 | list_del_init(&req->list); |
| 3704 | ret = 0; |
| 3705 | break; |
| 3706 | } |
| 3707 | } |
| 3708 | |
| 3709 | if (ret == -ENOENT) |
| 3710 | return ret; |
| 3711 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3712 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3713 | if (ret == -1) |
| 3714 | return -EALREADY; |
| 3715 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3716 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3717 | io_cqring_fill_event(req, -ECANCELED); |
| 3718 | io_put_req(req); |
| 3719 | return 0; |
| 3720 | } |
| 3721 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3722 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 3723 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3724 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3725 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3726 | return -EINVAL; |
| 3727 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 3728 | return -EINVAL; |
| 3729 | |
| 3730 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 3731 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 3732 | if (req->timeout.flags) |
| 3733 | return -EINVAL; |
| 3734 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3735 | return 0; |
| 3736 | } |
| 3737 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3738 | /* |
| 3739 | * Remove or update an existing timeout command |
| 3740 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3741 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3742 | { |
| 3743 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3744 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3745 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3746 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3747 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3748 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3749 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3750 | io_commit_cqring(ctx); |
| 3751 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3752 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3753 | if (ret < 0) |
| 3754 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3755 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3756 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3757 | } |
| 3758 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3759 | 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] | 3760 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3761 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3762 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3763 | unsigned flags; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3764 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3765 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3766 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3767 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3768 | return -EINVAL; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3769 | if (sqe->off && is_timeout_link) |
| 3770 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3771 | flags = READ_ONCE(sqe->timeout_flags); |
| 3772 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3773 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 3774 | |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3775 | req->timeout.count = READ_ONCE(sqe->off); |
| 3776 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3777 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3778 | return -ENOMEM; |
| 3779 | |
| 3780 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3781 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3782 | req->flags |= REQ_F_TIMEOUT; |
| 3783 | |
| 3784 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3785 | return -EFAULT; |
| 3786 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3787 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3788 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3789 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3790 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3791 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3792 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 3793 | return 0; |
| 3794 | } |
| 3795 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3796 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3797 | { |
| 3798 | unsigned count; |
| 3799 | struct io_ring_ctx *ctx = req->ctx; |
| 3800 | struct io_timeout_data *data; |
| 3801 | struct list_head *entry; |
| 3802 | unsigned span = 0; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3803 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3804 | data = &req->io->timeout; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3805 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3806 | /* |
| 3807 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3808 | * timeout event to be satisfied. If it isn't set, then this is |
| 3809 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3810 | */ |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3811 | count = req->timeout.count; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3812 | if (!count) { |
| 3813 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 3814 | spin_lock_irq(&ctx->completion_lock); |
| 3815 | entry = ctx->timeout_list.prev; |
| 3816 | goto add; |
| 3817 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3818 | |
| 3819 | req->sequence = ctx->cached_sq_head + count - 1; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3820 | data->seq_offset = count; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3821 | |
| 3822 | /* |
| 3823 | * Insertion sort, ensuring the first entry in the list is always |
| 3824 | * the one we need first. |
| 3825 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3826 | spin_lock_irq(&ctx->completion_lock); |
| 3827 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 3828 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3829 | unsigned nxt_sq_head; |
| 3830 | long long tmp, tmp_nxt; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3831 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3832 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3833 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 3834 | continue; |
| 3835 | |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3836 | /* |
| 3837 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 3838 | * long to store it. |
| 3839 | */ |
| 3840 | tmp = (long long)ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 3841 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
| 3842 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3843 | |
| 3844 | /* |
| 3845 | * cached_sq_head may overflow, and it will never overflow twice |
| 3846 | * once there is some timeout req still be valid. |
| 3847 | */ |
| 3848 | if (ctx->cached_sq_head < nxt_sq_head) |
yangerkun | 8b07a65 | 2019-10-17 12:12:35 +0800 | [diff] [blame] | 3849 | tmp += UINT_MAX; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3850 | |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3851 | if (tmp > tmp_nxt) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3852 | break; |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3853 | |
| 3854 | /* |
| 3855 | * Sequence of reqs after the insert one and itself should |
| 3856 | * be adjusted because each timeout req consumes a slot. |
| 3857 | */ |
| 3858 | span++; |
| 3859 | nxt->sequence++; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3860 | } |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3861 | req->sequence -= span; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3862 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3863 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3864 | data->timer.function = io_timeout_fn; |
| 3865 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3866 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3867 | return 0; |
| 3868 | } |
| 3869 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3870 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3871 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3872 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3873 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3874 | return req->user_data == (unsigned long) data; |
| 3875 | } |
| 3876 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3877 | 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] | 3878 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3879 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3880 | int ret = 0; |
| 3881 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3882 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
| 3883 | switch (cancel_ret) { |
| 3884 | case IO_WQ_CANCEL_OK: |
| 3885 | ret = 0; |
| 3886 | break; |
| 3887 | case IO_WQ_CANCEL_RUNNING: |
| 3888 | ret = -EALREADY; |
| 3889 | break; |
| 3890 | case IO_WQ_CANCEL_NOTFOUND: |
| 3891 | ret = -ENOENT; |
| 3892 | break; |
| 3893 | } |
| 3894 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3895 | return ret; |
| 3896 | } |
| 3897 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3898 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 3899 | struct io_kiocb *req, __u64 sqe_addr, |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3900 | struct io_kiocb **nxt, int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3901 | { |
| 3902 | unsigned long flags; |
| 3903 | int ret; |
| 3904 | |
| 3905 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 3906 | if (ret != -ENOENT) { |
| 3907 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3908 | goto done; |
| 3909 | } |
| 3910 | |
| 3911 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3912 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 3913 | if (ret != -ENOENT) |
| 3914 | goto done; |
| 3915 | ret = io_poll_cancel(ctx, sqe_addr); |
| 3916 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3917 | if (!ret) |
| 3918 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3919 | io_cqring_fill_event(req, ret); |
| 3920 | io_commit_cqring(ctx); |
| 3921 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3922 | io_cqring_ev_posted(ctx); |
| 3923 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3924 | if (ret < 0) |
| 3925 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3926 | io_put_req_find_next(req, nxt); |
| 3927 | } |
| 3928 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3929 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 3930 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3931 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3932 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3933 | return -EINVAL; |
| 3934 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 3935 | sqe->cancel_flags) |
| 3936 | return -EINVAL; |
| 3937 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3938 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 3939 | return 0; |
| 3940 | } |
| 3941 | |
| 3942 | static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3943 | { |
| 3944 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3945 | |
| 3946 | io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3947 | return 0; |
| 3948 | } |
| 3949 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 3950 | static int io_files_update_prep(struct io_kiocb *req, |
| 3951 | const struct io_uring_sqe *sqe) |
| 3952 | { |
| 3953 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) |
| 3954 | return -EINVAL; |
| 3955 | |
| 3956 | req->files_update.offset = READ_ONCE(sqe->off); |
| 3957 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 3958 | if (!req->files_update.nr_args) |
| 3959 | return -EINVAL; |
| 3960 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 3961 | return 0; |
| 3962 | } |
| 3963 | |
| 3964 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) |
| 3965 | { |
| 3966 | struct io_ring_ctx *ctx = req->ctx; |
| 3967 | struct io_uring_files_update up; |
| 3968 | int ret; |
| 3969 | |
| 3970 | if (force_nonblock) { |
| 3971 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 3972 | return -EAGAIN; |
| 3973 | } |
| 3974 | |
| 3975 | up.offset = req->files_update.offset; |
| 3976 | up.fds = req->files_update.arg; |
| 3977 | |
| 3978 | mutex_lock(&ctx->uring_lock); |
| 3979 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 3980 | mutex_unlock(&ctx->uring_lock); |
| 3981 | |
| 3982 | if (ret < 0) |
| 3983 | req_set_fail_links(req); |
| 3984 | io_cqring_add_event(req, ret); |
| 3985 | io_put_req(req); |
| 3986 | return 0; |
| 3987 | } |
| 3988 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3989 | static int io_req_defer_prep(struct io_kiocb *req, |
| 3990 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3991 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3992 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3993 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 3994 | io_req_work_grab_env(req, &io_op_defs[req->opcode]); |
| 3995 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3996 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3997 | case IORING_OP_NOP: |
| 3998 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3999 | case IORING_OP_READV: |
| 4000 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4001 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4002 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4003 | break; |
| 4004 | case IORING_OP_WRITEV: |
| 4005 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4006 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4007 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4008 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4009 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4010 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4011 | break; |
| 4012 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4013 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 4014 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4015 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4016 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4017 | break; |
| 4018 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4019 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4020 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4021 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4022 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4023 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4024 | break; |
| 4025 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4026 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4027 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4028 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4029 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4030 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4031 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4032 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4033 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4034 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4035 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4036 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4037 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4038 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4039 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4040 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4041 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4042 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4043 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4044 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4045 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4046 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4047 | case IORING_OP_FALLOCATE: |
| 4048 | ret = io_fallocate_prep(req, sqe); |
| 4049 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4050 | case IORING_OP_OPENAT: |
| 4051 | ret = io_openat_prep(req, sqe); |
| 4052 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4053 | case IORING_OP_CLOSE: |
| 4054 | ret = io_close_prep(req, sqe); |
| 4055 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4056 | case IORING_OP_FILES_UPDATE: |
| 4057 | ret = io_files_update_prep(req, sqe); |
| 4058 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4059 | case IORING_OP_STATX: |
| 4060 | ret = io_statx_prep(req, sqe); |
| 4061 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4062 | case IORING_OP_FADVISE: |
| 4063 | ret = io_fadvise_prep(req, sqe); |
| 4064 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4065 | case IORING_OP_MADVISE: |
| 4066 | ret = io_madvise_prep(req, sqe); |
| 4067 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4068 | case IORING_OP_OPENAT2: |
| 4069 | ret = io_openat2_prep(req, sqe); |
| 4070 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4071 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4072 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 4073 | req->opcode); |
| 4074 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4075 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4076 | } |
| 4077 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4078 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4079 | } |
| 4080 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4081 | 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] | 4082 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4083 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4084 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4085 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4086 | /* Still need defer if there is pending req in defer list. */ |
| 4087 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4088 | return 0; |
| 4089 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4090 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4091 | return -EAGAIN; |
| 4092 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4093 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4094 | if (ret < 0) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4095 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4096 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4097 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4098 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4099 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4100 | return 0; |
| 4101 | } |
| 4102 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 4103 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4104 | list_add_tail(&req->list, &ctx->defer_list); |
| 4105 | spin_unlock_irq(&ctx->completion_lock); |
| 4106 | return -EIOCBQUEUED; |
| 4107 | } |
| 4108 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4109 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4110 | struct io_kiocb **nxt, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4111 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4112 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4113 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4114 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4115 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4116 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4117 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4118 | break; |
| 4119 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4120 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4121 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4122 | if (sqe) { |
| 4123 | ret = io_read_prep(req, sqe, force_nonblock); |
| 4124 | if (ret < 0) |
| 4125 | break; |
| 4126 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4127 | ret = io_read(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4128 | break; |
| 4129 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4130 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4131 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4132 | if (sqe) { |
| 4133 | ret = io_write_prep(req, sqe, force_nonblock); |
| 4134 | if (ret < 0) |
| 4135 | break; |
| 4136 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4137 | ret = io_write(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4138 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4139 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4140 | if (sqe) { |
| 4141 | ret = io_prep_fsync(req, sqe); |
| 4142 | if (ret < 0) |
| 4143 | break; |
| 4144 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4145 | ret = io_fsync(req, nxt, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4146 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4147 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4148 | if (sqe) { |
| 4149 | ret = io_poll_add_prep(req, sqe); |
| 4150 | if (ret) |
| 4151 | break; |
| 4152 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4153 | ret = io_poll_add(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4154 | break; |
| 4155 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4156 | if (sqe) { |
| 4157 | ret = io_poll_remove_prep(req, sqe); |
| 4158 | if (ret < 0) |
| 4159 | break; |
| 4160 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4161 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4162 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4163 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4164 | if (sqe) { |
| 4165 | ret = io_prep_sfr(req, sqe); |
| 4166 | if (ret < 0) |
| 4167 | break; |
| 4168 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4169 | ret = io_sync_file_range(req, nxt, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4170 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4171 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4172 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4173 | if (sqe) { |
| 4174 | ret = io_sendmsg_prep(req, sqe); |
| 4175 | if (ret < 0) |
| 4176 | break; |
| 4177 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4178 | if (req->opcode == IORING_OP_SENDMSG) |
| 4179 | ret = io_sendmsg(req, nxt, force_nonblock); |
| 4180 | else |
| 4181 | ret = io_send(req, nxt, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4182 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4183 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4184 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4185 | if (sqe) { |
| 4186 | ret = io_recvmsg_prep(req, sqe); |
| 4187 | if (ret) |
| 4188 | break; |
| 4189 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4190 | if (req->opcode == IORING_OP_RECVMSG) |
| 4191 | ret = io_recvmsg(req, nxt, force_nonblock); |
| 4192 | else |
| 4193 | ret = io_recv(req, nxt, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4194 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4195 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4196 | if (sqe) { |
| 4197 | ret = io_timeout_prep(req, sqe, false); |
| 4198 | if (ret) |
| 4199 | break; |
| 4200 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4201 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4202 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4203 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4204 | if (sqe) { |
| 4205 | ret = io_timeout_remove_prep(req, sqe); |
| 4206 | if (ret) |
| 4207 | break; |
| 4208 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4209 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4210 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4211 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4212 | if (sqe) { |
| 4213 | ret = io_accept_prep(req, sqe); |
| 4214 | if (ret) |
| 4215 | break; |
| 4216 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4217 | ret = io_accept(req, nxt, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4218 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4219 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4220 | if (sqe) { |
| 4221 | ret = io_connect_prep(req, sqe); |
| 4222 | if (ret) |
| 4223 | break; |
| 4224 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4225 | ret = io_connect(req, nxt, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4226 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4227 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4228 | if (sqe) { |
| 4229 | ret = io_async_cancel_prep(req, sqe); |
| 4230 | if (ret) |
| 4231 | break; |
| 4232 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4233 | ret = io_async_cancel(req, nxt); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4234 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4235 | case IORING_OP_FALLOCATE: |
| 4236 | if (sqe) { |
| 4237 | ret = io_fallocate_prep(req, sqe); |
| 4238 | if (ret) |
| 4239 | break; |
| 4240 | } |
| 4241 | ret = io_fallocate(req, nxt, force_nonblock); |
| 4242 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4243 | case IORING_OP_OPENAT: |
| 4244 | if (sqe) { |
| 4245 | ret = io_openat_prep(req, sqe); |
| 4246 | if (ret) |
| 4247 | break; |
| 4248 | } |
| 4249 | ret = io_openat(req, nxt, force_nonblock); |
| 4250 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4251 | case IORING_OP_CLOSE: |
| 4252 | if (sqe) { |
| 4253 | ret = io_close_prep(req, sqe); |
| 4254 | if (ret) |
| 4255 | break; |
| 4256 | } |
| 4257 | ret = io_close(req, nxt, force_nonblock); |
| 4258 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4259 | case IORING_OP_FILES_UPDATE: |
| 4260 | if (sqe) { |
| 4261 | ret = io_files_update_prep(req, sqe); |
| 4262 | if (ret) |
| 4263 | break; |
| 4264 | } |
| 4265 | ret = io_files_update(req, force_nonblock); |
| 4266 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4267 | case IORING_OP_STATX: |
| 4268 | if (sqe) { |
| 4269 | ret = io_statx_prep(req, sqe); |
| 4270 | if (ret) |
| 4271 | break; |
| 4272 | } |
| 4273 | ret = io_statx(req, nxt, force_nonblock); |
| 4274 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4275 | case IORING_OP_FADVISE: |
| 4276 | if (sqe) { |
| 4277 | ret = io_fadvise_prep(req, sqe); |
| 4278 | if (ret) |
| 4279 | break; |
| 4280 | } |
| 4281 | ret = io_fadvise(req, nxt, force_nonblock); |
| 4282 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4283 | case IORING_OP_MADVISE: |
| 4284 | if (sqe) { |
| 4285 | ret = io_madvise_prep(req, sqe); |
| 4286 | if (ret) |
| 4287 | break; |
| 4288 | } |
| 4289 | ret = io_madvise(req, nxt, force_nonblock); |
| 4290 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4291 | case IORING_OP_OPENAT2: |
| 4292 | if (sqe) { |
| 4293 | ret = io_openat2_prep(req, sqe); |
| 4294 | if (ret) |
| 4295 | break; |
| 4296 | } |
| 4297 | ret = io_openat2(req, nxt, force_nonblock); |
| 4298 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4299 | default: |
| 4300 | ret = -EINVAL; |
| 4301 | break; |
| 4302 | } |
| 4303 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4304 | if (ret) |
| 4305 | return ret; |
| 4306 | |
| 4307 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4308 | const bool in_async = io_wq_current_is_worker(); |
| 4309 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4310 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4311 | return -EAGAIN; |
| 4312 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4313 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 4314 | if (in_async) |
| 4315 | mutex_lock(&ctx->uring_lock); |
| 4316 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4317 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4318 | |
| 4319 | if (in_async) |
| 4320 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4324 | } |
| 4325 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4326 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4327 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4328 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4329 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4330 | struct io_kiocb *nxt = NULL; |
| 4331 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4332 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4333 | /* if NO_CANCEL is set, we must still run the work */ |
| 4334 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 4335 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4336 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4337 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4338 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4339 | if (!ret) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4340 | req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0; |
| 4341 | req->in_async = true; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4342 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4343 | ret = io_issue_sqe(req, NULL, &nxt, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4344 | /* |
| 4345 | * We can get EAGAIN for polled IO even though we're |
| 4346 | * forcing a sync submission from here, since we can't |
| 4347 | * wait for request slots on the block side. |
| 4348 | */ |
| 4349 | if (ret != -EAGAIN) |
| 4350 | break; |
| 4351 | cond_resched(); |
| 4352 | } while (1); |
| 4353 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4354 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4355 | /* drop submission reference */ |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 4356 | io_put_req(req); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4357 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4358 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4359 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4360 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4361 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4362 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4363 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4364 | /* if a dependent link is ready, pass it back */ |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4365 | if (!ret && nxt) |
| 4366 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4367 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4368 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4369 | static int io_req_needs_file(struct io_kiocb *req, int fd) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4370 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4371 | if (!io_op_defs[req->opcode].needs_file) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4372 | return 0; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4373 | if (fd == -1 && io_op_defs[req->opcode].fd_non_neg) |
| 4374 | return 0; |
| 4375 | return 1; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4376 | } |
| 4377 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4378 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 4379 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4380 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4381 | struct fixed_file_table *table; |
| 4382 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4383 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
| 4384 | return table->files[index & IORING_FILE_TABLE_MASK];; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4385 | } |
| 4386 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4387 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
| 4388 | const struct io_uring_sqe *sqe) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4389 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4390 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4391 | unsigned flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4392 | int fd; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4393 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4394 | flags = READ_ONCE(sqe->flags); |
| 4395 | fd = READ_ONCE(sqe->fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4396 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4397 | if (!io_req_needs_file(req, fd)) |
| 4398 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4399 | |
| 4400 | if (flags & IOSQE_FIXED_FILE) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4401 | if (unlikely(!ctx->file_data || |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4402 | (unsigned) fd >= ctx->nr_user_files)) |
| 4403 | return -EBADF; |
Jens Axboe | b762012 | 2019-10-26 07:22:55 -0600 | [diff] [blame] | 4404 | fd = array_index_nospec(fd, ctx->nr_user_files); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4405 | req->file = io_file_from_index(ctx, fd); |
| 4406 | if (!req->file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4407 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4408 | req->flags |= REQ_F_FIXED_FILE; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4409 | percpu_ref_get(&ctx->file_data->refs); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4410 | } else { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4411 | if (req->needs_fixed_file) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4412 | return -EBADF; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 4413 | trace_io_uring_file_get(ctx, fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4414 | req->file = io_file_get(state, fd); |
| 4415 | if (unlikely(!req->file)) |
| 4416 | return -EBADF; |
| 4417 | } |
| 4418 | |
| 4419 | return 0; |
| 4420 | } |
| 4421 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4422 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4423 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4424 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4425 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4426 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4427 | if (!ctx->ring_file) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4428 | return -EBADF; |
| 4429 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4430 | rcu_read_lock(); |
| 4431 | spin_lock_irq(&ctx->inflight_lock); |
| 4432 | /* |
| 4433 | * We use the f_ops->flush() handler to ensure that we can flush |
| 4434 | * out work accessing these files if the fd is closed. Check if |
| 4435 | * the fd has changed since we started down this path, and disallow |
| 4436 | * this operation if it has. |
| 4437 | */ |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4438 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4439 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 4440 | req->flags |= REQ_F_INFLIGHT; |
| 4441 | req->work.files = current->files; |
| 4442 | ret = 0; |
| 4443 | } |
| 4444 | spin_unlock_irq(&ctx->inflight_lock); |
| 4445 | rcu_read_unlock(); |
| 4446 | |
| 4447 | return ret; |
| 4448 | } |
| 4449 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4450 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 4451 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4452 | struct io_timeout_data *data = container_of(timer, |
| 4453 | struct io_timeout_data, timer); |
| 4454 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4455 | struct io_ring_ctx *ctx = req->ctx; |
| 4456 | struct io_kiocb *prev = NULL; |
| 4457 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4458 | |
| 4459 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4460 | |
| 4461 | /* |
| 4462 | * We don't expect the list to be empty, that will only happen if we |
| 4463 | * race with the completion of the linked work. |
| 4464 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4465 | if (!list_empty(&req->link_list)) { |
| 4466 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 4467 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4468 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4469 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4470 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 4471 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4472 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4473 | } |
| 4474 | |
| 4475 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4476 | |
| 4477 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4478 | req_set_fail_links(prev); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4479 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
| 4480 | -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4481 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4482 | } else { |
| 4483 | io_cqring_add_event(req, -ETIME); |
| 4484 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4485 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4486 | return HRTIMER_NORESTART; |
| 4487 | } |
| 4488 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4489 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4490 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4491 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4492 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4493 | /* |
| 4494 | * If the list is now empty, then our linked request finished before |
| 4495 | * we got a chance to setup the timer |
| 4496 | */ |
| 4497 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4498 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4499 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4500 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4501 | data->timer.function = io_link_timeout_fn; |
| 4502 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 4503 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4504 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4505 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4506 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4507 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4508 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4509 | } |
| 4510 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4511 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4512 | { |
| 4513 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4514 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4515 | if (!(req->flags & REQ_F_LINK)) |
| 4516 | return NULL; |
| 4517 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4518 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 4519 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4520 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4521 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4522 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4523 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4524 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4525 | } |
| 4526 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4527 | 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] | 4528 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4529 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4530 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4531 | int ret; |
| 4532 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4533 | again: |
| 4534 | linked_timeout = io_prep_linked_timeout(req); |
| 4535 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4536 | ret = io_issue_sqe(req, sqe, &nxt, true); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 4537 | |
| 4538 | /* |
| 4539 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 4540 | * doesn't support non-blocking read/write attempts |
| 4541 | */ |
| 4542 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 4543 | (req->flags & REQ_F_MUST_PUNT))) { |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 4544 | punt: |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4545 | if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) { |
| 4546 | ret = io_grab_files(req); |
| 4547 | if (ret) |
| 4548 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4549 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4550 | |
| 4551 | /* |
| 4552 | * Queued up for async execution, worker will release |
| 4553 | * submit reference when the iocb is actually submitted. |
| 4554 | */ |
| 4555 | io_queue_async_work(req); |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4556 | goto done_req; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4557 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4558 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4559 | err: |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4560 | /* drop submission reference */ |
| 4561 | io_put_req(req); |
| 4562 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4563 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4564 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4565 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4566 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4567 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4568 | } |
| 4569 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4570 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4571 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4572 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4573 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4574 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4575 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4576 | done_req: |
| 4577 | if (nxt) { |
| 4578 | req = nxt; |
| 4579 | nxt = NULL; |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame] | 4580 | |
| 4581 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 4582 | goto punt; |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4583 | goto again; |
| 4584 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4585 | } |
| 4586 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4587 | 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] | 4588 | { |
| 4589 | int ret; |
| 4590 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4591 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4592 | if (ret) { |
| 4593 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 4594 | fail_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4595 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4596 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4597 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4598 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 4599 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 4600 | ret = io_req_defer_prep(req, sqe); |
| 4601 | if (unlikely(ret < 0)) |
| 4602 | goto fail_req; |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4603 | /* |
| 4604 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 4605 | * to async execution. |
| 4606 | */ |
| 4607 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 4608 | io_queue_async_work(req); |
| 4609 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4610 | __io_queue_sqe(req, sqe); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4611 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4612 | } |
| 4613 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4614 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4615 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4616 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4617 | io_cqring_add_event(req, -ECANCELED); |
| 4618 | io_double_put_req(req); |
| 4619 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4620 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4621 | } |
| 4622 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4623 | #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] | 4624 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4625 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4626 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4627 | struct io_submit_state *state, struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4628 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4629 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4630 | unsigned int sqe_flags; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4631 | int ret; |
| 4632 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4633 | sqe_flags = READ_ONCE(sqe->flags); |
| 4634 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4635 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4636 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4637 | ret = -EINVAL; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4638 | goto err_req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4639 | } |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 4640 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 4641 | req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK| |
| 4642 | IOSQE_ASYNC); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4643 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4644 | ret = io_req_set_file(state, req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4645 | if (unlikely(ret)) { |
| 4646 | err_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4647 | io_cqring_add_event(req, ret); |
| 4648 | io_double_put_req(req); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4649 | return false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4650 | } |
| 4651 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4652 | /* |
| 4653 | * If we already have a head request, queue this one for async |
| 4654 | * submittal once the head completes. If we don't have a head but |
| 4655 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 4656 | * submitted sync once the chain is complete. If none of those |
| 4657 | * conditions are true (normal request), then just queue it. |
| 4658 | */ |
| 4659 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4660 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4661 | |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 4662 | /* |
| 4663 | * Taking sequential execution of a link, draining both sides |
| 4664 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all |
| 4665 | * requests in the link. So, it drains the head and the |
| 4666 | * next after the link request. The last one is done via |
| 4667 | * drain_next flag to persist the effect across calls. |
| 4668 | */ |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4669 | if (sqe_flags & IOSQE_IO_DRAIN) { |
| 4670 | head->flags |= REQ_F_IO_DRAIN; |
| 4671 | ctx->drain_next = 1; |
| 4672 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4673 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4674 | ret = -EAGAIN; |
| 4675 | goto err_req; |
| 4676 | } |
| 4677 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4678 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4679 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4680 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4681 | head->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4682 | goto err_req; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4683 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4684 | trace_io_uring_link(ctx, req, head); |
| 4685 | list_add_tail(&req->link_list, &head->link_list); |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4686 | |
| 4687 | /* last request of a link, enqueue the link */ |
| 4688 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) { |
| 4689 | io_queue_link_head(head); |
| 4690 | *link = NULL; |
| 4691 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4692 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4693 | if (unlikely(ctx->drain_next)) { |
| 4694 | req->flags |= REQ_F_IO_DRAIN; |
| 4695 | req->ctx->drain_next = 0; |
| 4696 | } |
| 4697 | if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { |
| 4698 | req->flags |= REQ_F_LINK; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4699 | INIT_LIST_HEAD(&req->link_list); |
| 4700 | ret = io_req_defer_prep(req, sqe); |
| 4701 | if (ret) |
| 4702 | req->flags |= REQ_F_FAIL_LINK; |
| 4703 | *link = req; |
| 4704 | } else { |
| 4705 | io_queue_sqe(req, sqe); |
| 4706 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4707 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4708 | |
| 4709 | return true; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4710 | } |
| 4711 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4712 | /* |
| 4713 | * Batched submission is done, ensure local IO is flushed out. |
| 4714 | */ |
| 4715 | static void io_submit_state_end(struct io_submit_state *state) |
| 4716 | { |
| 4717 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 4718 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4719 | if (state->free_reqs) |
| 4720 | kmem_cache_free_bulk(req_cachep, state->free_reqs, |
| 4721 | &state->reqs[state->cur_req]); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4722 | } |
| 4723 | |
| 4724 | /* |
| 4725 | * Start submission side cache. |
| 4726 | */ |
| 4727 | static void io_submit_state_start(struct io_submit_state *state, |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4728 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4729 | { |
| 4730 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4731 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4732 | state->file = NULL; |
| 4733 | state->ios_left = max_ios; |
| 4734 | } |
| 4735 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4736 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 4737 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4738 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4739 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 4740 | /* |
| 4741 | * Ensure any loads from the SQEs are done at this point, |
| 4742 | * since once we write the new head, the application could |
| 4743 | * write new data to them. |
| 4744 | */ |
| 4745 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4746 | } |
| 4747 | |
| 4748 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4749 | * 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] | 4750 | * that is mapped by userspace. This means that care needs to be taken to |
| 4751 | * ensure that reads are stable, as we cannot rely on userspace always |
| 4752 | * being a good citizen. If members of the sqe are validated and then later |
| 4753 | * used, it's important that those reads are done through READ_ONCE() to |
| 4754 | * prevent a re-load down the line. |
| 4755 | */ |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4756 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 4757 | const struct io_uring_sqe **sqe_ptr) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4758 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4759 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4760 | unsigned head; |
| 4761 | |
| 4762 | /* |
| 4763 | * The cached sq head (or cq tail) serves two purposes: |
| 4764 | * |
| 4765 | * 1) allows us to batch the cost of updating the user visible |
| 4766 | * head updates. |
| 4767 | * 2) allows the kernel side to track the head on its own, even |
| 4768 | * though the application is the one updating it. |
| 4769 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4770 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 4771 | if (likely(head < ctx->sq_entries)) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4772 | /* |
| 4773 | * All io need record the previous position, if LINK vs DARIN, |
| 4774 | * it can be used to mark the position of the first IO in the |
| 4775 | * link list. |
| 4776 | */ |
| 4777 | req->sequence = ctx->cached_sq_head; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4778 | *sqe_ptr = &ctx->sq_sqes[head]; |
| 4779 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); |
| 4780 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4781 | ctx->cached_sq_head++; |
| 4782 | return true; |
| 4783 | } |
| 4784 | |
| 4785 | /* drop invalid entries */ |
| 4786 | ctx->cached_sq_head++; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 4787 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4788 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4789 | return false; |
| 4790 | } |
| 4791 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4792 | 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] | 4793 | struct file *ring_file, int ring_fd, |
| 4794 | struct mm_struct **mm, bool async) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4795 | { |
| 4796 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4797 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4798 | int i, submitted = 0; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4799 | bool mm_fault = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4800 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 4801 | /* 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] | 4802 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 4803 | if (!list_empty(&ctx->cq_overflow_list) && |
| 4804 | !io_cqring_overflow_flush(ctx, false)) |
| 4805 | return -EBUSY; |
| 4806 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4807 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4808 | /* make sure SQ entry isn't read before tail */ |
| 4809 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 4810 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4811 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 4812 | return -EAGAIN; |
| 4813 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4814 | if (nr > IO_PLUG_THRESHOLD) { |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4815 | io_submit_state_start(&state, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4816 | statep = &state; |
| 4817 | } |
| 4818 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4819 | ctx->ring_fd = ring_fd; |
| 4820 | ctx->ring_file = ring_file; |
| 4821 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4822 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4823 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4824 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4825 | |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4826 | req = io_get_req(ctx, statep); |
| 4827 | if (unlikely(!req)) { |
| 4828 | if (!submitted) |
| 4829 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4830 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4831 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4832 | if (!io_get_sqring(ctx, req, &sqe)) { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4833 | __io_req_do_free(req); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4834 | break; |
| 4835 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4836 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4837 | /* will complete beyond this point, count as submitted */ |
| 4838 | submitted++; |
| 4839 | |
| 4840 | if (unlikely(req->opcode >= IORING_OP_LAST)) { |
| 4841 | io_cqring_add_event(req, -EINVAL); |
| 4842 | io_double_put_req(req); |
| 4843 | break; |
| 4844 | } |
| 4845 | |
| 4846 | if (io_op_defs[req->opcode].needs_mm && !*mm) { |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4847 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
| 4848 | if (!mm_fault) { |
| 4849 | use_mm(ctx->sqo_mm); |
| 4850 | *mm = ctx->sqo_mm; |
| 4851 | } |
| 4852 | } |
| 4853 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4854 | req->has_user = *mm != NULL; |
| 4855 | req->in_async = async; |
| 4856 | req->needs_fixed_file = async; |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 4857 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 4858 | true, async); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4859 | if (!io_submit_sqe(req, sqe, statep, &link)) |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4860 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4861 | } |
| 4862 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 4863 | if (unlikely(submitted != nr)) { |
| 4864 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
| 4865 | |
| 4866 | percpu_ref_put_many(&ctx->refs, nr - ref_used); |
| 4867 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4868 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4869 | io_queue_link_head(link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4870 | if (statep) |
| 4871 | io_submit_state_end(&state); |
| 4872 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 4873 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 4874 | io_commit_sqring(ctx); |
| 4875 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4876 | return submitted; |
| 4877 | } |
| 4878 | |
| 4879 | static int io_sq_thread(void *data) |
| 4880 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4881 | struct io_ring_ctx *ctx = data; |
| 4882 | struct mm_struct *cur_mm = NULL; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4883 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4884 | mm_segment_t old_fs; |
| 4885 | DEFINE_WAIT(wait); |
| 4886 | unsigned inflight; |
| 4887 | unsigned long timeout; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4888 | int ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4889 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 4890 | complete(&ctx->completions[1]); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 4891 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4892 | old_fs = get_fs(); |
| 4893 | set_fs(USER_DS); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4894 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4895 | |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4896 | ret = timeout = inflight = 0; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4897 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4898 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4899 | |
| 4900 | if (inflight) { |
| 4901 | unsigned nr_events = 0; |
| 4902 | |
| 4903 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 4904 | /* |
| 4905 | * inflight is the count of the maximum possible |
| 4906 | * entries we submitted, but it can be smaller |
| 4907 | * if we dropped some of them. If we don't have |
| 4908 | * poll entries available, then we know that we |
| 4909 | * have nothing left to poll for. Reset the |
| 4910 | * inflight count to zero in that case. |
| 4911 | */ |
| 4912 | mutex_lock(&ctx->uring_lock); |
| 4913 | if (!list_empty(&ctx->poll_list)) |
| 4914 | __io_iopoll_check(ctx, &nr_events, 0); |
| 4915 | else |
| 4916 | inflight = 0; |
| 4917 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4918 | } else { |
| 4919 | /* |
| 4920 | * Normal IO, just pretend everything completed. |
| 4921 | * We don't have to poll completions for that. |
| 4922 | */ |
| 4923 | nr_events = inflight; |
| 4924 | } |
| 4925 | |
| 4926 | inflight -= nr_events; |
| 4927 | if (!inflight) |
| 4928 | timeout = jiffies + ctx->sq_thread_idle; |
| 4929 | } |
| 4930 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4931 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4932 | |
| 4933 | /* |
| 4934 | * If submit got -EBUSY, flag us as needing the application |
| 4935 | * to enter the kernel to reap and flush events. |
| 4936 | */ |
| 4937 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4938 | /* |
| 4939 | * We're polling. If we're within the defined idle |
| 4940 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4941 | * to sleep. The exception is if we got EBUSY doing |
| 4942 | * more IO, we should wait for the application to |
| 4943 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4944 | */ |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4945 | if (inflight || |
| 4946 | (!time_after(jiffies, timeout) && ret != -EBUSY)) { |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 4947 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4948 | continue; |
| 4949 | } |
| 4950 | |
| 4951 | /* |
| 4952 | * Drop cur_mm before scheduling, we can't hold it for |
| 4953 | * long periods (or over schedule()). Do this before |
| 4954 | * adding ourselves to the waitqueue, as the unuse/drop |
| 4955 | * may sleep. |
| 4956 | */ |
| 4957 | if (cur_mm) { |
| 4958 | unuse_mm(cur_mm); |
| 4959 | mmput(cur_mm); |
| 4960 | cur_mm = NULL; |
| 4961 | } |
| 4962 | |
| 4963 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 4964 | TASK_INTERRUPTIBLE); |
| 4965 | |
| 4966 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4967 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 4968 | /* make sure to read SQ tail after writing flags */ |
| 4969 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4970 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4971 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4972 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4973 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4974 | finish_wait(&ctx->sqo_wait, &wait); |
| 4975 | break; |
| 4976 | } |
| 4977 | if (signal_pending(current)) |
| 4978 | flush_signals(current); |
| 4979 | schedule(); |
| 4980 | finish_wait(&ctx->sqo_wait, &wait); |
| 4981 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4982 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4983 | continue; |
| 4984 | } |
| 4985 | finish_wait(&ctx->sqo_wait, &wait); |
| 4986 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4987 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4988 | } |
| 4989 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 4990 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4991 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 4992 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4993 | if (ret > 0) |
| 4994 | inflight += ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4995 | } |
| 4996 | |
| 4997 | set_fs(old_fs); |
| 4998 | if (cur_mm) { |
| 4999 | unuse_mm(cur_mm); |
| 5000 | mmput(cur_mm); |
| 5001 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5002 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5003 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5004 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5005 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5006 | return 0; |
| 5007 | } |
| 5008 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5009 | struct io_wait_queue { |
| 5010 | struct wait_queue_entry wq; |
| 5011 | struct io_ring_ctx *ctx; |
| 5012 | unsigned to_wait; |
| 5013 | unsigned nr_timeouts; |
| 5014 | }; |
| 5015 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5016 | 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] | 5017 | { |
| 5018 | struct io_ring_ctx *ctx = iowq->ctx; |
| 5019 | |
| 5020 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 5021 | * 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] | 5022 | * started waiting. For timeouts, we always want to return to userspace, |
| 5023 | * regardless of event count. |
| 5024 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5025 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5026 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 5027 | } |
| 5028 | |
| 5029 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 5030 | int wake_flags, void *key) |
| 5031 | { |
| 5032 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 5033 | wq); |
| 5034 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5035 | /* use noflush == true, as we can't safely rely on locking context */ |
| 5036 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5037 | return -1; |
| 5038 | |
| 5039 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 5040 | } |
| 5041 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5042 | /* |
| 5043 | * Wait until events become available, if we don't already have some. The |
| 5044 | * application must reap them itself, as they reside on the shared cq ring. |
| 5045 | */ |
| 5046 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 5047 | const sigset_t __user *sig, size_t sigsz) |
| 5048 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5049 | struct io_wait_queue iowq = { |
| 5050 | .wq = { |
| 5051 | .private = current, |
| 5052 | .func = io_wake_function, |
| 5053 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 5054 | }, |
| 5055 | .ctx = ctx, |
| 5056 | .to_wait = min_events, |
| 5057 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5058 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5059 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5060 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5061 | if (io_cqring_events(ctx, false) >= min_events) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5062 | return 0; |
| 5063 | |
| 5064 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5065 | #ifdef CONFIG_COMPAT |
| 5066 | if (in_compat_syscall()) |
| 5067 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5068 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5069 | else |
| 5070 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5071 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5072 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5073 | if (ret) |
| 5074 | return ret; |
| 5075 | } |
| 5076 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5077 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 5078 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5079 | do { |
| 5080 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 5081 | TASK_INTERRUPTIBLE); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5082 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5083 | break; |
| 5084 | schedule(); |
| 5085 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5086 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5087 | break; |
| 5088 | } |
| 5089 | } while (1); |
| 5090 | finish_wait(&ctx->wait, &iowq.wq); |
| 5091 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5092 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5093 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5094 | 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] | 5095 | } |
| 5096 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5097 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5098 | { |
| 5099 | #if defined(CONFIG_UNIX) |
| 5100 | if (ctx->ring_sock) { |
| 5101 | struct sock *sock = ctx->ring_sock->sk; |
| 5102 | struct sk_buff *skb; |
| 5103 | |
| 5104 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 5105 | kfree_skb(skb); |
| 5106 | } |
| 5107 | #else |
| 5108 | int i; |
| 5109 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5110 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5111 | struct file *file; |
| 5112 | |
| 5113 | file = io_file_from_index(ctx, i); |
| 5114 | if (file) |
| 5115 | fput(file); |
| 5116 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5117 | #endif |
| 5118 | } |
| 5119 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5120 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 5121 | { |
| 5122 | struct fixed_file_data *data; |
| 5123 | |
| 5124 | data = container_of(ref, struct fixed_file_data, refs); |
| 5125 | complete(&data->done); |
| 5126 | } |
| 5127 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5128 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5129 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5130 | struct fixed_file_data *data = ctx->file_data; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5131 | unsigned nr_tables, i; |
| 5132 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5133 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5134 | return -ENXIO; |
| 5135 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5136 | /* protect against inflight atomic switch, which drops the ref */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5137 | percpu_ref_get(&data->refs); |
Jens Axboe | e46a795 | 2020-01-17 11:15:34 -0700 | [diff] [blame] | 5138 | /* wait for existing switches */ |
| 5139 | flush_work(&data->ref_work); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5140 | percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill); |
| 5141 | wait_for_completion(&data->done); |
| 5142 | percpu_ref_put(&data->refs); |
Jens Axboe | e46a795 | 2020-01-17 11:15:34 -0700 | [diff] [blame] | 5143 | /* flush potential new switch */ |
| 5144 | flush_work(&data->ref_work); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5145 | percpu_ref_exit(&data->refs); |
| 5146 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5147 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5148 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 5149 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5150 | kfree(data->table[i].files); |
| 5151 | kfree(data->table); |
| 5152 | kfree(data); |
| 5153 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5154 | ctx->nr_user_files = 0; |
| 5155 | return 0; |
| 5156 | } |
| 5157 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5158 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 5159 | { |
| 5160 | if (ctx->sqo_thread) { |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5161 | wait_for_completion(&ctx->completions[1]); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5162 | /* |
| 5163 | * The park is a bit of a work-around, without it we get |
| 5164 | * warning spews on shutdown with SQPOLL set and affinity |
| 5165 | * set to a single CPU. |
| 5166 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5167 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5168 | kthread_stop(ctx->sqo_thread); |
| 5169 | ctx->sqo_thread = NULL; |
| 5170 | } |
| 5171 | } |
| 5172 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5173 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 5174 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5175 | io_sq_thread_stop(ctx); |
| 5176 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5177 | if (ctx->io_wq) { |
| 5178 | io_wq_destroy(ctx->io_wq); |
| 5179 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5180 | } |
| 5181 | } |
| 5182 | |
| 5183 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5184 | /* |
| 5185 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 5186 | * the io_uring can be safely unregistered on process exit, even if we have |
| 5187 | * loops in the file referencing. |
| 5188 | */ |
| 5189 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 5190 | { |
| 5191 | struct sock *sk = ctx->ring_sock->sk; |
| 5192 | struct scm_fp_list *fpl; |
| 5193 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5194 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5195 | |
| 5196 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 5197 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 5198 | |
| 5199 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 5200 | return -EMFILE; |
| 5201 | } |
| 5202 | |
| 5203 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 5204 | if (!fpl) |
| 5205 | return -ENOMEM; |
| 5206 | |
| 5207 | skb = alloc_skb(0, GFP_KERNEL); |
| 5208 | if (!skb) { |
| 5209 | kfree(fpl); |
| 5210 | return -ENOMEM; |
| 5211 | } |
| 5212 | |
| 5213 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5214 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5215 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5216 | fpl->user = get_uid(ctx->user); |
| 5217 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5218 | struct file *file = io_file_from_index(ctx, i + offset); |
| 5219 | |
| 5220 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5221 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5222 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5223 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 5224 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5225 | } |
| 5226 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5227 | if (nr_files) { |
| 5228 | fpl->max = SCM_MAX_FD; |
| 5229 | fpl->count = nr_files; |
| 5230 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5231 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5232 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 5233 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5234 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5235 | for (i = 0; i < nr_files; i++) |
| 5236 | fput(fpl->fp[i]); |
| 5237 | } else { |
| 5238 | kfree_skb(skb); |
| 5239 | kfree(fpl); |
| 5240 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5241 | |
| 5242 | return 0; |
| 5243 | } |
| 5244 | |
| 5245 | /* |
| 5246 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 5247 | * causes regular reference counting to break down. We rely on the UNIX |
| 5248 | * garbage collection to take care of this problem for us. |
| 5249 | */ |
| 5250 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5251 | { |
| 5252 | unsigned left, total; |
| 5253 | int ret = 0; |
| 5254 | |
| 5255 | total = 0; |
| 5256 | left = ctx->nr_user_files; |
| 5257 | while (left) { |
| 5258 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5259 | |
| 5260 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 5261 | if (ret) |
| 5262 | break; |
| 5263 | left -= this_files; |
| 5264 | total += this_files; |
| 5265 | } |
| 5266 | |
| 5267 | if (!ret) |
| 5268 | return 0; |
| 5269 | |
| 5270 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5271 | struct file *file = io_file_from_index(ctx, total); |
| 5272 | |
| 5273 | if (file) |
| 5274 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5275 | total++; |
| 5276 | } |
| 5277 | |
| 5278 | return ret; |
| 5279 | } |
| 5280 | #else |
| 5281 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5282 | { |
| 5283 | return 0; |
| 5284 | } |
| 5285 | #endif |
| 5286 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5287 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 5288 | unsigned nr_files) |
| 5289 | { |
| 5290 | int i; |
| 5291 | |
| 5292 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5293 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5294 | unsigned this_files; |
| 5295 | |
| 5296 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 5297 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 5298 | GFP_KERNEL); |
| 5299 | if (!table->files) |
| 5300 | break; |
| 5301 | nr_files -= this_files; |
| 5302 | } |
| 5303 | |
| 5304 | if (i == nr_tables) |
| 5305 | return 0; |
| 5306 | |
| 5307 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5308 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5309 | kfree(table->files); |
| 5310 | } |
| 5311 | return 1; |
| 5312 | } |
| 5313 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5314 | 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] | 5315 | { |
| 5316 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5317 | struct sock *sock = ctx->ring_sock->sk; |
| 5318 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 5319 | struct sk_buff *skb; |
| 5320 | int i; |
| 5321 | |
| 5322 | __skb_queue_head_init(&list); |
| 5323 | |
| 5324 | /* |
| 5325 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 5326 | * remove this entry and rearrange the file array. |
| 5327 | */ |
| 5328 | skb = skb_dequeue(head); |
| 5329 | while (skb) { |
| 5330 | struct scm_fp_list *fp; |
| 5331 | |
| 5332 | fp = UNIXCB(skb).fp; |
| 5333 | for (i = 0; i < fp->count; i++) { |
| 5334 | int left; |
| 5335 | |
| 5336 | if (fp->fp[i] != file) |
| 5337 | continue; |
| 5338 | |
| 5339 | unix_notinflight(fp->user, fp->fp[i]); |
| 5340 | left = fp->count - 1 - i; |
| 5341 | if (left) { |
| 5342 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 5343 | left * sizeof(struct file *)); |
| 5344 | } |
| 5345 | fp->count--; |
| 5346 | if (!fp->count) { |
| 5347 | kfree_skb(skb); |
| 5348 | skb = NULL; |
| 5349 | } else { |
| 5350 | __skb_queue_tail(&list, skb); |
| 5351 | } |
| 5352 | fput(file); |
| 5353 | file = NULL; |
| 5354 | break; |
| 5355 | } |
| 5356 | |
| 5357 | if (!file) |
| 5358 | break; |
| 5359 | |
| 5360 | __skb_queue_tail(&list, skb); |
| 5361 | |
| 5362 | skb = skb_dequeue(head); |
| 5363 | } |
| 5364 | |
| 5365 | if (skb_peek(&list)) { |
| 5366 | spin_lock_irq(&head->lock); |
| 5367 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 5368 | __skb_queue_tail(head, skb); |
| 5369 | spin_unlock_irq(&head->lock); |
| 5370 | } |
| 5371 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5372 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5373 | #endif |
| 5374 | } |
| 5375 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5376 | struct io_file_put { |
| 5377 | struct llist_node llist; |
| 5378 | struct file *file; |
| 5379 | struct completion *done; |
| 5380 | }; |
| 5381 | |
| 5382 | static void io_ring_file_ref_switch(struct work_struct *work) |
| 5383 | { |
| 5384 | struct io_file_put *pfile, *tmp; |
| 5385 | struct fixed_file_data *data; |
| 5386 | struct llist_node *node; |
| 5387 | |
| 5388 | data = container_of(work, struct fixed_file_data, ref_work); |
| 5389 | |
| 5390 | while ((node = llist_del_all(&data->put_llist)) != NULL) { |
| 5391 | llist_for_each_entry_safe(pfile, tmp, node, llist) { |
| 5392 | io_ring_file_put(data->ctx, pfile->file); |
| 5393 | if (pfile->done) |
| 5394 | complete(pfile->done); |
| 5395 | else |
| 5396 | kfree(pfile); |
| 5397 | } |
| 5398 | } |
| 5399 | |
| 5400 | percpu_ref_get(&data->refs); |
| 5401 | percpu_ref_switch_to_percpu(&data->refs); |
| 5402 | } |
| 5403 | |
| 5404 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 5405 | { |
| 5406 | struct fixed_file_data *data; |
| 5407 | |
| 5408 | data = container_of(ref, struct fixed_file_data, refs); |
| 5409 | |
| 5410 | /* we can't safely switch from inside this context, punt to wq */ |
| 5411 | queue_work(system_wq, &data->ref_work); |
| 5412 | } |
| 5413 | |
| 5414 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5415 | unsigned nr_args) |
| 5416 | { |
| 5417 | __s32 __user *fds = (__s32 __user *) arg; |
| 5418 | unsigned nr_tables; |
| 5419 | struct file *file; |
| 5420 | int fd, ret = 0; |
| 5421 | unsigned i; |
| 5422 | |
| 5423 | if (ctx->file_data) |
| 5424 | return -EBUSY; |
| 5425 | if (!nr_args) |
| 5426 | return -EINVAL; |
| 5427 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 5428 | return -EMFILE; |
| 5429 | |
| 5430 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 5431 | if (!ctx->file_data) |
| 5432 | return -ENOMEM; |
| 5433 | ctx->file_data->ctx = ctx; |
| 5434 | init_completion(&ctx->file_data->done); |
| 5435 | |
| 5436 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 5437 | ctx->file_data->table = kcalloc(nr_tables, |
| 5438 | sizeof(struct fixed_file_table), |
| 5439 | GFP_KERNEL); |
| 5440 | if (!ctx->file_data->table) { |
| 5441 | kfree(ctx->file_data); |
| 5442 | ctx->file_data = NULL; |
| 5443 | return -ENOMEM; |
| 5444 | } |
| 5445 | |
| 5446 | if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero, |
| 5447 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 5448 | kfree(ctx->file_data->table); |
| 5449 | kfree(ctx->file_data); |
| 5450 | ctx->file_data = NULL; |
| 5451 | return -ENOMEM; |
| 5452 | } |
| 5453 | ctx->file_data->put_llist.first = NULL; |
| 5454 | INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch); |
| 5455 | |
| 5456 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 5457 | percpu_ref_exit(&ctx->file_data->refs); |
| 5458 | kfree(ctx->file_data->table); |
| 5459 | kfree(ctx->file_data); |
| 5460 | ctx->file_data = NULL; |
| 5461 | return -ENOMEM; |
| 5462 | } |
| 5463 | |
| 5464 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 5465 | struct fixed_file_table *table; |
| 5466 | unsigned index; |
| 5467 | |
| 5468 | ret = -EFAULT; |
| 5469 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 5470 | break; |
| 5471 | /* allow sparse sets */ |
| 5472 | if (fd == -1) { |
| 5473 | ret = 0; |
| 5474 | continue; |
| 5475 | } |
| 5476 | |
| 5477 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 5478 | index = i & IORING_FILE_TABLE_MASK; |
| 5479 | file = fget(fd); |
| 5480 | |
| 5481 | ret = -EBADF; |
| 5482 | if (!file) |
| 5483 | break; |
| 5484 | |
| 5485 | /* |
| 5486 | * Don't allow io_uring instances to be registered. If UNIX |
| 5487 | * isn't enabled, then this causes a reference cycle and this |
| 5488 | * instance can never get freed. If UNIX is enabled we'll |
| 5489 | * handle it just fine, but there's still no point in allowing |
| 5490 | * a ring fd as it doesn't support regular read/write anyway. |
| 5491 | */ |
| 5492 | if (file->f_op == &io_uring_fops) { |
| 5493 | fput(file); |
| 5494 | break; |
| 5495 | } |
| 5496 | ret = 0; |
| 5497 | table->files[index] = file; |
| 5498 | } |
| 5499 | |
| 5500 | if (ret) { |
| 5501 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5502 | file = io_file_from_index(ctx, i); |
| 5503 | if (file) |
| 5504 | fput(file); |
| 5505 | } |
| 5506 | for (i = 0; i < nr_tables; i++) |
| 5507 | kfree(ctx->file_data->table[i].files); |
| 5508 | |
| 5509 | kfree(ctx->file_data->table); |
| 5510 | kfree(ctx->file_data); |
| 5511 | ctx->file_data = NULL; |
| 5512 | ctx->nr_user_files = 0; |
| 5513 | return ret; |
| 5514 | } |
| 5515 | |
| 5516 | ret = io_sqe_files_scm(ctx); |
| 5517 | if (ret) |
| 5518 | io_sqe_files_unregister(ctx); |
| 5519 | |
| 5520 | return ret; |
| 5521 | } |
| 5522 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5523 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 5524 | int index) |
| 5525 | { |
| 5526 | #if defined(CONFIG_UNIX) |
| 5527 | struct sock *sock = ctx->ring_sock->sk; |
| 5528 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 5529 | struct sk_buff *skb; |
| 5530 | |
| 5531 | /* |
| 5532 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 5533 | * file set. If there's no room, fall back to allocating a new skb |
| 5534 | * and filling it in. |
| 5535 | */ |
| 5536 | spin_lock_irq(&head->lock); |
| 5537 | skb = skb_peek(head); |
| 5538 | if (skb) { |
| 5539 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 5540 | |
| 5541 | if (fpl->count < SCM_MAX_FD) { |
| 5542 | __skb_unlink(skb, head); |
| 5543 | spin_unlock_irq(&head->lock); |
| 5544 | fpl->fp[fpl->count] = get_file(file); |
| 5545 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 5546 | fpl->count++; |
| 5547 | spin_lock_irq(&head->lock); |
| 5548 | __skb_queue_head(head, skb); |
| 5549 | } else { |
| 5550 | skb = NULL; |
| 5551 | } |
| 5552 | } |
| 5553 | spin_unlock_irq(&head->lock); |
| 5554 | |
| 5555 | if (skb) { |
| 5556 | fput(file); |
| 5557 | return 0; |
| 5558 | } |
| 5559 | |
| 5560 | return __io_sqe_files_scm(ctx, 1, index); |
| 5561 | #else |
| 5562 | return 0; |
| 5563 | #endif |
| 5564 | } |
| 5565 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5566 | static void io_atomic_switch(struct percpu_ref *ref) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5567 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5568 | struct fixed_file_data *data; |
| 5569 | |
| 5570 | data = container_of(ref, struct fixed_file_data, refs); |
| 5571 | clear_bit(FFD_F_ATOMIC, &data->state); |
| 5572 | } |
| 5573 | |
| 5574 | static bool io_queue_file_removal(struct fixed_file_data *data, |
| 5575 | struct file *file) |
| 5576 | { |
| 5577 | struct io_file_put *pfile, pfile_stack; |
| 5578 | DECLARE_COMPLETION_ONSTACK(done); |
| 5579 | |
| 5580 | /* |
| 5581 | * If we fail allocating the struct we need for doing async reomval |
| 5582 | * of this file, just punt to sync and wait for it. |
| 5583 | */ |
| 5584 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
| 5585 | if (!pfile) { |
| 5586 | pfile = &pfile_stack; |
| 5587 | pfile->done = &done; |
| 5588 | } |
| 5589 | |
| 5590 | pfile->file = file; |
| 5591 | llist_add(&pfile->llist, &data->put_llist); |
| 5592 | |
| 5593 | if (pfile == &pfile_stack) { |
| 5594 | if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5595 | percpu_ref_put(&data->refs); |
| 5596 | percpu_ref_switch_to_atomic(&data->refs, |
| 5597 | io_atomic_switch); |
| 5598 | } |
| 5599 | wait_for_completion(&done); |
| 5600 | flush_work(&data->ref_work); |
| 5601 | return false; |
| 5602 | } |
| 5603 | |
| 5604 | return true; |
| 5605 | } |
| 5606 | |
| 5607 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 5608 | struct io_uring_files_update *up, |
| 5609 | unsigned nr_args) |
| 5610 | { |
| 5611 | struct fixed_file_data *data = ctx->file_data; |
| 5612 | bool ref_switch = false; |
| 5613 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5614 | __s32 __user *fds; |
| 5615 | int fd, i, err; |
| 5616 | __u32 done; |
| 5617 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5618 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5619 | return -EOVERFLOW; |
| 5620 | if (done > ctx->nr_user_files) |
| 5621 | return -EINVAL; |
| 5622 | |
| 5623 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5624 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5625 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5626 | struct fixed_file_table *table; |
| 5627 | unsigned index; |
| 5628 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5629 | err = 0; |
| 5630 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 5631 | err = -EFAULT; |
| 5632 | break; |
| 5633 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5634 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 5635 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5636 | index = i & IORING_FILE_TABLE_MASK; |
| 5637 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5638 | file = io_file_from_index(ctx, index); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5639 | table->files[index] = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5640 | if (io_queue_file_removal(data, file)) |
| 5641 | ref_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5642 | } |
| 5643 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5644 | file = fget(fd); |
| 5645 | if (!file) { |
| 5646 | err = -EBADF; |
| 5647 | break; |
| 5648 | } |
| 5649 | /* |
| 5650 | * Don't allow io_uring instances to be registered. If |
| 5651 | * UNIX isn't enabled, then this causes a reference |
| 5652 | * cycle and this instance can never get freed. If UNIX |
| 5653 | * is enabled we'll handle it just fine, but there's |
| 5654 | * still no point in allowing a ring fd as it doesn't |
| 5655 | * support regular read/write anyway. |
| 5656 | */ |
| 5657 | if (file->f_op == &io_uring_fops) { |
| 5658 | fput(file); |
| 5659 | err = -EBADF; |
| 5660 | break; |
| 5661 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5662 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5663 | err = io_sqe_file_register(ctx, file, i); |
| 5664 | if (err) |
| 5665 | break; |
| 5666 | } |
| 5667 | nr_args--; |
| 5668 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5669 | up->offset++; |
| 5670 | } |
| 5671 | |
| 5672 | if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5673 | percpu_ref_put(&data->refs); |
| 5674 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5675 | } |
| 5676 | |
| 5677 | return done ? done : err; |
| 5678 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5679 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 5680 | unsigned nr_args) |
| 5681 | { |
| 5682 | struct io_uring_files_update up; |
| 5683 | |
| 5684 | if (!ctx->file_data) |
| 5685 | return -ENXIO; |
| 5686 | if (!nr_args) |
| 5687 | return -EINVAL; |
| 5688 | if (copy_from_user(&up, arg, sizeof(up))) |
| 5689 | return -EFAULT; |
| 5690 | if (up.resv) |
| 5691 | return -EINVAL; |
| 5692 | |
| 5693 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 5694 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5695 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 5696 | static void io_put_work(struct io_wq_work *work) |
| 5697 | { |
| 5698 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5699 | |
| 5700 | io_put_req(req); |
| 5701 | } |
| 5702 | |
| 5703 | static void io_get_work(struct io_wq_work *work) |
| 5704 | { |
| 5705 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5706 | |
| 5707 | refcount_inc(&req->refs); |
| 5708 | } |
| 5709 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 5710 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
| 5711 | struct io_uring_params *p) |
| 5712 | { |
| 5713 | struct io_wq_data data; |
| 5714 | struct fd f; |
| 5715 | struct io_ring_ctx *ctx_attach; |
| 5716 | unsigned int concurrency; |
| 5717 | int ret = 0; |
| 5718 | |
| 5719 | data.user = ctx->user; |
| 5720 | data.get_work = io_get_work; |
| 5721 | data.put_work = io_put_work; |
| 5722 | |
| 5723 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { |
| 5724 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 5725 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
| 5726 | |
| 5727 | ctx->io_wq = io_wq_create(concurrency, &data); |
| 5728 | if (IS_ERR(ctx->io_wq)) { |
| 5729 | ret = PTR_ERR(ctx->io_wq); |
| 5730 | ctx->io_wq = NULL; |
| 5731 | } |
| 5732 | return ret; |
| 5733 | } |
| 5734 | |
| 5735 | f = fdget(p->wq_fd); |
| 5736 | if (!f.file) |
| 5737 | return -EBADF; |
| 5738 | |
| 5739 | if (f.file->f_op != &io_uring_fops) { |
| 5740 | ret = -EINVAL; |
| 5741 | goto out_fput; |
| 5742 | } |
| 5743 | |
| 5744 | ctx_attach = f.file->private_data; |
| 5745 | /* @io_wq is protected by holding the fd */ |
| 5746 | if (!io_wq_get(ctx_attach->io_wq, &data)) { |
| 5747 | ret = -EINVAL; |
| 5748 | goto out_fput; |
| 5749 | } |
| 5750 | |
| 5751 | ctx->io_wq = ctx_attach->io_wq; |
| 5752 | out_fput: |
| 5753 | fdput(f); |
| 5754 | return ret; |
| 5755 | } |
| 5756 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5757 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 5758 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5759 | { |
| 5760 | int ret; |
| 5761 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5762 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5763 | mmgrab(current->mm); |
| 5764 | ctx->sqo_mm = current->mm; |
| 5765 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5766 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 5767 | ret = -EPERM; |
| 5768 | if (!capable(CAP_SYS_ADMIN)) |
| 5769 | goto err; |
| 5770 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5771 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 5772 | if (!ctx->sq_thread_idle) |
| 5773 | ctx->sq_thread_idle = HZ; |
| 5774 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5775 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5776 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5777 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5778 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5779 | if (cpu >= nr_cpu_ids) |
| 5780 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 5781 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5782 | goto err; |
| 5783 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5784 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 5785 | ctx, cpu, |
| 5786 | "io_uring-sq"); |
| 5787 | } else { |
| 5788 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 5789 | "io_uring-sq"); |
| 5790 | } |
| 5791 | if (IS_ERR(ctx->sqo_thread)) { |
| 5792 | ret = PTR_ERR(ctx->sqo_thread); |
| 5793 | ctx->sqo_thread = NULL; |
| 5794 | goto err; |
| 5795 | } |
| 5796 | wake_up_process(ctx->sqo_thread); |
| 5797 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 5798 | /* Can't have SQ_AFF without SQPOLL */ |
| 5799 | ret = -EINVAL; |
| 5800 | goto err; |
| 5801 | } |
| 5802 | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 5803 | ret = io_init_wq_offload(ctx, p); |
| 5804 | if (ret) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5805 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5806 | |
| 5807 | return 0; |
| 5808 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 5809 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5810 | mmdrop(ctx->sqo_mm); |
| 5811 | ctx->sqo_mm = NULL; |
| 5812 | return ret; |
| 5813 | } |
| 5814 | |
| 5815 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 5816 | { |
| 5817 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 5818 | } |
| 5819 | |
| 5820 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 5821 | { |
| 5822 | unsigned long page_limit, cur_pages, new_pages; |
| 5823 | |
| 5824 | /* Don't allow more pages than we can safely lock */ |
| 5825 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 5826 | |
| 5827 | do { |
| 5828 | cur_pages = atomic_long_read(&user->locked_vm); |
| 5829 | new_pages = cur_pages + nr_pages; |
| 5830 | if (new_pages > page_limit) |
| 5831 | return -ENOMEM; |
| 5832 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 5833 | new_pages) != cur_pages); |
| 5834 | |
| 5835 | return 0; |
| 5836 | } |
| 5837 | |
| 5838 | static void io_mem_free(void *ptr) |
| 5839 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 5840 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5841 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 5842 | if (!ptr) |
| 5843 | return; |
| 5844 | |
| 5845 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5846 | if (put_page_testzero(page)) |
| 5847 | free_compound_page(page); |
| 5848 | } |
| 5849 | |
| 5850 | static void *io_mem_alloc(size_t size) |
| 5851 | { |
| 5852 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 5853 | __GFP_NORETRY; |
| 5854 | |
| 5855 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 5856 | } |
| 5857 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5858 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 5859 | size_t *sq_offset) |
| 5860 | { |
| 5861 | struct io_rings *rings; |
| 5862 | size_t off, sq_array_size; |
| 5863 | |
| 5864 | off = struct_size(rings, cqes, cq_entries); |
| 5865 | if (off == SIZE_MAX) |
| 5866 | return SIZE_MAX; |
| 5867 | |
| 5868 | #ifdef CONFIG_SMP |
| 5869 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 5870 | if (off == 0) |
| 5871 | return SIZE_MAX; |
| 5872 | #endif |
| 5873 | |
| 5874 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 5875 | if (sq_array_size == SIZE_MAX) |
| 5876 | return SIZE_MAX; |
| 5877 | |
| 5878 | if (check_add_overflow(off, sq_array_size, &off)) |
| 5879 | return SIZE_MAX; |
| 5880 | |
| 5881 | if (sq_offset) |
| 5882 | *sq_offset = off; |
| 5883 | |
| 5884 | return off; |
| 5885 | } |
| 5886 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5887 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 5888 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5889 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5890 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5891 | pages = (size_t)1 << get_order( |
| 5892 | rings_size(sq_entries, cq_entries, NULL)); |
| 5893 | pages += (size_t)1 << get_order( |
| 5894 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5895 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5896 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5897 | } |
| 5898 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5899 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 5900 | { |
| 5901 | int i, j; |
| 5902 | |
| 5903 | if (!ctx->user_bufs) |
| 5904 | return -ENXIO; |
| 5905 | |
| 5906 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 5907 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 5908 | |
| 5909 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 5910 | put_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5911 | |
| 5912 | if (ctx->account_mem) |
| 5913 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5914 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5915 | imu->nr_bvecs = 0; |
| 5916 | } |
| 5917 | |
| 5918 | kfree(ctx->user_bufs); |
| 5919 | ctx->user_bufs = NULL; |
| 5920 | ctx->nr_user_bufs = 0; |
| 5921 | return 0; |
| 5922 | } |
| 5923 | |
| 5924 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 5925 | void __user *arg, unsigned index) |
| 5926 | { |
| 5927 | struct iovec __user *src; |
| 5928 | |
| 5929 | #ifdef CONFIG_COMPAT |
| 5930 | if (ctx->compat) { |
| 5931 | struct compat_iovec __user *ciovs; |
| 5932 | struct compat_iovec ciov; |
| 5933 | |
| 5934 | ciovs = (struct compat_iovec __user *) arg; |
| 5935 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 5936 | return -EFAULT; |
| 5937 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 5938 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5939 | dst->iov_len = ciov.iov_len; |
| 5940 | return 0; |
| 5941 | } |
| 5942 | #endif |
| 5943 | src = (struct iovec __user *) arg; |
| 5944 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 5945 | return -EFAULT; |
| 5946 | return 0; |
| 5947 | } |
| 5948 | |
| 5949 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5950 | unsigned nr_args) |
| 5951 | { |
| 5952 | struct vm_area_struct **vmas = NULL; |
| 5953 | struct page **pages = NULL; |
| 5954 | int i, j, got_pages = 0; |
| 5955 | int ret = -EINVAL; |
| 5956 | |
| 5957 | if (ctx->user_bufs) |
| 5958 | return -EBUSY; |
| 5959 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 5960 | return -EINVAL; |
| 5961 | |
| 5962 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 5963 | GFP_KERNEL); |
| 5964 | if (!ctx->user_bufs) |
| 5965 | return -ENOMEM; |
| 5966 | |
| 5967 | for (i = 0; i < nr_args; i++) { |
| 5968 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 5969 | unsigned long off, start, end, ubuf; |
| 5970 | int pret, nr_pages; |
| 5971 | struct iovec iov; |
| 5972 | size_t size; |
| 5973 | |
| 5974 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 5975 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 5976 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5977 | |
| 5978 | /* |
| 5979 | * Don't impose further limits on the size and buffer |
| 5980 | * constraints here, we'll -EINVAL later when IO is |
| 5981 | * submitted if they are wrong. |
| 5982 | */ |
| 5983 | ret = -EFAULT; |
| 5984 | if (!iov.iov_base || !iov.iov_len) |
| 5985 | goto err; |
| 5986 | |
| 5987 | /* arbitrary limit, but we need something */ |
| 5988 | if (iov.iov_len > SZ_1G) |
| 5989 | goto err; |
| 5990 | |
| 5991 | ubuf = (unsigned long) iov.iov_base; |
| 5992 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 5993 | start = ubuf >> PAGE_SHIFT; |
| 5994 | nr_pages = end - start; |
| 5995 | |
| 5996 | if (ctx->account_mem) { |
| 5997 | ret = io_account_mem(ctx->user, nr_pages); |
| 5998 | if (ret) |
| 5999 | goto err; |
| 6000 | } |
| 6001 | |
| 6002 | ret = 0; |
| 6003 | if (!pages || nr_pages > got_pages) { |
| 6004 | kfree(vmas); |
| 6005 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6006 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6007 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6008 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6009 | sizeof(struct vm_area_struct *), |
| 6010 | GFP_KERNEL); |
| 6011 | if (!pages || !vmas) { |
| 6012 | ret = -ENOMEM; |
| 6013 | if (ctx->account_mem) |
| 6014 | io_unaccount_mem(ctx->user, nr_pages); |
| 6015 | goto err; |
| 6016 | } |
| 6017 | got_pages = nr_pages; |
| 6018 | } |
| 6019 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6020 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6021 | GFP_KERNEL); |
| 6022 | ret = -ENOMEM; |
| 6023 | if (!imu->bvec) { |
| 6024 | if (ctx->account_mem) |
| 6025 | io_unaccount_mem(ctx->user, nr_pages); |
| 6026 | goto err; |
| 6027 | } |
| 6028 | |
| 6029 | ret = 0; |
| 6030 | down_read(¤t->mm->mmap_sem); |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 6031 | pret = get_user_pages(ubuf, nr_pages, |
| 6032 | FOLL_WRITE | FOLL_LONGTERM, |
| 6033 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6034 | if (pret == nr_pages) { |
| 6035 | /* don't support file backed memory */ |
| 6036 | for (j = 0; j < nr_pages; j++) { |
| 6037 | struct vm_area_struct *vma = vmas[j]; |
| 6038 | |
| 6039 | if (vma->vm_file && |
| 6040 | !is_file_hugepages(vma->vm_file)) { |
| 6041 | ret = -EOPNOTSUPP; |
| 6042 | break; |
| 6043 | } |
| 6044 | } |
| 6045 | } else { |
| 6046 | ret = pret < 0 ? pret : -EFAULT; |
| 6047 | } |
| 6048 | up_read(¤t->mm->mmap_sem); |
| 6049 | if (ret) { |
| 6050 | /* |
| 6051 | * if we did partial map, or found file backed vmas, |
| 6052 | * release any pages we did get |
| 6053 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 6054 | if (pret > 0) |
| 6055 | put_user_pages(pages, pret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6056 | if (ctx->account_mem) |
| 6057 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6058 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6059 | goto err; |
| 6060 | } |
| 6061 | |
| 6062 | off = ubuf & ~PAGE_MASK; |
| 6063 | size = iov.iov_len; |
| 6064 | for (j = 0; j < nr_pages; j++) { |
| 6065 | size_t vec_len; |
| 6066 | |
| 6067 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 6068 | imu->bvec[j].bv_page = pages[j]; |
| 6069 | imu->bvec[j].bv_len = vec_len; |
| 6070 | imu->bvec[j].bv_offset = off; |
| 6071 | off = 0; |
| 6072 | size -= vec_len; |
| 6073 | } |
| 6074 | /* store original address for later verification */ |
| 6075 | imu->ubuf = ubuf; |
| 6076 | imu->len = iov.iov_len; |
| 6077 | imu->nr_bvecs = nr_pages; |
| 6078 | |
| 6079 | ctx->nr_user_bufs++; |
| 6080 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6081 | kvfree(pages); |
| 6082 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6083 | return 0; |
| 6084 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6085 | kvfree(pages); |
| 6086 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6087 | io_sqe_buffer_unregister(ctx); |
| 6088 | return ret; |
| 6089 | } |
| 6090 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6091 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 6092 | { |
| 6093 | __s32 __user *fds = arg; |
| 6094 | int fd; |
| 6095 | |
| 6096 | if (ctx->cq_ev_fd) |
| 6097 | return -EBUSY; |
| 6098 | |
| 6099 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 6100 | return -EFAULT; |
| 6101 | |
| 6102 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 6103 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 6104 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 6105 | ctx->cq_ev_fd = NULL; |
| 6106 | return ret; |
| 6107 | } |
| 6108 | |
| 6109 | return 0; |
| 6110 | } |
| 6111 | |
| 6112 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 6113 | { |
| 6114 | if (ctx->cq_ev_fd) { |
| 6115 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 6116 | ctx->cq_ev_fd = NULL; |
| 6117 | return 0; |
| 6118 | } |
| 6119 | |
| 6120 | return -ENXIO; |
| 6121 | } |
| 6122 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6123 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 6124 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6125 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6126 | if (ctx->sqo_mm) |
| 6127 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6128 | |
| 6129 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6130 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6131 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6132 | io_eventfd_unregister(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6133 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6134 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6135 | if (ctx->ring_sock) { |
| 6136 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6137 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6138 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6139 | #endif |
| 6140 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6141 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6142 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6143 | |
| 6144 | percpu_ref_exit(&ctx->refs); |
| 6145 | if (ctx->account_mem) |
| 6146 | io_unaccount_mem(ctx->user, |
| 6147 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 6148 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6149 | put_cred(ctx->creds); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6150 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 6151 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 6152 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6153 | kfree(ctx); |
| 6154 | } |
| 6155 | |
| 6156 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 6157 | { |
| 6158 | struct io_ring_ctx *ctx = file->private_data; |
| 6159 | __poll_t mask = 0; |
| 6160 | |
| 6161 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 6162 | /* |
| 6163 | * synchronizes with barrier from wq_has_sleeper call in |
| 6164 | * io_commit_cqring |
| 6165 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6166 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6167 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 6168 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6169 | mask |= EPOLLOUT | EPOLLWRNORM; |
yangerkun | daa5de5 | 2019-09-24 20:53:34 +0800 | [diff] [blame] | 6170 | if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6171 | mask |= EPOLLIN | EPOLLRDNORM; |
| 6172 | |
| 6173 | return mask; |
| 6174 | } |
| 6175 | |
| 6176 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 6177 | { |
| 6178 | struct io_ring_ctx *ctx = file->private_data; |
| 6179 | |
| 6180 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 6181 | } |
| 6182 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame^] | 6183 | static int io_remove_personalities(int id, void *p, void *data) |
| 6184 | { |
| 6185 | struct io_ring_ctx *ctx = data; |
| 6186 | const struct cred *cred; |
| 6187 | |
| 6188 | cred = idr_remove(&ctx->personality_idr, id); |
| 6189 | if (cred) |
| 6190 | put_cred(cred); |
| 6191 | return 0; |
| 6192 | } |
| 6193 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6194 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 6195 | { |
| 6196 | mutex_lock(&ctx->uring_lock); |
| 6197 | percpu_ref_kill(&ctx->refs); |
| 6198 | mutex_unlock(&ctx->uring_lock); |
| 6199 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6200 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6201 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6202 | |
| 6203 | if (ctx->io_wq) |
| 6204 | io_wq_cancel_all(ctx->io_wq); |
| 6205 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6206 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 6207 | /* if we failed setting up the ctx, we might not have any rings */ |
| 6208 | if (ctx->rings) |
| 6209 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame^] | 6210 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6211 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6212 | io_ring_ctx_free(ctx); |
| 6213 | } |
| 6214 | |
| 6215 | static int io_uring_release(struct inode *inode, struct file *file) |
| 6216 | { |
| 6217 | struct io_ring_ctx *ctx = file->private_data; |
| 6218 | |
| 6219 | file->private_data = NULL; |
| 6220 | io_ring_ctx_wait_and_kill(ctx); |
| 6221 | return 0; |
| 6222 | } |
| 6223 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6224 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 6225 | struct files_struct *files) |
| 6226 | { |
| 6227 | struct io_kiocb *req; |
| 6228 | DEFINE_WAIT(wait); |
| 6229 | |
| 6230 | while (!list_empty_careful(&ctx->inflight_list)) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6231 | struct io_kiocb *cancel_req = NULL; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6232 | |
| 6233 | spin_lock_irq(&ctx->inflight_lock); |
| 6234 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6235 | if (req->work.files != files) |
| 6236 | continue; |
| 6237 | /* req is being completed, ignore */ |
| 6238 | if (!refcount_inc_not_zero(&req->refs)) |
| 6239 | continue; |
| 6240 | cancel_req = req; |
| 6241 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6242 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6243 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6244 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6245 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6246 | spin_unlock_irq(&ctx->inflight_lock); |
| 6247 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6248 | /* We need to keep going until we don't find a matching req */ |
| 6249 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6250 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 6251 | |
| 6252 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 6253 | io_put_req(cancel_req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6254 | schedule(); |
| 6255 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6256 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6257 | } |
| 6258 | |
| 6259 | static int io_uring_flush(struct file *file, void *data) |
| 6260 | { |
| 6261 | struct io_ring_ctx *ctx = file->private_data; |
| 6262 | |
| 6263 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6264 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
| 6265 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6266 | io_wq_cancel_all(ctx->io_wq); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6267 | } |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6268 | return 0; |
| 6269 | } |
| 6270 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6271 | static void *io_uring_validate_mmap_request(struct file *file, |
| 6272 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6273 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6274 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6275 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6276 | struct page *page; |
| 6277 | void *ptr; |
| 6278 | |
| 6279 | switch (offset) { |
| 6280 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6281 | case IORING_OFF_CQ_RING: |
| 6282 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6283 | break; |
| 6284 | case IORING_OFF_SQES: |
| 6285 | ptr = ctx->sq_sqes; |
| 6286 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6287 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6288 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6289 | } |
| 6290 | |
| 6291 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 6292 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6293 | return ERR_PTR(-EINVAL); |
| 6294 | |
| 6295 | return ptr; |
| 6296 | } |
| 6297 | |
| 6298 | #ifdef CONFIG_MMU |
| 6299 | |
| 6300 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6301 | { |
| 6302 | size_t sz = vma->vm_end - vma->vm_start; |
| 6303 | unsigned long pfn; |
| 6304 | void *ptr; |
| 6305 | |
| 6306 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 6307 | if (IS_ERR(ptr)) |
| 6308 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6309 | |
| 6310 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 6311 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 6312 | } |
| 6313 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6314 | #else /* !CONFIG_MMU */ |
| 6315 | |
| 6316 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6317 | { |
| 6318 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 6319 | } |
| 6320 | |
| 6321 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 6322 | { |
| 6323 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 6324 | } |
| 6325 | |
| 6326 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 6327 | unsigned long addr, unsigned long len, |
| 6328 | unsigned long pgoff, unsigned long flags) |
| 6329 | { |
| 6330 | void *ptr; |
| 6331 | |
| 6332 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 6333 | if (IS_ERR(ptr)) |
| 6334 | return PTR_ERR(ptr); |
| 6335 | |
| 6336 | return (unsigned long) ptr; |
| 6337 | } |
| 6338 | |
| 6339 | #endif /* !CONFIG_MMU */ |
| 6340 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6341 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 6342 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 6343 | size_t, sigsz) |
| 6344 | { |
| 6345 | struct io_ring_ctx *ctx; |
| 6346 | long ret = -EBADF; |
| 6347 | int submitted = 0; |
| 6348 | struct fd f; |
| 6349 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6350 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6351 | return -EINVAL; |
| 6352 | |
| 6353 | f = fdget(fd); |
| 6354 | if (!f.file) |
| 6355 | return -EBADF; |
| 6356 | |
| 6357 | ret = -EOPNOTSUPP; |
| 6358 | if (f.file->f_op != &io_uring_fops) |
| 6359 | goto out_fput; |
| 6360 | |
| 6361 | ret = -ENXIO; |
| 6362 | ctx = f.file->private_data; |
| 6363 | if (!percpu_ref_tryget(&ctx->refs)) |
| 6364 | goto out_fput; |
| 6365 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6366 | /* |
| 6367 | * For SQ polling, the thread will do all submissions and completions. |
| 6368 | * Just return the requested submit count, and wake the thread if |
| 6369 | * we were asked to. |
| 6370 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6371 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6372 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6373 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 6374 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6375 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 6376 | wake_up(&ctx->sqo_wait); |
| 6377 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6378 | } else if (to_submit) { |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6379 | struct mm_struct *cur_mm; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6380 | |
Jens Axboe | 44d2827 | 2020-01-16 19:00:24 -0700 | [diff] [blame] | 6381 | if (current->mm != ctx->sqo_mm || |
| 6382 | current_cred() != ctx->creds) { |
| 6383 | ret = -EPERM; |
| 6384 | goto out; |
| 6385 | } |
| 6386 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6387 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6388 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
| 6389 | cur_mm = ctx->sqo_mm; |
| 6390 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, |
| 6391 | &cur_mm, false); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6392 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6393 | |
| 6394 | if (submitted != to_submit) |
| 6395 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6396 | } |
| 6397 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6398 | unsigned nr_events = 0; |
| 6399 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6400 | min_complete = min(min_complete, ctx->cq_entries); |
| 6401 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6402 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6403 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6404 | } else { |
| 6405 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 6406 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6407 | } |
| 6408 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6409 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 6410 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6411 | out_fput: |
| 6412 | fdput(f); |
| 6413 | return submitted ? submitted : ret; |
| 6414 | } |
| 6415 | |
| 6416 | static const struct file_operations io_uring_fops = { |
| 6417 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6418 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6419 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6420 | #ifndef CONFIG_MMU |
| 6421 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 6422 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 6423 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6424 | .poll = io_uring_poll, |
| 6425 | .fasync = io_uring_fasync, |
| 6426 | }; |
| 6427 | |
| 6428 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 6429 | struct io_uring_params *p) |
| 6430 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6431 | struct io_rings *rings; |
| 6432 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6433 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6434 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 6435 | if (size == SIZE_MAX) |
| 6436 | return -EOVERFLOW; |
| 6437 | |
| 6438 | rings = io_mem_alloc(size); |
| 6439 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6440 | return -ENOMEM; |
| 6441 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6442 | ctx->rings = rings; |
| 6443 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 6444 | rings->sq_ring_mask = p->sq_entries - 1; |
| 6445 | rings->cq_ring_mask = p->cq_entries - 1; |
| 6446 | rings->sq_ring_entries = p->sq_entries; |
| 6447 | rings->cq_ring_entries = p->cq_entries; |
| 6448 | ctx->sq_mask = rings->sq_ring_mask; |
| 6449 | ctx->cq_mask = rings->cq_ring_mask; |
| 6450 | ctx->sq_entries = rings->sq_ring_entries; |
| 6451 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6452 | |
| 6453 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6454 | if (size == SIZE_MAX) { |
| 6455 | io_mem_free(ctx->rings); |
| 6456 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6457 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6458 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6459 | |
| 6460 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6461 | if (!ctx->sq_sqes) { |
| 6462 | io_mem_free(ctx->rings); |
| 6463 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6464 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6465 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6466 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6467 | return 0; |
| 6468 | } |
| 6469 | |
| 6470 | /* |
| 6471 | * Allocate an anonymous fd, this is what constitutes the application |
| 6472 | * visible backing of an io_uring instance. The application mmaps this |
| 6473 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 6474 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 6475 | */ |
| 6476 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 6477 | { |
| 6478 | struct file *file; |
| 6479 | int ret; |
| 6480 | |
| 6481 | #if defined(CONFIG_UNIX) |
| 6482 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 6483 | &ctx->ring_sock); |
| 6484 | if (ret) |
| 6485 | return ret; |
| 6486 | #endif |
| 6487 | |
| 6488 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 6489 | if (ret < 0) |
| 6490 | goto err; |
| 6491 | |
| 6492 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 6493 | O_RDWR | O_CLOEXEC); |
| 6494 | if (IS_ERR(file)) { |
| 6495 | put_unused_fd(ret); |
| 6496 | ret = PTR_ERR(file); |
| 6497 | goto err; |
| 6498 | } |
| 6499 | |
| 6500 | #if defined(CONFIG_UNIX) |
| 6501 | ctx->ring_sock->file = file; |
| 6502 | #endif |
| 6503 | fd_install(ret, file); |
| 6504 | return ret; |
| 6505 | err: |
| 6506 | #if defined(CONFIG_UNIX) |
| 6507 | sock_release(ctx->ring_sock); |
| 6508 | ctx->ring_sock = NULL; |
| 6509 | #endif |
| 6510 | return ret; |
| 6511 | } |
| 6512 | |
| 6513 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 6514 | { |
| 6515 | struct user_struct *user = NULL; |
| 6516 | struct io_ring_ctx *ctx; |
| 6517 | bool account_mem; |
| 6518 | int ret; |
| 6519 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6520 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6521 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6522 | if (entries > IORING_MAX_ENTRIES) { |
| 6523 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6524 | return -EINVAL; |
| 6525 | entries = IORING_MAX_ENTRIES; |
| 6526 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6527 | |
| 6528 | /* |
| 6529 | * Use twice as many entries for the CQ ring. It's possible for the |
| 6530 | * application to drive a higher depth than the size of the SQ ring, |
| 6531 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6532 | * some flexibility in overcommitting a bit. If the application has |
| 6533 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 6534 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6535 | */ |
| 6536 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6537 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 6538 | /* |
| 6539 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 6540 | * to a power-of-two, if it isn't already. We do NOT impose |
| 6541 | * any cq vs sq ring sizing. |
| 6542 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6543 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6544 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6545 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 6546 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6547 | return -EINVAL; |
| 6548 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 6549 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6550 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 6551 | } else { |
| 6552 | p->cq_entries = 2 * p->sq_entries; |
| 6553 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6554 | |
| 6555 | user = get_uid(current_user()); |
| 6556 | account_mem = !capable(CAP_IPC_LOCK); |
| 6557 | |
| 6558 | if (account_mem) { |
| 6559 | ret = io_account_mem(user, |
| 6560 | ring_pages(p->sq_entries, p->cq_entries)); |
| 6561 | if (ret) { |
| 6562 | free_uid(user); |
| 6563 | return ret; |
| 6564 | } |
| 6565 | } |
| 6566 | |
| 6567 | ctx = io_ring_ctx_alloc(p); |
| 6568 | if (!ctx) { |
| 6569 | if (account_mem) |
| 6570 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 6571 | p->cq_entries)); |
| 6572 | free_uid(user); |
| 6573 | return -ENOMEM; |
| 6574 | } |
| 6575 | ctx->compat = in_compat_syscall(); |
| 6576 | ctx->account_mem = account_mem; |
| 6577 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 6578 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6579 | |
| 6580 | ret = io_allocate_scq_urings(ctx, p); |
| 6581 | if (ret) |
| 6582 | goto err; |
| 6583 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6584 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6585 | if (ret) |
| 6586 | goto err; |
| 6587 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6588 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6589 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 6590 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 6591 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 6592 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 6593 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 6594 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 6595 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6596 | |
| 6597 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6598 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 6599 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 6600 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 6601 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 6602 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 6603 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 6604 | |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 6605 | /* |
| 6606 | * Install ring fd as the very last thing, so we don't risk someone |
| 6607 | * having closed it before we finish setup |
| 6608 | */ |
| 6609 | ret = io_uring_get_fd(ctx); |
| 6610 | if (ret < 0) |
| 6611 | goto err; |
| 6612 | |
Jens Axboe | da8c969 | 2019-12-02 18:51:26 -0700 | [diff] [blame] | 6613 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 6614 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
| 6615 | IORING_FEAT_CUR_PERSONALITY; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6616 | 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] | 6617 | return ret; |
| 6618 | err: |
| 6619 | io_ring_ctx_wait_and_kill(ctx); |
| 6620 | return ret; |
| 6621 | } |
| 6622 | |
| 6623 | /* |
| 6624 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 6625 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 6626 | * params structure passed in. |
| 6627 | */ |
| 6628 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 6629 | { |
| 6630 | struct io_uring_params p; |
| 6631 | long ret; |
| 6632 | int i; |
| 6633 | |
| 6634 | if (copy_from_user(&p, params, sizeof(p))) |
| 6635 | return -EFAULT; |
| 6636 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 6637 | if (p.resv[i]) |
| 6638 | return -EINVAL; |
| 6639 | } |
| 6640 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6641 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6642 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 6643 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6644 | return -EINVAL; |
| 6645 | |
| 6646 | ret = io_uring_create(entries, &p); |
| 6647 | if (ret < 0) |
| 6648 | return ret; |
| 6649 | |
| 6650 | if (copy_to_user(params, &p, sizeof(p))) |
| 6651 | return -EFAULT; |
| 6652 | |
| 6653 | return ret; |
| 6654 | } |
| 6655 | |
| 6656 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 6657 | struct io_uring_params __user *, params) |
| 6658 | { |
| 6659 | return io_uring_setup(entries, params); |
| 6660 | } |
| 6661 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 6662 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 6663 | { |
| 6664 | struct io_uring_probe *p; |
| 6665 | size_t size; |
| 6666 | int i, ret; |
| 6667 | |
| 6668 | size = struct_size(p, ops, nr_args); |
| 6669 | if (size == SIZE_MAX) |
| 6670 | return -EOVERFLOW; |
| 6671 | p = kzalloc(size, GFP_KERNEL); |
| 6672 | if (!p) |
| 6673 | return -ENOMEM; |
| 6674 | |
| 6675 | ret = -EFAULT; |
| 6676 | if (copy_from_user(p, arg, size)) |
| 6677 | goto out; |
| 6678 | ret = -EINVAL; |
| 6679 | if (memchr_inv(p, 0, size)) |
| 6680 | goto out; |
| 6681 | |
| 6682 | p->last_op = IORING_OP_LAST - 1; |
| 6683 | if (nr_args > IORING_OP_LAST) |
| 6684 | nr_args = IORING_OP_LAST; |
| 6685 | |
| 6686 | for (i = 0; i < nr_args; i++) { |
| 6687 | p->ops[i].op = i; |
| 6688 | if (!io_op_defs[i].not_supported) |
| 6689 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 6690 | } |
| 6691 | p->ops_len = i; |
| 6692 | |
| 6693 | ret = 0; |
| 6694 | if (copy_to_user(arg, p, size)) |
| 6695 | ret = -EFAULT; |
| 6696 | out: |
| 6697 | kfree(p); |
| 6698 | return ret; |
| 6699 | } |
| 6700 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame^] | 6701 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 6702 | { |
| 6703 | const struct cred *creds = get_current_cred(); |
| 6704 | int id; |
| 6705 | |
| 6706 | id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, |
| 6707 | USHRT_MAX, GFP_KERNEL); |
| 6708 | if (id < 0) |
| 6709 | put_cred(creds); |
| 6710 | return id; |
| 6711 | } |
| 6712 | |
| 6713 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
| 6714 | { |
| 6715 | const struct cred *old_creds; |
| 6716 | |
| 6717 | old_creds = idr_remove(&ctx->personality_idr, id); |
| 6718 | if (old_creds) { |
| 6719 | put_cred(old_creds); |
| 6720 | return 0; |
| 6721 | } |
| 6722 | |
| 6723 | return -EINVAL; |
| 6724 | } |
| 6725 | |
| 6726 | static bool io_register_op_must_quiesce(int op) |
| 6727 | { |
| 6728 | switch (op) { |
| 6729 | case IORING_UNREGISTER_FILES: |
| 6730 | case IORING_REGISTER_FILES_UPDATE: |
| 6731 | case IORING_REGISTER_PROBE: |
| 6732 | case IORING_REGISTER_PERSONALITY: |
| 6733 | case IORING_UNREGISTER_PERSONALITY: |
| 6734 | return false; |
| 6735 | default: |
| 6736 | return true; |
| 6737 | } |
| 6738 | } |
| 6739 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6740 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 6741 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 6742 | __releases(ctx->uring_lock) |
| 6743 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6744 | { |
| 6745 | int ret; |
| 6746 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 6747 | /* |
| 6748 | * We're inside the ring mutex, if the ref is already dying, then |
| 6749 | * someone else killed the ctx or is already going through |
| 6750 | * io_uring_register(). |
| 6751 | */ |
| 6752 | if (percpu_ref_is_dying(&ctx->refs)) |
| 6753 | return -ENXIO; |
| 6754 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame^] | 6755 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6756 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 6757 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6758 | /* |
| 6759 | * Drop uring mutex before waiting for references to exit. If |
| 6760 | * another thread is currently inside io_uring_enter() it might |
| 6761 | * need to grab the uring_lock to make progress. If we hold it |
| 6762 | * here across the drain wait, then we can deadlock. It's safe |
| 6763 | * to drop the mutex here, since no new references will come in |
| 6764 | * after we've killed the percpu ref. |
| 6765 | */ |
| 6766 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6767 | ret = wait_for_completion_interruptible(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6768 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6769 | if (ret) { |
| 6770 | percpu_ref_resurrect(&ctx->refs); |
| 6771 | ret = -EINTR; |
| 6772 | goto out; |
| 6773 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6774 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6775 | |
| 6776 | switch (opcode) { |
| 6777 | case IORING_REGISTER_BUFFERS: |
| 6778 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 6779 | break; |
| 6780 | case IORING_UNREGISTER_BUFFERS: |
| 6781 | ret = -EINVAL; |
| 6782 | if (arg || nr_args) |
| 6783 | break; |
| 6784 | ret = io_sqe_buffer_unregister(ctx); |
| 6785 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6786 | case IORING_REGISTER_FILES: |
| 6787 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 6788 | break; |
| 6789 | case IORING_UNREGISTER_FILES: |
| 6790 | ret = -EINVAL; |
| 6791 | if (arg || nr_args) |
| 6792 | break; |
| 6793 | ret = io_sqe_files_unregister(ctx); |
| 6794 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6795 | case IORING_REGISTER_FILES_UPDATE: |
| 6796 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 6797 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6798 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 6799 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6800 | ret = -EINVAL; |
| 6801 | if (nr_args != 1) |
| 6802 | break; |
| 6803 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 6804 | if (ret) |
| 6805 | break; |
| 6806 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 6807 | ctx->eventfd_async = 1; |
| 6808 | else |
| 6809 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6810 | break; |
| 6811 | case IORING_UNREGISTER_EVENTFD: |
| 6812 | ret = -EINVAL; |
| 6813 | if (arg || nr_args) |
| 6814 | break; |
| 6815 | ret = io_eventfd_unregister(ctx); |
| 6816 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 6817 | case IORING_REGISTER_PROBE: |
| 6818 | ret = -EINVAL; |
| 6819 | if (!arg || nr_args > 256) |
| 6820 | break; |
| 6821 | ret = io_probe(ctx, arg, nr_args); |
| 6822 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame^] | 6823 | case IORING_REGISTER_PERSONALITY: |
| 6824 | ret = -EINVAL; |
| 6825 | if (arg || nr_args) |
| 6826 | break; |
| 6827 | ret = io_register_personality(ctx); |
| 6828 | break; |
| 6829 | case IORING_UNREGISTER_PERSONALITY: |
| 6830 | ret = -EINVAL; |
| 6831 | if (arg) |
| 6832 | break; |
| 6833 | ret = io_unregister_personality(ctx, nr_args); |
| 6834 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6835 | default: |
| 6836 | ret = -EINVAL; |
| 6837 | break; |
| 6838 | } |
| 6839 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame^] | 6840 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6841 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6842 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6843 | out: |
| 6844 | reinit_completion(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6845 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6846 | return ret; |
| 6847 | } |
| 6848 | |
| 6849 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 6850 | void __user *, arg, unsigned int, nr_args) |
| 6851 | { |
| 6852 | struct io_ring_ctx *ctx; |
| 6853 | long ret = -EBADF; |
| 6854 | struct fd f; |
| 6855 | |
| 6856 | f = fdget(fd); |
| 6857 | if (!f.file) |
| 6858 | return -EBADF; |
| 6859 | |
| 6860 | ret = -EOPNOTSUPP; |
| 6861 | if (f.file->f_op != &io_uring_fops) |
| 6862 | goto out_fput; |
| 6863 | |
| 6864 | ctx = f.file->private_data; |
| 6865 | |
| 6866 | mutex_lock(&ctx->uring_lock); |
| 6867 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 6868 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6869 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 6870 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6871 | out_fput: |
| 6872 | fdput(f); |
| 6873 | return ret; |
| 6874 | } |
| 6875 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6876 | static int __init io_uring_init(void) |
| 6877 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6878 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6879 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 6880 | return 0; |
| 6881 | }; |
| 6882 | __initcall(io_uring_init); |