Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Shared application/kernel submission and completion ring pairs, for |
| 4 | * supporting fast/efficient IO. |
| 5 | * |
| 6 | * A note on the read/write ordering memory barriers that are matched between |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 7 | * the application and kernel side. |
| 8 | * |
| 9 | * After the application reads the CQ ring tail, it must use an |
| 10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses |
| 11 | * before writing the tail (using smp_load_acquire to read the tail will |
| 12 | * do). It also needs a smp_mb() before updating CQ head (ordering the |
| 13 | * entry load(s) with the head store), pairing with an implicit barrier |
| 14 | * through a control-dependency in io_get_cqring (smp_store_release to |
| 15 | * store head will do). Failure to do so could lead to reading invalid |
| 16 | * CQ entries. |
| 17 | * |
| 18 | * Likewise, the application must use an appropriate smp_wmb() before |
| 19 | * writing the SQ tail (ordering SQ entry stores with the tail store), |
| 20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release |
| 21 | * to store the tail will do). And it needs a barrier ordering the SQ |
| 22 | * head load before writing new SQ entries (smp_load_acquire to read |
| 23 | * head will do). |
| 24 | * |
| 25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application |
| 26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* |
| 27 | * updating the SQ tail; a full memory barrier smp_mb() is needed |
| 28 | * between. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 29 | * |
| 30 | * Also see the examples in the liburing library: |
| 31 | * |
| 32 | * git://git.kernel.dk/liburing |
| 33 | * |
| 34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens |
| 35 | * from data shared between the kernel and application. This is done both |
| 36 | * for ordering purposes, but also to ensure that once a value is loaded from |
| 37 | * data that the application could potentially modify, it remains stable. |
| 38 | * |
| 39 | * Copyright (C) 2018-2019 Jens Axboe |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 41 | */ |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/errno.h> |
| 45 | #include <linux/syscalls.h> |
| 46 | #include <linux/compat.h> |
| 47 | #include <linux/refcount.h> |
| 48 | #include <linux/uio.h> |
| 49 | |
| 50 | #include <linux/sched/signal.h> |
| 51 | #include <linux/fs.h> |
| 52 | #include <linux/file.h> |
| 53 | #include <linux/fdtable.h> |
| 54 | #include <linux/mm.h> |
| 55 | #include <linux/mman.h> |
| 56 | #include <linux/mmu_context.h> |
| 57 | #include <linux/percpu.h> |
| 58 | #include <linux/slab.h> |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 59 | #include <linux/kthread.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 60 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 61 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 62 | #include <linux/net.h> |
| 63 | #include <net/sock.h> |
| 64 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 65 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 66 | #include <linux/anon_inodes.h> |
| 67 | #include <linux/sched/mm.h> |
| 68 | #include <linux/uaccess.h> |
| 69 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 70 | #include <linux/sizes.h> |
| 71 | #include <linux/hugetlb.h> |
Jens Axboe | aa4c396 | 2019-11-29 10:14:00 -0700 | [diff] [blame] | 72 | #include <linux/highmem.h> |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 73 | #include <linux/namei.h> |
| 74 | #include <linux/fsnotify.h> |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 75 | #include <linux/fadvise.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 76 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 77 | #define CREATE_TRACE_POINTS |
| 78 | #include <trace/events/io_uring.h> |
| 79 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 80 | #include <uapi/linux/io_uring.h> |
| 81 | |
| 82 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 83 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 84 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 85 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 86 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| 90 | */ |
| 91 | #define IORING_FILE_TABLE_SHIFT 9 |
| 92 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| 93 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| 94 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 95 | |
| 96 | struct io_uring { |
| 97 | u32 head ____cacheline_aligned_in_smp; |
| 98 | u32 tail ____cacheline_aligned_in_smp; |
| 99 | }; |
| 100 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 101 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 102 | * This data is shared with the application through the mmap at offsets |
| 103 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 104 | * |
| 105 | * The offsets to the member fields are published through struct |
| 106 | * io_sqring_offsets when calling io_uring_setup. |
| 107 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 108 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 109 | /* |
| 110 | * Head and tail offsets into the ring; the offsets need to be |
| 111 | * masked to get valid indices. |
| 112 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 113 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 114 | * and the application controls tail of the sq ring and the head of the |
| 115 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 116 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 117 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 118 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 119 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 120 | * ring_entries - 1) |
| 121 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 122 | u32 sq_ring_mask, cq_ring_mask; |
| 123 | /* Ring sizes (constant, power of 2) */ |
| 124 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 125 | /* |
| 126 | * Number of invalid entries dropped by the kernel due to |
| 127 | * invalid index stored in array |
| 128 | * |
| 129 | * Written by the kernel, shouldn't be modified by the |
| 130 | * application (i.e. get number of "new events" by comparing to |
| 131 | * cached value). |
| 132 | * |
| 133 | * After a new SQ head value was read by the application this |
| 134 | * counter includes all submissions that were dropped reaching |
| 135 | * the new SQ head (and possibly more). |
| 136 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 137 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 138 | /* |
| 139 | * Runtime flags |
| 140 | * |
| 141 | * Written by the kernel, shouldn't be modified by the |
| 142 | * application. |
| 143 | * |
| 144 | * The application needs a full memory barrier before checking |
| 145 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 146 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 147 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 148 | /* |
| 149 | * Number of completion events lost because the queue was full; |
| 150 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 151 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 152 | * the completion queue. |
| 153 | * |
| 154 | * Written by the kernel, shouldn't be modified by the |
| 155 | * application (i.e. get number of "new events" by comparing to |
| 156 | * cached value). |
| 157 | * |
| 158 | * As completion events come in out of order this counter is not |
| 159 | * ordered with any other data. |
| 160 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 161 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 162 | /* |
| 163 | * Ring buffer of completion events. |
| 164 | * |
| 165 | * The kernel writes completion events fresh every time they are |
| 166 | * produced, so the application is allowed to modify pending |
| 167 | * entries. |
| 168 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 169 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 172 | struct io_mapped_ubuf { |
| 173 | u64 ubuf; |
| 174 | size_t len; |
| 175 | struct bio_vec *bvec; |
| 176 | unsigned int nr_bvecs; |
| 177 | }; |
| 178 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 179 | struct fixed_file_table { |
| 180 | struct file **files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 183 | enum { |
| 184 | FFD_F_ATOMIC, |
| 185 | }; |
| 186 | |
| 187 | struct fixed_file_data { |
| 188 | struct fixed_file_table *table; |
| 189 | struct io_ring_ctx *ctx; |
| 190 | |
| 191 | struct percpu_ref refs; |
| 192 | struct llist_head put_llist; |
| 193 | unsigned long state; |
| 194 | struct work_struct ref_work; |
| 195 | struct completion done; |
| 196 | }; |
| 197 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 198 | struct io_ring_ctx { |
| 199 | struct { |
| 200 | struct percpu_ref refs; |
| 201 | } ____cacheline_aligned_in_smp; |
| 202 | |
| 203 | struct { |
| 204 | unsigned int flags; |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 205 | int compat: 1; |
| 206 | int account_mem: 1; |
| 207 | int cq_overflow_flushed: 1; |
| 208 | int drain_next: 1; |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 209 | int eventfd_async: 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 210 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 211 | /* |
| 212 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 213 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 214 | * |
| 215 | * This indirection could e.g. be used to assign fixed |
| 216 | * io_uring_sqe entries to operations and only submit them to |
| 217 | * the queue when needed. |
| 218 | * |
| 219 | * The kernel modifies neither the indices array nor the entries |
| 220 | * array. |
| 221 | */ |
| 222 | u32 *sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 223 | unsigned cached_sq_head; |
| 224 | unsigned sq_entries; |
| 225 | unsigned sq_mask; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 226 | unsigned sq_thread_idle; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 227 | unsigned cached_sq_dropped; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 228 | atomic_t cached_cq_overflow; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 229 | unsigned long sq_check_overflow; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 230 | |
| 231 | struct list_head defer_list; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 232 | struct list_head timeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 233 | struct list_head cq_overflow_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 234 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 235 | wait_queue_head_t inflight_wait; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 236 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 237 | } ____cacheline_aligned_in_smp; |
| 238 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 239 | struct io_rings *rings; |
| 240 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 241 | /* IO offload */ |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 242 | struct io_wq *io_wq; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 243 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 244 | struct mm_struct *sqo_mm; |
| 245 | wait_queue_head_t sqo_wait; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 246 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 247 | /* |
| 248 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 249 | * readers must ensure that ->refs is alive as long as the file* is |
| 250 | * used. Only updated through io_uring_register(2). |
| 251 | */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 252 | struct fixed_file_data *file_data; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 253 | unsigned nr_user_files; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 254 | int ring_fd; |
| 255 | struct file *ring_file; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 256 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 257 | /* if used, fixed mapped user buffers */ |
| 258 | unsigned nr_user_bufs; |
| 259 | struct io_mapped_ubuf *user_bufs; |
| 260 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 261 | struct user_struct *user; |
| 262 | |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 263 | const struct cred *creds; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 264 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 265 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
| 266 | struct completion *completions; |
| 267 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 268 | /* if all else fails... */ |
| 269 | struct io_kiocb *fallback_req; |
| 270 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 271 | #if defined(CONFIG_UNIX) |
| 272 | struct socket *ring_sock; |
| 273 | #endif |
| 274 | |
| 275 | struct { |
| 276 | unsigned cached_cq_tail; |
| 277 | unsigned cq_entries; |
| 278 | unsigned cq_mask; |
| 279 | atomic_t cq_timeouts; |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 280 | unsigned long cq_check_overflow; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 281 | struct wait_queue_head cq_wait; |
| 282 | struct fasync_struct *cq_fasync; |
| 283 | struct eventfd_ctx *cq_ev_fd; |
| 284 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 285 | |
| 286 | struct { |
| 287 | struct mutex uring_lock; |
| 288 | wait_queue_head_t wait; |
| 289 | } ____cacheline_aligned_in_smp; |
| 290 | |
| 291 | struct { |
| 292 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 293 | struct llist_head poll_llist; |
| 294 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 295 | /* |
| 296 | * ->poll_list is protected by the ctx->uring_lock for |
| 297 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 298 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 299 | * manipulate the list, hence no extra locking is needed there. |
| 300 | */ |
| 301 | struct list_head poll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 302 | struct hlist_head *cancel_hash; |
| 303 | unsigned cancel_hash_bits; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 304 | bool poll_multi_file; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 305 | |
| 306 | spinlock_t inflight_lock; |
| 307 | struct list_head inflight_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 308 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 309 | }; |
| 310 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 311 | /* |
| 312 | * First field must be the file pointer in all the |
| 313 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 314 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 315 | struct io_poll_iocb { |
| 316 | struct file *file; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 317 | union { |
| 318 | struct wait_queue_head *head; |
| 319 | u64 addr; |
| 320 | }; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 321 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 322 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 323 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 324 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 325 | }; |
| 326 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 327 | struct io_close { |
| 328 | struct file *file; |
| 329 | struct file *put_file; |
| 330 | int fd; |
| 331 | }; |
| 332 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 333 | struct io_timeout_data { |
| 334 | struct io_kiocb *req; |
| 335 | struct hrtimer timer; |
| 336 | struct timespec64 ts; |
| 337 | enum hrtimer_mode mode; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 338 | u32 seq_offset; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 339 | }; |
| 340 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 341 | struct io_accept { |
| 342 | struct file *file; |
| 343 | struct sockaddr __user *addr; |
| 344 | int __user *addr_len; |
| 345 | int flags; |
| 346 | }; |
| 347 | |
| 348 | struct io_sync { |
| 349 | struct file *file; |
| 350 | loff_t len; |
| 351 | loff_t off; |
| 352 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 353 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 354 | }; |
| 355 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 356 | struct io_cancel { |
| 357 | struct file *file; |
| 358 | u64 addr; |
| 359 | }; |
| 360 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 361 | struct io_timeout { |
| 362 | struct file *file; |
| 363 | u64 addr; |
| 364 | int flags; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 365 | unsigned count; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 366 | }; |
| 367 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 368 | struct io_rw { |
| 369 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 370 | struct kiocb kiocb; |
| 371 | u64 addr; |
| 372 | u64 len; |
| 373 | }; |
| 374 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 375 | struct io_connect { |
| 376 | struct file *file; |
| 377 | struct sockaddr __user *addr; |
| 378 | int addr_len; |
| 379 | }; |
| 380 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 381 | struct io_sr_msg { |
| 382 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 383 | union { |
| 384 | struct user_msghdr __user *msg; |
| 385 | void __user *buf; |
| 386 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 387 | int msg_flags; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 388 | size_t len; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 389 | }; |
| 390 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 391 | struct io_open { |
| 392 | struct file *file; |
| 393 | int dfd; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 394 | union { |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 395 | unsigned mask; |
| 396 | }; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 397 | struct filename *filename; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 398 | struct statx __user *buffer; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 399 | struct open_how how; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 400 | }; |
| 401 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 402 | struct io_files_update { |
| 403 | struct file *file; |
| 404 | u64 arg; |
| 405 | u32 nr_args; |
| 406 | u32 offset; |
| 407 | }; |
| 408 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 409 | struct io_fadvise { |
| 410 | struct file *file; |
| 411 | u64 offset; |
| 412 | u32 len; |
| 413 | u32 advice; |
| 414 | }; |
| 415 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 416 | struct io_madvise { |
| 417 | struct file *file; |
| 418 | u64 addr; |
| 419 | u32 len; |
| 420 | u32 advice; |
| 421 | }; |
| 422 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 423 | struct io_async_connect { |
| 424 | struct sockaddr_storage address; |
| 425 | }; |
| 426 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 427 | struct io_async_msghdr { |
| 428 | struct iovec fast_iov[UIO_FASTIOV]; |
| 429 | struct iovec *iov; |
| 430 | struct sockaddr __user *uaddr; |
| 431 | struct msghdr msg; |
| 432 | }; |
| 433 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 434 | struct io_async_rw { |
| 435 | struct iovec fast_iov[UIO_FASTIOV]; |
| 436 | struct iovec *iov; |
| 437 | ssize_t nr_segs; |
| 438 | ssize_t size; |
| 439 | }; |
| 440 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 441 | struct io_async_open { |
| 442 | struct filename *filename; |
| 443 | }; |
| 444 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 445 | struct io_async_ctx { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 446 | union { |
| 447 | struct io_async_rw rw; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 448 | struct io_async_msghdr msg; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 449 | struct io_async_connect connect; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 450 | struct io_timeout_data timeout; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 451 | struct io_async_open open; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 452 | }; |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 453 | }; |
| 454 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 455 | /* |
| 456 | * NOTE! Each of the iocb union members has the file pointer |
| 457 | * as the first entry in their struct definition. So you can |
| 458 | * access the file pointer through any of the sub-structs, |
| 459 | * or directly as just 'ki_filp' in this struct. |
| 460 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 461 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 462 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 463 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 464 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 465 | struct io_poll_iocb poll; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 466 | struct io_accept accept; |
| 467 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 468 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 469 | struct io_timeout timeout; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 470 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 471 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 472 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 473 | struct io_close close; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 474 | struct io_files_update files_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 475 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 476 | struct io_madvise madvise; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 477 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 478 | |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 479 | struct io_async_ctx *io; |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 480 | /* |
| 481 | * llist_node is only used for poll deferred completions |
| 482 | */ |
| 483 | struct llist_node llist_node; |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 484 | bool has_user; |
| 485 | bool in_async; |
| 486 | bool needs_fixed_file; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 487 | u8 opcode; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 488 | |
| 489 | struct io_ring_ctx *ctx; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 490 | union { |
| 491 | struct list_head list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 492 | struct hlist_node hash_node; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 493 | }; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 494 | struct list_head link_list; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 495 | unsigned int flags; |
Jens Axboe | c16361c | 2019-01-17 08:39:48 -0700 | [diff] [blame] | 496 | refcount_t refs; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 497 | #define REQ_F_NOWAIT 1 /* must not punt to workers */ |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 498 | #define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */ |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 499 | #define REQ_F_FIXED_FILE 4 /* ctx owns file */ |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 500 | #define REQ_F_LINK_NEXT 8 /* already grabbed next link */ |
Stefan Bühler | e2033e3 | 2019-05-11 19:08:01 +0200 | [diff] [blame] | 501 | #define REQ_F_IO_DRAIN 16 /* drain existing IO first */ |
| 502 | #define REQ_F_IO_DRAINED 32 /* drain done */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 503 | #define REQ_F_LINK 64 /* linked sqes */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 504 | #define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */ |
Zhengyuan Liu | f7b76ac | 2019-07-16 23:26:14 +0800 | [diff] [blame] | 505 | #define REQ_F_FAIL_LINK 256 /* fail rest of links */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 506 | #define REQ_F_TIMEOUT 1024 /* timeout request */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 507 | #define REQ_F_ISREG 2048 /* regular file */ |
| 508 | #define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */ |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 509 | #define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */ |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 510 | #define REQ_F_INFLIGHT 16384 /* on inflight list */ |
| 511 | #define REQ_F_COMP_LOCKED 32768 /* completion under lock */ |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 512 | #define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */ |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 513 | #define REQ_F_FORCE_ASYNC 131072 /* IOSQE_ASYNC */ |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 514 | #define REQ_F_CUR_POS 262144 /* read/write uses file position */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 515 | u64 user_data; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 516 | u32 result; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 517 | u32 sequence; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 518 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 519 | struct list_head inflight_entry; |
| 520 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 521 | struct io_wq_work work; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 522 | }; |
| 523 | |
| 524 | #define IO_PLUG_THRESHOLD 2 |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 525 | #define IO_IOPOLL_BATCH 8 |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 526 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 527 | struct io_submit_state { |
| 528 | struct blk_plug plug; |
| 529 | |
| 530 | /* |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 531 | * io_kiocb alloc cache |
| 532 | */ |
| 533 | void *reqs[IO_IOPOLL_BATCH]; |
| 534 | unsigned int free_reqs; |
| 535 | unsigned int cur_req; |
| 536 | |
| 537 | /* |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 538 | * File reference cache |
| 539 | */ |
| 540 | struct file *file; |
| 541 | unsigned int fd; |
| 542 | unsigned int has_refs; |
| 543 | unsigned int used_refs; |
| 544 | unsigned int ios_left; |
| 545 | }; |
| 546 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 547 | struct io_op_def { |
| 548 | /* needs req->io allocated for deferral/async */ |
| 549 | unsigned async_ctx : 1; |
| 550 | /* needs current->mm setup, does mm access */ |
| 551 | unsigned needs_mm : 1; |
| 552 | /* needs req->file assigned */ |
| 553 | unsigned needs_file : 1; |
| 554 | /* needs req->file assigned IFF fd is >= 0 */ |
| 555 | unsigned fd_non_neg : 1; |
| 556 | /* hash wq insertion if file is a regular file */ |
| 557 | unsigned hash_reg_file : 1; |
| 558 | /* unbound wq insertion if file is a non-regular file */ |
| 559 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 560 | /* opcode is not supported by this kernel */ |
| 561 | unsigned not_supported : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 562 | }; |
| 563 | |
| 564 | static const struct io_op_def io_op_defs[] = { |
| 565 | { |
| 566 | /* IORING_OP_NOP */ |
| 567 | }, |
| 568 | { |
| 569 | /* IORING_OP_READV */ |
| 570 | .async_ctx = 1, |
| 571 | .needs_mm = 1, |
| 572 | .needs_file = 1, |
| 573 | .unbound_nonreg_file = 1, |
| 574 | }, |
| 575 | { |
| 576 | /* IORING_OP_WRITEV */ |
| 577 | .async_ctx = 1, |
| 578 | .needs_mm = 1, |
| 579 | .needs_file = 1, |
| 580 | .hash_reg_file = 1, |
| 581 | .unbound_nonreg_file = 1, |
| 582 | }, |
| 583 | { |
| 584 | /* IORING_OP_FSYNC */ |
| 585 | .needs_file = 1, |
| 586 | }, |
| 587 | { |
| 588 | /* IORING_OP_READ_FIXED */ |
| 589 | .needs_file = 1, |
| 590 | .unbound_nonreg_file = 1, |
| 591 | }, |
| 592 | { |
| 593 | /* IORING_OP_WRITE_FIXED */ |
| 594 | .needs_file = 1, |
| 595 | .hash_reg_file = 1, |
| 596 | .unbound_nonreg_file = 1, |
| 597 | }, |
| 598 | { |
| 599 | /* IORING_OP_POLL_ADD */ |
| 600 | .needs_file = 1, |
| 601 | .unbound_nonreg_file = 1, |
| 602 | }, |
| 603 | { |
| 604 | /* IORING_OP_POLL_REMOVE */ |
| 605 | }, |
| 606 | { |
| 607 | /* IORING_OP_SYNC_FILE_RANGE */ |
| 608 | .needs_file = 1, |
| 609 | }, |
| 610 | { |
| 611 | /* IORING_OP_SENDMSG */ |
| 612 | .async_ctx = 1, |
| 613 | .needs_mm = 1, |
| 614 | .needs_file = 1, |
| 615 | .unbound_nonreg_file = 1, |
| 616 | }, |
| 617 | { |
| 618 | /* IORING_OP_RECVMSG */ |
| 619 | .async_ctx = 1, |
| 620 | .needs_mm = 1, |
| 621 | .needs_file = 1, |
| 622 | .unbound_nonreg_file = 1, |
| 623 | }, |
| 624 | { |
| 625 | /* IORING_OP_TIMEOUT */ |
| 626 | .async_ctx = 1, |
| 627 | .needs_mm = 1, |
| 628 | }, |
| 629 | { |
| 630 | /* IORING_OP_TIMEOUT_REMOVE */ |
| 631 | }, |
| 632 | { |
| 633 | /* IORING_OP_ACCEPT */ |
| 634 | .needs_mm = 1, |
| 635 | .needs_file = 1, |
| 636 | .unbound_nonreg_file = 1, |
| 637 | }, |
| 638 | { |
| 639 | /* IORING_OP_ASYNC_CANCEL */ |
| 640 | }, |
| 641 | { |
| 642 | /* IORING_OP_LINK_TIMEOUT */ |
| 643 | .async_ctx = 1, |
| 644 | .needs_mm = 1, |
| 645 | }, |
| 646 | { |
| 647 | /* IORING_OP_CONNECT */ |
| 648 | .async_ctx = 1, |
| 649 | .needs_mm = 1, |
| 650 | .needs_file = 1, |
| 651 | .unbound_nonreg_file = 1, |
| 652 | }, |
| 653 | { |
| 654 | /* IORING_OP_FALLOCATE */ |
| 655 | .needs_file = 1, |
| 656 | }, |
| 657 | { |
| 658 | /* IORING_OP_OPENAT */ |
| 659 | .needs_file = 1, |
| 660 | .fd_non_neg = 1, |
| 661 | }, |
| 662 | { |
| 663 | /* IORING_OP_CLOSE */ |
| 664 | .needs_file = 1, |
| 665 | }, |
| 666 | { |
| 667 | /* IORING_OP_FILES_UPDATE */ |
| 668 | .needs_mm = 1, |
| 669 | }, |
| 670 | { |
| 671 | /* IORING_OP_STATX */ |
| 672 | .needs_mm = 1, |
| 673 | .needs_file = 1, |
| 674 | .fd_non_neg = 1, |
| 675 | }, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 676 | { |
| 677 | /* IORING_OP_READ */ |
| 678 | .needs_mm = 1, |
| 679 | .needs_file = 1, |
| 680 | .unbound_nonreg_file = 1, |
| 681 | }, |
| 682 | { |
| 683 | /* IORING_OP_WRITE */ |
| 684 | .needs_mm = 1, |
| 685 | .needs_file = 1, |
| 686 | .unbound_nonreg_file = 1, |
| 687 | }, |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 688 | { |
| 689 | /* IORING_OP_FADVISE */ |
| 690 | .needs_file = 1, |
| 691 | }, |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 692 | { |
| 693 | /* IORING_OP_MADVISE */ |
| 694 | .needs_mm = 1, |
| 695 | }, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 696 | { |
| 697 | /* IORING_OP_SEND */ |
| 698 | .needs_mm = 1, |
| 699 | .needs_file = 1, |
| 700 | .unbound_nonreg_file = 1, |
| 701 | }, |
| 702 | { |
| 703 | /* IORING_OP_RECV */ |
| 704 | .needs_mm = 1, |
| 705 | .needs_file = 1, |
| 706 | .unbound_nonreg_file = 1, |
| 707 | }, |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 708 | { |
| 709 | /* IORING_OP_OPENAT2 */ |
| 710 | .needs_file = 1, |
| 711 | .fd_non_neg = 1, |
| 712 | }, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 713 | }; |
| 714 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 715 | static void io_wq_submit_work(struct io_wq_work **workptr); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 716 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 717 | static void io_put_req(struct io_kiocb *req); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 718 | static void __io_double_put_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 719 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| 720 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 721 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 722 | struct io_uring_files_update *ip, |
| 723 | unsigned nr_args); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 724 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 725 | static struct kmem_cache *req_cachep; |
| 726 | |
| 727 | static const struct file_operations io_uring_fops; |
| 728 | |
| 729 | struct sock *io_uring_get_socket(struct file *file) |
| 730 | { |
| 731 | #if defined(CONFIG_UNIX) |
| 732 | if (file->f_op == &io_uring_fops) { |
| 733 | struct io_ring_ctx *ctx = file->private_data; |
| 734 | |
| 735 | return ctx->ring_sock->sk; |
| 736 | } |
| 737 | #endif |
| 738 | return NULL; |
| 739 | } |
| 740 | EXPORT_SYMBOL(io_uring_get_socket); |
| 741 | |
| 742 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 743 | { |
| 744 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 745 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 746 | complete(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 750 | { |
| 751 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 752 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 753 | |
| 754 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 755 | if (!ctx) |
| 756 | return NULL; |
| 757 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 758 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| 759 | if (!ctx->fallback_req) |
| 760 | goto err; |
| 761 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 762 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
| 763 | if (!ctx->completions) |
| 764 | goto err; |
| 765 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 766 | /* |
| 767 | * Use 5 bits less than the max cq entries, that should give us around |
| 768 | * 32 entries per hash list if totally full and uniformly spread. |
| 769 | */ |
| 770 | hash_bits = ilog2(p->cq_entries); |
| 771 | hash_bits -= 5; |
| 772 | if (hash_bits <= 0) |
| 773 | hash_bits = 1; |
| 774 | ctx->cancel_hash_bits = hash_bits; |
| 775 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 776 | GFP_KERNEL); |
| 777 | if (!ctx->cancel_hash) |
| 778 | goto err; |
| 779 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 780 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 781 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 782 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 783 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 784 | |
| 785 | ctx->flags = p->flags; |
| 786 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 787 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 788 | init_completion(&ctx->completions[0]); |
| 789 | init_completion(&ctx->completions[1]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 790 | mutex_init(&ctx->uring_lock); |
| 791 | init_waitqueue_head(&ctx->wait); |
| 792 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 793 | init_llist_head(&ctx->poll_llist); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 794 | INIT_LIST_HEAD(&ctx->poll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 795 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 796 | INIT_LIST_HEAD(&ctx->timeout_list); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 797 | init_waitqueue_head(&ctx->inflight_wait); |
| 798 | spin_lock_init(&ctx->inflight_lock); |
| 799 | INIT_LIST_HEAD(&ctx->inflight_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 800 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 801 | err: |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 802 | if (ctx->fallback_req) |
| 803 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 804 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 805 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 806 | kfree(ctx); |
| 807 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 810 | static inline bool __req_need_defer(struct io_kiocb *req) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 811 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 812 | struct io_ring_ctx *ctx = req->ctx; |
| 813 | |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 814 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 815 | + atomic_read(&ctx->cached_cq_overflow); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 816 | } |
| 817 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 818 | static inline bool req_need_defer(struct io_kiocb *req) |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 819 | { |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 820 | if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN) |
| 821 | return __req_need_defer(req); |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 822 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 823 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | 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] | 827 | { |
| 828 | struct io_kiocb *req; |
| 829 | |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 830 | 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] | 831 | if (req && !req_need_defer(req)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 832 | list_del_init(&req->list); |
| 833 | return req; |
| 834 | } |
| 835 | |
| 836 | return NULL; |
| 837 | } |
| 838 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 839 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 840 | { |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 841 | struct io_kiocb *req; |
| 842 | |
| 843 | 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] | 844 | if (req) { |
| 845 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 846 | return NULL; |
Linus Torvalds | fb4b3d3 | 2019-11-25 10:40:27 -0800 | [diff] [blame] | 847 | if (!__req_need_defer(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 848 | list_del_init(&req->list); |
| 849 | return req; |
| 850 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | return NULL; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 854 | } |
| 855 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 856 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 857 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 858 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 859 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 860 | /* order cqe stores with ring update */ |
| 861 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 862 | |
Pavel Begunkov | 0791015 | 2020-01-17 03:52:46 +0300 | [diff] [blame] | 863 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 864 | wake_up_interruptible(&ctx->cq_wait); |
| 865 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 869 | static inline bool io_prep_async_work(struct io_kiocb *req, |
| 870 | struct io_kiocb **link) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 871 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 872 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 873 | bool do_hashed = false; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 874 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 875 | if (req->flags & REQ_F_ISREG) { |
| 876 | if (def->hash_reg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 877 | do_hashed = true; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 878 | } else { |
| 879 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 880 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 881 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 882 | if (def->needs_mm) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 883 | req->work.flags |= IO_WQ_WORK_NEEDS_USER; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 884 | |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 885 | *link = io_prep_linked_timeout(req); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 886 | return do_hashed; |
| 887 | } |
| 888 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 889 | static inline void io_queue_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 890 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 891 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 892 | struct io_kiocb *link; |
| 893 | bool do_hashed; |
| 894 | |
| 895 | do_hashed = io_prep_async_work(req, &link); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 896 | |
| 897 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, |
| 898 | req->flags); |
| 899 | if (!do_hashed) { |
| 900 | io_wq_enqueue(ctx->io_wq, &req->work); |
| 901 | } else { |
| 902 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, |
| 903 | file_inode(req->file)); |
| 904 | } |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 905 | |
| 906 | if (link) |
| 907 | io_queue_linked_timeout(link); |
Jens Axboe | 18d9be1 | 2019-09-10 09:13:05 -0600 | [diff] [blame] | 908 | } |
| 909 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 910 | static void io_kill_timeout(struct io_kiocb *req) |
| 911 | { |
| 912 | int ret; |
| 913 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 914 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 915 | if (ret != -1) { |
| 916 | atomic_inc(&req->ctx->cq_timeouts); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 917 | list_del_init(&req->list); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 918 | io_cqring_fill_event(req, 0); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 919 | io_put_req(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | |
| 923 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 924 | { |
| 925 | struct io_kiocb *req, *tmp; |
| 926 | |
| 927 | spin_lock_irq(&ctx->completion_lock); |
| 928 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 929 | io_kill_timeout(req); |
| 930 | spin_unlock_irq(&ctx->completion_lock); |
| 931 | } |
| 932 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 933 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 934 | { |
| 935 | struct io_kiocb *req; |
| 936 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 937 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 938 | io_kill_timeout(req); |
| 939 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 940 | __io_commit_cqring(ctx); |
| 941 | |
| 942 | while ((req = io_get_deferred_req(ctx)) != NULL) { |
| 943 | req->flags |= REQ_F_IO_DRAINED; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 944 | io_queue_async_work(req); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 948 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 949 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 950 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 951 | unsigned tail; |
| 952 | |
| 953 | tail = ctx->cached_cq_tail; |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 954 | /* |
| 955 | * writes to the cq entry need to come after reading head; the |
| 956 | * control dependency is enough as we're using WRITE_ONCE to |
| 957 | * fill the cq entry |
| 958 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 959 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 960 | return NULL; |
| 961 | |
| 962 | ctx->cached_cq_tail++; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 963 | return &rings->cqes[tail & ctx->cq_mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 966 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 967 | { |
| 968 | if (!ctx->eventfd_async) |
| 969 | return true; |
| 970 | return io_wq_current_is_worker() || in_interrupt(); |
| 971 | } |
| 972 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 973 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| 974 | { |
| 975 | if (waitqueue_active(&ctx->wait)) |
| 976 | wake_up(&ctx->wait); |
| 977 | if (waitqueue_active(&ctx->sqo_wait)) |
| 978 | wake_up(&ctx->sqo_wait); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 979 | if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 980 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 981 | } |
| 982 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 983 | /* Returns true if there are no backlogged entries after the flush */ |
| 984 | 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] | 985 | { |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 986 | struct io_rings *rings = ctx->rings; |
| 987 | struct io_uring_cqe *cqe; |
| 988 | struct io_kiocb *req; |
| 989 | unsigned long flags; |
| 990 | LIST_HEAD(list); |
| 991 | |
| 992 | if (!force) { |
| 993 | if (list_empty_careful(&ctx->cq_overflow_list)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 994 | return true; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 995 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| 996 | rings->cq_ring_entries)) |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 997 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1001 | |
| 1002 | /* if force is set, the ring is going away. always drop after that */ |
| 1003 | if (force) |
Jens Axboe | 69b3e54 | 2020-01-08 11:01:46 -0700 | [diff] [blame] | 1004 | ctx->cq_overflow_flushed = 1; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1005 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1006 | cqe = NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1007 | while (!list_empty(&ctx->cq_overflow_list)) { |
| 1008 | cqe = io_get_cqring(ctx); |
| 1009 | if (!cqe && !force) |
| 1010 | break; |
| 1011 | |
| 1012 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, |
| 1013 | list); |
| 1014 | list_move(&req->list, &list); |
| 1015 | if (cqe) { |
| 1016 | WRITE_ONCE(cqe->user_data, req->user_data); |
| 1017 | WRITE_ONCE(cqe->res, req->result); |
| 1018 | WRITE_ONCE(cqe->flags, 0); |
| 1019 | } else { |
| 1020 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1021 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | io_commit_cqring(ctx); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1026 | if (cqe) { |
| 1027 | clear_bit(0, &ctx->sq_check_overflow); |
| 1028 | clear_bit(0, &ctx->cq_check_overflow); |
| 1029 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1030 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1031 | io_cqring_ev_posted(ctx); |
| 1032 | |
| 1033 | while (!list_empty(&list)) { |
| 1034 | req = list_first_entry(&list, struct io_kiocb, list); |
| 1035 | list_del(&req->list); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1036 | io_put_req(req); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1037 | } |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1038 | |
| 1039 | return cqe != NULL; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1042 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1043 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1044 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1045 | struct io_uring_cqe *cqe; |
| 1046 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1047 | trace_io_uring_complete(ctx, req->user_data, res); |
Jens Axboe | 51c3ff6 | 2019-11-03 06:52:50 -0700 | [diff] [blame] | 1048 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1049 | /* |
| 1050 | * If we can't get a cq entry, userspace overflowed the |
| 1051 | * submission (by quite a lot). Increment the overflow count in |
| 1052 | * the ring. |
| 1053 | */ |
| 1054 | cqe = io_get_cqring(ctx); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1055 | if (likely(cqe)) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1056 | WRITE_ONCE(cqe->user_data, req->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1057 | WRITE_ONCE(cqe->res, res); |
| 1058 | WRITE_ONCE(cqe->flags, 0); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1059 | } else if (ctx->cq_overflow_flushed) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1060 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 1061 | atomic_inc_return(&ctx->cached_cq_overflow)); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1062 | } else { |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1063 | if (list_empty(&ctx->cq_overflow_list)) { |
| 1064 | set_bit(0, &ctx->sq_check_overflow); |
| 1065 | set_bit(0, &ctx->cq_check_overflow); |
| 1066 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1067 | refcount_inc(&req->refs); |
| 1068 | req->result = res; |
| 1069 | list_add_tail(&req->list, &ctx->cq_overflow_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1073 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1074 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1075 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1076 | unsigned long flags; |
| 1077 | |
| 1078 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1079 | io_cqring_fill_event(req, res); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1080 | io_commit_cqring(ctx); |
| 1081 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1082 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1083 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1086 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
| 1087 | { |
| 1088 | return req == (struct io_kiocb *) |
| 1089 | ((unsigned long) req->ctx->fallback_req & ~1UL); |
| 1090 | } |
| 1091 | |
| 1092 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| 1093 | { |
| 1094 | struct io_kiocb *req; |
| 1095 | |
| 1096 | req = ctx->fallback_req; |
| 1097 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) |
| 1098 | return req; |
| 1099 | |
| 1100 | return NULL; |
| 1101 | } |
| 1102 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1103 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 1104 | struct io_submit_state *state) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1105 | { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1106 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1107 | struct io_kiocb *req; |
| 1108 | |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1109 | if (!state) { |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1110 | req = kmem_cache_alloc(req_cachep, gfp); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1111 | if (unlikely(!req)) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1112 | goto fallback; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1113 | } else if (!state->free_reqs) { |
| 1114 | size_t sz; |
| 1115 | int ret; |
| 1116 | |
| 1117 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1118 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 1119 | |
| 1120 | /* |
| 1121 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 1122 | * retry single alloc to be on the safe side. |
| 1123 | */ |
| 1124 | if (unlikely(ret <= 0)) { |
| 1125 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 1126 | if (!state->reqs[0]) |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1127 | goto fallback; |
Jens Axboe | fd6fab2 | 2019-03-14 16:30:06 -0600 | [diff] [blame] | 1128 | ret = 1; |
| 1129 | } |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1130 | state->free_reqs = ret - 1; |
| 1131 | state->cur_req = 1; |
| 1132 | req = state->reqs[0]; |
| 1133 | } else { |
| 1134 | req = state->reqs[state->cur_req]; |
| 1135 | state->free_reqs--; |
| 1136 | state->cur_req++; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1139 | got_it: |
Jens Axboe | 1a6b74f | 2019-12-02 10:33:15 -0700 | [diff] [blame] | 1140 | req->io = NULL; |
Jens Axboe | 60c112b | 2019-06-21 10:20:18 -0600 | [diff] [blame] | 1141 | req->file = NULL; |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1142 | req->ctx = ctx; |
| 1143 | req->flags = 0; |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1144 | /* one is dropped after submission, the other at completion */ |
| 1145 | refcount_set(&req->refs, 2); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1146 | req->result = 0; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1147 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 1148 | return req; |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 1149 | fallback: |
| 1150 | req = io_get_fallback_req(ctx); |
| 1151 | if (req) |
| 1152 | goto got_it; |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 1153 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1154 | return NULL; |
| 1155 | } |
| 1156 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1157 | static void __io_req_do_free(struct io_kiocb *req) |
| 1158 | { |
| 1159 | if (likely(!io_is_fallback_req(req))) |
| 1160 | kmem_cache_free(req_cachep, req); |
| 1161 | else |
| 1162 | clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req); |
| 1163 | } |
| 1164 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1165 | static void __io_req_aux_free(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1166 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1167 | struct io_ring_ctx *ctx = req->ctx; |
| 1168 | |
YueHaibing | 96fd84d | 2020-01-07 22:22:44 +0800 | [diff] [blame] | 1169 | kfree(req->io); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 1170 | if (req->file) { |
| 1171 | if (req->flags & REQ_F_FIXED_FILE) |
| 1172 | percpu_ref_put(&ctx->file_data->refs); |
| 1173 | else |
| 1174 | fput(req->file); |
| 1175 | } |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | static void __io_free_req(struct io_kiocb *req) |
| 1179 | { |
| 1180 | __io_req_aux_free(req); |
| 1181 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1182 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1183 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 1184 | unsigned long flags; |
| 1185 | |
| 1186 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1187 | list_del(&req->inflight_entry); |
| 1188 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1189 | wake_up(&ctx->inflight_wait); |
| 1190 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1191 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 1192 | |
| 1193 | percpu_ref_put(&req->ctx->refs); |
| 1194 | __io_req_do_free(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1195 | } |
| 1196 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1197 | struct req_batch { |
| 1198 | void *reqs[IO_IOPOLL_BATCH]; |
| 1199 | int to_free; |
| 1200 | int need_iter; |
| 1201 | }; |
| 1202 | |
| 1203 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) |
| 1204 | { |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1205 | int fixed_refs = rb->to_free; |
| 1206 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1207 | if (!rb->to_free) |
| 1208 | return; |
| 1209 | if (rb->need_iter) { |
| 1210 | int i, inflight = 0; |
| 1211 | unsigned long flags; |
| 1212 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1213 | fixed_refs = 0; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1214 | for (i = 0; i < rb->to_free; i++) { |
| 1215 | struct io_kiocb *req = rb->reqs[i]; |
| 1216 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1217 | if (req->flags & REQ_F_FIXED_FILE) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1218 | req->file = NULL; |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1219 | fixed_refs++; |
| 1220 | } |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1221 | if (req->flags & REQ_F_INFLIGHT) |
| 1222 | inflight++; |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1223 | __io_req_aux_free(req); |
| 1224 | } |
| 1225 | if (!inflight) |
| 1226 | goto do_free; |
| 1227 | |
| 1228 | spin_lock_irqsave(&ctx->inflight_lock, flags); |
| 1229 | for (i = 0; i < rb->to_free; i++) { |
| 1230 | struct io_kiocb *req = rb->reqs[i]; |
| 1231 | |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1232 | if (req->flags & REQ_F_INFLIGHT) { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1233 | list_del(&req->inflight_entry); |
| 1234 | if (!--inflight) |
| 1235 | break; |
| 1236 | } |
| 1237 | } |
| 1238 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); |
| 1239 | |
| 1240 | if (waitqueue_active(&ctx->inflight_wait)) |
| 1241 | wake_up(&ctx->inflight_wait); |
| 1242 | } |
| 1243 | do_free: |
| 1244 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
Jens Axboe | 10fef4b | 2020-01-09 07:52:28 -0700 | [diff] [blame] | 1245 | if (fixed_refs) |
| 1246 | percpu_ref_put_many(&ctx->file_data->refs, fixed_refs); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1247 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1248 | rb->to_free = rb->need_iter = 0; |
| 1249 | } |
| 1250 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1251 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1252 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1253 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1254 | int ret; |
| 1255 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 1256 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1257 | if (ret != -1) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1258 | io_cqring_fill_event(req, -ECANCELED); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1259 | io_commit_cqring(ctx); |
| 1260 | req->flags &= ~REQ_F_LINK; |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1261 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1262 | return true; |
| 1263 | } |
| 1264 | |
| 1265 | return false; |
| 1266 | } |
| 1267 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1268 | 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] | 1269 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1270 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1271 | bool wake_ev = false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1272 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1273 | /* Already got next link */ |
| 1274 | if (req->flags & REQ_F_LINK_NEXT) |
| 1275 | return; |
| 1276 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1277 | /* |
| 1278 | * The list should never be empty when we are called here. But could |
| 1279 | * potentially happen if the chain is messed up, check to be on the |
| 1280 | * safe side. |
| 1281 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1282 | while (!list_empty(&req->link_list)) { |
| 1283 | struct io_kiocb *nxt = list_first_entry(&req->link_list, |
| 1284 | struct io_kiocb, link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1285 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1286 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
| 1287 | (nxt->flags & REQ_F_TIMEOUT))) { |
| 1288 | list_del_init(&nxt->link_list); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1289 | wake_ev |= io_link_cancel_timeout(nxt); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1290 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
| 1291 | continue; |
| 1292 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1293 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1294 | list_del_init(&req->link_list); |
| 1295 | if (!list_empty(&nxt->link_list)) |
| 1296 | nxt->flags |= REQ_F_LINK; |
Pavel Begunkov | b18fdf7 | 2019-11-21 23:21:02 +0300 | [diff] [blame] | 1297 | *nxtptr = nxt; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1298 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1299 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1300 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1301 | req->flags |= REQ_F_LINK_NEXT; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1302 | if (wake_ev) |
| 1303 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | /* |
| 1307 | * Called if REQ_F_LINK is set, and we fail the head request |
| 1308 | */ |
| 1309 | static void io_fail_links(struct io_kiocb *req) |
| 1310 | { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1311 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1312 | unsigned long flags; |
| 1313 | |
| 1314 | spin_lock_irqsave(&ctx->completion_lock, flags); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1315 | |
| 1316 | while (!list_empty(&req->link_list)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1317 | struct io_kiocb *link = list_first_entry(&req->link_list, |
| 1318 | struct io_kiocb, link_list); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1319 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 1320 | list_del_init(&link->link_list); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 1321 | trace_io_uring_fail_link(req, link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1322 | |
| 1323 | if ((req->flags & REQ_F_LINK_TIMEOUT) && |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1324 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1325 | io_link_cancel_timeout(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1326 | } else { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1327 | io_cqring_fill_event(link, -ECANCELED); |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1328 | __io_double_put_req(link); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1329 | } |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 1330 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1331 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1332 | |
| 1333 | io_commit_cqring(ctx); |
| 1334 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1335 | io_cqring_ev_posted(ctx); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1336 | } |
| 1337 | |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1338 | 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] | 1339 | { |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1340 | if (likely(!(req->flags & REQ_F_LINK))) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1341 | return; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1342 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1343 | /* |
| 1344 | * If LINK is set, we have dependent requests in this chain. If we |
| 1345 | * didn't fail this request, queue the first one up, moving any other |
| 1346 | * dependencies to the next request. In case of failure, fail the rest |
| 1347 | * of the chain. |
| 1348 | */ |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1349 | if (req->flags & REQ_F_FAIL_LINK) { |
| 1350 | io_fail_links(req); |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1351 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
| 1352 | REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1353 | struct io_ring_ctx *ctx = req->ctx; |
| 1354 | unsigned long flags; |
| 1355 | |
| 1356 | /* |
| 1357 | * If this is a timeout link, we could be racing with the |
| 1358 | * timeout timer. Grab the completion lock for this case to |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 1359 | * protect against that. |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 1360 | */ |
| 1361 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1362 | io_req_link_next(req, nxt); |
| 1363 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1364 | } else { |
| 1365 | io_req_link_next(req, nxt); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1366 | } |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1367 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1368 | |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1369 | static void io_free_req(struct io_kiocb *req) |
| 1370 | { |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1371 | struct io_kiocb *nxt = NULL; |
| 1372 | |
| 1373 | io_req_find_next(req, &nxt); |
Pavel Begunkov | 70cf9f3 | 2019-11-21 23:21:00 +0300 | [diff] [blame] | 1374 | __io_free_req(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 1375 | |
| 1376 | if (nxt) |
| 1377 | io_queue_async_work(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 1378 | } |
| 1379 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1380 | /* |
| 1381 | * Drop reference to request, return next in chain (if there is one) if this |
| 1382 | * was the last reference to this request. |
| 1383 | */ |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1384 | __attribute__((nonnull)) |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1385 | 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] | 1386 | { |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1387 | io_req_find_next(req, nxtptr); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1388 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1389 | if (refcount_dec_and_test(&req->refs)) |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 1390 | __io_free_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1393 | static void io_put_req(struct io_kiocb *req) |
| 1394 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1395 | if (refcount_dec_and_test(&req->refs)) |
| 1396 | io_free_req(req); |
| 1397 | } |
| 1398 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1399 | /* |
| 1400 | * Must only be used if we don't need to care about links, usually from |
| 1401 | * within the completion handling itself. |
| 1402 | */ |
| 1403 | static void __io_double_put_req(struct io_kiocb *req) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1404 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1405 | /* drop both submit and complete references */ |
| 1406 | if (refcount_sub_and_test(2, &req->refs)) |
| 1407 | __io_free_req(req); |
| 1408 | } |
| 1409 | |
Jens Axboe | 978db57 | 2019-11-14 22:39:04 -0700 | [diff] [blame] | 1410 | static void io_double_put_req(struct io_kiocb *req) |
| 1411 | { |
| 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 | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1417 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1418 | { |
Jens Axboe | 84f97dc | 2019-11-06 11:27:53 -0700 | [diff] [blame] | 1419 | struct io_rings *rings = ctx->rings; |
| 1420 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1421 | if (test_bit(0, &ctx->cq_check_overflow)) { |
| 1422 | /* |
| 1423 | * noflush == true is from the waitqueue handler, just ensure |
| 1424 | * we wake up the task, and the next invocation will flush the |
| 1425 | * entries. We cannot safely to it from here. |
| 1426 | */ |
| 1427 | if (noflush && !list_empty(&ctx->cq_overflow_list)) |
| 1428 | return -1U; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1429 | |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1430 | io_cqring_overflow_flush(ctx, false); |
| 1431 | } |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1432 | |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1433 | /* See comment at the top of this file */ |
| 1434 | smp_rmb(); |
Jens Axboe | ad3eb2c | 2019-12-18 17:12:20 -0700 | [diff] [blame] | 1435 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1436 | } |
| 1437 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 1438 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 1439 | { |
| 1440 | struct io_rings *rings = ctx->rings; |
| 1441 | |
| 1442 | /* make sure SQ entry isn't read before tail */ |
| 1443 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 1444 | } |
| 1445 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1446 | 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] | 1447 | { |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1448 | if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req)) |
| 1449 | return false; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1450 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1451 | if (!(req->flags & REQ_F_FIXED_FILE) || req->io) |
| 1452 | rb->need_iter++; |
| 1453 | |
| 1454 | rb->reqs[rb->to_free++] = req; |
| 1455 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| 1456 | io_free_req_many(req->ctx, rb); |
| 1457 | return true; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1460 | /* |
| 1461 | * Find and free completed poll iocbs |
| 1462 | */ |
| 1463 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1464 | struct list_head *done) |
| 1465 | { |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1466 | struct req_batch rb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1467 | struct io_kiocb *req; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1468 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 1469 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1470 | while (!list_empty(done)) { |
| 1471 | req = list_first_entry(done, struct io_kiocb, list); |
| 1472 | list_del(&req->list); |
| 1473 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1474 | io_cqring_fill_event(req, req->result); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1475 | (*nr_events)++; |
| 1476 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1477 | if (refcount_dec_and_test(&req->refs) && |
| 1478 | !io_req_multi_free(&rb, req)) |
| 1479 | io_free_req(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1480 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1481 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1482 | io_commit_cqring(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 1483 | io_free_req_many(ctx, &rb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1487 | long min) |
| 1488 | { |
| 1489 | struct io_kiocb *req, *tmp; |
| 1490 | LIST_HEAD(done); |
| 1491 | bool spin; |
| 1492 | int ret; |
| 1493 | |
| 1494 | /* |
| 1495 | * Only spin for completions if we don't have multiple devices hanging |
| 1496 | * off our complete list, and we're under the requested amount. |
| 1497 | */ |
| 1498 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 1499 | |
| 1500 | ret = 0; |
| 1501 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1502 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1503 | |
| 1504 | /* |
| 1505 | * Move completed entries to our local list. If we find a |
| 1506 | * request that requires polling, break out and complete |
| 1507 | * the done list first, if we have entries there. |
| 1508 | */ |
| 1509 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 1510 | list_move_tail(&req->list, &done); |
| 1511 | continue; |
| 1512 | } |
| 1513 | if (!list_empty(&done)) |
| 1514 | break; |
| 1515 | |
| 1516 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 1517 | if (ret < 0) |
| 1518 | break; |
| 1519 | |
| 1520 | if (ret && spin) |
| 1521 | spin = false; |
| 1522 | ret = 0; |
| 1523 | } |
| 1524 | |
| 1525 | if (!list_empty(&done)) |
| 1526 | io_iopoll_complete(ctx, nr_events, &done); |
| 1527 | |
| 1528 | return ret; |
| 1529 | } |
| 1530 | |
| 1531 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 1532 | * 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] | 1533 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 1534 | * as a non-spinning completion check. |
| 1535 | */ |
| 1536 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 1537 | long min) |
| 1538 | { |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1539 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1540 | int ret; |
| 1541 | |
| 1542 | ret = io_do_iopoll(ctx, nr_events, min); |
| 1543 | if (ret < 0) |
| 1544 | return ret; |
| 1545 | if (!min || *nr_events >= min) |
| 1546 | return 0; |
| 1547 | } |
| 1548 | |
| 1549 | return 1; |
| 1550 | } |
| 1551 | |
| 1552 | /* |
| 1553 | * We can't just wait for polled events to come to us, we have to actively |
| 1554 | * find and complete them. |
| 1555 | */ |
| 1556 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 1557 | { |
| 1558 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1559 | return; |
| 1560 | |
| 1561 | mutex_lock(&ctx->uring_lock); |
| 1562 | while (!list_empty(&ctx->poll_list)) { |
| 1563 | unsigned int nr_events = 0; |
| 1564 | |
| 1565 | io_iopoll_getevents(ctx, &nr_events, 1); |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 1566 | |
| 1567 | /* |
| 1568 | * Ensure we allow local-to-the-cpu processing to take place, |
| 1569 | * in this case we need to ensure that we reap all events. |
| 1570 | */ |
| 1571 | cond_resched(); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1572 | } |
| 1573 | mutex_unlock(&ctx->uring_lock); |
| 1574 | } |
| 1575 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1576 | static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1577 | long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1578 | { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1579 | int iters = 0, ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1580 | |
| 1581 | do { |
| 1582 | int tmin = 0; |
| 1583 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1584 | /* |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1585 | * Don't enter poll loop if we already have events pending. |
| 1586 | * If we do, we can potentially be spinning for commands that |
| 1587 | * already triggered a CQE (eg in error). |
| 1588 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1589 | if (io_cqring_events(ctx, false)) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 1590 | break; |
| 1591 | |
| 1592 | /* |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1593 | * If a submit got punted to a workqueue, we can have the |
| 1594 | * application entering polling for a command before it gets |
| 1595 | * issued. That app will hold the uring_lock for the duration |
| 1596 | * of the poll right here, so we need to take a breather every |
| 1597 | * now and then to ensure that the issue has a chance to add |
| 1598 | * the poll to the issued list. Otherwise we can spin here |
| 1599 | * forever, while the workqueue is stuck trying to acquire the |
| 1600 | * very same mutex. |
| 1601 | */ |
| 1602 | if (!(++iters & 7)) { |
| 1603 | mutex_unlock(&ctx->uring_lock); |
| 1604 | mutex_lock(&ctx->uring_lock); |
| 1605 | } |
| 1606 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1607 | if (*nr_events < min) |
| 1608 | tmin = min - *nr_events; |
| 1609 | |
| 1610 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 1611 | if (ret <= 0) |
| 1612 | break; |
| 1613 | ret = 0; |
| 1614 | } while (min && !*nr_events && !need_resched()); |
| 1615 | |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 1616 | return ret; |
| 1617 | } |
| 1618 | |
| 1619 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 1620 | long min) |
| 1621 | { |
| 1622 | int ret; |
| 1623 | |
| 1624 | /* |
| 1625 | * We disallow the app entering submit/complete with polling, but we |
| 1626 | * still need to lock the ring to prevent racing with polled issue |
| 1627 | * that got punted to a workqueue. |
| 1628 | */ |
| 1629 | mutex_lock(&ctx->uring_lock); |
| 1630 | ret = __io_iopoll_check(ctx, nr_events, min); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 1631 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1632 | return ret; |
| 1633 | } |
| 1634 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1635 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1636 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1637 | /* |
| 1638 | * Tell lockdep we inherited freeze protection from submission |
| 1639 | * thread. |
| 1640 | */ |
| 1641 | if (req->flags & REQ_F_ISREG) { |
| 1642 | struct inode *inode = file_inode(req->file); |
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 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1645 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1646 | file_end_write(req->file); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1649 | static inline void req_set_fail_links(struct io_kiocb *req) |
| 1650 | { |
| 1651 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| 1652 | req->flags |= REQ_F_FAIL_LINK; |
| 1653 | } |
| 1654 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1655 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1656 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1657 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1658 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1659 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1660 | kiocb_end_write(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1661 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1662 | if (res != req->result) |
| 1663 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1664 | io_cqring_add_event(req, res); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 1668 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1669 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1670 | |
| 1671 | io_complete_rw_common(kiocb, res); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 1672 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1675 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
| 1676 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1677 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1678 | struct io_kiocb *nxt = NULL; |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1679 | |
| 1680 | io_complete_rw_common(kiocb, res); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1681 | io_put_req_find_next(req, &nxt); |
| 1682 | |
| 1683 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1684 | } |
| 1685 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1686 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 1687 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1688 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1689 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1690 | if (kiocb->ki_flags & IOCB_WRITE) |
| 1691 | kiocb_end_write(req); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1692 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 1693 | if (res != req->result) |
| 1694 | req_set_fail_links(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 1695 | req->result = res; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1696 | if (res != -EAGAIN) |
| 1697 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 1698 | } |
| 1699 | |
| 1700 | /* |
| 1701 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 1702 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 1703 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 1704 | * accessing the kiocb cookie. |
| 1705 | */ |
| 1706 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 1707 | { |
| 1708 | struct io_ring_ctx *ctx = req->ctx; |
| 1709 | |
| 1710 | /* |
| 1711 | * Track whether we have multiple files in our lists. This will impact |
| 1712 | * how we do polling eventually, not spinning if we're on potentially |
| 1713 | * different devices. |
| 1714 | */ |
| 1715 | if (list_empty(&ctx->poll_list)) { |
| 1716 | ctx->poll_multi_file = false; |
| 1717 | } else if (!ctx->poll_multi_file) { |
| 1718 | struct io_kiocb *list_req; |
| 1719 | |
| 1720 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1721 | list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1722 | if (list_req->file != req->file) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1723 | ctx->poll_multi_file = true; |
| 1724 | } |
| 1725 | |
| 1726 | /* |
| 1727 | * For fast devices, IO may have already completed. If it has, add |
| 1728 | * it to the front so we find it first. |
| 1729 | */ |
| 1730 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1731 | list_add(&req->list, &ctx->poll_list); |
| 1732 | else |
| 1733 | list_add_tail(&req->list, &ctx->poll_list); |
| 1734 | } |
| 1735 | |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1736 | static void io_file_put(struct io_submit_state *state) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1737 | { |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1738 | if (state->file) { |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1739 | int diff = state->has_refs - state->used_refs; |
| 1740 | |
| 1741 | if (diff) |
| 1742 | fput_many(state->file, diff); |
| 1743 | state->file = NULL; |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | /* |
| 1748 | * Get as many references to a file as we have IOs left in this submission, |
| 1749 | * assuming most submissions are for one file, or at least that each file |
| 1750 | * has more than one submission. |
| 1751 | */ |
| 1752 | static struct file *io_file_get(struct io_submit_state *state, int fd) |
| 1753 | { |
| 1754 | if (!state) |
| 1755 | return fget(fd); |
| 1756 | |
| 1757 | if (state->file) { |
| 1758 | if (state->fd == fd) { |
| 1759 | state->used_refs++; |
| 1760 | state->ios_left--; |
| 1761 | return state->file; |
| 1762 | } |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 1763 | io_file_put(state); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1764 | } |
| 1765 | state->file = fget_many(fd, state->ios_left); |
| 1766 | if (!state->file) |
| 1767 | return NULL; |
| 1768 | |
| 1769 | state->fd = fd; |
| 1770 | state->has_refs = state->ios_left; |
| 1771 | state->used_refs = 1; |
| 1772 | state->ios_left--; |
| 1773 | return state->file; |
| 1774 | } |
| 1775 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1776 | /* |
| 1777 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 1778 | * any file. For now, just ensure that anything potentially problematic is done |
| 1779 | * inline. |
| 1780 | */ |
| 1781 | static bool io_file_supports_async(struct file *file) |
| 1782 | { |
| 1783 | umode_t mode = file_inode(file)->i_mode; |
| 1784 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 1785 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1786 | return true; |
| 1787 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 1788 | return true; |
| 1789 | |
| 1790 | return false; |
| 1791 | } |
| 1792 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1793 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1794 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1795 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1796 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1797 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1798 | unsigned ioprio; |
| 1799 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1800 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1801 | if (!req->file) |
| 1802 | return -EBADF; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1803 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1804 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 1805 | req->flags |= REQ_F_ISREG; |
| 1806 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1807 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1808 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| 1809 | req->flags |= REQ_F_CUR_POS; |
| 1810 | kiocb->ki_pos = req->file->f_pos; |
| 1811 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1812 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 1813 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
| 1814 | |
| 1815 | ioprio = READ_ONCE(sqe->ioprio); |
| 1816 | if (ioprio) { |
| 1817 | ret = ioprio_check_cap(ioprio); |
| 1818 | if (ret) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1819 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1820 | |
| 1821 | kiocb->ki_ioprio = ioprio; |
| 1822 | } else |
| 1823 | kiocb->ki_ioprio = get_current_ioprio(); |
| 1824 | |
| 1825 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 1826 | if (unlikely(ret)) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1827 | return ret; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1828 | |
| 1829 | /* don't allow async punt if RWF_NOWAIT was requested */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 1830 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 1831 | (req->file->f_flags & O_NONBLOCK)) |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1832 | req->flags |= REQ_F_NOWAIT; |
| 1833 | |
| 1834 | if (force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1835 | kiocb->ki_flags |= IOCB_NOWAIT; |
Stefan Bühler | 8449eed | 2019-04-27 20:34:19 +0200 | [diff] [blame] | 1836 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1837 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1838 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 1839 | !kiocb->ki_filp->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1840 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1841 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1842 | kiocb->ki_flags |= IOCB_HIPRI; |
| 1843 | kiocb->ki_complete = io_complete_rw_iopoll; |
Jens Axboe | 6873e0b | 2019-10-30 13:53:09 -0600 | [diff] [blame] | 1844 | req->result = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1845 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 1846 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1847 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 1848 | kiocb->ki_complete = io_complete_rw; |
| 1849 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1850 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1851 | req->rw.addr = READ_ONCE(sqe->addr); |
| 1852 | req->rw.len = READ_ONCE(sqe->len); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1853 | /* we own ->private, reuse it for the buffer index */ |
| 1854 | req->rw.kiocb.private = (void *) (unsigned long) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1855 | READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1856 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 1860 | { |
| 1861 | switch (ret) { |
| 1862 | case -EIOCBQUEUED: |
| 1863 | break; |
| 1864 | case -ERESTARTSYS: |
| 1865 | case -ERESTARTNOINTR: |
| 1866 | case -ERESTARTNOHAND: |
| 1867 | case -ERESTART_RESTARTBLOCK: |
| 1868 | /* |
| 1869 | * We can't just restart the syscall, since previously |
| 1870 | * submitted sqes may already be in progress. Just fail this |
| 1871 | * IO with EINTR. |
| 1872 | */ |
| 1873 | ret = -EINTR; |
| 1874 | /* fall through */ |
| 1875 | default: |
| 1876 | kiocb->ki_complete(kiocb, ret, 0); |
| 1877 | } |
| 1878 | } |
| 1879 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1880 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt, |
| 1881 | bool in_async) |
| 1882 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 1883 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| 1884 | |
| 1885 | if (req->flags & REQ_F_CUR_POS) |
| 1886 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 1887 | if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 1888 | *nxt = __io_complete_rw(kiocb, ret); |
| 1889 | else |
| 1890 | io_rw_done(kiocb, ret); |
| 1891 | } |
| 1892 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1893 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 1894 | struct iov_iter *iter) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1895 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1896 | struct io_ring_ctx *ctx = req->ctx; |
| 1897 | size_t len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1898 | struct io_mapped_ubuf *imu; |
| 1899 | unsigned index, buf_index; |
| 1900 | size_t offset; |
| 1901 | u64 buf_addr; |
| 1902 | |
| 1903 | /* attempt to use fixed buffers without having provided iovecs */ |
| 1904 | if (unlikely(!ctx->user_bufs)) |
| 1905 | return -EFAULT; |
| 1906 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1907 | buf_index = (unsigned long) req->rw.kiocb.private; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1908 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 1909 | return -EFAULT; |
| 1910 | |
| 1911 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 1912 | imu = &ctx->user_bufs[index]; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1913 | buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1914 | |
| 1915 | /* overflow */ |
| 1916 | if (buf_addr + len < buf_addr) |
| 1917 | return -EFAULT; |
| 1918 | /* not inside the mapped region */ |
| 1919 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 1920 | return -EFAULT; |
| 1921 | |
| 1922 | /* |
| 1923 | * May not be a start of buffer, set size appropriately |
| 1924 | * and advance us to the beginning. |
| 1925 | */ |
| 1926 | offset = buf_addr - imu->ubuf; |
| 1927 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1928 | |
| 1929 | if (offset) { |
| 1930 | /* |
| 1931 | * Don't use iov_iter_advance() here, as it's really slow for |
| 1932 | * using the latter parts of a big fixed buffer - it iterates |
| 1933 | * over each segment manually. We can cheat a bit here, because |
| 1934 | * we know that: |
| 1935 | * |
| 1936 | * 1) it's a BVEC iter, we set it up |
| 1937 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 1938 | * first and last bvec |
| 1939 | * |
| 1940 | * So just find our index, and adjust the iterator afterwards. |
| 1941 | * If the offset is within the first bvec (or the whole first |
| 1942 | * bvec, just use iov_iter_advance(). This makes it easier |
| 1943 | * since we can just skip the first segment, which may not |
| 1944 | * be PAGE_SIZE aligned. |
| 1945 | */ |
| 1946 | const struct bio_vec *bvec = imu->bvec; |
| 1947 | |
| 1948 | if (offset <= bvec->bv_len) { |
| 1949 | iov_iter_advance(iter, offset); |
| 1950 | } else { |
| 1951 | unsigned long seg_skip; |
| 1952 | |
| 1953 | /* skip first vec */ |
| 1954 | offset -= bvec->bv_len; |
| 1955 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 1956 | |
| 1957 | iter->bvec = bvec + seg_skip; |
| 1958 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 1959 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1960 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 1961 | } |
| 1962 | } |
| 1963 | |
Jens Axboe | 5e55956 | 2019-11-13 16:12:46 -0700 | [diff] [blame] | 1964 | return len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1965 | } |
| 1966 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 1967 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| 1968 | struct iovec **iovec, struct iov_iter *iter) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1969 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1970 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
| 1971 | size_t sqe_len = req->rw.len; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1972 | u8 opcode; |
| 1973 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 1974 | opcode = req->opcode; |
Pavel Begunkov | 7d00916 | 2019-11-25 23:14:40 +0300 | [diff] [blame] | 1975 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1976 | *iovec = NULL; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1977 | return io_import_fixed(req, rw, iter); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 1978 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1979 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 1980 | /* buffer index only valid with fixed read/write */ |
| 1981 | if (req->rw.kiocb.private) |
| 1982 | return -EINVAL; |
| 1983 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1984 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
| 1985 | ssize_t ret; |
| 1986 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| 1987 | *iovec = NULL; |
| 1988 | return ret; |
| 1989 | } |
| 1990 | |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 1991 | if (req->io) { |
| 1992 | struct io_async_rw *iorw = &req->io->rw; |
| 1993 | |
| 1994 | *iovec = iorw->iov; |
| 1995 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); |
| 1996 | if (iorw->iov == iorw->fast_iov) |
| 1997 | *iovec = NULL; |
| 1998 | return iorw->size; |
| 1999 | } |
| 2000 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2001 | if (!req->has_user) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2002 | return -EFAULT; |
| 2003 | |
| 2004 | #ifdef CONFIG_COMPAT |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2005 | if (req->ctx->compat) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2006 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 2007 | iovec, iter); |
| 2008 | #endif |
| 2009 | |
| 2010 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 2011 | } |
| 2012 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2013 | /* |
| 2014 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 2015 | * by looping over ->read() or ->write() manually. |
| 2016 | */ |
| 2017 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 2018 | struct iov_iter *iter) |
| 2019 | { |
| 2020 | ssize_t ret = 0; |
| 2021 | |
| 2022 | /* |
| 2023 | * Don't support polled IO through this interface, and we can't |
| 2024 | * support non-blocking either. For the latter, this just causes |
| 2025 | * the kiocb to be handled from an async context. |
| 2026 | */ |
| 2027 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2028 | return -EOPNOTSUPP; |
| 2029 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 2030 | return -EAGAIN; |
| 2031 | |
| 2032 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2033 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2034 | ssize_t nr; |
| 2035 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2036 | if (!iov_iter_is_bvec(iter)) { |
| 2037 | iovec = iov_iter_iovec(iter); |
| 2038 | } else { |
| 2039 | /* fixed buffers import bvec */ |
| 2040 | iovec.iov_base = kmap(iter->bvec->bv_page) |
| 2041 | + iter->iov_offset; |
| 2042 | iovec.iov_len = min(iter->count, |
| 2043 | iter->bvec->bv_len - iter->iov_offset); |
| 2044 | } |
| 2045 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2046 | if (rw == READ) { |
| 2047 | nr = file->f_op->read(file, iovec.iov_base, |
| 2048 | iovec.iov_len, &kiocb->ki_pos); |
| 2049 | } else { |
| 2050 | nr = file->f_op->write(file, iovec.iov_base, |
| 2051 | iovec.iov_len, &kiocb->ki_pos); |
| 2052 | } |
| 2053 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 2054 | if (iov_iter_is_bvec(iter)) |
| 2055 | kunmap(iter->bvec->bv_page); |
| 2056 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2057 | if (nr < 0) { |
| 2058 | if (!ret) |
| 2059 | ret = nr; |
| 2060 | break; |
| 2061 | } |
| 2062 | ret += nr; |
| 2063 | if (nr != iovec.iov_len) |
| 2064 | break; |
| 2065 | iov_iter_advance(iter, nr); |
| 2066 | } |
| 2067 | |
| 2068 | return ret; |
| 2069 | } |
| 2070 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2071 | 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] | 2072 | struct iovec *iovec, struct iovec *fast_iov, |
| 2073 | struct iov_iter *iter) |
| 2074 | { |
| 2075 | req->io->rw.nr_segs = iter->nr_segs; |
| 2076 | req->io->rw.size = io_size; |
| 2077 | req->io->rw.iov = iovec; |
| 2078 | if (!req->io->rw.iov) { |
| 2079 | req->io->rw.iov = req->io->rw.fast_iov; |
| 2080 | memcpy(req->io->rw.iov, fast_iov, |
| 2081 | sizeof(struct iovec) * iter->nr_segs); |
| 2082 | } |
| 2083 | } |
| 2084 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2085 | static int io_alloc_async_ctx(struct io_kiocb *req) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2086 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 2087 | if (!io_op_defs[req->opcode].async_ctx) |
| 2088 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2089 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2090 | return req->io == NULL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | static void io_rw_async(struct io_wq_work **workptr) |
| 2094 | { |
| 2095 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2096 | struct iovec *iov = NULL; |
| 2097 | |
| 2098 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2099 | iov = req->io->rw.iov; |
| 2100 | io_wq_submit_work(workptr); |
| 2101 | kfree(iov); |
| 2102 | } |
| 2103 | |
| 2104 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
| 2105 | struct iovec *iovec, struct iovec *fast_iov, |
| 2106 | struct iov_iter *iter) |
| 2107 | { |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 2108 | if (req->opcode == IORING_OP_READ_FIXED || |
| 2109 | req->opcode == IORING_OP_WRITE_FIXED) |
| 2110 | return 0; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2111 | if (!req->io && io_alloc_async_ctx(req)) |
| 2112 | return -ENOMEM; |
| 2113 | |
| 2114 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
| 2115 | req->work.func = io_rw_async; |
| 2116 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2117 | } |
| 2118 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2119 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2120 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2121 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2122 | struct io_async_ctx *io; |
| 2123 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2124 | ssize_t ret; |
| 2125 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2126 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2127 | if (ret) |
| 2128 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2129 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2130 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 2131 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2132 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2133 | if (!req->io) |
| 2134 | return 0; |
| 2135 | |
| 2136 | io = req->io; |
| 2137 | io->rw.iov = io->rw.fast_iov; |
| 2138 | req->io = NULL; |
| 2139 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter); |
| 2140 | req->io = io; |
| 2141 | if (ret < 0) |
| 2142 | return ret; |
| 2143 | |
| 2144 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2145 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2146 | } |
| 2147 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2148 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2149 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2150 | { |
| 2151 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2152 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2153 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2154 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2155 | ssize_t io_size, ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2156 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2157 | ret = io_import_iovec(READ, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2158 | if (ret < 0) |
| 2159 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2160 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2161 | /* Ensure we clear previously set non-block flag */ |
| 2162 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2163 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2164 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2165 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2166 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2167 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2168 | req->result = io_size; |
| 2169 | |
| 2170 | /* |
| 2171 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2172 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2173 | */ |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2174 | if (force_nonblock && !io_file_supports_async(req->file)) { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2175 | req->flags |= REQ_F_MUST_PUNT; |
| 2176 | goto copy_iov; |
| 2177 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2178 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2179 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2180 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2181 | if (!ret) { |
| 2182 | ssize_t ret2; |
| 2183 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2184 | if (req->file->f_op->read_iter) |
| 2185 | ret2 = call_read_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2186 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2187 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2188 | |
Jens Axboe | 9d93a3f | 2019-05-15 13:53:07 -0600 | [diff] [blame] | 2189 | /* Catch -EAGAIN return for forced non-blocking submission */ |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2190 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2191 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2192 | } else { |
| 2193 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2194 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2195 | inline_vecs, &iter); |
| 2196 | if (ret) |
| 2197 | goto out_free; |
| 2198 | return -EAGAIN; |
| 2199 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2200 | } |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2201 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2202 | if (!io_wq_current_is_worker()) |
| 2203 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2204 | return ret; |
| 2205 | } |
| 2206 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2207 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 2208 | bool force_nonblock) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2209 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2210 | struct io_async_ctx *io; |
| 2211 | struct iov_iter iter; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2212 | ssize_t ret; |
| 2213 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2214 | ret = io_prep_rw(req, sqe, force_nonblock); |
| 2215 | if (ret) |
| 2216 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2217 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2218 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 2219 | return -EBADF; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2220 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2221 | if (!req->io) |
| 2222 | return 0; |
| 2223 | |
| 2224 | io = req->io; |
| 2225 | io->rw.iov = io->rw.fast_iov; |
| 2226 | req->io = NULL; |
| 2227 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter); |
| 2228 | req->io = io; |
| 2229 | if (ret < 0) |
| 2230 | return ret; |
| 2231 | |
| 2232 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); |
| 2233 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2234 | } |
| 2235 | |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 2236 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
Jens Axboe | 8358e3a | 2019-04-23 08:17:58 -0600 | [diff] [blame] | 2237 | bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2238 | { |
| 2239 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2240 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2241 | struct iov_iter iter; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2242 | size_t iov_count; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2243 | ssize_t ret, io_size; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2244 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2245 | ret = io_import_iovec(WRITE, req, &iovec, &iter); |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 2246 | if (ret < 0) |
| 2247 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2248 | |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2249 | /* Ensure we clear previously set non-block flag */ |
| 2250 | if (!force_nonblock) |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2251 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | fd6c2e4 | 2019-12-18 12:19:41 -0700 | [diff] [blame] | 2252 | |
Bijan Mottahedeh | 797f3f5 | 2020-01-15 18:37:45 -0800 | [diff] [blame] | 2253 | req->result = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2254 | io_size = ret; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2255 | if (req->flags & REQ_F_LINK) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2256 | req->result = io_size; |
| 2257 | |
| 2258 | /* |
| 2259 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 2260 | * we know to async punt it even if it was opened O_NONBLOCK |
| 2261 | */ |
| 2262 | if (force_nonblock && !io_file_supports_async(req->file)) { |
| 2263 | req->flags |= REQ_F_MUST_PUNT; |
| 2264 | goto copy_iov; |
| 2265 | } |
| 2266 | |
Jens Axboe | 10d5934 | 2019-12-09 20:16:22 -0700 | [diff] [blame] | 2267 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 2268 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 2269 | (req->flags & REQ_F_ISREG)) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2270 | goto copy_iov; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2271 | |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2272 | iov_count = iov_iter_count(&iter); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2273 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2274 | if (!ret) { |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2275 | ssize_t ret2; |
| 2276 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2277 | /* |
| 2278 | * Open-code file_start_write here to grab freeze protection, |
| 2279 | * which will be released by another thread in |
| 2280 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 2281 | * released so that it doesn't complain about the held lock when |
| 2282 | * we return to userspace. |
| 2283 | */ |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2284 | if (req->flags & REQ_F_ISREG) { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2285 | __sb_start_write(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2286 | SB_FREEZE_WRITE, true); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2287 | __sb_writers_release(file_inode(req->file)->i_sb, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2288 | SB_FREEZE_WRITE); |
| 2289 | } |
| 2290 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 2291 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2292 | if (req->file->f_op->write_iter) |
| 2293 | ret2 = call_write_iter(req->file, kiocb, &iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 2294 | else |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2295 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2296 | if (!force_nonblock || ret2 != -EAGAIN) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 2297 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2298 | } else { |
| 2299 | copy_iov: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2300 | ret = io_setup_async_rw(req, io_size, iovec, |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 2301 | inline_vecs, &iter); |
| 2302 | if (ret) |
| 2303 | goto out_free; |
| 2304 | return -EAGAIN; |
| 2305 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2306 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 2307 | out_free: |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2308 | if (!io_wq_current_is_worker()) |
| 2309 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2310 | return ret; |
| 2311 | } |
| 2312 | |
| 2313 | /* |
| 2314 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 2315 | */ |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2316 | static int io_nop(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2317 | { |
| 2318 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2319 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2320 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2321 | return -EINVAL; |
| 2322 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 2323 | io_cqring_add_event(req, 0); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2324 | io_put_req(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2325 | return 0; |
| 2326 | } |
| 2327 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2328 | 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] | 2329 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2330 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2331 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2332 | if (!req->file) |
| 2333 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2334 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 2335 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2336 | return -EINVAL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 2337 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2338 | return -EINVAL; |
| 2339 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2340 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 2341 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 2342 | return -EINVAL; |
| 2343 | |
| 2344 | req->sync.off = READ_ONCE(sqe->off); |
| 2345 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2346 | return 0; |
| 2347 | } |
| 2348 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2349 | static bool io_req_cancelled(struct io_kiocb *req) |
| 2350 | { |
| 2351 | if (req->work.flags & IO_WQ_WORK_CANCEL) { |
| 2352 | req_set_fail_links(req); |
| 2353 | io_cqring_add_event(req, -ECANCELED); |
| 2354 | io_put_req(req); |
| 2355 | return true; |
| 2356 | } |
| 2357 | |
| 2358 | return false; |
| 2359 | } |
| 2360 | |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2361 | static void io_link_work_cb(struct io_wq_work **workptr) |
| 2362 | { |
| 2363 | struct io_wq_work *work = *workptr; |
| 2364 | struct io_kiocb *link = work->data; |
| 2365 | |
| 2366 | io_queue_linked_timeout(link); |
| 2367 | work->func = io_wq_submit_work; |
| 2368 | } |
| 2369 | |
| 2370 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) |
| 2371 | { |
| 2372 | struct io_kiocb *link; |
| 2373 | |
| 2374 | io_prep_async_work(nxt, &link); |
| 2375 | *workptr = &nxt->work; |
| 2376 | if (link) { |
| 2377 | nxt->work.flags |= IO_WQ_WORK_CB; |
| 2378 | nxt->work.func = io_link_work_cb; |
| 2379 | nxt->work.data = link; |
| 2380 | } |
| 2381 | } |
| 2382 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2383 | static void io_fsync_finish(struct io_wq_work **workptr) |
| 2384 | { |
| 2385 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2386 | loff_t end = req->sync.off + req->sync.len; |
| 2387 | struct io_kiocb *nxt = NULL; |
| 2388 | int ret; |
| 2389 | |
| 2390 | if (io_req_cancelled(req)) |
| 2391 | return; |
| 2392 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2393 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2394 | end > 0 ? end : LLONG_MAX, |
| 2395 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 2396 | if (ret < 0) |
| 2397 | req_set_fail_links(req); |
| 2398 | io_cqring_add_event(req, ret); |
| 2399 | io_put_req_find_next(req, &nxt); |
| 2400 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2401 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2402 | } |
| 2403 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2404 | static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2405 | bool force_nonblock) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2406 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2407 | struct io_wq_work *work, *old_work; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2408 | |
| 2409 | /* fsync always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2410 | if (force_nonblock) { |
| 2411 | io_put_req(req); |
| 2412 | req->work.func = io_fsync_finish; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2413 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2414 | } |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2415 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2416 | work = old_work = &req->work; |
| 2417 | io_fsync_finish(&work); |
| 2418 | if (work && work != old_work) |
| 2419 | *nxt = container_of(work, struct io_kiocb, work); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 2420 | return 0; |
| 2421 | } |
| 2422 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 2423 | static void io_fallocate_finish(struct io_wq_work **workptr) |
| 2424 | { |
| 2425 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2426 | struct io_kiocb *nxt = NULL; |
| 2427 | int ret; |
| 2428 | |
| 2429 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 2430 | req->sync.len); |
| 2431 | if (ret < 0) |
| 2432 | req_set_fail_links(req); |
| 2433 | io_cqring_add_event(req, ret); |
| 2434 | io_put_req_find_next(req, &nxt); |
| 2435 | if (nxt) |
| 2436 | io_wq_assign_next(workptr, nxt); |
| 2437 | } |
| 2438 | |
| 2439 | static int io_fallocate_prep(struct io_kiocb *req, |
| 2440 | const struct io_uring_sqe *sqe) |
| 2441 | { |
| 2442 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) |
| 2443 | return -EINVAL; |
| 2444 | |
| 2445 | req->sync.off = READ_ONCE(sqe->off); |
| 2446 | req->sync.len = READ_ONCE(sqe->addr); |
| 2447 | req->sync.mode = READ_ONCE(sqe->len); |
| 2448 | return 0; |
| 2449 | } |
| 2450 | |
| 2451 | static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2452 | bool force_nonblock) |
| 2453 | { |
| 2454 | struct io_wq_work *work, *old_work; |
| 2455 | |
| 2456 | /* fallocate always requiring blocking context */ |
| 2457 | if (force_nonblock) { |
| 2458 | io_put_req(req); |
| 2459 | req->work.func = io_fallocate_finish; |
| 2460 | return -EAGAIN; |
| 2461 | } |
| 2462 | |
| 2463 | work = old_work = &req->work; |
| 2464 | io_fallocate_finish(&work); |
| 2465 | if (work && work != old_work) |
| 2466 | *nxt = container_of(work, struct io_kiocb, work); |
| 2467 | |
| 2468 | return 0; |
| 2469 | } |
| 2470 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2471 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2472 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2473 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2474 | int ret; |
| 2475 | |
| 2476 | if (sqe->ioprio || sqe->buf_index) |
| 2477 | return -EINVAL; |
| 2478 | |
| 2479 | req->open.dfd = READ_ONCE(sqe->fd); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2480 | req->open.how.mode = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2481 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2482 | req->open.how.flags = READ_ONCE(sqe->open_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2483 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2484 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2485 | if (IS_ERR(req->open.filename)) { |
| 2486 | ret = PTR_ERR(req->open.filename); |
| 2487 | req->open.filename = NULL; |
| 2488 | return ret; |
| 2489 | } |
| 2490 | |
| 2491 | return 0; |
| 2492 | } |
| 2493 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2494 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2495 | { |
| 2496 | struct open_how __user *how; |
| 2497 | const char __user *fname; |
| 2498 | size_t len; |
| 2499 | int ret; |
| 2500 | |
| 2501 | if (sqe->ioprio || sqe->buf_index) |
| 2502 | return -EINVAL; |
| 2503 | |
| 2504 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2505 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 2506 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 2507 | len = READ_ONCE(sqe->len); |
| 2508 | |
| 2509 | if (len < OPEN_HOW_SIZE_VER0) |
| 2510 | return -EINVAL; |
| 2511 | |
| 2512 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 2513 | len); |
| 2514 | if (ret) |
| 2515 | return ret; |
| 2516 | |
| 2517 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
| 2518 | req->open.how.flags |= O_LARGEFILE; |
| 2519 | |
| 2520 | req->open.filename = getname(fname); |
| 2521 | if (IS_ERR(req->open.filename)) { |
| 2522 | ret = PTR_ERR(req->open.filename); |
| 2523 | req->open.filename = NULL; |
| 2524 | return ret; |
| 2525 | } |
| 2526 | |
| 2527 | return 0; |
| 2528 | } |
| 2529 | |
| 2530 | static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2531 | bool force_nonblock) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2532 | { |
| 2533 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2534 | struct file *file; |
| 2535 | int ret; |
| 2536 | |
| 2537 | if (force_nonblock) { |
| 2538 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 2539 | return -EAGAIN; |
| 2540 | } |
| 2541 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2542 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2543 | if (ret) |
| 2544 | goto err; |
| 2545 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2546 | ret = get_unused_fd_flags(req->open.how.flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 2547 | if (ret < 0) |
| 2548 | goto err; |
| 2549 | |
| 2550 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
| 2551 | if (IS_ERR(file)) { |
| 2552 | put_unused_fd(ret); |
| 2553 | ret = PTR_ERR(file); |
| 2554 | } else { |
| 2555 | fsnotify_open(file); |
| 2556 | fd_install(ret, file); |
| 2557 | } |
| 2558 | err: |
| 2559 | putname(req->open.filename); |
| 2560 | if (ret < 0) |
| 2561 | req_set_fail_links(req); |
| 2562 | io_cqring_add_event(req, ret); |
| 2563 | io_put_req_find_next(req, nxt); |
| 2564 | return 0; |
| 2565 | } |
| 2566 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 2567 | static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2568 | bool force_nonblock) |
| 2569 | { |
| 2570 | req->open.how = build_open_how(req->open.how.flags, req->open.how.mode); |
| 2571 | return io_openat2(req, nxt, force_nonblock); |
| 2572 | } |
| 2573 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 2574 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2575 | { |
| 2576 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2577 | if (sqe->ioprio || sqe->buf_index || sqe->off) |
| 2578 | return -EINVAL; |
| 2579 | |
| 2580 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 2581 | req->madvise.len = READ_ONCE(sqe->len); |
| 2582 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2583 | return 0; |
| 2584 | #else |
| 2585 | return -EOPNOTSUPP; |
| 2586 | #endif |
| 2587 | } |
| 2588 | |
| 2589 | static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2590 | bool force_nonblock) |
| 2591 | { |
| 2592 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 2593 | struct io_madvise *ma = &req->madvise; |
| 2594 | int ret; |
| 2595 | |
| 2596 | if (force_nonblock) |
| 2597 | return -EAGAIN; |
| 2598 | |
| 2599 | ret = do_madvise(ma->addr, ma->len, ma->advice); |
| 2600 | if (ret < 0) |
| 2601 | req_set_fail_links(req); |
| 2602 | io_cqring_add_event(req, ret); |
| 2603 | io_put_req_find_next(req, nxt); |
| 2604 | return 0; |
| 2605 | #else |
| 2606 | return -EOPNOTSUPP; |
| 2607 | #endif |
| 2608 | } |
| 2609 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 2610 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2611 | { |
| 2612 | if (sqe->ioprio || sqe->buf_index || sqe->addr) |
| 2613 | return -EINVAL; |
| 2614 | |
| 2615 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 2616 | req->fadvise.len = READ_ONCE(sqe->len); |
| 2617 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 2618 | return 0; |
| 2619 | } |
| 2620 | |
| 2621 | static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2622 | bool force_nonblock) |
| 2623 | { |
| 2624 | struct io_fadvise *fa = &req->fadvise; |
| 2625 | int ret; |
| 2626 | |
| 2627 | /* DONTNEED may block, others _should_ not */ |
| 2628 | if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock) |
| 2629 | return -EAGAIN; |
| 2630 | |
| 2631 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 2632 | if (ret < 0) |
| 2633 | req_set_fail_links(req); |
| 2634 | io_cqring_add_event(req, ret); |
| 2635 | io_put_req_find_next(req, nxt); |
| 2636 | return 0; |
| 2637 | } |
| 2638 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2639 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2640 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2641 | const char __user *fname; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2642 | unsigned lookup_flags; |
| 2643 | int ret; |
| 2644 | |
| 2645 | if (sqe->ioprio || sqe->buf_index) |
| 2646 | return -EINVAL; |
| 2647 | |
| 2648 | req->open.dfd = READ_ONCE(sqe->fd); |
| 2649 | req->open.mask = READ_ONCE(sqe->len); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2650 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2651 | req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2652 | req->open.how.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2653 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2654 | if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2655 | return -EINVAL; |
| 2656 | |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 2657 | req->open.filename = getname_flags(fname, lookup_flags, NULL); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2658 | if (IS_ERR(req->open.filename)) { |
| 2659 | ret = PTR_ERR(req->open.filename); |
| 2660 | req->open.filename = NULL; |
| 2661 | return ret; |
| 2662 | } |
| 2663 | |
| 2664 | return 0; |
| 2665 | } |
| 2666 | |
| 2667 | static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2668 | bool force_nonblock) |
| 2669 | { |
| 2670 | struct io_open *ctx = &req->open; |
| 2671 | unsigned lookup_flags; |
| 2672 | struct path path; |
| 2673 | struct kstat stat; |
| 2674 | int ret; |
| 2675 | |
| 2676 | if (force_nonblock) |
| 2677 | return -EAGAIN; |
| 2678 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2679 | if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags)) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2680 | return -EINVAL; |
| 2681 | |
| 2682 | retry: |
| 2683 | /* filename_lookup() drops it, keep a reference */ |
| 2684 | ctx->filename->refcnt++; |
| 2685 | |
| 2686 | ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path, |
| 2687 | NULL); |
| 2688 | if (ret) |
| 2689 | goto err; |
| 2690 | |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 2691 | ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 2692 | path_put(&path); |
| 2693 | if (retry_estale(ret, lookup_flags)) { |
| 2694 | lookup_flags |= LOOKUP_REVAL; |
| 2695 | goto retry; |
| 2696 | } |
| 2697 | if (!ret) |
| 2698 | ret = cp_statx(&stat, ctx->buffer); |
| 2699 | err: |
| 2700 | putname(ctx->filename); |
| 2701 | if (ret < 0) |
| 2702 | req_set_fail_links(req); |
| 2703 | io_cqring_add_event(req, ret); |
| 2704 | io_put_req_find_next(req, nxt); |
| 2705 | return 0; |
| 2706 | } |
| 2707 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2708 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 2709 | { |
| 2710 | /* |
| 2711 | * If we queue this for async, it must not be cancellable. That would |
| 2712 | * leave the 'file' in an undeterminate state. |
| 2713 | */ |
| 2714 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; |
| 2715 | |
| 2716 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
| 2717 | sqe->rw_flags || sqe->buf_index) |
| 2718 | return -EINVAL; |
| 2719 | if (sqe->flags & IOSQE_FIXED_FILE) |
| 2720 | return -EINVAL; |
| 2721 | |
| 2722 | req->close.fd = READ_ONCE(sqe->fd); |
| 2723 | if (req->file->f_op == &io_uring_fops || |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 2724 | req->close.fd == req->ctx->ring_fd) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 2725 | return -EBADF; |
| 2726 | |
| 2727 | return 0; |
| 2728 | } |
| 2729 | |
| 2730 | static void io_close_finish(struct io_wq_work **workptr) |
| 2731 | { |
| 2732 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2733 | struct io_kiocb *nxt = NULL; |
| 2734 | |
| 2735 | /* Invoked with files, we need to do the close */ |
| 2736 | if (req->work.files) { |
| 2737 | int ret; |
| 2738 | |
| 2739 | ret = filp_close(req->close.put_file, req->work.files); |
| 2740 | if (ret < 0) { |
| 2741 | req_set_fail_links(req); |
| 2742 | } |
| 2743 | io_cqring_add_event(req, ret); |
| 2744 | } |
| 2745 | |
| 2746 | fput(req->close.put_file); |
| 2747 | |
| 2748 | /* we bypassed the re-issue, drop the submission reference */ |
| 2749 | io_put_req(req); |
| 2750 | io_put_req_find_next(req, &nxt); |
| 2751 | if (nxt) |
| 2752 | io_wq_assign_next(workptr, nxt); |
| 2753 | } |
| 2754 | |
| 2755 | static int io_close(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2756 | bool force_nonblock) |
| 2757 | { |
| 2758 | int ret; |
| 2759 | |
| 2760 | req->close.put_file = NULL; |
| 2761 | ret = __close_fd_get_file(req->close.fd, &req->close.put_file); |
| 2762 | if (ret < 0) |
| 2763 | return ret; |
| 2764 | |
| 2765 | /* if the file has a flush method, be safe and punt to async */ |
| 2766 | if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) { |
| 2767 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 2768 | goto eagain; |
| 2769 | } |
| 2770 | |
| 2771 | /* |
| 2772 | * No ->flush(), safely close from here and just punt the |
| 2773 | * fput() to async context. |
| 2774 | */ |
| 2775 | ret = filp_close(req->close.put_file, current->files); |
| 2776 | |
| 2777 | if (ret < 0) |
| 2778 | req_set_fail_links(req); |
| 2779 | io_cqring_add_event(req, ret); |
| 2780 | |
| 2781 | if (io_wq_current_is_worker()) { |
| 2782 | struct io_wq_work *old_work, *work; |
| 2783 | |
| 2784 | old_work = work = &req->work; |
| 2785 | io_close_finish(&work); |
| 2786 | if (work && work != old_work) |
| 2787 | *nxt = container_of(work, struct io_kiocb, work); |
| 2788 | return 0; |
| 2789 | } |
| 2790 | |
| 2791 | eagain: |
| 2792 | req->work.func = io_close_finish; |
| 2793 | return -EAGAIN; |
| 2794 | } |
| 2795 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2796 | 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] | 2797 | { |
| 2798 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2799 | |
| 2800 | if (!req->file) |
| 2801 | return -EBADF; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2802 | |
| 2803 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2804 | return -EINVAL; |
| 2805 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 2806 | return -EINVAL; |
| 2807 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2808 | req->sync.off = READ_ONCE(sqe->off); |
| 2809 | req->sync.len = READ_ONCE(sqe->len); |
| 2810 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2811 | return 0; |
| 2812 | } |
| 2813 | |
| 2814 | static void io_sync_file_range_finish(struct io_wq_work **workptr) |
| 2815 | { |
| 2816 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2817 | struct io_kiocb *nxt = NULL; |
| 2818 | int ret; |
| 2819 | |
| 2820 | if (io_req_cancelled(req)) |
| 2821 | return; |
| 2822 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2823 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2824 | req->sync.flags); |
| 2825 | if (ret < 0) |
| 2826 | req_set_fail_links(req); |
| 2827 | io_cqring_add_event(req, ret); |
| 2828 | io_put_req_find_next(req, &nxt); |
| 2829 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 2830 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2831 | } |
| 2832 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2833 | 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] | 2834 | bool force_nonblock) |
| 2835 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2836 | struct io_wq_work *work, *old_work; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2837 | |
| 2838 | /* sync_file_range always requires a blocking context */ |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2839 | if (force_nonblock) { |
| 2840 | io_put_req(req); |
| 2841 | req->work.func = io_sync_file_range_finish; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2842 | return -EAGAIN; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2843 | } |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2844 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 2845 | work = old_work = &req->work; |
| 2846 | io_sync_file_range_finish(&work); |
| 2847 | if (work && work != old_work) |
| 2848 | *nxt = container_of(work, struct io_kiocb, work); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 2849 | return 0; |
| 2850 | } |
| 2851 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2852 | #if defined(CONFIG_NET) |
| 2853 | static void io_sendrecv_async(struct io_wq_work **workptr) |
| 2854 | { |
| 2855 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 2856 | struct iovec *iov = NULL; |
| 2857 | |
| 2858 | if (req->io->rw.iov != req->io->rw.fast_iov) |
| 2859 | iov = req->io->msg.iov; |
| 2860 | io_wq_submit_work(workptr); |
| 2861 | kfree(iov); |
| 2862 | } |
| 2863 | #endif |
| 2864 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2865 | 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] | 2866 | { |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2867 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2868 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2869 | struct io_async_ctx *io = req->io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2870 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2871 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 2872 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2873 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2874 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2875 | if (!io || req->opcode == IORING_OP_SEND) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2876 | return 0; |
| 2877 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 2878 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2879 | return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2880 | &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2881 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2882 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2883 | #endif |
| 2884 | } |
| 2885 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 2886 | static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2887 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2888 | { |
| 2889 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2890 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2891 | struct socket *sock; |
| 2892 | int ret; |
| 2893 | |
| 2894 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2895 | return -EINVAL; |
| 2896 | |
| 2897 | sock = sock_from_file(req->file, &ret); |
| 2898 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2899 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2900 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2901 | unsigned flags; |
| 2902 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2903 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2904 | kmsg = &req->io->msg; |
| 2905 | kmsg->msg.msg_name = &addr; |
| 2906 | /* if iov is set, it's allocated already */ |
| 2907 | if (!kmsg->iov) |
| 2908 | kmsg->iov = kmsg->fast_iov; |
| 2909 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2910 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2911 | struct io_sr_msg *sr = &req->sr_msg; |
| 2912 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2913 | kmsg = &io.msg; |
| 2914 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2915 | |
| 2916 | io.msg.iov = io.msg.fast_iov; |
| 2917 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 2918 | sr->msg_flags, &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2919 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2920 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2921 | } |
| 2922 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 2923 | flags = req->sr_msg.msg_flags; |
| 2924 | if (flags & MSG_DONTWAIT) |
| 2925 | req->flags |= REQ_F_NOWAIT; |
| 2926 | else if (force_nonblock) |
| 2927 | flags |= MSG_DONTWAIT; |
| 2928 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2929 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2930 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2931 | if (req->io) |
| 2932 | return -EAGAIN; |
| 2933 | if (io_alloc_async_ctx(req)) |
| 2934 | return -ENOMEM; |
| 2935 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 2936 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2937 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2938 | } |
| 2939 | if (ret == -ERESTARTSYS) |
| 2940 | ret = -EINTR; |
| 2941 | } |
| 2942 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 2943 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 2944 | kfree(kmsg->iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2945 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 2946 | if (ret < 0) |
| 2947 | req_set_fail_links(req); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 2948 | io_put_req_find_next(req, nxt); |
| 2949 | return 0; |
| 2950 | #else |
| 2951 | return -EOPNOTSUPP; |
| 2952 | #endif |
| 2953 | } |
| 2954 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 2955 | static int io_send(struct io_kiocb *req, struct io_kiocb **nxt, |
| 2956 | bool force_nonblock) |
| 2957 | { |
| 2958 | #if defined(CONFIG_NET) |
| 2959 | struct socket *sock; |
| 2960 | int ret; |
| 2961 | |
| 2962 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 2963 | return -EINVAL; |
| 2964 | |
| 2965 | sock = sock_from_file(req->file, &ret); |
| 2966 | if (sock) { |
| 2967 | struct io_sr_msg *sr = &req->sr_msg; |
| 2968 | struct msghdr msg; |
| 2969 | struct iovec iov; |
| 2970 | unsigned flags; |
| 2971 | |
| 2972 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, |
| 2973 | &msg.msg_iter); |
| 2974 | if (ret) |
| 2975 | return ret; |
| 2976 | |
| 2977 | msg.msg_name = NULL; |
| 2978 | msg.msg_control = NULL; |
| 2979 | msg.msg_controllen = 0; |
| 2980 | msg.msg_namelen = 0; |
| 2981 | |
| 2982 | flags = req->sr_msg.msg_flags; |
| 2983 | if (flags & MSG_DONTWAIT) |
| 2984 | req->flags |= REQ_F_NOWAIT; |
| 2985 | else if (force_nonblock) |
| 2986 | flags |= MSG_DONTWAIT; |
| 2987 | |
| 2988 | ret = __sys_sendmsg_sock(sock, &msg, flags); |
| 2989 | if (force_nonblock && ret == -EAGAIN) |
| 2990 | return -EAGAIN; |
| 2991 | if (ret == -ERESTARTSYS) |
| 2992 | ret = -EINTR; |
| 2993 | } |
| 2994 | |
| 2995 | io_cqring_add_event(req, ret); |
| 2996 | if (ret < 0) |
| 2997 | req_set_fail_links(req); |
| 2998 | io_put_req_find_next(req, nxt); |
| 2999 | return 0; |
| 3000 | #else |
| 3001 | return -EOPNOTSUPP; |
| 3002 | #endif |
| 3003 | } |
| 3004 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3005 | static int io_recvmsg_prep(struct io_kiocb *req, |
| 3006 | const struct io_uring_sqe *sqe) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3007 | { |
| 3008 | #if defined(CONFIG_NET) |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3009 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3010 | struct io_async_ctx *io = req->io; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3011 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3012 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
| 3013 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3014 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3015 | if (!io || req->opcode == IORING_OP_RECV) |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 3016 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3017 | |
Jens Axboe | d968856 | 2019-12-09 19:35:20 -0700 | [diff] [blame] | 3018 | io->msg.iov = io->msg.fast_iov; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3019 | return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3020 | &io->msg.uaddr, &io->msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3021 | #else |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3022 | return -EOPNOTSUPP; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3023 | #endif |
| 3024 | } |
| 3025 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3026 | static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3027 | bool force_nonblock) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3028 | { |
| 3029 | #if defined(CONFIG_NET) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3030 | struct io_async_msghdr *kmsg = NULL; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3031 | struct socket *sock; |
| 3032 | int ret; |
| 3033 | |
| 3034 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3035 | return -EINVAL; |
| 3036 | |
| 3037 | sock = sock_from_file(req->file, &ret); |
| 3038 | if (sock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3039 | struct io_async_ctx io; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3040 | struct sockaddr_storage addr; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3041 | unsigned flags; |
| 3042 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3043 | if (req->io) { |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3044 | kmsg = &req->io->msg; |
| 3045 | kmsg->msg.msg_name = &addr; |
| 3046 | /* if iov is set, it's allocated already */ |
| 3047 | if (!kmsg->iov) |
| 3048 | kmsg->iov = kmsg->fast_iov; |
| 3049 | kmsg->msg.msg_iter.iov = kmsg->iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3050 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3051 | struct io_sr_msg *sr = &req->sr_msg; |
| 3052 | |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3053 | kmsg = &io.msg; |
| 3054 | kmsg->msg.msg_name = &addr; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3055 | |
| 3056 | io.msg.iov = io.msg.fast_iov; |
| 3057 | ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg, |
| 3058 | sr->msg_flags, &io.msg.uaddr, |
| 3059 | &io.msg.iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3060 | if (ret) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3061 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3062 | } |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3063 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 3064 | flags = req->sr_msg.msg_flags; |
| 3065 | if (flags & MSG_DONTWAIT) |
| 3066 | req->flags |= REQ_F_NOWAIT; |
| 3067 | else if (force_nonblock) |
| 3068 | flags |= MSG_DONTWAIT; |
| 3069 | |
| 3070 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, |
| 3071 | kmsg->uaddr, flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3072 | if (force_nonblock && ret == -EAGAIN) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3073 | if (req->io) |
| 3074 | return -EAGAIN; |
| 3075 | if (io_alloc_async_ctx(req)) |
| 3076 | return -ENOMEM; |
| 3077 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
| 3078 | req->work.func = io_sendrecv_async; |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3079 | return -EAGAIN; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3080 | } |
Jens Axboe | 441cdbd | 2019-12-02 18:49:10 -0700 | [diff] [blame] | 3081 | if (ret == -ERESTARTSYS) |
| 3082 | ret = -EINTR; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3083 | } |
| 3084 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3085 | if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov) |
Jens Axboe | 0b416c3 | 2019-12-15 10:57:46 -0700 | [diff] [blame] | 3086 | kfree(kmsg->iov); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3087 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3088 | if (ret < 0) |
| 3089 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3090 | io_put_req_find_next(req, nxt); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 3091 | return 0; |
| 3092 | #else |
| 3093 | return -EOPNOTSUPP; |
| 3094 | #endif |
| 3095 | } |
| 3096 | |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3097 | static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3098 | bool force_nonblock) |
| 3099 | { |
| 3100 | #if defined(CONFIG_NET) |
| 3101 | struct socket *sock; |
| 3102 | int ret; |
| 3103 | |
| 3104 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3105 | return -EINVAL; |
| 3106 | |
| 3107 | sock = sock_from_file(req->file, &ret); |
| 3108 | if (sock) { |
| 3109 | struct io_sr_msg *sr = &req->sr_msg; |
| 3110 | struct msghdr msg; |
| 3111 | struct iovec iov; |
| 3112 | unsigned flags; |
| 3113 | |
| 3114 | ret = import_single_range(READ, sr->buf, sr->len, &iov, |
| 3115 | &msg.msg_iter); |
| 3116 | if (ret) |
| 3117 | return ret; |
| 3118 | |
| 3119 | msg.msg_name = NULL; |
| 3120 | msg.msg_control = NULL; |
| 3121 | msg.msg_controllen = 0; |
| 3122 | msg.msg_namelen = 0; |
| 3123 | msg.msg_iocb = NULL; |
| 3124 | msg.msg_flags = 0; |
| 3125 | |
| 3126 | flags = req->sr_msg.msg_flags; |
| 3127 | if (flags & MSG_DONTWAIT) |
| 3128 | req->flags |= REQ_F_NOWAIT; |
| 3129 | else if (force_nonblock) |
| 3130 | flags |= MSG_DONTWAIT; |
| 3131 | |
| 3132 | ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags); |
| 3133 | if (force_nonblock && ret == -EAGAIN) |
| 3134 | return -EAGAIN; |
| 3135 | if (ret == -ERESTARTSYS) |
| 3136 | ret = -EINTR; |
| 3137 | } |
| 3138 | |
| 3139 | io_cqring_add_event(req, ret); |
| 3140 | if (ret < 0) |
| 3141 | req_set_fail_links(req); |
| 3142 | io_put_req_find_next(req, nxt); |
| 3143 | return 0; |
| 3144 | #else |
| 3145 | return -EOPNOTSUPP; |
| 3146 | #endif |
| 3147 | } |
| 3148 | |
| 3149 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3150 | 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] | 3151 | { |
| 3152 | #if defined(CONFIG_NET) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3153 | struct io_accept *accept = &req->accept; |
| 3154 | |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3155 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3156 | return -EINVAL; |
Hrvoje Zeba | 8042d6c | 2019-11-25 14:40:22 -0500 | [diff] [blame] | 3157 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3158 | return -EINVAL; |
| 3159 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 3160 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3161 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3162 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3163 | return 0; |
| 3164 | #else |
| 3165 | return -EOPNOTSUPP; |
| 3166 | #endif |
| 3167 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3168 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3169 | #if defined(CONFIG_NET) |
| 3170 | static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3171 | bool force_nonblock) |
| 3172 | { |
| 3173 | struct io_accept *accept = &req->accept; |
| 3174 | unsigned file_flags; |
| 3175 | int ret; |
| 3176 | |
| 3177 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3178 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, |
| 3179 | accept->addr_len, accept->flags); |
| 3180 | if (ret == -EAGAIN && force_nonblock) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3181 | return -EAGAIN; |
Jens Axboe | 8e3cca1 | 2019-11-09 19:52:33 -0700 | [diff] [blame] | 3182 | if (ret == -ERESTARTSYS) |
| 3183 | ret = -EINTR; |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3184 | if (ret < 0) |
| 3185 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3186 | io_cqring_add_event(req, ret); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3187 | io_put_req_find_next(req, nxt); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3188 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3189 | } |
| 3190 | |
| 3191 | static void io_accept_finish(struct io_wq_work **workptr) |
| 3192 | { |
| 3193 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3194 | struct io_kiocb *nxt = NULL; |
| 3195 | |
| 3196 | if (io_req_cancelled(req)) |
| 3197 | return; |
| 3198 | __io_accept(req, &nxt, false); |
| 3199 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3200 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3201 | } |
| 3202 | #endif |
| 3203 | |
| 3204 | static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3205 | bool force_nonblock) |
| 3206 | { |
| 3207 | #if defined(CONFIG_NET) |
| 3208 | int ret; |
| 3209 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3210 | ret = __io_accept(req, nxt, force_nonblock); |
| 3211 | if (ret == -EAGAIN && force_nonblock) { |
| 3212 | req->work.func = io_accept_finish; |
| 3213 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 3214 | io_put_req(req); |
| 3215 | return -EAGAIN; |
| 3216 | } |
| 3217 | return 0; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 3218 | #else |
| 3219 | return -EOPNOTSUPP; |
| 3220 | #endif |
| 3221 | } |
| 3222 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3223 | 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] | 3224 | { |
| 3225 | #if defined(CONFIG_NET) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3226 | struct io_connect *conn = &req->connect; |
| 3227 | struct io_async_ctx *io = req->io; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3228 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3229 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
| 3230 | return -EINVAL; |
| 3231 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) |
| 3232 | return -EINVAL; |
| 3233 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3234 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3235 | conn->addr_len = READ_ONCE(sqe->addr2); |
| 3236 | |
| 3237 | if (!io) |
| 3238 | return 0; |
| 3239 | |
| 3240 | return move_addr_to_kernel(conn->addr, conn->addr_len, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3241 | &io->connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3242 | #else |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3243 | return -EOPNOTSUPP; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3244 | #endif |
| 3245 | } |
| 3246 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3247 | static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt, |
| 3248 | bool force_nonblock) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3249 | { |
| 3250 | #if defined(CONFIG_NET) |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3251 | struct io_async_ctx __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3252 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3253 | int ret; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3254 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3255 | if (req->io) { |
| 3256 | io = req->io; |
| 3257 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3258 | ret = move_addr_to_kernel(req->connect.addr, |
| 3259 | req->connect.addr_len, |
| 3260 | &__io.connect.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3261 | if (ret) |
| 3262 | goto out; |
| 3263 | io = &__io; |
| 3264 | } |
| 3265 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 3266 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 3267 | |
| 3268 | ret = __sys_connect_file(req->file, &io->connect.address, |
| 3269 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 3270 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3271 | if (req->io) |
| 3272 | return -EAGAIN; |
| 3273 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3274 | ret = -ENOMEM; |
| 3275 | goto out; |
| 3276 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3277 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3278 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3279 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3280 | if (ret == -ERESTARTSYS) |
| 3281 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3282 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3283 | if (ret < 0) |
| 3284 | req_set_fail_links(req); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 3285 | io_cqring_add_event(req, ret); |
| 3286 | io_put_req_find_next(req, nxt); |
| 3287 | return 0; |
| 3288 | #else |
| 3289 | return -EOPNOTSUPP; |
| 3290 | #endif |
| 3291 | } |
| 3292 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3293 | static void io_poll_remove_one(struct io_kiocb *req) |
| 3294 | { |
| 3295 | struct io_poll_iocb *poll = &req->poll; |
| 3296 | |
| 3297 | spin_lock(&poll->head->lock); |
| 3298 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3299 | if (!list_empty(&poll->wait.entry)) { |
| 3300 | list_del_init(&poll->wait.entry); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3301 | io_queue_async_work(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3302 | } |
| 3303 | spin_unlock(&poll->head->lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3304 | hash_del(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3305 | } |
| 3306 | |
| 3307 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 3308 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3309 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3310 | struct io_kiocb *req; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3311 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3312 | |
| 3313 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3314 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 3315 | struct hlist_head *list; |
| 3316 | |
| 3317 | list = &ctx->cancel_hash[i]; |
| 3318 | hlist_for_each_entry_safe(req, tmp, list, hash_node) |
| 3319 | io_poll_remove_one(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3320 | } |
| 3321 | spin_unlock_irq(&ctx->completion_lock); |
| 3322 | } |
| 3323 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3324 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
| 3325 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3326 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3327 | struct io_kiocb *req; |
| 3328 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3329 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 3330 | hlist_for_each_entry(req, list, hash_node) { |
| 3331 | if (sqe_addr == req->user_data) { |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3332 | io_poll_remove_one(req); |
| 3333 | return 0; |
| 3334 | } |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3335 | } |
| 3336 | |
| 3337 | return -ENOENT; |
| 3338 | } |
| 3339 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3340 | static int io_poll_remove_prep(struct io_kiocb *req, |
| 3341 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3342 | { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3343 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3344 | return -EINVAL; |
| 3345 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3346 | sqe->poll_events) |
| 3347 | return -EINVAL; |
| 3348 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3349 | req->poll.addr = READ_ONCE(sqe->addr); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3350 | return 0; |
| 3351 | } |
| 3352 | |
| 3353 | /* |
| 3354 | * Find a running poll command that matches one specified in sqe->addr, |
| 3355 | * and remove it if found. |
| 3356 | */ |
| 3357 | static int io_poll_remove(struct io_kiocb *req) |
| 3358 | { |
| 3359 | struct io_ring_ctx *ctx = req->ctx; |
| 3360 | u64 addr; |
| 3361 | int ret; |
| 3362 | |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3363 | addr = req->poll.addr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3364 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3365 | ret = io_poll_cancel(ctx, addr); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3366 | spin_unlock_irq(&ctx->completion_lock); |
| 3367 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3368 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3369 | if (ret < 0) |
| 3370 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 3371 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3372 | return 0; |
| 3373 | } |
| 3374 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3375 | 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] | 3376 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 3377 | struct io_ring_ctx *ctx = req->ctx; |
| 3378 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3379 | req->poll.done = true; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3380 | if (error) |
| 3381 | io_cqring_fill_event(req, error); |
| 3382 | else |
| 3383 | io_cqring_fill_event(req, mangle_poll(mask)); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3384 | io_commit_cqring(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3385 | } |
| 3386 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3387 | static void io_poll_complete_work(struct io_wq_work **workptr) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3388 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3389 | struct io_wq_work *work = *workptr; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3390 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 3391 | struct io_poll_iocb *poll = &req->poll; |
| 3392 | struct poll_table_struct pt = { ._key = poll->events }; |
| 3393 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3394 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3395 | __poll_t mask = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3396 | int ret = 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3397 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3398 | if (work->flags & IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3399 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3400 | ret = -ECANCELED; |
| 3401 | } else if (READ_ONCE(poll->canceled)) { |
| 3402 | ret = -ECANCELED; |
| 3403 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 3404 | |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3405 | if (ret != -ECANCELED) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3406 | mask = vfs_poll(poll->file, &pt) & poll->events; |
| 3407 | |
| 3408 | /* |
| 3409 | * Note that ->ki_cancel callers also delete iocb from active_reqs after |
| 3410 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to |
| 3411 | * synchronize with them. In the cancellation case the list_del_init |
| 3412 | * itself is not actually needed, but harmless so we keep it in to |
| 3413 | * avoid further branches in the fast path. |
| 3414 | */ |
| 3415 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3416 | if (!mask && ret != -ECANCELED) { |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3417 | add_wait_queue(poll->head, &poll->wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3418 | spin_unlock_irq(&ctx->completion_lock); |
| 3419 | return; |
| 3420 | } |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3421 | hash_del(&req->hash_node); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3422 | io_poll_complete(req, mask, ret); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3423 | spin_unlock_irq(&ctx->completion_lock); |
| 3424 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3425 | io_cqring_ev_posted(ctx); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3426 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3427 | if (ret < 0) |
| 3428 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3429 | io_put_req_find_next(req, &nxt); |
Jens Axboe | 89723d0 | 2019-11-05 15:32:58 -0700 | [diff] [blame] | 3430 | if (nxt) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 3431 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3432 | } |
| 3433 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3434 | static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes) |
| 3435 | { |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3436 | struct io_kiocb *req, *tmp; |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3437 | struct req_batch rb; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3438 | |
Jens Axboe | c6ca97b30 | 2019-12-28 12:11:08 -0700 | [diff] [blame] | 3439 | rb.to_free = rb.need_iter = 0; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3440 | spin_lock_irq(&ctx->completion_lock); |
| 3441 | llist_for_each_entry_safe(req, tmp, nodes, llist_node) { |
| 3442 | hash_del(&req->hash_node); |
| 3443 | io_poll_complete(req, req->result, 0); |
| 3444 | |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3445 | if (refcount_dec_and_test(&req->refs) && |
| 3446 | !io_req_multi_free(&rb, req)) { |
| 3447 | req->flags |= REQ_F_COMP_LOCKED; |
| 3448 | io_free_req(req); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3449 | } |
| 3450 | } |
| 3451 | spin_unlock_irq(&ctx->completion_lock); |
| 3452 | |
| 3453 | io_cqring_ev_posted(ctx); |
Jens Axboe | 8237e04 | 2019-12-28 10:48:22 -0700 | [diff] [blame] | 3454 | io_free_req_many(ctx, &rb); |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3455 | } |
| 3456 | |
| 3457 | static void io_poll_flush(struct io_wq_work **workptr) |
| 3458 | { |
| 3459 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); |
| 3460 | struct llist_node *nodes; |
| 3461 | |
| 3462 | nodes = llist_del_all(&req->ctx->poll_llist); |
| 3463 | if (nodes) |
| 3464 | __io_poll_flush(req->ctx, nodes); |
| 3465 | } |
| 3466 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3467 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 3468 | void *key) |
| 3469 | { |
Jens Axboe | e944475 | 2019-11-26 15:02:04 -0700 | [diff] [blame] | 3470 | struct io_poll_iocb *poll = wait->private; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3471 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); |
| 3472 | struct io_ring_ctx *ctx = req->ctx; |
| 3473 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3474 | |
| 3475 | /* for instances that support it check for an event match first: */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3476 | if (mask && !(mask & poll->events)) |
| 3477 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3478 | |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3479 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3480 | |
Jens Axboe | 7c9e7f0 | 2019-11-12 08:15:53 -0700 | [diff] [blame] | 3481 | /* |
| 3482 | * Run completion inline if we can. We're using trylock here because |
| 3483 | * we are violating the completion_lock -> poll wq lock ordering. |
| 3484 | * If we have a link timeout we're going to need the completion_lock |
| 3485 | * for finalizing the request, mark us as having grabbed that already. |
| 3486 | */ |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3487 | if (mask) { |
| 3488 | unsigned long flags; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3489 | |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3490 | if (llist_empty(&ctx->poll_llist) && |
| 3491 | spin_trylock_irqsave(&ctx->completion_lock, flags)) { |
| 3492 | hash_del(&req->hash_node); |
| 3493 | io_poll_complete(req, mask, 0); |
| 3494 | req->flags |= REQ_F_COMP_LOCKED; |
| 3495 | io_put_req(req); |
| 3496 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3497 | |
| 3498 | io_cqring_ev_posted(ctx); |
| 3499 | req = NULL; |
| 3500 | } else { |
| 3501 | req->result = mask; |
| 3502 | req->llist_node.next = NULL; |
| 3503 | /* if the list wasn't empty, we're done */ |
| 3504 | if (!llist_add(&req->llist_node, &ctx->poll_llist)) |
| 3505 | req = NULL; |
| 3506 | else |
| 3507 | req->work.func = io_poll_flush; |
| 3508 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3509 | } |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 3510 | if (req) |
| 3511 | io_queue_async_work(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3512 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3513 | return 1; |
| 3514 | } |
| 3515 | |
| 3516 | struct io_poll_table { |
| 3517 | struct poll_table_struct pt; |
| 3518 | struct io_kiocb *req; |
| 3519 | int error; |
| 3520 | }; |
| 3521 | |
| 3522 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 3523 | struct poll_table_struct *p) |
| 3524 | { |
| 3525 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 3526 | |
| 3527 | if (unlikely(pt->req->poll.head)) { |
| 3528 | pt->error = -EINVAL; |
| 3529 | return; |
| 3530 | } |
| 3531 | |
| 3532 | pt->error = 0; |
| 3533 | pt->req->poll.head = head; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3534 | add_wait_queue(head, &pt->req->poll.wait); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3535 | } |
| 3536 | |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3537 | static void io_poll_req_insert(struct io_kiocb *req) |
| 3538 | { |
| 3539 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3540 | struct hlist_head *list; |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3541 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3542 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 3543 | hlist_add_head(&req->hash_node, list); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3544 | } |
| 3545 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3546 | 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] | 3547 | { |
| 3548 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3549 | u16 events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3550 | |
| 3551 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3552 | return -EINVAL; |
| 3553 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 3554 | return -EINVAL; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3555 | if (!poll->file) |
| 3556 | return -EBADF; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3557 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3558 | events = READ_ONCE(sqe->poll_events); |
| 3559 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3560 | return 0; |
| 3561 | } |
| 3562 | |
| 3563 | static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3564 | { |
| 3565 | struct io_poll_iocb *poll = &req->poll; |
| 3566 | struct io_ring_ctx *ctx = req->ctx; |
| 3567 | struct io_poll_table ipt; |
| 3568 | bool cancel = false; |
| 3569 | __poll_t mask; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3570 | |
| 3571 | INIT_IO_WORK(&req->work, io_poll_complete_work); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 3572 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3573 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3574 | poll->head = NULL; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3575 | poll->done = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3576 | poll->canceled = false; |
| 3577 | |
| 3578 | ipt.pt._qproc = io_poll_queue_proc; |
| 3579 | ipt.pt._key = poll->events; |
| 3580 | ipt.req = req; |
| 3581 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ |
| 3582 | |
| 3583 | /* initialized the list so that we can do list_empty checks */ |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3584 | INIT_LIST_HEAD(&poll->wait.entry); |
| 3585 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); |
| 3586 | poll->wait.private = poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3587 | |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 3588 | INIT_LIST_HEAD(&req->list); |
| 3589 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3590 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3591 | |
| 3592 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3593 | if (likely(poll->head)) { |
| 3594 | spin_lock(&poll->head->lock); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3595 | if (unlikely(list_empty(&poll->wait.entry))) { |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3596 | if (ipt.error) |
| 3597 | cancel = true; |
| 3598 | ipt.error = 0; |
| 3599 | mask = 0; |
| 3600 | } |
| 3601 | if (mask || ipt.error) |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 3602 | list_del_init(&poll->wait.entry); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3603 | else if (cancel) |
| 3604 | WRITE_ONCE(poll->canceled, true); |
| 3605 | else if (!poll->done) /* actually waiting for an event */ |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 3606 | io_poll_req_insert(req); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3607 | spin_unlock(&poll->head->lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3608 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3609 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3610 | ipt.error = 0; |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3611 | io_poll_complete(req, mask, 0); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3612 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3613 | spin_unlock_irq(&ctx->completion_lock); |
| 3614 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3615 | if (mask) { |
| 3616 | io_cqring_ev_posted(ctx); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3617 | io_put_req_find_next(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3618 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 3619 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 3620 | } |
| 3621 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3622 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 3623 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3624 | struct io_timeout_data *data = container_of(timer, |
| 3625 | struct io_timeout_data, timer); |
| 3626 | struct io_kiocb *req = data->req; |
| 3627 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3628 | unsigned long flags; |
| 3629 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3630 | atomic_inc(&ctx->cq_timeouts); |
| 3631 | |
| 3632 | spin_lock_irqsave(&ctx->completion_lock, flags); |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3633 | /* |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3634 | * We could be racing with timeout deletion. If the list is empty, |
| 3635 | * then timeout lookup already found it and will be handling it. |
zhangyi (F) | ef03681 | 2019-10-23 15:10:08 +0800 | [diff] [blame] | 3636 | */ |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3637 | if (!list_empty(&req->list)) { |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3638 | struct io_kiocb *prev; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3639 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3640 | /* |
| 3641 | * Adjust the reqs sequence before the current one because it |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 3642 | * will consume a slot in the cq_ring and the cq_tail |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3643 | * pointer will be increased, otherwise other timeout reqs may |
| 3644 | * return in advance without waiting for enough wait_nr. |
| 3645 | */ |
| 3646 | prev = req; |
| 3647 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 3648 | prev->sequence++; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3649 | list_del_init(&req->list); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3650 | } |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3651 | |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 3652 | io_cqring_fill_event(req, -ETIME); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3653 | io_commit_cqring(ctx); |
| 3654 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3655 | |
| 3656 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3657 | req_set_fail_links(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3658 | io_put_req(req); |
| 3659 | return HRTIMER_NORESTART; |
| 3660 | } |
| 3661 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3662 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
| 3663 | { |
| 3664 | struct io_kiocb *req; |
| 3665 | int ret = -ENOENT; |
| 3666 | |
| 3667 | list_for_each_entry(req, &ctx->timeout_list, list) { |
| 3668 | if (user_data == req->user_data) { |
| 3669 | list_del_init(&req->list); |
| 3670 | ret = 0; |
| 3671 | break; |
| 3672 | } |
| 3673 | } |
| 3674 | |
| 3675 | if (ret == -ENOENT) |
| 3676 | return ret; |
| 3677 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3678 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3679 | if (ret == -1) |
| 3680 | return -EALREADY; |
| 3681 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3682 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3683 | io_cqring_fill_event(req, -ECANCELED); |
| 3684 | io_put_req(req); |
| 3685 | return 0; |
| 3686 | } |
| 3687 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3688 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 3689 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3690 | { |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3691 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3692 | return -EINVAL; |
| 3693 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) |
| 3694 | return -EINVAL; |
| 3695 | |
| 3696 | req->timeout.addr = READ_ONCE(sqe->addr); |
| 3697 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); |
| 3698 | if (req->timeout.flags) |
| 3699 | return -EINVAL; |
| 3700 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3701 | return 0; |
| 3702 | } |
| 3703 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3704 | /* |
| 3705 | * Remove or update an existing timeout command |
| 3706 | */ |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3707 | static int io_timeout_remove(struct io_kiocb *req) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3708 | { |
| 3709 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3710 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3711 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3712 | spin_lock_irq(&ctx->completion_lock); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3713 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3714 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3715 | io_cqring_fill_event(req, ret); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3716 | io_commit_cqring(ctx); |
| 3717 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3718 | io_cqring_ev_posted(ctx); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3719 | if (ret < 0) |
| 3720 | req_set_fail_links(req); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 3721 | io_put_req(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3722 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3723 | } |
| 3724 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3725 | 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] | 3726 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3727 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3728 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3729 | unsigned flags; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3730 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3731 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3732 | return -EINVAL; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3733 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3734 | return -EINVAL; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3735 | if (sqe->off && is_timeout_link) |
| 3736 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 3737 | flags = READ_ONCE(sqe->timeout_flags); |
| 3738 | if (flags & ~IORING_TIMEOUT_ABS) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3739 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 3740 | |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3741 | req->timeout.count = READ_ONCE(sqe->off); |
| 3742 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3743 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3744 | return -ENOMEM; |
| 3745 | |
| 3746 | data = &req->io->timeout; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3747 | data->req = req; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3748 | req->flags |= REQ_F_TIMEOUT; |
| 3749 | |
| 3750 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3751 | return -EFAULT; |
| 3752 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3753 | if (flags & IORING_TIMEOUT_ABS) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3754 | data->mode = HRTIMER_MODE_ABS; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3755 | else |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3756 | data->mode = HRTIMER_MODE_REL; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 3757 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3758 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
| 3759 | return 0; |
| 3760 | } |
| 3761 | |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 3762 | static int io_timeout(struct io_kiocb *req) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3763 | { |
| 3764 | unsigned count; |
| 3765 | struct io_ring_ctx *ctx = req->ctx; |
| 3766 | struct io_timeout_data *data; |
| 3767 | struct list_head *entry; |
| 3768 | unsigned span = 0; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3769 | |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3770 | data = &req->io->timeout; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3771 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3772 | /* |
| 3773 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3774 | * timeout event to be satisfied. If it isn't set, then this is |
| 3775 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3776 | */ |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 3777 | count = req->timeout.count; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3778 | if (!count) { |
| 3779 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 3780 | spin_lock_irq(&ctx->completion_lock); |
| 3781 | entry = ctx->timeout_list.prev; |
| 3782 | goto add; |
| 3783 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3784 | |
| 3785 | req->sequence = ctx->cached_sq_head + count - 1; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3786 | data->seq_offset = count; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3787 | |
| 3788 | /* |
| 3789 | * Insertion sort, ensuring the first entry in the list is always |
| 3790 | * the one we need first. |
| 3791 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3792 | spin_lock_irq(&ctx->completion_lock); |
| 3793 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 3794 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3795 | unsigned nxt_sq_head; |
| 3796 | long long tmp, tmp_nxt; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3797 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3798 | |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3799 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 3800 | continue; |
| 3801 | |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3802 | /* |
| 3803 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 3804 | * long to store it. |
| 3805 | */ |
| 3806 | tmp = (long long)ctx->cached_sq_head + count - 1; |
Pavel Begunkov | cc42e0a | 2019-11-25 23:14:38 +0300 | [diff] [blame] | 3807 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
| 3808 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3809 | |
| 3810 | /* |
| 3811 | * cached_sq_head may overflow, and it will never overflow twice |
| 3812 | * once there is some timeout req still be valid. |
| 3813 | */ |
| 3814 | if (ctx->cached_sq_head < nxt_sq_head) |
yangerkun | 8b07a65 | 2019-10-17 12:12:35 +0800 | [diff] [blame] | 3815 | tmp += UINT_MAX; |
yangerkun | 5da0fb1 | 2019-10-15 21:59:29 +0800 | [diff] [blame] | 3816 | |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3817 | if (tmp > tmp_nxt) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3818 | break; |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3819 | |
| 3820 | /* |
| 3821 | * Sequence of reqs after the insert one and itself should |
| 3822 | * be adjusted because each timeout req consumes a slot. |
| 3823 | */ |
| 3824 | span++; |
| 3825 | nxt->sequence++; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3826 | } |
zhangyi (F) | a1f58ba | 2019-10-23 15:10:09 +0800 | [diff] [blame] | 3827 | req->sequence -= span; |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 3828 | add: |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3829 | list_add(&req->list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 3830 | data->timer.function = io_timeout_fn; |
| 3831 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 842f961 | 2019-10-29 12:34:10 -0600 | [diff] [blame] | 3832 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 3833 | return 0; |
| 3834 | } |
| 3835 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3836 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3837 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3838 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 3839 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3840 | return req->user_data == (unsigned long) data; |
| 3841 | } |
| 3842 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3843 | 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] | 3844 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3845 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3846 | int ret = 0; |
| 3847 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3848 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
| 3849 | switch (cancel_ret) { |
| 3850 | case IO_WQ_CANCEL_OK: |
| 3851 | ret = 0; |
| 3852 | break; |
| 3853 | case IO_WQ_CANCEL_RUNNING: |
| 3854 | ret = -EALREADY; |
| 3855 | break; |
| 3856 | case IO_WQ_CANCEL_NOTFOUND: |
| 3857 | ret = -ENOENT; |
| 3858 | break; |
| 3859 | } |
| 3860 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3861 | return ret; |
| 3862 | } |
| 3863 | |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3864 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
| 3865 | struct io_kiocb *req, __u64 sqe_addr, |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3866 | struct io_kiocb **nxt, int success_ret) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3867 | { |
| 3868 | unsigned long flags; |
| 3869 | int ret; |
| 3870 | |
| 3871 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); |
| 3872 | if (ret != -ENOENT) { |
| 3873 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3874 | goto done; |
| 3875 | } |
| 3876 | |
| 3877 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 3878 | ret = io_timeout_cancel(ctx, sqe_addr); |
| 3879 | if (ret != -ENOENT) |
| 3880 | goto done; |
| 3881 | ret = io_poll_cancel(ctx, sqe_addr); |
| 3882 | done: |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 3883 | if (!ret) |
| 3884 | ret = success_ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3885 | io_cqring_fill_event(req, ret); |
| 3886 | io_commit_cqring(ctx); |
| 3887 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 3888 | io_cqring_ev_posted(ctx); |
| 3889 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 3890 | if (ret < 0) |
| 3891 | req_set_fail_links(req); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 3892 | io_put_req_find_next(req, nxt); |
| 3893 | } |
| 3894 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3895 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 3896 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3897 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3898 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 3899 | return -EINVAL; |
| 3900 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || |
| 3901 | sqe->cancel_flags) |
| 3902 | return -EINVAL; |
| 3903 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3904 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 3905 | return 0; |
| 3906 | } |
| 3907 | |
| 3908 | static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt) |
| 3909 | { |
| 3910 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 3911 | |
| 3912 | io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 3913 | return 0; |
| 3914 | } |
| 3915 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 3916 | static int io_files_update_prep(struct io_kiocb *req, |
| 3917 | const struct io_uring_sqe *sqe) |
| 3918 | { |
| 3919 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) |
| 3920 | return -EINVAL; |
| 3921 | |
| 3922 | req->files_update.offset = READ_ONCE(sqe->off); |
| 3923 | req->files_update.nr_args = READ_ONCE(sqe->len); |
| 3924 | if (!req->files_update.nr_args) |
| 3925 | return -EINVAL; |
| 3926 | req->files_update.arg = READ_ONCE(sqe->addr); |
| 3927 | return 0; |
| 3928 | } |
| 3929 | |
| 3930 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) |
| 3931 | { |
| 3932 | struct io_ring_ctx *ctx = req->ctx; |
| 3933 | struct io_uring_files_update up; |
| 3934 | int ret; |
| 3935 | |
| 3936 | if (force_nonblock) { |
| 3937 | req->work.flags |= IO_WQ_WORK_NEEDS_FILES; |
| 3938 | return -EAGAIN; |
| 3939 | } |
| 3940 | |
| 3941 | up.offset = req->files_update.offset; |
| 3942 | up.fds = req->files_update.arg; |
| 3943 | |
| 3944 | mutex_lock(&ctx->uring_lock); |
| 3945 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); |
| 3946 | mutex_unlock(&ctx->uring_lock); |
| 3947 | |
| 3948 | if (ret < 0) |
| 3949 | req_set_fail_links(req); |
| 3950 | io_cqring_add_event(req, ret); |
| 3951 | io_put_req(req); |
| 3952 | return 0; |
| 3953 | } |
| 3954 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3955 | static int io_req_defer_prep(struct io_kiocb *req, |
| 3956 | const struct io_uring_sqe *sqe) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3957 | { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3958 | ssize_t ret = 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3959 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 3960 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 3961 | case IORING_OP_NOP: |
| 3962 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3963 | case IORING_OP_READV: |
| 3964 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3965 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3966 | ret = io_read_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3967 | break; |
| 3968 | case IORING_OP_WRITEV: |
| 3969 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3970 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3971 | ret = io_write_prep(req, sqe, true); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3972 | break; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3973 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3974 | ret = io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3975 | break; |
| 3976 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3977 | ret = io_poll_remove_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 3978 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3979 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3980 | ret = io_prep_fsync(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3981 | break; |
| 3982 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3983 | ret = io_prep_sfr(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 3984 | break; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3985 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3986 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3987 | ret = io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3988 | break; |
| 3989 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 3990 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3991 | ret = io_recvmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 3992 | break; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3993 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3994 | ret = io_connect_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 3995 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 3996 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3997 | ret = io_timeout_prep(req, sqe, false); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3998 | break; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 3999 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4000 | ret = io_timeout_remove_prep(req, sqe); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 4001 | break; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4002 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4003 | ret = io_async_cancel_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 4004 | break; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4005 | case IORING_OP_LINK_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4006 | ret = io_timeout_prep(req, sqe, true); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4007 | break; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4008 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4009 | ret = io_accept_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4010 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4011 | case IORING_OP_FALLOCATE: |
| 4012 | ret = io_fallocate_prep(req, sqe); |
| 4013 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4014 | case IORING_OP_OPENAT: |
| 4015 | ret = io_openat_prep(req, sqe); |
| 4016 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4017 | case IORING_OP_CLOSE: |
| 4018 | ret = io_close_prep(req, sqe); |
| 4019 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4020 | case IORING_OP_FILES_UPDATE: |
| 4021 | ret = io_files_update_prep(req, sqe); |
| 4022 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4023 | case IORING_OP_STATX: |
| 4024 | ret = io_statx_prep(req, sqe); |
| 4025 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4026 | case IORING_OP_FADVISE: |
| 4027 | ret = io_fadvise_prep(req, sqe); |
| 4028 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4029 | case IORING_OP_MADVISE: |
| 4030 | ret = io_madvise_prep(req, sqe); |
| 4031 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4032 | case IORING_OP_OPENAT2: |
| 4033 | ret = io_openat2_prep(req, sqe); |
| 4034 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4035 | default: |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 4036 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 4037 | req->opcode); |
| 4038 | ret = -EINVAL; |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4039 | break; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4040 | } |
| 4041 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4042 | return ret; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4043 | } |
| 4044 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4045 | 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] | 4046 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4047 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4048 | int ret; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4049 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4050 | /* Still need defer if there is pending req in defer list. */ |
| 4051 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4052 | return 0; |
| 4053 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4054 | if (!req->io && io_alloc_async_ctx(req)) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4055 | return -EAGAIN; |
| 4056 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4057 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4058 | if (ret < 0) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4059 | return ret; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4060 | |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4061 | spin_lock_irq(&ctx->completion_lock); |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 4062 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4063 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4064 | return 0; |
| 4065 | } |
| 4066 | |
Jens Axboe | 915967f | 2019-11-21 09:01:20 -0700 | [diff] [blame] | 4067 | trace_io_uring_defer(ctx, req, req->user_data); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4068 | list_add_tail(&req->list, &ctx->defer_list); |
| 4069 | spin_unlock_irq(&ctx->completion_lock); |
| 4070 | return -EIOCBQUEUED; |
| 4071 | } |
| 4072 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4073 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4074 | struct io_kiocb **nxt, bool force_nonblock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4075 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4076 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4077 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4078 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4079 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4080 | case IORING_OP_NOP: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4081 | ret = io_nop(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4082 | break; |
| 4083 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4084 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4085 | case IORING_OP_READ: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4086 | if (sqe) { |
| 4087 | ret = io_read_prep(req, sqe, force_nonblock); |
| 4088 | if (ret < 0) |
| 4089 | break; |
| 4090 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4091 | ret = io_read(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4092 | break; |
| 4093 | case IORING_OP_WRITEV: |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4094 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 4095 | case IORING_OP_WRITE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4096 | if (sqe) { |
| 4097 | ret = io_write_prep(req, sqe, force_nonblock); |
| 4098 | if (ret < 0) |
| 4099 | break; |
| 4100 | } |
Pavel Begunkov | 267bc90 | 2019-11-07 01:41:08 +0300 | [diff] [blame] | 4101 | ret = io_write(req, nxt, force_nonblock); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4102 | break; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4103 | case IORING_OP_FSYNC: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4104 | if (sqe) { |
| 4105 | ret = io_prep_fsync(req, sqe); |
| 4106 | if (ret < 0) |
| 4107 | break; |
| 4108 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4109 | ret = io_fsync(req, nxt, force_nonblock); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4110 | break; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4111 | case IORING_OP_POLL_ADD: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4112 | if (sqe) { |
| 4113 | ret = io_poll_add_prep(req, sqe); |
| 4114 | if (ret) |
| 4115 | break; |
| 4116 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4117 | ret = io_poll_add(req, nxt); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4118 | break; |
| 4119 | case IORING_OP_POLL_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4120 | if (sqe) { |
| 4121 | ret = io_poll_remove_prep(req, sqe); |
| 4122 | if (ret < 0) |
| 4123 | break; |
| 4124 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4125 | ret = io_poll_remove(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 4126 | break; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4127 | case IORING_OP_SYNC_FILE_RANGE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4128 | if (sqe) { |
| 4129 | ret = io_prep_sfr(req, sqe); |
| 4130 | if (ret < 0) |
| 4131 | break; |
| 4132 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4133 | ret = io_sync_file_range(req, nxt, force_nonblock); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4134 | break; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4135 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4136 | case IORING_OP_SEND: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4137 | if (sqe) { |
| 4138 | ret = io_sendmsg_prep(req, sqe); |
| 4139 | if (ret < 0) |
| 4140 | break; |
| 4141 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4142 | if (req->opcode == IORING_OP_SENDMSG) |
| 4143 | ret = io_sendmsg(req, nxt, force_nonblock); |
| 4144 | else |
| 4145 | ret = io_send(req, nxt, force_nonblock); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 4146 | break; |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4147 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4148 | case IORING_OP_RECV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4149 | if (sqe) { |
| 4150 | ret = io_recvmsg_prep(req, sqe); |
| 4151 | if (ret) |
| 4152 | break; |
| 4153 | } |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4154 | if (req->opcode == IORING_OP_RECVMSG) |
| 4155 | ret = io_recvmsg(req, nxt, force_nonblock); |
| 4156 | else |
| 4157 | ret = io_recv(req, nxt, force_nonblock); |
Jens Axboe | aa1fa28 | 2019-04-19 13:38:09 -0600 | [diff] [blame] | 4158 | break; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4159 | case IORING_OP_TIMEOUT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4160 | if (sqe) { |
| 4161 | ret = io_timeout_prep(req, sqe, false); |
| 4162 | if (ret) |
| 4163 | break; |
| 4164 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4165 | ret = io_timeout(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 4166 | break; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4167 | case IORING_OP_TIMEOUT_REMOVE: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4168 | if (sqe) { |
| 4169 | ret = io_timeout_remove_prep(req, sqe); |
| 4170 | if (ret) |
| 4171 | break; |
| 4172 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4173 | ret = io_timeout_remove(req); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 4174 | break; |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4175 | case IORING_OP_ACCEPT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4176 | if (sqe) { |
| 4177 | ret = io_accept_prep(req, sqe); |
| 4178 | if (ret) |
| 4179 | break; |
| 4180 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4181 | ret = io_accept(req, nxt, force_nonblock); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 4182 | break; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4183 | case IORING_OP_CONNECT: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4184 | if (sqe) { |
| 4185 | ret = io_connect_prep(req, sqe); |
| 4186 | if (ret) |
| 4187 | break; |
| 4188 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4189 | ret = io_connect(req, nxt, force_nonblock); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 4190 | break; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4191 | case IORING_OP_ASYNC_CANCEL: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4192 | if (sqe) { |
| 4193 | ret = io_async_cancel_prep(req, sqe); |
| 4194 | if (ret) |
| 4195 | break; |
| 4196 | } |
Jens Axboe | fc4df99 | 2019-12-10 14:38:45 -0700 | [diff] [blame] | 4197 | ret = io_async_cancel(req, nxt); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 4198 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4199 | case IORING_OP_FALLOCATE: |
| 4200 | if (sqe) { |
| 4201 | ret = io_fallocate_prep(req, sqe); |
| 4202 | if (ret) |
| 4203 | break; |
| 4204 | } |
| 4205 | ret = io_fallocate(req, nxt, force_nonblock); |
| 4206 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4207 | case IORING_OP_OPENAT: |
| 4208 | if (sqe) { |
| 4209 | ret = io_openat_prep(req, sqe); |
| 4210 | if (ret) |
| 4211 | break; |
| 4212 | } |
| 4213 | ret = io_openat(req, nxt, force_nonblock); |
| 4214 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4215 | case IORING_OP_CLOSE: |
| 4216 | if (sqe) { |
| 4217 | ret = io_close_prep(req, sqe); |
| 4218 | if (ret) |
| 4219 | break; |
| 4220 | } |
| 4221 | ret = io_close(req, nxt, force_nonblock); |
| 4222 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4223 | case IORING_OP_FILES_UPDATE: |
| 4224 | if (sqe) { |
| 4225 | ret = io_files_update_prep(req, sqe); |
| 4226 | if (ret) |
| 4227 | break; |
| 4228 | } |
| 4229 | ret = io_files_update(req, force_nonblock); |
| 4230 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4231 | case IORING_OP_STATX: |
| 4232 | if (sqe) { |
| 4233 | ret = io_statx_prep(req, sqe); |
| 4234 | if (ret) |
| 4235 | break; |
| 4236 | } |
| 4237 | ret = io_statx(req, nxt, force_nonblock); |
| 4238 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4239 | case IORING_OP_FADVISE: |
| 4240 | if (sqe) { |
| 4241 | ret = io_fadvise_prep(req, sqe); |
| 4242 | if (ret) |
| 4243 | break; |
| 4244 | } |
| 4245 | ret = io_fadvise(req, nxt, force_nonblock); |
| 4246 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4247 | case IORING_OP_MADVISE: |
| 4248 | if (sqe) { |
| 4249 | ret = io_madvise_prep(req, sqe); |
| 4250 | if (ret) |
| 4251 | break; |
| 4252 | } |
| 4253 | ret = io_madvise(req, nxt, force_nonblock); |
| 4254 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4255 | case IORING_OP_OPENAT2: |
| 4256 | if (sqe) { |
| 4257 | ret = io_openat2_prep(req, sqe); |
| 4258 | if (ret) |
| 4259 | break; |
| 4260 | } |
| 4261 | ret = io_openat2(req, nxt, force_nonblock); |
| 4262 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4263 | default: |
| 4264 | ret = -EINVAL; |
| 4265 | break; |
| 4266 | } |
| 4267 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4268 | if (ret) |
| 4269 | return ret; |
| 4270 | |
| 4271 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4272 | const bool in_async = io_wq_current_is_worker(); |
| 4273 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4274 | if (req->result == -EAGAIN) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4275 | return -EAGAIN; |
| 4276 | |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4277 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 4278 | if (in_async) |
| 4279 | mutex_lock(&ctx->uring_lock); |
| 4280 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4281 | io_iopoll_req_issued(req); |
Jens Axboe | 11ba820 | 2020-01-15 21:51:17 -0700 | [diff] [blame] | 4282 | |
| 4283 | if (in_async) |
| 4284 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4285 | } |
| 4286 | |
| 4287 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4288 | } |
| 4289 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4290 | static void io_wq_submit_work(struct io_wq_work **workptr) |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4291 | { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4292 | struct io_wq_work *work = *workptr; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4293 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4294 | struct io_kiocb *nxt = NULL; |
| 4295 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4296 | |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4297 | /* if NO_CANCEL is set, we must still run the work */ |
| 4298 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == |
| 4299 | IO_WQ_WORK_CANCEL) { |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4300 | ret = -ECANCELED; |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 4301 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4302 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4303 | if (!ret) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4304 | req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0; |
| 4305 | req->in_async = true; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4306 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4307 | ret = io_issue_sqe(req, NULL, &nxt, false); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4308 | /* |
| 4309 | * We can get EAGAIN for polled IO even though we're |
| 4310 | * forcing a sync submission from here, since we can't |
| 4311 | * wait for request slots on the block side. |
| 4312 | */ |
| 4313 | if (ret != -EAGAIN) |
| 4314 | break; |
| 4315 | cond_resched(); |
| 4316 | } while (1); |
| 4317 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4318 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4319 | /* drop submission reference */ |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 4320 | io_put_req(req); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4321 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4322 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4323 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4324 | io_cqring_add_event(req, ret); |
Jens Axboe | 817869d | 2019-04-30 14:44:05 -0600 | [diff] [blame] | 4325 | io_put_req(req); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 4326 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4327 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 4328 | /* if a dependent link is ready, pass it back */ |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4329 | if (!ret && nxt) |
| 4330 | io_wq_assign_next(workptr, nxt); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 4331 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4332 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4333 | static int io_req_needs_file(struct io_kiocb *req, int fd) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4334 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4335 | if (!io_op_defs[req->opcode].needs_file) |
Jens Axboe | 9e3aa61 | 2019-12-11 15:55:43 -0700 | [diff] [blame] | 4336 | return 0; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4337 | if (fd == -1 && io_op_defs[req->opcode].fd_non_neg) |
| 4338 | return 0; |
| 4339 | return 1; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4340 | } |
| 4341 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4342 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 4343 | int index) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4344 | { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4345 | struct fixed_file_table *table; |
| 4346 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4347 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
| 4348 | return table->files[index & IORING_FILE_TABLE_MASK];; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4349 | } |
| 4350 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4351 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
| 4352 | const struct io_uring_sqe *sqe) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4353 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4354 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4355 | unsigned flags; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4356 | int fd; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4357 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4358 | flags = READ_ONCE(sqe->flags); |
| 4359 | fd = READ_ONCE(sqe->fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4360 | |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4361 | if (flags & IOSQE_IO_DRAIN) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4362 | req->flags |= REQ_F_IO_DRAIN; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 4363 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4364 | if (!io_req_needs_file(req, fd)) |
| 4365 | return 0; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4366 | |
| 4367 | if (flags & IOSQE_FIXED_FILE) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4368 | if (unlikely(!ctx->file_data || |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4369 | (unsigned) fd >= ctx->nr_user_files)) |
| 4370 | return -EBADF; |
Jens Axboe | b762012 | 2019-10-26 07:22:55 -0600 | [diff] [blame] | 4371 | fd = array_index_nospec(fd, ctx->nr_user_files); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 4372 | req->file = io_file_from_index(ctx, fd); |
| 4373 | if (!req->file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 4374 | return -EBADF; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4375 | req->flags |= REQ_F_FIXED_FILE; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 4376 | percpu_ref_get(&ctx->file_data->refs); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4377 | } else { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4378 | if (req->needs_fixed_file) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4379 | return -EBADF; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 4380 | trace_io_uring_file_get(ctx, fd); |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4381 | req->file = io_file_get(state, fd); |
| 4382 | if (unlikely(!req->file)) |
| 4383 | return -EBADF; |
| 4384 | } |
| 4385 | |
| 4386 | return 0; |
| 4387 | } |
| 4388 | |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4389 | static int io_grab_files(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4390 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4391 | int ret = -EBADF; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4392 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4393 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4394 | if (!ctx->ring_file) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4395 | return -EBADF; |
| 4396 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4397 | rcu_read_lock(); |
| 4398 | spin_lock_irq(&ctx->inflight_lock); |
| 4399 | /* |
| 4400 | * We use the f_ops->flush() handler to ensure that we can flush |
| 4401 | * out work accessing these files if the fd is closed. Check if |
| 4402 | * the fd has changed since we started down this path, and disallow |
| 4403 | * this operation if it has. |
| 4404 | */ |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4405 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4406 | list_add(&req->inflight_entry, &ctx->inflight_list); |
| 4407 | req->flags |= REQ_F_INFLIGHT; |
| 4408 | req->work.files = current->files; |
| 4409 | ret = 0; |
| 4410 | } |
| 4411 | spin_unlock_irq(&ctx->inflight_lock); |
| 4412 | rcu_read_unlock(); |
| 4413 | |
| 4414 | return ret; |
| 4415 | } |
| 4416 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4417 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 4418 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4419 | struct io_timeout_data *data = container_of(timer, |
| 4420 | struct io_timeout_data, timer); |
| 4421 | struct io_kiocb *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4422 | struct io_ring_ctx *ctx = req->ctx; |
| 4423 | struct io_kiocb *prev = NULL; |
| 4424 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4425 | |
| 4426 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 4427 | |
| 4428 | /* |
| 4429 | * We don't expect the list to be empty, that will only happen if we |
| 4430 | * race with the completion of the linked work. |
| 4431 | */ |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4432 | if (!list_empty(&req->link_list)) { |
| 4433 | prev = list_entry(req->link_list.prev, struct io_kiocb, |
| 4434 | link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4435 | if (refcount_inc_not_zero(&prev->refs)) { |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4436 | list_del_init(&req->link_list); |
Jens Axboe | 5d96072 | 2019-11-19 15:31:28 -0700 | [diff] [blame] | 4437 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
| 4438 | } else |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4439 | prev = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4440 | } |
| 4441 | |
| 4442 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 4443 | |
| 4444 | if (prev) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4445 | req_set_fail_links(prev); |
Jens Axboe | b0dd8a4 | 2019-11-18 12:14:54 -0700 | [diff] [blame] | 4446 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
| 4447 | -ETIME); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4448 | io_put_req(prev); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 4449 | } else { |
| 4450 | io_cqring_add_event(req, -ETIME); |
| 4451 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4452 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4453 | return HRTIMER_NORESTART; |
| 4454 | } |
| 4455 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4456 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4457 | { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4458 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4459 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4460 | /* |
| 4461 | * If the list is now empty, then our linked request finished before |
| 4462 | * we got a chance to setup the timer |
| 4463 | */ |
| 4464 | spin_lock_irq(&ctx->completion_lock); |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4465 | if (!list_empty(&req->link_list)) { |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4466 | struct io_timeout_data *data = &req->io->timeout; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4467 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4468 | data->timer.function = io_link_timeout_fn; |
| 4469 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 4470 | data->mode); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4471 | } |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4472 | spin_unlock_irq(&ctx->completion_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4473 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4474 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4475 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4476 | } |
| 4477 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 4478 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4479 | { |
| 4480 | struct io_kiocb *nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4481 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4482 | if (!(req->flags & REQ_F_LINK)) |
| 4483 | return NULL; |
| 4484 | |
Pavel Begunkov | 4493233 | 2019-12-05 16:16:35 +0300 | [diff] [blame] | 4485 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| 4486 | link_list); |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 4487 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4488 | return NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4489 | |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4490 | req->flags |= REQ_F_LINK_TIMEOUT; |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4491 | return nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 4492 | } |
| 4493 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4494 | static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4495 | { |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4496 | struct io_kiocb *linked_timeout; |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4497 | struct io_kiocb *nxt = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4498 | int ret; |
| 4499 | |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4500 | again: |
| 4501 | linked_timeout = io_prep_linked_timeout(req); |
| 4502 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4503 | ret = io_issue_sqe(req, sqe, &nxt, true); |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 4504 | |
| 4505 | /* |
| 4506 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 4507 | * doesn't support non-blocking read/write attempts |
| 4508 | */ |
| 4509 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 4510 | (req->flags & REQ_F_MUST_PUNT))) { |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4511 | if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) { |
| 4512 | ret = io_grab_files(req); |
| 4513 | if (ret) |
| 4514 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4515 | } |
Pavel Begunkov | bbad27b | 2019-11-19 23:32:47 +0300 | [diff] [blame] | 4516 | |
| 4517 | /* |
| 4518 | * Queued up for async execution, worker will release |
| 4519 | * submit reference when the iocb is actually submitted. |
| 4520 | */ |
| 4521 | io_queue_async_work(req); |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4522 | goto done_req; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4523 | } |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4524 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 4525 | err: |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4526 | /* drop submission reference */ |
| 4527 | io_put_req(req); |
| 4528 | |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4529 | if (linked_timeout) { |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4530 | if (!ret) |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4531 | io_queue_linked_timeout(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4532 | else |
Pavel Begunkov | f9bd67f | 2019-11-21 23:21:03 +0300 | [diff] [blame] | 4533 | io_put_req(linked_timeout); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 4534 | } |
| 4535 | |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4536 | /* and drop final reference, if we failed */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4537 | if (ret) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4538 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4539 | req_set_fail_links(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 4540 | io_put_req(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4541 | } |
Jens Axboe | 4a0a7a1 | 2019-12-09 20:01:01 -0700 | [diff] [blame] | 4542 | done_req: |
| 4543 | if (nxt) { |
| 4544 | req = nxt; |
| 4545 | nxt = NULL; |
| 4546 | goto again; |
| 4547 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4548 | } |
| 4549 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4550 | 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] | 4551 | { |
| 4552 | int ret; |
| 4553 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4554 | ret = io_req_defer(req, sqe); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4555 | if (ret) { |
| 4556 | if (ret != -EIOCBQUEUED) { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4557 | io_cqring_add_event(req, ret); |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4558 | req_set_fail_links(req); |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4559 | io_double_put_req(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4560 | } |
Pavel Begunkov | 2550878 | 2019-12-30 21:24:47 +0300 | [diff] [blame] | 4561 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4562 | /* |
| 4563 | * Never try inline submit of IOSQE_ASYNC is set, go straight |
| 4564 | * to async execution. |
| 4565 | */ |
| 4566 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 4567 | io_queue_async_work(req); |
| 4568 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4569 | __io_queue_sqe(req, sqe); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4570 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4571 | } |
| 4572 | |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4573 | static inline void io_queue_link_head(struct io_kiocb *req) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4574 | { |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 4575 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4576 | io_cqring_add_event(req, -ECANCELED); |
| 4577 | io_double_put_req(req); |
| 4578 | } else |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4579 | io_queue_sqe(req, NULL); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 4580 | } |
| 4581 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4582 | #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] | 4583 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4584 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4585 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 4586 | struct io_submit_state *state, struct io_kiocb **link) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4587 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 4588 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4589 | unsigned int sqe_flags; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4590 | int ret; |
| 4591 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4592 | sqe_flags = READ_ONCE(sqe->flags); |
| 4593 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4594 | /* enforce forwards compatibility on users */ |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4595 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4596 | ret = -EINVAL; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4597 | goto err_req; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4598 | } |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4599 | if (sqe_flags & IOSQE_ASYNC) |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 4600 | req->flags |= REQ_F_FORCE_ASYNC; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4601 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4602 | ret = io_req_set_file(state, req, sqe); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4603 | if (unlikely(ret)) { |
| 4604 | err_req: |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 4605 | io_cqring_add_event(req, ret); |
| 4606 | io_double_put_req(req); |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4607 | return false; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4608 | } |
| 4609 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4610 | /* |
| 4611 | * If we already have a head request, queue this one for async |
| 4612 | * submittal once the head completes. If we don't have a head but |
| 4613 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 4614 | * submitted sync once the chain is complete. If none of those |
| 4615 | * conditions are true (normal request), then just queue it. |
| 4616 | */ |
| 4617 | if (*link) { |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4618 | struct io_kiocb *head = *link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4619 | |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4620 | if (sqe_flags & IOSQE_IO_DRAIN) { |
| 4621 | head->flags |= REQ_F_IO_DRAIN; |
| 4622 | ctx->drain_next = 1; |
| 4623 | } |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4624 | |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4625 | if (sqe_flags & IOSQE_IO_HARDLINK) |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4626 | req->flags |= REQ_F_HARDLINK; |
| 4627 | |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 4628 | if (io_alloc_async_ctx(req)) { |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4629 | ret = -EAGAIN; |
| 4630 | goto err_req; |
| 4631 | } |
| 4632 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4633 | ret = io_req_defer_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4634 | if (ret) { |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 4635 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4636 | head->flags |= REQ_F_FAIL_LINK; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 4637 | goto err_req; |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 4638 | } |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 4639 | trace_io_uring_link(ctx, req, head); |
| 4640 | list_add_tail(&req->link_list, &head->link_list); |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 4641 | |
| 4642 | /* last request of a link, enqueue the link */ |
| 4643 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) { |
| 4644 | io_queue_link_head(head); |
| 4645 | *link = NULL; |
| 4646 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4647 | } else { |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 4648 | if (unlikely(ctx->drain_next)) { |
| 4649 | req->flags |= REQ_F_IO_DRAIN; |
| 4650 | req->ctx->drain_next = 0; |
| 4651 | } |
| 4652 | if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { |
| 4653 | req->flags |= REQ_F_LINK; |
| 4654 | if (sqe_flags & IOSQE_IO_HARDLINK) |
| 4655 | req->flags |= REQ_F_HARDLINK; |
| 4656 | |
| 4657 | INIT_LIST_HEAD(&req->link_list); |
| 4658 | ret = io_req_defer_prep(req, sqe); |
| 4659 | if (ret) |
| 4660 | req->flags |= REQ_F_FAIL_LINK; |
| 4661 | *link = req; |
| 4662 | } else { |
| 4663 | io_queue_sqe(req, sqe); |
| 4664 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4665 | } |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4666 | |
| 4667 | return true; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4668 | } |
| 4669 | |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4670 | /* |
| 4671 | * Batched submission is done, ensure local IO is flushed out. |
| 4672 | */ |
| 4673 | static void io_submit_state_end(struct io_submit_state *state) |
| 4674 | { |
| 4675 | blk_finish_plug(&state->plug); |
Jens Axboe | 3d6770f | 2019-04-13 11:50:54 -0600 | [diff] [blame] | 4676 | io_file_put(state); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4677 | if (state->free_reqs) |
| 4678 | kmem_cache_free_bulk(req_cachep, state->free_reqs, |
| 4679 | &state->reqs[state->cur_req]); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | /* |
| 4683 | * Start submission side cache. |
| 4684 | */ |
| 4685 | static void io_submit_state_start(struct io_submit_state *state, |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4686 | unsigned int max_ios) |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4687 | { |
| 4688 | blk_start_plug(&state->plug); |
Jens Axboe | 2579f91 | 2019-01-09 09:10:43 -0700 | [diff] [blame] | 4689 | state->free_reqs = 0; |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 4690 | state->file = NULL; |
| 4691 | state->ios_left = max_ios; |
| 4692 | } |
| 4693 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4694 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 4695 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4696 | struct io_rings *rings = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4697 | |
Pavel Begunkov | caf582c | 2019-12-30 21:24:46 +0300 | [diff] [blame] | 4698 | /* |
| 4699 | * Ensure any loads from the SQEs are done at this point, |
| 4700 | * since once we write the new head, the application could |
| 4701 | * write new data to them. |
| 4702 | */ |
| 4703 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4704 | } |
| 4705 | |
| 4706 | /* |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4707 | * 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] | 4708 | * that is mapped by userspace. This means that care needs to be taken to |
| 4709 | * ensure that reads are stable, as we cannot rely on userspace always |
| 4710 | * being a good citizen. If members of the sqe are validated and then later |
| 4711 | * used, it's important that those reads are done through READ_ONCE() to |
| 4712 | * prevent a re-load down the line. |
| 4713 | */ |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4714 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 4715 | const struct io_uring_sqe **sqe_ptr) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4716 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4717 | u32 *sq_array = ctx->sq_array; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4718 | unsigned head; |
| 4719 | |
| 4720 | /* |
| 4721 | * The cached sq head (or cq tail) serves two purposes: |
| 4722 | * |
| 4723 | * 1) allows us to batch the cost of updating the user visible |
| 4724 | * head updates. |
| 4725 | * 2) allows the kernel side to track the head on its own, even |
| 4726 | * though the application is the one updating it. |
| 4727 | */ |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4728 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
Pavel Begunkov | 9835d6f | 2019-11-21 21:24:56 +0300 | [diff] [blame] | 4729 | if (likely(head < ctx->sq_entries)) { |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4730 | /* |
| 4731 | * All io need record the previous position, if LINK vs DARIN, |
| 4732 | * it can be used to mark the position of the first IO in the |
| 4733 | * link list. |
| 4734 | */ |
| 4735 | req->sequence = ctx->cached_sq_head; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4736 | *sqe_ptr = &ctx->sq_sqes[head]; |
| 4737 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); |
| 4738 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4739 | ctx->cached_sq_head++; |
| 4740 | return true; |
| 4741 | } |
| 4742 | |
| 4743 | /* drop invalid entries */ |
| 4744 | ctx->cached_sq_head++; |
Jens Axboe | 498ccd9 | 2019-10-25 10:04:25 -0600 | [diff] [blame] | 4745 | ctx->cached_sq_dropped++; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4746 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4747 | return false; |
| 4748 | } |
| 4749 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4750 | 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] | 4751 | struct file *ring_file, int ring_fd, |
| 4752 | struct mm_struct **mm, bool async) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4753 | { |
| 4754 | struct io_submit_state state, *statep = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4755 | struct io_kiocb *link = NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4756 | int i, submitted = 0; |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4757 | bool mm_fault = false; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4758 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 4759 | /* 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] | 4760 | if (test_bit(0, &ctx->sq_check_overflow)) { |
| 4761 | if (!list_empty(&ctx->cq_overflow_list) && |
| 4762 | !io_cqring_overflow_flush(ctx, false)) |
| 4763 | return -EBUSY; |
| 4764 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4765 | |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 4766 | /* make sure SQ entry isn't read before tail */ |
| 4767 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); |
Pavel Begunkov | 9ef4f12 | 2019-12-30 21:24:44 +0300 | [diff] [blame] | 4768 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4769 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
| 4770 | return -EAGAIN; |
| 4771 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4772 | if (nr > IO_PLUG_THRESHOLD) { |
Jackie Liu | 22efde5 | 2019-12-02 17:14:52 +0800 | [diff] [blame] | 4773 | io_submit_state_start(&state, nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4774 | statep = &state; |
| 4775 | } |
| 4776 | |
Pavel Begunkov | b14cca0 | 2020-01-17 04:45:59 +0300 | [diff] [blame] | 4777 | ctx->ring_fd = ring_fd; |
| 4778 | ctx->ring_file = ring_file; |
| 4779 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4780 | for (i = 0; i < nr; i++) { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4781 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4782 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4783 | |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4784 | req = io_get_req(ctx, statep); |
| 4785 | if (unlikely(!req)) { |
| 4786 | if (!submitted) |
| 4787 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4788 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4789 | } |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4790 | if (!io_get_sqring(ctx, req, &sqe)) { |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4791 | __io_req_do_free(req); |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 4792 | break; |
| 4793 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4794 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 4795 | /* will complete beyond this point, count as submitted */ |
| 4796 | submitted++; |
| 4797 | |
| 4798 | if (unlikely(req->opcode >= IORING_OP_LAST)) { |
| 4799 | io_cqring_add_event(req, -EINVAL); |
| 4800 | io_double_put_req(req); |
| 4801 | break; |
| 4802 | } |
| 4803 | |
| 4804 | if (io_op_defs[req->opcode].needs_mm && !*mm) { |
Pavel Begunkov | 95a1b3ff | 2019-10-27 23:15:41 +0300 | [diff] [blame] | 4805 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
| 4806 | if (!mm_fault) { |
| 4807 | use_mm(ctx->sqo_mm); |
| 4808 | *mm = ctx->sqo_mm; |
| 4809 | } |
| 4810 | } |
| 4811 | |
Pavel Begunkov | cf6fd4b | 2019-11-25 23:14:39 +0300 | [diff] [blame] | 4812 | req->has_user = *mm != NULL; |
| 4813 | req->in_async = async; |
| 4814 | req->needs_fixed_file = async; |
Jens Axboe | 354420f | 2020-01-08 18:55:15 -0700 | [diff] [blame] | 4815 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
| 4816 | true, async); |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4817 | if (!io_submit_sqe(req, sqe, statep, &link)) |
Pavel Begunkov | 2e6e1fd | 2019-12-05 16:15:45 +0300 | [diff] [blame] | 4818 | break; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4819 | } |
| 4820 | |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 4821 | if (submitted != nr) |
| 4822 | percpu_ref_put_many(&ctx->refs, nr - submitted); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 4823 | if (link) |
Pavel Begunkov | 1b4a51b | 2019-11-21 11:54:28 +0300 | [diff] [blame] | 4824 | io_queue_link_head(link); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4825 | if (statep) |
| 4826 | io_submit_state_end(&state); |
| 4827 | |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 4828 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 4829 | io_commit_sqring(ctx); |
| 4830 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4831 | return submitted; |
| 4832 | } |
| 4833 | |
| 4834 | static int io_sq_thread(void *data) |
| 4835 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4836 | struct io_ring_ctx *ctx = data; |
| 4837 | struct mm_struct *cur_mm = NULL; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4838 | const struct cred *old_cred; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4839 | mm_segment_t old_fs; |
| 4840 | DEFINE_WAIT(wait); |
| 4841 | unsigned inflight; |
| 4842 | unsigned long timeout; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4843 | int ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4844 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 4845 | complete(&ctx->completions[1]); |
Jackie Liu | a4c0b3d | 2019-07-08 13:41:12 +0800 | [diff] [blame] | 4846 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4847 | old_fs = get_fs(); |
| 4848 | set_fs(USER_DS); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4849 | old_cred = override_creds(ctx->creds); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4850 | |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4851 | ret = timeout = inflight = 0; |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4852 | while (!kthread_should_park()) { |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4853 | unsigned int to_submit; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4854 | |
| 4855 | if (inflight) { |
| 4856 | unsigned nr_events = 0; |
| 4857 | |
| 4858 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | 2b2ed97 | 2019-10-25 10:06:15 -0600 | [diff] [blame] | 4859 | /* |
| 4860 | * inflight is the count of the maximum possible |
| 4861 | * entries we submitted, but it can be smaller |
| 4862 | * if we dropped some of them. If we don't have |
| 4863 | * poll entries available, then we know that we |
| 4864 | * have nothing left to poll for. Reset the |
| 4865 | * inflight count to zero in that case. |
| 4866 | */ |
| 4867 | mutex_lock(&ctx->uring_lock); |
| 4868 | if (!list_empty(&ctx->poll_list)) |
| 4869 | __io_iopoll_check(ctx, &nr_events, 0); |
| 4870 | else |
| 4871 | inflight = 0; |
| 4872 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4873 | } else { |
| 4874 | /* |
| 4875 | * Normal IO, just pretend everything completed. |
| 4876 | * We don't have to poll completions for that. |
| 4877 | */ |
| 4878 | nr_events = inflight; |
| 4879 | } |
| 4880 | |
| 4881 | inflight -= nr_events; |
| 4882 | if (!inflight) |
| 4883 | timeout = jiffies + ctx->sq_thread_idle; |
| 4884 | } |
| 4885 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4886 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4887 | |
| 4888 | /* |
| 4889 | * If submit got -EBUSY, flag us as needing the application |
| 4890 | * to enter the kernel to reap and flush events. |
| 4891 | */ |
| 4892 | if (!to_submit || ret == -EBUSY) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4893 | /* |
| 4894 | * We're polling. If we're within the defined idle |
| 4895 | * period, then let us spin without work before going |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4896 | * to sleep. The exception is if we got EBUSY doing |
| 4897 | * more IO, we should wait for the application to |
| 4898 | * reap events and wake us up. |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4899 | */ |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4900 | if (inflight || |
| 4901 | (!time_after(jiffies, timeout) && ret != -EBUSY)) { |
Jens Axboe | 9831a90 | 2019-09-19 09:48:55 -0600 | [diff] [blame] | 4902 | cond_resched(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4903 | continue; |
| 4904 | } |
| 4905 | |
| 4906 | /* |
| 4907 | * Drop cur_mm before scheduling, we can't hold it for |
| 4908 | * long periods (or over schedule()). Do this before |
| 4909 | * adding ourselves to the waitqueue, as the unuse/drop |
| 4910 | * may sleep. |
| 4911 | */ |
| 4912 | if (cur_mm) { |
| 4913 | unuse_mm(cur_mm); |
| 4914 | mmput(cur_mm); |
| 4915 | cur_mm = NULL; |
| 4916 | } |
| 4917 | |
| 4918 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 4919 | TASK_INTERRUPTIBLE); |
| 4920 | |
| 4921 | /* Tell userspace we may need a wakeup call */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4922 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
Stefan Bühler | 0d7bae6 | 2019-04-19 11:57:45 +0200 | [diff] [blame] | 4923 | /* make sure to read SQ tail after writing flags */ |
| 4924 | smp_mb(); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4925 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 4926 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 4927 | if (!to_submit || ret == -EBUSY) { |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4928 | if (kthread_should_park()) { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4929 | finish_wait(&ctx->sqo_wait, &wait); |
| 4930 | break; |
| 4931 | } |
| 4932 | if (signal_pending(current)) |
| 4933 | flush_signals(current); |
| 4934 | schedule(); |
| 4935 | finish_wait(&ctx->sqo_wait, &wait); |
| 4936 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4937 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4938 | continue; |
| 4939 | } |
| 4940 | finish_wait(&ctx->sqo_wait, &wait); |
| 4941 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 4942 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4943 | } |
| 4944 | |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 4945 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4946 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
Jens Axboe | 8a4955f | 2019-12-09 14:52:35 -0700 | [diff] [blame] | 4947 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4948 | if (ret > 0) |
| 4949 | inflight += ret; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4950 | } |
| 4951 | |
| 4952 | set_fs(old_fs); |
| 4953 | if (cur_mm) { |
| 4954 | unuse_mm(cur_mm); |
| 4955 | mmput(cur_mm); |
| 4956 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 4957 | revert_creds(old_cred); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 4958 | |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 4959 | kthread_parkme(); |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 4960 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 4961 | return 0; |
| 4962 | } |
| 4963 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4964 | struct io_wait_queue { |
| 4965 | struct wait_queue_entry wq; |
| 4966 | struct io_ring_ctx *ctx; |
| 4967 | unsigned to_wait; |
| 4968 | unsigned nr_timeouts; |
| 4969 | }; |
| 4970 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4971 | 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] | 4972 | { |
| 4973 | struct io_ring_ctx *ctx = iowq->ctx; |
| 4974 | |
| 4975 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 4976 | * 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] | 4977 | * started waiting. For timeouts, we always want to return to userspace, |
| 4978 | * regardless of event count. |
| 4979 | */ |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4980 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4981 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 4982 | } |
| 4983 | |
| 4984 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 4985 | int wake_flags, void *key) |
| 4986 | { |
| 4987 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 4988 | wq); |
| 4989 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 4990 | /* use noflush == true, as we can't safely rely on locking context */ |
| 4991 | if (!io_should_wake(iowq, true)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 4992 | return -1; |
| 4993 | |
| 4994 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 4995 | } |
| 4996 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4997 | /* |
| 4998 | * Wait until events become available, if we don't already have some. The |
| 4999 | * application must reap them itself, as they reside on the shared cq ring. |
| 5000 | */ |
| 5001 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 5002 | const sigset_t __user *sig, size_t sigsz) |
| 5003 | { |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5004 | struct io_wait_queue iowq = { |
| 5005 | .wq = { |
| 5006 | .private = current, |
| 5007 | .func = io_wake_function, |
| 5008 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 5009 | }, |
| 5010 | .ctx = ctx, |
| 5011 | .to_wait = min_events, |
| 5012 | }; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5013 | struct io_rings *rings = ctx->rings; |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5014 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5015 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5016 | if (io_cqring_events(ctx, false) >= min_events) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5017 | return 0; |
| 5018 | |
| 5019 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5020 | #ifdef CONFIG_COMPAT |
| 5021 | if (in_compat_syscall()) |
| 5022 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5023 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5024 | else |
| 5025 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 5026 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 5027 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5028 | if (ret) |
| 5029 | return ret; |
| 5030 | } |
| 5031 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5032 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 5033 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5034 | do { |
| 5035 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 5036 | TASK_INTERRUPTIBLE); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 5037 | if (io_should_wake(&iowq, false)) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5038 | break; |
| 5039 | schedule(); |
| 5040 | if (signal_pending(current)) { |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5041 | ret = -EINTR; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 5042 | break; |
| 5043 | } |
| 5044 | } while (1); |
| 5045 | finish_wait(&ctx->wait, &iowq.wq); |
| 5046 | |
Jackie Liu | e9ffa5c | 2019-10-29 11:16:42 +0800 | [diff] [blame] | 5047 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5048 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5049 | 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] | 5050 | } |
| 5051 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5052 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5053 | { |
| 5054 | #if defined(CONFIG_UNIX) |
| 5055 | if (ctx->ring_sock) { |
| 5056 | struct sock *sock = ctx->ring_sock->sk; |
| 5057 | struct sk_buff *skb; |
| 5058 | |
| 5059 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 5060 | kfree_skb(skb); |
| 5061 | } |
| 5062 | #else |
| 5063 | int i; |
| 5064 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5065 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5066 | struct file *file; |
| 5067 | |
| 5068 | file = io_file_from_index(ctx, i); |
| 5069 | if (file) |
| 5070 | fput(file); |
| 5071 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5072 | #endif |
| 5073 | } |
| 5074 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5075 | static void io_file_ref_kill(struct percpu_ref *ref) |
| 5076 | { |
| 5077 | struct fixed_file_data *data; |
| 5078 | |
| 5079 | data = container_of(ref, struct fixed_file_data, refs); |
| 5080 | complete(&data->done); |
| 5081 | } |
| 5082 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5083 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 5084 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5085 | struct fixed_file_data *data = ctx->file_data; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5086 | unsigned nr_tables, i; |
| 5087 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5088 | if (!data) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5089 | return -ENXIO; |
| 5090 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5091 | /* protect against inflight atomic switch, which drops the ref */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5092 | percpu_ref_get(&data->refs); |
Jens Axboe | e46a795 | 2020-01-17 11:15:34 -0700 | [diff] [blame^] | 5093 | /* wait for existing switches */ |
| 5094 | flush_work(&data->ref_work); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5095 | percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill); |
| 5096 | wait_for_completion(&data->done); |
| 5097 | percpu_ref_put(&data->refs); |
Jens Axboe | e46a795 | 2020-01-17 11:15:34 -0700 | [diff] [blame^] | 5098 | /* flush potential new switch */ |
| 5099 | flush_work(&data->ref_work); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5100 | percpu_ref_exit(&data->refs); |
| 5101 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5102 | __io_sqe_files_unregister(ctx); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5103 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
| 5104 | for (i = 0; i < nr_tables; i++) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5105 | kfree(data->table[i].files); |
| 5106 | kfree(data->table); |
| 5107 | kfree(data); |
| 5108 | ctx->file_data = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5109 | ctx->nr_user_files = 0; |
| 5110 | return 0; |
| 5111 | } |
| 5112 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5113 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 5114 | { |
| 5115 | if (ctx->sqo_thread) { |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 5116 | wait_for_completion(&ctx->completions[1]); |
Roman Penyaev | 2bbcd6d | 2019-05-16 10:53:57 +0200 | [diff] [blame] | 5117 | /* |
| 5118 | * The park is a bit of a work-around, without it we get |
| 5119 | * warning spews on shutdown with SQPOLL set and affinity |
| 5120 | * set to a single CPU. |
| 5121 | */ |
Jens Axboe | 0605863 | 2019-04-13 09:26:03 -0600 | [diff] [blame] | 5122 | kthread_park(ctx->sqo_thread); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5123 | kthread_stop(ctx->sqo_thread); |
| 5124 | ctx->sqo_thread = NULL; |
| 5125 | } |
| 5126 | } |
| 5127 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5128 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 5129 | { |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5130 | io_sq_thread_stop(ctx); |
| 5131 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5132 | if (ctx->io_wq) { |
| 5133 | io_wq_destroy(ctx->io_wq); |
| 5134 | ctx->io_wq = NULL; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5135 | } |
| 5136 | } |
| 5137 | |
| 5138 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5139 | /* |
| 5140 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 5141 | * the io_uring can be safely unregistered on process exit, even if we have |
| 5142 | * loops in the file referencing. |
| 5143 | */ |
| 5144 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 5145 | { |
| 5146 | struct sock *sk = ctx->ring_sock->sk; |
| 5147 | struct scm_fp_list *fpl; |
| 5148 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5149 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5150 | |
| 5151 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 5152 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 5153 | |
| 5154 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 5155 | return -EMFILE; |
| 5156 | } |
| 5157 | |
| 5158 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 5159 | if (!fpl) |
| 5160 | return -ENOMEM; |
| 5161 | |
| 5162 | skb = alloc_skb(0, GFP_KERNEL); |
| 5163 | if (!skb) { |
| 5164 | kfree(fpl); |
| 5165 | return -ENOMEM; |
| 5166 | } |
| 5167 | |
| 5168 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5169 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5170 | nr_files = 0; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5171 | fpl->user = get_uid(ctx->user); |
| 5172 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5173 | struct file *file = io_file_from_index(ctx, i + offset); |
| 5174 | |
| 5175 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5176 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5177 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5178 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 5179 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5180 | } |
| 5181 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5182 | if (nr_files) { |
| 5183 | fpl->max = SCM_MAX_FD; |
| 5184 | fpl->count = nr_files; |
| 5185 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5186 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5187 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 5188 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5189 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 5190 | for (i = 0; i < nr_files; i++) |
| 5191 | fput(fpl->fp[i]); |
| 5192 | } else { |
| 5193 | kfree_skb(skb); |
| 5194 | kfree(fpl); |
| 5195 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5196 | |
| 5197 | return 0; |
| 5198 | } |
| 5199 | |
| 5200 | /* |
| 5201 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 5202 | * causes regular reference counting to break down. We rely on the UNIX |
| 5203 | * garbage collection to take care of this problem for us. |
| 5204 | */ |
| 5205 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5206 | { |
| 5207 | unsigned left, total; |
| 5208 | int ret = 0; |
| 5209 | |
| 5210 | total = 0; |
| 5211 | left = ctx->nr_user_files; |
| 5212 | while (left) { |
| 5213 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5214 | |
| 5215 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 5216 | if (ret) |
| 5217 | break; |
| 5218 | left -= this_files; |
| 5219 | total += this_files; |
| 5220 | } |
| 5221 | |
| 5222 | if (!ret) |
| 5223 | return 0; |
| 5224 | |
| 5225 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5226 | struct file *file = io_file_from_index(ctx, total); |
| 5227 | |
| 5228 | if (file) |
| 5229 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 5230 | total++; |
| 5231 | } |
| 5232 | |
| 5233 | return ret; |
| 5234 | } |
| 5235 | #else |
| 5236 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 5237 | { |
| 5238 | return 0; |
| 5239 | } |
| 5240 | #endif |
| 5241 | |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5242 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
| 5243 | unsigned nr_files) |
| 5244 | { |
| 5245 | int i; |
| 5246 | |
| 5247 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5248 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5249 | unsigned this_files; |
| 5250 | |
| 5251 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); |
| 5252 | table->files = kcalloc(this_files, sizeof(struct file *), |
| 5253 | GFP_KERNEL); |
| 5254 | if (!table->files) |
| 5255 | break; |
| 5256 | nr_files -= this_files; |
| 5257 | } |
| 5258 | |
| 5259 | if (i == nr_tables) |
| 5260 | return 0; |
| 5261 | |
| 5262 | for (i = 0; i < nr_tables; i++) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5263 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5264 | kfree(table->files); |
| 5265 | } |
| 5266 | return 1; |
| 5267 | } |
| 5268 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5269 | 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] | 5270 | { |
| 5271 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5272 | struct sock *sock = ctx->ring_sock->sk; |
| 5273 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 5274 | struct sk_buff *skb; |
| 5275 | int i; |
| 5276 | |
| 5277 | __skb_queue_head_init(&list); |
| 5278 | |
| 5279 | /* |
| 5280 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 5281 | * remove this entry and rearrange the file array. |
| 5282 | */ |
| 5283 | skb = skb_dequeue(head); |
| 5284 | while (skb) { |
| 5285 | struct scm_fp_list *fp; |
| 5286 | |
| 5287 | fp = UNIXCB(skb).fp; |
| 5288 | for (i = 0; i < fp->count; i++) { |
| 5289 | int left; |
| 5290 | |
| 5291 | if (fp->fp[i] != file) |
| 5292 | continue; |
| 5293 | |
| 5294 | unix_notinflight(fp->user, fp->fp[i]); |
| 5295 | left = fp->count - 1 - i; |
| 5296 | if (left) { |
| 5297 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 5298 | left * sizeof(struct file *)); |
| 5299 | } |
| 5300 | fp->count--; |
| 5301 | if (!fp->count) { |
| 5302 | kfree_skb(skb); |
| 5303 | skb = NULL; |
| 5304 | } else { |
| 5305 | __skb_queue_tail(&list, skb); |
| 5306 | } |
| 5307 | fput(file); |
| 5308 | file = NULL; |
| 5309 | break; |
| 5310 | } |
| 5311 | |
| 5312 | if (!file) |
| 5313 | break; |
| 5314 | |
| 5315 | __skb_queue_tail(&list, skb); |
| 5316 | |
| 5317 | skb = skb_dequeue(head); |
| 5318 | } |
| 5319 | |
| 5320 | if (skb_peek(&list)) { |
| 5321 | spin_lock_irq(&head->lock); |
| 5322 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 5323 | __skb_queue_tail(head, skb); |
| 5324 | spin_unlock_irq(&head->lock); |
| 5325 | } |
| 5326 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5327 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5328 | #endif |
| 5329 | } |
| 5330 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5331 | struct io_file_put { |
| 5332 | struct llist_node llist; |
| 5333 | struct file *file; |
| 5334 | struct completion *done; |
| 5335 | }; |
| 5336 | |
| 5337 | static void io_ring_file_ref_switch(struct work_struct *work) |
| 5338 | { |
| 5339 | struct io_file_put *pfile, *tmp; |
| 5340 | struct fixed_file_data *data; |
| 5341 | struct llist_node *node; |
| 5342 | |
| 5343 | data = container_of(work, struct fixed_file_data, ref_work); |
| 5344 | |
| 5345 | while ((node = llist_del_all(&data->put_llist)) != NULL) { |
| 5346 | llist_for_each_entry_safe(pfile, tmp, node, llist) { |
| 5347 | io_ring_file_put(data->ctx, pfile->file); |
| 5348 | if (pfile->done) |
| 5349 | complete(pfile->done); |
| 5350 | else |
| 5351 | kfree(pfile); |
| 5352 | } |
| 5353 | } |
| 5354 | |
| 5355 | percpu_ref_get(&data->refs); |
| 5356 | percpu_ref_switch_to_percpu(&data->refs); |
| 5357 | } |
| 5358 | |
| 5359 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
| 5360 | { |
| 5361 | struct fixed_file_data *data; |
| 5362 | |
| 5363 | data = container_of(ref, struct fixed_file_data, refs); |
| 5364 | |
| 5365 | /* we can't safely switch from inside this context, punt to wq */ |
| 5366 | queue_work(system_wq, &data->ref_work); |
| 5367 | } |
| 5368 | |
| 5369 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5370 | unsigned nr_args) |
| 5371 | { |
| 5372 | __s32 __user *fds = (__s32 __user *) arg; |
| 5373 | unsigned nr_tables; |
| 5374 | struct file *file; |
| 5375 | int fd, ret = 0; |
| 5376 | unsigned i; |
| 5377 | |
| 5378 | if (ctx->file_data) |
| 5379 | return -EBUSY; |
| 5380 | if (!nr_args) |
| 5381 | return -EINVAL; |
| 5382 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 5383 | return -EMFILE; |
| 5384 | |
| 5385 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
| 5386 | if (!ctx->file_data) |
| 5387 | return -ENOMEM; |
| 5388 | ctx->file_data->ctx = ctx; |
| 5389 | init_completion(&ctx->file_data->done); |
| 5390 | |
| 5391 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
| 5392 | ctx->file_data->table = kcalloc(nr_tables, |
| 5393 | sizeof(struct fixed_file_table), |
| 5394 | GFP_KERNEL); |
| 5395 | if (!ctx->file_data->table) { |
| 5396 | kfree(ctx->file_data); |
| 5397 | ctx->file_data = NULL; |
| 5398 | return -ENOMEM; |
| 5399 | } |
| 5400 | |
| 5401 | if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero, |
| 5402 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 5403 | kfree(ctx->file_data->table); |
| 5404 | kfree(ctx->file_data); |
| 5405 | ctx->file_data = NULL; |
| 5406 | return -ENOMEM; |
| 5407 | } |
| 5408 | ctx->file_data->put_llist.first = NULL; |
| 5409 | INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch); |
| 5410 | |
| 5411 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
| 5412 | percpu_ref_exit(&ctx->file_data->refs); |
| 5413 | kfree(ctx->file_data->table); |
| 5414 | kfree(ctx->file_data); |
| 5415 | ctx->file_data = NULL; |
| 5416 | return -ENOMEM; |
| 5417 | } |
| 5418 | |
| 5419 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
| 5420 | struct fixed_file_table *table; |
| 5421 | unsigned index; |
| 5422 | |
| 5423 | ret = -EFAULT; |
| 5424 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 5425 | break; |
| 5426 | /* allow sparse sets */ |
| 5427 | if (fd == -1) { |
| 5428 | ret = 0; |
| 5429 | continue; |
| 5430 | } |
| 5431 | |
| 5432 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
| 5433 | index = i & IORING_FILE_TABLE_MASK; |
| 5434 | file = fget(fd); |
| 5435 | |
| 5436 | ret = -EBADF; |
| 5437 | if (!file) |
| 5438 | break; |
| 5439 | |
| 5440 | /* |
| 5441 | * Don't allow io_uring instances to be registered. If UNIX |
| 5442 | * isn't enabled, then this causes a reference cycle and this |
| 5443 | * instance can never get freed. If UNIX is enabled we'll |
| 5444 | * handle it just fine, but there's still no point in allowing |
| 5445 | * a ring fd as it doesn't support regular read/write anyway. |
| 5446 | */ |
| 5447 | if (file->f_op == &io_uring_fops) { |
| 5448 | fput(file); |
| 5449 | break; |
| 5450 | } |
| 5451 | ret = 0; |
| 5452 | table->files[index] = file; |
| 5453 | } |
| 5454 | |
| 5455 | if (ret) { |
| 5456 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 5457 | file = io_file_from_index(ctx, i); |
| 5458 | if (file) |
| 5459 | fput(file); |
| 5460 | } |
| 5461 | for (i = 0; i < nr_tables; i++) |
| 5462 | kfree(ctx->file_data->table[i].files); |
| 5463 | |
| 5464 | kfree(ctx->file_data->table); |
| 5465 | kfree(ctx->file_data); |
| 5466 | ctx->file_data = NULL; |
| 5467 | ctx->nr_user_files = 0; |
| 5468 | return ret; |
| 5469 | } |
| 5470 | |
| 5471 | ret = io_sqe_files_scm(ctx); |
| 5472 | if (ret) |
| 5473 | io_sqe_files_unregister(ctx); |
| 5474 | |
| 5475 | return ret; |
| 5476 | } |
| 5477 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5478 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 5479 | int index) |
| 5480 | { |
| 5481 | #if defined(CONFIG_UNIX) |
| 5482 | struct sock *sock = ctx->ring_sock->sk; |
| 5483 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 5484 | struct sk_buff *skb; |
| 5485 | |
| 5486 | /* |
| 5487 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 5488 | * file set. If there's no room, fall back to allocating a new skb |
| 5489 | * and filling it in. |
| 5490 | */ |
| 5491 | spin_lock_irq(&head->lock); |
| 5492 | skb = skb_peek(head); |
| 5493 | if (skb) { |
| 5494 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 5495 | |
| 5496 | if (fpl->count < SCM_MAX_FD) { |
| 5497 | __skb_unlink(skb, head); |
| 5498 | spin_unlock_irq(&head->lock); |
| 5499 | fpl->fp[fpl->count] = get_file(file); |
| 5500 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 5501 | fpl->count++; |
| 5502 | spin_lock_irq(&head->lock); |
| 5503 | __skb_queue_head(head, skb); |
| 5504 | } else { |
| 5505 | skb = NULL; |
| 5506 | } |
| 5507 | } |
| 5508 | spin_unlock_irq(&head->lock); |
| 5509 | |
| 5510 | if (skb) { |
| 5511 | fput(file); |
| 5512 | return 0; |
| 5513 | } |
| 5514 | |
| 5515 | return __io_sqe_files_scm(ctx, 1, index); |
| 5516 | #else |
| 5517 | return 0; |
| 5518 | #endif |
| 5519 | } |
| 5520 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5521 | static void io_atomic_switch(struct percpu_ref *ref) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5522 | { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5523 | struct fixed_file_data *data; |
| 5524 | |
| 5525 | data = container_of(ref, struct fixed_file_data, refs); |
| 5526 | clear_bit(FFD_F_ATOMIC, &data->state); |
| 5527 | } |
| 5528 | |
| 5529 | static bool io_queue_file_removal(struct fixed_file_data *data, |
| 5530 | struct file *file) |
| 5531 | { |
| 5532 | struct io_file_put *pfile, pfile_stack; |
| 5533 | DECLARE_COMPLETION_ONSTACK(done); |
| 5534 | |
| 5535 | /* |
| 5536 | * If we fail allocating the struct we need for doing async reomval |
| 5537 | * of this file, just punt to sync and wait for it. |
| 5538 | */ |
| 5539 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); |
| 5540 | if (!pfile) { |
| 5541 | pfile = &pfile_stack; |
| 5542 | pfile->done = &done; |
| 5543 | } |
| 5544 | |
| 5545 | pfile->file = file; |
| 5546 | llist_add(&pfile->llist, &data->put_llist); |
| 5547 | |
| 5548 | if (pfile == &pfile_stack) { |
| 5549 | if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5550 | percpu_ref_put(&data->refs); |
| 5551 | percpu_ref_switch_to_atomic(&data->refs, |
| 5552 | io_atomic_switch); |
| 5553 | } |
| 5554 | wait_for_completion(&done); |
| 5555 | flush_work(&data->ref_work); |
| 5556 | return false; |
| 5557 | } |
| 5558 | |
| 5559 | return true; |
| 5560 | } |
| 5561 | |
| 5562 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| 5563 | struct io_uring_files_update *up, |
| 5564 | unsigned nr_args) |
| 5565 | { |
| 5566 | struct fixed_file_data *data = ctx->file_data; |
| 5567 | bool ref_switch = false; |
| 5568 | struct file *file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5569 | __s32 __user *fds; |
| 5570 | int fd, i, err; |
| 5571 | __u32 done; |
| 5572 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5573 | if (check_add_overflow(up->offset, nr_args, &done)) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5574 | return -EOVERFLOW; |
| 5575 | if (done > ctx->nr_user_files) |
| 5576 | return -EINVAL; |
| 5577 | |
| 5578 | done = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5579 | fds = u64_to_user_ptr(up->fds); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5580 | while (nr_args) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5581 | struct fixed_file_table *table; |
| 5582 | unsigned index; |
| 5583 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5584 | err = 0; |
| 5585 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { |
| 5586 | err = -EFAULT; |
| 5587 | break; |
| 5588 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5589 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
| 5590 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5591 | index = i & IORING_FILE_TABLE_MASK; |
| 5592 | if (table->files[index]) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5593 | file = io_file_from_index(ctx, index); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5594 | table->files[index] = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5595 | if (io_queue_file_removal(data, file)) |
| 5596 | ref_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5597 | } |
| 5598 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5599 | file = fget(fd); |
| 5600 | if (!file) { |
| 5601 | err = -EBADF; |
| 5602 | break; |
| 5603 | } |
| 5604 | /* |
| 5605 | * Don't allow io_uring instances to be registered. If |
| 5606 | * UNIX isn't enabled, then this causes a reference |
| 5607 | * cycle and this instance can never get freed. If UNIX |
| 5608 | * is enabled we'll handle it just fine, but there's |
| 5609 | * still no point in allowing a ring fd as it doesn't |
| 5610 | * support regular read/write anyway. |
| 5611 | */ |
| 5612 | if (file->f_op == &io_uring_fops) { |
| 5613 | fput(file); |
| 5614 | err = -EBADF; |
| 5615 | break; |
| 5616 | } |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 5617 | table->files[index] = file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5618 | err = io_sqe_file_register(ctx, file, i); |
| 5619 | if (err) |
| 5620 | break; |
| 5621 | } |
| 5622 | nr_args--; |
| 5623 | done++; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5624 | up->offset++; |
| 5625 | } |
| 5626 | |
| 5627 | if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) { |
| 5628 | percpu_ref_put(&data->refs); |
| 5629 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5630 | } |
| 5631 | |
| 5632 | return done ? done : err; |
| 5633 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 5634 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 5635 | unsigned nr_args) |
| 5636 | { |
| 5637 | struct io_uring_files_update up; |
| 5638 | |
| 5639 | if (!ctx->file_data) |
| 5640 | return -ENXIO; |
| 5641 | if (!nr_args) |
| 5642 | return -EINVAL; |
| 5643 | if (copy_from_user(&up, arg, sizeof(up))) |
| 5644 | return -EFAULT; |
| 5645 | if (up.resv) |
| 5646 | return -EINVAL; |
| 5647 | |
| 5648 | return __io_sqe_files_update(ctx, &up, nr_args); |
| 5649 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 5650 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 5651 | static void io_put_work(struct io_wq_work *work) |
| 5652 | { |
| 5653 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5654 | |
| 5655 | io_put_req(req); |
| 5656 | } |
| 5657 | |
| 5658 | static void io_get_work(struct io_wq_work *work) |
| 5659 | { |
| 5660 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 5661 | |
| 5662 | refcount_inc(&req->refs); |
| 5663 | } |
| 5664 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5665 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 5666 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5667 | { |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5668 | struct io_wq_data data; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5669 | unsigned concurrency; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5670 | int ret; |
| 5671 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5672 | init_waitqueue_head(&ctx->sqo_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5673 | mmgrab(current->mm); |
| 5674 | ctx->sqo_mm = current->mm; |
| 5675 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5676 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 3ec482d | 2019-04-08 10:51:01 -0600 | [diff] [blame] | 5677 | ret = -EPERM; |
| 5678 | if (!capable(CAP_SYS_ADMIN)) |
| 5679 | goto err; |
| 5680 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5681 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 5682 | if (!ctx->sq_thread_idle) |
| 5683 | ctx->sq_thread_idle = HZ; |
| 5684 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5685 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5686 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5687 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5688 | ret = -EINVAL; |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 5689 | if (cpu >= nr_cpu_ids) |
| 5690 | goto err; |
Shenghui Wang | 7889f44 | 2019-05-07 16:03:19 +0800 | [diff] [blame] | 5691 | if (!cpu_online(cpu)) |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 5692 | goto err; |
| 5693 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 5694 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 5695 | ctx, cpu, |
| 5696 | "io_uring-sq"); |
| 5697 | } else { |
| 5698 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 5699 | "io_uring-sq"); |
| 5700 | } |
| 5701 | if (IS_ERR(ctx->sqo_thread)) { |
| 5702 | ret = PTR_ERR(ctx->sqo_thread); |
| 5703 | ctx->sqo_thread = NULL; |
| 5704 | goto err; |
| 5705 | } |
| 5706 | wake_up_process(ctx->sqo_thread); |
| 5707 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 5708 | /* Can't have SQ_AFF without SQPOLL */ |
| 5709 | ret = -EINVAL; |
| 5710 | goto err; |
| 5711 | } |
| 5712 | |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5713 | data.mm = ctx->sqo_mm; |
| 5714 | data.user = ctx->user; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 5715 | data.creds = ctx->creds; |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5716 | data.get_work = io_get_work; |
| 5717 | data.put_work = io_put_work; |
| 5718 | |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 5719 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 5720 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 5721 | ctx->io_wq = io_wq_create(concurrency, &data); |
Jens Axboe | 975c99a5 | 2019-10-30 08:42:56 -0600 | [diff] [blame] | 5722 | if (IS_ERR(ctx->io_wq)) { |
| 5723 | ret = PTR_ERR(ctx->io_wq); |
| 5724 | ctx->io_wq = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5725 | goto err; |
| 5726 | } |
| 5727 | |
| 5728 | return 0; |
| 5729 | err: |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 5730 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5731 | mmdrop(ctx->sqo_mm); |
| 5732 | ctx->sqo_mm = NULL; |
| 5733 | return ret; |
| 5734 | } |
| 5735 | |
| 5736 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 5737 | { |
| 5738 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 5739 | } |
| 5740 | |
| 5741 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 5742 | { |
| 5743 | unsigned long page_limit, cur_pages, new_pages; |
| 5744 | |
| 5745 | /* Don't allow more pages than we can safely lock */ |
| 5746 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 5747 | |
| 5748 | do { |
| 5749 | cur_pages = atomic_long_read(&user->locked_vm); |
| 5750 | new_pages = cur_pages + nr_pages; |
| 5751 | if (new_pages > page_limit) |
| 5752 | return -ENOMEM; |
| 5753 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 5754 | new_pages) != cur_pages); |
| 5755 | |
| 5756 | return 0; |
| 5757 | } |
| 5758 | |
| 5759 | static void io_mem_free(void *ptr) |
| 5760 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 5761 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5762 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 5763 | if (!ptr) |
| 5764 | return; |
| 5765 | |
| 5766 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5767 | if (put_page_testzero(page)) |
| 5768 | free_compound_page(page); |
| 5769 | } |
| 5770 | |
| 5771 | static void *io_mem_alloc(size_t size) |
| 5772 | { |
| 5773 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 5774 | __GFP_NORETRY; |
| 5775 | |
| 5776 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 5777 | } |
| 5778 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5779 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 5780 | size_t *sq_offset) |
| 5781 | { |
| 5782 | struct io_rings *rings; |
| 5783 | size_t off, sq_array_size; |
| 5784 | |
| 5785 | off = struct_size(rings, cqes, cq_entries); |
| 5786 | if (off == SIZE_MAX) |
| 5787 | return SIZE_MAX; |
| 5788 | |
| 5789 | #ifdef CONFIG_SMP |
| 5790 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 5791 | if (off == 0) |
| 5792 | return SIZE_MAX; |
| 5793 | #endif |
| 5794 | |
| 5795 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 5796 | if (sq_array_size == SIZE_MAX) |
| 5797 | return SIZE_MAX; |
| 5798 | |
| 5799 | if (check_add_overflow(off, sq_array_size, &off)) |
| 5800 | return SIZE_MAX; |
| 5801 | |
| 5802 | if (sq_offset) |
| 5803 | *sq_offset = off; |
| 5804 | |
| 5805 | return off; |
| 5806 | } |
| 5807 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5808 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 5809 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5810 | size_t pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5811 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5812 | pages = (size_t)1 << get_order( |
| 5813 | rings_size(sq_entries, cq_entries, NULL)); |
| 5814 | pages += (size_t)1 << get_order( |
| 5815 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5816 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 5817 | return pages; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5818 | } |
| 5819 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5820 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 5821 | { |
| 5822 | int i, j; |
| 5823 | |
| 5824 | if (!ctx->user_bufs) |
| 5825 | return -ENXIO; |
| 5826 | |
| 5827 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 5828 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 5829 | |
| 5830 | for (j = 0; j < imu->nr_bvecs; j++) |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 5831 | put_user_page(imu->bvec[j].bv_page); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5832 | |
| 5833 | if (ctx->account_mem) |
| 5834 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5835 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5836 | imu->nr_bvecs = 0; |
| 5837 | } |
| 5838 | |
| 5839 | kfree(ctx->user_bufs); |
| 5840 | ctx->user_bufs = NULL; |
| 5841 | ctx->nr_user_bufs = 0; |
| 5842 | return 0; |
| 5843 | } |
| 5844 | |
| 5845 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 5846 | void __user *arg, unsigned index) |
| 5847 | { |
| 5848 | struct iovec __user *src; |
| 5849 | |
| 5850 | #ifdef CONFIG_COMPAT |
| 5851 | if (ctx->compat) { |
| 5852 | struct compat_iovec __user *ciovs; |
| 5853 | struct compat_iovec ciov; |
| 5854 | |
| 5855 | ciovs = (struct compat_iovec __user *) arg; |
| 5856 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 5857 | return -EFAULT; |
| 5858 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 5859 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5860 | dst->iov_len = ciov.iov_len; |
| 5861 | return 0; |
| 5862 | } |
| 5863 | #endif |
| 5864 | src = (struct iovec __user *) arg; |
| 5865 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 5866 | return -EFAULT; |
| 5867 | return 0; |
| 5868 | } |
| 5869 | |
| 5870 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 5871 | unsigned nr_args) |
| 5872 | { |
| 5873 | struct vm_area_struct **vmas = NULL; |
| 5874 | struct page **pages = NULL; |
| 5875 | int i, j, got_pages = 0; |
| 5876 | int ret = -EINVAL; |
| 5877 | |
| 5878 | if (ctx->user_bufs) |
| 5879 | return -EBUSY; |
| 5880 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 5881 | return -EINVAL; |
| 5882 | |
| 5883 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 5884 | GFP_KERNEL); |
| 5885 | if (!ctx->user_bufs) |
| 5886 | return -ENOMEM; |
| 5887 | |
| 5888 | for (i = 0; i < nr_args; i++) { |
| 5889 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 5890 | unsigned long off, start, end, ubuf; |
| 5891 | int pret, nr_pages; |
| 5892 | struct iovec iov; |
| 5893 | size_t size; |
| 5894 | |
| 5895 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 5896 | if (ret) |
Pavel Begunkov | a278682 | 2019-05-26 12:35:47 +0300 | [diff] [blame] | 5897 | goto err; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5898 | |
| 5899 | /* |
| 5900 | * Don't impose further limits on the size and buffer |
| 5901 | * constraints here, we'll -EINVAL later when IO is |
| 5902 | * submitted if they are wrong. |
| 5903 | */ |
| 5904 | ret = -EFAULT; |
| 5905 | if (!iov.iov_base || !iov.iov_len) |
| 5906 | goto err; |
| 5907 | |
| 5908 | /* arbitrary limit, but we need something */ |
| 5909 | if (iov.iov_len > SZ_1G) |
| 5910 | goto err; |
| 5911 | |
| 5912 | ubuf = (unsigned long) iov.iov_base; |
| 5913 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 5914 | start = ubuf >> PAGE_SHIFT; |
| 5915 | nr_pages = end - start; |
| 5916 | |
| 5917 | if (ctx->account_mem) { |
| 5918 | ret = io_account_mem(ctx->user, nr_pages); |
| 5919 | if (ret) |
| 5920 | goto err; |
| 5921 | } |
| 5922 | |
| 5923 | ret = 0; |
| 5924 | if (!pages || nr_pages > got_pages) { |
| 5925 | kfree(vmas); |
| 5926 | kfree(pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5927 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5928 | GFP_KERNEL); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5929 | vmas = kvmalloc_array(nr_pages, |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5930 | sizeof(struct vm_area_struct *), |
| 5931 | GFP_KERNEL); |
| 5932 | if (!pages || !vmas) { |
| 5933 | ret = -ENOMEM; |
| 5934 | if (ctx->account_mem) |
| 5935 | io_unaccount_mem(ctx->user, nr_pages); |
| 5936 | goto err; |
| 5937 | } |
| 5938 | got_pages = nr_pages; |
| 5939 | } |
| 5940 | |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5941 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5942 | GFP_KERNEL); |
| 5943 | ret = -ENOMEM; |
| 5944 | if (!imu->bvec) { |
| 5945 | if (ctx->account_mem) |
| 5946 | io_unaccount_mem(ctx->user, nr_pages); |
| 5947 | goto err; |
| 5948 | } |
| 5949 | |
| 5950 | ret = 0; |
| 5951 | down_read(¤t->mm->mmap_sem); |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 5952 | pret = get_user_pages(ubuf, nr_pages, |
| 5953 | FOLL_WRITE | FOLL_LONGTERM, |
| 5954 | pages, vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5955 | if (pret == nr_pages) { |
| 5956 | /* don't support file backed memory */ |
| 5957 | for (j = 0; j < nr_pages; j++) { |
| 5958 | struct vm_area_struct *vma = vmas[j]; |
| 5959 | |
| 5960 | if (vma->vm_file && |
| 5961 | !is_file_hugepages(vma->vm_file)) { |
| 5962 | ret = -EOPNOTSUPP; |
| 5963 | break; |
| 5964 | } |
| 5965 | } |
| 5966 | } else { |
| 5967 | ret = pret < 0 ? pret : -EFAULT; |
| 5968 | } |
| 5969 | up_read(¤t->mm->mmap_sem); |
| 5970 | if (ret) { |
| 5971 | /* |
| 5972 | * if we did partial map, or found file backed vmas, |
| 5973 | * release any pages we did get |
| 5974 | */ |
John Hubbard | 27c4d3a | 2019-08-04 19:32:06 -0700 | [diff] [blame] | 5975 | if (pret > 0) |
| 5976 | put_user_pages(pages, pret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5977 | if (ctx->account_mem) |
| 5978 | io_unaccount_mem(ctx->user, nr_pages); |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 5979 | kvfree(imu->bvec); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 5980 | goto err; |
| 5981 | } |
| 5982 | |
| 5983 | off = ubuf & ~PAGE_MASK; |
| 5984 | size = iov.iov_len; |
| 5985 | for (j = 0; j < nr_pages; j++) { |
| 5986 | size_t vec_len; |
| 5987 | |
| 5988 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 5989 | imu->bvec[j].bv_page = pages[j]; |
| 5990 | imu->bvec[j].bv_len = vec_len; |
| 5991 | imu->bvec[j].bv_offset = off; |
| 5992 | off = 0; |
| 5993 | size -= vec_len; |
| 5994 | } |
| 5995 | /* store original address for later verification */ |
| 5996 | imu->ubuf = ubuf; |
| 5997 | imu->len = iov.iov_len; |
| 5998 | imu->nr_bvecs = nr_pages; |
| 5999 | |
| 6000 | ctx->nr_user_bufs++; |
| 6001 | } |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6002 | kvfree(pages); |
| 6003 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6004 | return 0; |
| 6005 | err: |
Mark Rutland | d4ef647 | 2019-05-01 16:59:16 +0100 | [diff] [blame] | 6006 | kvfree(pages); |
| 6007 | kvfree(vmas); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6008 | io_sqe_buffer_unregister(ctx); |
| 6009 | return ret; |
| 6010 | } |
| 6011 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6012 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 6013 | { |
| 6014 | __s32 __user *fds = arg; |
| 6015 | int fd; |
| 6016 | |
| 6017 | if (ctx->cq_ev_fd) |
| 6018 | return -EBUSY; |
| 6019 | |
| 6020 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 6021 | return -EFAULT; |
| 6022 | |
| 6023 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 6024 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 6025 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 6026 | ctx->cq_ev_fd = NULL; |
| 6027 | return ret; |
| 6028 | } |
| 6029 | |
| 6030 | return 0; |
| 6031 | } |
| 6032 | |
| 6033 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 6034 | { |
| 6035 | if (ctx->cq_ev_fd) { |
| 6036 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 6037 | ctx->cq_ev_fd = NULL; |
| 6038 | return 0; |
| 6039 | } |
| 6040 | |
| 6041 | return -ENXIO; |
| 6042 | } |
| 6043 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6044 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 6045 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6046 | io_finish_async(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6047 | if (ctx->sqo_mm) |
| 6048 | mmdrop(ctx->sqo_mm); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6049 | |
| 6050 | io_iopoll_reap_events(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6051 | io_sqe_buffer_unregister(ctx); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6052 | io_sqe_files_unregister(ctx); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6053 | io_eventfd_unregister(ctx); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6054 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6055 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6056 | if (ctx->ring_sock) { |
| 6057 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6058 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 6059 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6060 | #endif |
| 6061 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6062 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6063 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6064 | |
| 6065 | percpu_ref_exit(&ctx->refs); |
| 6066 | if (ctx->account_mem) |
| 6067 | io_unaccount_mem(ctx->user, |
| 6068 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 6069 | free_uid(ctx->user); |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 6070 | put_cred(ctx->creds); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6071 | kfree(ctx->completions); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 6072 | kfree(ctx->cancel_hash); |
Jens Axboe | 0ddf92e | 2019-11-08 08:52:53 -0700 | [diff] [blame] | 6073 | kmem_cache_free(req_cachep, ctx->fallback_req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6074 | kfree(ctx); |
| 6075 | } |
| 6076 | |
| 6077 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 6078 | { |
| 6079 | struct io_ring_ctx *ctx = file->private_data; |
| 6080 | __poll_t mask = 0; |
| 6081 | |
| 6082 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 6083 | /* |
| 6084 | * synchronizes with barrier from wq_has_sleeper call in |
| 6085 | * io_commit_cqring |
| 6086 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6087 | smp_rmb(); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6088 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 6089 | ctx->rings->sq_ring_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6090 | mask |= EPOLLOUT | EPOLLWRNORM; |
yangerkun | daa5de5 | 2019-09-24 20:53:34 +0800 | [diff] [blame] | 6091 | if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6092 | mask |= EPOLLIN | EPOLLRDNORM; |
| 6093 | |
| 6094 | return mask; |
| 6095 | } |
| 6096 | |
| 6097 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 6098 | { |
| 6099 | struct io_ring_ctx *ctx = file->private_data; |
| 6100 | |
| 6101 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 6102 | } |
| 6103 | |
| 6104 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 6105 | { |
| 6106 | mutex_lock(&ctx->uring_lock); |
| 6107 | percpu_ref_kill(&ctx->refs); |
| 6108 | mutex_unlock(&ctx->uring_lock); |
| 6109 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6110 | io_kill_timeouts(ctx); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 6111 | io_poll_remove_all(ctx); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6112 | |
| 6113 | if (ctx->io_wq) |
| 6114 | io_wq_cancel_all(ctx->io_wq); |
| 6115 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6116 | io_iopoll_reap_events(ctx); |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 6117 | /* if we failed setting up the ctx, we might not have any rings */ |
| 6118 | if (ctx->rings) |
| 6119 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 6120 | wait_for_completion(&ctx->completions[0]); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6121 | io_ring_ctx_free(ctx); |
| 6122 | } |
| 6123 | |
| 6124 | static int io_uring_release(struct inode *inode, struct file *file) |
| 6125 | { |
| 6126 | struct io_ring_ctx *ctx = file->private_data; |
| 6127 | |
| 6128 | file->private_data = NULL; |
| 6129 | io_ring_ctx_wait_and_kill(ctx); |
| 6130 | return 0; |
| 6131 | } |
| 6132 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6133 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
| 6134 | struct files_struct *files) |
| 6135 | { |
| 6136 | struct io_kiocb *req; |
| 6137 | DEFINE_WAIT(wait); |
| 6138 | |
| 6139 | while (!list_empty_careful(&ctx->inflight_list)) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6140 | struct io_kiocb *cancel_req = NULL; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6141 | |
| 6142 | spin_lock_irq(&ctx->inflight_lock); |
| 6143 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6144 | if (req->work.files != files) |
| 6145 | continue; |
| 6146 | /* req is being completed, ignore */ |
| 6147 | if (!refcount_inc_not_zero(&req->refs)) |
| 6148 | continue; |
| 6149 | cancel_req = req; |
| 6150 | break; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6151 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6152 | if (cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6153 | prepare_to_wait(&ctx->inflight_wait, &wait, |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6154 | TASK_UNINTERRUPTIBLE); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6155 | spin_unlock_irq(&ctx->inflight_lock); |
| 6156 | |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6157 | /* We need to keep going until we don't find a matching req */ |
| 6158 | if (!cancel_req) |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6159 | break; |
Bob Liu | 2f6d9b9 | 2019-11-13 18:06:24 +0800 | [diff] [blame] | 6160 | |
| 6161 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); |
| 6162 | io_put_req(cancel_req); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6163 | schedule(); |
| 6164 | } |
Jens Axboe | 768134d | 2019-11-10 20:30:53 -0700 | [diff] [blame] | 6165 | finish_wait(&ctx->inflight_wait, &wait); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6166 | } |
| 6167 | |
| 6168 | static int io_uring_flush(struct file *file, void *data) |
| 6169 | { |
| 6170 | struct io_ring_ctx *ctx = file->private_data; |
| 6171 | |
| 6172 | io_uring_cancel_files(ctx, data); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6173 | if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) { |
| 6174 | io_cqring_overflow_flush(ctx, true); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6175 | io_wq_cancel_all(ctx->io_wq); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 6176 | } |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6177 | return 0; |
| 6178 | } |
| 6179 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6180 | static void *io_uring_validate_mmap_request(struct file *file, |
| 6181 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6182 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6183 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6184 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6185 | struct page *page; |
| 6186 | void *ptr; |
| 6187 | |
| 6188 | switch (offset) { |
| 6189 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6190 | case IORING_OFF_CQ_RING: |
| 6191 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6192 | break; |
| 6193 | case IORING_OFF_SQES: |
| 6194 | ptr = ctx->sq_sqes; |
| 6195 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6196 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6197 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6198 | } |
| 6199 | |
| 6200 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 6201 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6202 | return ERR_PTR(-EINVAL); |
| 6203 | |
| 6204 | return ptr; |
| 6205 | } |
| 6206 | |
| 6207 | #ifdef CONFIG_MMU |
| 6208 | |
| 6209 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6210 | { |
| 6211 | size_t sz = vma->vm_end - vma->vm_start; |
| 6212 | unsigned long pfn; |
| 6213 | void *ptr; |
| 6214 | |
| 6215 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 6216 | if (IS_ERR(ptr)) |
| 6217 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6218 | |
| 6219 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 6220 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 6221 | } |
| 6222 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6223 | #else /* !CONFIG_MMU */ |
| 6224 | |
| 6225 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 6226 | { |
| 6227 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 6228 | } |
| 6229 | |
| 6230 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 6231 | { |
| 6232 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 6233 | } |
| 6234 | |
| 6235 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 6236 | unsigned long addr, unsigned long len, |
| 6237 | unsigned long pgoff, unsigned long flags) |
| 6238 | { |
| 6239 | void *ptr; |
| 6240 | |
| 6241 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 6242 | if (IS_ERR(ptr)) |
| 6243 | return PTR_ERR(ptr); |
| 6244 | |
| 6245 | return (unsigned long) ptr; |
| 6246 | } |
| 6247 | |
| 6248 | #endif /* !CONFIG_MMU */ |
| 6249 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6250 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 6251 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 6252 | size_t, sigsz) |
| 6253 | { |
| 6254 | struct io_ring_ctx *ctx; |
| 6255 | long ret = -EBADF; |
| 6256 | int submitted = 0; |
| 6257 | struct fd f; |
| 6258 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6259 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6260 | return -EINVAL; |
| 6261 | |
| 6262 | f = fdget(fd); |
| 6263 | if (!f.file) |
| 6264 | return -EBADF; |
| 6265 | |
| 6266 | ret = -EOPNOTSUPP; |
| 6267 | if (f.file->f_op != &io_uring_fops) |
| 6268 | goto out_fput; |
| 6269 | |
| 6270 | ret = -ENXIO; |
| 6271 | ctx = f.file->private_data; |
| 6272 | if (!percpu_ref_tryget(&ctx->refs)) |
| 6273 | goto out_fput; |
| 6274 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6275 | /* |
| 6276 | * For SQ polling, the thread will do all submissions and completions. |
| 6277 | * Just return the requested submit count, and wake the thread if |
| 6278 | * we were asked to. |
| 6279 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6280 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6281 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 6282 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
| 6283 | io_cqring_overflow_flush(ctx, false); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6284 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 6285 | wake_up(&ctx->sqo_wait); |
| 6286 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 6287 | } else if (to_submit) { |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6288 | struct mm_struct *cur_mm; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6289 | |
Jens Axboe | 44d2827 | 2020-01-16 19:00:24 -0700 | [diff] [blame] | 6290 | if (current->mm != ctx->sqo_mm || |
| 6291 | current_cred() != ctx->creds) { |
| 6292 | ret = -EPERM; |
| 6293 | goto out; |
| 6294 | } |
| 6295 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6296 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 6297 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
| 6298 | cur_mm = ctx->sqo_mm; |
| 6299 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, |
| 6300 | &cur_mm, false); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6301 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6302 | |
| 6303 | if (submitted != to_submit) |
| 6304 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6305 | } |
| 6306 | if (flags & IORING_ENTER_GETEVENTS) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6307 | unsigned nr_events = 0; |
| 6308 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6309 | min_complete = min(min_complete, ctx->cq_entries); |
| 6310 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6311 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6312 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6313 | } else { |
| 6314 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 6315 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6316 | } |
| 6317 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 6318 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 6319 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6320 | out_fput: |
| 6321 | fdput(f); |
| 6322 | return submitted ? submitted : ret; |
| 6323 | } |
| 6324 | |
| 6325 | static const struct file_operations io_uring_fops = { |
| 6326 | .release = io_uring_release, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 6327 | .flush = io_uring_flush, |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6328 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 6329 | #ifndef CONFIG_MMU |
| 6330 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 6331 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 6332 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6333 | .poll = io_uring_poll, |
| 6334 | .fasync = io_uring_fasync, |
| 6335 | }; |
| 6336 | |
| 6337 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 6338 | struct io_uring_params *p) |
| 6339 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6340 | struct io_rings *rings; |
| 6341 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6342 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6343 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 6344 | if (size == SIZE_MAX) |
| 6345 | return -EOVERFLOW; |
| 6346 | |
| 6347 | rings = io_mem_alloc(size); |
| 6348 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6349 | return -ENOMEM; |
| 6350 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6351 | ctx->rings = rings; |
| 6352 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 6353 | rings->sq_ring_mask = p->sq_entries - 1; |
| 6354 | rings->cq_ring_mask = p->cq_entries - 1; |
| 6355 | rings->sq_ring_entries = p->sq_entries; |
| 6356 | rings->cq_ring_entries = p->cq_entries; |
| 6357 | ctx->sq_mask = rings->sq_ring_mask; |
| 6358 | ctx->cq_mask = rings->cq_ring_mask; |
| 6359 | ctx->sq_entries = rings->sq_ring_entries; |
| 6360 | ctx->cq_entries = rings->cq_ring_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6361 | |
| 6362 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6363 | if (size == SIZE_MAX) { |
| 6364 | io_mem_free(ctx->rings); |
| 6365 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6366 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6367 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6368 | |
| 6369 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6370 | if (!ctx->sq_sqes) { |
| 6371 | io_mem_free(ctx->rings); |
| 6372 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6373 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 6374 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6375 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6376 | return 0; |
| 6377 | } |
| 6378 | |
| 6379 | /* |
| 6380 | * Allocate an anonymous fd, this is what constitutes the application |
| 6381 | * visible backing of an io_uring instance. The application mmaps this |
| 6382 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 6383 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 6384 | */ |
| 6385 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 6386 | { |
| 6387 | struct file *file; |
| 6388 | int ret; |
| 6389 | |
| 6390 | #if defined(CONFIG_UNIX) |
| 6391 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 6392 | &ctx->ring_sock); |
| 6393 | if (ret) |
| 6394 | return ret; |
| 6395 | #endif |
| 6396 | |
| 6397 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 6398 | if (ret < 0) |
| 6399 | goto err; |
| 6400 | |
| 6401 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 6402 | O_RDWR | O_CLOEXEC); |
| 6403 | if (IS_ERR(file)) { |
| 6404 | put_unused_fd(ret); |
| 6405 | ret = PTR_ERR(file); |
| 6406 | goto err; |
| 6407 | } |
| 6408 | |
| 6409 | #if defined(CONFIG_UNIX) |
| 6410 | ctx->ring_sock->file = file; |
| 6411 | #endif |
| 6412 | fd_install(ret, file); |
| 6413 | return ret; |
| 6414 | err: |
| 6415 | #if defined(CONFIG_UNIX) |
| 6416 | sock_release(ctx->ring_sock); |
| 6417 | ctx->ring_sock = NULL; |
| 6418 | #endif |
| 6419 | return ret; |
| 6420 | } |
| 6421 | |
| 6422 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 6423 | { |
| 6424 | struct user_struct *user = NULL; |
| 6425 | struct io_ring_ctx *ctx; |
| 6426 | bool account_mem; |
| 6427 | int ret; |
| 6428 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6429 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6430 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6431 | if (entries > IORING_MAX_ENTRIES) { |
| 6432 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6433 | return -EINVAL; |
| 6434 | entries = IORING_MAX_ENTRIES; |
| 6435 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6436 | |
| 6437 | /* |
| 6438 | * Use twice as many entries for the CQ ring. It's possible for the |
| 6439 | * application to drive a higher depth than the size of the SQ ring, |
| 6440 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6441 | * some flexibility in overcommitting a bit. If the application has |
| 6442 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 6443 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6444 | */ |
| 6445 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6446 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 6447 | /* |
| 6448 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 6449 | * to a power-of-two, if it isn't already. We do NOT impose |
| 6450 | * any cq vs sq ring sizing. |
| 6451 | */ |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6452 | if (p->cq_entries < p->sq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6453 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6454 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 6455 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 6456 | return -EINVAL; |
| 6457 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 6458 | } |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 6459 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 6460 | } else { |
| 6461 | p->cq_entries = 2 * p->sq_entries; |
| 6462 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6463 | |
| 6464 | user = get_uid(current_user()); |
| 6465 | account_mem = !capable(CAP_IPC_LOCK); |
| 6466 | |
| 6467 | if (account_mem) { |
| 6468 | ret = io_account_mem(user, |
| 6469 | ring_pages(p->sq_entries, p->cq_entries)); |
| 6470 | if (ret) { |
| 6471 | free_uid(user); |
| 6472 | return ret; |
| 6473 | } |
| 6474 | } |
| 6475 | |
| 6476 | ctx = io_ring_ctx_alloc(p); |
| 6477 | if (!ctx) { |
| 6478 | if (account_mem) |
| 6479 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 6480 | p->cq_entries)); |
| 6481 | free_uid(user); |
| 6482 | return -ENOMEM; |
| 6483 | } |
| 6484 | ctx->compat = in_compat_syscall(); |
| 6485 | ctx->account_mem = account_mem; |
| 6486 | ctx->user = user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 6487 | ctx->creds = get_current_cred(); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6488 | |
| 6489 | ret = io_allocate_scq_urings(ctx, p); |
| 6490 | if (ret) |
| 6491 | goto err; |
| 6492 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6493 | ret = io_sq_offload_start(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6494 | if (ret) |
| 6495 | goto err; |
| 6496 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6497 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6498 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 6499 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 6500 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 6501 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 6502 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 6503 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 6504 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6505 | |
| 6506 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 6507 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 6508 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 6509 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 6510 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 6511 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 6512 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 6513 | |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 6514 | /* |
| 6515 | * Install ring fd as the very last thing, so we don't risk someone |
| 6516 | * having closed it before we finish setup |
| 6517 | */ |
| 6518 | ret = io_uring_get_fd(ctx); |
| 6519 | if (ret < 0) |
| 6520 | goto err; |
| 6521 | |
Jens Axboe | da8c969 | 2019-12-02 18:51:26 -0700 | [diff] [blame] | 6522 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 6523 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS; |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6524 | 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] | 6525 | return ret; |
| 6526 | err: |
| 6527 | io_ring_ctx_wait_and_kill(ctx); |
| 6528 | return ret; |
| 6529 | } |
| 6530 | |
| 6531 | /* |
| 6532 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 6533 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 6534 | * params structure passed in. |
| 6535 | */ |
| 6536 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 6537 | { |
| 6538 | struct io_uring_params p; |
| 6539 | long ret; |
| 6540 | int i; |
| 6541 | |
| 6542 | if (copy_from_user(&p, params, sizeof(p))) |
| 6543 | return -EFAULT; |
| 6544 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 6545 | if (p.resv[i]) |
| 6546 | return -EINVAL; |
| 6547 | } |
| 6548 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 6549 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 6550 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
| 6551 | IORING_SETUP_CLAMP)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6552 | return -EINVAL; |
| 6553 | |
| 6554 | ret = io_uring_create(entries, &p); |
| 6555 | if (ret < 0) |
| 6556 | return ret; |
| 6557 | |
| 6558 | if (copy_to_user(params, &p, sizeof(p))) |
| 6559 | return -EFAULT; |
| 6560 | |
| 6561 | return ret; |
| 6562 | } |
| 6563 | |
| 6564 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 6565 | struct io_uring_params __user *, params) |
| 6566 | { |
| 6567 | return io_uring_setup(entries, params); |
| 6568 | } |
| 6569 | |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 6570 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
| 6571 | { |
| 6572 | struct io_uring_probe *p; |
| 6573 | size_t size; |
| 6574 | int i, ret; |
| 6575 | |
| 6576 | size = struct_size(p, ops, nr_args); |
| 6577 | if (size == SIZE_MAX) |
| 6578 | return -EOVERFLOW; |
| 6579 | p = kzalloc(size, GFP_KERNEL); |
| 6580 | if (!p) |
| 6581 | return -ENOMEM; |
| 6582 | |
| 6583 | ret = -EFAULT; |
| 6584 | if (copy_from_user(p, arg, size)) |
| 6585 | goto out; |
| 6586 | ret = -EINVAL; |
| 6587 | if (memchr_inv(p, 0, size)) |
| 6588 | goto out; |
| 6589 | |
| 6590 | p->last_op = IORING_OP_LAST - 1; |
| 6591 | if (nr_args > IORING_OP_LAST) |
| 6592 | nr_args = IORING_OP_LAST; |
| 6593 | |
| 6594 | for (i = 0; i < nr_args; i++) { |
| 6595 | p->ops[i].op = i; |
| 6596 | if (!io_op_defs[i].not_supported) |
| 6597 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 6598 | } |
| 6599 | p->ops_len = i; |
| 6600 | |
| 6601 | ret = 0; |
| 6602 | if (copy_to_user(arg, p, size)) |
| 6603 | ret = -EFAULT; |
| 6604 | out: |
| 6605 | kfree(p); |
| 6606 | return ret; |
| 6607 | } |
| 6608 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6609 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 6610 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 6611 | __releases(ctx->uring_lock) |
| 6612 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6613 | { |
| 6614 | int ret; |
| 6615 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 6616 | /* |
| 6617 | * We're inside the ring mutex, if the ref is already dying, then |
| 6618 | * someone else killed the ctx or is already going through |
| 6619 | * io_uring_register(). |
| 6620 | */ |
| 6621 | if (percpu_ref_is_dying(&ctx->refs)) |
| 6622 | return -ENXIO; |
| 6623 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6624 | if (opcode != IORING_UNREGISTER_FILES && |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 6625 | opcode != IORING_REGISTER_FILES_UPDATE && |
| 6626 | opcode != IORING_REGISTER_PROBE) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6627 | percpu_ref_kill(&ctx->refs); |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 6628 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6629 | /* |
| 6630 | * Drop uring mutex before waiting for references to exit. If |
| 6631 | * another thread is currently inside io_uring_enter() it might |
| 6632 | * need to grab the uring_lock to make progress. If we hold it |
| 6633 | * here across the drain wait, then we can deadlock. It's safe |
| 6634 | * to drop the mutex here, since no new references will come in |
| 6635 | * after we've killed the percpu ref. |
| 6636 | */ |
| 6637 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6638 | ret = wait_for_completion_interruptible(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6639 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6640 | if (ret) { |
| 6641 | percpu_ref_resurrect(&ctx->refs); |
| 6642 | ret = -EINTR; |
| 6643 | goto out; |
| 6644 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6645 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6646 | |
| 6647 | switch (opcode) { |
| 6648 | case IORING_REGISTER_BUFFERS: |
| 6649 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 6650 | break; |
| 6651 | case IORING_UNREGISTER_BUFFERS: |
| 6652 | ret = -EINVAL; |
| 6653 | if (arg || nr_args) |
| 6654 | break; |
| 6655 | ret = io_sqe_buffer_unregister(ctx); |
| 6656 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 6657 | case IORING_REGISTER_FILES: |
| 6658 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 6659 | break; |
| 6660 | case IORING_UNREGISTER_FILES: |
| 6661 | ret = -EINVAL; |
| 6662 | if (arg || nr_args) |
| 6663 | break; |
| 6664 | ret = io_sqe_files_unregister(ctx); |
| 6665 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 6666 | case IORING_REGISTER_FILES_UPDATE: |
| 6667 | ret = io_sqe_files_update(ctx, arg, nr_args); |
| 6668 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6669 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 6670 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6671 | ret = -EINVAL; |
| 6672 | if (nr_args != 1) |
| 6673 | break; |
| 6674 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 6675 | if (ret) |
| 6676 | break; |
| 6677 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 6678 | ctx->eventfd_async = 1; |
| 6679 | else |
| 6680 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 6681 | break; |
| 6682 | case IORING_UNREGISTER_EVENTFD: |
| 6683 | ret = -EINVAL; |
| 6684 | if (arg || nr_args) |
| 6685 | break; |
| 6686 | ret = io_eventfd_unregister(ctx); |
| 6687 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 6688 | case IORING_REGISTER_PROBE: |
| 6689 | ret = -EINVAL; |
| 6690 | if (!arg || nr_args > 256) |
| 6691 | break; |
| 6692 | ret = io_probe(ctx, arg, nr_args); |
| 6693 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6694 | default: |
| 6695 | ret = -EINVAL; |
| 6696 | break; |
| 6697 | } |
| 6698 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6699 | |
| 6700 | if (opcode != IORING_UNREGISTER_FILES && |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 6701 | opcode != IORING_REGISTER_FILES_UPDATE && |
| 6702 | opcode != IORING_REGISTER_PROBE) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6703 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6704 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | c150368 | 2020-01-08 08:26:07 -0700 | [diff] [blame] | 6705 | out: |
| 6706 | reinit_completion(&ctx->completions[0]); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6707 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6708 | return ret; |
| 6709 | } |
| 6710 | |
| 6711 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 6712 | void __user *, arg, unsigned int, nr_args) |
| 6713 | { |
| 6714 | struct io_ring_ctx *ctx; |
| 6715 | long ret = -EBADF; |
| 6716 | struct fd f; |
| 6717 | |
| 6718 | f = fdget(fd); |
| 6719 | if (!f.file) |
| 6720 | return -EBADF; |
| 6721 | |
| 6722 | ret = -EOPNOTSUPP; |
| 6723 | if (f.file->f_op != &io_uring_fops) |
| 6724 | goto out_fput; |
| 6725 | |
| 6726 | ctx = f.file->private_data; |
| 6727 | |
| 6728 | mutex_lock(&ctx->uring_lock); |
| 6729 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 6730 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 6731 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 6732 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6733 | out_fput: |
| 6734 | fdput(f); |
| 6735 | return ret; |
| 6736 | } |
| 6737 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6738 | static int __init io_uring_init(void) |
| 6739 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 6740 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6741 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 6742 | return 0; |
| 6743 | }; |
| 6744 | __initcall(io_uring_init); |