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 | |
| 276 | struct { |
| 277 | unsigned cached_cq_tail; |
| 278 | unsigned cq_entries; |
| 279 | unsigned cq_mask; |
| 280 | atomic_t cq_timeouts; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 281 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 282 | struct wait_queue_head cq_wait; |
| 283 | struct fasync_struct *cq_fasync; |
| 284 | struct eventfd_ctx *cq_ev_fd; |
| 285 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 286 | |
| 287 | struct { |
| 288 | struct mutex uring_lock; |
| 289 | wait_queue_head_t wait; |
| 290 | } ____cacheline_aligned_in_smp; |
| 291 | |
| 292 | struct { |
| 293 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 294 | struct llist_head poll_llist; |
| 295 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 296 | /* |
| 297 | * ->poll_list is protected by the ctx->uring_lock for |
| 298 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 299 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 300 | * manipulate the list, hence no extra locking is needed there. |
| 301 | */ |
| 302 | struct list_head poll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 303 | struct hlist_head *cancel_hash; |
| 304 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 305 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 306 | |
| 307 | spinlock_t inflight_lock; |
| 308 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 309 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 310 | }; |
| 311 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 312 | /* |
| 313 | * First field must be the file pointer in all the |
| 314 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 315 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 316 | struct io_poll_iocb { |
| 317 | struct file *file; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 318 | union { |
| 319 | struct wait_queue_head *head; |
| 320 | u64 addr; |
| 321 | }; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 322 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 323 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 324 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 325 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 326 | }; |
| 327 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 328 | struct io_close { |
| 329 | struct file *file; |
| 330 | struct file *put_file; |
| 331 | int fd; |
| 332 | }; |
| 333 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 334 | struct io_timeout_data { |
| 335 | struct io_kiocb *req; |
| 336 | struct hrtimer timer; |
| 337 | struct timespec64 ts; |
| 338 | enum hrtimer_mode mode; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 339 | u32 seq_offset; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 340 | }; |
| 341 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 342 | struct io_accept { |
| 343 | struct file *file; |
| 344 | struct sockaddr __user *addr; |
| 345 | int __user *addr_len; |
| 346 | int flags; |
| 347 | }; |
| 348 | |
| 349 | struct io_sync { |
| 350 | struct file *file; |
| 351 | loff_t len; |
| 352 | loff_t off; |
| 353 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 354 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 355 | }; |
| 356 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 357 | struct io_cancel { |
| 358 | struct file *file; |
| 359 | u64 addr; |
| 360 | }; |
| 361 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 362 | struct io_timeout { |
| 363 | struct file *file; |
| 364 | u64 addr; |
| 365 | int flags; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 366 | unsigned count; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 367 | }; |
| 368 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 369 | struct io_rw { |
| 370 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 371 | struct kiocb kiocb; |
| 372 | u64 addr; |
| 373 | u64 len; |
| 374 | }; |
| 375 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 376 | struct io_connect { |
| 377 | struct file *file; |
| 378 | struct sockaddr __user *addr; |
| 379 | int addr_len; |
| 380 | }; |
| 381 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 382 | struct io_sr_msg { |
| 383 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 384 | union { |
| 385 | struct user_msghdr __user *msg; |
| 386 | void __user *buf; |
| 387 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 388 | int msg_flags; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 389 | size_t len; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 390 | }; |
| 391 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 392 | struct io_open { |
| 393 | struct file *file; |
| 394 | int dfd; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 395 | union { |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 396 | unsigned mask; |
| 397 | }; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 398 | struct filename *filename; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 399 | struct statx __user *buffer; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 400 | struct open_how how; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 401 | }; |
| 402 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 403 | struct io_files_update { |
| 404 | struct file *file; |
| 405 | u64 arg; |
| 406 | u32 nr_args; |
| 407 | u32 offset; |
| 408 | }; |
| 409 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 410 | struct io_fadvise { |
| 411 | struct file *file; |
| 412 | u64 offset; |
| 413 | u32 len; |
| 414 | u32 advice; |
| 415 | }; |
| 416 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 417 | struct io_madvise { |
| 418 | struct file *file; |
| 419 | u64 addr; |
| 420 | u32 len; |
| 421 | u32 advice; |
| 422 | }; |
| 423 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 424 | struct io_async_connect { |
| 425 | struct sockaddr_storage address; |
| 426 | }; |
| 427 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 428 | struct io_async_msghdr { |
| 429 | struct iovec fast_iov[UIO_FASTIOV]; |
| 430 | struct iovec *iov; |
| 431 | struct sockaddr __user *uaddr; |
| 432 | struct msghdr msg; |
| 433 | }; |
| 434 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 435 | struct io_async_rw { |
| 436 | struct iovec fast_iov[UIO_FASTIOV]; |
| 437 | struct iovec *iov; |
| 438 | ssize_t nr_segs; |
| 439 | ssize_t size; |
| 440 | }; |
| 441 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 442 | struct io_async_open { |
| 443 | struct filename *filename; |
| 444 | }; |
| 445 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 446 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 447 | union { |
| 448 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 449 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 450 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 451 | struct io_timeout_data timeout; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 452 | struct io_async_open open; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 453 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 454 | }; |
| 455 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 456 | enum { |
| 457 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 458 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 459 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 460 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 461 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
| 462 | |
| 463 | REQ_F_LINK_NEXT_BIT, |
| 464 | REQ_F_FAIL_LINK_BIT, |
| 465 | REQ_F_INFLIGHT_BIT, |
| 466 | REQ_F_CUR_POS_BIT, |
| 467 | REQ_F_NOWAIT_BIT, |
| 468 | REQ_F_IOPOLL_COMPLETED_BIT, |
| 469 | REQ_F_LINK_TIMEOUT_BIT, |
| 470 | REQ_F_TIMEOUT_BIT, |
| 471 | REQ_F_ISREG_BIT, |
| 472 | REQ_F_MUST_PUNT_BIT, |
| 473 | REQ_F_TIMEOUT_NOSEQ_BIT, |
| 474 | REQ_F_COMP_LOCKED_BIT, |
| 475 | }; |
| 476 | |
| 477 | enum { |
| 478 | /* ctx owns file */ |
| 479 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 480 | /* drain existing IO first */ |
| 481 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 482 | /* linked sqes */ |
| 483 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 484 | /* doesn't sever on completion < 0 */ |
| 485 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 486 | /* IOSQE_ASYNC */ |
| 487 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
| 488 | |
| 489 | /* already grabbed next link */ |
| 490 | REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT), |
| 491 | /* fail rest of links */ |
| 492 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| 493 | /* on inflight list */ |
| 494 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 495 | /* read/write uses file position */ |
| 496 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 497 | /* must not punt to workers */ |
| 498 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
| 499 | /* polled IO has completed */ |
| 500 | REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT), |
| 501 | /* has linked timeout */ |
| 502 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
| 503 | /* timeout request */ |
| 504 | REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT), |
| 505 | /* regular file */ |
| 506 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
| 507 | /* must be punted even for NONBLOCK */ |
| 508 | REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT), |
| 509 | /* no timeout sequence */ |
| 510 | REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT), |
| 511 | /* completion under lock */ |
| 512 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), |
| 513 | }; |
| 514 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 515 | /* |
| 516 | * NOTE! Each of the iocb union members has the file pointer |
| 517 | * as the first entry in their struct definition. So you can |
| 518 | * access the file pointer through any of the sub-structs, |
| 519 | * or directly as just 'ki_filp' in this struct. |
| 520 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 521 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 522 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 523 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 524 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 525 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 526 | struct io_accept accept; |
| 527 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 528 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 529 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 530 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 531 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 532 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 533 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 534 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 535 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 536 | struct io_madvise madvise; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 537 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 538 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 539 | struct io_async_ctx *io; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 540 | /* |
| 541 | * llist_node is only used for poll deferred completions |
| 542 | */ |
| 543 | struct llist_node llist_node; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 544 | bool has_user; |
| 545 | bool in_async; |
| 546 | bool needs_fixed_file; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 547 | u8 opcode; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 548 | |
| 549 | struct io_ring_ctx *ctx; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 550 | union { |
| 551 | struct list_head list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 552 | struct hlist_node hash_node; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 553 | }; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 554 | struct list_head link_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 555 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 556 | refcount_t refs; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 557 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 558 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 559 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 560 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 561 | struct list_head inflight_entry; |
| 562 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 563 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 564 | }; |
| 565 | |
| 566 | #define IO_PLUG_THRESHOLD 2 |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 567 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 568 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 569 | struct io_submit_state { |
| 570 | struct blk_plug plug; |
| 571 | |
| 572 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 573 | * io_kiocb alloc cache |
| 574 | */ |
| 575 | void *reqs[IO_IOPOLL_BATCH]; |
| 576 | unsigned int free_reqs; |
| 577 | unsigned int cur_req; |
| 578 | |
| 579 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 580 | * File reference cache |
| 581 | */ |
| 582 | struct file *file; |
| 583 | unsigned int fd; |
| 584 | unsigned int has_refs; |
| 585 | unsigned int used_refs; |
| 586 | unsigned int ios_left; |
| 587 | }; |
| 588 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 589 | struct io_op_def { |
| 590 | /* needs req->io allocated for deferral/async */ |
| 591 | unsigned async_ctx : 1; |
| 592 | /* needs current->mm setup, does mm access */ |
| 593 | unsigned needs_mm : 1; |
| 594 | /* needs req->file assigned */ |
| 595 | unsigned needs_file : 1; |
| 596 | /* needs req->file assigned IFF fd is >= 0 */ |
| 597 | unsigned fd_non_neg : 1; |
| 598 | /* hash wq insertion if file is a regular file */ |
| 599 | unsigned hash_reg_file : 1; |
| 600 | /* unbound wq insertion if file is a non-regular file */ |
| 601 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 602 | /* opcode is not supported by this kernel */ |
| 603 | unsigned not_supported : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 604 | }; |
| 605 | |
| 606 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 607 | [IORING_OP_NOP] = {}, |
| 608 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 609 | .async_ctx = 1, |
| 610 | .needs_mm = 1, |
| 611 | .needs_file = 1, |
| 612 | .unbound_nonreg_file = 1, |
| 613 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 614 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 615 | .async_ctx = 1, |
| 616 | .needs_mm = 1, |
| 617 | .needs_file = 1, |
| 618 | .hash_reg_file = 1, |
| 619 | .unbound_nonreg_file = 1, |
| 620 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 621 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 622 | .needs_file = 1, |
| 623 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 624 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 625 | .needs_file = 1, |
| 626 | .unbound_nonreg_file = 1, |
| 627 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 628 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 629 | .needs_file = 1, |
| 630 | .hash_reg_file = 1, |
| 631 | .unbound_nonreg_file = 1, |
| 632 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 633 | [IORING_OP_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 634 | .needs_file = 1, |
| 635 | .unbound_nonreg_file = 1, |
| 636 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 637 | [IORING_OP_POLL_REMOVE] = {}, |
| 638 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 639 | .needs_file = 1, |
| 640 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 641 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 642 | .async_ctx = 1, |
| 643 | .needs_mm = 1, |
| 644 | .needs_file = 1, |
| 645 | .unbound_nonreg_file = 1, |
| 646 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 647 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 648 | .async_ctx = 1, |
| 649 | .needs_mm = 1, |
| 650 | .needs_file = 1, |
| 651 | .unbound_nonreg_file = 1, |
| 652 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 653 | [IORING_OP_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 654 | .async_ctx = 1, |
| 655 | .needs_mm = 1, |
| 656 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 657 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
| 658 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 659 | .needs_mm = 1, |
| 660 | .needs_file = 1, |
| 661 | .unbound_nonreg_file = 1, |
| 662 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 663 | [IORING_OP_ASYNC_CANCEL] = {}, |
| 664 | [IORING_OP_LINK_TIMEOUT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 665 | .async_ctx = 1, |
| 666 | .needs_mm = 1, |
| 667 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 668 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 669 | .async_ctx = 1, |
| 670 | .needs_mm = 1, |
| 671 | .needs_file = 1, |
| 672 | .unbound_nonreg_file = 1, |
| 673 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 674 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 675 | .needs_file = 1, |
| 676 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 677 | [IORING_OP_OPENAT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 678 | .needs_file = 1, |
| 679 | .fd_non_neg = 1, |
| 680 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 681 | [IORING_OP_CLOSE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 682 | .needs_file = 1, |
| 683 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 684 | [IORING_OP_FILES_UPDATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 685 | .needs_mm = 1, |
| 686 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 687 | [IORING_OP_STATX] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 688 | .needs_mm = 1, |
| 689 | .needs_file = 1, |
| 690 | .fd_non_neg = 1, |
| 691 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 692 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 693 | .needs_mm = 1, |
| 694 | .needs_file = 1, |
| 695 | .unbound_nonreg_file = 1, |
| 696 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 697 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 698 | .needs_mm = 1, |
| 699 | .needs_file = 1, |
| 700 | .unbound_nonreg_file = 1, |
| 701 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 702 | [IORING_OP_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 703 | .needs_file = 1, |
| 704 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 705 | [IORING_OP_MADVISE] = { |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 706 | .needs_mm = 1, |
| 707 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 708 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 709 | .needs_mm = 1, |
| 710 | .needs_file = 1, |
| 711 | .unbound_nonreg_file = 1, |
| 712 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 713 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 714 | .needs_mm = 1, |
| 715 | .needs_file = 1, |
| 716 | .unbound_nonreg_file = 1, |
| 717 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 718 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 719 | .needs_file = 1, |
| 720 | .fd_non_neg = 1, |
| 721 | }, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 722 | }; |
| 723 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 724 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 725 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 726 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 727 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 728 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 729 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 730 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 731 | struct io_uring_files_update *ip, |
| 732 | unsigned nr_args); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 733 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 734 | static struct kmem_cache *req_cachep; |
| 735 | |
| 736 | static const struct file_operations io_uring_fops; |
| 737 | |
| 738 | struct sock *io_uring_get_socket(struct file *file) |
| 739 | { |
| 740 | #if defined(CONFIG_UNIX) |
| 741 | if (file->f_op == &io_uring_fops) { |
| 742 | struct io_ring_ctx *ctx = file->private_data; |
| 743 | |
| 744 | return ctx->ring_sock->sk; |
| 745 | } |
| 746 | #endif |
| 747 | return NULL; |
| 748 | } |
| 749 | EXPORT_SYMBOL(io_uring_get_socket); |
| 750 | |
| 751 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 752 | { |
| 753 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 754 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 755 | complete(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 759 | { |
| 760 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 761 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 762 | |
| 763 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 764 | if (!ctx) |
| 765 | return NULL; |
| 766 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 767 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 768 | if (!ctx->fallback_req) |
| 769 | goto err; |
| 770 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 771 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
| 772 | if (!ctx->completions) |
| 773 | goto err; |
| 774 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 775 | /* |
| 776 | * Use 5 bits less than the max cq entries, that should give us around |
| 777 | * 32 entries per hash list if totally full and uniformly spread. |
| 778 | */ |
| 779 | hash_bits = ilog2(p->cq_entries); |
| 780 | hash_bits -= 5; |
| 781 | if (hash_bits <= 0) |
| 782 | hash_bits = 1; |
| 783 | ctx->cancel_hash_bits = hash_bits; |
| 784 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 785 | GFP_KERNEL); |
| 786 | if (!ctx->cancel_hash) |
| 787 | goto err; |
| 788 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 789 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 790 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 791 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 792 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 793 | |
| 794 | ctx->flags = p->flags; |
| 795 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 796 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 797 | init_completion(&ctx->completions[0]); |
| 798 | init_completion(&ctx->completions[1]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 799 | mutex_init(&ctx->uring_lock); |
| 800 | init_waitqueue_head(&ctx->wait); |
| 801 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 802 | init_llist_head(&ctx->poll_llist); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 803 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 804 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 805 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 806 | init_waitqueue_head(&ctx->inflight_wait); |
| 807 | spin_lock_init(&ctx->inflight_lock); |
| 808 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 809 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 810 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 811 | if (ctx->fallback_req) |
| 812 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 813 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 814 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 815 | kfree(ctx); |
| 816 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 819 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 820 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 821 | struct io_ring_ctx *ctx = req->ctx; |
| 822 | |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 823 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 824 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 825 | } |
| 826 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 827 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 828 | { |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 829 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 830 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 831 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 832 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | 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] | 836 | { |
| 837 | struct io_kiocb *req; |
| 838 | |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 839 | 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] | 840 | if (req && !req_need_defer(req)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 841 | list_del_init(&req->list); |
| 842 | return req; |
| 843 | } |
| 844 | |
| 845 | return NULL; |
| 846 | } |
| 847 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 848 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 849 | { |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 850 | struct io_kiocb *req; |
| 851 | |
| 852 | 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] | 853 | if (req) { |
| 854 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 855 | return NULL; |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 856 | if (!__req_need_defer(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 857 | list_del_init(&req->list); |
| 858 | return req; |
| 859 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | return NULL; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 863 | } |
| 864 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 865 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 866 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 867 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 868 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 869 | /* order cqe stores with ring update */ |
| 870 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
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 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 873 | wake_up_interruptible(&ctx->cq_wait); |
| 874 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 878 | static inline bool io_prep_async_work(struct io_kiocb *req, |
| 879 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 880 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 881 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 882 | bool do_hashed = false; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 883 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 884 | if (req->flags & REQ_F_ISREG) { |
| 885 | if (def->hash_reg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 886 | do_hashed = true; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 887 | } else { |
| 888 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 889 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 890 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 891 | if (def->needs_mm) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 892 | req->work.flags |= IO_WQ_WORK_NEEDS_USER; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 893 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 894 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 895 | return do_hashed; |
| 896 | } |
| 897 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 898 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 899 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 900 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 901 | struct io_kiocb *link; |
| 902 | bool do_hashed; |
| 903 | |
| 904 | do_hashed = io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 905 | |
| 906 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, |
| 907 | req->flags); |
| 908 | if (!do_hashed) { |
| 909 | io_wq_enqueue(ctx->io_wq, &req->work); |
| 910 | } else { |
| 911 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, |
| 912 | file_inode(req->file)); |
| 913 | } |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 914 | |
| 915 | if (link) |
| 916 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 917 | } |
| 918 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 919 | static void io_kill_timeout(struct io_kiocb *req) |
| 920 | { |
| 921 | int ret; |
| 922 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 923 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 924 | if (ret != -1) { |
| 925 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 926 | list_del_init(&req->list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 927 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 928 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | |
| 932 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 933 | { |
| 934 | struct io_kiocb *req, *tmp; |
| 935 | |
| 936 | spin_lock_irq(&ctx->completion_lock); |
| 937 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 938 | io_kill_timeout(req); |
| 939 | spin_unlock_irq(&ctx->completion_lock); |
| 940 | } |
| 941 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 942 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 943 | { |
| 944 | struct io_kiocb *req; |
| 945 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 946 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 947 | io_kill_timeout(req); |
| 948 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 949 | __io_commit_cqring(ctx); |
| 950 | |
Pavel Begunkov | 8798789 | 2020-01-18 01:22:30 +0300 | [diff] [blame] | 951 | while ((req = io_get_deferred_req(ctx)) != NULL) |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 952 | io_queue_async_work(req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 953 | } |
| 954 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 955 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 956 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 957 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 958 | unsigned tail; |
| 959 | |
| 960 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 961 | /* |
| 962 | * writes to the cq entry need to come after reading head; the |
| 963 | * control dependency is enough as we're using WRITE_ONCE to |
| 964 | * fill the cq entry |
| 965 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 966 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 967 | return NULL; |
| 968 | |
| 969 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 970 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 973 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 974 | { |
| 975 | if (!ctx->eventfd_async) |
| 976 | return true; |
| 977 | return io_wq_current_is_worker() || in_interrupt(); |
| 978 | } |
| 979 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 980 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| 981 | { |
| 982 | if (waitqueue_active(&ctx->wait)) |
| 983 | wake_up(&ctx->wait); |
| 984 | if (waitqueue_active(&ctx->sqo_wait)) |
| 985 | wake_up(&ctx->sqo_wait); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 986 | if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 987 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 988 | } |
| 989 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 990 | /* Returns true if there are no backlogged entries after the flush */ |
| 991 | 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] | 992 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 993 | struct io_rings *rings = ctx->rings; |
| 994 | struct io_uring_cqe *cqe; |
| 995 | struct io_kiocb *req; |
| 996 | unsigned long flags; |
| 997 | LIST_HEAD(list); |
| 998 | |
| 999 | if (!force) { |
| 1000 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1001 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1002 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 1003 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1004 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1008 | |
| 1009 | /* if force is set, the ring is going away. always drop after that */ |
| 1010 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1011 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1012 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1013 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1014 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1015 | cqe = io_get_cqring(ctx); |
| 1016 | if (!cqe && !force) |
| 1017 | break; |
| 1018 | |
| 1019 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 1020 | list); |
| 1021 | list_move(&req->list, &list); |
| 1022 | if (cqe) { |
| 1023 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1024 | WRITE_ONCE(cqe->res, req->result); |
| 1025 | WRITE_ONCE(cqe->flags, 0); |
| 1026 | } else { |
| 1027 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1028 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | io_commit_cqring(ctx); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1033 | if (cqe) { |
| 1034 | clear_bit(0, &ctx->sq_check_overflow); |
| 1035 | clear_bit(0, &ctx->cq_check_overflow); |
| 1036 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1037 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1038 | io_cqring_ev_posted(ctx); |
| 1039 | |
| 1040 | while (!list_empty(&list)) { |
| 1041 | req = list_first_entry(&list, struct io_kiocb, list); |
| 1042 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1043 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1044 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1045 | |
| 1046 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1049 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1050 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1051 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1052 | struct io_uring_cqe *cqe; |
| 1053 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1054 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1055 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1056 | /* |
| 1057 | * If we can't get a cq entry, userspace overflowed the |
| 1058 | * submission (by quite a lot). Increment the overflow count in |
| 1059 | * the ring. |
| 1060 | */ |
| 1061 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1062 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1063 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1064 | WRITE_ONCE(cqe->res, res); |
| 1065 | WRITE_ONCE(cqe->flags, 0); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1066 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1067 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1068 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1069 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1070 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1071 | set_bit(0, &ctx->sq_check_overflow); |
| 1072 | set_bit(0, &ctx->cq_check_overflow); |
| 1073 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1074 | refcount_inc(&req->refs); |
| 1075 | req->result = res; |
| 1076 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1080 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1081 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1082 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1083 | unsigned long flags; |
| 1084 | |
| 1085 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1086 | io_cqring_fill_event(req, res); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1087 | io_commit_cqring(ctx); |
| 1088 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1089 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1090 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1093 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1094 | { |
| 1095 | return req == (struct io_kiocb *) |
| 1096 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1097 | } |
| 1098 | |
| 1099 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1100 | { |
| 1101 | struct io_kiocb *req; |
| 1102 | |
| 1103 | req = ctx->fallback_req; |
| 1104 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) |
| 1105 | return req; |
| 1106 | |
| 1107 | return NULL; |
| 1108 | } |
| 1109 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1110 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 1111 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1112 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1113 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1114 | struct io_kiocb *req; |
| 1115 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1116 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1117 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1118 | if (unlikely(!req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1119 | goto fallback; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1120 | } else if (!state->free_reqs) { |
| 1121 | size_t sz; |
| 1122 | int ret; |
| 1123 | |
| 1124 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1125 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1126 | |
| 1127 | /* |
| 1128 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1129 | * retry single alloc to be on the safe side. |
| 1130 | */ |
| 1131 | if (unlikely(ret <= 0)) { |
| 1132 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1133 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1134 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1135 | ret = 1; |
| 1136 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1137 | state->free_reqs = ret - 1; |
| 1138 | state->cur_req = 1; |
| 1139 | req = state->reqs[0]; |
| 1140 | } else { |
| 1141 | req = state->reqs[state->cur_req]; |
| 1142 | state->free_reqs--; |
| 1143 | state->cur_req++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1146 | got_it: |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 1147 | req->io = NULL; |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 1148 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1149 | req->ctx = ctx; |
| 1150 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1151 | /* one is dropped after submission, the other at completion */ |
| 1152 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1153 | req->result = 0; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1154 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1155 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1156 | fallback: |
| 1157 | req = io_get_fallback_req(ctx); |
| 1158 | if (req) |
| 1159 | goto got_it; |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 1160 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1161 | return NULL; |
| 1162 | } |
| 1163 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1164 | static void __io_req_do_free(struct io_kiocb *req) |
| 1165 | { |
| 1166 | if (likely(!io_is_fallback_req(req))) |
| 1167 | kmem_cache_free(req_cachep, req); |
| 1168 | else |
| 1169 | clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req); |
| 1170 | } |
| 1171 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1172 | static void __io_req_aux_free(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1173 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1174 | struct io_ring_ctx *ctx = req->ctx; |
| 1175 | |
YueHaibing | 96fd84d | 2020-01-07 22:22:44 +0800 | [diff] [blame] | 1176 | kfree(req->io); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1177 | if (req->file) { |
| 1178 | if (req->flags & REQ_F_FIXED_FILE) |
| 1179 | percpu_ref_put(&ctx->file_data->refs); |
| 1180 | else |
| 1181 | fput(req->file); |
| 1182 | } |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | static void __io_free_req(struct io_kiocb *req) |
| 1186 | { |
| 1187 | __io_req_aux_free(req); |
| 1188 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1189 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1190 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1191 | unsigned long flags; |
| 1192 | |
| 1193 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1194 | list_del(&req->inflight_entry); |
| 1195 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1196 | wake_up(&ctx->inflight_wait); |
| 1197 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1198 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1199 | |
| 1200 | percpu_ref_put(&req->ctx->refs); |
| 1201 | __io_req_do_free(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1202 | } |
| 1203 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1204 | struct req_batch { |
| 1205 | void *reqs[IO_IOPOLL_BATCH]; |
| 1206 | int to_free; |
| 1207 | int need_iter; |
| 1208 | }; |
| 1209 | |
| 1210 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) |
| 1211 | { |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1212 | int fixed_refs = rb->to_free; |
| 1213 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1214 | if (!rb->to_free) |
| 1215 | return; |
| 1216 | if (rb->need_iter) { |
| 1217 | int i, inflight = 0; |
| 1218 | unsigned long flags; |
| 1219 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1220 | fixed_refs = 0; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1221 | for (i = 0; i < rb->to_free; i++) { |
| 1222 | struct io_kiocb *req = rb->reqs[i]; |
| 1223 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1224 | if (req->flags & REQ_F_FIXED_FILE) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1225 | req->file = NULL; |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1226 | fixed_refs++; |
| 1227 | } |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1228 | if (req->flags & REQ_F_INFLIGHT) |
| 1229 | inflight++; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1230 | __io_req_aux_free(req); |
| 1231 | } |
| 1232 | if (!inflight) |
| 1233 | goto do_free; |
| 1234 | |
| 1235 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1236 | for (i = 0; i < rb->to_free; i++) { |
| 1237 | struct io_kiocb *req = rb->reqs[i]; |
| 1238 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1239 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1240 | list_del(&req->inflight_entry); |
| 1241 | if (!--inflight) |
| 1242 | break; |
| 1243 | } |
| 1244 | } |
| 1245 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1246 | |
| 1247 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1248 | wake_up(&ctx->inflight_wait); |
| 1249 | } |
| 1250 | do_free: |
| 1251 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1252 | if (fixed_refs) |
| 1253 | percpu_ref_put_many(&ctx->file_data->refs, fixed_refs); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1254 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1255 | rb->to_free = rb->need_iter = 0; |
| 1256 | } |
| 1257 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1258 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1259 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1260 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1261 | int ret; |
| 1262 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1263 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1264 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1265 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1266 | io_commit_cqring(ctx); |
| 1267 | req->flags &= ~REQ_F_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1268 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1269 | return true; |
| 1270 | } |
| 1271 | |
| 1272 | return false; |
| 1273 | } |
| 1274 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1275 | 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] | 1276 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1277 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1278 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1279 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1280 | /* Already got next link */ |
| 1281 | if (req->flags & REQ_F_LINK_NEXT) |
| 1282 | return; |
| 1283 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1284 | /* |
| 1285 | * The list should never be empty when we are called here. But could |
| 1286 | * potentially happen if the chain is messed up, check to be on the |
| 1287 | * safe side. |
| 1288 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1289 | while (!list_empty(&req->link_list)) { |
| 1290 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 1291 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1292 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1293 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 1294 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 1295 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1296 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1297 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1298 | continue; |
| 1299 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1300 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1301 | list_del_init(&req->link_list); |
| 1302 | if (!list_empty(&nxt->link_list)) |
| 1303 | nxt->flags |= REQ_F_LINK; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 1304 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1305 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1306 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1307 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1308 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1309 | if (wake_ev) |
| 1310 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | /* |
| 1314 | * Called if REQ_F_LINK is set, and we fail the head request |
| 1315 | */ |
| 1316 | static void io_fail_links(struct io_kiocb *req) |
| 1317 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1318 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1319 | unsigned long flags; |
| 1320 | |
| 1321 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1322 | |
| 1323 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1324 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1325 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1326 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1327 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1328 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1329 | |
| 1330 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1331 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1332 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1333 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1334 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1335 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1336 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1337 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1338 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1339 | |
| 1340 | io_commit_cqring(ctx); |
| 1341 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1342 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1343 | } |
| 1344 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1345 | 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] | 1346 | { |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1347 | if (likely(!(req->flags & REQ_F_LINK))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1348 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1349 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1350 | /* |
| 1351 | * If LINK is set, we have dependent requests in this chain. If we |
| 1352 | * didn't fail this request, queue the first one up, moving any other |
| 1353 | * dependencies to the next request. In case of failure, fail the rest |
| 1354 | * of the chain. |
| 1355 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1356 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1357 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1358 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1359 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1360 | struct io_ring_ctx *ctx = req->ctx; |
| 1361 | unsigned long flags; |
| 1362 | |
| 1363 | /* |
| 1364 | * If this is a timeout link, we could be racing with the |
| 1365 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1366 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1367 | */ |
| 1368 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1369 | io_req_link_next(req, nxt); |
| 1370 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1371 | } else { |
| 1372 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1373 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1374 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1375 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1376 | static void io_free_req(struct io_kiocb *req) |
| 1377 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1378 | struct io_kiocb *nxt = NULL; |
| 1379 | |
| 1380 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1381 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1382 | |
| 1383 | if (nxt) |
| 1384 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1385 | } |
| 1386 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1387 | /* |
| 1388 | * Drop reference to request, return next in chain (if there is one) if this |
| 1389 | * was the last reference to this request. |
| 1390 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1391 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1392 | 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] | 1393 | { |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1394 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1395 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1396 | if (refcount_dec_and_test(&req->refs)) |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1397 | __io_free_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1400 | static void io_put_req(struct io_kiocb *req) |
| 1401 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1402 | if (refcount_dec_and_test(&req->refs)) |
| 1403 | io_free_req(req); |
| 1404 | } |
| 1405 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1406 | /* |
| 1407 | * Must only be used if we don't need to care about links, usually from |
| 1408 | * within the completion handling itself. |
| 1409 | */ |
| 1410 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1411 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1412 | /* drop both submit and complete references */ |
| 1413 | if (refcount_sub_and_test(2, &req->refs)) |
| 1414 | __io_free_req(req); |
| 1415 | } |
| 1416 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1417 | static void io_double_put_req(struct io_kiocb *req) |
| 1418 | { |
| 1419 | /* drop both submit and complete references */ |
| 1420 | if (refcount_sub_and_test(2, &req->refs)) |
| 1421 | io_free_req(req); |
| 1422 | } |
| 1423 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1424 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1425 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1426 | struct io_rings *rings = ctx->rings; |
| 1427 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1428 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1429 | /* |
| 1430 | * noflush == true is from the waitqueue handler, just ensure |
| 1431 | * we wake up the task, and the next invocation will flush the |
| 1432 | * entries. We cannot safely to it from here. |
| 1433 | */ |
| 1434 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1435 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1436 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1437 | io_cqring_overflow_flush(ctx, false); |
| 1438 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1439 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1440 | /* See comment at the top of this file */ |
| 1441 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1442 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1443 | } |
| 1444 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1445 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1446 | { |
| 1447 | struct io_rings *rings = ctx->rings; |
| 1448 | |
| 1449 | /* make sure SQ entry isn't read before tail */ |
| 1450 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1451 | } |
| 1452 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1453 | 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] | 1454 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1455 | if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req)) |
| 1456 | return false; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1457 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1458 | if (!(req->flags & REQ_F_FIXED_FILE) || req->io) |
| 1459 | rb->need_iter++; |
| 1460 | |
| 1461 | rb->reqs[rb->to_free++] = req; |
| 1462 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1463 | io_free_req_many(req->ctx, rb); |
| 1464 | return true; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1467 | /* |
| 1468 | * Find and free completed poll iocbs |
| 1469 | */ |
| 1470 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1471 | struct list_head *done) |
| 1472 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1473 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1474 | struct io_kiocb *req; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1475 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1476 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1477 | while (!list_empty(done)) { |
| 1478 | req = list_first_entry(done, struct io_kiocb, list); |
| 1479 | list_del(&req->list); |
| 1480 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1481 | io_cqring_fill_event(req, req->result); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1482 | (*nr_events)++; |
| 1483 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1484 | if (refcount_dec_and_test(&req->refs) && |
| 1485 | !io_req_multi_free(&rb, req)) |
| 1486 | io_free_req(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1487 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1488 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1489 | io_commit_cqring(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1490 | io_free_req_many(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1494 | long min) |
| 1495 | { |
| 1496 | struct io_kiocb *req, *tmp; |
| 1497 | LIST_HEAD(done); |
| 1498 | bool spin; |
| 1499 | int ret; |
| 1500 | |
| 1501 | /* |
| 1502 | * Only spin for completions if we don't have multiple devices hanging |
| 1503 | * off our complete list, and we're under the requested amount. |
| 1504 | */ |
| 1505 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1506 | |
| 1507 | ret = 0; |
| 1508 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1509 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1510 | |
| 1511 | /* |
| 1512 | * Move completed entries to our local list. If we find a |
| 1513 | * request that requires polling, break out and complete |
| 1514 | * the done list first, if we have entries there. |
| 1515 | */ |
| 1516 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 1517 | list_move_tail(&req->list, &done); |
| 1518 | continue; |
| 1519 | } |
| 1520 | if (!list_empty(&done)) |
| 1521 | break; |
| 1522 | |
| 1523 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1524 | if (ret < 0) |
| 1525 | break; |
| 1526 | |
| 1527 | if (ret && spin) |
| 1528 | spin = false; |
| 1529 | ret = 0; |
| 1530 | } |
| 1531 | |
| 1532 | if (!list_empty(&done)) |
| 1533 | io_iopoll_complete(ctx, nr_events, &done); |
| 1534 | |
| 1535 | return ret; |
| 1536 | } |
| 1537 | |
| 1538 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 1539 | * 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] | 1540 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1541 | * as a non-spinning completion check. |
| 1542 | */ |
| 1543 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1544 | long min) |
| 1545 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1546 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1547 | int ret; |
| 1548 | |
| 1549 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1550 | if (ret < 0) |
| 1551 | return ret; |
| 1552 | if (!min || *nr_events >= min) |
| 1553 | return 0; |
| 1554 | } |
| 1555 | |
| 1556 | return 1; |
| 1557 | } |
| 1558 | |
| 1559 | /* |
| 1560 | * We can't just wait for polled events to come to us, we have to actively |
| 1561 | * find and complete them. |
| 1562 | */ |
| 1563 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1564 | { |
| 1565 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1566 | return; |
| 1567 | |
| 1568 | mutex_lock(&ctx->uring_lock); |
| 1569 | while (!list_empty(&ctx->poll_list)) { |
| 1570 | unsigned int nr_events = 0; |
| 1571 | |
| 1572 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1573 | |
| 1574 | /* |
| 1575 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1576 | * in this case we need to ensure that we reap all events. |
| 1577 | */ |
| 1578 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1579 | } |
| 1580 | mutex_unlock(&ctx->uring_lock); |
| 1581 | } |
| 1582 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1583 | static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1584 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1585 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1586 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1587 | |
| 1588 | do { |
| 1589 | int tmin = 0; |
| 1590 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1591 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1592 | * Don't enter poll loop if we already have events pending. |
| 1593 | * If we do, we can potentially be spinning for commands that |
| 1594 | * already triggered a CQE (eg in error). |
| 1595 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1596 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1597 | break; |
| 1598 | |
| 1599 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1600 | * If a submit got punted to a workqueue, we can have the |
| 1601 | * application entering polling for a command before it gets |
| 1602 | * issued. That app will hold the uring_lock for the duration |
| 1603 | * of the poll right here, so we need to take a breather every |
| 1604 | * now and then to ensure that the issue has a chance to add |
| 1605 | * the poll to the issued list. Otherwise we can spin here |
| 1606 | * forever, while the workqueue is stuck trying to acquire the |
| 1607 | * very same mutex. |
| 1608 | */ |
| 1609 | if (!(++iters & 7)) { |
| 1610 | mutex_unlock(&ctx->uring_lock); |
| 1611 | mutex_lock(&ctx->uring_lock); |
| 1612 | } |
| 1613 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1614 | if (*nr_events < min) |
| 1615 | tmin = min - *nr_events; |
| 1616 | |
| 1617 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1618 | if (ret <= 0) |
| 1619 | break; |
| 1620 | ret = 0; |
| 1621 | } while (min && !*nr_events && !need_resched()); |
| 1622 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1623 | return ret; |
| 1624 | } |
| 1625 | |
| 1626 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1627 | long min) |
| 1628 | { |
| 1629 | int ret; |
| 1630 | |
| 1631 | /* |
| 1632 | * We disallow the app entering submit/complete with polling, but we |
| 1633 | * still need to lock the ring to prevent racing with polled issue |
| 1634 | * that got punted to a workqueue. |
| 1635 | */ |
| 1636 | mutex_lock(&ctx->uring_lock); |
| 1637 | ret = __io_iopoll_check(ctx, nr_events, min); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1638 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1639 | return ret; |
| 1640 | } |
| 1641 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1642 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1643 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1644 | /* |
| 1645 | * Tell lockdep we inherited freeze protection from submission |
| 1646 | * thread. |
| 1647 | */ |
| 1648 | if (req->flags & REQ_F_ISREG) { |
| 1649 | struct inode *inode = file_inode(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1650 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1651 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1652 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1653 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1654 | } |
| 1655 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1656 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1657 | { |
| 1658 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1659 | req->flags |= REQ_F_FAIL_LINK; |
| 1660 | } |
| 1661 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1662 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1663 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1664 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1665 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1666 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1667 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1668 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1669 | if (res != req->result) |
| 1670 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1671 | io_cqring_add_event(req, res); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1675 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1676 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1677 | |
| 1678 | io_complete_rw_common(kiocb, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1679 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1682 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
| 1683 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1684 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1685 | struct io_kiocb *nxt = NULL; |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1686 | |
| 1687 | io_complete_rw_common(kiocb, res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1688 | io_put_req_find_next(req, &nxt); |
| 1689 | |
| 1690 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1691 | } |
| 1692 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1693 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 1694 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1695 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1696 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1697 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1698 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1699 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1700 | if (res != req->result) |
| 1701 | req_set_fail_links(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1702 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1703 | if (res != -EAGAIN) |
| 1704 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 1705 | } |
| 1706 | |
| 1707 | /* |
| 1708 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 1709 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 1710 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 1711 | * accessing the kiocb cookie. |
| 1712 | */ |
| 1713 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 1714 | { |
| 1715 | struct io_ring_ctx *ctx = req->ctx; |
| 1716 | |
| 1717 | /* |
| 1718 | * Track whether we have multiple files in our lists. This will impact |
| 1719 | * how we do polling eventually, not spinning if we're on potentially |
| 1720 | * different devices. |
| 1721 | */ |
| 1722 | if (list_empty(&ctx->poll_list)) { |
| 1723 | ctx->poll_multi_file = false; |
| 1724 | } else if (!ctx->poll_multi_file) { |
| 1725 | struct io_kiocb *list_req; |
| 1726 | |
| 1727 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1728 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1729 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1730 | ctx->poll_multi_file = true; |
| 1731 | } |
| 1732 | |
| 1733 | /* |
| 1734 | * For fast devices, IO may have already completed. If it has, add |
| 1735 | * it to the front so we find it first. |
| 1736 | */ |
| 1737 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1738 | list_add(&req->list, &ctx->poll_list); |
| 1739 | else |
| 1740 | list_add_tail(&req->list, &ctx->poll_list); |
| 1741 | } |
| 1742 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1743 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1744 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1745 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1746 | int diff = state->has_refs - state->used_refs; |
| 1747 | |
| 1748 | if (diff) |
| 1749 | fput_many(state->file, diff); |
| 1750 | state->file = NULL; |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | /* |
| 1755 | * Get as many references to a file as we have IOs left in this submission, |
| 1756 | * assuming most submissions are for one file, or at least that each file |
| 1757 | * has more than one submission. |
| 1758 | */ |
| 1759 | static struct file *io_file_get(struct io_submit_state *state, int fd) |
| 1760 | { |
| 1761 | if (!state) |
| 1762 | return fget(fd); |
| 1763 | |
| 1764 | if (state->file) { |
| 1765 | if (state->fd == fd) { |
| 1766 | state->used_refs++; |
| 1767 | state->ios_left--; |
| 1768 | return state->file; |
| 1769 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1770 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1771 | } |
| 1772 | state->file = fget_many(fd, state->ios_left); |
| 1773 | if (!state->file) |
| 1774 | return NULL; |
| 1775 | |
| 1776 | state->fd = fd; |
| 1777 | state->has_refs = state->ios_left; |
| 1778 | state->used_refs = 1; |
| 1779 | state->ios_left--; |
| 1780 | return state->file; |
| 1781 | } |
| 1782 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1783 | /* |
| 1784 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 1785 | * any file. For now, just ensure that anything potentially problematic is done |
| 1786 | * inline. |
| 1787 | */ |
| 1788 | static bool io_file_supports_async(struct file *file) |
| 1789 | { |
| 1790 | umode_t mode = file_inode(file)->i_mode; |
| 1791 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 1792 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1793 | return true; |
| 1794 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 1795 | return true; |
| 1796 | |
| 1797 | return false; |
| 1798 | } |
| 1799 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1800 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1801 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1802 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1803 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1804 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1805 | unsigned ioprio; |
| 1806 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1807 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1808 | if (!req->file) |
| 1809 | return -EBADF; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1810 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1811 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 1812 | req->flags |= REQ_F_ISREG; |
| 1813 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1814 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1815 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 1816 | req->flags |= REQ_F_CUR_POS; |
| 1817 | kiocb->ki_pos = req->file->f_pos; |
| 1818 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1819 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 1820 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
| 1821 | |
| 1822 | ioprio = READ_ONCE(sqe->ioprio); |
| 1823 | if (ioprio) { |
| 1824 | ret = ioprio_check_cap(ioprio); |
| 1825 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1826 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1827 | |
| 1828 | kiocb->ki_ioprio = ioprio; |
| 1829 | } else |
| 1830 | kiocb->ki_ioprio = get_current_ioprio(); |
| 1831 | |
| 1832 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 1833 | if (unlikely(ret)) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1834 | return ret; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1835 | |
| 1836 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1837 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 1838 | (req->file->f_flags & O_NONBLOCK)) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1839 | req->flags |= REQ_F_NOWAIT; |
| 1840 | |
| 1841 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1842 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1843 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1844 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1845 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 1846 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1847 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1848 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1849 | kiocb->ki_flags |= IOCB_HIPRI; |
| 1850 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 1851 | req->result = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1852 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1853 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1854 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1855 | kiocb->ki_complete = io_complete_rw; |
| 1856 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1857 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1858 | req->rw.addr = READ_ONCE(sqe->addr); |
| 1859 | req->rw.len = READ_ONCE(sqe->len); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1860 | /* we own ->private, reuse it for the buffer index */ |
| 1861 | req->rw.kiocb.private = (void *) (unsigned long) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1862 | READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1863 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 1867 | { |
| 1868 | switch (ret) { |
| 1869 | case -EIOCBQUEUED: |
| 1870 | break; |
| 1871 | case -ERESTARTSYS: |
| 1872 | case -ERESTARTNOINTR: |
| 1873 | case -ERESTARTNOHAND: |
| 1874 | case -ERESTART_RESTARTBLOCK: |
| 1875 | /* |
| 1876 | * We can't just restart the syscall, since previously |
| 1877 | * submitted sqes may already be in progress. Just fail this |
| 1878 | * IO with EINTR. |
| 1879 | */ |
| 1880 | ret = -EINTR; |
| 1881 | /* fall through */ |
| 1882 | default: |
| 1883 | kiocb->ki_complete(kiocb, ret, 0); |
| 1884 | } |
| 1885 | } |
| 1886 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1887 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt, |
| 1888 | bool in_async) |
| 1889 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1890 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 1891 | |
| 1892 | if (req->flags & REQ_F_CUR_POS) |
| 1893 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1894 | if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1895 | *nxt = __io_complete_rw(kiocb, ret); |
| 1896 | else |
| 1897 | io_rw_done(kiocb, ret); |
| 1898 | } |
| 1899 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1900 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 1901 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1902 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1903 | struct io_ring_ctx *ctx = req->ctx; |
| 1904 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1905 | struct io_mapped_ubuf *imu; |
| 1906 | unsigned index, buf_index; |
| 1907 | size_t offset; |
| 1908 | u64 buf_addr; |
| 1909 | |
| 1910 | /* attempt to use fixed buffers without having provided iovecs */ |
| 1911 | if (unlikely(!ctx->user_bufs)) |
| 1912 | return -EFAULT; |
| 1913 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1914 | buf_index = (unsigned long) req->rw.kiocb.private; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1915 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 1916 | return -EFAULT; |
| 1917 | |
| 1918 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 1919 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1920 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1921 | |
| 1922 | /* overflow */ |
| 1923 | if (buf_addr + len < buf_addr) |
| 1924 | return -EFAULT; |
| 1925 | /* not inside the mapped region */ |
| 1926 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 1927 | return -EFAULT; |
| 1928 | |
| 1929 | /* |
| 1930 | * May not be a start of buffer, set size appropriately |
| 1931 | * and advance us to the beginning. |
| 1932 | */ |
| 1933 | offset = buf_addr - imu->ubuf; |
| 1934 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1935 | |
| 1936 | if (offset) { |
| 1937 | /* |
| 1938 | * Don't use iov_iter_advance() here, as it's really slow for |
| 1939 | * using the latter parts of a big fixed buffer - it iterates |
| 1940 | * over each segment manually. We can cheat a bit here, because |
| 1941 | * we know that: |
| 1942 | * |
| 1943 | * 1) it's a BVEC iter, we set it up |
| 1944 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 1945 | * first and last bvec |
| 1946 | * |
| 1947 | * So just find our index, and adjust the iterator afterwards. |
| 1948 | * If the offset is within the first bvec (or the whole first |
| 1949 | * bvec, just use iov_iter_advance(). This makes it easier |
| 1950 | * since we can just skip the first segment, which may not |
| 1951 | * be PAGE_SIZE aligned. |
| 1952 | */ |
| 1953 | const struct bio_vec *bvec = imu->bvec; |
| 1954 | |
| 1955 | if (offset <= bvec->bv_len) { |
| 1956 | iov_iter_advance(iter, offset); |
| 1957 | } else { |
| 1958 | unsigned long seg_skip; |
| 1959 | |
| 1960 | /* skip first vec */ |
| 1961 | offset -= bvec->bv_len; |
| 1962 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 1963 | |
| 1964 | iter->bvec = bvec + seg_skip; |
| 1965 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 1966 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1967 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1968 | } |
| 1969 | } |
| 1970 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 1971 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 1974 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 1975 | struct iovec **iovec, struct iov_iter *iter) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1976 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1977 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 1978 | size_t sqe_len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1979 | u8 opcode; |
| 1980 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1981 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 1982 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1983 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1984 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1985 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1986 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1987 | /* buffer index only valid with fixed read/write */ |
| 1988 | if (req->rw.kiocb.private) |
| 1989 | return -EINVAL; |
| 1990 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1991 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
| 1992 | ssize_t ret; |
| 1993 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 1994 | *iovec = NULL; |
| 1995 | return ret; |
| 1996 | } |
| 1997 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1998 | if (req->io) { |
| 1999 | struct io_async_rw *iorw = &req->io->rw; |
| 2000 | |
| 2001 | *iovec = iorw->iov; |
| 2002 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 2003 | if (iorw->iov == iorw->fast_iov) |
| 2004 | *iovec = NULL; |
| 2005 | return iorw->size; |
| 2006 | } |
| 2007 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2008 | if (!req->has_user) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2009 | return -EFAULT; |
| 2010 | |
| 2011 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2012 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2013 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2014 | iovec, iter); |
| 2015 | #endif |
| 2016 | |
| 2017 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2018 | } |
| 2019 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2020 | /* |
| 2021 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2022 | * by looping over ->read() or ->write() manually. |
| 2023 | */ |
| 2024 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2025 | struct iov_iter *iter) |
| 2026 | { |
| 2027 | ssize_t ret = 0; |
| 2028 | |
| 2029 | /* |
| 2030 | * Don't support polled IO through this interface, and we can't |
| 2031 | * support non-blocking either. For the latter, this just causes |
| 2032 | * the kiocb to be handled from an async context. |
| 2033 | */ |
| 2034 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2035 | return -EOPNOTSUPP; |
| 2036 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2037 | return -EAGAIN; |
| 2038 | |
| 2039 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2040 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2041 | ssize_t nr; |
| 2042 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2043 | if (!iov_iter_is_bvec(iter)) { |
| 2044 | iovec = iov_iter_iovec(iter); |
| 2045 | } else { |
| 2046 | /* fixed buffers import bvec */ |
| 2047 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2048 | + iter->iov_offset; |
| 2049 | iovec.iov_len = min(iter->count, |
| 2050 | iter->bvec->bv_len - iter->iov_offset); |
| 2051 | } |
| 2052 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2053 | if (rw == READ) { |
| 2054 | nr = file->f_op->read(file, iovec.iov_base, |
| 2055 | iovec.iov_len, &kiocb->ki_pos); |
| 2056 | } else { |
| 2057 | nr = file->f_op->write(file, iovec.iov_base, |
| 2058 | iovec.iov_len, &kiocb->ki_pos); |
| 2059 | } |
| 2060 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2061 | if (iov_iter_is_bvec(iter)) |
| 2062 | kunmap(iter->bvec->bv_page); |
| 2063 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2064 | if (nr < 0) { |
| 2065 | if (!ret) |
| 2066 | ret = nr; |
| 2067 | break; |
| 2068 | } |
| 2069 | ret += nr; |
| 2070 | if (nr != iovec.iov_len) |
| 2071 | break; |
| 2072 | iov_iter_advance(iter, nr); |
| 2073 | } |
| 2074 | |
| 2075 | return ret; |
| 2076 | } |
| 2077 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2078 | 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] | 2079 | struct iovec *iovec, struct iovec *fast_iov, |
| 2080 | struct iov_iter *iter) |
| 2081 | { |
| 2082 | req->io->rw.nr_segs = iter->nr_segs; |
| 2083 | req->io->rw.size = io_size; |
| 2084 | req->io->rw.iov = iovec; |
| 2085 | if (!req->io->rw.iov) { |
| 2086 | req->io->rw.iov = req->io->rw.fast_iov; |
| 2087 | memcpy(req->io->rw.iov, fast_iov, |
| 2088 | sizeof(struct iovec) * iter->nr_segs); |
| 2089 | } |
| 2090 | } |
| 2091 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2092 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2093 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2094 | if (!io_op_defs[req->opcode].async_ctx) |
| 2095 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2096 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2097 | return req->io == NULL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | static void io_rw_async(struct io_wq_work **workptr) |
| 2101 | { |
| 2102 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2103 | struct iovec *iov = NULL; |
| 2104 | |
| 2105 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2106 | iov = req->io->rw.iov; |
| 2107 | io_wq_submit_work(workptr); |
| 2108 | kfree(iov); |
| 2109 | } |
| 2110 | |
| 2111 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2112 | struct iovec *iovec, struct iovec *fast_iov, |
| 2113 | struct iov_iter *iter) |
| 2114 | { |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2115 | if (req->opcode == IORING_OP_READ_FIXED || |
| 2116 | req->opcode == IORING_OP_WRITE_FIXED) |
| 2117 | return 0; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2118 | if (!req->io && io_alloc_async_ctx(req)) |
| 2119 | return -ENOMEM; |
| 2120 | |
| 2121 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2122 | req->work.func = io_rw_async; |
| 2123 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2124 | } |
| 2125 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2126 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2127 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2128 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2129 | struct io_async_ctx *io; |
| 2130 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2131 | ssize_t ret; |
| 2132 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2133 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2134 | if (ret) |
| 2135 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2136 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2137 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2138 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2139 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2140 | if (!req->io) |
| 2141 | return 0; |
| 2142 | |
| 2143 | io = req->io; |
| 2144 | io->rw.iov = io->rw.fast_iov; |
| 2145 | req->io = NULL; |
| 2146 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter); |
| 2147 | req->io = io; |
| 2148 | if (ret < 0) |
| 2149 | return ret; |
| 2150 | |
| 2151 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2152 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2153 | } |
| 2154 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2155 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2156 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2157 | { |
| 2158 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2159 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2160 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2161 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2162 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2163 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2164 | ret = io_import_iovec(READ, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2165 | if (ret < 0) |
| 2166 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2167 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2168 | /* Ensure we clear previously set non-block flag */ |
| 2169 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2170 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2171 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2172 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2173 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2174 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2175 | req->result = io_size; |
| 2176 | |
| 2177 | /* |
| 2178 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2179 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2180 | */ |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2181 | if (force_nonblock && !io_file_supports_async(req->file)) { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2182 | req->flags |= REQ_F_MUST_PUNT; |
| 2183 | goto copy_iov; |
| 2184 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2185 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2186 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2187 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2188 | if (!ret) { |
| 2189 | ssize_t ret2; |
| 2190 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2191 | if (req->file->f_op->read_iter) |
| 2192 | ret2 = call_read_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2193 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2194 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2195 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 2196 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2197 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2198 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2199 | } else { |
| 2200 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2201 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2202 | inline_vecs, &iter); |
| 2203 | if (ret) |
| 2204 | goto out_free; |
| 2205 | return -EAGAIN; |
| 2206 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2207 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2208 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2209 | if (!io_wq_current_is_worker()) |
| 2210 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2211 | return ret; |
| 2212 | } |
| 2213 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2214 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2215 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2216 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2217 | struct io_async_ctx *io; |
| 2218 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2219 | ssize_t ret; |
| 2220 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2221 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2222 | if (ret) |
| 2223 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2224 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2225 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 2226 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2227 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2228 | if (!req->io) |
| 2229 | return 0; |
| 2230 | |
| 2231 | io = req->io; |
| 2232 | io->rw.iov = io->rw.fast_iov; |
| 2233 | req->io = NULL; |
| 2234 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter); |
| 2235 | req->io = io; |
| 2236 | if (ret < 0) |
| 2237 | return ret; |
| 2238 | |
| 2239 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2240 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2241 | } |
| 2242 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2243 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2244 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2245 | { |
| 2246 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2247 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2248 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2249 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2250 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2251 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2252 | ret = io_import_iovec(WRITE, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2253 | if (ret < 0) |
| 2254 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2255 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2256 | /* Ensure we clear previously set non-block flag */ |
| 2257 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2258 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2259 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2260 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2261 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2262 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2263 | req->result = io_size; |
| 2264 | |
| 2265 | /* |
| 2266 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2267 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2268 | */ |
| 2269 | if (force_nonblock && !io_file_supports_async(req->file)) { |
| 2270 | req->flags |= REQ_F_MUST_PUNT; |
| 2271 | goto copy_iov; |
| 2272 | } |
| 2273 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2274 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 2275 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 2276 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2277 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2278 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2279 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2280 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2281 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2282 | ssize_t ret2; |
| 2283 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2284 | /* |
| 2285 | * Open-code file_start_write here to grab freeze protection, |
| 2286 | * which will be released by another thread in |
| 2287 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 2288 | * released so that it doesn't complain about the held lock when |
| 2289 | * we return to userspace. |
| 2290 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2291 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2292 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2293 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2294 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2295 | SB_FREEZE_WRITE); |
| 2296 | } |
| 2297 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2298 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2299 | if (req->file->f_op->write_iter) |
| 2300 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2301 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2302 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2303 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2304 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2305 | } else { |
| 2306 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2307 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2308 | inline_vecs, &iter); |
| 2309 | if (ret) |
| 2310 | goto out_free; |
| 2311 | return -EAGAIN; |
| 2312 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2313 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2314 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2315 | if (!io_wq_current_is_worker()) |
| 2316 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2317 | return ret; |
| 2318 | } |
| 2319 | |
| 2320 | /* |
| 2321 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 2322 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2323 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2324 | { |
| 2325 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2326 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2327 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2328 | return -EINVAL; |
| 2329 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2330 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2331 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2332 | return 0; |
| 2333 | } |
| 2334 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2335 | 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] | 2336 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2337 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2338 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2339 | if (!req->file) |
| 2340 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2341 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2342 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2343 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2344 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2345 | return -EINVAL; |
| 2346 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2347 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 2348 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 2349 | return -EINVAL; |
| 2350 | |
| 2351 | req->sync.off = READ_ONCE(sqe->off); |
| 2352 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2353 | return 0; |
| 2354 | } |
| 2355 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2356 | static bool io_req_cancelled(struct io_kiocb *req) |
| 2357 | { |
| 2358 | if (req->work.flags & IO_WQ_WORK_CANCEL) { |
| 2359 | req_set_fail_links(req); |
| 2360 | io_cqring_add_event(req, -ECANCELED); |
| 2361 | io_put_req(req); |
| 2362 | return true; |
| 2363 | } |
| 2364 | |
| 2365 | return false; |
| 2366 | } |
| 2367 | |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2368 | static void io_link_work_cb(struct io_wq_work **workptr) |
| 2369 | { |
| 2370 | struct io_wq_work *work = *workptr; |
| 2371 | struct io_kiocb *link = work->data; |
| 2372 | |
| 2373 | io_queue_linked_timeout(link); |
| 2374 | work->func = io_wq_submit_work; |
| 2375 | } |
| 2376 | |
| 2377 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) |
| 2378 | { |
| 2379 | struct io_kiocb *link; |
| 2380 | |
| 2381 | io_prep_async_work(nxt, &link); |
| 2382 | *workptr = &nxt->work; |
| 2383 | if (link) { |
| 2384 | nxt->work.flags |= IO_WQ_WORK_CB; |
| 2385 | nxt->work.func = io_link_work_cb; |
| 2386 | nxt->work.data = link; |
| 2387 | } |
| 2388 | } |
| 2389 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2390 | static void io_fsync_finish(struct io_wq_work **workptr) |
| 2391 | { |
| 2392 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2393 | loff_t end = req->sync.off + req->sync.len; |
| 2394 | struct io_kiocb *nxt = NULL; |
| 2395 | int ret; |
| 2396 | |
| 2397 | if (io_req_cancelled(req)) |
| 2398 | return; |
| 2399 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2400 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2401 | end > 0 ? end : LLONG_MAX, |
| 2402 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 2403 | if (ret < 0) |
| 2404 | req_set_fail_links(req); |
| 2405 | io_cqring_add_event(req, ret); |
| 2406 | io_put_req_find_next(req, &nxt); |
| 2407 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2408 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2409 | } |
| 2410 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2411 | static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2412 | bool force_nonblock) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2413 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2414 | struct io_wq_work *work, *old_work; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2415 | |
| 2416 | /* fsync always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2417 | if (force_nonblock) { |
| 2418 | io_put_req(req); |
| 2419 | req->work.func = io_fsync_finish; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2420 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2421 | } |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2422 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2423 | work = old_work = &req->work; |
| 2424 | io_fsync_finish(&work); |
| 2425 | if (work && work != old_work) |
| 2426 | *nxt = container_of(work, struct io_kiocb, work); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2427 | return 0; |
| 2428 | } |
| 2429 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2430 | static void io_fallocate_finish(struct io_wq_work **workptr) |
| 2431 | { |
| 2432 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2433 | struct io_kiocb *nxt = NULL; |
| 2434 | int ret; |
| 2435 | |
| 2436 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 2437 | req->sync.len); |
| 2438 | if (ret < 0) |
| 2439 | req_set_fail_links(req); |
| 2440 | io_cqring_add_event(req, ret); |
| 2441 | io_put_req_find_next(req, &nxt); |
| 2442 | if (nxt) |
| 2443 | io_wq_assign_next(workptr, nxt); |
| 2444 | } |
| 2445 | |
| 2446 | static int io_fallocate_prep(struct io_kiocb *req, |
| 2447 | const struct io_uring_sqe *sqe) |
| 2448 | { |
| 2449 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 2450 | return -EINVAL; |
| 2451 | |
| 2452 | req->sync.off = READ_ONCE(sqe->off); |
| 2453 | req->sync.len = READ_ONCE(sqe->addr); |
| 2454 | req->sync.mode = READ_ONCE(sqe->len); |
| 2455 | return 0; |
| 2456 | } |
| 2457 | |
| 2458 | static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2459 | bool force_nonblock) |
| 2460 | { |
| 2461 | struct io_wq_work *work, *old_work; |
| 2462 | |
| 2463 | /* fallocate always requiring blocking context */ |
| 2464 | if (force_nonblock) { |
| 2465 | io_put_req(req); |
| 2466 | req->work.func = io_fallocate_finish; |
| 2467 | return -EAGAIN; |
| 2468 | } |
| 2469 | |
| 2470 | work = old_work = &req->work; |
| 2471 | io_fallocate_finish(&work); |
| 2472 | if (work && work != old_work) |
| 2473 | *nxt = container_of(work, struct io_kiocb, work); |
| 2474 | |
| 2475 | return 0; |
| 2476 | } |
| 2477 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2478 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2479 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2480 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2481 | int ret; |
| 2482 | |
| 2483 | if (sqe->ioprio || sqe->buf_index) |
| 2484 | return -EINVAL; |
| 2485 | |
| 2486 | req->open.dfd = READ_ONCE(sqe->fd); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2487 | req->open.how.mode = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2488 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2489 | req->open.how.flags = READ_ONCE(sqe->open_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2490 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2491 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2492 | if (IS_ERR(req->open.filename)) { |
| 2493 | ret = PTR_ERR(req->open.filename); |
| 2494 | req->open.filename = NULL; |
| 2495 | return ret; |
| 2496 | } |
| 2497 | |
| 2498 | return 0; |
| 2499 | } |
| 2500 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2501 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2502 | { |
| 2503 | struct open_how __user *how; |
| 2504 | const char __user *fname; |
| 2505 | size_t len; |
| 2506 | int ret; |
| 2507 | |
| 2508 | if (sqe->ioprio || sqe->buf_index) |
| 2509 | return -EINVAL; |
| 2510 | |
| 2511 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2512 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2513 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 2514 | len = READ_ONCE(sqe->len); |
| 2515 | |
| 2516 | if (len < OPEN_HOW_SIZE_VER0) |
| 2517 | return -EINVAL; |
| 2518 | |
| 2519 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 2520 | len); |
| 2521 | if (ret) |
| 2522 | return ret; |
| 2523 | |
| 2524 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
| 2525 | req->open.how.flags |= O_LARGEFILE; |
| 2526 | |
| 2527 | req->open.filename = getname(fname); |
| 2528 | if (IS_ERR(req->open.filename)) { |
| 2529 | ret = PTR_ERR(req->open.filename); |
| 2530 | req->open.filename = NULL; |
| 2531 | return ret; |
| 2532 | } |
| 2533 | |
| 2534 | return 0; |
| 2535 | } |
| 2536 | |
| 2537 | static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2538 | bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2539 | { |
| 2540 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2541 | struct file *file; |
| 2542 | int ret; |
| 2543 | |
| 2544 | if (force_nonblock) { |
| 2545 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 2546 | return -EAGAIN; |
| 2547 | } |
| 2548 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2549 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2550 | if (ret) |
| 2551 | goto err; |
| 2552 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2553 | ret = get_unused_fd_flags(req->open.how.flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2554 | if (ret < 0) |
| 2555 | goto err; |
| 2556 | |
| 2557 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 2558 | if (IS_ERR(file)) { |
| 2559 | put_unused_fd(ret); |
| 2560 | ret = PTR_ERR(file); |
| 2561 | } else { |
| 2562 | fsnotify_open(file); |
| 2563 | fd_install(ret, file); |
| 2564 | } |
| 2565 | err: |
| 2566 | putname(req->open.filename); |
| 2567 | if (ret < 0) |
| 2568 | req_set_fail_links(req); |
| 2569 | io_cqring_add_event(req, ret); |
| 2570 | io_put_req_find_next(req, nxt); |
| 2571 | return 0; |
| 2572 | } |
| 2573 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2574 | static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2575 | bool force_nonblock) |
| 2576 | { |
| 2577 | req->open.how = build_open_how(req->open.how.flags, req->open.how.mode); |
| 2578 | return io_openat2(req, nxt, force_nonblock); |
| 2579 | } |
| 2580 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 2581 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2582 | { |
| 2583 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2584 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 2585 | return -EINVAL; |
| 2586 | |
| 2587 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 2588 | req->madvise.len = READ_ONCE(sqe->len); |
| 2589 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2590 | return 0; |
| 2591 | #else |
| 2592 | return -EOPNOTSUPP; |
| 2593 | #endif |
| 2594 | } |
| 2595 | |
| 2596 | static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2597 | bool force_nonblock) |
| 2598 | { |
| 2599 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2600 | struct io_madvise *ma = &req->madvise; |
| 2601 | int ret; |
| 2602 | |
| 2603 | if (force_nonblock) |
| 2604 | return -EAGAIN; |
| 2605 | |
| 2606 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 2607 | if (ret < 0) |
| 2608 | req_set_fail_links(req); |
| 2609 | io_cqring_add_event(req, ret); |
| 2610 | io_put_req_find_next(req, nxt); |
| 2611 | return 0; |
| 2612 | #else |
| 2613 | return -EOPNOTSUPP; |
| 2614 | #endif |
| 2615 | } |
| 2616 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 2617 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2618 | { |
| 2619 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 2620 | return -EINVAL; |
| 2621 | |
| 2622 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 2623 | req->fadvise.len = READ_ONCE(sqe->len); |
| 2624 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2625 | return 0; |
| 2626 | } |
| 2627 | |
| 2628 | static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2629 | bool force_nonblock) |
| 2630 | { |
| 2631 | struct io_fadvise *fa = &req->fadvise; |
| 2632 | int ret; |
| 2633 | |
| 2634 | /* DONTNEED may block, others _should_ not */ |
| 2635 | if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock) |
| 2636 | return -EAGAIN; |
| 2637 | |
| 2638 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 2639 | if (ret < 0) |
| 2640 | req_set_fail_links(req); |
| 2641 | io_cqring_add_event(req, ret); |
| 2642 | io_put_req_find_next(req, nxt); |
| 2643 | return 0; |
| 2644 | } |
| 2645 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2646 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2647 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2648 | const char __user *fname; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2649 | unsigned lookup_flags; |
| 2650 | int ret; |
| 2651 | |
| 2652 | if (sqe->ioprio || sqe->buf_index) |
| 2653 | return -EINVAL; |
| 2654 | |
| 2655 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2656 | req->open.mask = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2657 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2658 | req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2659 | req->open.how.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2660 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2661 | if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2662 | return -EINVAL; |
| 2663 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2664 | req->open.filename = getname_flags(fname, lookup_flags, NULL); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2665 | if (IS_ERR(req->open.filename)) { |
| 2666 | ret = PTR_ERR(req->open.filename); |
| 2667 | req->open.filename = NULL; |
| 2668 | return ret; |
| 2669 | } |
| 2670 | |
| 2671 | return 0; |
| 2672 | } |
| 2673 | |
| 2674 | static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2675 | bool force_nonblock) |
| 2676 | { |
| 2677 | struct io_open *ctx = &req->open; |
| 2678 | unsigned lookup_flags; |
| 2679 | struct path path; |
| 2680 | struct kstat stat; |
| 2681 | int ret; |
| 2682 | |
| 2683 | if (force_nonblock) |
| 2684 | return -EAGAIN; |
| 2685 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2686 | if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2687 | return -EINVAL; |
| 2688 | |
| 2689 | retry: |
| 2690 | /* filename_lookup() drops it, keep a reference */ |
| 2691 | ctx->filename->refcnt++; |
| 2692 | |
| 2693 | ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path, |
| 2694 | NULL); |
| 2695 | if (ret) |
| 2696 | goto err; |
| 2697 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2698 | ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2699 | path_put(&path); |
| 2700 | if (retry_estale(ret, lookup_flags)) { |
| 2701 | lookup_flags |= LOOKUP_REVAL; |
| 2702 | goto retry; |
| 2703 | } |
| 2704 | if (!ret) |
| 2705 | ret = cp_statx(&stat, ctx->buffer); |
| 2706 | err: |
| 2707 | putname(ctx->filename); |
| 2708 | if (ret < 0) |
| 2709 | req_set_fail_links(req); |
| 2710 | io_cqring_add_event(req, ret); |
| 2711 | io_put_req_find_next(req, nxt); |
| 2712 | return 0; |
| 2713 | } |
| 2714 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2715 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2716 | { |
| 2717 | /* |
| 2718 | * If we queue this for async, it must not be cancellable. That would |
| 2719 | * leave the 'file' in an undeterminate state. |
| 2720 | */ |
| 2721 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 2722 | |
| 2723 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 2724 | sqe->rw_flags || sqe->buf_index) |
| 2725 | return -EINVAL; |
| 2726 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2727 | return -EINVAL; |
| 2728 | |
| 2729 | req->close.fd = READ_ONCE(sqe->fd); |
| 2730 | if (req->file->f_op == &io_uring_fops || |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 2731 | req->close.fd == req->ctx->ring_fd) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2732 | return -EBADF; |
| 2733 | |
| 2734 | return 0; |
| 2735 | } |
| 2736 | |
| 2737 | static void io_close_finish(struct io_wq_work **workptr) |
| 2738 | { |
| 2739 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2740 | struct io_kiocb *nxt = NULL; |
| 2741 | |
| 2742 | /* Invoked with files, we need to do the close */ |
| 2743 | if (req->work.files) { |
| 2744 | int ret; |
| 2745 | |
| 2746 | ret = filp_close(req->close.put_file, req->work.files); |
| 2747 | if (ret < 0) { |
| 2748 | req_set_fail_links(req); |
| 2749 | } |
| 2750 | io_cqring_add_event(req, ret); |
| 2751 | } |
| 2752 | |
| 2753 | fput(req->close.put_file); |
| 2754 | |
| 2755 | /* we bypassed the re-issue, drop the submission reference */ |
| 2756 | io_put_req(req); |
| 2757 | io_put_req_find_next(req, &nxt); |
| 2758 | if (nxt) |
| 2759 | io_wq_assign_next(workptr, nxt); |
| 2760 | } |
| 2761 | |
| 2762 | static int io_close(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2763 | bool force_nonblock) |
| 2764 | { |
| 2765 | int ret; |
| 2766 | |
| 2767 | req->close.put_file = NULL; |
| 2768 | ret = __close_fd_get_file(req->close.fd, &req->close.put_file); |
| 2769 | if (ret < 0) |
| 2770 | return ret; |
| 2771 | |
| 2772 | /* if the file has a flush method, be safe and punt to async */ |
| 2773 | if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) { |
| 2774 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 2775 | goto eagain; |
| 2776 | } |
| 2777 | |
| 2778 | /* |
| 2779 | * No ->flush(), safely close from here and just punt the |
| 2780 | * fput() to async context. |
| 2781 | */ |
| 2782 | ret = filp_close(req->close.put_file, current->files); |
| 2783 | |
| 2784 | if (ret < 0) |
| 2785 | req_set_fail_links(req); |
| 2786 | io_cqring_add_event(req, ret); |
| 2787 | |
| 2788 | if (io_wq_current_is_worker()) { |
| 2789 | struct io_wq_work *old_work, *work; |
| 2790 | |
| 2791 | old_work = work = &req->work; |
| 2792 | io_close_finish(&work); |
| 2793 | if (work && work != old_work) |
| 2794 | *nxt = container_of(work, struct io_kiocb, work); |
| 2795 | return 0; |
| 2796 | } |
| 2797 | |
| 2798 | eagain: |
| 2799 | req->work.func = io_close_finish; |
| 2800 | return -EAGAIN; |
| 2801 | } |
| 2802 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2803 | 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] | 2804 | { |
| 2805 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2806 | |
| 2807 | if (!req->file) |
| 2808 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2809 | |
| 2810 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2811 | return -EINVAL; |
| 2812 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 2813 | return -EINVAL; |
| 2814 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2815 | req->sync.off = READ_ONCE(sqe->off); |
| 2816 | req->sync.len = READ_ONCE(sqe->len); |
| 2817 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2818 | return 0; |
| 2819 | } |
| 2820 | |
| 2821 | static void io_sync_file_range_finish(struct io_wq_work **workptr) |
| 2822 | { |
| 2823 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2824 | struct io_kiocb *nxt = NULL; |
| 2825 | int ret; |
| 2826 | |
| 2827 | if (io_req_cancelled(req)) |
| 2828 | return; |
| 2829 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2830 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2831 | req->sync.flags); |
| 2832 | if (ret < 0) |
| 2833 | req_set_fail_links(req); |
| 2834 | io_cqring_add_event(req, ret); |
| 2835 | io_put_req_find_next(req, &nxt); |
| 2836 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2837 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2838 | } |
| 2839 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2840 | 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] | 2841 | bool force_nonblock) |
| 2842 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2843 | struct io_wq_work *work, *old_work; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2844 | |
| 2845 | /* sync_file_range always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2846 | if (force_nonblock) { |
| 2847 | io_put_req(req); |
| 2848 | req->work.func = io_sync_file_range_finish; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2849 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2850 | } |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2851 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2852 | work = old_work = &req->work; |
| 2853 | io_sync_file_range_finish(&work); |
| 2854 | if (work && work != old_work) |
| 2855 | *nxt = container_of(work, struct io_kiocb, work); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2856 | return 0; |
| 2857 | } |
| 2858 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2859 | #if defined(CONFIG_NET) |
| 2860 | static void io_sendrecv_async(struct io_wq_work **workptr) |
| 2861 | { |
| 2862 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2863 | struct iovec *iov = NULL; |
| 2864 | |
| 2865 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2866 | iov = req->io->msg.iov; |
| 2867 | io_wq_submit_work(workptr); |
| 2868 | kfree(iov); |
| 2869 | } |
| 2870 | #endif |
| 2871 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2872 | 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] | 2873 | { |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2874 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2875 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2876 | struct io_async_ctx *io = req->io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2877 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2878 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 2879 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2880 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2881 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2882 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2883 | return 0; |
| 2884 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 2885 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2886 | return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2887 | &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2888 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2889 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2890 | #endif |
| 2891 | } |
| 2892 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2893 | static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2894 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2895 | { |
| 2896 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2897 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2898 | struct socket *sock; |
| 2899 | int ret; |
| 2900 | |
| 2901 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2902 | return -EINVAL; |
| 2903 | |
| 2904 | sock = sock_from_file(req->file, &ret); |
| 2905 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2906 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2907 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2908 | unsigned flags; |
| 2909 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2910 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2911 | kmsg = &req->io->msg; |
| 2912 | kmsg->msg.msg_name = &addr; |
| 2913 | /* if iov is set, it's allocated already */ |
| 2914 | if (!kmsg->iov) |
| 2915 | kmsg->iov = kmsg->fast_iov; |
| 2916 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2917 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2918 | struct io_sr_msg *sr = &req->sr_msg; |
| 2919 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2920 | kmsg = &io.msg; |
| 2921 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2922 | |
| 2923 | io.msg.iov = io.msg.fast_iov; |
| 2924 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 2925 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2926 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2927 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2928 | } |
| 2929 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2930 | flags = req->sr_msg.msg_flags; |
| 2931 | if (flags & MSG_DONTWAIT) |
| 2932 | req->flags |= REQ_F_NOWAIT; |
| 2933 | else if (force_nonblock) |
| 2934 | flags |= MSG_DONTWAIT; |
| 2935 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2936 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2937 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2938 | if (req->io) |
| 2939 | return -EAGAIN; |
| 2940 | if (io_alloc_async_ctx(req)) |
| 2941 | return -ENOMEM; |
| 2942 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 2943 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2944 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2945 | } |
| 2946 | if (ret == -ERESTARTSYS) |
| 2947 | ret = -EINTR; |
| 2948 | } |
| 2949 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2950 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2951 | kfree(kmsg->iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2952 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2953 | if (ret < 0) |
| 2954 | req_set_fail_links(req); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2955 | io_put_req_find_next(req, nxt); |
| 2956 | return 0; |
| 2957 | #else |
| 2958 | return -EOPNOTSUPP; |
| 2959 | #endif |
| 2960 | } |
| 2961 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2962 | static int io_send(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2963 | bool force_nonblock) |
| 2964 | { |
| 2965 | #if defined(CONFIG_NET) |
| 2966 | struct socket *sock; |
| 2967 | int ret; |
| 2968 | |
| 2969 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2970 | return -EINVAL; |
| 2971 | |
| 2972 | sock = sock_from_file(req->file, &ret); |
| 2973 | if (sock) { |
| 2974 | struct io_sr_msg *sr = &req->sr_msg; |
| 2975 | struct msghdr msg; |
| 2976 | struct iovec iov; |
| 2977 | unsigned flags; |
| 2978 | |
| 2979 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, |
| 2980 | &msg.msg_iter); |
| 2981 | if (ret) |
| 2982 | return ret; |
| 2983 | |
| 2984 | msg.msg_name = NULL; |
| 2985 | msg.msg_control = NULL; |
| 2986 | msg.msg_controllen = 0; |
| 2987 | msg.msg_namelen = 0; |
| 2988 | |
| 2989 | flags = req->sr_msg.msg_flags; |
| 2990 | if (flags & MSG_DONTWAIT) |
| 2991 | req->flags |= REQ_F_NOWAIT; |
| 2992 | else if (force_nonblock) |
| 2993 | flags |= MSG_DONTWAIT; |
| 2994 | |
| 2995 | ret = __sys_sendmsg_sock(sock, &msg, flags); |
| 2996 | if (force_nonblock && ret == -EAGAIN) |
| 2997 | return -EAGAIN; |
| 2998 | if (ret == -ERESTARTSYS) |
| 2999 | ret = -EINTR; |
| 3000 | } |
| 3001 | |
| 3002 | io_cqring_add_event(req, ret); |
| 3003 | if (ret < 0) |
| 3004 | req_set_fail_links(req); |
| 3005 | io_put_req_find_next(req, nxt); |
| 3006 | return 0; |
| 3007 | #else |
| 3008 | return -EOPNOTSUPP; |
| 3009 | #endif |
| 3010 | } |
| 3011 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3012 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 3013 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3014 | { |
| 3015 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3016 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3017 | struct io_async_ctx *io = req->io; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3018 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3019 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3020 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3021 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3022 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3023 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3024 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3025 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3026 | return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3027 | &io->msg.uaddr, &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3028 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3029 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3030 | #endif |
| 3031 | } |
| 3032 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3033 | static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3034 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3035 | { |
| 3036 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3037 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3038 | struct socket *sock; |
| 3039 | int ret; |
| 3040 | |
| 3041 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3042 | return -EINVAL; |
| 3043 | |
| 3044 | sock = sock_from_file(req->file, &ret); |
| 3045 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3046 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3047 | struct sockaddr_storage addr; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3048 | unsigned flags; |
| 3049 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3050 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3051 | kmsg = &req->io->msg; |
| 3052 | kmsg->msg.msg_name = &addr; |
| 3053 | /* if iov is set, it's allocated already */ |
| 3054 | if (!kmsg->iov) |
| 3055 | kmsg->iov = kmsg->fast_iov; |
| 3056 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3057 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3058 | struct io_sr_msg *sr = &req->sr_msg; |
| 3059 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3060 | kmsg = &io.msg; |
| 3061 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3062 | |
| 3063 | io.msg.iov = io.msg.fast_iov; |
| 3064 | ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3065 | sr->msg_flags, &io.msg.uaddr, |
| 3066 | &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3067 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3068 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3069 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3070 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3071 | flags = req->sr_msg.msg_flags; |
| 3072 | if (flags & MSG_DONTWAIT) |
| 3073 | req->flags |= REQ_F_NOWAIT; |
| 3074 | else if (force_nonblock) |
| 3075 | flags |= MSG_DONTWAIT; |
| 3076 | |
| 3077 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 3078 | kmsg->uaddr, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3079 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3080 | if (req->io) |
| 3081 | return -EAGAIN; |
| 3082 | if (io_alloc_async_ctx(req)) |
| 3083 | return -ENOMEM; |
| 3084 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 3085 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3086 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3087 | } |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 3088 | if (ret == -ERESTARTSYS) |
| 3089 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3090 | } |
| 3091 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3092 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3093 | kfree(kmsg->iov); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3094 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3095 | if (ret < 0) |
| 3096 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3097 | io_put_req_find_next(req, nxt); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3098 | return 0; |
| 3099 | #else |
| 3100 | return -EOPNOTSUPP; |
| 3101 | #endif |
| 3102 | } |
| 3103 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3104 | static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3105 | bool force_nonblock) |
| 3106 | { |
| 3107 | #if defined(CONFIG_NET) |
| 3108 | struct socket *sock; |
| 3109 | int ret; |
| 3110 | |
| 3111 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3112 | return -EINVAL; |
| 3113 | |
| 3114 | sock = sock_from_file(req->file, &ret); |
| 3115 | if (sock) { |
| 3116 | struct io_sr_msg *sr = &req->sr_msg; |
| 3117 | struct msghdr msg; |
| 3118 | struct iovec iov; |
| 3119 | unsigned flags; |
| 3120 | |
| 3121 | ret = import_single_range(READ, sr->buf, sr->len, &iov, |
| 3122 | &msg.msg_iter); |
| 3123 | if (ret) |
| 3124 | return ret; |
| 3125 | |
| 3126 | msg.msg_name = NULL; |
| 3127 | msg.msg_control = NULL; |
| 3128 | msg.msg_controllen = 0; |
| 3129 | msg.msg_namelen = 0; |
| 3130 | msg.msg_iocb = NULL; |
| 3131 | msg.msg_flags = 0; |
| 3132 | |
| 3133 | flags = req->sr_msg.msg_flags; |
| 3134 | if (flags & MSG_DONTWAIT) |
| 3135 | req->flags |= REQ_F_NOWAIT; |
| 3136 | else if (force_nonblock) |
| 3137 | flags |= MSG_DONTWAIT; |
| 3138 | |
| 3139 | ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags); |
| 3140 | if (force_nonblock && ret == -EAGAIN) |
| 3141 | return -EAGAIN; |
| 3142 | if (ret == -ERESTARTSYS) |
| 3143 | ret = -EINTR; |
| 3144 | } |
| 3145 | |
| 3146 | io_cqring_add_event(req, ret); |
| 3147 | if (ret < 0) |
| 3148 | req_set_fail_links(req); |
| 3149 | io_put_req_find_next(req, nxt); |
| 3150 | return 0; |
| 3151 | #else |
| 3152 | return -EOPNOTSUPP; |
| 3153 | #endif |
| 3154 | } |
| 3155 | |
| 3156 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3157 | 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] | 3158 | { |
| 3159 | #if defined(CONFIG_NET) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3160 | struct io_accept *accept = &req->accept; |
| 3161 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3162 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3163 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 3164 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3165 | return -EINVAL; |
| 3166 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 3167 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3168 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3169 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3170 | return 0; |
| 3171 | #else |
| 3172 | return -EOPNOTSUPP; |
| 3173 | #endif |
| 3174 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3175 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3176 | #if defined(CONFIG_NET) |
| 3177 | static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3178 | bool force_nonblock) |
| 3179 | { |
| 3180 | struct io_accept *accept = &req->accept; |
| 3181 | unsigned file_flags; |
| 3182 | int ret; |
| 3183 | |
| 3184 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3185 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
| 3186 | accept->addr_len, accept->flags); |
| 3187 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3188 | return -EAGAIN; |
Jens Axboe | 8e3cca1 | 2019-11-09 19:52:33 -0700 | [diff] [blame] | 3189 | if (ret == -ERESTARTSYS) |
| 3190 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3191 | if (ret < 0) |
| 3192 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3193 | io_cqring_add_event(req, ret); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3194 | io_put_req_find_next(req, nxt); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3195 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | static void io_accept_finish(struct io_wq_work **workptr) |
| 3199 | { |
| 3200 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3201 | struct io_kiocb *nxt = NULL; |
| 3202 | |
| 3203 | if (io_req_cancelled(req)) |
| 3204 | return; |
| 3205 | __io_accept(req, &nxt, false); |
| 3206 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3207 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3208 | } |
| 3209 | #endif |
| 3210 | |
| 3211 | static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3212 | bool force_nonblock) |
| 3213 | { |
| 3214 | #if defined(CONFIG_NET) |
| 3215 | int ret; |
| 3216 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3217 | ret = __io_accept(req, nxt, force_nonblock); |
| 3218 | if (ret == -EAGAIN && force_nonblock) { |
| 3219 | req->work.func = io_accept_finish; |
| 3220 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 3221 | io_put_req(req); |
| 3222 | return -EAGAIN; |
| 3223 | } |
| 3224 | return 0; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3225 | #else |
| 3226 | return -EOPNOTSUPP; |
| 3227 | #endif |
| 3228 | } |
| 3229 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3230 | 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] | 3231 | { |
| 3232 | #if defined(CONFIG_NET) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3233 | struct io_connect *conn = &req->connect; |
| 3234 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3235 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3236 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3237 | return -EINVAL; |
| 3238 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 3239 | return -EINVAL; |
| 3240 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3241 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3242 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 3243 | |
| 3244 | if (!io) |
| 3245 | return 0; |
| 3246 | |
| 3247 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3248 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3249 | #else |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3250 | return -EOPNOTSUPP; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3251 | #endif |
| 3252 | } |
| 3253 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3254 | static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3255 | bool force_nonblock) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3256 | { |
| 3257 | #if defined(CONFIG_NET) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3258 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3259 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3260 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3261 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3262 | if (req->io) { |
| 3263 | io = req->io; |
| 3264 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3265 | ret = move_addr_to_kernel(req->connect.addr, |
| 3266 | req->connect.addr_len, |
| 3267 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3268 | if (ret) |
| 3269 | goto out; |
| 3270 | io = &__io; |
| 3271 | } |
| 3272 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3273 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3274 | |
| 3275 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 3276 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 3277 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3278 | if (req->io) |
| 3279 | return -EAGAIN; |
| 3280 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3281 | ret = -ENOMEM; |
| 3282 | goto out; |
| 3283 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3284 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3285 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3286 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3287 | if (ret == -ERESTARTSYS) |
| 3288 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3289 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3290 | if (ret < 0) |
| 3291 | req_set_fail_links(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3292 | io_cqring_add_event(req, ret); |
| 3293 | io_put_req_find_next(req, nxt); |
| 3294 | return 0; |
| 3295 | #else |
| 3296 | return -EOPNOTSUPP; |
| 3297 | #endif |
| 3298 | } |
| 3299 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3300 | static void io_poll_remove_one(struct io_kiocb *req) |
| 3301 | { |
| 3302 | struct io_poll_iocb *poll = &req->poll; |
| 3303 | |
| 3304 | spin_lock(&poll->head->lock); |
| 3305 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3306 | if (!list_empty(&poll->wait.entry)) { |
| 3307 | list_del_init(&poll->wait.entry); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3308 | io_queue_async_work(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3309 | } |
| 3310 | spin_unlock(&poll->head->lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3311 | hash_del(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 3315 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3316 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3317 | struct io_kiocb *req; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3318 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3319 | |
| 3320 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3321 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 3322 | struct hlist_head *list; |
| 3323 | |
| 3324 | list = &ctx->cancel_hash[i]; |
| 3325 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
| 3326 | io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3327 | } |
| 3328 | spin_unlock_irq(&ctx->completion_lock); |
| 3329 | } |
| 3330 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3331 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 3332 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3333 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3334 | struct io_kiocb *req; |
| 3335 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3336 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 3337 | hlist_for_each_entry(req, list, hash_node) { |
| 3338 | if (sqe_addr == req->user_data) { |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3339 | io_poll_remove_one(req); |
| 3340 | return 0; |
| 3341 | } |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | return -ENOENT; |
| 3345 | } |
| 3346 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3347 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 3348 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3349 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3350 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3351 | return -EINVAL; |
| 3352 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3353 | sqe->poll_events) |
| 3354 | return -EINVAL; |
| 3355 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3356 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3357 | return 0; |
| 3358 | } |
| 3359 | |
| 3360 | /* |
| 3361 | * Find a running poll command that matches one specified in sqe->addr, |
| 3362 | * and remove it if found. |
| 3363 | */ |
| 3364 | static int io_poll_remove(struct io_kiocb *req) |
| 3365 | { |
| 3366 | struct io_ring_ctx *ctx = req->ctx; |
| 3367 | u64 addr; |
| 3368 | int ret; |
| 3369 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3370 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3371 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3372 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3373 | spin_unlock_irq(&ctx->completion_lock); |
| 3374 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3375 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3376 | if (ret < 0) |
| 3377 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3378 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3379 | return 0; |
| 3380 | } |
| 3381 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3382 | 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] | 3383 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3384 | struct io_ring_ctx *ctx = req->ctx; |
| 3385 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3386 | req->poll.done = true; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3387 | if (error) |
| 3388 | io_cqring_fill_event(req, error); |
| 3389 | else |
| 3390 | io_cqring_fill_event(req, mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3391 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3392 | } |
| 3393 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3394 | static void io_poll_complete_work(struct io_wq_work **workptr) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3395 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3396 | struct io_wq_work *work = *workptr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3397 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 3398 | struct io_poll_iocb *poll = &req->poll; |
| 3399 | struct poll_table_struct pt = { ._key = poll->events }; |
| 3400 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3401 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3402 | __poll_t mask = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3403 | int ret = 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3404 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3405 | if (work->flags & IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3406 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3407 | ret = -ECANCELED; |
| 3408 | } else if (READ_ONCE(poll->canceled)) { |
| 3409 | ret = -ECANCELED; |
| 3410 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3411 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3412 | if (ret != -ECANCELED) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3413 | mask = vfs_poll(poll->file, &pt) & poll->events; |
| 3414 | |
| 3415 | /* |
| 3416 | * Note that ->ki_cancel callers also delete iocb from active_reqs after |
| 3417 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to |
| 3418 | * synchronize with them. In the cancellation case the list_del_init |
| 3419 | * itself is not actually needed, but harmless so we keep it in to |
| 3420 | * avoid further branches in the fast path. |
| 3421 | */ |
| 3422 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3423 | if (!mask && ret != -ECANCELED) { |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3424 | add_wait_queue(poll->head, &poll->wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3425 | spin_unlock_irq(&ctx->completion_lock); |
| 3426 | return; |
| 3427 | } |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3428 | hash_del(&req->hash_node); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3429 | io_poll_complete(req, mask, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3430 | spin_unlock_irq(&ctx->completion_lock); |
| 3431 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3432 | io_cqring_ev_posted(ctx); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3433 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3434 | if (ret < 0) |
| 3435 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3436 | io_put_req_find_next(req, &nxt); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3437 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3438 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3439 | } |
| 3440 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3441 | static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes) |
| 3442 | { |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3443 | struct io_kiocb *req, *tmp; |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3444 | struct req_batch rb; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3445 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 3446 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3447 | spin_lock_irq(&ctx->completion_lock); |
| 3448 | llist_for_each_entry_safe(req, tmp, nodes, llist_node) { |
| 3449 | hash_del(&req->hash_node); |
| 3450 | io_poll_complete(req, req->result, 0); |
| 3451 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3452 | if (refcount_dec_and_test(&req->refs) && |
| 3453 | !io_req_multi_free(&rb, req)) { |
| 3454 | req->flags |= REQ_F_COMP_LOCKED; |
| 3455 | io_free_req(req); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3456 | } |
| 3457 | } |
| 3458 | spin_unlock_irq(&ctx->completion_lock); |
| 3459 | |
| 3460 | io_cqring_ev_posted(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3461 | io_free_req_many(ctx, &rb); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3462 | } |
| 3463 | |
| 3464 | static void io_poll_flush(struct io_wq_work **workptr) |
| 3465 | { |
| 3466 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3467 | struct llist_node *nodes; |
| 3468 | |
| 3469 | nodes = llist_del_all(&req->ctx->poll_llist); |
| 3470 | if (nodes) |
| 3471 | __io_poll_flush(req->ctx, nodes); |
| 3472 | } |
| 3473 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3474 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 3475 | void *key) |
| 3476 | { |
Jens Axboe | e944475 | 2019-11-26 15:02:04 -0700 | [diff] [blame] | 3477 | struct io_poll_iocb *poll = wait->private; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3478 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); |
| 3479 | struct io_ring_ctx *ctx = req->ctx; |
| 3480 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3481 | |
| 3482 | /* for instances that support it check for an event match first: */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3483 | if (mask && !(mask & poll->events)) |
| 3484 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3485 | |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3486 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3487 | |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 3488 | /* |
| 3489 | * Run completion inline if we can. We're using trylock here because |
| 3490 | * we are violating the completion_lock -> poll wq lock ordering. |
| 3491 | * If we have a link timeout we're going to need the completion_lock |
| 3492 | * for finalizing the request, mark us as having grabbed that already. |
| 3493 | */ |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3494 | if (mask) { |
| 3495 | unsigned long flags; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3496 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3497 | if (llist_empty(&ctx->poll_llist) && |
| 3498 | spin_trylock_irqsave(&ctx->completion_lock, flags)) { |
| 3499 | hash_del(&req->hash_node); |
| 3500 | io_poll_complete(req, mask, 0); |
| 3501 | req->flags |= REQ_F_COMP_LOCKED; |
| 3502 | io_put_req(req); |
| 3503 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3504 | |
| 3505 | io_cqring_ev_posted(ctx); |
| 3506 | req = NULL; |
| 3507 | } else { |
| 3508 | req->result = mask; |
| 3509 | req->llist_node.next = NULL; |
| 3510 | /* if the list wasn't empty, we're done */ |
| 3511 | if (!llist_add(&req->llist_node, &ctx->poll_llist)) |
| 3512 | req = NULL; |
| 3513 | else |
| 3514 | req->work.func = io_poll_flush; |
| 3515 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3516 | } |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3517 | if (req) |
| 3518 | io_queue_async_work(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3519 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3520 | return 1; |
| 3521 | } |
| 3522 | |
| 3523 | struct io_poll_table { |
| 3524 | struct poll_table_struct pt; |
| 3525 | struct io_kiocb *req; |
| 3526 | int error; |
| 3527 | }; |
| 3528 | |
| 3529 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 3530 | struct poll_table_struct *p) |
| 3531 | { |
| 3532 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 3533 | |
| 3534 | if (unlikely(pt->req->poll.head)) { |
| 3535 | pt->error = -EINVAL; |
| 3536 | return; |
| 3537 | } |
| 3538 | |
| 3539 | pt->error = 0; |
| 3540 | pt->req->poll.head = head; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3541 | add_wait_queue(head, &pt->req->poll.wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3542 | } |
| 3543 | |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3544 | static void io_poll_req_insert(struct io_kiocb *req) |
| 3545 | { |
| 3546 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3547 | struct hlist_head *list; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3548 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3549 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 3550 | hlist_add_head(&req->hash_node, list); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3551 | } |
| 3552 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3553 | 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] | 3554 | { |
| 3555 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3556 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3557 | |
| 3558 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3559 | return -EINVAL; |
| 3560 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 3561 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3562 | if (!poll->file) |
| 3563 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3564 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3565 | events = READ_ONCE(sqe->poll_events); |
| 3566 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3567 | return 0; |
| 3568 | } |
| 3569 | |
| 3570 | static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3571 | { |
| 3572 | struct io_poll_iocb *poll = &req->poll; |
| 3573 | struct io_ring_ctx *ctx = req->ctx; |
| 3574 | struct io_poll_table ipt; |
| 3575 | bool cancel = false; |
| 3576 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3577 | |
| 3578 | INIT_IO_WORK(&req->work, io_poll_complete_work); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3579 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3580 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3581 | poll->head = NULL; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3582 | poll->done = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3583 | poll->canceled = false; |
| 3584 | |
| 3585 | ipt.pt._qproc = io_poll_queue_proc; |
| 3586 | ipt.pt._key = poll->events; |
| 3587 | ipt.req = req; |
| 3588 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ |
| 3589 | |
| 3590 | /* initialized the list so that we can do list_empty checks */ |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3591 | INIT_LIST_HEAD(&poll->wait.entry); |
| 3592 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); |
| 3593 | poll->wait.private = poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3594 | |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 3595 | INIT_LIST_HEAD(&req->list); |
| 3596 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3597 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3598 | |
| 3599 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3600 | if (likely(poll->head)) { |
| 3601 | spin_lock(&poll->head->lock); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3602 | if (unlikely(list_empty(&poll->wait.entry))) { |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3603 | if (ipt.error) |
| 3604 | cancel = true; |
| 3605 | ipt.error = 0; |
| 3606 | mask = 0; |
| 3607 | } |
| 3608 | if (mask || ipt.error) |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3609 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3610 | else if (cancel) |
| 3611 | WRITE_ONCE(poll->canceled, true); |
| 3612 | else if (!poll->done) /* actually waiting for an event */ |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3613 | io_poll_req_insert(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3614 | spin_unlock(&poll->head->lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3615 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3616 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3617 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3618 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3619 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3620 | spin_unlock_irq(&ctx->completion_lock); |
| 3621 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3622 | if (mask) { |
| 3623 | io_cqring_ev_posted(ctx); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3624 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3625 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3626 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3627 | } |
| 3628 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3629 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 3630 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3631 | struct io_timeout_data *data = container_of(timer, |
| 3632 | struct io_timeout_data, timer); |
| 3633 | struct io_kiocb *req = data->req; |
| 3634 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3635 | unsigned long flags; |
| 3636 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3637 | atomic_inc(&ctx->cq_timeouts); |
| 3638 | |
| 3639 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3640 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3641 | * We could be racing with timeout deletion. If the list is empty, |
| 3642 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3643 | */ |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3644 | if (!list_empty(&req->list)) { |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3645 | struct io_kiocb *prev; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3646 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3647 | /* |
| 3648 | * Adjust the reqs sequence before the current one because it |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 3649 | * will consume a slot in the cq_ring and the cq_tail |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3650 | * pointer will be increased, otherwise other timeout reqs may |
| 3651 | * return in advance without waiting for enough wait_nr. |
| 3652 | */ |
| 3653 | prev = req; |
| 3654 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 3655 | prev->sequence++; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3656 | list_del_init(&req->list); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3657 | } |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3658 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3659 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3660 | io_commit_cqring(ctx); |
| 3661 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3662 | |
| 3663 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3664 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3665 | io_put_req(req); |
| 3666 | return HRTIMER_NORESTART; |
| 3667 | } |
| 3668 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3669 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 3670 | { |
| 3671 | struct io_kiocb *req; |
| 3672 | int ret = -ENOENT; |
| 3673 | |
| 3674 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 3675 | if (user_data == req->user_data) { |
| 3676 | list_del_init(&req->list); |
| 3677 | ret = 0; |
| 3678 | break; |
| 3679 | } |
| 3680 | } |
| 3681 | |
| 3682 | if (ret == -ENOENT) |
| 3683 | return ret; |
| 3684 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3685 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3686 | if (ret == -1) |
| 3687 | return -EALREADY; |
| 3688 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3689 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3690 | io_cqring_fill_event(req, -ECANCELED); |
| 3691 | io_put_req(req); |
| 3692 | return 0; |
| 3693 | } |
| 3694 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3695 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 3696 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3697 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3698 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3699 | return -EINVAL; |
| 3700 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 3701 | return -EINVAL; |
| 3702 | |
| 3703 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 3704 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 3705 | if (req->timeout.flags) |
| 3706 | return -EINVAL; |
| 3707 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3708 | return 0; |
| 3709 | } |
| 3710 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3711 | /* |
| 3712 | * Remove or update an existing timeout command |
| 3713 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3714 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3715 | { |
| 3716 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3717 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3718 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3719 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3720 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3721 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3722 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3723 | io_commit_cqring(ctx); |
| 3724 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3725 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3726 | if (ret < 0) |
| 3727 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3728 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3729 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3730 | } |
| 3731 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3732 | 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] | 3733 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3734 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3735 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3736 | unsigned flags; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3737 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3738 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3739 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3740 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3741 | return -EINVAL; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3742 | if (sqe->off && is_timeout_link) |
| 3743 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3744 | flags = READ_ONCE(sqe->timeout_flags); |
| 3745 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3746 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 3747 | |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3748 | req->timeout.count = READ_ONCE(sqe->off); |
| 3749 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3750 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3751 | return -ENOMEM; |
| 3752 | |
| 3753 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3754 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3755 | req->flags |= REQ_F_TIMEOUT; |
| 3756 | |
| 3757 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3758 | return -EFAULT; |
| 3759 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3760 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3761 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3762 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3763 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3764 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3765 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 3766 | return 0; |
| 3767 | } |
| 3768 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3769 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3770 | { |
| 3771 | unsigned count; |
| 3772 | struct io_ring_ctx *ctx = req->ctx; |
| 3773 | struct io_timeout_data *data; |
| 3774 | struct list_head *entry; |
| 3775 | unsigned span = 0; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3776 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3777 | data = &req->io->timeout; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3778 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3779 | /* |
| 3780 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3781 | * timeout event to be satisfied. If it isn't set, then this is |
| 3782 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3783 | */ |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3784 | count = req->timeout.count; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3785 | if (!count) { |
| 3786 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 3787 | spin_lock_irq(&ctx->completion_lock); |
| 3788 | entry = ctx->timeout_list.prev; |
| 3789 | goto add; |
| 3790 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3791 | |
| 3792 | req->sequence = ctx->cached_sq_head + count - 1; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3793 | data->seq_offset = count; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3794 | |
| 3795 | /* |
| 3796 | * Insertion sort, ensuring the first entry in the list is always |
| 3797 | * the one we need first. |
| 3798 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3799 | spin_lock_irq(&ctx->completion_lock); |
| 3800 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 3801 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3802 | unsigned nxt_sq_head; |
| 3803 | long long tmp, tmp_nxt; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3804 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3805 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3806 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 3807 | continue; |
| 3808 | |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3809 | /* |
| 3810 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 3811 | * long to store it. |
| 3812 | */ |
| 3813 | tmp = (long long)ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 3814 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
| 3815 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3816 | |
| 3817 | /* |
| 3818 | * cached_sq_head may overflow, and it will never overflow twice |
| 3819 | * once there is some timeout req still be valid. |
| 3820 | */ |
| 3821 | if (ctx->cached_sq_head < nxt_sq_head) |
yangerkun | 8b07a65 | 2019-10-17 12:12:35 +0800 | [diff] [blame] | 3822 | tmp += UINT_MAX; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3823 | |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3824 | if (tmp > tmp_nxt) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3825 | break; |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3826 | |
| 3827 | /* |
| 3828 | * Sequence of reqs after the insert one and itself should |
| 3829 | * be adjusted because each timeout req consumes a slot. |
| 3830 | */ |
| 3831 | span++; |
| 3832 | nxt->sequence++; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3833 | } |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3834 | req->sequence -= span; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3835 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3836 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3837 | data->timer.function = io_timeout_fn; |
| 3838 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3839 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3840 | return 0; |
| 3841 | } |
| 3842 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3843 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3844 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3845 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3846 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3847 | return req->user_data == (unsigned long) data; |
| 3848 | } |
| 3849 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3850 | 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] | 3851 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3852 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3853 | int ret = 0; |
| 3854 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3855 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
| 3856 | switch (cancel_ret) { |
| 3857 | case IO_WQ_CANCEL_OK: |
| 3858 | ret = 0; |
| 3859 | break; |
| 3860 | case IO_WQ_CANCEL_RUNNING: |
| 3861 | ret = -EALREADY; |
| 3862 | break; |
| 3863 | case IO_WQ_CANCEL_NOTFOUND: |
| 3864 | ret = -ENOENT; |
| 3865 | break; |
| 3866 | } |
| 3867 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3868 | return ret; |
| 3869 | } |
| 3870 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3871 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 3872 | struct io_kiocb *req, __u64 sqe_addr, |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3873 | struct io_kiocb **nxt, int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3874 | { |
| 3875 | unsigned long flags; |
| 3876 | int ret; |
| 3877 | |
| 3878 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 3879 | if (ret != -ENOENT) { |
| 3880 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3881 | goto done; |
| 3882 | } |
| 3883 | |
| 3884 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3885 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 3886 | if (ret != -ENOENT) |
| 3887 | goto done; |
| 3888 | ret = io_poll_cancel(ctx, sqe_addr); |
| 3889 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3890 | if (!ret) |
| 3891 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3892 | io_cqring_fill_event(req, ret); |
| 3893 | io_commit_cqring(ctx); |
| 3894 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3895 | io_cqring_ev_posted(ctx); |
| 3896 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3897 | if (ret < 0) |
| 3898 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3899 | io_put_req_find_next(req, nxt); |
| 3900 | } |
| 3901 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3902 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 3903 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3904 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3905 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3906 | return -EINVAL; |
| 3907 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 3908 | sqe->cancel_flags) |
| 3909 | return -EINVAL; |
| 3910 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3911 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 3912 | return 0; |
| 3913 | } |
| 3914 | |
| 3915 | static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3916 | { |
| 3917 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3918 | |
| 3919 | io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3920 | return 0; |
| 3921 | } |
| 3922 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 3923 | static int io_files_update_prep(struct io_kiocb *req, |
| 3924 | const struct io_uring_sqe *sqe) |
| 3925 | { |
| 3926 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) |
| 3927 | return -EINVAL; |
| 3928 | |
| 3929 | req->files_update.offset = READ_ONCE(sqe->off); |
| 3930 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 3931 | if (!req->files_update.nr_args) |
| 3932 | return -EINVAL; |
| 3933 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 3934 | return 0; |
| 3935 | } |
| 3936 | |
| 3937 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) |
| 3938 | { |
| 3939 | struct io_ring_ctx *ctx = req->ctx; |
| 3940 | struct io_uring_files_update up; |
| 3941 | int ret; |
| 3942 | |
| 3943 | if (force_nonblock) { |
| 3944 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 3945 | return -EAGAIN; |
| 3946 | } |
| 3947 | |
| 3948 | up.offset = req->files_update.offset; |
| 3949 | up.fds = req->files_update.arg; |
| 3950 | |
| 3951 | mutex_lock(&ctx->uring_lock); |
| 3952 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 3953 | mutex_unlock(&ctx->uring_lock); |
| 3954 | |
| 3955 | if (ret < 0) |
| 3956 | req_set_fail_links(req); |
| 3957 | io_cqring_add_event(req, ret); |
| 3958 | io_put_req(req); |
| 3959 | return 0; |
| 3960 | } |
| 3961 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3962 | static int io_req_defer_prep(struct io_kiocb *req, |
| 3963 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3964 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3965 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3966 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3967 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3968 | case IORING_OP_NOP: |
| 3969 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3970 | case IORING_OP_READV: |
| 3971 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3972 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3973 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3974 | break; |
| 3975 | case IORING_OP_WRITEV: |
| 3976 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3977 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3978 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3979 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3980 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3981 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3982 | break; |
| 3983 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3984 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3985 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3986 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3987 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3988 | break; |
| 3989 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3990 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3991 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3992 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3993 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3994 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3995 | break; |
| 3996 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3997 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3998 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3999 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4000 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4001 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 4002 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4003 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4004 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4005 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4006 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4007 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4008 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4009 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4010 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4011 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4012 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4013 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4014 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4015 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4016 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4017 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4018 | case IORING_OP_FALLOCATE: |
| 4019 | ret = io_fallocate_prep(req, sqe); |
| 4020 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4021 | case IORING_OP_OPENAT: |
| 4022 | ret = io_openat_prep(req, sqe); |
| 4023 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4024 | case IORING_OP_CLOSE: |
| 4025 | ret = io_close_prep(req, sqe); |
| 4026 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4027 | case IORING_OP_FILES_UPDATE: |
| 4028 | ret = io_files_update_prep(req, sqe); |
| 4029 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4030 | case IORING_OP_STATX: |
| 4031 | ret = io_statx_prep(req, sqe); |
| 4032 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4033 | case IORING_OP_FADVISE: |
| 4034 | ret = io_fadvise_prep(req, sqe); |
| 4035 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4036 | case IORING_OP_MADVISE: |
| 4037 | ret = io_madvise_prep(req, sqe); |
| 4038 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4039 | case IORING_OP_OPENAT2: |
| 4040 | ret = io_openat2_prep(req, sqe); |
| 4041 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4042 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4043 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 4044 | req->opcode); |
| 4045 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4046 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4047 | } |
| 4048 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4049 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4050 | } |
| 4051 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4052 | 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] | 4053 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4054 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4055 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4056 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4057 | /* Still need defer if there is pending req in defer list. */ |
| 4058 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4059 | return 0; |
| 4060 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4061 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4062 | return -EAGAIN; |
| 4063 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4064 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4065 | if (ret < 0) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4066 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4067 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4068 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4069 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4070 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4071 | return 0; |
| 4072 | } |
| 4073 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 4074 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4075 | list_add_tail(&req->list, &ctx->defer_list); |
| 4076 | spin_unlock_irq(&ctx->completion_lock); |
| 4077 | return -EIOCBQUEUED; |
| 4078 | } |
| 4079 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4080 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4081 | struct io_kiocb **nxt, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [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 | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4084 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4085 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4086 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4087 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4088 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4089 | break; |
| 4090 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4091 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4092 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4093 | if (sqe) { |
| 4094 | ret = io_read_prep(req, sqe, force_nonblock); |
| 4095 | if (ret < 0) |
| 4096 | break; |
| 4097 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4098 | ret = io_read(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4099 | break; |
| 4100 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4101 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4102 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4103 | if (sqe) { |
| 4104 | ret = io_write_prep(req, sqe, force_nonblock); |
| 4105 | if (ret < 0) |
| 4106 | break; |
| 4107 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4108 | ret = io_write(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4109 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4110 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4111 | if (sqe) { |
| 4112 | ret = io_prep_fsync(req, sqe); |
| 4113 | if (ret < 0) |
| 4114 | break; |
| 4115 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4116 | ret = io_fsync(req, nxt, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4117 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4118 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4119 | if (sqe) { |
| 4120 | ret = io_poll_add_prep(req, sqe); |
| 4121 | if (ret) |
| 4122 | break; |
| 4123 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4124 | ret = io_poll_add(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4125 | break; |
| 4126 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4127 | if (sqe) { |
| 4128 | ret = io_poll_remove_prep(req, sqe); |
| 4129 | if (ret < 0) |
| 4130 | break; |
| 4131 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4132 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4133 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4134 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4135 | if (sqe) { |
| 4136 | ret = io_prep_sfr(req, sqe); |
| 4137 | if (ret < 0) |
| 4138 | break; |
| 4139 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4140 | ret = io_sync_file_range(req, nxt, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4141 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4142 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4143 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4144 | if (sqe) { |
| 4145 | ret = io_sendmsg_prep(req, sqe); |
| 4146 | if (ret < 0) |
| 4147 | break; |
| 4148 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4149 | if (req->opcode == IORING_OP_SENDMSG) |
| 4150 | ret = io_sendmsg(req, nxt, force_nonblock); |
| 4151 | else |
| 4152 | ret = io_send(req, nxt, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4153 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4154 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4155 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4156 | if (sqe) { |
| 4157 | ret = io_recvmsg_prep(req, sqe); |
| 4158 | if (ret) |
| 4159 | break; |
| 4160 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4161 | if (req->opcode == IORING_OP_RECVMSG) |
| 4162 | ret = io_recvmsg(req, nxt, force_nonblock); |
| 4163 | else |
| 4164 | ret = io_recv(req, nxt, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4165 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4166 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4167 | if (sqe) { |
| 4168 | ret = io_timeout_prep(req, sqe, false); |
| 4169 | if (ret) |
| 4170 | break; |
| 4171 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4172 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4173 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4174 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4175 | if (sqe) { |
| 4176 | ret = io_timeout_remove_prep(req, sqe); |
| 4177 | if (ret) |
| 4178 | break; |
| 4179 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4180 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4181 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4182 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4183 | if (sqe) { |
| 4184 | ret = io_accept_prep(req, sqe); |
| 4185 | if (ret) |
| 4186 | break; |
| 4187 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4188 | ret = io_accept(req, nxt, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4189 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4190 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4191 | if (sqe) { |
| 4192 | ret = io_connect_prep(req, sqe); |
| 4193 | if (ret) |
| 4194 | break; |
| 4195 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4196 | ret = io_connect(req, nxt, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4197 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4198 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4199 | if (sqe) { |
| 4200 | ret = io_async_cancel_prep(req, sqe); |
| 4201 | if (ret) |
| 4202 | break; |
| 4203 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4204 | ret = io_async_cancel(req, nxt); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4205 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4206 | case IORING_OP_FALLOCATE: |
| 4207 | if (sqe) { |
| 4208 | ret = io_fallocate_prep(req, sqe); |
| 4209 | if (ret) |
| 4210 | break; |
| 4211 | } |
| 4212 | ret = io_fallocate(req, nxt, force_nonblock); |
| 4213 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4214 | case IORING_OP_OPENAT: |
| 4215 | if (sqe) { |
| 4216 | ret = io_openat_prep(req, sqe); |
| 4217 | if (ret) |
| 4218 | break; |
| 4219 | } |
| 4220 | ret = io_openat(req, nxt, force_nonblock); |
| 4221 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4222 | case IORING_OP_CLOSE: |
| 4223 | if (sqe) { |
| 4224 | ret = io_close_prep(req, sqe); |
| 4225 | if (ret) |
| 4226 | break; |
| 4227 | } |
| 4228 | ret = io_close(req, nxt, force_nonblock); |
| 4229 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4230 | case IORING_OP_FILES_UPDATE: |
| 4231 | if (sqe) { |
| 4232 | ret = io_files_update_prep(req, sqe); |
| 4233 | if (ret) |
| 4234 | break; |
| 4235 | } |
| 4236 | ret = io_files_update(req, force_nonblock); |
| 4237 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4238 | case IORING_OP_STATX: |
| 4239 | if (sqe) { |
| 4240 | ret = io_statx_prep(req, sqe); |
| 4241 | if (ret) |
| 4242 | break; |
| 4243 | } |
| 4244 | ret = io_statx(req, nxt, force_nonblock); |
| 4245 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4246 | case IORING_OP_FADVISE: |
| 4247 | if (sqe) { |
| 4248 | ret = io_fadvise_prep(req, sqe); |
| 4249 | if (ret) |
| 4250 | break; |
| 4251 | } |
| 4252 | ret = io_fadvise(req, nxt, force_nonblock); |
| 4253 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4254 | case IORING_OP_MADVISE: |
| 4255 | if (sqe) { |
| 4256 | ret = io_madvise_prep(req, sqe); |
| 4257 | if (ret) |
| 4258 | break; |
| 4259 | } |
| 4260 | ret = io_madvise(req, nxt, force_nonblock); |
| 4261 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4262 | case IORING_OP_OPENAT2: |
| 4263 | if (sqe) { |
| 4264 | ret = io_openat2_prep(req, sqe); |
| 4265 | if (ret) |
| 4266 | break; |
| 4267 | } |
| 4268 | ret = io_openat2(req, nxt, force_nonblock); |
| 4269 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4270 | default: |
| 4271 | ret = -EINVAL; |
| 4272 | break; |
| 4273 | } |
| 4274 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4275 | if (ret) |
| 4276 | return ret; |
| 4277 | |
| 4278 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4279 | const bool in_async = io_wq_current_is_worker(); |
| 4280 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4281 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4282 | return -EAGAIN; |
| 4283 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4284 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 4285 | if (in_async) |
| 4286 | mutex_lock(&ctx->uring_lock); |
| 4287 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4288 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4289 | |
| 4290 | if (in_async) |
| 4291 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4292 | } |
| 4293 | |
| 4294 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4295 | } |
| 4296 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4297 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4298 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4299 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4300 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4301 | struct io_kiocb *nxt = NULL; |
| 4302 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4303 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4304 | /* if NO_CANCEL is set, we must still run the work */ |
| 4305 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 4306 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4307 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4308 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4309 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4310 | if (!ret) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4311 | req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0; |
| 4312 | req->in_async = true; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4313 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4314 | ret = io_issue_sqe(req, NULL, &nxt, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4315 | /* |
| 4316 | * We can get EAGAIN for polled IO even though we're |
| 4317 | * forcing a sync submission from here, since we can't |
| 4318 | * wait for request slots on the block side. |
| 4319 | */ |
| 4320 | if (ret != -EAGAIN) |
| 4321 | break; |
| 4322 | cond_resched(); |
| 4323 | } while (1); |
| 4324 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4325 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4326 | /* drop submission reference */ |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 4327 | io_put_req(req); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4328 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4329 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4330 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4331 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4332 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4333 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4334 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4335 | /* if a dependent link is ready, pass it back */ |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4336 | if (!ret && nxt) |
| 4337 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4338 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4339 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4340 | static int io_req_needs_file(struct io_kiocb *req, int fd) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4341 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4342 | if (!io_op_defs[req->opcode].needs_file) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4343 | return 0; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4344 | if (fd == -1 && io_op_defs[req->opcode].fd_non_neg) |
| 4345 | return 0; |
| 4346 | return 1; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4347 | } |
| 4348 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4349 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 4350 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4351 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4352 | struct fixed_file_table *table; |
| 4353 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4354 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
| 4355 | return table->files[index & IORING_FILE_TABLE_MASK];; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4356 | } |
| 4357 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4358 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
| 4359 | const struct io_uring_sqe *sqe) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4360 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4361 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4362 | unsigned flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4363 | int fd; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4364 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4365 | flags = READ_ONCE(sqe->flags); |
| 4366 | fd = READ_ONCE(sqe->fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4367 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4368 | if (!io_req_needs_file(req, fd)) |
| 4369 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4370 | |
| 4371 | if (flags & IOSQE_FIXED_FILE) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4372 | if (unlikely(!ctx->file_data || |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4373 | (unsigned) fd >= ctx->nr_user_files)) |
| 4374 | return -EBADF; |
Jens Axboe | b762012 | 2019-10-26 07:22:55 -0600 | [diff] [blame] | 4375 | fd = array_index_nospec(fd, ctx->nr_user_files); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4376 | req->file = io_file_from_index(ctx, fd); |
| 4377 | if (!req->file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4378 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4379 | req->flags |= REQ_F_FIXED_FILE; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4380 | percpu_ref_get(&ctx->file_data->refs); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4381 | } else { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4382 | if (req->needs_fixed_file) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4383 | return -EBADF; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 4384 | trace_io_uring_file_get(ctx, fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4385 | req->file = io_file_get(state, fd); |
| 4386 | if (unlikely(!req->file)) |
| 4387 | return -EBADF; |
| 4388 | } |
| 4389 | |
| 4390 | return 0; |
| 4391 | } |
| 4392 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4393 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4394 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4395 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4396 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4397 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4398 | if (!ctx->ring_file) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4399 | return -EBADF; |
| 4400 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4401 | rcu_read_lock(); |
| 4402 | spin_lock_irq(&ctx->inflight_lock); |
| 4403 | /* |
| 4404 | * We use the f_ops->flush() handler to ensure that we can flush |
| 4405 | * out work accessing these files if the fd is closed. Check if |
| 4406 | * the fd has changed since we started down this path, and disallow |
| 4407 | * this operation if it has. |
| 4408 | */ |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4409 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4410 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 4411 | req->flags |= REQ_F_INFLIGHT; |
| 4412 | req->work.files = current->files; |
| 4413 | ret = 0; |
| 4414 | } |
| 4415 | spin_unlock_irq(&ctx->inflight_lock); |
| 4416 | rcu_read_unlock(); |
| 4417 | |
| 4418 | return ret; |
| 4419 | } |
| 4420 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4421 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 4422 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4423 | struct io_timeout_data *data = container_of(timer, |
| 4424 | struct io_timeout_data, timer); |
| 4425 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4426 | struct io_ring_ctx *ctx = req->ctx; |
| 4427 | struct io_kiocb *prev = NULL; |
| 4428 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4429 | |
| 4430 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4431 | |
| 4432 | /* |
| 4433 | * We don't expect the list to be empty, that will only happen if we |
| 4434 | * race with the completion of the linked work. |
| 4435 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4436 | if (!list_empty(&req->link_list)) { |
| 4437 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 4438 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4439 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4440 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4441 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 4442 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4443 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4444 | } |
| 4445 | |
| 4446 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4447 | |
| 4448 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4449 | req_set_fail_links(prev); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4450 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
| 4451 | -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4452 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4453 | } else { |
| 4454 | io_cqring_add_event(req, -ETIME); |
| 4455 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4456 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4457 | return HRTIMER_NORESTART; |
| 4458 | } |
| 4459 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4460 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4461 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4462 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4463 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4464 | /* |
| 4465 | * If the list is now empty, then our linked request finished before |
| 4466 | * we got a chance to setup the timer |
| 4467 | */ |
| 4468 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4469 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4470 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4471 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4472 | data->timer.function = io_link_timeout_fn; |
| 4473 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 4474 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4475 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4476 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4477 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4478 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4479 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4480 | } |
| 4481 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4482 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4483 | { |
| 4484 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4485 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4486 | if (!(req->flags & REQ_F_LINK)) |
| 4487 | return NULL; |
| 4488 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4489 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 4490 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4491 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4492 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4493 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4494 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4495 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4496 | } |
| 4497 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4498 | 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] | 4499 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4500 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4501 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4502 | int ret; |
| 4503 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4504 | again: |
| 4505 | linked_timeout = io_prep_linked_timeout(req); |
| 4506 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4507 | ret = io_issue_sqe(req, sqe, &nxt, true); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 4508 | |
| 4509 | /* |
| 4510 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 4511 | * doesn't support non-blocking read/write attempts |
| 4512 | */ |
| 4513 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 4514 | (req->flags & REQ_F_MUST_PUNT))) { |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame^] | 4515 | punt: |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4516 | if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) { |
| 4517 | ret = io_grab_files(req); |
| 4518 | if (ret) |
| 4519 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4520 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4521 | |
| 4522 | /* |
| 4523 | * Queued up for async execution, worker will release |
| 4524 | * submit reference when the iocb is actually submitted. |
| 4525 | */ |
| 4526 | io_queue_async_work(req); |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4527 | goto done_req; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4528 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4529 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4530 | err: |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4531 | /* drop submission reference */ |
| 4532 | io_put_req(req); |
| 4533 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4534 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4535 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4536 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4537 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4538 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4539 | } |
| 4540 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4541 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4542 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4543 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4544 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4545 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4546 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4547 | done_req: |
| 4548 | if (nxt) { |
| 4549 | req = nxt; |
| 4550 | nxt = NULL; |
Pavel Begunkov | 86a761f | 2020-01-22 23:09:36 +0300 | [diff] [blame^] | 4551 | |
| 4552 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 4553 | goto punt; |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4554 | goto again; |
| 4555 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4556 | } |
| 4557 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4558 | 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] | 4559 | { |
| 4560 | int ret; |
| 4561 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4562 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4563 | if (ret) { |
| 4564 | if (ret != -EIOCBQUEUED) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 4565 | fail_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4566 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4567 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4568 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4569 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 4570 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | 1118591 | 2020-01-22 23:09:35 +0300 | [diff] [blame] | 4571 | ret = io_req_defer_prep(req, sqe); |
| 4572 | if (unlikely(ret < 0)) |
| 4573 | goto fail_req; |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4574 | /* |
| 4575 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 4576 | * to async execution. |
| 4577 | */ |
| 4578 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 4579 | io_queue_async_work(req); |
| 4580 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4581 | __io_queue_sqe(req, sqe); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4582 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4583 | } |
| 4584 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4585 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4586 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4587 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4588 | io_cqring_add_event(req, -ECANCELED); |
| 4589 | io_double_put_req(req); |
| 4590 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4591 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4592 | } |
| 4593 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4594 | #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] | 4595 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4596 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4597 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4598 | struct io_submit_state *state, struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4599 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4600 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4601 | unsigned int sqe_flags; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4602 | int ret; |
| 4603 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4604 | sqe_flags = READ_ONCE(sqe->flags); |
| 4605 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4606 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4607 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4608 | ret = -EINVAL; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4609 | goto err_req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4610 | } |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 4611 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 4612 | req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK| |
| 4613 | IOSQE_ASYNC); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4614 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4615 | ret = io_req_set_file(state, req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4616 | if (unlikely(ret)) { |
| 4617 | err_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4618 | io_cqring_add_event(req, ret); |
| 4619 | io_double_put_req(req); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4620 | return false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4621 | } |
| 4622 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4623 | /* |
| 4624 | * If we already have a head request, queue this one for async |
| 4625 | * submittal once the head completes. If we don't have a head but |
| 4626 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 4627 | * submitted sync once the chain is complete. If none of those |
| 4628 | * conditions are true (normal request), then just queue it. |
| 4629 | */ |
| 4630 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4631 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4632 | |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4633 | if (sqe_flags & IOSQE_IO_DRAIN) { |
| 4634 | head->flags |= REQ_F_IO_DRAIN; |
| 4635 | ctx->drain_next = 1; |
| 4636 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4637 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4638 | ret = -EAGAIN; |
| 4639 | goto err_req; |
| 4640 | } |
| 4641 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4642 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4643 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4644 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4645 | head->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4646 | goto err_req; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4647 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4648 | trace_io_uring_link(ctx, req, head); |
| 4649 | list_add_tail(&req->link_list, &head->link_list); |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4650 | |
| 4651 | /* last request of a link, enqueue the link */ |
| 4652 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) { |
| 4653 | io_queue_link_head(head); |
| 4654 | *link = NULL; |
| 4655 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4656 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4657 | if (unlikely(ctx->drain_next)) { |
| 4658 | req->flags |= REQ_F_IO_DRAIN; |
| 4659 | req->ctx->drain_next = 0; |
| 4660 | } |
| 4661 | if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { |
| 4662 | req->flags |= REQ_F_LINK; |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4663 | INIT_LIST_HEAD(&req->link_list); |
| 4664 | ret = io_req_defer_prep(req, sqe); |
| 4665 | if (ret) |
| 4666 | req->flags |= REQ_F_FAIL_LINK; |
| 4667 | *link = req; |
| 4668 | } else { |
| 4669 | io_queue_sqe(req, sqe); |
| 4670 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4671 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4672 | |
| 4673 | return true; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4674 | } |
| 4675 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4676 | /* |
| 4677 | * Batched submission is done, ensure local IO is flushed out. |
| 4678 | */ |
| 4679 | static void io_submit_state_end(struct io_submit_state *state) |
| 4680 | { |
| 4681 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 4682 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4683 | if (state->free_reqs) |
| 4684 | kmem_cache_free_bulk(req_cachep, state->free_reqs, |
| 4685 | &state->reqs[state->cur_req]); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4686 | } |
| 4687 | |
| 4688 | /* |
| 4689 | * Start submission side cache. |
| 4690 | */ |
| 4691 | static void io_submit_state_start(struct io_submit_state *state, |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4692 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4693 | { |
| 4694 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4695 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4696 | state->file = NULL; |
| 4697 | state->ios_left = max_ios; |
| 4698 | } |
| 4699 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4700 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 4701 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4702 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4703 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 4704 | /* |
| 4705 | * Ensure any loads from the SQEs are done at this point, |
| 4706 | * since once we write the new head, the application could |
| 4707 | * write new data to them. |
| 4708 | */ |
| 4709 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4713 | * 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] | 4714 | * that is mapped by userspace. This means that care needs to be taken to |
| 4715 | * ensure that reads are stable, as we cannot rely on userspace always |
| 4716 | * being a good citizen. If members of the sqe are validated and then later |
| 4717 | * used, it's important that those reads are done through READ_ONCE() to |
| 4718 | * prevent a re-load down the line. |
| 4719 | */ |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4720 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 4721 | const struct io_uring_sqe **sqe_ptr) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4722 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4723 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4724 | unsigned head; |
| 4725 | |
| 4726 | /* |
| 4727 | * The cached sq head (or cq tail) serves two purposes: |
| 4728 | * |
| 4729 | * 1) allows us to batch the cost of updating the user visible |
| 4730 | * head updates. |
| 4731 | * 2) allows the kernel side to track the head on its own, even |
| 4732 | * though the application is the one updating it. |
| 4733 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4734 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 4735 | if (likely(head < ctx->sq_entries)) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4736 | /* |
| 4737 | * All io need record the previous position, if LINK vs DARIN, |
| 4738 | * it can be used to mark the position of the first IO in the |
| 4739 | * link list. |
| 4740 | */ |
| 4741 | req->sequence = ctx->cached_sq_head; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4742 | *sqe_ptr = &ctx->sq_sqes[head]; |
| 4743 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); |
| 4744 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4745 | ctx->cached_sq_head++; |
| 4746 | return true; |
| 4747 | } |
| 4748 | |
| 4749 | /* drop invalid entries */ |
| 4750 | ctx->cached_sq_head++; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 4751 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4752 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4753 | return false; |
| 4754 | } |
| 4755 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4756 | 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] | 4757 | struct file *ring_file, int ring_fd, |
| 4758 | struct mm_struct **mm, bool async) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4759 | { |
| 4760 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4761 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4762 | int i, submitted = 0; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4763 | bool mm_fault = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4764 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 4765 | /* 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] | 4766 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 4767 | if (!list_empty(&ctx->cq_overflow_list) && |
| 4768 | !io_cqring_overflow_flush(ctx, false)) |
| 4769 | return -EBUSY; |
| 4770 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4771 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4772 | /* make sure SQ entry isn't read before tail */ |
| 4773 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 4774 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4775 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 4776 | return -EAGAIN; |
| 4777 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4778 | if (nr > IO_PLUG_THRESHOLD) { |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4779 | io_submit_state_start(&state, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4780 | statep = &state; |
| 4781 | } |
| 4782 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4783 | ctx->ring_fd = ring_fd; |
| 4784 | ctx->ring_file = ring_file; |
| 4785 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4786 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4787 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4788 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4789 | |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4790 | req = io_get_req(ctx, statep); |
| 4791 | if (unlikely(!req)) { |
| 4792 | if (!submitted) |
| 4793 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4794 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4795 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4796 | if (!io_get_sqring(ctx, req, &sqe)) { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4797 | __io_req_do_free(req); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4798 | break; |
| 4799 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4800 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4801 | /* will complete beyond this point, count as submitted */ |
| 4802 | submitted++; |
| 4803 | |
| 4804 | if (unlikely(req->opcode >= IORING_OP_LAST)) { |
| 4805 | io_cqring_add_event(req, -EINVAL); |
| 4806 | io_double_put_req(req); |
| 4807 | break; |
| 4808 | } |
| 4809 | |
| 4810 | if (io_op_defs[req->opcode].needs_mm && !*mm) { |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4811 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
| 4812 | if (!mm_fault) { |
| 4813 | use_mm(ctx->sqo_mm); |
| 4814 | *mm = ctx->sqo_mm; |
| 4815 | } |
| 4816 | } |
| 4817 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4818 | req->has_user = *mm != NULL; |
| 4819 | req->in_async = async; |
| 4820 | req->needs_fixed_file = async; |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 4821 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 4822 | true, async); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4823 | if (!io_submit_sqe(req, sqe, statep, &link)) |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4824 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4825 | } |
| 4826 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4827 | if (submitted != nr) |
| 4828 | percpu_ref_put_many(&ctx->refs, nr - submitted); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4829 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4830 | io_queue_link_head(link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4831 | if (statep) |
| 4832 | io_submit_state_end(&state); |
| 4833 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 4834 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 4835 | io_commit_sqring(ctx); |
| 4836 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4837 | return submitted; |
| 4838 | } |
| 4839 | |
| 4840 | static int io_sq_thread(void *data) |
| 4841 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4842 | struct io_ring_ctx *ctx = data; |
| 4843 | struct mm_struct *cur_mm = NULL; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4844 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4845 | mm_segment_t old_fs; |
| 4846 | DEFINE_WAIT(wait); |
| 4847 | unsigned inflight; |
| 4848 | unsigned long timeout; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4849 | int ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4850 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 4851 | complete(&ctx->completions[1]); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 4852 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4853 | old_fs = get_fs(); |
| 4854 | set_fs(USER_DS); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4855 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4856 | |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4857 | ret = timeout = inflight = 0; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4858 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4859 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4860 | |
| 4861 | if (inflight) { |
| 4862 | unsigned nr_events = 0; |
| 4863 | |
| 4864 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 4865 | /* |
| 4866 | * inflight is the count of the maximum possible |
| 4867 | * entries we submitted, but it can be smaller |
| 4868 | * if we dropped some of them. If we don't have |
| 4869 | * poll entries available, then we know that we |
| 4870 | * have nothing left to poll for. Reset the |
| 4871 | * inflight count to zero in that case. |
| 4872 | */ |
| 4873 | mutex_lock(&ctx->uring_lock); |
| 4874 | if (!list_empty(&ctx->poll_list)) |
| 4875 | __io_iopoll_check(ctx, &nr_events, 0); |
| 4876 | else |
| 4877 | inflight = 0; |
| 4878 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4879 | } else { |
| 4880 | /* |
| 4881 | * Normal IO, just pretend everything completed. |
| 4882 | * We don't have to poll completions for that. |
| 4883 | */ |
| 4884 | nr_events = inflight; |
| 4885 | } |
| 4886 | |
| 4887 | inflight -= nr_events; |
| 4888 | if (!inflight) |
| 4889 | timeout = jiffies + ctx->sq_thread_idle; |
| 4890 | } |
| 4891 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4892 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4893 | |
| 4894 | /* |
| 4895 | * If submit got -EBUSY, flag us as needing the application |
| 4896 | * to enter the kernel to reap and flush events. |
| 4897 | */ |
| 4898 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4899 | /* |
| 4900 | * We're polling. If we're within the defined idle |
| 4901 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4902 | * to sleep. The exception is if we got EBUSY doing |
| 4903 | * more IO, we should wait for the application to |
| 4904 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4905 | */ |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4906 | if (inflight || |
| 4907 | (!time_after(jiffies, timeout) && ret != -EBUSY)) { |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 4908 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4909 | continue; |
| 4910 | } |
| 4911 | |
| 4912 | /* |
| 4913 | * Drop cur_mm before scheduling, we can't hold it for |
| 4914 | * long periods (or over schedule()). Do this before |
| 4915 | * adding ourselves to the waitqueue, as the unuse/drop |
| 4916 | * may sleep. |
| 4917 | */ |
| 4918 | if (cur_mm) { |
| 4919 | unuse_mm(cur_mm); |
| 4920 | mmput(cur_mm); |
| 4921 | cur_mm = NULL; |
| 4922 | } |
| 4923 | |
| 4924 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 4925 | TASK_INTERRUPTIBLE); |
| 4926 | |
| 4927 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4928 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 4929 | /* make sure to read SQ tail after writing flags */ |
| 4930 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4931 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4932 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4933 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4934 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4935 | finish_wait(&ctx->sqo_wait, &wait); |
| 4936 | break; |
| 4937 | } |
| 4938 | if (signal_pending(current)) |
| 4939 | flush_signals(current); |
| 4940 | schedule(); |
| 4941 | finish_wait(&ctx->sqo_wait, &wait); |
| 4942 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4943 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4944 | continue; |
| 4945 | } |
| 4946 | finish_wait(&ctx->sqo_wait, &wait); |
| 4947 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4948 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4949 | } |
| 4950 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 4951 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4952 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 4953 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4954 | if (ret > 0) |
| 4955 | inflight += ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4956 | } |
| 4957 | |
| 4958 | set_fs(old_fs); |
| 4959 | if (cur_mm) { |
| 4960 | unuse_mm(cur_mm); |
| 4961 | mmput(cur_mm); |
| 4962 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4963 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 4964 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4965 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 4966 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4967 | return 0; |
| 4968 | } |
| 4969 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4970 | struct io_wait_queue { |
| 4971 | struct wait_queue_entry wq; |
| 4972 | struct io_ring_ctx *ctx; |
| 4973 | unsigned to_wait; |
| 4974 | unsigned nr_timeouts; |
| 4975 | }; |
| 4976 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4977 | 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] | 4978 | { |
| 4979 | struct io_ring_ctx *ctx = iowq->ctx; |
| 4980 | |
| 4981 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 4982 | * 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] | 4983 | * started waiting. For timeouts, we always want to return to userspace, |
| 4984 | * regardless of event count. |
| 4985 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4986 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4987 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 4988 | } |
| 4989 | |
| 4990 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 4991 | int wake_flags, void *key) |
| 4992 | { |
| 4993 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 4994 | wq); |
| 4995 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4996 | /* use noflush == true, as we can't safely rely on locking context */ |
| 4997 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4998 | return -1; |
| 4999 | |
| 5000 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 5001 | } |
| 5002 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5003 | /* |
| 5004 | * Wait until events become available, if we don't already have some. The |
| 5005 | * application must reap them itself, as they reside on the shared cq ring. |
| 5006 | */ |
| 5007 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 5008 | const sigset_t __user *sig, size_t sigsz) |
| 5009 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5010 | struct io_wait_queue iowq = { |
| 5011 | .wq = { |
| 5012 | .private = current, |
| 5013 | .func = io_wake_function, |
| 5014 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 5015 | }, |
| 5016 | .ctx = ctx, |
| 5017 | .to_wait = min_events, |
| 5018 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5019 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5020 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5021 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5022 | if (io_cqring_events(ctx, false) >= min_events) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5023 | return 0; |
| 5024 | |
| 5025 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5026 | #ifdef CONFIG_COMPAT |
| 5027 | if (in_compat_syscall()) |
| 5028 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5029 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5030 | else |
| 5031 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5032 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5033 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5034 | if (ret) |
| 5035 | return ret; |
| 5036 | } |
| 5037 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5038 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 5039 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5040 | do { |
| 5041 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 5042 | TASK_INTERRUPTIBLE); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5043 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5044 | break; |
| 5045 | schedule(); |
| 5046 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5047 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5048 | break; |
| 5049 | } |
| 5050 | } while (1); |
| 5051 | finish_wait(&ctx->wait, &iowq.wq); |
| 5052 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5053 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5054 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5055 | 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] | 5056 | } |
| 5057 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5058 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5059 | { |
| 5060 | #if defined(CONFIG_UNIX) |
| 5061 | if (ctx->ring_sock) { |
| 5062 | struct sock *sock = ctx->ring_sock->sk; |
| 5063 | struct sk_buff *skb; |
| 5064 | |
| 5065 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 5066 | kfree_skb(skb); |
| 5067 | } |
| 5068 | #else |
| 5069 | int i; |
| 5070 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5071 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5072 | struct file *file; |
| 5073 | |
| 5074 | file = io_file_from_index(ctx, i); |
| 5075 | if (file) |
| 5076 | fput(file); |
| 5077 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5078 | #endif |
| 5079 | } |
| 5080 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5081 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 5082 | { |
| 5083 | struct fixed_file_data *data; |
| 5084 | |
| 5085 | data = container_of(ref, struct fixed_file_data, refs); |
| 5086 | complete(&data->done); |
| 5087 | } |
| 5088 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5089 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5090 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5091 | struct fixed_file_data *data = ctx->file_data; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5092 | unsigned nr_tables, i; |
| 5093 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5094 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5095 | return -ENXIO; |
| 5096 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5097 | /* protect against inflight atomic switch, which drops the ref */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5098 | percpu_ref_get(&data->refs); |
Jens Axboe | e46a795 | 2020-01-17 11:15:34 -0700 | [diff] [blame] | 5099 | /* wait for existing switches */ |
| 5100 | flush_work(&data->ref_work); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5101 | percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill); |
| 5102 | wait_for_completion(&data->done); |
| 5103 | percpu_ref_put(&data->refs); |
Jens Axboe | e46a795 | 2020-01-17 11:15:34 -0700 | [diff] [blame] | 5104 | /* flush potential new switch */ |
| 5105 | flush_work(&data->ref_work); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5106 | percpu_ref_exit(&data->refs); |
| 5107 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5108 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5109 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 5110 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5111 | kfree(data->table[i].files); |
| 5112 | kfree(data->table); |
| 5113 | kfree(data); |
| 5114 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5115 | ctx->nr_user_files = 0; |
| 5116 | return 0; |
| 5117 | } |
| 5118 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5119 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 5120 | { |
| 5121 | if (ctx->sqo_thread) { |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5122 | wait_for_completion(&ctx->completions[1]); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5123 | /* |
| 5124 | * The park is a bit of a work-around, without it we get |
| 5125 | * warning spews on shutdown with SQPOLL set and affinity |
| 5126 | * set to a single CPU. |
| 5127 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5128 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5129 | kthread_stop(ctx->sqo_thread); |
| 5130 | ctx->sqo_thread = NULL; |
| 5131 | } |
| 5132 | } |
| 5133 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5134 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 5135 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5136 | io_sq_thread_stop(ctx); |
| 5137 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5138 | if (ctx->io_wq) { |
| 5139 | io_wq_destroy(ctx->io_wq); |
| 5140 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5141 | } |
| 5142 | } |
| 5143 | |
| 5144 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5145 | /* |
| 5146 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 5147 | * the io_uring can be safely unregistered on process exit, even if we have |
| 5148 | * loops in the file referencing. |
| 5149 | */ |
| 5150 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 5151 | { |
| 5152 | struct sock *sk = ctx->ring_sock->sk; |
| 5153 | struct scm_fp_list *fpl; |
| 5154 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5155 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5156 | |
| 5157 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 5158 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 5159 | |
| 5160 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 5161 | return -EMFILE; |
| 5162 | } |
| 5163 | |
| 5164 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 5165 | if (!fpl) |
| 5166 | return -ENOMEM; |
| 5167 | |
| 5168 | skb = alloc_skb(0, GFP_KERNEL); |
| 5169 | if (!skb) { |
| 5170 | kfree(fpl); |
| 5171 | return -ENOMEM; |
| 5172 | } |
| 5173 | |
| 5174 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5175 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5176 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5177 | fpl->user = get_uid(ctx->user); |
| 5178 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5179 | struct file *file = io_file_from_index(ctx, i + offset); |
| 5180 | |
| 5181 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5182 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5183 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5184 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 5185 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5186 | } |
| 5187 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5188 | if (nr_files) { |
| 5189 | fpl->max = SCM_MAX_FD; |
| 5190 | fpl->count = nr_files; |
| 5191 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5192 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5193 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 5194 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5195 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5196 | for (i = 0; i < nr_files; i++) |
| 5197 | fput(fpl->fp[i]); |
| 5198 | } else { |
| 5199 | kfree_skb(skb); |
| 5200 | kfree(fpl); |
| 5201 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5202 | |
| 5203 | return 0; |
| 5204 | } |
| 5205 | |
| 5206 | /* |
| 5207 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 5208 | * causes regular reference counting to break down. We rely on the UNIX |
| 5209 | * garbage collection to take care of this problem for us. |
| 5210 | */ |
| 5211 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5212 | { |
| 5213 | unsigned left, total; |
| 5214 | int ret = 0; |
| 5215 | |
| 5216 | total = 0; |
| 5217 | left = ctx->nr_user_files; |
| 5218 | while (left) { |
| 5219 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5220 | |
| 5221 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 5222 | if (ret) |
| 5223 | break; |
| 5224 | left -= this_files; |
| 5225 | total += this_files; |
| 5226 | } |
| 5227 | |
| 5228 | if (!ret) |
| 5229 | return 0; |
| 5230 | |
| 5231 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5232 | struct file *file = io_file_from_index(ctx, total); |
| 5233 | |
| 5234 | if (file) |
| 5235 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5236 | total++; |
| 5237 | } |
| 5238 | |
| 5239 | return ret; |
| 5240 | } |
| 5241 | #else |
| 5242 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5243 | { |
| 5244 | return 0; |
| 5245 | } |
| 5246 | #endif |
| 5247 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5248 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 5249 | unsigned nr_files) |
| 5250 | { |
| 5251 | int i; |
| 5252 | |
| 5253 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5254 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5255 | unsigned this_files; |
| 5256 | |
| 5257 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 5258 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 5259 | GFP_KERNEL); |
| 5260 | if (!table->files) |
| 5261 | break; |
| 5262 | nr_files -= this_files; |
| 5263 | } |
| 5264 | |
| 5265 | if (i == nr_tables) |
| 5266 | return 0; |
| 5267 | |
| 5268 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5269 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5270 | kfree(table->files); |
| 5271 | } |
| 5272 | return 1; |
| 5273 | } |
| 5274 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5275 | 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] | 5276 | { |
| 5277 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5278 | struct sock *sock = ctx->ring_sock->sk; |
| 5279 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 5280 | struct sk_buff *skb; |
| 5281 | int i; |
| 5282 | |
| 5283 | __skb_queue_head_init(&list); |
| 5284 | |
| 5285 | /* |
| 5286 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 5287 | * remove this entry and rearrange the file array. |
| 5288 | */ |
| 5289 | skb = skb_dequeue(head); |
| 5290 | while (skb) { |
| 5291 | struct scm_fp_list *fp; |
| 5292 | |
| 5293 | fp = UNIXCB(skb).fp; |
| 5294 | for (i = 0; i < fp->count; i++) { |
| 5295 | int left; |
| 5296 | |
| 5297 | if (fp->fp[i] != file) |
| 5298 | continue; |
| 5299 | |
| 5300 | unix_notinflight(fp->user, fp->fp[i]); |
| 5301 | left = fp->count - 1 - i; |
| 5302 | if (left) { |
| 5303 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 5304 | left * sizeof(struct file *)); |
| 5305 | } |
| 5306 | fp->count--; |
| 5307 | if (!fp->count) { |
| 5308 | kfree_skb(skb); |
| 5309 | skb = NULL; |
| 5310 | } else { |
| 5311 | __skb_queue_tail(&list, skb); |
| 5312 | } |
| 5313 | fput(file); |
| 5314 | file = NULL; |
| 5315 | break; |
| 5316 | } |
| 5317 | |
| 5318 | if (!file) |
| 5319 | break; |
| 5320 | |
| 5321 | __skb_queue_tail(&list, skb); |
| 5322 | |
| 5323 | skb = skb_dequeue(head); |
| 5324 | } |
| 5325 | |
| 5326 | if (skb_peek(&list)) { |
| 5327 | spin_lock_irq(&head->lock); |
| 5328 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 5329 | __skb_queue_tail(head, skb); |
| 5330 | spin_unlock_irq(&head->lock); |
| 5331 | } |
| 5332 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5333 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5334 | #endif |
| 5335 | } |
| 5336 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5337 | struct io_file_put { |
| 5338 | struct llist_node llist; |
| 5339 | struct file *file; |
| 5340 | struct completion *done; |
| 5341 | }; |
| 5342 | |
| 5343 | static void io_ring_file_ref_switch(struct work_struct *work) |
| 5344 | { |
| 5345 | struct io_file_put *pfile, *tmp; |
| 5346 | struct fixed_file_data *data; |
| 5347 | struct llist_node *node; |
| 5348 | |
| 5349 | data = container_of(work, struct fixed_file_data, ref_work); |
| 5350 | |
| 5351 | while ((node = llist_del_all(&data->put_llist)) != NULL) { |
| 5352 | llist_for_each_entry_safe(pfile, tmp, node, llist) { |
| 5353 | io_ring_file_put(data->ctx, pfile->file); |
| 5354 | if (pfile->done) |
| 5355 | complete(pfile->done); |
| 5356 | else |
| 5357 | kfree(pfile); |
| 5358 | } |
| 5359 | } |
| 5360 | |
| 5361 | percpu_ref_get(&data->refs); |
| 5362 | percpu_ref_switch_to_percpu(&data->refs); |
| 5363 | } |
| 5364 | |
| 5365 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 5366 | { |
| 5367 | struct fixed_file_data *data; |
| 5368 | |
| 5369 | data = container_of(ref, struct fixed_file_data, refs); |
| 5370 | |
| 5371 | /* we can't safely switch from inside this context, punt to wq */ |
| 5372 | queue_work(system_wq, &data->ref_work); |
| 5373 | } |
| 5374 | |
| 5375 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5376 | unsigned nr_args) |
| 5377 | { |
| 5378 | __s32 __user *fds = (__s32 __user *) arg; |
| 5379 | unsigned nr_tables; |
| 5380 | struct file *file; |
| 5381 | int fd, ret = 0; |
| 5382 | unsigned i; |
| 5383 | |
| 5384 | if (ctx->file_data) |
| 5385 | return -EBUSY; |
| 5386 | if (!nr_args) |
| 5387 | return -EINVAL; |
| 5388 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 5389 | return -EMFILE; |
| 5390 | |
| 5391 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 5392 | if (!ctx->file_data) |
| 5393 | return -ENOMEM; |
| 5394 | ctx->file_data->ctx = ctx; |
| 5395 | init_completion(&ctx->file_data->done); |
| 5396 | |
| 5397 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 5398 | ctx->file_data->table = kcalloc(nr_tables, |
| 5399 | sizeof(struct fixed_file_table), |
| 5400 | GFP_KERNEL); |
| 5401 | if (!ctx->file_data->table) { |
| 5402 | kfree(ctx->file_data); |
| 5403 | ctx->file_data = NULL; |
| 5404 | return -ENOMEM; |
| 5405 | } |
| 5406 | |
| 5407 | if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero, |
| 5408 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 5409 | kfree(ctx->file_data->table); |
| 5410 | kfree(ctx->file_data); |
| 5411 | ctx->file_data = NULL; |
| 5412 | return -ENOMEM; |
| 5413 | } |
| 5414 | ctx->file_data->put_llist.first = NULL; |
| 5415 | INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch); |
| 5416 | |
| 5417 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 5418 | percpu_ref_exit(&ctx->file_data->refs); |
| 5419 | kfree(ctx->file_data->table); |
| 5420 | kfree(ctx->file_data); |
| 5421 | ctx->file_data = NULL; |
| 5422 | return -ENOMEM; |
| 5423 | } |
| 5424 | |
| 5425 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 5426 | struct fixed_file_table *table; |
| 5427 | unsigned index; |
| 5428 | |
| 5429 | ret = -EFAULT; |
| 5430 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 5431 | break; |
| 5432 | /* allow sparse sets */ |
| 5433 | if (fd == -1) { |
| 5434 | ret = 0; |
| 5435 | continue; |
| 5436 | } |
| 5437 | |
| 5438 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 5439 | index = i & IORING_FILE_TABLE_MASK; |
| 5440 | file = fget(fd); |
| 5441 | |
| 5442 | ret = -EBADF; |
| 5443 | if (!file) |
| 5444 | break; |
| 5445 | |
| 5446 | /* |
| 5447 | * Don't allow io_uring instances to be registered. If UNIX |
| 5448 | * isn't enabled, then this causes a reference cycle and this |
| 5449 | * instance can never get freed. If UNIX is enabled we'll |
| 5450 | * handle it just fine, but there's still no point in allowing |
| 5451 | * a ring fd as it doesn't support regular read/write anyway. |
| 5452 | */ |
| 5453 | if (file->f_op == &io_uring_fops) { |
| 5454 | fput(file); |
| 5455 | break; |
| 5456 | } |
| 5457 | ret = 0; |
| 5458 | table->files[index] = file; |
| 5459 | } |
| 5460 | |
| 5461 | if (ret) { |
| 5462 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5463 | file = io_file_from_index(ctx, i); |
| 5464 | if (file) |
| 5465 | fput(file); |
| 5466 | } |
| 5467 | for (i = 0; i < nr_tables; i++) |
| 5468 | kfree(ctx->file_data->table[i].files); |
| 5469 | |
| 5470 | kfree(ctx->file_data->table); |
| 5471 | kfree(ctx->file_data); |
| 5472 | ctx->file_data = NULL; |
| 5473 | ctx->nr_user_files = 0; |
| 5474 | return ret; |
| 5475 | } |
| 5476 | |
| 5477 | ret = io_sqe_files_scm(ctx); |
| 5478 | if (ret) |
| 5479 | io_sqe_files_unregister(ctx); |
| 5480 | |
| 5481 | return ret; |
| 5482 | } |
| 5483 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5484 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 5485 | int index) |
| 5486 | { |
| 5487 | #if defined(CONFIG_UNIX) |
| 5488 | struct sock *sock = ctx->ring_sock->sk; |
| 5489 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 5490 | struct sk_buff *skb; |
| 5491 | |
| 5492 | /* |
| 5493 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 5494 | * file set. If there's no room, fall back to allocating a new skb |
| 5495 | * and filling it in. |
| 5496 | */ |
| 5497 | spin_lock_irq(&head->lock); |
| 5498 | skb = skb_peek(head); |
| 5499 | if (skb) { |
| 5500 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 5501 | |
| 5502 | if (fpl->count < SCM_MAX_FD) { |
| 5503 | __skb_unlink(skb, head); |
| 5504 | spin_unlock_irq(&head->lock); |
| 5505 | fpl->fp[fpl->count] = get_file(file); |
| 5506 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 5507 | fpl->count++; |
| 5508 | spin_lock_irq(&head->lock); |
| 5509 | __skb_queue_head(head, skb); |
| 5510 | } else { |
| 5511 | skb = NULL; |
| 5512 | } |
| 5513 | } |
| 5514 | spin_unlock_irq(&head->lock); |
| 5515 | |
| 5516 | if (skb) { |
| 5517 | fput(file); |
| 5518 | return 0; |
| 5519 | } |
| 5520 | |
| 5521 | return __io_sqe_files_scm(ctx, 1, index); |
| 5522 | #else |
| 5523 | return 0; |
| 5524 | #endif |
| 5525 | } |
| 5526 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5527 | static void io_atomic_switch(struct percpu_ref *ref) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5528 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5529 | struct fixed_file_data *data; |
| 5530 | |
| 5531 | data = container_of(ref, struct fixed_file_data, refs); |
| 5532 | clear_bit(FFD_F_ATOMIC, &data->state); |
| 5533 | } |
| 5534 | |
| 5535 | static bool io_queue_file_removal(struct fixed_file_data *data, |
| 5536 | struct file *file) |
| 5537 | { |
| 5538 | struct io_file_put *pfile, pfile_stack; |
| 5539 | DECLARE_COMPLETION_ONSTACK(done); |
| 5540 | |
| 5541 | /* |
| 5542 | * If we fail allocating the struct we need for doing async reomval |
| 5543 | * of this file, just punt to sync and wait for it. |
| 5544 | */ |
| 5545 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
| 5546 | if (!pfile) { |
| 5547 | pfile = &pfile_stack; |
| 5548 | pfile->done = &done; |
| 5549 | } |
| 5550 | |
| 5551 | pfile->file = file; |
| 5552 | llist_add(&pfile->llist, &data->put_llist); |
| 5553 | |
| 5554 | if (pfile == &pfile_stack) { |
| 5555 | if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5556 | percpu_ref_put(&data->refs); |
| 5557 | percpu_ref_switch_to_atomic(&data->refs, |
| 5558 | io_atomic_switch); |
| 5559 | } |
| 5560 | wait_for_completion(&done); |
| 5561 | flush_work(&data->ref_work); |
| 5562 | return false; |
| 5563 | } |
| 5564 | |
| 5565 | return true; |
| 5566 | } |
| 5567 | |
| 5568 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 5569 | struct io_uring_files_update *up, |
| 5570 | unsigned nr_args) |
| 5571 | { |
| 5572 | struct fixed_file_data *data = ctx->file_data; |
| 5573 | bool ref_switch = false; |
| 5574 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5575 | __s32 __user *fds; |
| 5576 | int fd, i, err; |
| 5577 | __u32 done; |
| 5578 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5579 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5580 | return -EOVERFLOW; |
| 5581 | if (done > ctx->nr_user_files) |
| 5582 | return -EINVAL; |
| 5583 | |
| 5584 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5585 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5586 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5587 | struct fixed_file_table *table; |
| 5588 | unsigned index; |
| 5589 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5590 | err = 0; |
| 5591 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 5592 | err = -EFAULT; |
| 5593 | break; |
| 5594 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5595 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 5596 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5597 | index = i & IORING_FILE_TABLE_MASK; |
| 5598 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5599 | file = io_file_from_index(ctx, index); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5600 | table->files[index] = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5601 | if (io_queue_file_removal(data, file)) |
| 5602 | ref_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5603 | } |
| 5604 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5605 | file = fget(fd); |
| 5606 | if (!file) { |
| 5607 | err = -EBADF; |
| 5608 | break; |
| 5609 | } |
| 5610 | /* |
| 5611 | * Don't allow io_uring instances to be registered. If |
| 5612 | * UNIX isn't enabled, then this causes a reference |
| 5613 | * cycle and this instance can never get freed. If UNIX |
| 5614 | * is enabled we'll handle it just fine, but there's |
| 5615 | * still no point in allowing a ring fd as it doesn't |
| 5616 | * support regular read/write anyway. |
| 5617 | */ |
| 5618 | if (file->f_op == &io_uring_fops) { |
| 5619 | fput(file); |
| 5620 | err = -EBADF; |
| 5621 | break; |
| 5622 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5623 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5624 | err = io_sqe_file_register(ctx, file, i); |
| 5625 | if (err) |
| 5626 | break; |
| 5627 | } |
| 5628 | nr_args--; |
| 5629 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5630 | up->offset++; |
| 5631 | } |
| 5632 | |
| 5633 | if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5634 | percpu_ref_put(&data->refs); |
| 5635 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5636 | } |
| 5637 | |
| 5638 | return done ? done : err; |
| 5639 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5640 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 5641 | unsigned nr_args) |
| 5642 | { |
| 5643 | struct io_uring_files_update up; |
| 5644 | |
| 5645 | if (!ctx->file_data) |
| 5646 | return -ENXIO; |
| 5647 | if (!nr_args) |
| 5648 | return -EINVAL; |
| 5649 | if (copy_from_user(&up, arg, sizeof(up))) |
| 5650 | return -EFAULT; |
| 5651 | if (up.resv) |
| 5652 | return -EINVAL; |
| 5653 | |
| 5654 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 5655 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5656 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 5657 | static void io_put_work(struct io_wq_work *work) |
| 5658 | { |
| 5659 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5660 | |
| 5661 | io_put_req(req); |
| 5662 | } |
| 5663 | |
| 5664 | static void io_get_work(struct io_wq_work *work) |
| 5665 | { |
| 5666 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5667 | |
| 5668 | refcount_inc(&req->refs); |
| 5669 | } |
| 5670 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5671 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 5672 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5673 | { |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5674 | struct io_wq_data data; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5675 | unsigned concurrency; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5676 | int ret; |
| 5677 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5678 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5679 | mmgrab(current->mm); |
| 5680 | ctx->sqo_mm = current->mm; |
| 5681 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5682 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 5683 | ret = -EPERM; |
| 5684 | if (!capable(CAP_SYS_ADMIN)) |
| 5685 | goto err; |
| 5686 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5687 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 5688 | if (!ctx->sq_thread_idle) |
| 5689 | ctx->sq_thread_idle = HZ; |
| 5690 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5691 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5692 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5693 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5694 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5695 | if (cpu >= nr_cpu_ids) |
| 5696 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 5697 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5698 | goto err; |
| 5699 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5700 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 5701 | ctx, cpu, |
| 5702 | "io_uring-sq"); |
| 5703 | } else { |
| 5704 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 5705 | "io_uring-sq"); |
| 5706 | } |
| 5707 | if (IS_ERR(ctx->sqo_thread)) { |
| 5708 | ret = PTR_ERR(ctx->sqo_thread); |
| 5709 | ctx->sqo_thread = NULL; |
| 5710 | goto err; |
| 5711 | } |
| 5712 | wake_up_process(ctx->sqo_thread); |
| 5713 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 5714 | /* Can't have SQ_AFF without SQPOLL */ |
| 5715 | ret = -EINVAL; |
| 5716 | goto err; |
| 5717 | } |
| 5718 | |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5719 | data.mm = ctx->sqo_mm; |
| 5720 | data.user = ctx->user; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5721 | data.creds = ctx->creds; |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5722 | data.get_work = io_get_work; |
| 5723 | data.put_work = io_put_work; |
| 5724 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5725 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 5726 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5727 | ctx->io_wq = io_wq_create(concurrency, &data); |
Jens Axboe | 975c99a5 | 2019-10-30 08:42:56 -0600 | [diff] [blame] | 5728 | if (IS_ERR(ctx->io_wq)) { |
| 5729 | ret = PTR_ERR(ctx->io_wq); |
| 5730 | ctx->io_wq = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5731 | goto err; |
| 5732 | } |
| 5733 | |
| 5734 | return 0; |
| 5735 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 5736 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5737 | mmdrop(ctx->sqo_mm); |
| 5738 | ctx->sqo_mm = NULL; |
| 5739 | return ret; |
| 5740 | } |
| 5741 | |
| 5742 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 5743 | { |
| 5744 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 5745 | } |
| 5746 | |
| 5747 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 5748 | { |
| 5749 | unsigned long page_limit, cur_pages, new_pages; |
| 5750 | |
| 5751 | /* Don't allow more pages than we can safely lock */ |
| 5752 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 5753 | |
| 5754 | do { |
| 5755 | cur_pages = atomic_long_read(&user->locked_vm); |
| 5756 | new_pages = cur_pages + nr_pages; |
| 5757 | if (new_pages > page_limit) |
| 5758 | return -ENOMEM; |
| 5759 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 5760 | new_pages) != cur_pages); |
| 5761 | |
| 5762 | return 0; |
| 5763 | } |
| 5764 | |
| 5765 | static void io_mem_free(void *ptr) |
| 5766 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 5767 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5768 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 5769 | if (!ptr) |
| 5770 | return; |
| 5771 | |
| 5772 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5773 | if (put_page_testzero(page)) |
| 5774 | free_compound_page(page); |
| 5775 | } |
| 5776 | |
| 5777 | static void *io_mem_alloc(size_t size) |
| 5778 | { |
| 5779 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 5780 | __GFP_NORETRY; |
| 5781 | |
| 5782 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 5783 | } |
| 5784 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5785 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 5786 | size_t *sq_offset) |
| 5787 | { |
| 5788 | struct io_rings *rings; |
| 5789 | size_t off, sq_array_size; |
| 5790 | |
| 5791 | off = struct_size(rings, cqes, cq_entries); |
| 5792 | if (off == SIZE_MAX) |
| 5793 | return SIZE_MAX; |
| 5794 | |
| 5795 | #ifdef CONFIG_SMP |
| 5796 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 5797 | if (off == 0) |
| 5798 | return SIZE_MAX; |
| 5799 | #endif |
| 5800 | |
| 5801 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 5802 | if (sq_array_size == SIZE_MAX) |
| 5803 | return SIZE_MAX; |
| 5804 | |
| 5805 | if (check_add_overflow(off, sq_array_size, &off)) |
| 5806 | return SIZE_MAX; |
| 5807 | |
| 5808 | if (sq_offset) |
| 5809 | *sq_offset = off; |
| 5810 | |
| 5811 | return off; |
| 5812 | } |
| 5813 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5814 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 5815 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5816 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5817 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5818 | pages = (size_t)1 << get_order( |
| 5819 | rings_size(sq_entries, cq_entries, NULL)); |
| 5820 | pages += (size_t)1 << get_order( |
| 5821 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5822 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5823 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5824 | } |
| 5825 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5826 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 5827 | { |
| 5828 | int i, j; |
| 5829 | |
| 5830 | if (!ctx->user_bufs) |
| 5831 | return -ENXIO; |
| 5832 | |
| 5833 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 5834 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 5835 | |
| 5836 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 5837 | put_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5838 | |
| 5839 | if (ctx->account_mem) |
| 5840 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5841 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5842 | imu->nr_bvecs = 0; |
| 5843 | } |
| 5844 | |
| 5845 | kfree(ctx->user_bufs); |
| 5846 | ctx->user_bufs = NULL; |
| 5847 | ctx->nr_user_bufs = 0; |
| 5848 | return 0; |
| 5849 | } |
| 5850 | |
| 5851 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 5852 | void __user *arg, unsigned index) |
| 5853 | { |
| 5854 | struct iovec __user *src; |
| 5855 | |
| 5856 | #ifdef CONFIG_COMPAT |
| 5857 | if (ctx->compat) { |
| 5858 | struct compat_iovec __user *ciovs; |
| 5859 | struct compat_iovec ciov; |
| 5860 | |
| 5861 | ciovs = (struct compat_iovec __user *) arg; |
| 5862 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 5863 | return -EFAULT; |
| 5864 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 5865 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5866 | dst->iov_len = ciov.iov_len; |
| 5867 | return 0; |
| 5868 | } |
| 5869 | #endif |
| 5870 | src = (struct iovec __user *) arg; |
| 5871 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 5872 | return -EFAULT; |
| 5873 | return 0; |
| 5874 | } |
| 5875 | |
| 5876 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5877 | unsigned nr_args) |
| 5878 | { |
| 5879 | struct vm_area_struct **vmas = NULL; |
| 5880 | struct page **pages = NULL; |
| 5881 | int i, j, got_pages = 0; |
| 5882 | int ret = -EINVAL; |
| 5883 | |
| 5884 | if (ctx->user_bufs) |
| 5885 | return -EBUSY; |
| 5886 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 5887 | return -EINVAL; |
| 5888 | |
| 5889 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 5890 | GFP_KERNEL); |
| 5891 | if (!ctx->user_bufs) |
| 5892 | return -ENOMEM; |
| 5893 | |
| 5894 | for (i = 0; i < nr_args; i++) { |
| 5895 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 5896 | unsigned long off, start, end, ubuf; |
| 5897 | int pret, nr_pages; |
| 5898 | struct iovec iov; |
| 5899 | size_t size; |
| 5900 | |
| 5901 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 5902 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 5903 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5904 | |
| 5905 | /* |
| 5906 | * Don't impose further limits on the size and buffer |
| 5907 | * constraints here, we'll -EINVAL later when IO is |
| 5908 | * submitted if they are wrong. |
| 5909 | */ |
| 5910 | ret = -EFAULT; |
| 5911 | if (!iov.iov_base || !iov.iov_len) |
| 5912 | goto err; |
| 5913 | |
| 5914 | /* arbitrary limit, but we need something */ |
| 5915 | if (iov.iov_len > SZ_1G) |
| 5916 | goto err; |
| 5917 | |
| 5918 | ubuf = (unsigned long) iov.iov_base; |
| 5919 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 5920 | start = ubuf >> PAGE_SHIFT; |
| 5921 | nr_pages = end - start; |
| 5922 | |
| 5923 | if (ctx->account_mem) { |
| 5924 | ret = io_account_mem(ctx->user, nr_pages); |
| 5925 | if (ret) |
| 5926 | goto err; |
| 5927 | } |
| 5928 | |
| 5929 | ret = 0; |
| 5930 | if (!pages || nr_pages > got_pages) { |
| 5931 | kfree(vmas); |
| 5932 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5933 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5934 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5935 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5936 | sizeof(struct vm_area_struct *), |
| 5937 | GFP_KERNEL); |
| 5938 | if (!pages || !vmas) { |
| 5939 | ret = -ENOMEM; |
| 5940 | if (ctx->account_mem) |
| 5941 | io_unaccount_mem(ctx->user, nr_pages); |
| 5942 | goto err; |
| 5943 | } |
| 5944 | got_pages = nr_pages; |
| 5945 | } |
| 5946 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5947 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5948 | GFP_KERNEL); |
| 5949 | ret = -ENOMEM; |
| 5950 | if (!imu->bvec) { |
| 5951 | if (ctx->account_mem) |
| 5952 | io_unaccount_mem(ctx->user, nr_pages); |
| 5953 | goto err; |
| 5954 | } |
| 5955 | |
| 5956 | ret = 0; |
| 5957 | down_read(¤t->mm->mmap_sem); |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 5958 | pret = get_user_pages(ubuf, nr_pages, |
| 5959 | FOLL_WRITE | FOLL_LONGTERM, |
| 5960 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5961 | if (pret == nr_pages) { |
| 5962 | /* don't support file backed memory */ |
| 5963 | for (j = 0; j < nr_pages; j++) { |
| 5964 | struct vm_area_struct *vma = vmas[j]; |
| 5965 | |
| 5966 | if (vma->vm_file && |
| 5967 | !is_file_hugepages(vma->vm_file)) { |
| 5968 | ret = -EOPNOTSUPP; |
| 5969 | break; |
| 5970 | } |
| 5971 | } |
| 5972 | } else { |
| 5973 | ret = pret < 0 ? pret : -EFAULT; |
| 5974 | } |
| 5975 | up_read(¤t->mm->mmap_sem); |
| 5976 | if (ret) { |
| 5977 | /* |
| 5978 | * if we did partial map, or found file backed vmas, |
| 5979 | * release any pages we did get |
| 5980 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 5981 | if (pret > 0) |
| 5982 | put_user_pages(pages, pret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5983 | if (ctx->account_mem) |
| 5984 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5985 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5986 | goto err; |
| 5987 | } |
| 5988 | |
| 5989 | off = ubuf & ~PAGE_MASK; |
| 5990 | size = iov.iov_len; |
| 5991 | for (j = 0; j < nr_pages; j++) { |
| 5992 | size_t vec_len; |
| 5993 | |
| 5994 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 5995 | imu->bvec[j].bv_page = pages[j]; |
| 5996 | imu->bvec[j].bv_len = vec_len; |
| 5997 | imu->bvec[j].bv_offset = off; |
| 5998 | off = 0; |
| 5999 | size -= vec_len; |
| 6000 | } |
| 6001 | /* store original address for later verification */ |
| 6002 | imu->ubuf = ubuf; |
| 6003 | imu->len = iov.iov_len; |
| 6004 | imu->nr_bvecs = nr_pages; |
| 6005 | |
| 6006 | ctx->nr_user_bufs++; |
| 6007 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6008 | kvfree(pages); |
| 6009 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6010 | return 0; |
| 6011 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6012 | kvfree(pages); |
| 6013 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6014 | io_sqe_buffer_unregister(ctx); |
| 6015 | return ret; |
| 6016 | } |
| 6017 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6018 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 6019 | { |
| 6020 | __s32 __user *fds = arg; |
| 6021 | int fd; |
| 6022 | |
| 6023 | if (ctx->cq_ev_fd) |
| 6024 | return -EBUSY; |
| 6025 | |
| 6026 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 6027 | return -EFAULT; |
| 6028 | |
| 6029 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 6030 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 6031 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 6032 | ctx->cq_ev_fd = NULL; |
| 6033 | return ret; |
| 6034 | } |
| 6035 | |
| 6036 | return 0; |
| 6037 | } |
| 6038 | |
| 6039 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 6040 | { |
| 6041 | if (ctx->cq_ev_fd) { |
| 6042 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 6043 | ctx->cq_ev_fd = NULL; |
| 6044 | return 0; |
| 6045 | } |
| 6046 | |
| 6047 | return -ENXIO; |
| 6048 | } |
| 6049 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6050 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 6051 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6052 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6053 | if (ctx->sqo_mm) |
| 6054 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6055 | |
| 6056 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6057 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6058 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6059 | io_eventfd_unregister(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6060 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6061 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6062 | if (ctx->ring_sock) { |
| 6063 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6064 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6065 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6066 | #endif |
| 6067 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6068 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6069 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6070 | |
| 6071 | percpu_ref_exit(&ctx->refs); |
| 6072 | if (ctx->account_mem) |
| 6073 | io_unaccount_mem(ctx->user, |
| 6074 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 6075 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6076 | put_cred(ctx->creds); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6077 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 6078 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 6079 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6080 | kfree(ctx); |
| 6081 | } |
| 6082 | |
| 6083 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 6084 | { |
| 6085 | struct io_ring_ctx *ctx = file->private_data; |
| 6086 | __poll_t mask = 0; |
| 6087 | |
| 6088 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 6089 | /* |
| 6090 | * synchronizes with barrier from wq_has_sleeper call in |
| 6091 | * io_commit_cqring |
| 6092 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6093 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6094 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 6095 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6096 | mask |= EPOLLOUT | EPOLLWRNORM; |
yangerkun | daa5de5 | 2019-09-24 20:53:34 +0800 | [diff] [blame] | 6097 | if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6098 | mask |= EPOLLIN | EPOLLRDNORM; |
| 6099 | |
| 6100 | return mask; |
| 6101 | } |
| 6102 | |
| 6103 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 6104 | { |
| 6105 | struct io_ring_ctx *ctx = file->private_data; |
| 6106 | |
| 6107 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 6108 | } |
| 6109 | |
| 6110 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 6111 | { |
| 6112 | mutex_lock(&ctx->uring_lock); |
| 6113 | percpu_ref_kill(&ctx->refs); |
| 6114 | mutex_unlock(&ctx->uring_lock); |
| 6115 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6116 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6117 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6118 | |
| 6119 | if (ctx->io_wq) |
| 6120 | io_wq_cancel_all(ctx->io_wq); |
| 6121 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6122 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 6123 | /* if we failed setting up the ctx, we might not have any rings */ |
| 6124 | if (ctx->rings) |
| 6125 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6126 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6127 | io_ring_ctx_free(ctx); |
| 6128 | } |
| 6129 | |
| 6130 | static int io_uring_release(struct inode *inode, struct file *file) |
| 6131 | { |
| 6132 | struct io_ring_ctx *ctx = file->private_data; |
| 6133 | |
| 6134 | file->private_data = NULL; |
| 6135 | io_ring_ctx_wait_and_kill(ctx); |
| 6136 | return 0; |
| 6137 | } |
| 6138 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6139 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 6140 | struct files_struct *files) |
| 6141 | { |
| 6142 | struct io_kiocb *req; |
| 6143 | DEFINE_WAIT(wait); |
| 6144 | |
| 6145 | while (!list_empty_careful(&ctx->inflight_list)) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6146 | struct io_kiocb *cancel_req = NULL; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6147 | |
| 6148 | spin_lock_irq(&ctx->inflight_lock); |
| 6149 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6150 | if (req->work.files != files) |
| 6151 | continue; |
| 6152 | /* req is being completed, ignore */ |
| 6153 | if (!refcount_inc_not_zero(&req->refs)) |
| 6154 | continue; |
| 6155 | cancel_req = req; |
| 6156 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6157 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6158 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6159 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6160 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6161 | spin_unlock_irq(&ctx->inflight_lock); |
| 6162 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6163 | /* We need to keep going until we don't find a matching req */ |
| 6164 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6165 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 6166 | |
| 6167 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 6168 | io_put_req(cancel_req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6169 | schedule(); |
| 6170 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6171 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6172 | } |
| 6173 | |
| 6174 | static int io_uring_flush(struct file *file, void *data) |
| 6175 | { |
| 6176 | struct io_ring_ctx *ctx = file->private_data; |
| 6177 | |
| 6178 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6179 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
| 6180 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6181 | io_wq_cancel_all(ctx->io_wq); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6182 | } |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6183 | return 0; |
| 6184 | } |
| 6185 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6186 | static void *io_uring_validate_mmap_request(struct file *file, |
| 6187 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6188 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6189 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6190 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6191 | struct page *page; |
| 6192 | void *ptr; |
| 6193 | |
| 6194 | switch (offset) { |
| 6195 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6196 | case IORING_OFF_CQ_RING: |
| 6197 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6198 | break; |
| 6199 | case IORING_OFF_SQES: |
| 6200 | ptr = ctx->sq_sqes; |
| 6201 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6202 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6203 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6204 | } |
| 6205 | |
| 6206 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 6207 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6208 | return ERR_PTR(-EINVAL); |
| 6209 | |
| 6210 | return ptr; |
| 6211 | } |
| 6212 | |
| 6213 | #ifdef CONFIG_MMU |
| 6214 | |
| 6215 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6216 | { |
| 6217 | size_t sz = vma->vm_end - vma->vm_start; |
| 6218 | unsigned long pfn; |
| 6219 | void *ptr; |
| 6220 | |
| 6221 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 6222 | if (IS_ERR(ptr)) |
| 6223 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6224 | |
| 6225 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 6226 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 6227 | } |
| 6228 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6229 | #else /* !CONFIG_MMU */ |
| 6230 | |
| 6231 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6232 | { |
| 6233 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 6234 | } |
| 6235 | |
| 6236 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 6237 | { |
| 6238 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 6239 | } |
| 6240 | |
| 6241 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 6242 | unsigned long addr, unsigned long len, |
| 6243 | unsigned long pgoff, unsigned long flags) |
| 6244 | { |
| 6245 | void *ptr; |
| 6246 | |
| 6247 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 6248 | if (IS_ERR(ptr)) |
| 6249 | return PTR_ERR(ptr); |
| 6250 | |
| 6251 | return (unsigned long) ptr; |
| 6252 | } |
| 6253 | |
| 6254 | #endif /* !CONFIG_MMU */ |
| 6255 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6256 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 6257 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 6258 | size_t, sigsz) |
| 6259 | { |
| 6260 | struct io_ring_ctx *ctx; |
| 6261 | long ret = -EBADF; |
| 6262 | int submitted = 0; |
| 6263 | struct fd f; |
| 6264 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6265 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6266 | return -EINVAL; |
| 6267 | |
| 6268 | f = fdget(fd); |
| 6269 | if (!f.file) |
| 6270 | return -EBADF; |
| 6271 | |
| 6272 | ret = -EOPNOTSUPP; |
| 6273 | if (f.file->f_op != &io_uring_fops) |
| 6274 | goto out_fput; |
| 6275 | |
| 6276 | ret = -ENXIO; |
| 6277 | ctx = f.file->private_data; |
| 6278 | if (!percpu_ref_tryget(&ctx->refs)) |
| 6279 | goto out_fput; |
| 6280 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6281 | /* |
| 6282 | * For SQ polling, the thread will do all submissions and completions. |
| 6283 | * Just return the requested submit count, and wake the thread if |
| 6284 | * we were asked to. |
| 6285 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6286 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6287 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6288 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 6289 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6290 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 6291 | wake_up(&ctx->sqo_wait); |
| 6292 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6293 | } else if (to_submit) { |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6294 | struct mm_struct *cur_mm; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6295 | |
Jens Axboe | 44d2827 | 2020-01-16 19:00:24 -0700 | [diff] [blame] | 6296 | if (current->mm != ctx->sqo_mm || |
| 6297 | current_cred() != ctx->creds) { |
| 6298 | ret = -EPERM; |
| 6299 | goto out; |
| 6300 | } |
| 6301 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6302 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6303 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
| 6304 | cur_mm = ctx->sqo_mm; |
| 6305 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, |
| 6306 | &cur_mm, false); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6307 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6308 | |
| 6309 | if (submitted != to_submit) |
| 6310 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6311 | } |
| 6312 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6313 | unsigned nr_events = 0; |
| 6314 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6315 | min_complete = min(min_complete, ctx->cq_entries); |
| 6316 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6317 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6318 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6319 | } else { |
| 6320 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 6321 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6322 | } |
| 6323 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6324 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 6325 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6326 | out_fput: |
| 6327 | fdput(f); |
| 6328 | return submitted ? submitted : ret; |
| 6329 | } |
| 6330 | |
| 6331 | static const struct file_operations io_uring_fops = { |
| 6332 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6333 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6334 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6335 | #ifndef CONFIG_MMU |
| 6336 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 6337 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 6338 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6339 | .poll = io_uring_poll, |
| 6340 | .fasync = io_uring_fasync, |
| 6341 | }; |
| 6342 | |
| 6343 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 6344 | struct io_uring_params *p) |
| 6345 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6346 | struct io_rings *rings; |
| 6347 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6348 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6349 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 6350 | if (size == SIZE_MAX) |
| 6351 | return -EOVERFLOW; |
| 6352 | |
| 6353 | rings = io_mem_alloc(size); |
| 6354 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6355 | return -ENOMEM; |
| 6356 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6357 | ctx->rings = rings; |
| 6358 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 6359 | rings->sq_ring_mask = p->sq_entries - 1; |
| 6360 | rings->cq_ring_mask = p->cq_entries - 1; |
| 6361 | rings->sq_ring_entries = p->sq_entries; |
| 6362 | rings->cq_ring_entries = p->cq_entries; |
| 6363 | ctx->sq_mask = rings->sq_ring_mask; |
| 6364 | ctx->cq_mask = rings->cq_ring_mask; |
| 6365 | ctx->sq_entries = rings->sq_ring_entries; |
| 6366 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6367 | |
| 6368 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6369 | if (size == SIZE_MAX) { |
| 6370 | io_mem_free(ctx->rings); |
| 6371 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6372 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6373 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6374 | |
| 6375 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6376 | if (!ctx->sq_sqes) { |
| 6377 | io_mem_free(ctx->rings); |
| 6378 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6379 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6380 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6381 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6382 | return 0; |
| 6383 | } |
| 6384 | |
| 6385 | /* |
| 6386 | * Allocate an anonymous fd, this is what constitutes the application |
| 6387 | * visible backing of an io_uring instance. The application mmaps this |
| 6388 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 6389 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 6390 | */ |
| 6391 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 6392 | { |
| 6393 | struct file *file; |
| 6394 | int ret; |
| 6395 | |
| 6396 | #if defined(CONFIG_UNIX) |
| 6397 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 6398 | &ctx->ring_sock); |
| 6399 | if (ret) |
| 6400 | return ret; |
| 6401 | #endif |
| 6402 | |
| 6403 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 6404 | if (ret < 0) |
| 6405 | goto err; |
| 6406 | |
| 6407 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 6408 | O_RDWR | O_CLOEXEC); |
| 6409 | if (IS_ERR(file)) { |
| 6410 | put_unused_fd(ret); |
| 6411 | ret = PTR_ERR(file); |
| 6412 | goto err; |
| 6413 | } |
| 6414 | |
| 6415 | #if defined(CONFIG_UNIX) |
| 6416 | ctx->ring_sock->file = file; |
| 6417 | #endif |
| 6418 | fd_install(ret, file); |
| 6419 | return ret; |
| 6420 | err: |
| 6421 | #if defined(CONFIG_UNIX) |
| 6422 | sock_release(ctx->ring_sock); |
| 6423 | ctx->ring_sock = NULL; |
| 6424 | #endif |
| 6425 | return ret; |
| 6426 | } |
| 6427 | |
| 6428 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 6429 | { |
| 6430 | struct user_struct *user = NULL; |
| 6431 | struct io_ring_ctx *ctx; |
| 6432 | bool account_mem; |
| 6433 | int ret; |
| 6434 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6435 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6436 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6437 | if (entries > IORING_MAX_ENTRIES) { |
| 6438 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6439 | return -EINVAL; |
| 6440 | entries = IORING_MAX_ENTRIES; |
| 6441 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6442 | |
| 6443 | /* |
| 6444 | * Use twice as many entries for the CQ ring. It's possible for the |
| 6445 | * application to drive a higher depth than the size of the SQ ring, |
| 6446 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6447 | * some flexibility in overcommitting a bit. If the application has |
| 6448 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 6449 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6450 | */ |
| 6451 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6452 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 6453 | /* |
| 6454 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 6455 | * to a power-of-two, if it isn't already. We do NOT impose |
| 6456 | * any cq vs sq ring sizing. |
| 6457 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6458 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6459 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6460 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 6461 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6462 | return -EINVAL; |
| 6463 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 6464 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6465 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 6466 | } else { |
| 6467 | p->cq_entries = 2 * p->sq_entries; |
| 6468 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6469 | |
| 6470 | user = get_uid(current_user()); |
| 6471 | account_mem = !capable(CAP_IPC_LOCK); |
| 6472 | |
| 6473 | if (account_mem) { |
| 6474 | ret = io_account_mem(user, |
| 6475 | ring_pages(p->sq_entries, p->cq_entries)); |
| 6476 | if (ret) { |
| 6477 | free_uid(user); |
| 6478 | return ret; |
| 6479 | } |
| 6480 | } |
| 6481 | |
| 6482 | ctx = io_ring_ctx_alloc(p); |
| 6483 | if (!ctx) { |
| 6484 | if (account_mem) |
| 6485 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 6486 | p->cq_entries)); |
| 6487 | free_uid(user); |
| 6488 | return -ENOMEM; |
| 6489 | } |
| 6490 | ctx->compat = in_compat_syscall(); |
| 6491 | ctx->account_mem = account_mem; |
| 6492 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 6493 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6494 | |
| 6495 | ret = io_allocate_scq_urings(ctx, p); |
| 6496 | if (ret) |
| 6497 | goto err; |
| 6498 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6499 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6500 | if (ret) |
| 6501 | goto err; |
| 6502 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6503 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6504 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 6505 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 6506 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 6507 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 6508 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 6509 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 6510 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6511 | |
| 6512 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6513 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 6514 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 6515 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 6516 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 6517 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 6518 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 6519 | |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 6520 | /* |
| 6521 | * Install ring fd as the very last thing, so we don't risk someone |
| 6522 | * having closed it before we finish setup |
| 6523 | */ |
| 6524 | ret = io_uring_get_fd(ctx); |
| 6525 | if (ret < 0) |
| 6526 | goto err; |
| 6527 | |
Jens Axboe | da8c969 | 2019-12-02 18:51:26 -0700 | [diff] [blame] | 6528 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 6529 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6530 | 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] | 6531 | return ret; |
| 6532 | err: |
| 6533 | io_ring_ctx_wait_and_kill(ctx); |
| 6534 | return ret; |
| 6535 | } |
| 6536 | |
| 6537 | /* |
| 6538 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 6539 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 6540 | * params structure passed in. |
| 6541 | */ |
| 6542 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 6543 | { |
| 6544 | struct io_uring_params p; |
| 6545 | long ret; |
| 6546 | int i; |
| 6547 | |
| 6548 | if (copy_from_user(&p, params, sizeof(p))) |
| 6549 | return -EFAULT; |
| 6550 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 6551 | if (p.resv[i]) |
| 6552 | return -EINVAL; |
| 6553 | } |
| 6554 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6555 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6556 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
| 6557 | IORING_SETUP_CLAMP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6558 | return -EINVAL; |
| 6559 | |
| 6560 | ret = io_uring_create(entries, &p); |
| 6561 | if (ret < 0) |
| 6562 | return ret; |
| 6563 | |
| 6564 | if (copy_to_user(params, &p, sizeof(p))) |
| 6565 | return -EFAULT; |
| 6566 | |
| 6567 | return ret; |
| 6568 | } |
| 6569 | |
| 6570 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 6571 | struct io_uring_params __user *, params) |
| 6572 | { |
| 6573 | return io_uring_setup(entries, params); |
| 6574 | } |
| 6575 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 6576 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 6577 | { |
| 6578 | struct io_uring_probe *p; |
| 6579 | size_t size; |
| 6580 | int i, ret; |
| 6581 | |
| 6582 | size = struct_size(p, ops, nr_args); |
| 6583 | if (size == SIZE_MAX) |
| 6584 | return -EOVERFLOW; |
| 6585 | p = kzalloc(size, GFP_KERNEL); |
| 6586 | if (!p) |
| 6587 | return -ENOMEM; |
| 6588 | |
| 6589 | ret = -EFAULT; |
| 6590 | if (copy_from_user(p, arg, size)) |
| 6591 | goto out; |
| 6592 | ret = -EINVAL; |
| 6593 | if (memchr_inv(p, 0, size)) |
| 6594 | goto out; |
| 6595 | |
| 6596 | p->last_op = IORING_OP_LAST - 1; |
| 6597 | if (nr_args > IORING_OP_LAST) |
| 6598 | nr_args = IORING_OP_LAST; |
| 6599 | |
| 6600 | for (i = 0; i < nr_args; i++) { |
| 6601 | p->ops[i].op = i; |
| 6602 | if (!io_op_defs[i].not_supported) |
| 6603 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 6604 | } |
| 6605 | p->ops_len = i; |
| 6606 | |
| 6607 | ret = 0; |
| 6608 | if (copy_to_user(arg, p, size)) |
| 6609 | ret = -EFAULT; |
| 6610 | out: |
| 6611 | kfree(p); |
| 6612 | return ret; |
| 6613 | } |
| 6614 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6615 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 6616 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 6617 | __releases(ctx->uring_lock) |
| 6618 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6619 | { |
| 6620 | int ret; |
| 6621 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 6622 | /* |
| 6623 | * We're inside the ring mutex, if the ref is already dying, then |
| 6624 | * someone else killed the ctx or is already going through |
| 6625 | * io_uring_register(). |
| 6626 | */ |
| 6627 | if (percpu_ref_is_dying(&ctx->refs)) |
| 6628 | return -ENXIO; |
| 6629 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6630 | if (opcode != IORING_UNREGISTER_FILES && |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 6631 | opcode != IORING_REGISTER_FILES_UPDATE && |
| 6632 | opcode != IORING_REGISTER_PROBE) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6633 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 6634 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6635 | /* |
| 6636 | * Drop uring mutex before waiting for references to exit. If |
| 6637 | * another thread is currently inside io_uring_enter() it might |
| 6638 | * need to grab the uring_lock to make progress. If we hold it |
| 6639 | * here across the drain wait, then we can deadlock. It's safe |
| 6640 | * to drop the mutex here, since no new references will come in |
| 6641 | * after we've killed the percpu ref. |
| 6642 | */ |
| 6643 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6644 | ret = wait_for_completion_interruptible(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6645 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6646 | if (ret) { |
| 6647 | percpu_ref_resurrect(&ctx->refs); |
| 6648 | ret = -EINTR; |
| 6649 | goto out; |
| 6650 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6651 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6652 | |
| 6653 | switch (opcode) { |
| 6654 | case IORING_REGISTER_BUFFERS: |
| 6655 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 6656 | break; |
| 6657 | case IORING_UNREGISTER_BUFFERS: |
| 6658 | ret = -EINVAL; |
| 6659 | if (arg || nr_args) |
| 6660 | break; |
| 6661 | ret = io_sqe_buffer_unregister(ctx); |
| 6662 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6663 | case IORING_REGISTER_FILES: |
| 6664 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 6665 | break; |
| 6666 | case IORING_UNREGISTER_FILES: |
| 6667 | ret = -EINVAL; |
| 6668 | if (arg || nr_args) |
| 6669 | break; |
| 6670 | ret = io_sqe_files_unregister(ctx); |
| 6671 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6672 | case IORING_REGISTER_FILES_UPDATE: |
| 6673 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 6674 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6675 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 6676 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6677 | ret = -EINVAL; |
| 6678 | if (nr_args != 1) |
| 6679 | break; |
| 6680 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 6681 | if (ret) |
| 6682 | break; |
| 6683 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 6684 | ctx->eventfd_async = 1; |
| 6685 | else |
| 6686 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6687 | break; |
| 6688 | case IORING_UNREGISTER_EVENTFD: |
| 6689 | ret = -EINVAL; |
| 6690 | if (arg || nr_args) |
| 6691 | break; |
| 6692 | ret = io_eventfd_unregister(ctx); |
| 6693 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 6694 | case IORING_REGISTER_PROBE: |
| 6695 | ret = -EINVAL; |
| 6696 | if (!arg || nr_args > 256) |
| 6697 | break; |
| 6698 | ret = io_probe(ctx, arg, nr_args); |
| 6699 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6700 | default: |
| 6701 | ret = -EINVAL; |
| 6702 | break; |
| 6703 | } |
| 6704 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6705 | |
| 6706 | if (opcode != IORING_UNREGISTER_FILES && |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 6707 | opcode != IORING_REGISTER_FILES_UPDATE && |
| 6708 | opcode != IORING_REGISTER_PROBE) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6709 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6710 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6711 | out: |
| 6712 | reinit_completion(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6713 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6714 | return ret; |
| 6715 | } |
| 6716 | |
| 6717 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 6718 | void __user *, arg, unsigned int, nr_args) |
| 6719 | { |
| 6720 | struct io_ring_ctx *ctx; |
| 6721 | long ret = -EBADF; |
| 6722 | struct fd f; |
| 6723 | |
| 6724 | f = fdget(fd); |
| 6725 | if (!f.file) |
| 6726 | return -EBADF; |
| 6727 | |
| 6728 | ret = -EOPNOTSUPP; |
| 6729 | if (f.file->f_op != &io_uring_fops) |
| 6730 | goto out_fput; |
| 6731 | |
| 6732 | ctx = f.file->private_data; |
| 6733 | |
| 6734 | mutex_lock(&ctx->uring_lock); |
| 6735 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 6736 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6737 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 6738 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6739 | out_fput: |
| 6740 | fdput(f); |
| 6741 | return ret; |
| 6742 | } |
| 6743 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6744 | static int __init io_uring_init(void) |
| 6745 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6746 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6747 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 6748 | return 0; |
| 6749 | }; |
| 6750 | __initcall(io_uring_init); |